1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // PolyCost.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 09:50:12 2015 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Wed Jun 19 10:45:00 2019 |
---|
13 | // Update Count : 4 |
---|
14 | // |
---|
15 | |
---|
16 | #include "AST/SymbolTable.hpp" |
---|
17 | #include "AST/Type.hpp" |
---|
18 | #include "AST/TypeEnvironment.hpp" |
---|
19 | #include "Common/PassVisitor.h" |
---|
20 | #include "SymTab/Indexer.h" // for Indexer |
---|
21 | #include "SynTree/Type.h" // for TypeInstType, Type |
---|
22 | #include "TypeEnvironment.h" // for EqvClass, TypeEnvironment |
---|
23 | |
---|
24 | namespace ResolvExpr { |
---|
25 | struct PolyCost { |
---|
26 | PolyCost( const TypeEnvironment &env, const SymTab::Indexer &indexer ); |
---|
27 | |
---|
28 | void previsit( TypeInstType * aggregateUseType ); |
---|
29 | int result; |
---|
30 | const TypeEnvironment &tenv; |
---|
31 | const SymTab::Indexer &indexer; |
---|
32 | }; |
---|
33 | |
---|
34 | int polyCost( Type *type, const TypeEnvironment & env, const SymTab::Indexer &indexer ) { |
---|
35 | PassVisitor<PolyCost> coster( env, indexer ); |
---|
36 | type->accept( coster ); |
---|
37 | return coster.pass.result; |
---|
38 | } |
---|
39 | |
---|
40 | PolyCost::PolyCost( const TypeEnvironment & env, const SymTab::Indexer & indexer ) : result( 0 ), tenv( env ), indexer( indexer ) { |
---|
41 | } |
---|
42 | |
---|
43 | void PolyCost::previsit(TypeInstType * typeInst) { |
---|
44 | if ( const EqvClass *eqvClass = tenv.lookup( typeInst->name ) ) { |
---|
45 | if ( eqvClass->type ) { |
---|
46 | if ( TypeInstType * otherTypeInst = dynamic_cast< TypeInstType* >( eqvClass->type ) ) { |
---|
47 | if ( indexer.lookupType( otherTypeInst->name ) ) { |
---|
48 | // bound to opaque type |
---|
49 | result += 1; |
---|
50 | } // if |
---|
51 | } else { |
---|
52 | // bound to concrete type |
---|
53 | result += 1; |
---|
54 | } // if |
---|
55 | } // if |
---|
56 | } // if |
---|
57 | } |
---|
58 | |
---|
59 | // TODO: When the old PolyCost is torn out get rid of the _new suffix. |
---|
60 | struct PolyCost_new { |
---|
61 | int result; |
---|
62 | const ast::SymbolTable &symtab; |
---|
63 | const ast::TypeEnvironment &env_; |
---|
64 | |
---|
65 | PolyCost_new( const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ) : |
---|
66 | result( 0 ), symtab( symtab ), env_( env ) {} |
---|
67 | |
---|
68 | void previsit( const ast::TypeInstType * type ) { |
---|
69 | if ( const ast::EqvClass * eqv = env_.lookup( type->name ) ) /* && */ if ( eqv->bound ) { |
---|
70 | if ( const ast::TypeInstType * otherType = eqv->bound.as< ast::TypeInstType >() ) { |
---|
71 | if ( symtab.lookupType( otherType->name ) ) { |
---|
72 | // Bound to opaque type. |
---|
73 | result += 1; |
---|
74 | } |
---|
75 | } else { |
---|
76 | // Bound to concrete type. |
---|
77 | result += 1; |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | }; |
---|
82 | |
---|
83 | int polyCost( |
---|
84 | const ast::Type * type, const ast::SymbolTable & symtab, const ast::TypeEnvironment & env |
---|
85 | ) { |
---|
86 | ast::Pass<PolyCost_new> costing( symtab, env ); |
---|
87 | type->accept( costing ); |
---|
88 | return costing.pass.result; |
---|
89 | } |
---|
90 | |
---|
91 | } // namespace ResolvExpr |
---|
92 | |
---|
93 | // Local Variables: // |
---|
94 | // tab-width: 4 // |
---|
95 | // mode: c++ // |
---|
96 | // compile-command: "make install" // |
---|
97 | // End: // |
---|