Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    ra12c81f3 r25fcb84  
    7777class SwitchStmt;
    7878
    79 #define debugPrint( x ) if ( doDebug ) x
     79#define debugPrint( x ) if ( doDebug ) { std::cout << 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 
    9482        struct HoistStruct final : public WithDeclsToAdd, public WithGuards {
    9583                /// Flattens nested struct types
    9684                static void hoistStruct( std::list< Declaration * > &translationUnit );
    9785
     86                void previsit( EnumInstType * enumInstType );
     87                void previsit( StructInstType * structInstType );
     88                void previsit( UnionInstType * unionInstType );
    9889                void previsit( StructDecl * aggregateDecl );
    9990                void previsit( UnionDecl * aggregateDecl );
    10091                void previsit( StaticAssertDecl * assertDecl );
    101                 void previsit( StructInstType * type );
    102                 void previsit( UnionInstType * type );
    103                 void previsit( EnumInstType * type );
    10492
    10593          private:
     
    124112
    125113        /// Associates forward declarations of aggregates with their definitions
    126         struct LinkReferenceToTypes final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes>, public WithShortCircuiting {
     114        struct LinkReferenceToTypes final : public WithIndexer, public WithGuards {
    127115                LinkReferenceToTypes( const Indexer *indexer );
    128116                void postvisit( TypeInstType *typeInst );
     
    132120                void postvisit( UnionInstType *unionInst );
    133121                void postvisit( TraitInstType *traitInst );
    134                 void previsit( QualifiedType * qualType );
    135                 void postvisit( QualifiedType * qualType );
    136122
    137123                void postvisit( EnumDecl *enumDecl );
     
    179165        };
    180166
    181         struct ReplaceTypedef final : public WithVisitorRef<ReplaceTypedef>, public WithGuards, public WithShortCircuiting, public WithDeclsToAdd {
    182                 ReplaceTypedef() : scopeLevel( 0 ) {}
     167        struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards {
     168                EliminateTypedef() : scopeLevel( 0 ) {}
    183169                /// Replaces typedefs by forward declarations
    184                 static void replaceTypedef( std::list< Declaration * > &translationUnit );
    185 
    186                 void premutate( QualifiedType * );
    187                 Type * postmutate( QualifiedType * qualType );
     170                static void eliminateTypedef( std::list< Declaration * > &translationUnit );
     171
    188172                Type * postmutate( TypeInstType * aggregateUseType );
    189173                Declaration * postmutate( TypedefDecl * typeDecl );
     
    196180
    197181                void premutate( CompoundStmt * compoundStmt );
     182                CompoundStmt * postmutate( CompoundStmt * compoundStmt );
    198183
    199184                void premutate( StructDecl * structDecl );
     185                Declaration * postmutate( StructDecl * structDecl );
    200186                void premutate( UnionDecl * unionDecl );
     187                Declaration * postmutate( UnionDecl * unionDecl );
    201188                void premutate( EnumDecl * enumDecl );
     189                Declaration * postmutate( EnumDecl * enumDecl );
     190                Declaration * postmutate( TraitDecl * contextDecl );
    202191
    203192                void premutate( FunctionType * ftype );
     
    205194          private:
    206195                template<typename AggDecl>
     196                AggDecl *handleAggregate( AggDecl * aggDecl );
     197
     198                template<typename AggDecl>
    207199                void addImplicitTypedef( AggDecl * aggDecl );
    208                 template< typename AggDecl >
    209                 void handleAggregate( AggDecl * aggr );
    210200
    211201                typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr;
    212202                typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap;
    213                 typedef std::map< std::string, TypeDecl * > TypeDeclMap; // xxx - convert to ScopedMap
     203                typedef std::map< std::string, TypeDecl * > TypeDeclMap;
    214204                TypedefMap typedefNames;
    215205                TypeDeclMap typedeclNames;
    216206                int scopeLevel;
    217207                bool inFunctionType = false;
    218         };
    219 
    220         struct EliminateTypedef {
    221                 /// removes TypedefDecls from the AST
    222                 static void eliminateTypedef( std::list< Declaration * > &translationUnit );
    223 
    224                 template<typename AggDecl>
    225                 void handleAggregate( AggDecl *aggregateDecl );
    226 
    227                 void previsit( StructDecl * aggregateDecl );
    228                 void previsit( UnionDecl * aggregateDecl );
    229                 void previsit( CompoundStmt * compoundStmt );
    230208        };
    231209
     
    285263                PassVisitor<FindSpecialDeclarations> finder;
    286264                PassVisitor<LabelAddressFixer> labelAddrFixer;
    287                 PassVisitor<HoistTypeDecls> hoistDecls;
    288                 PassVisitor<FixQualifiedTypes> fixQual;
    289 
    290                 acceptAll( translationUnit, hoistDecls );
    291                 ReplaceTypedef::replaceTypedef( translationUnit );
     265
     266                EliminateTypedef::eliminateTypedef( translationUnit );
     267                HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order
    292268                ReturnTypeFixer::fix( translationUnit ); // must happen before autogen
    293269                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
    294270                acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions
    295                 mutateAll( translationUnit, fixQual ); // must happen after LinkReferenceToTypes, because aggregate members are accessed
    296                 HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order
    297                 EliminateTypedef::eliminateTypedef( translationUnit ); //
    298271                acceptAll( translationUnit, genericParams );  // check as early as possible - can't happen before LinkReferenceToTypes
    299272                VerifyCtorDtorAssign::verify( translationUnit );  // must happen before autogen, because autogen examines existing ctor/dtors
     
    321294        }
    322295
    323 
    324         void HoistTypeDecls::handleType( Type * type ) {
    325                 // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here
    326                 AggregateDecl * aggr = nullptr;
    327                 if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) {
    328                         aggr = inst->baseStruct;
    329                 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) {
    330                         aggr = inst->baseUnion;
    331                 } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( type ) ) {
    332                         aggr = inst->baseEnum;
    333                 }
    334                 if ( aggr && aggr->body ) {
    335                         declsToAddBefore.push_front( aggr );
    336                 }
    337         }
    338 
    339         void HoistTypeDecls::previsit( SizeofExpr * expr ) {
    340                 handleType( expr->type );
    341         }
    342 
    343         void HoistTypeDecls::previsit( AlignofExpr * expr ) {
    344                 handleType( expr->type );
    345         }
    346 
    347         void HoistTypeDecls::previsit( UntypedOffsetofExpr * expr ) {
    348                 handleType( expr->type );
    349         }
    350 
    351 
    352         Type * FixQualifiedTypes::postmutate( QualifiedType * qualType ) {
    353                 // TODO: change asserts to SemanticErrors as necessary
    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 ( StructInstType * inst = dynamic_cast< StructInstType * >( child ) ) {
    359                                 auto aggr = indexer.globalLookupStruct( inst->name );
    360                                 return new StructInstType( qualType->get_qualifiers(), aggr );
    361                         } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( child ) ) {
    362                                 auto aggr =  indexer.globalLookupUnion( inst->name );
    363                                 return new UnionInstType( qualType->get_qualifiers(), aggr );
    364                         } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( child ) ) {
    365                                 auto aggr = indexer.globalLookupEnum( inst->name );
    366                                 return new EnumInstType( qualType->get_qualifiers(), aggr );
    367                         } else if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) {
    368                                 auto td = indexer.globalLookupType( inst->name );
    369                                 assertf( td, "did not find type at global scope with name: %s", inst->name.c_str() );
    370                                 auto base = td->base;
    371                                 if ( base ) return td->base->clone();
    372                                 assert( false );
    373                         } else {
    374                                 // .T => T is not a SUE type name
    375                                 assert( false );
    376                         }
    377                 } else {
    378                         // S.T => S must be an aggregate type, find the declaration for T in S.
    379                         AggregateDecl * aggr = nullptr;
    380                         if ( StructInstType * inst = dynamic_cast< StructInstType * >( parent ) ) {
    381                                 aggr = inst->baseStruct;
    382                         } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * > ( parent ) ) {
    383                                 aggr = inst->baseUnion;
    384                         } else {
    385                                 assert( false );
    386                         }
    387                         assert( aggr ); // TODO: need to handle forward declarations
    388                         for ( Declaration * member : aggr->members ) {
    389                                 if ( StructInstType * inst = dynamic_cast< StructInstType * >( child ) ) {
    390                                         if ( StructDecl * aggr = dynamic_cast< StructDecl * >( member ) ) {
    391                                                 if ( aggr->name == inst->name ) {
    392                                                         return new StructInstType( qualType->get_qualifiers(), aggr );
    393                                                 }
    394                                         }
    395                                 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( child ) ) {
    396                                         if ( UnionDecl * aggr = dynamic_cast< UnionDecl * > ( member ) ) {
    397                                                 if ( aggr->name == inst->name ) {
    398                                                         return new UnionInstType( qualType->get_qualifiers(), aggr );
    399                                                 }
    400                                         }
    401                                 } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( child ) ) {
    402                                         if ( EnumDecl * aggr = dynamic_cast< EnumDecl * > ( member ) ) {
    403                                                 if ( aggr->name == inst->name ) {
    404                                                         return new EnumInstType( qualType->get_qualifiers(), aggr );
    405                                                 }
    406                                         }
    407                                 } else if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) {
    408                                         // struct typedefs are being replaced by forward decls too early; move it to hoist struct
    409                                         if ( NamedTypeDecl * aggr = dynamic_cast< NamedTypeDecl * > ( member ) ) {
    410                                                 if ( aggr->name == inst->name ) {
    411                                                         if ( aggr->base ) return aggr->base->clone();
    412                                                         assert( false );
    413                                                 }
    414                                         }
    415                                 } else {
    416                                         // S.T - S is not an aggregate => error
    417                                         assertf( false, "unhandled qualified child type: %s", toCString(qualType) );
    418                                 }
    419                         }
    420                         // failed to find a satisfying definition of type
    421                         assertf( false, "failed to find a satisfying definition of %s in %s", toCString(child), toCString(parent) );
    422                 }
    423 
    424                 // ... may want to link canonical SUE definition to each forward decl so that it becomes easier to lookup?
    425         }
    426 
    427 
    428296        void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
    429297                PassVisitor<HoistStruct> hoister;
     
    435303        }
    436304
    437         namespace {
    438                 void qualifiedName( AggregateDecl * aggr, std::ostringstream & ss ) {
    439                         if ( aggr->parent ) qualifiedName( aggr->parent, ss );
    440                         ss << "__" << aggr->name;
    441                 }
    442 
    443                 // mangle nested type names using entire parent chain
    444                 std::string qualifiedName( AggregateDecl * aggr ) {
    445                         std::ostringstream ss;
    446                         qualifiedName( aggr, ss );
    447                         return ss.str();
    448                 }
    449         }
    450 
    451305        template< typename AggDecl >
    452306        void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
    453307                if ( parentAggr ) {
    454                         aggregateDecl->parent = parentAggr;
    455                         aggregateDecl->name = qualifiedName( aggregateDecl );
    456308                        // Add elements in stack order corresponding to nesting structure.
    457309                        declsToAddBefore.push_front( aggregateDecl );
     
    464316        }
    465317
     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
    466336        void HoistStruct::previsit( StaticAssertDecl * assertDecl ) {
    467337                if ( parentAggr ) {
     
    478348        }
    479349
    480         void HoistStruct::previsit( StructInstType * type ) {
    481                 // need to reset type name after expanding to qualified name
    482                 assert( type->baseStruct );
    483                 type->name = type->baseStruct->name;
    484         }
    485 
    486         void HoistStruct::previsit( UnionInstType * type ) {
    487                 assert( type->baseUnion );
    488                 type->name = type->baseUnion->name;
    489         }
    490 
    491         void HoistStruct::previsit( EnumInstType * type ) {
    492                 assert( type->baseEnum );
    493                 type->name = type->baseEnum->name;
    494         }
    495 
    496 
    497         bool isTypedef( Declaration *decl ) {
    498                 return dynamic_cast< TypedefDecl * >( decl );
    499         }
    500 
    501         void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
    502                 PassVisitor<EliminateTypedef> eliminator;
    503                 acceptAll( translationUnit, eliminator );
    504                 filter( translationUnit, isTypedef, true );
    505         }
    506 
    507         template< typename AggDecl >
    508         void EliminateTypedef::handleAggregate( AggDecl *aggregateDecl ) {
    509                 filter( aggregateDecl->members, isTypedef, true );
    510         }
    511 
    512         void EliminateTypedef::previsit( StructDecl * aggregateDecl ) {
    513                 handleAggregate( aggregateDecl );
    514         }
    515 
    516         void EliminateTypedef::previsit( UnionDecl * aggregateDecl ) {
    517                 handleAggregate( aggregateDecl );
    518         }
    519 
    520         void EliminateTypedef::previsit( CompoundStmt * compoundStmt ) {
    521                 // remove and delete decl stmts
    522                 filter( compoundStmt->kids, [](Statement * stmt) {
    523                         if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {
    524                                 if ( dynamic_cast< TypedefDecl * >( declStmt->decl ) ) {
    525                                         return true;
    526                                 } // if
    527                         } // if
    528                         return false;
    529                 }, true);
    530         }
    531 
    532350        void EnumAndPointerDecay::previsit( EnumDecl *enumDecl ) {
    533351                // Set the type of each member of the enumeration to be EnumConstant
    534                 for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) {
     352                for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
    535353                        ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
    536354                        assert( obj );
    537                         obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) );
     355                        obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->get_name() ) );
    538356                } // for
    539357        }
     
    577395
    578396        void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) {
    579                 EnumDecl *st = local_indexer->lookupEnum( enumInst->name );
     397                EnumDecl *st = local_indexer->lookupEnum( enumInst->get_name() );
    580398                // it's not a semantic error if the enum is not found, just an implicit forward declaration
    581399                if ( st ) {
    582                         enumInst->baseEnum = st;
    583                 } // if
    584                 if ( ! st || ! st->body ) {
     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() ) {
    585404                        // use of forward declaration
    586                         forwardEnums[ enumInst->name ].push_back( enumInst );
     405                        forwardEnums[ enumInst->get_name() ].push_back( enumInst );
    587406                } // if
    588407        }
     
    597416
    598417        void LinkReferenceToTypes::postvisit( StructInstType *structInst ) {
    599                 StructDecl *st = local_indexer->lookupStruct( structInst->name );
     418                StructDecl *st = local_indexer->lookupStruct( structInst->get_name() );
    600419                // it's not a semantic error if the struct is not found, just an implicit forward declaration
    601420                if ( st ) {
    602                         structInst->baseStruct = st;
    603                 } // if
    604                 if ( ! st || ! st->body ) {
     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() ) {
    605425                        // use of forward declaration
    606                         forwardStructs[ structInst->name ].push_back( structInst );
     426                        forwardStructs[ structInst->get_name() ].push_back( structInst );
    607427                } // if
    608428                checkGenericParameters( structInst );
     
    610430
    611431        void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) {
    612                 UnionDecl *un = local_indexer->lookupUnion( unionInst->name );
     432                UnionDecl *un = local_indexer->lookupUnion( unionInst->get_name() );
    613433                // it's not a semantic error if the union is not found, just an implicit forward declaration
    614434                if ( un ) {
    615                         unionInst->baseUnion = un;
    616                 } // if
    617                 if ( ! un || ! un->body ) {
     435                        unionInst->set_baseUnion( un );
     436                } // if
     437                if ( ! un || un->get_members().empty() ) {
    618438                        // use of forward declaration
    619                         forwardUnions[ unionInst->name ].push_back( unionInst );
     439                        forwardUnions[ unionInst->get_name() ].push_back( unionInst );
    620440                } // if
    621441                checkGenericParameters( unionInst );
    622         }
    623 
    624         void LinkReferenceToTypes::previsit( QualifiedType * ) {
    625                 visit_children = false;
    626         }
    627 
    628         void LinkReferenceToTypes::postvisit( QualifiedType * qualType ) {
    629                 // linking only makes sense for the 'oldest ancestor' of the qualified type
    630                 qualType->parent->accept( *visitor );
    631442        }
    632443
     
    639450                        DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 );
    640451                        if ( dwt1 && dwt2 ) {
    641                                 if ( dwt1->name == dwt2->name && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) {
     452                                if ( dwt1->get_name() == dwt2->get_name() && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) {
    642453                                        // std::cerr << "=========== equal:" << std::endl;
    643454                                        // std::cerr << "d1: " << d1 << std::endl;
     
    664475        template< typename Iterator >
    665476        void expandAssertions( TraitInstType * inst, Iterator out ) {
    666                 assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) );
     477                assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toString( inst ).c_str() );
    667478                std::list< DeclarationWithType * > asserts;
    668479                for ( Declaration * decl : inst->baseTrait->members ) {
     
    701512                        SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name );
    702513                } // if
    703                 if ( traitDecl->parameters.size() != traitInst->parameters.size() ) {
     514                if ( traitDecl->get_parameters().size() != traitInst->get_parameters().size() ) {
    704515                        SemanticError( traitInst, "incorrect number of trait parameters: " );
    705516                } // if
     
    707518
    708519                // need to carry over the 'sized' status of each decl in the instance
    709                 for ( auto p : group_iterate( traitDecl->parameters, traitInst->parameters ) ) {
     520                for ( auto p : group_iterate( traitDecl->get_parameters(), traitInst->get_parameters() ) ) {
    710521                        TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) );
    711522                        if ( ! expr ) {
     
    714525                        if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) {
    715526                                TypeDecl * formalDecl = std::get<0>(p);
    716                                 TypeDecl * instDecl = inst->baseType;
     527                                TypeDecl * instDecl = inst->get_baseType();
    717528                                if ( formalDecl->get_sized() ) instDecl->set_sized( true );
    718529                        }
     
    723534        void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) {
    724535                // visit enum members first so that the types of self-referencing members are updated properly
    725                 if ( enumDecl->body ) {
    726                         ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->name );
     536                if ( ! enumDecl->get_members().empty() ) {
     537                        ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->get_name() );
    727538                        if ( fwds != forwardEnums.end() ) {
    728539                                for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    729                                         (*inst)->baseEnum = enumDecl;
     540                                        (*inst )->set_baseEnum( enumDecl );
    730541                                } // for
    731542                                forwardEnums.erase( fwds );
     
    763574                // visit struct members first so that the types of self-referencing members are updated properly
    764575                // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults)
    765                 if ( structDecl->body ) {
    766                         ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->name );
     576                if ( ! structDecl->get_members().empty() ) {
     577                        ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
    767578                        if ( fwds != forwardStructs.end() ) {
    768579                                for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    769                                         (*inst)->baseStruct = structDecl;
     580                                        (*inst )->set_baseStruct( structDecl );
    770581                                } // for
    771582                                forwardStructs.erase( fwds );
     
    775586
    776587        void LinkReferenceToTypes::postvisit( UnionDecl *unionDecl ) {
    777                 if ( unionDecl->body ) {
    778                         ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name );
     588                if ( ! unionDecl->get_members().empty() ) {
     589                        ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
    779590                        if ( fwds != forwardUnions.end() ) {
    780591                                for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    781                                         (*inst)->baseUnion = unionDecl;
     592                                        (*inst )->set_baseUnion( unionDecl );
    782593                                } // for
    783594                                forwardUnions.erase( fwds );
     
    789600                // ensure generic parameter instances are renamed like the base type
    790601                if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name;
    791                 if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) {
     602                if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->get_name() ) ) {
    792603                        if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
    793604                                typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
     
    868679
    869680
    870         void ReplaceTypedef::replaceTypedef( std::list< Declaration * > &translationUnit ) {
    871                 PassVisitor<ReplaceTypedef> eliminator;
     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;
    872687                mutateAll( translationUnit, eliminator );
    873688                if ( eliminator.pass.typedefNames.count( "size_t" ) ) {
    874689                        // grab and remember declaration of size_t
    875                         SizeType = eliminator.pass.typedefNames["size_t"].first->base->clone();
     690                        SizeType = eliminator.pass.typedefNames["size_t"].first->get_base()->clone();
    876691                } else {
    877692                        // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
     
    879694                        SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    880695                }
    881         }
    882 
    883         void ReplaceTypedef::premutate( QualifiedType * ) {
    884                 visit_children = false;
    885         }
    886 
    887         Type * ReplaceTypedef::postmutate( QualifiedType * qualType ) {
    888                 // replacing typedefs only makes sense for the 'oldest ancestor' of the qualified type
    889                 qualType->parent = qualType->parent->acceptMutator( *visitor );
    890                 return qualType;
    891         }
    892 
    893         Type * ReplaceTypedef::postmutate( TypeInstType * typeInst ) {
     696                filter( translationUnit, isTypedef, true );
     697        }
     698
     699        Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) {
    894700                // instances of typedef types will come here. If it is an instance
    895701                // of a typdef type, link the instance to its actual type.
    896                 TypedefMap::const_iterator def = typedefNames.find( typeInst->name );
     702                TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
    897703                if ( def != typedefNames.end() ) {
    898704                        Type *ret = def->second.first->base->clone();
     
    911717                                        SemanticError( typeInst->location, "Cannot apply type parameters to base type of " + typeInst->name );
    912718                                }
    913                                 rtt->parameters.clear();
     719                                rtt->get_parameters().clear();
    914720                                cloneAll( typeInst->parameters, rtt->parameters );
    915721                                mutateAll( rtt->parameters, *visitor );  // recursively fix typedefs on parameters
     
    918724                        return ret;
    919725                } else {
    920                         TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->name );
     726                        TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() );
    921727                        assertf( base != typedeclNames.end(), "Cannot find typedecl name %s", typeInst->name.c_str() );
    922728                        typeInst->set_baseType( base->second );
     
    939745        }
    940746
    941         Declaration * ReplaceTypedef::postmutate( TypedefDecl * tyDecl ) {
    942                 if ( typedefNames.count( tyDecl->name ) == 1 && typedefNames[ tyDecl->name ].second == scopeLevel ) {
     747        Declaration *EliminateTypedef::postmutate( TypedefDecl * tyDecl ) {
     748                if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
    943749                        // typedef to the same name from the same scope
    944750                        // must be from the same type
    945751
    946                         Type * t1 = tyDecl->base;
    947                         Type * t2 = typedefNames[ tyDecl->name ].first->base;
     752                        Type * t1 = tyDecl->get_base();
     753                        Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
    948754                        if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
    949755                                SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name );
     
    957763                        }
    958764                } else {
    959                         typedefNames[ tyDecl->name ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
     765                        typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
    960766                } // if
    961767
     
    969775                // Note, qualifiers on the typedef are superfluous for the forward declaration.
    970776
    971                 Type *designatorType = tyDecl->base->stripDeclarator();
     777                Type *designatorType = tyDecl->get_base()->stripDeclarator();
    972778                if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) {
    973                         declsToAddBefore.push_back( new StructDecl( aggDecl->name, DeclarationNode::Struct, noAttributes, tyDecl->linkage ) );
     779                        return new StructDecl( aggDecl->get_name(), DeclarationNode::Struct, noAttributes, tyDecl->get_linkage() );
    974780                } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) {
    975                         declsToAddBefore.push_back( new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage ) );
     781                        return new UnionDecl( aggDecl->get_name(), noAttributes, tyDecl->get_linkage() );
    976782                } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) {
    977                         declsToAddBefore.push_back( new EnumDecl( enumDecl->name, noAttributes, tyDecl->linkage ) );
    978                 } // if
    979                 return tyDecl->clone();
    980         }
    981 
    982         void ReplaceTypedef::premutate( TypeDecl * typeDecl ) {
    983                 TypedefMap::iterator i = typedefNames.find( typeDecl->name );
     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() );
    984791                if ( i != typedefNames.end() ) {
    985792                        typedefNames.erase( i ) ;
    986793                } // if
    987794
    988                 typedeclNames[ typeDecl->name ] = typeDecl;
    989         }
    990 
    991         void ReplaceTypedef::premutate( FunctionDecl * ) {
     795                typedeclNames[ typeDecl->get_name() ] = typeDecl;
     796        }
     797
     798        void EliminateTypedef::premutate( FunctionDecl * ) {
    992799                GuardScope( typedefNames );
    993800        }
    994801
    995         void ReplaceTypedef::premutate( ObjectDecl * ) {
     802        void EliminateTypedef::premutate( ObjectDecl * ) {
    996803                GuardScope( typedefNames );
    997804        }
    998805
    999         DeclarationWithType * ReplaceTypedef::postmutate( ObjectDecl * objDecl ) {
    1000                 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type?
     806        DeclarationWithType *EliminateTypedef::postmutate( ObjectDecl * objDecl ) {
     807                if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->get_type() ) ) { // function type?
    1001808                        // replace the current object declaration with a function declaration
    1002                         FunctionDecl * newDecl = new FunctionDecl( objDecl->name, objDecl->get_storageClasses(), objDecl->linkage, funtype, 0, objDecl->attributes, objDecl->get_funcSpec() );
    1003                         objDecl->attributes.clear();
     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();
    1004811                        objDecl->set_type( nullptr );
    1005812                        delete objDecl;
     
    1009816        }
    1010817
    1011         void ReplaceTypedef::premutate( CastExpr * ) {
     818        void EliminateTypedef::premutate( CastExpr * ) {
    1012819                GuardScope( typedefNames );
    1013820        }
    1014821
    1015         void ReplaceTypedef::premutate( CompoundStmt * ) {
     822        void EliminateTypedef::premutate( CompoundStmt * ) {
    1016823                GuardScope( typedefNames );
    1017824                scopeLevel += 1;
     
    1019826        }
    1020827
     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
    1021843        template<typename AggDecl>
    1022         void ReplaceTypedef::addImplicitTypedef( AggDecl * 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 ) {
    1023851                if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
    1024852                        Type *type = nullptr;
     
    1032860                        TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type, aggDecl->get_linkage() ) );
    1033861                        typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel );
    1034                         // add the implicit typedef to the AST
    1035                         declsToAddBefore.push_back( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type->clone(), aggDecl->get_linkage() ) );
    1036                 } // if
    1037         }
    1038 
    1039         template< typename AggDecl >
    1040         void ReplaceTypedef::handleAggregate( AggDecl * aggr ) {
    1041                 SemanticErrorException errors;
    1042 
    1043                 ValueGuard< std::list<Declaration * > > oldBeforeDecls( declsToAddBefore );
    1044                 ValueGuard< std::list<Declaration * > > oldAfterDecls ( declsToAddAfter  );
    1045                 declsToAddBefore.clear();
    1046                 declsToAddAfter.clear();
    1047 
    1048                 GuardScope( typedefNames );
    1049                 mutateAll( aggr->parameters, *visitor );
    1050 
    1051                 // unroll mutateAll for aggr->members so that implicit typedefs for nested types are added to the aggregate body.
    1052                 for ( std::list< Declaration * >::iterator i = aggr->members.begin(); i != aggr->members.end(); ++i ) {
    1053                         if ( !declsToAddAfter.empty() ) { aggr->members.splice( i, declsToAddAfter ); }
    1054 
    1055                         try {
    1056                                 *i = maybeMutate( *i, *visitor );
    1057                         } catch ( SemanticErrorException &e ) {
    1058                                 errors.append( e );
    1059                         }
    1060 
    1061                         if ( !declsToAddBefore.empty() ) { aggr->members.splice( i, declsToAddBefore ); }
    1062                 }
    1063 
    1064                 if ( !declsToAddAfter.empty() ) { aggr->members.splice( aggr->members.end(), declsToAddAfter ); }
    1065                 if ( !errors.isEmpty() ) { throw errors; }
    1066         }
    1067 
    1068         void ReplaceTypedef::premutate( StructDecl * structDecl ) {
    1069                 visit_children = false;
     862                } // if
     863        }
     864
     865        void EliminateTypedef::premutate( StructDecl * structDecl ) {
    1070866                addImplicitTypedef( structDecl );
    1071                 handleAggregate( structDecl );
    1072         }
    1073 
    1074         void ReplaceTypedef::premutate( UnionDecl * unionDecl ) {
    1075                 visit_children = false;
     867        }
     868
     869
     870        Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) {
     871                return handleAggregate( structDecl );
     872        }
     873
     874        void EliminateTypedef::premutate( UnionDecl * unionDecl ) {
    1076875                addImplicitTypedef( unionDecl );
    1077                 handleAggregate( unionDecl );
    1078         }
    1079 
    1080         void ReplaceTypedef::premutate( EnumDecl * enumDecl ) {
     876        }
     877
     878        Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) {
     879                return handleAggregate( unionDecl );
     880        }
     881
     882        void EliminateTypedef::premutate( EnumDecl * enumDecl ) {
    1081883                addImplicitTypedef( enumDecl );
    1082884        }
    1083885
    1084         void ReplaceTypedef::premutate( FunctionType * ) {
     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 * ) {
    1085895                GuardValue( inFunctionType );
    1086896                inFunctionType = true;
     
    12141024
    12151025        void ArrayLength::previsit( ObjectDecl * objDecl ) {
    1216                 if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->type ) ) {
     1026                if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) {
    12171027                        if ( at->get_dimension() ) return;
    1218                         if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->init ) ) {
    1219                                 at->set_dimension( new ConstantExpr( Constant::from_ulong( init->initializers.size() ) ) );
     1028                        if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->get_init() ) ) {
     1029                                at->set_dimension( new ConstantExpr( Constant::from_ulong( init->get_initializers().size() ) ) );
    12201030                        }
    12211031                }
Note: See TracChangeset for help on using the changeset viewer.