Changes in src/InitTweak/FixInitNew.cpp [39d8950:ce36b55]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/FixInitNew.cpp
r39d8950 rce36b55 16 16 #include "CodeGen/GenType.h" // for genPrettyType 17 17 #include "CodeGen/OperatorTable.h" 18 #include "Common/CodeLocationTools.hpp"19 18 #include "Common/PassVisitor.h" // for PassVisitor, WithStmtsToAdd 20 19 #include "Common/SemanticError.h" // for SemanticError … … 86 85 /// generate/resolve copy construction expressions for each, and generate/resolve destructors for both 87 86 /// arguments and return value temporaries 88 struct ResolveCopyCtors final : public ast::WithGuards, public ast::WithStmtsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting, public ast::WithVisitorRef<ResolveCopyCtors> , public ast::WithConstTranslationUnit{87 struct ResolveCopyCtors final : public ast::WithGuards, public ast::WithStmtsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting, public ast::WithVisitorRef<ResolveCopyCtors> { 89 88 const ast::Expr * postvisit( const ast::ImplicitCopyCtorExpr * impCpCtorExpr ); 90 89 const ast::StmtExpr * previsit( const ast::StmtExpr * stmtExpr ); … … 190 189 /// for any member that is missing a corresponding ctor/dtor call. 191 190 /// error if a member is used before constructed 192 struct GenStructMemberCalls final : public ast::WithGuards, public ast::WithShortCircuiting, public ast::WithSymbolTable, public ast::WithVisitorRef<GenStructMemberCalls> , public ast::WithConstTranslationUnit{191 struct GenStructMemberCalls final : public ast::WithGuards, public ast::WithShortCircuiting, public ast::WithSymbolTable, public ast::WithVisitorRef<GenStructMemberCalls> { 193 192 void previsit( const ast::FunctionDecl * funcDecl ); 194 193 const ast::DeclWithType * postvisit( const ast::FunctionDecl * funcDecl ); … … 215 214 216 215 /// expands ConstructorExpr nodes into comma expressions, using a temporary for the first argument 217 struct FixCtorExprs final : public ast::WithDeclsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting , public ast::WithConstTranslationUnit{216 struct FixCtorExprs final : public ast::WithDeclsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting { 218 217 const ast::Expr * postvisit( const ast::ConstructorExpr * ctorExpr ); 219 218 }; … … 510 509 // (VariableExpr and already resolved expression) 511 510 CP_CTOR_PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; ) 512 ast::ptr<ast::Expr> resolved = ResolvExpr::findVoidExpression(untyped, { symtab, transUnit().global });511 ast::ptr<ast::Expr> resolved = ResolvExpr::findVoidExpression(untyped, symtab); 513 512 assert( resolved ); 514 513 if ( resolved->env ) { … … 554 553 ast::ptr<ast::Expr> guard = mutArg; 555 554 556 ast::ptr<ast::ObjectDecl> tmp = new ast::ObjectDecl( loc, "__tmp", mutResult, nullptr );555 ast::ptr<ast::ObjectDecl> tmp = new ast::ObjectDecl({}, "__tmp", mutResult, nullptr ); 557 556 558 557 // create and resolve copy constructor … … 588 587 589 588 ast::Expr * ResolveCopyCtors::destructRet( const ast::ObjectDecl * ret, const ast::Expr * arg ) { 590 auto global = transUnit().global;591 589 // TODO: refactor code for generating cleanup attribute, since it's common and reused in ~3-4 places 592 590 // check for existing cleanup attribute before adding another(?) 593 591 // need to add __Destructor for _tmp_cp variables as well 594 592 595 assertf( global.dtorStruct, "Destructor generation requires __Destructor definition." );596 assertf( global.dtorStruct->members.size() == 2, "__Destructor definition does not have expected fields." );597 assertf( global.dtorDestroy, "Destructor generation requires __destroy_Destructor." );593 assertf( ast::dtorStruct, "Destructor generation requires __Destructor definition." ); 594 assertf( ast::dtorStruct->members.size() == 2, "__Destructor definition does not have expected fields." ); 595 assertf( ast::dtorStructDestroy, "Destructor generation requires __destroy_Destructor." ); 598 596 599 597 const CodeLocation loc = ret->location; … … 612 610 auto dtorFunc = getDtorFunc( ret, new ast::ExprStmt(loc, dtor ), stmtsToAddBefore ); 613 611 614 auto dtorStructType = new ast::StructInstType( global.dtorStruct);612 auto dtorStructType = new ast::StructInstType(ast::dtorStruct); 615 613 616 614 // what does this do??? … … 624 622 static UniqueName namer( "_ret_dtor" ); 625 623 auto retDtor = new ast::ObjectDecl(loc, namer.newName(), dtorStructType, new ast::ListInit(loc, { new ast::SingleInit(loc, ast::ConstantExpr::null(loc) ), new ast::SingleInit(loc, new ast::CastExpr( new ast::VariableExpr(loc, dtorFunc ), dtorType ) ) } ) ); 626 retDtor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr(loc, global.dtorDestroy ) } ) );624 retDtor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr(loc, ast::dtorStructDestroy ) } ) ); 627 625 stmtsToAddBefore.push_back( new ast::DeclStmt(loc, retDtor ) ); 628 626 629 627 if ( arg ) { 630 auto member = new ast::MemberExpr(loc, global.dtorStruct->members.front().strict_as<ast::DeclWithType>(), new ast::VariableExpr(loc, retDtor ) );628 auto member = new ast::MemberExpr(loc, ast::dtorStruct->members.front().strict_as<ast::DeclWithType>(), new ast::VariableExpr(loc, retDtor ) ); 631 629 auto object = new ast::CastExpr( new ast::AddressExpr( new ast::VariableExpr(loc, ret ) ), new ast::PointerType(new ast::VoidType() ) ); 632 630 ast::Expr * assign = createBitwiseAssignment( member, object ); … … 801 799 // to prevent warnings ('_unq0' may be used uninitialized in this function), 802 800 // insert an appropriate zero initializer for UniqueExpr temporaries. 803 ast::Init * makeInit( const ast::Type * t , CodeLocation const & loc) {801 ast::Init * makeInit( const ast::Type * t ) { 804 802 if ( auto inst = dynamic_cast< const ast::StructInstType * >( t ) ) { 805 803 // initizer for empty struct must be empty 806 if ( inst->base->members.empty() ) { 807 return new ast::ListInit( loc, {} ); 808 } 804 if ( inst->base->members.empty() ) return new ast::ListInit({}, {}); 809 805 } else if ( auto inst = dynamic_cast< const ast::UnionInstType * >( t ) ) { 810 806 // initizer for empty union must be empty 811 if ( inst->base->members.empty() ) { 812 return new ast::ListInit( loc, {} ); 813 } 814 } 815 816 return new ast::ListInit( loc, { 817 new ast::SingleInit( loc, ast::ConstantExpr::from_int( loc, 0 ) ) 818 } ); 807 if ( inst->base->members.empty() ) return new ast::ListInit({}, {}); 808 } 809 810 return new ast::ListInit( {}, { new ast::SingleInit( {}, ast::ConstantExpr::from_int({}, 0) ) } ); 819 811 } 820 812 … … 840 832 } else { 841 833 // expr isn't a call expr, so create a new temporary variable to use to hold the value of the unique expression 842 mutExpr->object = new ast::ObjectDecl( mutExpr->location, toString("_unq", mutExpr->id), mutExpr->result, makeInit( mutExpr->result , mutExpr->location) );834 mutExpr->object = new ast::ObjectDecl( mutExpr->location, toString("_unq", mutExpr->id), mutExpr->result, makeInit( mutExpr->result ) ); 843 835 mutExpr->var = new ast::VariableExpr( mutExpr->location, mutExpr->object ); 844 836 } … … 1180 1172 auto guard = makeFuncGuard( [this]() { symtab.enterScope(); }, [this]() { symtab.leaveScope(); } ); 1181 1173 symtab.addFunction( function ); 1182 auto global = transUnit().global;1183 1174 1184 1175 // need to iterate through members in reverse in order for … … 1226 1217 1227 1218 static UniqueName memberDtorNamer = { "__memberDtor" }; 1228 assertf( global.dtorStruct, "builtin __Destructor not found." );1229 assertf( global.dtorDestroy, "builtin __destroy_Destructor not found." );1219 assertf( ast::dtorStruct, "builtin __Destructor not found." ); 1220 assertf( ast::dtorStructDestroy, "builtin __destroy_Destructor not found." ); 1230 1221 1231 1222 ast::Expr * thisExpr = new ast::CastExpr( new ast::AddressExpr( new ast::VariableExpr(loc, thisParam ) ), new ast::PointerType( new ast::VoidType(), ast::CV::Qualifiers() ) ); … … 1237 1228 auto dtorType = new ast::PointerType( dtorFtype ); 1238 1229 1239 auto destructor = new ast::ObjectDecl(loc, memberDtorNamer.newName(), new ast::StructInstType( global.dtorStruct ), new ast::ListInit(loc, { new ast::SingleInit(loc, thisExpr ), new ast::SingleInit(loc, new ast::CastExpr( dtorExpr, dtorType ) ) } ) );1240 destructor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr( loc, global.dtorDestroy ) } ) );1230 auto destructor = new ast::ObjectDecl(loc, memberDtorNamer.newName(), new ast::StructInstType( ast::dtorStruct ), new ast::ListInit(loc, { new ast::SingleInit(loc, thisExpr ), new ast::SingleInit(loc, new ast::CastExpr( dtorExpr, dtorType ) ) } ) ); 1231 destructor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr({}, ast::dtorStructDestroy ) } ) ); 1241 1232 mutStmts->push_front( new ast::DeclStmt(loc, destructor ) ); 1242 1233 mutStmts->kids.splice( mutStmts->kids.begin(), stmtsToAdd ); … … 1332 1323 1333 1324 const ast::Expr * GenStructMemberCalls::postvisit( const ast::UntypedExpr * untypedExpr ) { 1325 // Expression * newExpr = untypedExpr; 1334 1326 // xxx - functions returning ast::ptr seems wrong... 1335 auto res = ResolvExpr::findVoidExpression( untypedExpr, { symtab, transUnit().global } ); 1336 // Fix CodeLocation (at least until resolver is fixed). 1337 auto fix = localFillCodeLocations( untypedExpr->location, res.release() ); 1338 return strict_dynamic_cast<const ast::Expr *>( fix ); 1327 auto res = ResolvExpr::findVoidExpression( untypedExpr, symtab ); 1328 return res.release(); 1329 // return newExpr; 1339 1330 } 1340 1331 … … 1370 1361 1371 1362 // resolve assignment and dispose of new env 1372 auto resolved = ResolvExpr::findVoidExpression( assign, { symtab, transUnit().global });1363 auto resolved = ResolvExpr::findVoidExpression( assign, symtab ); 1373 1364 auto mut = resolved.get_and_mutate(); 1374 1365 assertf(resolved.get() == mut, "newly resolved expression must be unique");
Note:
See TracChangeset
for help on using the changeset viewer.