Changes in src/InitTweak/GenInit.cc [1ba88a0:f9cebb5]
- File:
-
- 1 edited
-
src/InitTweak/GenInit.cc (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/GenInit.cc
r1ba88a0 rf9cebb5 25 25 #include "SynTree/Mutator.h" 26 26 #include "SymTab/Autogen.h" 27 #include "SymTab/Mangler.h"28 27 #include "GenPoly/PolyMutator.h" 29 28 #include "GenPoly/DeclMutator.h" 30 #include "GenPoly/ScopedSet.h"31 29 32 30 namespace InitTweak { … … 57 55 class CtorDtor : public GenPoly::PolyMutator { 58 56 public: 59 typedef GenPoly::PolyMutator Parent;60 using Parent::mutate;61 57 /// create constructor and destructor statements for object declarations. 62 58 /// the actual call statements will be added in after the resolver has run … … 69 65 // should not traverse into any of these declarations to find objects 70 66 // that need to be constructed or destructed 71 virtual Declaration* mutate( StructDecl *aggregateDecl ) ;67 virtual Declaration* mutate( StructDecl *aggregateDecl ) { return aggregateDecl; } 72 68 virtual Declaration* mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; } 73 69 virtual Declaration* mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; } … … 78 74 virtual Type * mutate( FunctionType *funcType ) { return funcType; } 79 75 80 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ); 81 82 private: 83 // set of mangled type names for which a constructor or destructor exists in the current scope. 84 // these types require a ConstructorInit node to be generated, anything else is a POD type and thus 85 // should not have a ConstructorInit generated. 86 87 bool isManaged( ObjectDecl * objDecl ) const ; // determine if object is managed 88 void handleDWT( DeclarationWithType * dwt ); // add type to managed if ctor/dtor 89 GenPoly::ScopedSet< std::string > managedTypes; 90 bool inFunction = false; 76 protected: 91 77 }; 92 78 … … 156 142 157 143 DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) { 158 ValueGuard< std::list<DeclarationWithType*> > oldReturnVals( returnVals );159 ValueGuard< std::string > oldFuncName( funcName );144 std::list<DeclarationWithType*> oldReturnVals = returnVals; 145 std::string oldFuncName = funcName; 160 146 161 147 FunctionType * type = functionDecl->get_functionType(); … … 163 149 funcName = functionDecl->get_name(); 164 150 DeclarationWithType * decl = Mutator::mutate( functionDecl ); 151 returnVals = oldReturnVals; 152 funcName = oldFuncName; 165 153 return decl; 166 154 } … … 209 197 210 198 DeclarationWithType * HoistArrayDimension::mutate( FunctionDecl *functionDecl ) { 211 ValueGuard< bool > oldInFunc( inFunction );199 bool oldInFunc = inFunction; 212 200 inFunction = true; 213 201 DeclarationWithType * decl = Parent::mutate( functionDecl ); 202 inFunction = oldInFunc; 214 203 return decl; 215 204 } … … 220 209 } 221 210 222 bool CtorDtor::isManaged( ObjectDecl * objDecl ) const {223 Type * type = objDecl->get_type();224 while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {225 type = at->get_base();226 }227 return managedTypes.find( SymTab::Mangler::mangle( type ) ) != managedTypes.end();228 }229 230 void CtorDtor::handleDWT( DeclarationWithType * dwt ) {231 // if this function is a user-defined constructor or destructor, mark down the type as "managed"232 if ( ! LinkageSpec::isOverridable( dwt->get_linkage() ) && isCtorDtor( dwt->get_name() ) ) {233 std::list< DeclarationWithType * > & params = GenPoly::getFunctionType( dwt->get_type() )->get_parameters();234 assert( ! params.empty() );235 PointerType * type = safe_dynamic_cast< PointerType * >( params.front()->get_type() );236 managedTypes.insert( SymTab::Mangler::mangle( type->get_base() ) );237 }238 }239 240 211 DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) { 241 handleDWT( objDecl ); 242 // hands off if @=, extern, builtin, etc. 243 // if global but initializer is not constexpr, always try to construct, since this is not legal C 244 if ( ( tryConstruct( objDecl ) && isManaged( objDecl ) ) || (! inFunction && ! isConstExpr( objDecl->get_init() ) ) ) { 245 // constructed objects cannot be designated 246 if ( isDesignated( objDecl->get_init() ) ) throw SemanticError( "Cannot include designations in the initializer for a managed Object. If this is really what you want, then initialize with @=.", objDecl ); 247 // xxx - constructed objects should not have initializers nested too deeply 248 212 // hands off if designated, if @=, or if extern 213 if ( tryConstruct( objDecl ) ) { 249 214 // call into genImplicitCall from Autogen.h to generate calls to ctor/dtor 250 215 // for each constructable object … … 276 241 } 277 242 } 278 return Parent::mutate( objDecl );243 return Mutator::mutate( objDecl ); 279 244 } 280 245 281 246 DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) { 282 ValueGuard< bool > oldInFunc = inFunction;283 inFunction = true;284 285 handleDWT( functionDecl );286 287 managedTypes.beginScope();288 // go through assertions and recursively add seen ctor/dtors289 for ( TypeDecl * tyDecl : functionDecl->get_functionType()->get_forall() ) {290 for ( DeclarationWithType *& assertion : tyDecl->get_assertions() ) {291 assertion = assertion->acceptMutator( *this );292 }293 }294 247 // parameters should not be constructed and destructed, so don't mutate FunctionType 295 248 mutateAll( functionDecl->get_oldDecls(), *this ); 296 249 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); 297 298 managedTypes.endScope();299 250 return functionDecl; 300 251 } 301 302 Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) {303 // don't construct members, but need to take note if there is a managed member,304 // because that means that this type is also managed305 for ( Declaration * member : aggregateDecl->get_members() ) {306 if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) {307 if ( isManaged( field ) ) {308 managedTypes.insert( SymTab::Mangler::mangle( aggregateDecl ) );309 break;310 }311 }312 }313 return aggregateDecl;314 }315 316 CompoundStmt * CtorDtor::mutate( CompoundStmt * compoundStmt ) {317 managedTypes.beginScope();318 CompoundStmt * stmt = Parent::mutate( compoundStmt );319 managedTypes.endScope();320 return stmt;321 }322 323 252 } // namespace InitTweak 324 253
Note:
See TracChangeset
for help on using the changeset viewer.