Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Indexer.cc

    r743fbda r6a57da5  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // Indexer.cc --
     7// Indexer.cc -- 
    88//
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 21:37:33 2015
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Fri Apr 22 15:25:43 2016
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Wed Mar  2 17:31:29 2016
    1313// Update Count     : 11
    1414//
     
    5959                }
    6060        }
    61 
     61       
    6262        template< typename Decl >
    6363        void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) {
     
    6666                } // for
    6767        }
    68 
     68       
    6969        struct Indexer::Impl {
    7070                Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(),
     
    7676                unsigned long size;       ///< Number of elements stored in this table
    7777                const Indexer base;       ///< Base indexer this extends
    78 
     78               
    7979                IdTable idTable;          ///< Identifier namespace
    8080                TypeTable typeTable;      ///< Type namespace
     
    213213        void Indexer::visit( StructDecl *aggregateDecl ) {
    214214                // make up a forward declaration and add it before processing the members
    215                 // needs to be on the heap because addStruct saves the pointer
    216                 StructDecl &fwdDecl = *new StructDecl( aggregateDecl->get_name() );
     215                StructDecl fwdDecl( aggregateDecl->get_name() );
    217216                cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
    218217                debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl );
    219218                addStruct( &fwdDecl );
    220 
     219 
    221220                enterScope();
    222221                acceptAll( aggregateDecl->get_parameters(), *this );
    223222                acceptAll( aggregateDecl->get_members(), *this );
    224223                leaveScope();
    225 
     224 
    226225                debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl );
    227226                // this addition replaces the forward declaration
     
    235234                debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl );
    236235                addUnion( &fwdDecl );
    237 
     236 
    238237                enterScope();
    239238                acceptAll( aggregateDecl->get_parameters(), *this );
    240239                acceptAll( aggregateDecl->get_members(), *this );
    241240                leaveScope();
    242 
     241 
    243242                debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl );
    244243                addUnion( aggregateDecl );
     
    257256                acceptAll( aggregateDecl->get_members(), *this );
    258257                leaveScope();
    259 
     258 
    260259                debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl );
    261260                addTrait( aggregateDecl );
     
    345344                maybeAccept( offsetofExpr->get_type(), *this );
    346345                maybeAccept( offsetofExpr->get_member(), *this );
    347         }
    348 
    349         void Indexer::visit( OffsetPackExpr *offsetPackExpr ) {
    350                 acceptAllNewScope( offsetPackExpr->get_results(), *this );
    351                 maybeAccept( offsetPackExpr->get_type(), *this );
    352346        }
    353347
     
    439433        }
    440434
    441 
     435       
    442436
    443437        void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const {
    444438                std::unordered_set< std::string > foundMangleNames;
    445 
     439               
    446440                Indexer::Impl *searchTables = tables;
    447441                while ( searchTables ) {
     
    453447                                        // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found
    454448                                        if ( foundMangleNames.insert( decl->first ).second == false ) continue;
    455 
     449                                       
    456450                                        out.push_back( decl->second );
    457451                                }
    458452                        }
    459 
     453                       
    460454                        // get declarations from base indexers
    461455                        searchTables = searchTables->base.tables;
     
    512506        }
    513507
    514         bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
     508        bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName ) const {
    515509                if ( ! tables ) return false;
    516                 if ( tables->scope < scope ) return false;
    517510
    518511                IdTable::const_iterator decls = tables->idTable.find( id );
     
    520513                        const MangleTable &mangleTable = decls->second;
    521514                        for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
    522                                 // check for C decls with the same name, skipping
     515                                // check for C decls with the same name, skipping 
    523516                                // those with a compatible type (by mangleName)
    524517                                if ( decl->second->get_linkage() == LinkageSpec::C && decl->first != mangleName ) return true;
     
    526519                }
    527520
    528                 return tables->base.hasIncompatibleCDecl( id, mangleName, scope );
    529         }
    530 
     521                return tables->base.hasIncompatibleCDecl( id, mangleName );
     522        }
     523       
    531524        NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const {
    532525                if ( ! tables ) return 0;
     
    536529                return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope );
    537530        }
    538 
     531       
    539532        StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const {
    540533                if ( ! tables ) return 0;
     
    544537                return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope );
    545538        }
    546 
     539       
    547540        EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const {
    548541                if ( ! tables ) return 0;
     
    552545                return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope );
    553546        }
    554 
     547       
    555548        UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const {
    556549                if ( ! tables ) return 0;
     
    560553                return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope );
    561554        }
    562 
     555       
    563556        TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const {
    564557                if ( ! tables ) return 0;
     
    603596                return true;
    604597        }
    605 
     598       
    606599        void Indexer::addId( DeclarationWithType *decl ) {
    607600                makeWritable();
     
    619612                DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope );
    620613                if ( ! existing || ! addedIdConflicts( existing, decl ) ) {
    621                         // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage
    622                         if ( decl->get_linkage() == LinkageSpec::C && hasIncompatibleCDecl( name, mangleName, scope ) ) {
     614                        // this ensures that no two declarations with the same unmangled name both have C linkage
     615                        if ( decl->get_linkage() == LinkageSpec::C && hasIncompatibleCDecl( name, mangleName ) ) {
    623616                                throw SemanticError( "invalid overload of C function ", decl );
    624                         } // NOTE this is broken in Richard's original code in such a way that it never triggers (it
    625                           // doesn't check decls that have the same manglename, and all C-linkage decls are defined to
     617                        } // NOTE this is broken in Richard's original code in such a way that it never triggers (it 
     618                          // doesn't check decls that have the same manglename, and all C-linkage decls are defined to 
    626619                          // have their name as their manglename, hence the error can never trigger).
    627                           // The code here is closer to correct, but name mangling would have to be completely
     620                          // The code here is closer to correct, but name mangling would have to be completely 
    628621                          // isomorphic to C type-compatibility, which it may not be.
    629 
     622                       
    630623                        tables->idTable[ name ][ mangleName ] = decl;
    631624                        ++tables->size;
     
    642635                }
    643636        }
    644 
     637       
    645638        void Indexer::addType( NamedTypeDecl *decl ) {
    646639                makeWritable();
     
    673666                addStruct( new StructDecl( id ) );
    674667        }
    675 
     668       
    676669        void Indexer::addStruct( StructDecl *decl ) {
    677670                makeWritable();
     
    691684                }
    692685        }
    693 
     686       
    694687        void Indexer::addEnum( EnumDecl *decl ) {
    695688                makeWritable();
     
    713706                addUnion( new UnionDecl( id ) );
    714707        }
    715 
     708       
    716709        void Indexer::addUnion( UnionDecl *decl ) {
    717710                makeWritable();
     
    731724                }
    732725        }
    733 
     726       
    734727        void Indexer::addTrait( TraitDecl *decl ) {
    735728                makeWritable();
     
    752745        void Indexer::enterScope() {
    753746                ++scope;
    754 
     747               
    755748                if ( doDebug ) {
    756749                        std::cout << "--- Entering scope " << scope << std::endl;
     
    785778            using std::cerr;
    786779
    787                 if ( tables ) {
    788                         os << "--- scope " << tables->scope << " ---" << std::endl;
    789 
    790                         os << "===idTable===" << std::endl;
    791                         dump( tables->idTable, os );
    792                         os << "===typeTable===" << std::endl;
    793                         dump( tables->typeTable, os );
    794                         os << "===structTable===" << std::endl;
    795                         dump( tables->structTable, os );
    796                         os << "===enumTable===" << std::endl;
    797                         dump( tables->enumTable, os );
    798                         os << "===unionTable===" << std::endl;
    799                         dump( tables->unionTable, os );
    800                         os << "===contextTable===" << std::endl;
    801                         dump( tables->traitTable, os );
    802 
    803                         tables->base.print( os, indent );
    804                 } else {
    805                         os << "--- end ---" << std::endl;
    806                 }
    807 
     780            cerr << "===idTable===" << std::endl;
     781            if ( tables ) dump( tables->idTable, os );
     782            cerr << "===typeTable===" << std::endl;
     783            if ( tables ) dump( tables->typeTable, os );
     784            cerr << "===structTable===" << std::endl;
     785            if ( tables ) dump( tables->structTable, os );
     786            cerr << "===enumTable===" << std::endl;
     787            if ( tables ) dump( tables->enumTable, os );
     788            cerr << "===unionTable===" << std::endl;
     789            if ( tables ) dump( tables->unionTable, os );
     790            cerr << "===contextTable===" << std::endl;
     791            if ( tables ) dump( tables->traitTable, os );
    808792        }
    809793} // namespace SymTab
Note: See TracChangeset for help on using the changeset viewer.