Changeset 29f9e20


Ignore:
Timestamp:
Jun 14, 2018, 4:43:52 PM (6 years ago)
Author:
Rob Schluntz <rschlunt@…>
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:
c5d7701
Parents:
589a70b
Message:

Reorganize validate passes and reduce scope of HoistStruct? pass

Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    r589a70b r29f9e20  
    8080
    8181namespace SymTab {
     82        struct LinkNestedTypes final : public WithDeclsToAdd {
     83                void previsit( AggregateDecl * aggr );
     84
     85                void previsit( SizeofExpr * );
     86                void previsit( AlignofExpr * );
     87                void previsit( UntypedOffsetofExpr * );
     88                void handleType( Type * );
     89        };
     90
    8291        struct HoistStruct final : public WithDeclsToAdd, public WithGuards {
    8392                /// Flattens nested struct types
    8493                static void hoistStruct( std::list< Declaration * > &translationUnit );
    8594
    86                 void previsit( EnumInstType * enumInstType );
    87                 void previsit( StructInstType * structInstType );
    88                 void previsit( UnionInstType * unionInstType );
    8995                void previsit( StructDecl * aggregateDecl );
    9096                void previsit( UnionDecl * aggregateDecl );
     
    263269                PassVisitor<FindSpecialDeclarations> finder;
    264270                PassVisitor<LabelAddressFixer> labelAddrFixer;
    265 
     271                PassVisitor<LinkNestedTypes> nestedTypes;
     272
     273                acceptAll( translationUnit, nestedTypes );
    266274                EliminateTypedef::eliminateTypedef( translationUnit );
    267                 HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order
    268275                ReturnTypeFixer::fix( translationUnit ); // must happen before autogen
    269276                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
    270277                acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions
     278                HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order
    271279                acceptAll( translationUnit, genericParams );  // check as early as possible - can't happen before LinkReferenceToTypes
    272280                VerifyCtorDtorAssign::verify( translationUnit );  // must happen before autogen, because autogen examines existing ctor/dtors
     
    294302        }
    295303
     304
     305        void LinkNestedTypes::previsit( AggregateDecl * aggr ) {
     306                for ( auto it = aggr->members.begin(); it != aggr->members.end(); ) {
     307                        auto current = it++;
     308                        Declaration * member = *current;
     309                        if ( AggregateDecl * child = dynamic_cast<AggregateDecl *>( member ) ) {
     310                                child->parent = aggr;
     311                        }
     312                }
     313        }
     314
     315        void LinkNestedTypes::handleType( Type * type ) {
     316                // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here
     317                AggregateDecl * aggr = nullptr;
     318                if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) {
     319                        aggr = inst->baseStruct;
     320                } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) {
     321                        aggr = inst->baseUnion;
     322                } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( type ) ) {
     323                        aggr = inst->baseEnum;
     324                }
     325                if ( aggr && aggr->body ) {
     326                        declsToAddBefore.push_front( aggr );
     327                }
     328        }
     329
     330        void LinkNestedTypes::previsit( SizeofExpr * expr ) {
     331                handleType( expr->type );
     332        }
     333
     334        void LinkNestedTypes::previsit( AlignofExpr * expr ) {
     335                handleType( expr->type );
     336        }
     337
     338        void LinkNestedTypes::previsit( UntypedOffsetofExpr * expr ) {
     339                handleType( expr->type );
     340        }
     341
     342
    296343        void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
    297344                PassVisitor<HoistStruct> hoister;
     
    316363        }
    317364
    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 
    336365        void HoistStruct::previsit( StaticAssertDecl * assertDecl ) {
    337366                if ( parentAggr ) {
     
    398427                // it's not a semantic error if the enum is not found, just an implicit forward declaration
    399428                if ( st ) {
    400                         //assert( ! enumInst->get_baseEnum() || enumInst->get_baseEnum()->get_members().empty() || ! st->get_members().empty() );
    401429                        enumInst->baseEnum = st;
    402430                } // if
    403                 if ( ! st || st->members.empty() ) {
     431                if ( ! st || ! st->body ) {
    404432                        // use of forward declaration
    405433                        forwardEnums[ enumInst->name ].push_back( enumInst );
     
    419447                // it's not a semantic error if the struct is not found, just an implicit forward declaration
    420448                if ( st ) {
    421                         //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
    422449                        structInst->baseStruct = st;
    423450                } // if
    424                 if ( ! st || st->members.empty() ) {
     451                if ( ! st || ! st->body ) {
    425452                        // use of forward declaration
    426453                        forwardStructs[ structInst->name ].push_back( structInst );
     
    435462                        unionInst->baseUnion = un;
    436463                } // if
    437                 if ( ! un || un->members.empty() ) {
     464                if ( ! un || ! un->body ) {
    438465                        // use of forward declaration
    439466                        forwardUnions[ unionInst->name ].push_back( unionInst );
  • src/SynTree/Declaration.h

    r589a70b r29f9e20  
    266266        bool body;
    267267        std::list< Attribute * > attributes;
     268        AggregateDecl * parent = nullptr;
    268269
    269270        AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
Note: See TracChangeset for help on using the changeset viewer.