Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    re3e16bc r522363e  
    123123
    124124        /// Associates forward declarations of aggregates with their definitions
    125         class LinkReferenceToTypes final : public Indexer {
    126                 typedef Indexer Parent;
    127           public:
    128                 LinkReferenceToTypes( bool doDebug, const Indexer *indexer );
    129                 using Parent::visit;
    130                 void visit( TypeInstType *typeInst ) final;
    131 
    132                 void visit( EnumInstType *enumInst ) final;
    133                 void visit( StructInstType *structInst ) final;
    134                 void visit( UnionInstType *unionInst ) final;
    135                 void visit( TraitInstType *traitInst ) final;
    136 
    137                 void visit( EnumDecl *enumDecl ) final;
    138                 void visit( StructDecl *structDecl ) final;
    139                 void visit( UnionDecl *unionDecl ) final;
    140                 void visit( TraitDecl * traitDecl ) final;
     125        struct LinkReferenceToTypes final : public WithIndexer {
     126                LinkReferenceToTypes( const Indexer *indexer );
     127                void postvisit( TypeInstType *typeInst );
     128
     129                void postvisit( EnumInstType *enumInst );
     130                void postvisit( StructInstType *structInst );
     131                void postvisit( UnionInstType *unionInst );
     132                void postvisit( TraitInstType *traitInst );
     133
     134                void postvisit( EnumDecl *enumDecl );
     135                void postvisit( StructDecl *structDecl );
     136                void postvisit( UnionDecl *unionDecl );
     137                void postvisit( TraitDecl * traitDecl );
    141138
    142139          private:
    143                 const Indexer *indexer;
     140                const Indexer *local_indexer;
    144141
    145142                typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType;
     
    152149
    153150        /// Replaces array and function types in forall lists by appropriate pointer type and assigns each Object and Function declaration a unique ID.
    154         class ForallPointerDecay final : public Indexer {
    155                 typedef Indexer Parent;
    156           public:
    157                 using Parent::visit;
    158                 ForallPointerDecay( const Indexer *indexer );
    159 
    160                 virtual void visit( ObjectDecl *object ) override;
    161                 virtual void visit( FunctionDecl *func ) override;
    162 
    163                 const Indexer *indexer;
     151        struct ForallPointerDecay final {
     152                void previsit( ObjectDecl *object );
     153                void previsit( FunctionDecl *func );
    164154        };
    165155
     
    176166        };
    177167
    178         class EliminateTypedef : public Mutator {
    179           public:
     168        struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards {
    180169                EliminateTypedef() : scopeLevel( 0 ) {}
    181170                /// Replaces typedefs by forward declarations
    182171                static void eliminateTypedef( std::list< Declaration * > &translationUnit );
     172
     173                Type * postmutate( TypeInstType * aggregateUseType );
     174                Declaration * postmutate( TypedefDecl * typeDecl );
     175                void premutate( TypeDecl * typeDecl );
     176                void premutate( FunctionDecl * funcDecl );
     177                void premutate( ObjectDecl * objDecl );
     178                DeclarationWithType * postmutate( ObjectDecl * objDecl );
     179
     180                void premutate( CastExpr * castExpr );
     181
     182                void premutate( CompoundStmt * compoundStmt );
     183                CompoundStmt * postmutate( CompoundStmt * compoundStmt );
     184
     185                void premutate( StructDecl * structDecl );
     186                Declaration * postmutate( StructDecl * structDecl );
     187                void premutate( UnionDecl * unionDecl );
     188                Declaration * postmutate( UnionDecl * unionDecl );
     189                void premutate( EnumDecl * enumDecl );
     190                Declaration * postmutate( EnumDecl * enumDecl );
     191                Declaration * postmutate( TraitDecl * contextDecl );
     192
    183193          private:
    184                 virtual Declaration *mutate( TypedefDecl *typeDecl );
    185                 virtual TypeDecl *mutate( TypeDecl *typeDecl );
    186                 virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
    187                 virtual DeclarationWithType *mutate( ObjectDecl *objDecl );
    188                 virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
    189                 virtual Type *mutate( TypeInstType *aggregateUseType );
    190                 virtual Expression *mutate( CastExpr *castExpr );
    191 
    192                 virtual Declaration *mutate( StructDecl * structDecl );
    193                 virtual Declaration *mutate( UnionDecl * unionDecl );
    194                 virtual Declaration *mutate( EnumDecl * enumDecl );
    195                 virtual Declaration *mutate( TraitDecl * contextDecl );
    196 
    197194                template<typename AggDecl>
    198195                AggDecl *handleAggregate( AggDecl * aggDecl );
     
    256253        };
    257254
    258         void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
     255        void validate( std::list< Declaration * > &translationUnit, __attribute__((unused)) bool doDebug ) {
    259256                PassVisitor<EnumAndPointerDecay> epc;
    260                 LinkReferenceToTypes lrt( doDebug, 0 );
    261                 ForallPointerDecay fpd( 0 );
     257                PassVisitor<LinkReferenceToTypes> lrt( nullptr );
     258                PassVisitor<ForallPointerDecay> fpd;
    262259                PassVisitor<CompoundLiteral> compoundliteral;
    263260                PassVisitor<ValidateGenericParameters> genericParams;
     
    286283        void validateType( Type *type, const Indexer *indexer ) {
    287284                PassVisitor<EnumAndPointerDecay> epc;
    288                 LinkReferenceToTypes lrt( false, indexer );
    289                 ForallPointerDecay fpd( indexer );
     285                PassVisitor<LinkReferenceToTypes> lrt( indexer );
     286                PassVisitor<ForallPointerDecay> fpd;
    290287                type->accept( epc );
    291288                type->accept( lrt );
     
    401398        }
    402399
    403         LinkReferenceToTypes::LinkReferenceToTypes( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
     400        LinkReferenceToTypes::LinkReferenceToTypes( const Indexer *other_indexer ) {
    404401                if ( other_indexer ) {
    405                         indexer = other_indexer;
     402                        local_indexer = other_indexer;
    406403                } else {
    407                         indexer = this;
    408                 } // if
    409         }
    410 
    411         void LinkReferenceToTypes::visit( EnumInstType *enumInst ) {
    412                 Parent::visit( enumInst );
    413                 EnumDecl *st = indexer->lookupEnum( enumInst->get_name() );
     404                        local_indexer = &indexer;
     405                } // if
     406        }
     407
     408        void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) {
     409                EnumDecl *st = local_indexer->lookupEnum( enumInst->get_name() );
    414410                // it's not a semantic error if the enum is not found, just an implicit forward declaration
    415411                if ( st ) {
     
    423419        }
    424420
    425         void LinkReferenceToTypes::visit( StructInstType *structInst ) {
    426                 Parent::visit( structInst );
    427                 StructDecl *st = indexer->lookupStruct( structInst->get_name() );
     421        void LinkReferenceToTypes::postvisit( StructInstType *structInst ) {
     422                StructDecl *st = local_indexer->lookupStruct( structInst->get_name() );
    428423                // it's not a semantic error if the struct is not found, just an implicit forward declaration
    429424                if ( st ) {
     
    437432        }
    438433
    439         void LinkReferenceToTypes::visit( UnionInstType *unionInst ) {
    440                 Parent::visit( unionInst );
    441                 UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
     434        void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) {
     435                UnionDecl *un = local_indexer->lookupUnion( unionInst->get_name() );
    442436                // it's not a semantic error if the union is not found, just an implicit forward declaration
    443437                if ( un ) {
     
    492486        }
    493487
    494         void LinkReferenceToTypes::visit( TraitDecl * traitDecl ) {
    495                 Parent::visit( traitDecl );
    496 
     488        void LinkReferenceToTypes::postvisit( TraitDecl * traitDecl ) {
    497489                if ( traitDecl->name == "sized" ) {
    498490                        // "sized" is a special trait - flick the sized status on for the type variable
     
    516508        }
    517509
    518         void LinkReferenceToTypes::visit( TraitInstType * traitInst ) {
    519                 Parent::visit( traitInst );
     510        void LinkReferenceToTypes::postvisit( TraitInstType * traitInst ) {
    520511                // handle other traits
    521                 TraitDecl *traitDecl = indexer->lookupTrait( traitInst->name );
     512                TraitDecl *traitDecl = local_indexer->lookupTrait( traitInst->name );
    522513                if ( ! traitDecl ) {
    523514                        throw SemanticError( "use of undeclared trait " + traitInst->name );
     
    540531        }
    541532
    542         void LinkReferenceToTypes::visit( EnumDecl *enumDecl ) {
     533        void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) {
    543534                // visit enum members first so that the types of self-referencing members are updated properly
    544                 Parent::visit( enumDecl );
    545535                if ( ! enumDecl->get_members().empty() ) {
    546536                        ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->get_name() );
     
    554544        }
    555545
    556         void LinkReferenceToTypes::visit( StructDecl *structDecl ) {
     546        void LinkReferenceToTypes::postvisit( StructDecl *structDecl ) {
    557547                // visit struct members first so that the types of self-referencing members are updated properly
    558                 // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and and their defaults)
    559                 Parent::visit( structDecl );
     548                // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults)
    560549                if ( ! structDecl->get_members().empty() ) {
    561550                        ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
     
    569558        }
    570559
    571         void LinkReferenceToTypes::visit( UnionDecl *unionDecl ) {
    572                 Parent::visit( unionDecl );
     560        void LinkReferenceToTypes::postvisit( UnionDecl *unionDecl ) {
    573561                if ( ! unionDecl->get_members().empty() ) {
    574562                        ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
     
    582570        }
    583571
    584         void LinkReferenceToTypes::visit( TypeInstType *typeInst ) {
    585                 if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
     572        void LinkReferenceToTypes::postvisit( TypeInstType *typeInst ) {
     573                if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->get_name() ) ) {
    586574                        if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
    587575                                typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
    588576                        } // if
    589                 } // if
    590         }
    591 
    592         ForallPointerDecay::ForallPointerDecay( const Indexer *other_indexer ) :  Indexer( false ) {
    593                 if ( other_indexer ) {
    594                         indexer = other_indexer;
    595                 } else {
    596                         indexer = this;
    597577                } // if
    598578        }
     
    626606        }
    627607
    628         void ForallPointerDecay::visit( ObjectDecl *object ) {
     608        void ForallPointerDecay::previsit( ObjectDecl *object ) {
    629609                forallFixer( object->get_type() );
    630610                if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
    631611                        forallFixer( pointer->get_base() );
    632612                } // if
    633                 Parent::visit( object );
    634613                object->fixUniqueId();
    635614        }
    636615
    637         void ForallPointerDecay::visit( FunctionDecl *func ) {
     616        void ForallPointerDecay::previsit( FunctionDecl *func ) {
    638617                forallFixer( func->get_type() );
    639                 Parent::visit( func );
    640618                func->fixUniqueId();
    641619        }
     
    667645
    668646        void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
    669                 EliminateTypedef eliminator;
     647                PassVisitor<EliminateTypedef> eliminator;
    670648                mutateAll( translationUnit, eliminator );
    671                 if ( eliminator.typedefNames.count( "size_t" ) ) {
     649                if ( eliminator.pass.typedefNames.count( "size_t" ) ) {
    672650                        // grab and remember declaration of size_t
    673                         SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone();
     651                        SizeType = eliminator.pass.typedefNames["size_t"].first->get_base()->clone();
    674652                } else {
    675653                        // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
     
    681659        }
    682660
    683         Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
     661        Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) {
    684662                // instances of typedef types will come here. If it is an instance
    685663                // of a typdef type, link the instance to its actual type.
     
    696674                                rtt->get_parameters().clear();
    697675                                cloneAll( typeInst->get_parameters(), rtt->get_parameters() );
    698                                 mutateAll( rtt->get_parameters(), *this );  // recursively fix typedefs on parameters
     676                                mutateAll( rtt->get_parameters(), *visitor );  // recursively fix typedefs on parameters
    699677                        } // if
    700678                        delete typeInst;
     
    708686        }
    709687
    710         Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
    711                 Declaration *ret = Mutator::mutate( tyDecl );
    712 
     688        Declaration *EliminateTypedef::postmutate( TypedefDecl * tyDecl ) {
    713689                if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
    714690                        // typedef to the same name from the same scope
     
    741717                        return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() );
    742718                } else {
    743                         return ret->clone();
    744                 } // if
    745         }
    746 
    747         TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
     719                        return tyDecl->clone();
     720                } // if
     721        }
     722
     723        void EliminateTypedef::premutate( TypeDecl * typeDecl ) {
    748724                TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
    749725                if ( i != typedefNames.end() ) {
     
    752728
    753729                typedeclNames[ typeDecl->get_name() ] = typeDecl;
    754                 return Mutator::mutate( typeDecl );
    755         }
    756 
    757         DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
    758                 typedefNames.beginScope();
    759                 DeclarationWithType *ret = Mutator::mutate( funcDecl );
    760                 typedefNames.endScope();
    761                 return ret;
    762         }
    763 
    764         DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
    765                 typedefNames.beginScope();
    766                 DeclarationWithType *ret = Mutator::mutate( objDecl );
    767                 typedefNames.endScope();
    768 
    769                 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) { // function type?
     730        }
     731
     732        void EliminateTypedef::premutate( FunctionDecl * ) {
     733                GuardScope( typedefNames );
     734        }
     735
     736        void EliminateTypedef::premutate( ObjectDecl * ) {
     737                GuardScope( typedefNames );
     738        }
     739
     740        DeclarationWithType *EliminateTypedef::postmutate( ObjectDecl * objDecl ) {
     741                if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->get_type() ) ) { // function type?
    770742                        // replace the current object declaration with a function declaration
    771                         FunctionDecl * newDecl = new FunctionDecl( ret->get_name(), ret->get_storageClasses(), ret->get_linkage(), funtype, 0, objDecl->get_attributes(), ret->get_funcSpec() );
     743                        FunctionDecl * newDecl = new FunctionDecl( objDecl->get_name(), objDecl->get_storageClasses(), objDecl->get_linkage(), funtype, 0, objDecl->get_attributes(), objDecl->get_funcSpec() );
    772744                        objDecl->get_attributes().clear();
    773745                        objDecl->set_type( nullptr );
     
    775747                        return newDecl;
    776748                } // if
    777                 return ret;
    778         }
    779 
    780         Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
    781                 typedefNames.beginScope();
    782                 Expression *ret = Mutator::mutate( castExpr );
    783                 typedefNames.endScope();
    784                 return ret;
    785         }
    786 
    787         CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
    788                 typedefNames.beginScope();
     749                return objDecl;
     750        }
     751
     752        void EliminateTypedef::premutate( CastExpr * ) {
     753                GuardScope( typedefNames );
     754        }
     755
     756        void EliminateTypedef::premutate( CompoundStmt * ) {
     757                GuardScope( typedefNames );
    789758                scopeLevel += 1;
    790                 CompoundStmt *ret = Mutator::mutate( compoundStmt );
    791                 scopeLevel -= 1;
     759                GuardAction( [this](){ scopeLevel -= 1; } );
     760        }
     761
     762        CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) {
    792763                // remove and delete decl stmts
    793764                filter( compoundStmt->kids, [](Statement * stmt) {
     
    799770                        return false;
    800771                }, true);
    801                 typedefNames.endScope();
    802                 return ret;
     772                return compoundStmt;
    803773        }
    804774
     
    827797        }
    828798
    829         Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
     799        void EliminateTypedef::premutate( StructDecl * structDecl ) {
    830800                addImplicitTypedef( structDecl );
    831                 Mutator::mutate( structDecl );
     801        }
     802
     803
     804        Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) {
    832805                return handleAggregate( structDecl );
    833806        }
    834807
    835         Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
     808        void EliminateTypedef::premutate( UnionDecl * unionDecl ) {
    836809                addImplicitTypedef( unionDecl );
    837                 Mutator::mutate( unionDecl );
     810        }
     811
     812        Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) {
    838813                return handleAggregate( unionDecl );
    839814        }
    840815
    841         Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
     816        void EliminateTypedef::premutate( EnumDecl * enumDecl ) {
    842817                addImplicitTypedef( enumDecl );
    843                 Mutator::mutate( enumDecl );
     818        }
     819
     820        Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) {
    844821                return handleAggregate( enumDecl );
    845822        }
    846823
    847         Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) {
    848                 Mutator::mutate( contextDecl );
    849                 return handleAggregate( contextDecl );
     824        Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) {
     825                return handleAggregate( traitDecl );
    850826        }
    851827
Note: See TracChangeset for help on using the changeset viewer.