Changeset f1e012b for src/InitTweak
- Timestamp:
- Jan 19, 2016, 1:28:25 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- ca1c11f
- Parents:
- 71f4e4f
- Location:
- src/InitTweak
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/FixInit.cc
r71f4e4f rf1e012b 10 10 // Created On : Wed Jan 13 16:29:30 2016 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jan 13 16:45:37201613 // Update Count : 1712 // Last Modified On : Tue Jan 19 13:25:13 2016 13 // Update Count : 27 14 14 // 15 15 … … 35 35 36 36 virtual ObjectDecl * mutate( ObjectDecl *objDecl ); 37 38 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ); 37 39 }; 38 40 … … 50 52 ObjectDecl *FixInit::mutate( ObjectDecl *objDecl ) { 51 53 if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { 52 // a decision should have been made by the resolver, so either ctor is NULL or init isNULL54 // a decision should have been made by the resolver, so ctor and init are not both non-NULL 53 55 assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() ); 54 56 if ( Expression * ctor = ctorInit->get_ctor() ) { … … 61 63 ctorInit->set_init( NULL ); 62 64 } else { 63 // one of them should be non-NULL64 assert( ctorInit->get_ctor() || ctorInit->get_init());65 // no constructor and no initializer, which is okay 66 objDecl->set_init( NULL ); 65 67 } 66 68 delete ctorInit; … … 68 70 return objDecl; 69 71 } 72 73 CompoundStmt * FixInit::mutate( CompoundStmt * compoundStmt ) { 74 std::list< Statement * > & statements = compoundStmt->get_kids(); 75 for ( std::list< Statement * >::iterator it = statements.begin(); it != statements.end(); ++it ) { 76 // remove if instrinsic destructor statement 77 // xxx - test user manually calling intrinsic functions - what happens? 78 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( *it ) ) { 79 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( exprStmt->get_expr() ) ) { 80 if ( VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() ) ) { 81 if ( function->get_var()->get_name() == "^?{}" && function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) { 82 statements.erase(it++); 83 } 84 } 85 } 86 } 87 } 88 // mutate non-destructor statements 89 return Mutator::mutate( compoundStmt ); 90 } 91 70 92 } // namespace InitTweak 71 93 -
src/InitTweak/RemoveInit.cc
r71f4e4f rf1e012b 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jan 13 15:19:35201613 // Update Count : 1 5412 // Last Modified On : Tue Jan 19 11:12:49 2016 13 // Update Count : 165 14 14 // 15 15 … … 59 59 static void generateCtorDtor( std::list< Declaration * > &translationUnit ); 60 60 61 CtorDtor() : inFunction( false ) {} 62 61 63 virtual ObjectDecl * mutate( ObjectDecl * ); 62 64 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); … … 71 73 72 74 protected: 75 bool inFunction; 73 76 74 77 // to be added before block ends - use push_front so order is correct … … 93 96 if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) { 94 97 if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) { 95 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); 98 // xxx this can be more complicated - consider ListInit 99 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?{}" ) ); 96 100 assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) ); 97 101 assign->get_args().push_back( single->get_value()->clone() ); … … 107 111 // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address 108 112 // is being returned 113 // xxx - this should construct rather than assign 109 114 if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) { 110 115 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), 0 ); … … 186 191 // hands off if designated or if @= 187 192 if ( tryConstruct( objDecl ) ) { 188 Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) ); 189 Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() ); 190 191 // need to remember init expression, in case no ctors exist 192 // if ctor does exist, want to use ctor expression instead of init 193 // push this decision to the resolver 194 objDecl->set_init( new ConstructorInit( ctor, objDecl->get_init() ) ); 195 destructorStmts.push_front( new ExprStmt( noLabels, dtor ) ); 193 if ( inFunction ) { 194 Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) ); 195 Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() ); 196 197 // need to remember init expression, in case no ctors exist 198 // if ctor does exist, want to use ctor expression instead of init 199 // push this decision to the resolver 200 objDecl->set_init( new ConstructorInit( ctor, objDecl->get_init() ) ); 201 destructorStmts.push_front( new ExprStmt( noLabels, dtor ) ); 202 } else { 203 // xxx - find a way to construct/destruct globals 204 // hack: implicit "static" initialization routine for each struct type? or something similar? 205 // --ties into module system 206 } 196 207 } 197 208 return objDecl; … … 200 211 DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) { 201 212 // parameters should not be constructed and destructed, so don't mutate FunctionType 213 bool oldInFunc = inFunction; 202 214 mutateAll( functionDecl->get_oldDecls(), *this ); 215 inFunction = true; 203 216 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); 217 inFunction = oldInFunc; 204 218 return functionDecl; 205 219 }
Note: See TracChangeset
for help on using the changeset viewer.