Changeset d24d4e1


Ignore:
Timestamp:
Jun 23, 2017, 12:12:46 PM (7 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, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
2a7b3ca
Parents:
ba915fb5
Message:

convert more passes to PassVisitor?, fix PassVisitor? constructor bug, add WithDeclsToAdd? parent class

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.h

    rba915fb5 rd24d4e1  
    2626//                          stmtsToAddBefore or stmtsToAddAfter respectively.
    2727// | WithShortCircuiting  - provides the ability to skip visiting child nodes; set visit_children to false in pre{visit,mutate} to skip visiting children
    28 // | WithScopes           - provides the ability to save/restore data like a LIFO stack; to save, call GuardValue with the variable to save, the variable
     28// | WithGuards           - provides the ability to save/restore data like a LIFO stack; to save, call GuardValue with the variable to save, the variable
    2929//                          will automatically be restored to its previous value after the corresponding postvisit/postmutate teminates.
    3030//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     
    3232class PassVisitor final : public Visitor, public Mutator {
    3333public:
    34         PassVisitor() = default;
    3534
    3635        template< typename... Args >
     
    283282
    284283public:
    285         TypeSubstitution * env;
     284        TypeSubstitution * env = nullptr;
    286285};
    287286
     
    295294        std::list< Statement* > stmtsToAddAfter;
    296295};
     296
     297class WithDeclsToAdd {
     298protected:
     299        WithDeclsToAdd() = default;
     300        ~WithDeclsToAdd() = default;
     301
     302public:
     303        std::list< Declaration* > declsToAddBefore;
     304        std::list< Declaration* > declsToAddAfter;
     305};
     306
    297307class WithShortCircuiting {
    298308protected:
     
    304314};
    305315
    306 class WithScopes {
    307 protected:
    308         WithScopes() = default;
    309         ~WithScopes() = default;
     316class WithGuards {
     317protected:
     318        WithGuards() = default;
     319        ~WithGuards() = default;
    310320
    311321public:
     
    319329        }
    320330
     331        template< typename T >
     332        void GuardScope( T& val ) {
     333                val.beginScope();
     334                at_cleanup( []( void * val ) {
     335                        static_cast< T * >( val )->endScope();
     336                }, static_cast< void * >( & val ) );
     337        }
     338
    321339        template< typename Func >
    322         void GuardAction( Func&& func ) {
    323                 at_cleanup( std::forward<Func>( func ) );
     340        void GuardAction( Func func ) {
     341                at_cleanup( [func](__attribute__((unused)) void *) { func(); }, nullptr );
    324342        }
    325343};
     
    328346class WithVisitorRef {
    329347protected:
    330         WithVisitorRef() = default;
    331         ~WithVisitorRef() = default;
    332 
    333 public:
    334         PassVisitor<pass_type> * const visitor;
     348        WithVisitorRef() {}
     349        ~WithVisitorRef() {}
     350
     351public:
     352        PassVisitor<pass_type> * const visitor = nullptr;
    335353};
    336354
  • src/Common/PassVisitor.impl.h

    rba915fb5 rd24d4e1  
    6868        for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) {
    6969                // splice in new declarations after previous decl
    70                 if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); } 
     70                if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); }
    7171
    7272                if ( i == decls.end() ) break;
     
    8888        for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) {
    8989                // splice in new declarations after previous decl
    90                 if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); } 
     90                if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); }
    9191
    9292                if ( i == decls.end() ) break;
     
    181181Statement * PassVisitor< pass_type >::visitStatement( Statement * stmt ) {
    182182        return handleStatement( stmt, [this]( Statement * stmt ) {
    183                 maybeAccept( stmt, *this ); 
     183                maybeAccept( stmt, *this );
    184184                return stmt;
    185185        });
     
    212212                expr->accept( *this );
    213213                return expr;
    214         });             
     214        });
    215215}
    216216
  • src/InitTweak/GenInit.cc

    rba915fb5 rd24d4e1  
    4444        }
    4545
    46         class ReturnFixer : public WithStmtsToAdd, public WithScopes {
    47           public:
     46        struct ReturnFixer : public WithStmtsToAdd, public WithGuards {
    4847                /// consistently allocates a temporary variable for the return value
    4948                /// of a function so that anything which the resolver decides can be constructed
     
    5958        };
    6059
    61         class CtorDtor final : public GenPoly::PolyMutator {
    62           public:
    63                 typedef GenPoly::PolyMutator Parent;
    64                 using Parent::mutate;
     60        struct CtorDtor : public WithGuards, public WithShortCircuiting  {
    6561                /// create constructor and destructor statements for object declarations.
    6662                /// the actual call statements will be added in after the resolver has run
     
    6965                static void generateCtorDtor( std::list< Declaration * > &translationUnit );
    7066
    71                 virtual DeclarationWithType * mutate( ObjectDecl * ) override;
    72                 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override;
     67                void previsit( ObjectDecl * );
     68                void previsit( FunctionDecl *functionDecl );
     69
    7370                // should not traverse into any of these declarations to find objects
    7471                // that need to be constructed or destructed
    75                 virtual Declaration* mutate( StructDecl *aggregateDecl ) override;
    76                 virtual Declaration* mutate( UnionDecl *aggregateDecl ) override { return aggregateDecl; }
    77                 virtual Declaration* mutate( EnumDecl *aggregateDecl ) override { return aggregateDecl; }
    78                 virtual Declaration* mutate( TraitDecl *aggregateDecl ) override { return aggregateDecl; }
    79                 virtual TypeDecl* mutate( TypeDecl *typeDecl ) override { return typeDecl; }
    80                 virtual Declaration* mutate( TypedefDecl *typeDecl ) override { return typeDecl; }
    81 
    82                 virtual Type * mutate( FunctionType *funcType ) override { return funcType; }
    83 
    84                 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) override;
     72                void previsit( StructDecl *aggregateDecl );
     73                void previsit( UnionDecl *aggregateDecl ) { visit_children = false; }
     74                void previsit( EnumDecl *aggregateDecl ) { visit_children = false; }
     75                void previsit( TraitDecl *aggregateDecl ) { visit_children = false; }
     76                void previsit( TypeDecl *typeDecl ) { visit_children = false; }
     77                void previsit( TypedefDecl *typeDecl ) { visit_children = false; }
     78
     79                void previsit( FunctionType *funcType ) { visit_children = false; }
     80
     81                void previsit( CompoundStmt * compoundStmt );
    8582
    8683          private:
     
    211208
    212209        void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
    213                 CtorDtor ctordtor;
    214                 mutateAll( translationUnit, ctordtor );
     210                PassVisitor<CtorDtor> ctordtor;
     211                acceptAll( translationUnit, ctordtor );
    215212        }
    216213
     
    289286        }
    290287
    291         DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) {
     288        void CtorDtor::previsit( ObjectDecl * objDecl ) {
    292289                handleDWT( objDecl );
    293290                // hands off if @=, extern, builtin, etc.
     
    301298                        objDecl->set_init( genCtorInit( objDecl ) );
    302299                }
    303                 return Parent::mutate( objDecl );
    304         }
    305 
    306         DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
    307                 ValueGuard< bool > oldInFunc = inFunction;
     300        }
     301
     302        void CtorDtor::previsit( FunctionDecl *functionDecl ) {
     303                GuardValue( inFunction );
    308304                inFunction = true;
    309305
    310306                handleDWT( functionDecl );
    311307
    312                 managedTypes.beginScope();
     308                GuardScope( managedTypes );
    313309                // go through assertions and recursively add seen ctor/dtors
    314310                for ( auto & tyDecl : functionDecl->get_functionType()->get_forall() ) {
     
    317313                        }
    318314                }
    319                 // parameters should not be constructed and destructed, so don't mutate FunctionType
    320                 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
    321 
    322                 managedTypes.endScope();
    323                 return functionDecl;
    324         }
    325 
    326         Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) {
     315
     316                PassVisitor<CtorDtor> newCtorDtor;
     317                newCtorDtor.pass = *this;
     318                maybeAccept( functionDecl->get_statements(), newCtorDtor );
     319                visit_children = false;  // do not try and construct parameters or forall parameters - must happen after maybeAccept
     320        }
     321
     322        void CtorDtor::previsit( StructDecl *aggregateDecl ) {
     323                visit_children = false; // do not try to construct and destruct aggregate members
     324
    327325                // don't construct members, but need to take note if there is a managed member,
    328326                // because that means that this type is also managed
     
    336334                        }
    337335                }
    338                 return aggregateDecl;
    339         }
    340 
    341         CompoundStmt * CtorDtor::mutate( CompoundStmt * compoundStmt ) {
    342                 managedTypes.beginScope();
    343                 CompoundStmt * stmt = Parent::mutate( compoundStmt );
    344                 managedTypes.endScope();
    345                 return stmt;
    346         }
    347 
     336        }
     337
     338        void CtorDtor::previsit( CompoundStmt * compoundStmt ) {
     339                GuardScope( managedTypes );
     340        }
    348341} // namespace InitTweak
    349342
  • src/SymTab/Validate.cc

    rba915fb5 rd24d4e1  
    106106
    107107        /// Fix return types so that every function returns exactly one value
    108         class ReturnTypeFixer {
    109           public:
     108        struct ReturnTypeFixer {
    110109                static void fix( std::list< Declaration * > &translationUnit );
    111110
     
    115114
    116115        /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
    117         class EnumAndPointerDecay {
    118         public:
     116        struct EnumAndPointerDecay {
    119117                void previsit( EnumDecl *aggregateDecl );
    120118                void previsit( FunctionType *func );
     
    159157        };
    160158
    161         class ReturnChecker : public WithScopes {
    162           public:
     159        struct ReturnChecker : public WithGuards {
    163160                /// Checks that return statements return nothing if their return type is void
    164161                /// and return something if the return type is non-void.
    165162                static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
    166           private:
     163
    167164                void previsit( FunctionDecl * functionDecl );
    168165                void previsit( ReturnStmt * returnStmt );
     
    205202        };
    206203
    207         class VerifyCtorDtorAssign {
    208         public:
     204        struct VerifyCtorDtorAssign {
    209205                /// ensure that constructors, destructors, and assignment have at least one
    210206                /// parameter, the first of which must be a pointer, and that ctor/dtors have no
     
    216212
    217213        /// ensure that generic types have the correct number of type arguments
    218         class ValidateGenericParameters {
    219         public:
     214        struct ValidateGenericParameters {
    220215                void previsit( StructInstType * inst );
    221216                void previsit( UnionInstType * inst );
    222217        };
    223218
    224         class ArrayLength {
    225         public:
     219        struct ArrayLength {
    226220                /// for array types without an explicit length, compute the length and store it so that it
    227221                /// is known to the rest of the phases. For example,
     
    236230        };
    237231
    238         class CompoundLiteral final : public GenPoly::DeclMutator {
     232        struct CompoundLiteral final : public WithDeclsToAdd, public WithVisitorRef<CompoundLiteral> {
    239233                Type::StorageClasses storageClasses;
    240234
    241                 using GenPoly::DeclMutator::mutate;
    242                 DeclarationWithType * mutate( ObjectDecl *objectDecl ) final;
    243                 Expression *mutate( CompoundLiteralExpr *compLitExpr ) final;
     235                void premutate( ObjectDecl *objectDecl );
     236                Expression * postmutate( CompoundLiteralExpr *compLitExpr );
    244237        };
    245238
     
    248241                LinkReferenceToTypes lrt( doDebug, 0 );
    249242                ForallPointerDecay fpd( 0 );
    250                 CompoundLiteral compoundliteral;
     243                PassVisitor<CompoundLiteral> compoundliteral;
    251244                PassVisitor<ValidateGenericParameters> genericParams;
    252245
     
    263256                Concurrency::implementThreadStarter( translationUnit );
    264257                ReturnChecker::checkFunctionReturns( translationUnit );
    265                 compoundliteral.mutateDeclarationList( translationUnit );
     258                mutateAll( translationUnit, compoundliteral );
    266259                acceptAll( translationUnit, fpd );
    267260                ArrayLength::computeLength( translationUnit );
     
    883876        }
    884877
    885         DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) {
     878        void CompoundLiteral::premutate( ObjectDecl *objectDecl ) {
    886879                storageClasses = objectDecl->get_storageClasses();
    887                 DeclarationWithType * temp = Mutator::mutate( objectDecl );
    888                 return temp;
    889         }
    890 
    891         Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) {
     880        }
     881
     882        Expression *CompoundLiteral::postmutate( CompoundLiteralExpr *compLitExpr ) {
    892883                // transform [storage_class] ... (struct S){ 3, ... };
    893884                // into [storage_class] struct S temp =  { 3, ... };
    894885                static UniqueName indexName( "_compLit" );
    895886
    896                 ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, 0, compLitExpr->get_result(), compLitExpr->get_initializer() );
    897                 compLitExpr->set_result( 0 );
    898                 compLitExpr->set_initializer( 0 );
     887                ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, nullptr, compLitExpr->get_result(), compLitExpr->get_initializer() );
     888                compLitExpr->set_result( nullptr );
     889                compLitExpr->set_initializer( nullptr );
    899890                delete compLitExpr;
    900                 DeclarationWithType * newtempvar = mutate( tempvar );
    901                 addDeclaration( newtempvar );                                   // add modified temporary to current block
    902                 return new VariableExpr( newtempvar );
     891                declsToAddBefore.push_back( tempvar );                                  // add modified temporary to current block
     892                return new VariableExpr( tempvar );
    903893        }
    904894
Note: See TracChangeset for help on using the changeset viewer.