Changes in src/InitTweak/GenInit.cc [d24d4e1:ce8c12f]
- File:
-
- 1 edited
-
src/InitTweak/GenInit.cc (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/GenInit.cc
rd24d4e1 rce8c12f 16 16 #include <stack> 17 17 #include <list> 18 18 #include "GenInit.h" 19 19 #include "InitTweak.h" 20 #include "GenInit.h"21 22 #include "Common/PassVisitor.h"23 24 #include "GenPoly/DeclMutator.h"25 #include "GenPoly/PolyMutator.h"26 #include "GenPoly/ScopedSet.h"27 28 #include "ResolvExpr/typeops.h"29 30 20 #include "SynTree/Declaration.h" 21 #include "SynTree/Type.h" 31 22 #include "SynTree/Expression.h" 23 #include "SynTree/Statement.h" 32 24 #include "SynTree/Initializer.h" 33 25 #include "SynTree/Mutator.h" 34 #include "SynTree/Statement.h"35 #include "SynTree/Type.h"36 37 26 #include "SymTab/Autogen.h" 38 27 #include "SymTab/Mangler.h" 28 #include "GenPoly/PolyMutator.h" 29 #include "GenPoly/DeclMutator.h" 30 #include "GenPoly/ScopedSet.h" 31 #include "ResolvExpr/typeops.h" 39 32 40 33 namespace InitTweak { … … 44 37 } 45 38 46 struct ReturnFixer : public WithStmtsToAdd, public WithGuards { 39 class ReturnFixer final : public GenPoly::PolyMutator { 40 public: 47 41 /// consistently allocates a temporary variable for the return value 48 42 /// of a function so that anything which the resolver decides can be constructed … … 50 44 static void makeReturnTemp( std::list< Declaration * > &translationUnit ); 51 45 52 void premutate( FunctionDecl *functionDecl ); 53 void premutate( ReturnStmt * returnStmt ); 46 ReturnFixer(); 47 48 typedef GenPoly::PolyMutator Parent; 49 using Parent::mutate; 50 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override; 51 virtual Statement * mutate( ReturnStmt * returnStmt ) override; 54 52 55 53 protected: … … 58 56 }; 59 57 60 struct CtorDtor : public WithGuards, public WithShortCircuiting { 58 class CtorDtor final : public GenPoly::PolyMutator { 59 public: 60 typedef GenPoly::PolyMutator Parent; 61 using Parent::mutate; 61 62 /// create constructor and destructor statements for object declarations. 62 63 /// the actual call statements will be added in after the resolver has run … … 65 66 static void generateCtorDtor( std::list< Declaration * > &translationUnit ); 66 67 67 void previsit( ObjectDecl * ); 68 void previsit( FunctionDecl *functionDecl ); 69 68 virtual DeclarationWithType * mutate( ObjectDecl * ) override; 69 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override; 70 70 // should not traverse into any of these declarations to find objects 71 71 // that need to be constructed or destructed 72 v oid previsit( StructDecl *aggregateDecl );73 v oid previsit( UnionDecl *aggregateDecl ) { visit_children = false; }74 v oid previsit( EnumDecl *aggregateDecl ) { visit_children = false; }75 v oid previsit( TraitDecl *aggregateDecl ) { visit_children = false; }76 v oid previsit( TypeDecl *typeDecl ) { visit_children = false; }77 v oid previsit( TypedefDecl *typeDecl ) { visit_children = false; }78 79 v oid previsit( FunctionType *funcType ) { visit_children = false; }80 81 v oid previsit( CompoundStmt * compoundStmt );72 virtual Declaration* mutate( StructDecl *aggregateDecl ) override; 73 virtual Declaration* mutate( UnionDecl *aggregateDecl ) override { return aggregateDecl; } 74 virtual Declaration* mutate( EnumDecl *aggregateDecl ) override { return aggregateDecl; } 75 virtual Declaration* mutate( TraitDecl *aggregateDecl ) override { return aggregateDecl; } 76 virtual TypeDecl* mutate( TypeDecl *typeDecl ) override { return typeDecl; } 77 virtual Declaration* mutate( TypedefDecl *typeDecl ) override { return typeDecl; } 78 79 virtual Type * mutate( FunctionType *funcType ) override { return funcType; } 80 81 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) override; 82 82 83 83 private: … … 131 131 132 132 void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) { 133 PassVisitor<ReturnFixer>fixer;133 ReturnFixer fixer; 134 134 mutateAll( translationUnit, fixer ); 135 135 } 136 136 137 void ReturnFixer::premutate( ReturnStmt *returnStmt ) { 137 ReturnFixer::ReturnFixer() {} 138 139 Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) { 138 140 std::list< DeclarationWithType * > & returnVals = ftype->get_returnVals(); 139 141 assert( returnVals.size() == 0 || returnVals.size() == 1 ); … … 146 148 construct->get_args().push_back( new AddressExpr( new VariableExpr( returnVals.front() ) ) ); 147 149 construct->get_args().push_back( returnStmt->get_expr() ); 148 stmtsToAdd Before.push_back(new ExprStmt(noLabels, construct));150 stmtsToAdd.push_back(new ExprStmt(noLabels, construct)); 149 151 150 152 // return the retVal object 151 153 returnStmt->set_expr( new VariableExpr( returnVals.front() ) ); 152 154 } // if 153 } 154 155 void ReturnFixer::premutate( FunctionDecl *functionDecl ) { 156 GuardValue( ftype ); 157 GuardValue( funcName ); 155 return returnStmt; 156 } 157 158 DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) { 159 ValueGuard< FunctionType * > oldFtype( ftype ); 160 ValueGuard< std::string > oldFuncName( funcName ); 158 161 159 162 ftype = functionDecl->get_functionType(); 160 163 funcName = functionDecl->get_name(); 164 return Parent::mutate( functionDecl ); 161 165 } 162 166 … … 208 212 209 213 void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) { 210 PassVisitor<CtorDtor>ctordtor;211 acceptAll( translationUnit, ctordtor );214 CtorDtor ctordtor; 215 mutateAll( translationUnit, ctordtor ); 212 216 } 213 217 … … 239 243 std::list< DeclarationWithType * > & params = GenPoly::getFunctionType( dwt->get_type() )->get_parameters(); 240 244 assert( ! params.empty() ); 241 PointerType * type = safe_dynamic_cast< PointerType * >( params.front()->get_type() ); 242 managedTypes.insert( SymTab::Mangler::mangle( type->get_base() ) ); 245 Type * type = InitTweak::getPointerBase( params.front()->get_type() ); 246 assert( type ); 247 managedTypes.insert( SymTab::Mangler::mangle( type ) ); 243 248 } 244 249 } … … 286 291 } 287 292 288 void CtorDtor::previsit( ObjectDecl * objDecl ) {293 DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) { 289 294 handleDWT( objDecl ); 290 295 // hands off if @=, extern, builtin, etc. … … 298 303 objDecl->set_init( genCtorInit( objDecl ) ); 299 304 } 300 } 301 302 void CtorDtor::previsit( FunctionDecl *functionDecl ) { 303 GuardValue( inFunction ); 305 return Parent::mutate( objDecl ); 306 } 307 308 DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) { 309 ValueGuard< bool > oldInFunc = inFunction; 304 310 inFunction = true; 305 311 306 312 handleDWT( functionDecl ); 307 313 308 GuardScope( managedTypes);314 managedTypes.beginScope(); 309 315 // go through assertions and recursively add seen ctor/dtors 310 316 for ( auto & tyDecl : functionDecl->get_functionType()->get_forall() ) { 311 317 for ( DeclarationWithType *& assertion : tyDecl->get_assertions() ) { 312 handleDWT( assertion);318 assertion = assertion->acceptMutator( *this ); 313 319 } 314 320 } 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 321 // parameters should not be constructed and destructed, so don't mutate FunctionType 322 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); 323 324 managedTypes.endScope(); 325 return functionDecl; 326 } 327 328 Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { 325 329 // don't construct members, but need to take note if there is a managed member, 326 330 // because that means that this type is also managed … … 334 338 } 335 339 } 336 } 337 338 void CtorDtor::previsit( CompoundStmt * compoundStmt ) { 339 GuardScope( managedTypes ); 340 } 340 return aggregateDecl; 341 } 342 343 CompoundStmt * CtorDtor::mutate( CompoundStmt * compoundStmt ) { 344 managedTypes.beginScope(); 345 CompoundStmt * stmt = Parent::mutate( compoundStmt ); 346 managedTypes.endScope(); 347 return stmt; 348 } 349 341 350 } // namespace InitTweak 342 351
Note:
See TracChangeset
for help on using the changeset viewer.