Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    ra506df4 r5809461  
    176176        };
    177177
    178         struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards {
     178        class EliminateTypedef : public Mutator {
     179          public:
    179180                EliminateTypedef() : scopeLevel( 0 ) {}
    180181                /// Replaces typedefs by forward declarations
    181182                static void eliminateTypedef( std::list< Declaration * > &translationUnit );
    182 
    183                 Type * postmutate( TypeInstType * aggregateUseType );
    184                 Declaration * postmutate( TypedefDecl * typeDecl );
    185                 void premutate( TypeDecl * typeDecl );
    186                 void premutate( FunctionDecl * funcDecl );
    187                 void premutate( ObjectDecl * objDecl );
    188                 DeclarationWithType * postmutate( ObjectDecl * objDecl );
    189 
    190                 void premutate( CastExpr * castExpr );
    191 
    192                 void premutate( CompoundStmt * compoundStmt );
    193                 CompoundStmt * postmutate( CompoundStmt * compoundStmt );
    194 
    195                 void premutate( StructDecl * structDecl );
    196                 Declaration * postmutate( StructDecl * structDecl );
    197                 void premutate( UnionDecl * unionDecl );
    198                 Declaration * postmutate( UnionDecl * unionDecl );
    199                 void premutate( EnumDecl * enumDecl );
    200                 Declaration * postmutate( EnumDecl * enumDecl );
    201                 Declaration * postmutate( TraitDecl * contextDecl );
    202 
    203183          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
    204197                template<typename AggDecl>
    205198                AggDecl *handleAggregate( AggDecl * aggDecl );
     
    674667
    675668        void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
    676                 PassVisitor<EliminateTypedef> eliminator;
     669                EliminateTypedef eliminator;
    677670                mutateAll( translationUnit, eliminator );
    678                 if ( eliminator.pass.typedefNames.count( "size_t" ) ) {
     671                if ( eliminator.typedefNames.count( "size_t" ) ) {
    679672                        // grab and remember declaration of size_t
    680                         SizeType = eliminator.pass.typedefNames["size_t"].first->get_base()->clone();
     673                        SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone();
    681674                } else {
    682675                        // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
     
    688681        }
    689682
    690         Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) {
     683        Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
    691684                // instances of typedef types will come here. If it is an instance
    692685                // of a typdef type, link the instance to its actual type.
     
    703696                                rtt->get_parameters().clear();
    704697                                cloneAll( typeInst->get_parameters(), rtt->get_parameters() );
    705                                 mutateAll( rtt->get_parameters(), *visitor );  // recursively fix typedefs on parameters
     698                                mutateAll( rtt->get_parameters(), *this );  // recursively fix typedefs on parameters
    706699                        } // if
    707700                        delete typeInst;
     
    715708        }
    716709
    717         Declaration *EliminateTypedef::postmutate( TypedefDecl * tyDecl ) {
     710        Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
     711                Declaration *ret = Mutator::mutate( tyDecl );
     712
    718713                if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
    719714                        // typedef to the same name from the same scope
     
    746741                        return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() );
    747742                } else {
    748                         return tyDecl->clone();
    749                 } // if
    750         }
    751 
    752         void EliminateTypedef::premutate( TypeDecl * typeDecl ) {
     743                        return ret->clone();
     744                } // if
     745        }
     746
     747        TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
    753748                TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
    754749                if ( i != typedefNames.end() ) {
     
    757752
    758753                typedeclNames[ typeDecl->get_name() ] = typeDecl;
    759         }
    760 
    761         void EliminateTypedef::premutate( FunctionDecl * ) {
    762                 GuardScope( typedefNames );
    763         }
    764 
    765         void EliminateTypedef::premutate( ObjectDecl * ) {
    766                 GuardScope( typedefNames );
    767         }
    768 
    769         DeclarationWithType *EliminateTypedef::postmutate( ObjectDecl * objDecl ) {
    770                 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?
    771770                        // replace the current object declaration with a function declaration
    772                         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() );
    773772                        objDecl->get_attributes().clear();
    774773                        objDecl->set_type( nullptr );
     
    776775                        return newDecl;
    777776                } // if
    778                 return objDecl;
    779         }
    780 
    781         void EliminateTypedef::premutate( CastExpr * ) {
    782                 GuardScope( typedefNames );
    783         }
    784 
    785         void EliminateTypedef::premutate( CompoundStmt * ) {
    786                 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();
    787789                scopeLevel += 1;
    788                 GuardAction( [this](){ scopeLevel -= 1; } );
    789         }
    790 
    791         CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) {
     790                CompoundStmt *ret = Mutator::mutate( compoundStmt );
     791                scopeLevel -= 1;
    792792                // remove and delete decl stmts
    793793                filter( compoundStmt->kids, [](Statement * stmt) {
     
    799799                        return false;
    800800                }, true);
    801                 return compoundStmt;
     801                typedefNames.endScope();
     802                return ret;
    802803        }
    803804
     
    826827        }
    827828
    828         void EliminateTypedef::premutate( StructDecl * structDecl ) {
     829        Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
    829830                addImplicitTypedef( structDecl );
    830         }
    831 
    832 
    833         Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) {
     831                Mutator::mutate( structDecl );
    834832                return handleAggregate( structDecl );
    835833        }
    836834
    837         void EliminateTypedef::premutate( UnionDecl * unionDecl ) {
     835        Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
    838836                addImplicitTypedef( unionDecl );
    839         }
    840 
    841         Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) {
     837                Mutator::mutate( unionDecl );
    842838                return handleAggregate( unionDecl );
    843839        }
    844840
    845         void EliminateTypedef::premutate( EnumDecl * enumDecl ) {
     841        Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
    846842                addImplicitTypedef( enumDecl );
    847         }
    848 
    849         Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) {
     843                Mutator::mutate( enumDecl );
    850844                return handleAggregate( enumDecl );
    851845        }
    852846
    853         Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) {
    854                 return handleAggregate( traitDecl );
     847        Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) {
     848                Mutator::mutate( contextDecl );
     849                return handleAggregate( contextDecl );
    855850        }
    856851
Note: See TracChangeset for help on using the changeset viewer.