Changeset 777ed2b for src/SymTab


Ignore:
Timestamp:
Jul 11, 2018, 11:55:59 AM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
Children:
0fc52b6
Parents:
fc20514 (diff), 7de22b28 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

fix conflicts

Location:
src/SymTab
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Indexer.cc

    rfc20514 r777ed2b  
    272272        }
    273273
     274        NamedTypeDecl *Indexer::globalLookupType( const std::string &id ) const {
     275                return lookupTypeAtScope( id, 0 );
     276        }
     277
     278        StructDecl *Indexer::globalLookupStruct( const std::string &id ) const {
     279                return lookupStructAtScope( id, 0 );
     280        }
     281
     282        UnionDecl *Indexer::globalLookupUnion( const std::string &id ) const {
     283                return lookupUnionAtScope( id, 0 );
     284        }
     285
     286        EnumDecl *Indexer::globalLookupEnum( const std::string &id ) const {
     287                return lookupEnumAtScope( id, 0 );
     288        }
     289
    274290        EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
    275291                if ( ! tables ) return 0;
     
    347363                if ( ! tables ) return 0;
    348364                if ( tables->scope < scope ) return 0;
     365                if ( tables->scope > scope ) return tables->base.lookupTypeAtScope( id, scope );
    349366
    350367                TypeTable::const_iterator ret = tables->typeTable.find( id );
     
    355372                if ( ! tables ) return 0;
    356373                if ( tables->scope < scope ) return 0;
     374                if ( tables->scope > scope ) return tables->base.lookupStructAtScope( id, scope );
    357375
    358376                StructTable::const_iterator ret = tables->structTable.find( id );
     
    363381                if ( ! tables ) return 0;
    364382                if ( tables->scope < scope ) return 0;
     383                if ( tables->scope > scope ) return tables->base.lookupEnumAtScope( id, scope );
    365384
    366385                EnumTable::const_iterator ret = tables->enumTable.find( id );
     
    371390                if ( ! tables ) return 0;
    372391                if ( tables->scope < scope ) return 0;
     392                if ( tables->scope > scope ) return tables->base.lookupUnionAtScope( id, scope );
    373393
    374394                UnionTable::const_iterator ret = tables->unionTable.find( id );
     
    379399                if ( ! tables ) return 0;
    380400                if ( tables->scope < scope ) return 0;
     401                if ( tables->scope > scope ) return tables->base.lookupTraitAtScope( id, scope );
    381402
    382403                TraitTable::const_iterator ret = tables->traitTable.find( id );
     
    486507
    487508        bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) {
    488                 if ( existing->get_base() == 0 ) {
     509                if ( existing->base == nullptr ) {
    489510                        return false;
    490                 } else if ( added->get_base() == 0 ) {
     511                } else if ( added->base == nullptr ) {
    491512                        return true;
    492513                } else {
    493                         SemanticError( added, "redeclaration of " );
    494                 }
     514                        assert( existing->base && added->base );
     515                        // typedef redeclarations are errors only if types are different
     516                        if ( ! ResolvExpr::typesCompatible( existing->base, added->base, Indexer() ) ) {
     517                                SemanticError( added->location, "redeclaration of " + added->name );
     518                        }
     519                }
     520                // does not need to be added to the table if both existing and added have a base that are the same
     521                return true;
    495522        }
    496523
     
    499526                makeWritable();
    500527
    501                 const std::string &id = decl->get_name();
     528                const std::string &id = decl->name;
    502529                TypeTable::iterator existing = tables->typeTable.find( id );
    503530                if ( existing == tables->typeTable.end() ) {
     
    532559                makeWritable();
    533560
    534                 const std::string &id = decl->get_name();
     561                const std::string &id = decl->name;
    535562                StructTable::iterator existing = tables->structTable.find( id );
    536563                if ( existing == tables->structTable.end() ) {
     
    551578                makeWritable();
    552579
    553                 const std::string &id = decl->get_name();
     580                const std::string &id = decl->name;
    554581                EnumTable::iterator existing = tables->enumTable.find( id );
    555582                if ( existing == tables->enumTable.end() ) {
     
    575602                makeWritable();
    576603
    577                 const std::string &id = decl->get_name();
     604                const std::string &id = decl->name;
    578605                UnionTable::iterator existing = tables->unionTable.find( id );
    579606                if ( existing == tables->unionTable.end() ) {
     
    594621                makeWritable();
    595622
    596                 const std::string &id = decl->get_name();
     623                const std::string &id = decl->name;
    597624                TraitTable::iterator existing = tables->traitTable.find( id );
    598625                if ( existing == tables->traitTable.end() ) {
  • src/SymTab/Indexer.h

    rfc20514 r777ed2b  
    7070                /// Gets the top-most trait declaration with the given ID
    7171                TraitDecl *lookupTrait( const std::string &id ) const;
     72
     73                /// Gets the type declaration with the given ID at global scope
     74                NamedTypeDecl *globalLookupType( const std::string &id ) const;
     75                /// Gets the struct declaration with the given ID at global scope
     76                StructDecl *globalLookupStruct( const std::string &id ) const;
     77                /// Gets the union declaration with the given ID at global scope
     78                UnionDecl *globalLookupUnion( const std::string &id ) const;
     79                /// Gets the enum declaration with the given ID at global scope
     80                EnumDecl *globalLookupEnum( const std::string &id ) const;
    7281
    7382                void print( std::ostream &os, int indent = 0 ) const;
  • src/SymTab/Mangler.cc

    rfc20514 r777ed2b  
    6060                                void postvisit( ZeroType * zeroType );
    6161                                void postvisit( OneType * oneType );
     62                                void postvisit( QualifiedType * qualType );
    6263
    6364                                std::string get_mangleName() { return mangleName.str(); }
     
    171172                                        "w",    // SignedInt128
    172173                                        "Uw",   // UnsignedInt128
    173                                         "x",   // Float80
    174                                         "y",   // Float128
     174                                        "x",    // Float80
     175                                        "y",    // Float128
    175176                                };
    176177                                static_assert(
     
    312313                        void Mangler::postvisit( OneType * ) {
    313314                                mangleName << "O";
     315                        }
     316
     317                        void Mangler::postvisit( QualifiedType * qualType ) {
     318                                maybeAccept( qualType->parent, *visitor );
     319                                mangleName << "__";
     320                                maybeAccept( qualType->child, *visitor );
    314321                        }
    315322
  • src/SymTab/Validate.cc

    rfc20514 r777ed2b  
    7777class SwitchStmt;
    7878
    79 #define debugPrint( x ) if ( doDebug ) { std::cout << x; }
     79#define debugPrint( x ) if ( doDebug ) x
    8080
    8181namespace SymTab {
     82        /// hoists declarations that are difficult to hoist while parsing
     83        struct HoistTypeDecls final : public WithDeclsToAdd {
     84                void previsit( SizeofExpr * );
     85                void previsit( AlignofExpr * );
     86                void previsit( UntypedOffsetofExpr * );
     87                void handleType( Type * );
     88        };
     89
     90        struct FixQualifiedTypes final : public WithIndexer {
     91                Type * postmutate( QualifiedType * );
     92        };
     93
    8294        struct HoistStruct final : public WithDeclsToAdd, public WithGuards {
    8395                /// Flattens nested struct types
    8496                static void hoistStruct( std::list< Declaration * > &translationUnit );
    8597
    86                 void previsit( EnumInstType * enumInstType );
    87                 void previsit( StructInstType * structInstType );
    88                 void previsit( UnionInstType * unionInstType );
    8998                void previsit( StructDecl * aggregateDecl );
    9099                void previsit( UnionDecl * aggregateDecl );
    91100                void previsit( StaticAssertDecl * assertDecl );
     101                void previsit( StructInstType * type );
     102                void previsit( UnionInstType * type );
     103                void previsit( EnumInstType * type );
    92104
    93105          private:
     
    112124
    113125        /// Associates forward declarations of aggregates with their definitions
    114         struct LinkReferenceToTypes final : public WithIndexer, public WithGuards {
     126        struct LinkReferenceToTypes final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes>, public WithShortCircuiting {
    115127                LinkReferenceToTypes( const Indexer *indexer );
    116128                void postvisit( TypeInstType *typeInst );
     
    120132                void postvisit( UnionInstType *unionInst );
    121133                void postvisit( TraitInstType *traitInst );
     134                void previsit( QualifiedType * qualType );
     135                void postvisit( QualifiedType * qualType );
    122136
    123137                void postvisit( EnumDecl *enumDecl );
     
    165179        };
    166180
    167         struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards {
    168                 EliminateTypedef() : scopeLevel( 0 ) {}
     181        struct ReplaceTypedef final : public WithVisitorRef<ReplaceTypedef>, public WithGuards, public WithShortCircuiting, public WithDeclsToAdd {
     182                ReplaceTypedef() : scopeLevel( 0 ) {}
    169183                /// Replaces typedefs by forward declarations
    170                 static void eliminateTypedef( std::list< Declaration * > &translationUnit );
    171 
     184                static void replaceTypedef( std::list< Declaration * > &translationUnit );
     185
     186                void premutate( QualifiedType * );
     187                Type * postmutate( QualifiedType * qualType );
    172188                Type * postmutate( TypeInstType * aggregateUseType );
    173189                Declaration * postmutate( TypedefDecl * typeDecl );
     
    180196
    181197                void premutate( CompoundStmt * compoundStmt );
    182                 CompoundStmt * postmutate( CompoundStmt * compoundStmt );
    183198
    184199                void premutate( StructDecl * structDecl );
    185                 Declaration * postmutate( StructDecl * structDecl );
    186200                void premutate( UnionDecl * unionDecl );
    187                 Declaration * postmutate( UnionDecl * unionDecl );
    188201                void premutate( EnumDecl * enumDecl );
    189                 Declaration * postmutate( EnumDecl * enumDecl );
    190                 Declaration * postmutate( TraitDecl * contextDecl );
     202                void premutate( TraitDecl * );
    191203
    192204                void premutate( FunctionType * ftype );
     
    194206          private:
    195207                template<typename AggDecl>
    196                 AggDecl *handleAggregate( AggDecl * aggDecl );
    197 
    198                 template<typename AggDecl>
    199208                void addImplicitTypedef( AggDecl * aggDecl );
     209                template< typename AggDecl >
     210                void handleAggregate( AggDecl * aggr );
    200211
    201212                typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr;
    202213                typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap;
    203                 typedef std::map< std::string, TypeDecl * > TypeDeclMap;
     214                typedef ScopedMap< std::string, TypeDecl * > TypeDeclMap;
    204215                TypedefMap typedefNames;
    205216                TypeDeclMap typedeclNames;
    206217                int scopeLevel;
    207218                bool inFunctionType = false;
     219        };
     220
     221        struct EliminateTypedef {
     222                /// removes TypedefDecls from the AST
     223                static void eliminateTypedef( std::list< Declaration * > &translationUnit );
     224
     225                template<typename AggDecl>
     226                void handleAggregate( AggDecl *aggregateDecl );
     227
     228                void previsit( StructDecl * aggregateDecl );
     229                void previsit( UnionDecl * aggregateDecl );
     230                void previsit( CompoundStmt * compoundStmt );
    208231        };
    209232
     
    263286                PassVisitor<FindSpecialDeclarations> finder;
    264287                PassVisitor<LabelAddressFixer> labelAddrFixer;
    265 
    266                 EliminateTypedef::eliminateTypedef( translationUnit );
    267                 HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order
     288                PassVisitor<HoistTypeDecls> hoistDecls;
     289                PassVisitor<FixQualifiedTypes> fixQual;
     290
     291                acceptAll( translationUnit, hoistDecls );
     292                ReplaceTypedef::replaceTypedef( translationUnit );
    268293                ReturnTypeFixer::fix( translationUnit ); // must happen before autogen
    269294                acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes because it is an indexer and needs correct types for mangling
    270295                acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions
     296                mutateAll( translationUnit, fixQual ); // must happen after LinkReferenceToTypes, because aggregate members are accessed
     297                HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order
     298                EliminateTypedef::eliminateTypedef( translationUnit ); //
    271299                acceptAll( translationUnit, genericParams );  // check as early as possible - can't happen before LinkReferenceToTypes
    272300                VerifyCtorDtorAssign::verify( translationUnit );  // must happen before autogen, because autogen examines existing ctor/dtors
     
    294322        }
    295323
     324
     325        void HoistTypeDecls::handleType( Type * type ) {
     326                // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here
     327                AggregateDecl * aggr = nullptr;
     328                if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) {
     329                        aggr = inst->baseStruct;
     330                } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) {
     331                        aggr = inst->baseUnion;
     332                } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( type ) ) {
     333                        aggr = inst->baseEnum;
     334                }
     335                if ( aggr && aggr->body ) {
     336                        declsToAddBefore.push_front( aggr );
     337                }
     338        }
     339
     340        void HoistTypeDecls::previsit( SizeofExpr * expr ) {
     341                handleType( expr->type );
     342        }
     343
     344        void HoistTypeDecls::previsit( AlignofExpr * expr ) {
     345                handleType( expr->type );
     346        }
     347
     348        void HoistTypeDecls::previsit( UntypedOffsetofExpr * expr ) {
     349                handleType( expr->type );
     350        }
     351
     352
     353        Type * FixQualifiedTypes::postmutate( QualifiedType * qualType ) {
     354                Type * parent = qualType->parent;
     355                Type * child = qualType->child;
     356                if ( dynamic_cast< GlobalScopeType * >( qualType->parent ) ) {
     357                        // .T => lookup T at global scope
     358                        if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) {
     359                                auto td = indexer.globalLookupType( inst->name );
     360                                if ( ! td ) {
     361                                        SemanticError( qualType->location, toString("Use of undefined global type ", inst->name) );
     362                                }
     363                                auto base = td->base;
     364                                assert( base );
     365                                return base->clone();
     366                        } else {
     367                                // .T => T is not a type name
     368                                assertf( false, "unhandled global qualified child type: %s", toCString(child) );
     369                        }
     370                } else {
     371                        // S.T => S must be an aggregate type, find the declaration for T in S.
     372                        AggregateDecl * aggr = nullptr;
     373                        if ( StructInstType * inst = dynamic_cast< StructInstType * >( parent ) ) {
     374                                aggr = inst->baseStruct;
     375                        } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * > ( parent ) ) {
     376                                aggr = inst->baseUnion;
     377                        } else {
     378                                SemanticError( qualType->location, toString("Qualified type requires an aggregate on the left, but has: ", parent) );
     379                        }
     380                        assert( aggr ); // TODO: need to handle forward declarations
     381                        for ( Declaration * member : aggr->members ) {
     382                                if ( StructInstType * inst = dynamic_cast< StructInstType * >( child ) ) {
     383                                        if ( StructDecl * aggr = dynamic_cast< StructDecl * >( member ) ) {
     384                                                if ( aggr->name == inst->name ) {
     385                                                        return new StructInstType( qualType->get_qualifiers(), aggr );
     386                                                }
     387                                        }
     388                                } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( child ) ) {
     389                                        if ( UnionDecl * aggr = dynamic_cast< UnionDecl * > ( member ) ) {
     390                                                if ( aggr->name == inst->name ) {
     391                                                        return new UnionInstType( qualType->get_qualifiers(), aggr );
     392                                                }
     393                                        }
     394                                } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( child ) ) {
     395                                        if ( EnumDecl * aggr = dynamic_cast< EnumDecl * > ( member ) ) {
     396                                                if ( aggr->name == inst->name ) {
     397                                                        return new EnumInstType( qualType->get_qualifiers(), aggr );
     398                                                }
     399                                        }
     400                                } else if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) {
     401                                        // struct typedefs are being replaced by forward decls too early; move it to hoist struct
     402                                        if ( NamedTypeDecl * aggr = dynamic_cast< NamedTypeDecl * > ( member ) ) {
     403                                                if ( aggr->name == inst->name ) {
     404                                                        assert( aggr->base );
     405                                                        return aggr->base->clone();
     406                                                }
     407                                        }
     408                                } else {
     409                                        // S.T - S is not an aggregate => error
     410                                        assertf( false, "unhandled qualified child type: %s", toCString(qualType) );
     411                                }
     412                        }
     413                        // failed to find a satisfying definition of type
     414                        SemanticError( qualType->location, toString("Undefined type in qualified type: ", qualType) );
     415                }
     416
     417                // ... may want to link canonical SUE definition to each forward decl so that it becomes easier to lookup?
     418        }
     419
     420
    296421        void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
    297422                PassVisitor<HoistStruct> hoister;
     
    303428        }
    304429
     430        namespace {
     431                void qualifiedName( AggregateDecl * aggr, std::ostringstream & ss ) {
     432                        if ( aggr->parent ) qualifiedName( aggr->parent, ss );
     433                        ss << "__" << aggr->name;
     434                }
     435
     436                // mangle nested type names using entire parent chain
     437                std::string qualifiedName( AggregateDecl * aggr ) {
     438                        std::ostringstream ss;
     439                        qualifiedName( aggr, ss );
     440                        return ss.str();
     441                }
     442        }
     443
    305444        template< typename AggDecl >
    306445        void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
    307446                if ( parentAggr ) {
     447                        aggregateDecl->parent = parentAggr;
     448                        aggregateDecl->name = qualifiedName( aggregateDecl );
    308449                        // Add elements in stack order corresponding to nesting structure.
    309450                        declsToAddBefore.push_front( aggregateDecl );
     
    316457        }
    317458
    318         void HoistStruct::previsit( EnumInstType * inst ) {
    319                 if ( inst->baseEnum && inst->baseEnum->body ) {
    320                         declsToAddBefore.push_front( inst->baseEnum );
    321                 }
    322         }
    323 
    324         void HoistStruct::previsit( StructInstType * inst ) {
    325                 if ( inst->baseStruct && inst->baseStruct->body ) {
    326                         declsToAddBefore.push_front( inst->baseStruct );
    327                 }
    328         }
    329 
    330         void HoistStruct::previsit( UnionInstType * inst ) {
    331                 if ( inst->baseUnion && inst->baseUnion->body ) {
    332                         declsToAddBefore.push_front( inst->baseUnion );
    333                 }
    334         }
    335 
    336459        void HoistStruct::previsit( StaticAssertDecl * assertDecl ) {
    337460                if ( parentAggr ) {
     
    348471        }
    349472
     473        void HoistStruct::previsit( StructInstType * type ) {
     474                // need to reset type name after expanding to qualified name
     475                assert( type->baseStruct );
     476                type->name = type->baseStruct->name;
     477        }
     478
     479        void HoistStruct::previsit( UnionInstType * type ) {
     480                assert( type->baseUnion );
     481                type->name = type->baseUnion->name;
     482        }
     483
     484        void HoistStruct::previsit( EnumInstType * type ) {
     485                assert( type->baseEnum );
     486                type->name = type->baseEnum->name;
     487        }
     488
     489
     490        bool isTypedef( Declaration *decl ) {
     491                return dynamic_cast< TypedefDecl * >( decl );
     492        }
     493
     494        void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
     495                PassVisitor<EliminateTypedef> eliminator;
     496                acceptAll( translationUnit, eliminator );
     497                filter( translationUnit, isTypedef, true );
     498        }
     499
     500        template< typename AggDecl >
     501        void EliminateTypedef::handleAggregate( AggDecl *aggregateDecl ) {
     502                filter( aggregateDecl->members, isTypedef, true );
     503        }
     504
     505        void EliminateTypedef::previsit( StructDecl * aggregateDecl ) {
     506                handleAggregate( aggregateDecl );
     507        }
     508
     509        void EliminateTypedef::previsit( UnionDecl * aggregateDecl ) {
     510                handleAggregate( aggregateDecl );
     511        }
     512
     513        void EliminateTypedef::previsit( CompoundStmt * compoundStmt ) {
     514                // remove and delete decl stmts
     515                filter( compoundStmt->kids, [](Statement * stmt) {
     516                        if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {
     517                                if ( dynamic_cast< TypedefDecl * >( declStmt->decl ) ) {
     518                                        return true;
     519                                } // if
     520                        } // if
     521                        return false;
     522                }, true);
     523        }
     524
    350525        void EnumAndPointerDecay::previsit( EnumDecl *enumDecl ) {
    351526                // Set the type of each member of the enumeration to be EnumConstant
    352                 for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
     527                for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) {
    353528                        ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
    354529                        assert( obj );
    355                         obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->get_name() ) );
     530                        obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) );
    356531                } // for
    357532        }
     
    395570
    396571        void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) {
    397                 EnumDecl *st = local_indexer->lookupEnum( enumInst->get_name() );
     572                EnumDecl *st = local_indexer->lookupEnum( enumInst->name );
    398573                // it's not a semantic error if the enum is not found, just an implicit forward declaration
    399574                if ( st ) {
    400                         //assert( ! enumInst->get_baseEnum() || enumInst->get_baseEnum()->get_members().empty() || ! st->get_members().empty() );
    401                         enumInst->set_baseEnum( st );
    402                 } // if
    403                 if ( ! st || st->get_members().empty() ) {
     575                        enumInst->baseEnum = st;
     576                } // if
     577                if ( ! st || ! st->body ) {
    404578                        // use of forward declaration
    405                         forwardEnums[ enumInst->get_name() ].push_back( enumInst );
     579                        forwardEnums[ enumInst->name ].push_back( enumInst );
    406580                } // if
    407581        }
     
    416590
    417591        void LinkReferenceToTypes::postvisit( StructInstType *structInst ) {
    418                 StructDecl *st = local_indexer->lookupStruct( structInst->get_name() );
     592                StructDecl *st = local_indexer->lookupStruct( structInst->name );
    419593                // it's not a semantic error if the struct is not found, just an implicit forward declaration
    420594                if ( st ) {
    421                         //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
    422                         structInst->set_baseStruct( st );
    423                 } // if
    424                 if ( ! st || st->get_members().empty() ) {
     595                        structInst->baseStruct = st;
     596                } // if
     597                if ( ! st || ! st->body ) {
    425598                        // use of forward declaration
    426                         forwardStructs[ structInst->get_name() ].push_back( structInst );
     599                        forwardStructs[ structInst->name ].push_back( structInst );
    427600                } // if
    428601                checkGenericParameters( structInst );
     
    430603
    431604        void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) {
    432                 UnionDecl *un = local_indexer->lookupUnion( unionInst->get_name() );
     605                UnionDecl *un = local_indexer->lookupUnion( unionInst->name );
    433606                // it's not a semantic error if the union is not found, just an implicit forward declaration
    434607                if ( un ) {
    435                         unionInst->set_baseUnion( un );
    436                 } // if
    437                 if ( ! un || un->get_members().empty() ) {
     608                        unionInst->baseUnion = un;
     609                } // if
     610                if ( ! un || ! un->body ) {
    438611                        // use of forward declaration
    439                         forwardUnions[ unionInst->get_name() ].push_back( unionInst );
     612                        forwardUnions[ unionInst->name ].push_back( unionInst );
    440613                } // if
    441614                checkGenericParameters( unionInst );
     615        }
     616
     617        void LinkReferenceToTypes::previsit( QualifiedType * ) {
     618                visit_children = false;
     619        }
     620
     621        void LinkReferenceToTypes::postvisit( QualifiedType * qualType ) {
     622                // linking only makes sense for the 'oldest ancestor' of the qualified type
     623                qualType->parent->accept( *visitor );
    442624        }
    443625
     
    450632                        DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 );
    451633                        if ( dwt1 && dwt2 ) {
    452                                 if ( dwt1->get_name() == dwt2->get_name() && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) {
     634                                if ( dwt1->name == dwt2->name && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) {
    453635                                        // std::cerr << "=========== equal:" << std::endl;
    454636                                        // std::cerr << "d1: " << d1 << std::endl;
     
    475657        template< typename Iterator >
    476658        void expandAssertions( TraitInstType * inst, Iterator out ) {
    477                 assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toString( inst ).c_str() );
     659                assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) );
    478660                std::list< DeclarationWithType * > asserts;
    479661                for ( Declaration * decl : inst->baseTrait->members ) {
     
    512694                        SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name );
    513695                } // if
    514                 if ( traitDecl->get_parameters().size() != traitInst->get_parameters().size() ) {
     696                if ( traitDecl->parameters.size() != traitInst->parameters.size() ) {
    515697                        SemanticError( traitInst, "incorrect number of trait parameters: " );
    516698                } // if
     
    518700
    519701                // need to carry over the 'sized' status of each decl in the instance
    520                 for ( auto p : group_iterate( traitDecl->get_parameters(), traitInst->get_parameters() ) ) {
     702                for ( auto p : group_iterate( traitDecl->parameters, traitInst->parameters ) ) {
    521703                        TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) );
    522704                        if ( ! expr ) {
     
    525707                        if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) {
    526708                                TypeDecl * formalDecl = std::get<0>(p);
    527                                 TypeDecl * instDecl = inst->get_baseType();
     709                                TypeDecl * instDecl = inst->baseType;
    528710                                if ( formalDecl->get_sized() ) instDecl->set_sized( true );
    529711                        }
     
    534716        void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) {
    535717                // visit enum members first so that the types of self-referencing members are updated properly
    536                 if ( ! enumDecl->get_members().empty() ) {
    537                         ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->get_name() );
     718                if ( enumDecl->body ) {
     719                        ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->name );
    538720                        if ( fwds != forwardEnums.end() ) {
    539721                                for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    540                                         (*inst )->set_baseEnum( enumDecl );
     722                                        (*inst)->baseEnum = enumDecl;
    541723                                } // for
    542724                                forwardEnums.erase( fwds );
     
    574756                // visit struct members first so that the types of self-referencing members are updated properly
    575757                // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults)
    576                 if ( ! structDecl->get_members().empty() ) {
    577                         ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
     758                if ( structDecl->body ) {
     759                        ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->name );
    578760                        if ( fwds != forwardStructs.end() ) {
    579761                                for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    580                                         (*inst )->set_baseStruct( structDecl );
     762                                        (*inst)->baseStruct = structDecl;
    581763                                } // for
    582764                                forwardStructs.erase( fwds );
     
    586768
    587769        void LinkReferenceToTypes::postvisit( UnionDecl *unionDecl ) {
    588                 if ( ! unionDecl->get_members().empty() ) {
    589                         ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
     770                if ( unionDecl->body ) {
     771                        ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name );
    590772                        if ( fwds != forwardUnions.end() ) {
    591773                                for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    592                                         (*inst )->set_baseUnion( unionDecl );
     774                                        (*inst)->baseUnion = unionDecl;
    593775                                } // for
    594776                                forwardUnions.erase( fwds );
     
    600782                // ensure generic parameter instances are renamed like the base type
    601783                if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name;
    602                 if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->get_name() ) ) {
     784                if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) {
    603785                        if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
    604786                                typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
     
    679861
    680862
    681         bool isTypedef( Declaration *decl ) {
    682                 return dynamic_cast< TypedefDecl * >( decl );
    683         }
    684 
    685         void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
    686                 PassVisitor<EliminateTypedef> eliminator;
     863        void ReplaceTypedef::replaceTypedef( std::list< Declaration * > &translationUnit ) {
     864                PassVisitor<ReplaceTypedef> eliminator;
    687865                mutateAll( translationUnit, eliminator );
    688866                if ( eliminator.pass.typedefNames.count( "size_t" ) ) {
    689867                        // grab and remember declaration of size_t
    690                         SizeType = eliminator.pass.typedefNames["size_t"].first->get_base()->clone();
     868                        SizeType = eliminator.pass.typedefNames["size_t"].first->base->clone();
    691869                } else {
    692870                        // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
     
    694872                        SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    695873                }
    696                 filter( translationUnit, isTypedef, true );
    697         }
    698 
    699         Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) {
     874        }
     875
     876        void ReplaceTypedef::premutate( QualifiedType * ) {
     877                visit_children = false;
     878        }
     879
     880        Type * ReplaceTypedef::postmutate( QualifiedType * qualType ) {
     881                // replacing typedefs only makes sense for the 'oldest ancestor' of the qualified type
     882                qualType->parent = qualType->parent->acceptMutator( *visitor );
     883                return qualType;
     884        }
     885
     886        Type * ReplaceTypedef::postmutate( TypeInstType * typeInst ) {
    700887                // instances of typedef types will come here. If it is an instance
    701888                // of a typdef type, link the instance to its actual type.
    702                 TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
     889                TypedefMap::const_iterator def = typedefNames.find( typeInst->name );
    703890                if ( def != typedefNames.end() ) {
    704891                        Type *ret = def->second.first->base->clone();
     892                        ret->location = typeInst->location;
    705893                        ret->get_qualifiers() |= typeInst->get_qualifiers();
    706894                        // attributes are not carried over from typedef to function parameters/return values
     
    717905                                        SemanticError( typeInst->location, "Cannot apply type parameters to base type of " + typeInst->name );
    718906                                }
    719                                 rtt->get_parameters().clear();
     907                                rtt->parameters.clear();
    720908                                cloneAll( typeInst->parameters, rtt->parameters );
    721909                                mutateAll( rtt->parameters, *visitor );  // recursively fix typedefs on parameters
     
    724912                        return ret;
    725913                } else {
    726                         TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() );
    727                         assertf( base != typedeclNames.end(), "Cannot find typedecl name %s", typeInst->name.c_str() );
     914                        TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->name );
     915                        if ( base == typedeclNames.end() ) {
     916                                SemanticError( typeInst->location, toString("Use of undefined type ", typeInst->name) );
     917                        }
    728918                        typeInst->set_baseType( base->second );
    729                 } // if
    730                 return typeInst;
     919                        return typeInst;
     920                } // if
     921                assert( false );
    731922        }
    732923
     
    745936        }
    746937
    747         Declaration *EliminateTypedef::postmutate( TypedefDecl * tyDecl ) {
    748                 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
     938        Declaration * ReplaceTypedef::postmutate( TypedefDecl * tyDecl ) {
     939                if ( typedefNames.count( tyDecl->name ) == 1 && typedefNames[ tyDecl->name ].second == scopeLevel ) {
    749940                        // typedef to the same name from the same scope
    750941                        // must be from the same type
    751942
    752                         Type * t1 = tyDecl->get_base();
    753                         Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
     943                        Type * t1 = tyDecl->base;
     944                        Type * t2 = typedefNames[ tyDecl->name ].first->base;
    754945                        if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
    755946                                SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name );
     
    763954                        }
    764955                } else {
    765                         typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
     956                        typedefNames[ tyDecl->name ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
    766957                } // if
    767958
     
    775966                // Note, qualifiers on the typedef are superfluous for the forward declaration.
    776967
    777                 Type *designatorType = tyDecl->get_base()->stripDeclarator();
     968                Type *designatorType = tyDecl->base->stripDeclarator();
    778969                if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) {
    779                         return new StructDecl( aggDecl->get_name(), DeclarationNode::Struct, noAttributes, tyDecl->get_linkage() );
     970                        declsToAddBefore.push_back( new StructDecl( aggDecl->name, DeclarationNode::Struct, noAttributes, tyDecl->linkage ) );
    780971                } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) {
    781                         return new UnionDecl( aggDecl->get_name(), noAttributes, tyDecl->get_linkage() );
     972                        declsToAddBefore.push_back( new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage ) );
    782973                } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) {
    783                         return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() );
    784                 } else {
    785                         return tyDecl->clone();
    786                 } // if
    787         }
    788 
    789         void EliminateTypedef::premutate( TypeDecl * typeDecl ) {
    790                 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
     974                        declsToAddBefore.push_back( new EnumDecl( enumDecl->name, noAttributes, tyDecl->linkage ) );
     975                } // if
     976                return tyDecl->clone();
     977        }
     978
     979        void ReplaceTypedef::premutate( TypeDecl * typeDecl ) {
     980                TypedefMap::iterator i = typedefNames.find( typeDecl->name );
    791981                if ( i != typedefNames.end() ) {
    792982                        typedefNames.erase( i ) ;
    793983                } // if
    794984
    795                 typedeclNames[ typeDecl->get_name() ] = typeDecl;
    796         }
    797 
    798         void EliminateTypedef::premutate( FunctionDecl * ) {
     985                typedeclNames.insert( typeDecl->name, typeDecl );
     986        }
     987
     988        void ReplaceTypedef::premutate( FunctionDecl * ) {
    799989                GuardScope( typedefNames );
    800         }
    801 
    802         void EliminateTypedef::premutate( ObjectDecl * ) {
     990                GuardScope( typedeclNames );
     991        }
     992
     993        void ReplaceTypedef::premutate( ObjectDecl * ) {
    803994                GuardScope( typedefNames );
    804         }
    805 
    806         DeclarationWithType *EliminateTypedef::postmutate( ObjectDecl * objDecl ) {
    807                 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->get_type() ) ) { // function type?
     995                GuardScope( typedeclNames );
     996        }
     997
     998        DeclarationWithType * ReplaceTypedef::postmutate( ObjectDecl * objDecl ) {
     999                if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type?
    8081000                        // replace the current object declaration with a function declaration
    809                         FunctionDecl * newDecl = new FunctionDecl( objDecl->get_name(), objDecl->get_storageClasses(), objDecl->get_linkage(), funtype, 0, objDecl->get_attributes(), objDecl->get_funcSpec() );
    810                         objDecl->get_attributes().clear();
     1001                        FunctionDecl * newDecl = new FunctionDecl( objDecl->name, objDecl->get_storageClasses(), objDecl->linkage, funtype, 0, objDecl->attributes, objDecl->get_funcSpec() );
     1002                        objDecl->attributes.clear();
    8111003                        objDecl->set_type( nullptr );
    8121004                        delete objDecl;
     
    8161008        }
    8171009
    818         void EliminateTypedef::premutate( CastExpr * ) {
     1010        void ReplaceTypedef::premutate( CastExpr * ) {
    8191011                GuardScope( typedefNames );
    820         }
    821 
    822         void EliminateTypedef::premutate( CompoundStmt * ) {
     1012                GuardScope( typedeclNames );
     1013        }
     1014
     1015        void ReplaceTypedef::premutate( CompoundStmt * ) {
    8231016                GuardScope( typedefNames );
     1017                GuardScope( typedeclNames );
    8241018                scopeLevel += 1;
    8251019                GuardAction( [this](){ scopeLevel -= 1; } );
    8261020        }
    8271021
    828         CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) {
    829                 // remove and delete decl stmts
    830                 filter( compoundStmt->kids, [](Statement * stmt) {
    831                         if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {
    832                                 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
    833                                         return true;
    834                                 } // if
    835                         } // if
    836                         return false;
    837                 }, true);
    838                 return compoundStmt;
    839         }
    840 
    841         // there may be typedefs nested within aggregates. in order for everything to work properly, these should be removed
    842         // as well
    8431022        template<typename AggDecl>
    844         AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
    845                 filter( aggDecl->members, isTypedef, true );
    846                 return aggDecl;
    847         }
    848 
    849         template<typename AggDecl>
    850         void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) {
     1023        void ReplaceTypedef::addImplicitTypedef( AggDecl * aggDecl ) {
    8511024                if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
    8521025                        Type *type = nullptr;
     
    8601033                        TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type, aggDecl->get_linkage() ) );
    8611034                        typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel );
    862                 } // if
    863         }
    864 
    865         void EliminateTypedef::premutate( StructDecl * structDecl ) {
     1035                        // add the implicit typedef to the AST
     1036                        declsToAddBefore.push_back( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type->clone(), aggDecl->get_linkage() ) );
     1037                } // if
     1038        }
     1039
     1040        template< typename AggDecl >
     1041        void ReplaceTypedef::handleAggregate( AggDecl * aggr ) {
     1042                SemanticErrorException errors;
     1043
     1044                ValueGuard< std::list<Declaration * > > oldBeforeDecls( declsToAddBefore );
     1045                ValueGuard< std::list<Declaration * > > oldAfterDecls ( declsToAddAfter  );
     1046                declsToAddBefore.clear();
     1047                declsToAddAfter.clear();
     1048
     1049                GuardScope( typedefNames );
     1050                GuardScope( typedeclNames );
     1051                mutateAll( aggr->parameters, *visitor );
     1052
     1053                // unroll mutateAll for aggr->members so that implicit typedefs for nested types are added to the aggregate body.
     1054                for ( std::list< Declaration * >::iterator i = aggr->members.begin(); i != aggr->members.end(); ++i ) {
     1055                        if ( !declsToAddAfter.empty() ) { aggr->members.splice( i, declsToAddAfter ); }
     1056
     1057                        try {
     1058                                *i = maybeMutate( *i, *visitor );
     1059                        } catch ( SemanticErrorException &e ) {
     1060                                errors.append( e );
     1061                        }
     1062
     1063                        if ( !declsToAddBefore.empty() ) { aggr->members.splice( i, declsToAddBefore ); }
     1064                }
     1065
     1066                if ( !declsToAddAfter.empty() ) { aggr->members.splice( aggr->members.end(), declsToAddAfter ); }
     1067                if ( !errors.isEmpty() ) { throw errors; }
     1068        }
     1069
     1070        void ReplaceTypedef::premutate( StructDecl * structDecl ) {
     1071                visit_children = false;
    8661072                addImplicitTypedef( structDecl );
    867         }
    868 
    869 
    870         Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) {
    871                 return handleAggregate( structDecl );
    872         }
    873 
    874         void EliminateTypedef::premutate( UnionDecl * unionDecl ) {
     1073                handleAggregate( structDecl );
     1074        }
     1075
     1076        void ReplaceTypedef::premutate( UnionDecl * unionDecl ) {
     1077                visit_children = false;
    8751078                addImplicitTypedef( unionDecl );
    876         }
    877 
    878         Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) {
    879                 return handleAggregate( unionDecl );
    880         }
    881 
    882         void EliminateTypedef::premutate( EnumDecl * enumDecl ) {
     1079                handleAggregate( unionDecl );
     1080        }
     1081
     1082        void ReplaceTypedef::premutate( EnumDecl * enumDecl ) {
    8831083                addImplicitTypedef( enumDecl );
    8841084        }
    8851085
    886         Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) {
    887                 return handleAggregate( enumDecl );
    888         }
    889 
    890         Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) {
    891                 return handleAggregate( traitDecl );
    892         }
    893 
    894         void EliminateTypedef::premutate( FunctionType * ) {
     1086        void ReplaceTypedef::premutate( FunctionType * ) {
    8951087                GuardValue( inFunctionType );
    8961088                inFunctionType = true;
     1089        }
     1090
     1091        void ReplaceTypedef::premutate( TraitDecl * ) {
     1092                GuardScope( typedefNames );
     1093                GuardScope( typedeclNames);
    8971094        }
    8981095
     
    10241221
    10251222        void ArrayLength::previsit( ObjectDecl * objDecl ) {
    1026                 if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) {
     1223                if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->type ) ) {
    10271224                        if ( at->get_dimension() ) return;
    1028                         if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->get_init() ) ) {
    1029                                 at->set_dimension( new ConstantExpr( Constant::from_ulong( init->get_initializers().size() ) ) );
     1225                        if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->init ) ) {
     1226                                at->set_dimension( new ConstantExpr( Constant::from_ulong( init->initializers.size() ) ) );
    10301227                        }
    10311228                }
Note: See TracChangeset for help on using the changeset viewer.