Changeset d24d4e1
- Timestamp:
- Jun 23, 2017, 12:12:46 PM (7 years ago)
- 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
- Location:
- src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/PassVisitor.h
rba915fb5 rd24d4e1 26 26 // stmtsToAddBefore or stmtsToAddAfter respectively. 27 27 // | WithShortCircuiting - provides the ability to skip visiting child nodes; set visit_children to false in pre{visit,mutate} to skip visiting children 28 // | With Scopes - provides the ability to save/restore data like a LIFO stack; to save, call GuardValue with the variable to save, the variable28 // | WithGuards - provides the ability to save/restore data like a LIFO stack; to save, call GuardValue with the variable to save, the variable 29 29 // will automatically be restored to its previous value after the corresponding postvisit/postmutate teminates. 30 30 //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- … … 32 32 class PassVisitor final : public Visitor, public Mutator { 33 33 public: 34 PassVisitor() = default;35 34 36 35 template< typename... Args > … … 283 282 284 283 public: 285 TypeSubstitution * env ;284 TypeSubstitution * env = nullptr; 286 285 }; 287 286 … … 295 294 std::list< Statement* > stmtsToAddAfter; 296 295 }; 296 297 class WithDeclsToAdd { 298 protected: 299 WithDeclsToAdd() = default; 300 ~WithDeclsToAdd() = default; 301 302 public: 303 std::list< Declaration* > declsToAddBefore; 304 std::list< Declaration* > declsToAddAfter; 305 }; 306 297 307 class WithShortCircuiting { 298 308 protected: … … 304 314 }; 305 315 306 class With Scopes {307 protected: 308 With Scopes() = default;309 ~With Scopes() = default;316 class WithGuards { 317 protected: 318 WithGuards() = default; 319 ~WithGuards() = default; 310 320 311 321 public: … … 319 329 } 320 330 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 321 339 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 ); 324 342 } 325 343 }; … … 328 346 class WithVisitorRef { 329 347 protected: 330 WithVisitorRef() = default;331 ~WithVisitorRef() = default;332 333 public: 334 PassVisitor<pass_type> * const visitor ;348 WithVisitorRef() {} 349 ~WithVisitorRef() {} 350 351 public: 352 PassVisitor<pass_type> * const visitor = nullptr; 335 353 }; 336 354 -
src/Common/PassVisitor.impl.h
rba915fb5 rd24d4e1 68 68 for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) { 69 69 // splice in new declarations after previous decl 70 if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); } 70 if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); } 71 71 72 72 if ( i == decls.end() ) break; … … 88 88 for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) { 89 89 // splice in new declarations after previous decl 90 if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); } 90 if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); } 91 91 92 92 if ( i == decls.end() ) break; … … 181 181 Statement * PassVisitor< pass_type >::visitStatement( Statement * stmt ) { 182 182 return handleStatement( stmt, [this]( Statement * stmt ) { 183 maybeAccept( stmt, *this ); 183 maybeAccept( stmt, *this ); 184 184 return stmt; 185 185 }); … … 212 212 expr->accept( *this ); 213 213 return expr; 214 }); 214 }); 215 215 } 216 216 -
src/InitTweak/GenInit.cc
rba915fb5 rd24d4e1 44 44 } 45 45 46 class ReturnFixer : public WithStmtsToAdd, public WithScopes { 47 public: 46 struct ReturnFixer : public WithStmtsToAdd, public WithGuards { 48 47 /// consistently allocates a temporary variable for the return value 49 48 /// of a function so that anything which the resolver decides can be constructed … … 59 58 }; 60 59 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 { 65 61 /// create constructor and destructor statements for object declarations. 66 62 /// the actual call statements will be added in after the resolver has run … … 69 65 static void generateCtorDtor( std::list< Declaration * > &translationUnit ); 70 66 71 virtual DeclarationWithType * mutate( ObjectDecl * ) override; 72 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override; 67 void previsit( ObjectDecl * ); 68 void previsit( FunctionDecl *functionDecl ); 69 73 70 // should not traverse into any of these declarations to find objects 74 71 // that need to be constructed or destructed 75 v irtual Declaration* mutate( StructDecl *aggregateDecl ) override;76 v irtual Declaration* mutate( UnionDecl *aggregateDecl ) override { return aggregateDecl; }77 v irtual Declaration* mutate( EnumDecl *aggregateDecl ) override { return aggregateDecl; }78 v irtual Declaration* mutate( TraitDecl *aggregateDecl ) override { return aggregateDecl; }79 v irtual TypeDecl* mutate( TypeDecl *typeDecl ) override { return typeDecl; }80 v irtual Declaration* mutate( TypedefDecl *typeDecl ) override { return typeDecl; }81 82 v irtual Type * mutate( FunctionType *funcType ) override { return funcType; }83 84 v irtual 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 ); 85 82 86 83 private: … … 211 208 212 209 void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) { 213 CtorDtorctordtor;214 mutateAll( translationUnit, ctordtor );210 PassVisitor<CtorDtor> ctordtor; 211 acceptAll( translationUnit, ctordtor ); 215 212 } 216 213 … … 289 286 } 290 287 291 DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) {288 void CtorDtor::previsit( ObjectDecl * objDecl ) { 292 289 handleDWT( objDecl ); 293 290 // hands off if @=, extern, builtin, etc. … … 301 298 objDecl->set_init( genCtorInit( objDecl ) ); 302 299 } 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 ); 308 304 inFunction = true; 309 305 310 306 handleDWT( functionDecl ); 311 307 312 managedTypes.beginScope();308 GuardScope( managedTypes ); 313 309 // go through assertions and recursively add seen ctor/dtors 314 310 for ( auto & tyDecl : functionDecl->get_functionType()->get_forall() ) { … … 317 313 } 318 314 } 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 327 325 // don't construct members, but need to take note if there is a managed member, 328 326 // because that means that this type is also managed … … 336 334 } 337 335 } 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 } 348 341 } // namespace InitTweak 349 342 -
src/SymTab/Validate.cc
rba915fb5 rd24d4e1 106 106 107 107 /// Fix return types so that every function returns exactly one value 108 class ReturnTypeFixer { 109 public: 108 struct ReturnTypeFixer { 110 109 static void fix( std::list< Declaration * > &translationUnit ); 111 110 … … 115 114 116 115 /// 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 { 119 117 void previsit( EnumDecl *aggregateDecl ); 120 118 void previsit( FunctionType *func ); … … 159 157 }; 160 158 161 class ReturnChecker : public WithScopes { 162 public: 159 struct ReturnChecker : public WithGuards { 163 160 /// Checks that return statements return nothing if their return type is void 164 161 /// and return something if the return type is non-void. 165 162 static void checkFunctionReturns( std::list< Declaration * > & translationUnit ); 166 private: 163 167 164 void previsit( FunctionDecl * functionDecl ); 168 165 void previsit( ReturnStmt * returnStmt ); … … 205 202 }; 206 203 207 class VerifyCtorDtorAssign { 208 public: 204 struct VerifyCtorDtorAssign { 209 205 /// ensure that constructors, destructors, and assignment have at least one 210 206 /// parameter, the first of which must be a pointer, and that ctor/dtors have no … … 216 212 217 213 /// ensure that generic types have the correct number of type arguments 218 class ValidateGenericParameters { 219 public: 214 struct ValidateGenericParameters { 220 215 void previsit( StructInstType * inst ); 221 216 void previsit( UnionInstType * inst ); 222 217 }; 223 218 224 class ArrayLength { 225 public: 219 struct ArrayLength { 226 220 /// for array types without an explicit length, compute the length and store it so that it 227 221 /// is known to the rest of the phases. For example, … … 236 230 }; 237 231 238 class CompoundLiteral final : public GenPoly::DeclMutator{232 struct CompoundLiteral final : public WithDeclsToAdd, public WithVisitorRef<CompoundLiteral> { 239 233 Type::StorageClasses storageClasses; 240 234 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 ); 244 237 }; 245 238 … … 248 241 LinkReferenceToTypes lrt( doDebug, 0 ); 249 242 ForallPointerDecay fpd( 0 ); 250 CompoundLiteralcompoundliteral;243 PassVisitor<CompoundLiteral> compoundliteral; 251 244 PassVisitor<ValidateGenericParameters> genericParams; 252 245 … … 263 256 Concurrency::implementThreadStarter( translationUnit ); 264 257 ReturnChecker::checkFunctionReturns( translationUnit ); 265 compoundliteral.mutateDeclarationList( translationUnit);258 mutateAll( translationUnit, compoundliteral ); 266 259 acceptAll( translationUnit, fpd ); 267 260 ArrayLength::computeLength( translationUnit ); … … 883 876 } 884 877 885 DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) {878 void CompoundLiteral::premutate( ObjectDecl *objectDecl ) { 886 879 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 ) { 892 883 // transform [storage_class] ... (struct S){ 3, ... }; 893 884 // into [storage_class] struct S temp = { 3, ... }; 894 885 static UniqueName indexName( "_compLit" ); 895 886 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 ); 899 890 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 ); 903 893 } 904 894
Note: See TracChangeset
for help on using the changeset viewer.