// // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // Indexer.cc -- // // Author : Richard C. Bilson // Created On : Sun May 17 21:37:33 2015 // Last Modified By : Peter A. Buhr // Last Modified On : Wed Mar 2 17:31:29 2016 // Update Count : 11 // #include "Indexer.h" #include #include #include #include #include #include "Mangler.h" #include "Common/utility.h" #include "ResolvExpr/typeops.h" #include "SynTree/Declaration.h" #include "SynTree/Type.h" #include "SynTree/Expression.h" #include "SynTree/Initializer.h" #include "SynTree/Statement.h" #define debugPrint(x) if ( doDebug ) { std::cout << x; } namespace SymTab { template< typename Container, typename VisitorType > inline void acceptAllNewScope( Container &container, VisitorType &visitor ) { visitor.enterScope(); acceptAll( container, visitor ); visitor.leaveScope(); } typedef std::unordered_map< std::string, DeclarationWithType* > MangleTable; typedef std::unordered_map< std::string, MangleTable > IdTable; typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable; typedef std::unordered_map< std::string, StructDecl* > StructTable; typedef std::unordered_map< std::string, EnumDecl* > EnumTable; typedef std::unordered_map< std::string, UnionDecl* > UnionTable; typedef std::unordered_map< std::string, TraitDecl* > TraitTable; void dump( const IdTable &table, std::ostream &os ) { for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) { for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) { os << mangle->second << std::endl; } } } template< typename Decl > void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) { for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) { os << it->second << std::endl; } // for } struct Indexer::Impl { Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(), idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ), idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} unsigned long refCount; ///< Number of references to these tables unsigned long scope; ///< Scope these tables are associated with unsigned long size; ///< Number of elements stored in this table const Indexer base; ///< Base indexer this extends IdTable idTable; ///< Identifier namespace TypeTable typeTable; ///< Type namespace StructTable structTable; ///< Struct namespace EnumTable enumTable; ///< Enum namespace UnionTable unionTable; ///< Union namespace TraitTable traitTable; ///< Trait namespace }; Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) { if ( ! toClone ) return 0; // shorten the search chain by skipping empty links Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone; if ( ret ) { ++ret->refCount; } return ret; } void Indexer::deleteRef( Indexer::Impl *toFree ) { if ( ! toFree ) return; if ( --toFree->refCount == 0 ) delete toFree; } void Indexer::makeWritable() { if ( ! tables ) { // create indexer if not yet set tables = new Indexer::Impl( scope ); } else if ( tables->refCount > 1 || tables->scope != scope ) { // make this indexer the base of a fresh indexer at the current scope tables = new Indexer::Impl( scope, std::move( *this ) ); } } Indexer::Indexer( bool _doDebug ) : tables( 0 ), scope( 0 ), doDebug( _doDebug ) {} Indexer::Indexer( const Indexer &that ) : tables( newRef( that.tables ) ), scope( that.scope ), doDebug( that.doDebug ) {} Indexer::Indexer( Indexer &&that ) : tables( that.tables ), scope( that.scope ), doDebug( that.doDebug ) { that.tables = 0; } Indexer::~Indexer() { deleteRef( tables ); } Indexer& Indexer::operator= ( const Indexer &that ) { deleteRef( tables ); tables = newRef( that.tables ); scope = that.scope; doDebug = that.doDebug; return *this; } Indexer& Indexer::operator= ( Indexer &&that ) { deleteRef( tables ); tables = that.tables; scope = that.scope; doDebug = that.doDebug; that.tables = 0; return *this; } void Indexer::visit( ObjectDecl *objectDecl ) { enterScope(); maybeAccept( objectDecl->get_type(), *this ); leaveScope(); maybeAccept( objectDecl->get_init(), *this ); maybeAccept( objectDecl->get_bitfieldWidth(), *this ); if ( objectDecl->get_name() != "" ) { debugPrint( "Adding object " << objectDecl->get_name() << std::endl ); addId( objectDecl ); } // if } void Indexer::visit( FunctionDecl *functionDecl ) { if ( functionDecl->get_name() == "" ) return; debugPrint( "Adding function " << functionDecl->get_name() << std::endl ); addId( functionDecl ); enterScope(); maybeAccept( functionDecl->get_functionType(), *this ); acceptAll( functionDecl->get_oldDecls(), *this ); maybeAccept( functionDecl->get_statements(), *this ); leaveScope(); } // A NOTE ON THE ORDER OF TRAVERSAL // // Types and typedefs have their base types visited before they are added to the type table. This is ok, since there is // no such thing as a recursive type or typedef. // // typedef struct { T *x; } T; // never allowed // // for structs/unions, it is possible to have recursion, so the decl should be added as if it's incomplete to begin, the // members are traversed, and then the complete type should be added (assuming the type is completed by this particular // declaration). // // struct T { struct T *x; }; // allowed // // It is important to add the complete type to the symbol table *after* the members/base has been traversed, since that // traversal may modify the definition of the type and these modifications should be visible when the symbol table is // queried later in this pass. // // TODO: figure out whether recursive contexts are sensible/possible/reasonable. void Indexer::visit( TypeDecl *typeDecl ) { // see A NOTE ON THE ORDER OF TRAVERSAL, above // note that assertions come after the type is added to the symtab, since they are not part of the type proper // and may depend on the type itself enterScope(); acceptAll( typeDecl->get_parameters(), *this ); maybeAccept( typeDecl->get_base(), *this ); leaveScope(); debugPrint( "Adding type " << typeDecl->get_name() << std::endl ); addType( typeDecl ); acceptAll( typeDecl->get_assertions(), *this ); } void Indexer::visit( TypedefDecl *typeDecl ) { enterScope(); acceptAll( typeDecl->get_parameters(), *this ); maybeAccept( typeDecl->get_base(), *this ); leaveScope(); debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl ); addType( typeDecl ); } void Indexer::visit( StructDecl *aggregateDecl ) { // make up a forward declaration and add it before processing the members StructDecl fwdDecl( aggregateDecl->get_name() ); cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl ); addStruct( &fwdDecl ); enterScope(); acceptAll( aggregateDecl->get_parameters(), *this ); acceptAll( aggregateDecl->get_members(), *this ); leaveScope(); debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl ); // this addition replaces the forward declaration addStruct( aggregateDecl ); } void Indexer::visit( UnionDecl *aggregateDecl ) { // make up a forward declaration and add it before processing the members UnionDecl fwdDecl( aggregateDecl->get_name() ); cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl ); addUnion( &fwdDecl ); enterScope(); acceptAll( aggregateDecl->get_parameters(), *this ); acceptAll( aggregateDecl->get_members(), *this ); leaveScope(); debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl ); addUnion( aggregateDecl ); } void Indexer::visit( EnumDecl *aggregateDecl ) { debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl ); addEnum( aggregateDecl ); // unlike structs, contexts, and unions, enums inject their members into the global scope acceptAll( aggregateDecl->get_members(), *this ); } void Indexer::visit( TraitDecl *aggregateDecl ) { enterScope(); acceptAll( aggregateDecl->get_parameters(), *this ); acceptAll( aggregateDecl->get_members(), *this ); leaveScope(); debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl ); addTrait( aggregateDecl ); } void Indexer::visit( CompoundStmt *compoundStmt ) { enterScope(); acceptAll( compoundStmt->get_kids(), *this ); leaveScope(); } void Indexer::visit( ApplicationExpr *applicationExpr ) { acceptAllNewScope( applicationExpr->get_results(), *this ); maybeAccept( applicationExpr->get_function(), *this ); acceptAll( applicationExpr->get_args(), *this ); } void Indexer::visit( UntypedExpr *untypedExpr ) { acceptAllNewScope( untypedExpr->get_results(), *this ); acceptAll( untypedExpr->get_args(), *this ); } void Indexer::visit( NameExpr *nameExpr ) { acceptAllNewScope( nameExpr->get_results(), *this ); } void Indexer::visit( AddressExpr *addressExpr ) { acceptAllNewScope( addressExpr->get_results(), *this ); maybeAccept( addressExpr->get_arg(), *this ); } void Indexer::visit( LabelAddressExpr *labAddressExpr ) { acceptAllNewScope( labAddressExpr->get_results(), *this ); maybeAccept( labAddressExpr->get_arg(), *this ); } void Indexer::visit( CastExpr *castExpr ) { acceptAllNewScope( castExpr->get_results(), *this ); maybeAccept( castExpr->get_arg(), *this ); } void Indexer::visit( UntypedMemberExpr *memberExpr ) { acceptAllNewScope( memberExpr->get_results(), *this ); maybeAccept( memberExpr->get_aggregate(), *this ); } void Indexer::visit( MemberExpr *memberExpr ) { acceptAllNewScope( memberExpr->get_results(), *this ); maybeAccept( memberExpr->get_aggregate(), *this ); } void Indexer::visit( VariableExpr *variableExpr ) { acceptAllNewScope( variableExpr->get_results(), *this ); } void Indexer::visit( ConstantExpr *constantExpr ) { acceptAllNewScope( constantExpr->get_results(), *this ); maybeAccept( constantExpr->get_constant(), *this ); } void Indexer::visit( SizeofExpr *sizeofExpr ) { acceptAllNewScope( sizeofExpr->get_results(), *this ); if ( sizeofExpr->get_isType() ) { maybeAccept( sizeofExpr->get_type(), *this ); } else { maybeAccept( sizeofExpr->get_expr(), *this ); } } void Indexer::visit( AlignofExpr *alignofExpr ) { acceptAllNewScope( alignofExpr->get_results(), *this ); if ( alignofExpr->get_isType() ) { maybeAccept( alignofExpr->get_type(), *this ); } else { maybeAccept( alignofExpr->get_expr(), *this ); } } void Indexer::visit( UntypedOffsetofExpr *offsetofExpr ) { acceptAllNewScope( offsetofExpr->get_results(), *this ); maybeAccept( offsetofExpr->get_type(), *this ); } void Indexer::visit( OffsetofExpr *offsetofExpr ) { acceptAllNewScope( offsetofExpr->get_results(), *this ); maybeAccept( offsetofExpr->get_type(), *this ); maybeAccept( offsetofExpr->get_member(), *this ); } void Indexer::visit( AttrExpr *attrExpr ) { acceptAllNewScope( attrExpr->get_results(), *this ); if ( attrExpr->get_isType() ) { maybeAccept( attrExpr->get_type(), *this ); } else { maybeAccept( attrExpr->get_expr(), *this ); } } void Indexer::visit( LogicalExpr *logicalExpr ) { acceptAllNewScope( logicalExpr->get_results(), *this ); maybeAccept( logicalExpr->get_arg1(), *this ); maybeAccept( logicalExpr->get_arg2(), *this ); } void Indexer::visit( ConditionalExpr *conditionalExpr ) { acceptAllNewScope( conditionalExpr->get_results(), *this ); maybeAccept( conditionalExpr->get_arg1(), *this ); maybeAccept( conditionalExpr->get_arg2(), *this ); maybeAccept( conditionalExpr->get_arg3(), *this ); } void Indexer::visit( CommaExpr *commaExpr ) { acceptAllNewScope( commaExpr->get_results(), *this ); maybeAccept( commaExpr->get_arg1(), *this ); maybeAccept( commaExpr->get_arg2(), *this ); } void Indexer::visit( TupleExpr *tupleExpr ) { acceptAllNewScope( tupleExpr->get_results(), *this ); acceptAll( tupleExpr->get_exprs(), *this ); } void Indexer::visit( SolvedTupleExpr *tupleExpr ) { acceptAllNewScope( tupleExpr->get_results(), *this ); acceptAll( tupleExpr->get_exprs(), *this ); } void Indexer::visit( TypeExpr *typeExpr ) { acceptAllNewScope( typeExpr->get_results(), *this ); maybeAccept( typeExpr->get_type(), *this ); } void Indexer::visit( AsmExpr *asmExpr ) { maybeAccept( asmExpr->get_inout(), *this ); maybeAccept( asmExpr->get_constraint(), *this ); maybeAccept( asmExpr->get_operand(), *this ); } void Indexer::visit( UntypedValofExpr *valofExpr ) { acceptAllNewScope( valofExpr->get_results(), *this ); maybeAccept( valofExpr->get_body(), *this ); } void Indexer::visit( TraitInstType *contextInst ) { acceptAll( contextInst->get_parameters(), *this ); acceptAll( contextInst->get_members(), *this ); } void Indexer::visit( StructInstType *structInst ) { if ( ! lookupStruct( structInst->get_name() ) ) { debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl ); addStruct( structInst->get_name() ); } enterScope(); acceptAll( structInst->get_parameters(), *this ); leaveScope(); } void Indexer::visit( UnionInstType *unionInst ) { if ( ! lookupUnion( unionInst->get_name() ) ) { debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl ); addUnion( unionInst->get_name() ); } enterScope(); acceptAll( unionInst->get_parameters(), *this ); leaveScope(); } void Indexer::visit( ForStmt *forStmt ) { // for statements introduce a level of scope enterScope(); Visitor::visit( forStmt ); leaveScope(); } void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const { std::unordered_set< std::string > foundMangleNames; Indexer::Impl *searchTables = tables; while ( searchTables ) { IdTable::const_iterator decls = searchTables->idTable.find( id ); if ( decls != searchTables->idTable.end() ) { const MangleTable &mangleTable = decls->second; for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found if ( foundMangleNames.insert( decl->first ).second == false ) continue; out.push_back( decl->second ); } } // get declarations from base indexers searchTables = searchTables->base.tables; } } NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { if ( ! tables ) return 0; TypeTable::const_iterator ret = tables->typeTable.find( id ); return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id ); } StructDecl *Indexer::lookupStruct( const std::string &id ) const { if ( ! tables ) return 0; StructTable::const_iterator ret = tables->structTable.find( id ); return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id ); } EnumDecl *Indexer::lookupEnum( const std::string &id ) const { if ( ! tables ) return 0; EnumTable::const_iterator ret = tables->enumTable.find( id ); return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id ); } UnionDecl *Indexer::lookupUnion( const std::string &id ) const { if ( ! tables ) return 0; UnionTable::const_iterator ret = tables->unionTable.find( id ); return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id ); } TraitDecl *Indexer::lookupTrait( const std::string &id ) const { if ( ! tables ) return 0; TraitTable::const_iterator ret = tables->traitTable.find( id ); return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id ); } DeclarationWithType *Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const { if ( ! tables ) return 0; if ( tables->scope < scope ) return 0; IdTable::const_iterator decls = tables->idTable.find( id ); if ( decls != tables->idTable.end() ) { const MangleTable &mangleTable = decls->second; MangleTable::const_iterator decl = mangleTable.find( mangleName ); if ( decl != mangleTable.end() ) return decl->second; } return tables->base.lookupIdAtScope( id, mangleName, scope ); } bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName ) const { if ( ! tables ) return false; IdTable::const_iterator decls = tables->idTable.find( id ); if ( decls != tables->idTable.end() ) { const MangleTable &mangleTable = decls->second; for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { // check for C decls with the same name, skipping // those with a compatible type (by mangleName) if ( decl->second->get_linkage() == LinkageSpec::C && decl->first != mangleName ) return true; } } return tables->base.hasIncompatibleCDecl( id, mangleName ); } NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const { if ( ! tables ) return 0; if ( tables->scope < scope ) return 0; TypeTable::const_iterator ret = tables->typeTable.find( id ); return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope ); } StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const { if ( ! tables ) return 0; if ( tables->scope < scope ) return 0; StructTable::const_iterator ret = tables->structTable.find( id ); return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope ); } EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const { if ( ! tables ) return 0; if ( tables->scope < scope ) return 0; EnumTable::const_iterator ret = tables->enumTable.find( id ); return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope ); } UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const { if ( ! tables ) return 0; if ( tables->scope < scope ) return 0; UnionTable::const_iterator ret = tables->unionTable.find( id ); return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope ); } TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const { if ( ! tables ) return 0; if ( tables->scope < scope ) return 0; TraitTable::const_iterator ret = tables->traitTable.find( id ); return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope ); } bool addedIdConflicts( DeclarationWithType *existing, DeclarationWithType *added ) { // if we're giving the same name mangling to things of different types then there is something wrong assert( (dynamic_cast( added ) && dynamic_cast( existing ) ) || (dynamic_cast( added ) && dynamic_cast( existing ) ) ); if ( LinkageSpec::isOverridable( existing->get_linkage() ) ) { // new definition shadows the autogenerated one, even at the same scope return false; } else if ( added->get_linkage() != LinkageSpec::C || ResolvExpr::typesCompatible( added->get_type(), existing->get_type(), Indexer() ) ) { // typesCompatible doesn't really do the right thing here. When checking compatibility of function types, // we should ignore outermost pointer qualifiers, except _Atomic? FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( added ); FunctionDecl *oldentry = dynamic_cast< FunctionDecl* >( existing ); if ( newentry && oldentry ) { if ( newentry->get_statements() && oldentry->get_statements() ) { throw SemanticError( "duplicate function definition for ", added ); } // if } else { // two objects with the same mangled name defined in the same scope. // both objects must be marked extern or both must be intrinsic for this to be okay // xxx - perhaps it's actually if either is intrinsic then this is okay? // might also need to be same storage class? ObjectDecl *newobj = dynamic_cast< ObjectDecl* >( added ); ObjectDecl *oldobj = dynamic_cast< ObjectDecl* >( existing ); if ( newobj->get_storageClass() != DeclarationNode::Extern && oldobj->get_storageClass() != DeclarationNode::Extern ) { throw SemanticError( "duplicate object definition for ", added ); } // if } // if } else { throw SemanticError( "duplicate definition for ", added ); } // if return true; } void Indexer::addId( DeclarationWithType *decl ) { makeWritable(); const std::string &name = decl->get_name(); std::string mangleName; // TODO the first branch of this if is a bug-for-bug replication of Richard's code; // it can allow C decls to hide CFA decls when they shouldn't, but the current prelude // depends on this bug to build ... 's random() should be fixed to *not* hide // 's random() before this first branch is taken out again if ( decl->get_linkage() == LinkageSpec::C ) { mangleName = name; } else if ( LinkageSpec::isOverridable( decl->get_linkage() ) ) { // mangle the name without including the appropriate suffix, so overridable routines are placed into the // same "bucket" as their user defined versions. mangleName = Mangler::mangle( decl, false ); } else { mangleName = Mangler::mangle( decl ); } // if DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope ); if ( ! existing || ! addedIdConflicts( existing, decl ) ) { // this ensures that no two declarations with the same unmangled name both have C linkage if ( decl->get_linkage() == LinkageSpec::C && hasIncompatibleCDecl( name, mangleName ) ) { throw SemanticError( "invalid overload of C function ", decl ); } // NOTE this is broken in Richard's original code in such a way that it never triggers (it // doesn't check decls that have the same manglename, and all C-linkage decls are defined to // have their name as their manglename, hence the error can never trigger). // The elided code here is closer to correct, but name mangling would have to be completely // isomorphic to C type-compatibility, which it may not be. tables->idTable[ name ][ mangleName ] = decl; ++tables->size; } } bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) { if ( existing->get_base() == 0 ) { return false; } else if ( added->get_base() == 0 ) { return true; } else { throw SemanticError( "redeclaration of ", added ); } } void Indexer::addType( NamedTypeDecl *decl ) { makeWritable(); const std::string &id = decl->get_name(); TypeTable::iterator existing = tables->typeTable.find( id ); if ( existing == tables->typeTable.end() ) { NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope ); if ( ! parent || ! addedTypeConflicts( parent, decl ) ) { tables->typeTable.insert( existing, std::make_pair( id, decl ) ); ++tables->size; } } else { if ( ! addedTypeConflicts( existing->second, decl ) ) { existing->second = decl; } } } bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) { if ( existing->get_members().empty() ) { return false; } else if ( ! added->get_members().empty() ) { throw SemanticError( "redeclaration of ", added ); } // if return true; } void Indexer::addStruct( const std::string &id ) { addStruct( new StructDecl( id ) ); } void Indexer::addStruct( StructDecl *decl ) { makeWritable(); const std::string &id = decl->get_name(); StructTable::iterator existing = tables->structTable.find( id ); if ( existing == tables->structTable.end() ) { StructDecl *parent = tables->base.lookupStructAtScope( id, scope ); if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { tables->structTable.insert( existing, std::make_pair( id, decl ) ); ++tables->size; } } else { if ( ! addedDeclConflicts( existing->second, decl ) ) { existing->second = decl; } } } void Indexer::addEnum( EnumDecl *decl ) { makeWritable(); const std::string &id = decl->get_name(); EnumTable::iterator existing = tables->enumTable.find( id ); if ( existing == tables->enumTable.end() ) { EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope ); if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { tables->enumTable.insert( existing, std::make_pair( id, decl ) ); ++tables->size; } } else { if ( ! addedDeclConflicts( existing->second, decl ) ) { existing->second = decl; } } } void Indexer::addUnion( const std::string &id ) { addUnion( new UnionDecl( id ) ); } void Indexer::addUnion( UnionDecl *decl ) { makeWritable(); const std::string &id = decl->get_name(); UnionTable::iterator existing = tables->unionTable.find( id ); if ( existing == tables->unionTable.end() ) { UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope ); if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { tables->unionTable.insert( existing, std::make_pair( id, decl ) ); ++tables->size; } } else { if ( ! addedDeclConflicts( existing->second, decl ) ) { existing->second = decl; } } } void Indexer::addTrait( TraitDecl *decl ) { makeWritable(); const std::string &id = decl->get_name(); TraitTable::iterator existing = tables->traitTable.find( id ); if ( existing == tables->traitTable.end() ) { TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope ); if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { tables->traitTable.insert( existing, std::make_pair( id, decl ) ); ++tables->size; } } else { if ( ! addedDeclConflicts( existing->second, decl ) ) { existing->second = decl; } } } void Indexer::enterScope() { ++scope; if ( doDebug ) { std::cout << "--- Entering scope " << scope << std::endl; } } void Indexer::leaveScope() { using std::cout; assert( scope > 0 && "cannot leave initial scope" ); --scope; while ( tables && tables->scope > scope ) { if ( doDebug ) { cout << "--- Leaving scope " << tables->scope << " containing" << std::endl; dump( tables->idTable, cout ); dump( tables->typeTable, cout ); dump( tables->structTable, cout ); dump( tables->enumTable, cout ); dump( tables->unionTable, cout ); dump( tables->traitTable, cout ); } // swap tables for base table until we find one at an appropriate scope Indexer::Impl *base = newRef( tables->base.tables ); deleteRef( tables ); tables = base; } } void Indexer::print( std::ostream &os, int indent ) const { using std::cerr; cerr << "===idTable===" << std::endl; if ( tables ) dump( tables->idTable, os ); cerr << "===typeTable===" << std::endl; if ( tables ) dump( tables->typeTable, os ); cerr << "===structTable===" << std::endl; if ( tables ) dump( tables->structTable, os ); cerr << "===enumTable===" << std::endl; if ( tables ) dump( tables->enumTable, os ); cerr << "===unionTable===" << std::endl; if ( tables ) dump( tables->unionTable, os ); cerr << "===contextTable===" << std::endl; if ( tables ) dump( tables->traitTable, os ); } } // namespace SymTab // Local Variables: // // tab-width: 4 // // mode: c++ // // compile-command: "make install" // // End: //