Changeset 02c7d04 for src/InitTweak
- Timestamp:
- Jan 11, 2016, 4:14:18 PM (9 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:
- 71f4e4f
- Parents:
- 77acda06
- Location:
- src/InitTweak
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/RemoveInit.cc
r77acda06 r02c7d04 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Mon Jan 11 1 4:41:26201613 // Update Count : 1 1812 // Last Modified On : Mon Jan 11 16:10:45 2016 13 // Update Count : 150 14 14 // 15 15 … … 32 32 class RemoveInit : public GenPoly::PolyMutator { 33 33 public: 34 /// removes and replaces initialization for polymorphic value objects 35 /// with assignment (TODO: constructor) statements. 36 /// also consistently allocates a temporary variable for the return value 37 /// of a function so that anything which the resolver decides can be assigned 38 /// into the return type of a function can be returned. 39 static void removeInitializers( std::list< Declaration * > &translationUnit ); 40 34 41 RemoveInit(); 35 42 virtual ObjectDecl * mutate( ObjectDecl *objDecl ); … … 46 53 class CtorDtor : public GenPoly::PolyMutator { 47 54 public: 48 // CtorDtor(); 55 /// create constructor and destructor statements for object declarations. 56 /// Destructors are inserted directly into the code, whereas constructors 57 /// will be added in after the resolver has run so that the initializer expression 58 /// is only removed if a constructor is found 59 static void generateCtorDtor( std::list< Declaration * > &translationUnit ); 49 60 50 61 virtual ObjectDecl * mutate( ObjectDecl * ); 62 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); 63 virtual Declaration* mutate( StructDecl *aggregateDecl ); 64 virtual Declaration* mutate( UnionDecl *aggregateDecl ); 65 virtual Declaration* mutate( EnumDecl *aggregateDecl ); 66 virtual Declaration* mutate( ContextDecl *aggregateDecl ); 67 virtual TypeDecl* mutate( TypeDecl *typeDecl ); 68 virtual Declaration* mutate( TypedefDecl *typeDecl ); 51 69 52 70 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ); 53 71 54 72 protected: 55 typedef std::map< std::string, DeclarationWithType * > MMMMAP;56 std::stack< MMMMAP > constructedObjects;57 73 58 74 // to be added before block ends - use push_front so order is correct … … 60 76 }; 61 77 62 void tweak( std::list< Declaration * > translationUnit ) { 78 void tweak( std::list< Declaration * > & translationUnit ) { 79 RemoveInit::removeInitializers( translationUnit ); 80 CtorDtor::generateCtorDtor( translationUnit ); 81 } 82 83 void RemoveInit::removeInitializers( std::list< Declaration * > & translationUnit ) { 63 84 RemoveInit remover; 64 CtorDtor ctordtor;65 85 mutateAll( translationUnit, remover ); 66 mutateAll( translationUnit, ctordtor );67 86 } 68 87 … … 115 134 } 116 135 117 bool tryConstruct( ObjectDecl * objDecl ) { 118 // xxx - handle designations 119 return objDecl->get_init() == NULL || 120 ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() ); 121 } 122 123 ExprStmt * makeCtorDtorStmt( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) { 124 UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) ); 125 expr->get_args().push_back( new VariableExpr( objDecl ) ); 126 expr->get_args().splice( expr->get_args().end(), args ); 127 return new ExprStmt( noLabels, expr ); 128 } 129 130 // InitExpander ctor/dtor being marked as weak symbols 131 // this is causing a bug - Rodolfo's InitExpander is being constructed and destructed 132 // with different fields, which causes a segfault 133 134 class InitExpander : public Visitor { 135 public: 136 InitExpander() {} 137 // ~InitExpander() {} 138 virtual void visit( SingleInit * singleInit ); 139 virtual void visit( ListInit * listInit ); 140 std::list< Expression * > argList; 141 }; 142 143 void InitExpander::visit( SingleInit * singleInit ) { 144 argList.push_back( singleInit->get_value()->clone() ); 145 } 146 147 void InitExpander::visit( ListInit * listInit ) { 148 // xxx - for now, assume no nested list inits 149 std::list<Initializer*>::iterator it = listInit->begin_initializers(); 150 for ( ; it != listInit->end_initializers(); ++it ) { 151 (*it)->accept( *this ); 152 } 153 } 154 155 std::list< Expression * > makeInitList( Initializer * init ) { 156 if ( init ) { 136 137 void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) { 138 CtorDtor ctordtor; 139 mutateAll( translationUnit, ctordtor ); 140 } 141 142 namespace { 143 bool tryConstruct( ObjectDecl * objDecl ) { 144 // xxx - handle designations 145 return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) && 146 (objDecl->get_init() == NULL || 147 ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )); 148 } 149 150 ExprStmt * makeCtorDtorStmt( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) { 151 UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) ); 152 expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); 153 expr->get_args().splice( expr->get_args().end(), args ); 154 return new ExprStmt( noLabels, expr ); 155 } 156 157 class InitExpander : public Visitor { 158 public: 159 InitExpander() {} 160 // ~InitExpander() {} 161 virtual void visit( SingleInit * singleInit ); 162 virtual void visit( ListInit * listInit ); 163 std::list< Expression * > argList; 164 }; 165 166 void InitExpander::visit( SingleInit * singleInit ) { 167 argList.push_back( singleInit->get_value()->clone() ); 168 } 169 170 void InitExpander::visit( ListInit * listInit ) { 171 // xxx - for now, assume no nested list inits 172 std::list<Initializer*>::iterator it = listInit->begin_initializers(); 173 for ( ; it != listInit->end_initializers(); ++it ) { 174 (*it)->accept( *this ); 175 } 176 } 177 178 std::list< Expression * > makeInitList( Initializer * init ) { 157 179 InitExpander expander; 158 // init->accept( expander ); 159 // std::list< Expression * > l = expander.argList; 160 std::list< Expression * > l; 161 return l; 162 } else { 163 std::list< Expression * > l; 164 return l; 180 maybeAccept( init, expander ); 181 return expander.argList; 165 182 } 166 183 } … … 179 196 } 180 197 return objDecl; 198 } 199 200 DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) { 201 // parameters should not be constructed and destructed, so don't mutate FunctionType 202 mutateAll( functionDecl->get_oldDecls(), *this ); 203 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); 204 return functionDecl; 181 205 } 182 206 … … 190 214 } 191 215 192 216 // should not traverse into any of these declarations to find objects 217 // that need to be constructed or destructed 218 Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; } 219 Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; } 220 Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; } 221 Declaration* CtorDtor::mutate( ContextDecl *aggregateDecl ) { return aggregateDecl; } 222 TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; } 223 Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; } 193 224 194 225 } // namespace InitTweak -
src/InitTweak/RemoveInit.h
r77acda06 r02c7d04 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // RemoveInit.h -- 7 // RemoveInit.h -- 8 8 // 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Fri Nov 27 17:00:47 201513 // Update Count : 211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Mon Jan 11 16:02:44 2016 13 // Update Count : 3 14 14 // 15 15 … … 26 26 namespace InitTweak { 27 27 /// Adds assignment statements for polymorphic type initializers 28 void tweak( std::list< Declaration * > translationUnit );29 } // namespace 28 void tweak( std::list< Declaration * > & translationUnit ); 29 } // namespace 30 30 31 31 #endif // GENPOLY_POLYMUTATOR_H
Note: See TracChangeset
for help on using the changeset viewer.