Changeset 0531b5d


Ignore:
Timestamp:
Mar 9, 2016, 3:11:25 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:
89173242
Parents:
d1caa6c
Message:

Fixed ScopedMap?'s const find, refactored InstantionMap? to use ScopedMap?

Location:
src/GenPoly
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    rd1caa6c r0531b5d  
    115115                        /// Wraps value for a specific (Key, TypeList) combination
    116116                        typedef std::pair< TypeList, Value* > Instantiation;
    117                         /// Map of keys to instantiations of them
    118                         typedef std::map< Key*, std::vector< Instantiation > > Scope;
    119 
    120                         std::vector< Scope > scopes;  ///< list of scopes, from outermost to innermost
     117                        /// List of TypeLists paired with their appropriate values
     118                        typedef std::vector< Instantiation > ValueList;
     119                        /// Underlying map type; maps keys to a linear list of corresponding TypeLists and values
     120                        typedef ScopedMap< Key*, std::vector< Instantiation > > InnerMap;
     121
     122                        InnerMap instantiations;  ///< instantiations
    121123
    122124                public:
    123125                        /// Starts a new scope
    124                         void beginScope() {
    125                                 Scope scope;
    126                                 scopes.push_back(scope);
    127                         }
     126                        void beginScope() { instantiations.beginScope(); }
    128127
    129128                        /// Ends a scope
    130                         void endScope() {
    131                                 scopes.pop_back();
    132                         }
    133 
    134                         /// Default constructor initializes with one scope
    135                         InstantiationMap() { beginScope(); }
     129                        void endScope() { instantiations.endScope(); }
    136130
    137131                        /// 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                                 // scan scopes from innermost out
    142                                 for ( typename std::vector< Scope >::const_reverse_iterator scope = scopes.rbegin(); scope != scopes.rend(); ++scope ) {
    143                                         // skip scope if nothing for this key
    144                                         typename Scope::const_iterator insts = scope->find( key );
    145                                         if ( insts == scope->end() ) continue;
    146                                         // look through instantiations for matches to typeList
    147                                         for ( typename std::vector< Instantiation >::const_iterator inst = insts->second.begin(); inst != insts->second.end(); ++inst ) {
     132                        Value *lookup( Key *key, const std::list< TypeExpr* >& params ) const {
     133                                TypeList typeList( params );
     134                               
     135                                // scan scopes for matches to the key
     136                                for ( typename InnerMap::const_iterator insts = instantiations.find( key ); insts != instantiations.end(); insts = instantiations.findNext( insts, key ) ) {
     137                                        for ( typename ValueList::const_reverse_iterator inst = insts->second.rbegin(); inst != insts->second.rend(); ++inst ) {
    148138                                                if ( inst->first == typeList ) return inst->second;
    149139                                        }
    150140                                }
    151                                 // no matching instantiation found
     141                                // no matching instantiations found
    152142                                return 0;
    153143                        }
     
    155145                        /// Adds a value for a (key, typeList) pair to the current scope
    156146                        void insert( Key *key, const std::list< TypeExpr* > &params, Value *value ) {
    157                                 scopes.back()[key].push_back( Instantiation( TypeList( params ), value ) );
     147                                instantiations[ key ].push_back( Instantiation( TypeList( params ), value ) );
    158148                        }
    159149                };
  • src/GenPoly/ScopedMap.h

    rd1caa6c r0531b5d  
    1717#define _SCOPEDMAP_H
    1818
     19#include <cassert>
    1920#include <iterator>
    2021#include <map>
     
    164165                void endScope() {
    165166                        scopes.pop_back();
     167                        assert( ! scopes.empty() );
    166168                }
    167169
     
    188190                        return end();
    189191                }
    190                 const_iterator find( const Key &key ) const { return const_iterator( find( key ) ); }
     192                const_iterator find( const Key &key ) const {
     193                                return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) );
     194                }
    191195               
    192196                /// Finds the given key in the outermost scope inside the given scope where it occurs
     
    200204                        return end();
    201205                }
    202                 const_iterator findNext( const_iterator &it, const Key &key ) const { return const_iterator( findNext( it, key ) ); }
     206                const_iterator findNext( const_iterator &it, const Key &key ) const {
     207                                return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) );
     208                }
    203209
    204210                /// Inserts the given key-value pair into the outermost scope
     
    208214                }
    209215                std::pair< iterator, bool > insert( const Key &key, const Value &value ) { return insert( std::make_pair( key, value ) ); }
    210                
     216
     217                Value& operator[] ( const Key &key ) {
     218                        iterator slot = find( key );
     219                        if ( slot != end() ) return slot->second;
     220                        return insert( key, Value() ).first->second;
     221                }
    211222        };
    212223} // namespace GenPoly
Note: See TracChangeset for help on using the changeset viewer.