- Timestamp:
- Aug 21, 2018, 3:59:45 PM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
- Children:
- 305581d
- Parents:
- cdbab55
- Location:
- src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/Resolver.cc
rcdbab55 r2a6292d 75 75 void previsit( CatchStmt *catchStmt ); 76 76 void previsit( WaitForStmt * stmt ); 77 void previsit( WithStmt * withStmt );78 77 79 78 void previsit( SingleInit *singleInit ); … … 86 85 void handlePtrType( PtrType * type ); 87 86 88 void resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts );89 87 void fallbackInit( ConstructorInit * ctorInit ); 90 88 … … 92 90 CurrentObject currentObject = nullptr; 93 91 bool inEnumDecl = false; 92 }; 93 94 struct ResolveWithExprs : public WithIndexer, public WithGuards, public WithVisitorRef<ResolveWithExprs>, public WithShortCircuiting, public WithStmtsToAdd { 95 void previsit( FunctionDecl * ); 96 void previsit( WithStmt * ); 97 98 void resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ); 94 99 }; 95 100 … … 300 305 } 301 306 307 308 bool isStructOrUnion( const Alternative & alt ) { 309 Type * t = alt.expr->result->stripReferences(); 310 return dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ); 311 } 312 313 void resolveWithExprs( std::list< Declaration * > & translationUnit ) { 314 PassVisitor<ResolveWithExprs> resolver; 315 acceptAll( translationUnit, resolver ); 316 } 317 318 void ResolveWithExprs::resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ) { 319 for ( Expression *& expr : withExprs ) { 320 // only struct- and union-typed expressions are viable candidates 321 findKindExpression( expr, indexer, "with statement", isStructOrUnion ); 322 323 // if with expression might be impure, create a temporary so that it is evaluated once 324 if ( Tuples::maybeImpure( expr ) ) { 325 static UniqueName tmpNamer( "_with_tmp_" ); 326 ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), expr->result->clone(), new SingleInit( expr ) ); 327 expr = new VariableExpr( tmp ); 328 newStmts.push_back( new DeclStmt( tmp ) ); 329 if ( InitTweak::isConstructable( tmp->type ) ) { 330 // generate ctor/dtor and resolve them 331 tmp->init = InitTweak::genCtorInit( tmp ); 332 tmp->accept( *visitor ); 333 } 334 } 335 } 336 } 337 338 void ResolveWithExprs::previsit( WithStmt * withStmt ) { 339 resolveWithExprs( withStmt->exprs, stmtsToAddBefore ); 340 } 341 342 void ResolveWithExprs::previsit( FunctionDecl * functionDecl ) { 343 { 344 // resolve with-exprs with parameters in scope and add any newly generated declarations to the 345 // front of the function body. 346 auto guard = makeFuncGuard( [this]() { indexer.enterScope(); }, [this](){ indexer.leaveScope(); } ); 347 indexer.addFunctionType( functionDecl->type ); 348 std::list< Statement * > newStmts; 349 resolveWithExprs( functionDecl->withExprs, newStmts ); 350 if ( functionDecl->statements ) { 351 functionDecl->statements->kids.splice( functionDecl->statements->kids.begin(), newStmts ); 352 } else { 353 assertf( functionDecl->withExprs.empty() && newStmts.empty(), "Function %s without a body has with-clause and/or generated with declarations.", functionDecl->name.c_str() ); 354 } 355 } 356 } 357 302 358 void Resolver::previsit( ObjectDecl *objectDecl ) { 303 359 // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable … … 338 394 GuardValue( functionReturn ); 339 395 functionReturn = ResolvExpr::extractResultType( functionDecl->type ); 340 341 {342 // resolve with-exprs with parameters in scope and add any newly generated declarations to the343 // front of the function body.344 auto guard = makeFuncGuard( [this]() { indexer.enterScope(); }, [this](){ indexer.leaveScope(); } );345 indexer.addFunctionType( functionDecl->type );346 std::list< Statement * > newStmts;347 resolveWithExprs( functionDecl->withExprs, newStmts );348 if ( functionDecl->statements ) {349 functionDecl->statements->kids.splice( functionDecl->statements->kids.begin(), newStmts );350 } else {351 assertf( functionDecl->withExprs.empty() && newStmts.empty(), "Function %s without a body has with-clause and/or generated with declarations.", functionDecl->name.c_str() );352 }353 }354 396 } 355 397 … … 682 724 stmt->orelse.statement->accept( *visitor ); 683 725 } 684 }685 686 bool isStructOrUnion( const Alternative & alt ) {687 Type * t = alt.expr->result->stripReferences();688 return dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t );689 }690 691 void Resolver::resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ) {692 for ( Expression *& expr : withExprs ) {693 // only struct- and union-typed expressions are viable candidates694 findKindExpression( expr, indexer, "with statement", isStructOrUnion );695 696 // if with expression might be impure, create a temporary so that it is evaluated once697 if ( Tuples::maybeImpure( expr ) ) {698 static UniqueName tmpNamer( "_with_tmp_" );699 ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), expr->result->clone(), new SingleInit( expr ) );700 expr = new VariableExpr( tmp );701 newStmts.push_back( new DeclStmt( tmp ) );702 if ( InitTweak::isConstructable( tmp->type ) ) {703 // generate ctor/dtor and resolve them704 tmp->init = InitTweak::genCtorInit( tmp );705 tmp->accept( *visitor );706 }707 }708 }709 }710 711 void Resolver::previsit( WithStmt * withStmt ) {712 resolveWithExprs( withStmt->exprs, stmtsToAddBefore );713 726 } 714 727 -
src/ResolvExpr/Resolver.h
rcdbab55 r2a6292d 38 38 /// Searches expr and returns the first DeletedExpr found, otherwise nullptr 39 39 DeletedExpr * findDeletedExpr( Expression * expr ); 40 /// Resolves with-stmts and with-clauses on functions 41 void resolveWithExprs( std::list< Declaration * > & translationUnit ); 40 42 } // namespace ResolvExpr 41 43 -
src/SymTab/Validate.cc
rcdbab55 r2a6292d 322 322 Concurrency::implementThreadStarter( translationUnit ); 323 323 mutateAll( translationUnit, compoundliteral ); 324 ResolvExpr::resolveWithExprs( translationUnit ); // must happen before FixObjectType because user-code is resolved and may contain with variables 324 325 FixObjectType::fix( translationUnit ); 325 326 ArrayLength::computeLength( translationUnit );
Note: See TracChangeset
for help on using the changeset viewer.