Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    r522363e re3e16bc  
    123123
    124124        /// Associates forward declarations of aggregates with their definitions
    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 );
     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;
    138141
    139142          private:
    140                 const Indexer *local_indexer;
     143                const Indexer *indexer;
    141144
    142145                typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType;
     
    149152
    150153        /// Replaces array and function types in forall lists by appropriate pointer type and assigns each Object and Function declaration a unique ID.
    151         struct ForallPointerDecay final {
    152                 void previsit( ObjectDecl *object );
    153                 void previsit( FunctionDecl *func );
     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;
    154164        };
    155165
     
    166176        };
    167177
    168         struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards {
     178        class EliminateTypedef : public Mutator {
     179          public:
    169180                EliminateTypedef() : scopeLevel( 0 ) {}
    170181                /// Replaces typedefs by forward declarations
    171182                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 
    193183          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
    194197                template<typename AggDecl>
    195198                AggDecl *handleAggregate( AggDecl * aggDecl );
     
    253256        };
    254257
    255         void validate( std::list< Declaration * > &translationUnit, __attribute__((unused)) bool doDebug ) {
     258        void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
    256259                PassVisitor<EnumAndPointerDecay> epc;
    257                 PassVisitor<LinkReferenceToTypes> lrt( nullptr );
    258                 PassVisitor<ForallPointerDecay> fpd;
     260                LinkReferenceToTypes lrt( doDebug, 0 );
     261                ForallPointerDecay fpd( 0 );
    259262                PassVisitor<CompoundLiteral> compoundliteral;
    260263                PassVisitor<ValidateGenericParameters> genericParams;
     
    283286        void validateType( Type *type, const Indexer *indexer ) {
    284287                PassVisitor<EnumAndPointerDecay> epc;
    285                 PassVisitor<LinkReferenceToTypes> lrt( indexer );
    286                 PassVisitor<ForallPointerDecay> fpd;
     288                LinkReferenceToTypes lrt( false, indexer );
     289                ForallPointerDecay fpd( indexer );
    287290                type->accept( epc );
    288291                type->accept( lrt );
     
    398401        }
    399402
    400         LinkReferenceToTypes::LinkReferenceToTypes( const Indexer *other_indexer ) {
     403        LinkReferenceToTypes::LinkReferenceToTypes( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
    401404                if ( other_indexer ) {
    402                         local_indexer = other_indexer;
     405                        indexer = other_indexer;
    403406                } else {
    404                         local_indexer = &indexer;
    405                 } // if
    406         }
    407 
    408         void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) {
    409                 EnumDecl *st = local_indexer->lookupEnum( enumInst->get_name() );
     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() );
    410414                // it's not a semantic error if the enum is not found, just an implicit forward declaration
    411415                if ( st ) {
     
    419423        }
    420424
    421         void LinkReferenceToTypes::postvisit( StructInstType *structInst ) {
    422                 StructDecl *st = local_indexer->lookupStruct( structInst->get_name() );
     425        void LinkReferenceToTypes::visit( StructInstType *structInst ) {
     426                Parent::visit( structInst );
     427                StructDecl *st = indexer->lookupStruct( structInst->get_name() );
    423428                // it's not a semantic error if the struct is not found, just an implicit forward declaration
    424429                if ( st ) {
     
    432437        }
    433438
    434         void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) {
    435                 UnionDecl *un = local_indexer->lookupUnion( unionInst->get_name() );
     439        void LinkReferenceToTypes::visit( UnionInstType *unionInst ) {
     440                Parent::visit( unionInst );
     441                UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
    436442                // it's not a semantic error if the union is not found, just an implicit forward declaration
    437443                if ( un ) {
     
    486492        }
    487493
    488         void LinkReferenceToTypes::postvisit( TraitDecl * traitDecl ) {
     494        void LinkReferenceToTypes::visit( TraitDecl * traitDecl ) {
     495                Parent::visit( traitDecl );
     496
    489497                if ( traitDecl->name == "sized" ) {
    490498                        // "sized" is a special trait - flick the sized status on for the type variable
     
    508516        }
    509517
    510         void LinkReferenceToTypes::postvisit( TraitInstType * traitInst ) {
     518        void LinkReferenceToTypes::visit( TraitInstType * traitInst ) {
     519                Parent::visit( traitInst );
    511520                // handle other traits
    512                 TraitDecl *traitDecl = local_indexer->lookupTrait( traitInst->name );
     521                TraitDecl *traitDecl = indexer->lookupTrait( traitInst->name );
    513522                if ( ! traitDecl ) {
    514523                        throw SemanticError( "use of undeclared trait " + traitInst->name );
     
    531540        }
    532541
    533         void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) {
     542        void LinkReferenceToTypes::visit( EnumDecl *enumDecl ) {
    534543                // visit enum members first so that the types of self-referencing members are updated properly
     544                Parent::visit( enumDecl );
    535545                if ( ! enumDecl->get_members().empty() ) {
    536546                        ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->get_name() );
     
    544554        }
    545555
    546         void LinkReferenceToTypes::postvisit( StructDecl *structDecl ) {
     556        void LinkReferenceToTypes::visit( StructDecl *structDecl ) {
    547557                // visit struct members first so that the types of self-referencing members are updated properly
    548                 // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults)
     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 );
    549560                if ( ! structDecl->get_members().empty() ) {
    550561                        ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
     
    558569        }
    559570
    560         void LinkReferenceToTypes::postvisit( UnionDecl *unionDecl ) {
     571        void LinkReferenceToTypes::visit( UnionDecl *unionDecl ) {
     572                Parent::visit( unionDecl );
    561573                if ( ! unionDecl->get_members().empty() ) {
    562574                        ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
     
    570582        }
    571583
    572         void LinkReferenceToTypes::postvisit( TypeInstType *typeInst ) {
    573                 if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->get_name() ) ) {
     584        void LinkReferenceToTypes::visit( TypeInstType *typeInst ) {
     585                if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
    574586                        if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
    575587                                typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
    576588                        } // 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;
    577597                } // if
    578598        }
     
    606626        }
    607627
    608         void ForallPointerDecay::previsit( ObjectDecl *object ) {
     628        void ForallPointerDecay::visit( ObjectDecl *object ) {
    609629                forallFixer( object->get_type() );
    610630                if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
    611631                        forallFixer( pointer->get_base() );
    612632                } // if
     633                Parent::visit( object );
    613634                object->fixUniqueId();
    614635        }
    615636
    616         void ForallPointerDecay::previsit( FunctionDecl *func ) {
     637        void ForallPointerDecay::visit( FunctionDecl *func ) {
    617638                forallFixer( func->get_type() );
     639                Parent::visit( func );
    618640                func->fixUniqueId();
    619641        }
     
    645667
    646668        void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
    647                 PassVisitor<EliminateTypedef> eliminator;
     669                EliminateTypedef eliminator;
    648670                mutateAll( translationUnit, eliminator );
    649                 if ( eliminator.pass.typedefNames.count( "size_t" ) ) {
     671                if ( eliminator.typedefNames.count( "size_t" ) ) {
    650672                        // grab and remember declaration of size_t
    651                         SizeType = eliminator.pass.typedefNames["size_t"].first->get_base()->clone();
     673                        SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone();
    652674                } else {
    653675                        // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
     
    659681        }
    660682
    661         Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) {
     683        Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
    662684                // instances of typedef types will come here. If it is an instance
    663685                // of a typdef type, link the instance to its actual type.
     
    674696                                rtt->get_parameters().clear();
    675697                                cloneAll( typeInst->get_parameters(), rtt->get_parameters() );
    676                                 mutateAll( rtt->get_parameters(), *visitor );  // recursively fix typedefs on parameters
     698                                mutateAll( rtt->get_parameters(), *this );  // recursively fix typedefs on parameters
    677699                        } // if
    678700                        delete typeInst;
     
    686708        }
    687709
    688         Declaration *EliminateTypedef::postmutate( TypedefDecl * tyDecl ) {
     710        Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
     711                Declaration *ret = Mutator::mutate( tyDecl );
     712
    689713                if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
    690714                        // typedef to the same name from the same scope
     
    717741                        return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() );
    718742                } else {
    719                         return tyDecl->clone();
    720                 } // if
    721         }
    722 
    723         void EliminateTypedef::premutate( TypeDecl * typeDecl ) {
     743                        return ret->clone();
     744                } // if
     745        }
     746
     747        TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
    724748                TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
    725749                if ( i != typedefNames.end() ) {
     
    728752
    729753                typedeclNames[ typeDecl->get_name() ] = typeDecl;
    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?
     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?
    742770                        // replace the current object declaration with a function declaration
    743                         FunctionDecl * newDecl = new FunctionDecl( objDecl->get_name(), objDecl->get_storageClasses(), objDecl->get_linkage(), funtype, 0, objDecl->get_attributes(), objDecl->get_funcSpec() );
     771                        FunctionDecl * newDecl = new FunctionDecl( ret->get_name(), ret->get_storageClasses(), ret->get_linkage(), funtype, 0, objDecl->get_attributes(), ret->get_funcSpec() );
    744772                        objDecl->get_attributes().clear();
    745773                        objDecl->set_type( nullptr );
     
    747775                        return newDecl;
    748776                } // if
    749                 return objDecl;
    750         }
    751 
    752         void EliminateTypedef::premutate( CastExpr * ) {
    753                 GuardScope( typedefNames );
    754         }
    755 
    756         void EliminateTypedef::premutate( CompoundStmt * ) {
    757                 GuardScope( typedefNames );
     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();
    758789                scopeLevel += 1;
    759                 GuardAction( [this](){ scopeLevel -= 1; } );
    760         }
    761 
    762         CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) {
     790                CompoundStmt *ret = Mutator::mutate( compoundStmt );
     791                scopeLevel -= 1;
    763792                // remove and delete decl stmts
    764793                filter( compoundStmt->kids, [](Statement * stmt) {
     
    770799                        return false;
    771800                }, true);
    772                 return compoundStmt;
     801                typedefNames.endScope();
     802                return ret;
    773803        }
    774804
     
    797827        }
    798828
    799         void EliminateTypedef::premutate( StructDecl * structDecl ) {
     829        Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
    800830                addImplicitTypedef( structDecl );
    801         }
    802 
    803 
    804         Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) {
     831                Mutator::mutate( structDecl );
    805832                return handleAggregate( structDecl );
    806833        }
    807834
    808         void EliminateTypedef::premutate( UnionDecl * unionDecl ) {
     835        Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
    809836                addImplicitTypedef( unionDecl );
    810         }
    811 
    812         Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) {
     837                Mutator::mutate( unionDecl );
    813838                return handleAggregate( unionDecl );
    814839        }
    815840
    816         void EliminateTypedef::premutate( EnumDecl * enumDecl ) {
     841        Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
    817842                addImplicitTypedef( enumDecl );
    818         }
    819 
    820         Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) {
     843                Mutator::mutate( enumDecl );
    821844                return handleAggregate( enumDecl );
    822845        }
    823846
    824         Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) {
    825                 return handleAggregate( traitDecl );
     847        Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) {
     848                Mutator::mutate( contextDecl );
     849                return handleAggregate( contextDecl );
    826850        }
    827851
Note: See TracChangeset for help on using the changeset viewer.