Changeset d1caa6c
- Timestamp:
- Mar 9, 2016, 12:30:14 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 0531b5d
- Parents:
- ac633d0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
rac633d0 rd1caa6c 62 62 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ); 63 63 64 /// Key for a unique concrete type; generic base type paired with type parameter list 65 struct ConcreteType { 66 ConcreteType() : base(NULL), params() {} 67 68 ConcreteType(AggregateDecl *_base, const std::list< Type* >& _params) : base(_base), params() { cloneAll(_params, params); } 69 70 ConcreteType(const ConcreteType& that) : base(that.base), params() { cloneAll(that.params, params); } 64 /// Abstracts type equality for a list of parameter types 65 struct TypeList { 66 TypeList() : params() {} 67 TypeList( const std::list< Type* > &_params ) : params() { cloneAll(_params, params); } 68 TypeList( std::list< Type* > &&_params ) : params( _params ) {} 69 70 TypeList( const TypeList &that ) : params() { cloneAll(that.params, params); } 71 TypeList( TypeList &&that ) : params( std::move( that.params ) ) {} 71 72 72 73 /// Extracts types from a list of TypeExpr* 73 ConcreteType(AggregateDecl *_base, const std::list< TypeExpr* >& _params) : base(_base),params() {74 TypeList( const std::list< TypeExpr* >& _params ) : params() { 74 75 for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) { 75 76 params.push_back( (*param)->get_type()->clone() ); … … 77 78 } 78 79 79 ConcreteType& operator= (const ConcreteType& that) {80 TypeList& operator= ( const TypeList &that ) { 80 81 deleteAll( params ); 82 81 83 params.clear(); 82 83 base = that.base;84 84 cloneAll( that.params, params ); 85 85 … … 87 87 } 88 88 89 ~ConcreteType() { deleteAll( params ); } 90 91 bool operator== (const ConcreteType& that) const { 92 if ( base != that.base ) return false; 89 TypeList& operator= ( TypeList &&that ) { 90 deleteAll( params ); 91 92 params = std::move( that.params ); 93 94 return *this; 95 } 96 97 ~TypeList() { deleteAll( params ); } 98 99 bool operator== ( const TypeList& that ) const { 100 if ( params.size() != that.params.size() ) return false; 93 101 94 102 SymTab::Indexer dummy; 95 if ( params.size() != that.params.size() ) return false;96 103 for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) { 97 104 if ( ! ResolvExpr::typesCompatible( *it, *jt, dummy ) ) return false; … … 100 107 } 101 108 102 AggregateDecl *base; ///< Base generic type103 109 std::list< Type* > params; ///< Instantiation parameters 104 110 }; 105 111 106 /// Maps a concrete typeto the some value, accounting for scope107 template< typename Value >112 /// Maps a key and a TypeList to the some value, accounting for scope 113 template< typename Key, typename Value > 108 114 class InstantiationMap { 109 /// Information about a specific instantiation of a generic type 110 struct Instantiation { 111 ConcreteType key; ///< Instantiation parameters for this type 112 Value *value; ///< Value for this instantiation 113 114 Instantiation() : key(), value(0) {} 115 Instantiation(const ConcreteType &_key, Value *_value) : key(_key), value(_value) {} 116 }; 117 /// Map of generic types to instantiations of them 118 typedef std::map< AggregateDecl*, std::vector< Instantiation > > Scope; 115 /// Wraps value for a specific (Key, TypeList) combination 116 typedef std::pair< TypeList, Value* > Instantiation; 117 /// Map of keys to instantiations of them 118 typedef std::map< Key*, std::vector< Instantiation > > Scope; 119 119 120 120 std::vector< Scope > scopes; ///< list of scopes, from outermost to innermost … … 135 135 InstantiationMap() { beginScope(); } 136 136 137 /// Gets the value for the concrete instantiation of this type, assuming it has already been instantiated in the current scope.138 /// Returns NULL on none such.139 Value *lookup( AggregateDecl *generic, const std::list< TypeExpr* >& params ) {140 ConcreteType key(generic, params);137 /// Gets the value for the (key, typeList) pair, returns NULL on none such. 138 Value *lookup( Key *key, const std::list< TypeExpr* >& params ) { 139 TypeList typeList( params ); 140 141 141 // scan scopes from innermost out 142 142 for ( typename std::vector< Scope >::const_reverse_iterator scope = scopes.rbegin(); scope != scopes.rend(); ++scope ) { 143 // skip scope if no instantiations of this generic type144 typename Scope::const_iterator insts = scope->find( generic);143 // skip scope if nothing for this key 144 typename Scope::const_iterator insts = scope->find( key ); 145 145 if ( insts == scope->end() ) continue; 146 // look through instantiations for matches to concrete type146 // look through instantiations for matches to typeList 147 147 for ( typename std::vector< Instantiation >::const_iterator inst = insts->second.begin(); inst != insts->second.end(); ++inst ) { 148 if ( inst-> key == key ) return inst->value;148 if ( inst->first == typeList ) return inst->second; 149 149 } 150 150 } … … 153 153 } 154 154 155 /// Adds a value for a concrete type to the current scope 156 void insert( AggregateDecl *generic, const std::list< TypeExpr* > ¶ms, Value *value ) { 157 ConcreteType key(generic, params); 158 scopes.back()[generic].push_back( Instantiation( key, value ) ); 155 /// Adds a value for a (key, typeList) pair to the current scope 156 void insert( Key *key, const std::list< TypeExpr* > ¶ms, Value *value ) { 157 scopes.back()[key].push_back( Instantiation( TypeList( params ), value ) ); 159 158 } 160 159 }; … … 245 244 class GenericInstantiator : public DeclMutator { 246 245 /// Map of (generic type, parameter list) pairs to concrete type instantiations 247 InstantiationMap< AggregateDecl > instantiations;246 InstantiationMap< AggregateDecl, AggregateDecl > instantiations; 248 247 /// Namer for concrete types 249 248 UniqueName typeNamer;
Note: See TracChangeset
for help on using the changeset viewer.