Changes in src/InitTweak/FixGlobalInit.cc [03e5d14:ec79847]
- File:
-
- 1 edited
-
src/InitTweak/FixGlobalInit.cc (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/FixGlobalInit.cc
r03e5d14 rec79847 10 10 // Created On : Mon May 04 15:14:56 2016 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Fri May 06 15:40:48201612 // Last Modified On : Mon May 09 11:44:29 2016 13 13 // Update Count : 2 14 14 // 15 15 16 16 #include "FixGlobalInit.h" 17 #include "GenInit.h" 17 18 #include "SynTree/Declaration.h" 18 19 #include "SynTree/Type.h" … … 42 43 UniqueName tempNamer; 43 44 FunctionDecl * initFunction; 45 FunctionDecl * destroyFunction; 44 46 }; 45 47 … … 90 92 GlobalFixer fixer( name, inLibrary ); 91 93 acceptAll( translationUnit, fixer ); 92 translationUnit.push_back( fixer.initFunction ); 94 // don't need to include function if it's empty 95 if ( fixer.initFunction->get_statements()->get_kids().empty() ) { 96 delete fixer.initFunction; 97 } else { 98 translationUnit.push_back( fixer.initFunction ); 99 } 100 if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) { 101 delete fixer.destroyFunction; 102 } else { 103 translationUnit.push_back( fixer.destroyFunction ); 104 } 93 105 } 94 106 95 std::string initName( const std::string & name ) {107 std::string globalFunctionName( const std::string & name ) { 96 108 // get basename 97 109 std::string ret = name.substr( 0, name.find( '.' ) ); … … 99 111 static std::string invalid = "/-"; 100 112 replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' ); 101 return "_init_" +ret;113 return ret; 102 114 } 103 115 104 116 GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) { 105 initFunction = new FunctionDecl( initName( name ), DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Constructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) ); 117 std::string fixedName = globalFunctionName( name ); 118 initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Constructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) ); 119 120 destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Destructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) ); 106 121 } 107 122 108 123 void GlobalFixer::visit( ObjectDecl *objDecl ) { 109 std::list< Statement * > & statements = initFunction->get_statements()->get_kids(); 124 std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids(); 125 std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids(); 110 126 111 127 if ( objDecl->get_init() == NULL ) return; 128 if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects 112 129 if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable 113 130 // C allows you to initialize objects with constant expressions … … 119 136 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() ); 120 137 objDecl->set_init( NULL ); 121 statements.push_back( new DeclStmt( noLabels, newObj ) );138 initStatements.push_back( new DeclStmt( noLabels, newObj ) ); 122 139 123 // assign (later: copy construct)objDecl using temporary124 UntypedExpr * init = new UntypedExpr( new NameExpr( "? =?" ) );140 // copy construct objDecl using temporary 141 UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) ); 125 142 init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); 126 143 init->get_args().push_back( new VariableExpr( newObj ) ); 127 statements.push_back( new ExprStmt( noLabels, init ) );144 initStatements.push_back( new ExprStmt( noLabels, init ) ); 128 145 129 // xxx- need to destruct objDecl atexit 146 // add destructor calls to global destroy function 147 UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) ); 148 destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); 149 destroyStatements.push_front( new ExprStmt( noLabels, destroy ) ); 130 150 } 131 151
Note:
See TracChangeset
for help on using the changeset viewer.