Changeset d1caa6c


Ignore:
Timestamp:
Mar 9, 2016, 12:30:14 PM (8 years ago)
Author:
Aaron Moss <a3moss@…>
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
Message:

Refactor ConcreteType?, InstantiationMap? to be more generically useful

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    rac633d0 rd1caa6c  
    6262                FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
    6363
    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 ) ) {}
    7172
    7273                        /// 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() {
    7475                                for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) {
    7576                                        params.push_back( (*param)->get_type()->clone() );
     
    7778                        }
    7879
    79                         ConcreteType& operator= (const ConcreteType& that) {
     80                        TypeList& operator= ( const TypeList &that ) {
    8081                                deleteAll( params );
     82
    8183                                params.clear();
    82 
    83                                 base = that.base;
    8484                                cloneAll( that.params, params );
    8585
     
    8787                        }
    8888
    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;
    93101
    94102                                SymTab::Indexer dummy;
    95                                 if ( params.size() != that.params.size() ) return false;
    96103                                for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) {
    97104                                        if ( ! ResolvExpr::typesCompatible( *it, *jt, dummy ) ) return false;
     
    100107                        }
    101108
    102                         AggregateDecl *base;        ///< Base generic type
    103109                        std::list< Type* > params;  ///< Instantiation parameters
    104110                };
    105111
    106                 /// Maps a concrete type to the some value, accounting for scope
    107                 template< typename Value >
     112                /// Maps a key and a TypeList to the some value, accounting for scope
     113                template< typename Key, typename Value >
    108114                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;
    119119
    120120                        std::vector< Scope > scopes;  ///< list of scopes, from outermost to innermost
     
    135135                        InstantiationMap() { beginScope(); }
    136136
    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                               
    141141                                // scan scopes from innermost out
    142142                                for ( typename std::vector< Scope >::const_reverse_iterator scope = scopes.rbegin(); scope != scopes.rend(); ++scope ) {
    143                                         // skip scope if no instantiations of this generic type
    144                                         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 );
    145145                                        if ( insts == scope->end() ) continue;
    146                                         // look through instantiations for matches to concrete type
     146                                        // look through instantiations for matches to typeList
    147147                                        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;
    149149                                        }
    150150                                }
     
    153153                        }
    154154
    155                         /// Adds a value for a concrete type to the current scope
    156                         void insert( AggregateDecl *generic, const std::list< TypeExpr* > &params, 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* > &params, Value *value ) {
     157                                scopes.back()[key].push_back( Instantiation( TypeList( params ), value ) );
    159158                        }
    160159                };
     
    245244                class GenericInstantiator : public DeclMutator {
    246245                        /// Map of (generic type, parameter list) pairs to concrete type instantiations
    247                         InstantiationMap< AggregateDecl > instantiations;
     246                        InstantiationMap< AggregateDecl, AggregateDecl > instantiations;
    248247                        /// Namer for concrete types
    249248                        UniqueName typeNamer;
Note: See TracChangeset for help on using the changeset viewer.