Changeset 1ba88a0 for src/InitTweak
- Timestamp:
- Sep 4, 2016, 3:25:32 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- b16898e
- Parents:
- 85517ddb
- Location:
- src/InitTweak
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/InitTweak/FixInit.cc ¶
r85517ddb r1ba88a0 787 787 // need to iterate through members in reverse in order for 788 788 // ctor/dtor statements to come out in the right order 789 for ( Declaration * member : ReverseIterate( structDecl->get_members() ) ) {789 for ( Declaration * member : reverseIterate( structDecl->get_members() ) ) { 790 790 DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member ); 791 791 // skip non-DWT members -
TabularUnified src/InitTweak/GenInit.cc ¶
r85517ddb r1ba88a0 25 25 #include "SynTree/Mutator.h" 26 26 #include "SymTab/Autogen.h" 27 #include "SymTab/Mangler.h" 27 28 #include "GenPoly/PolyMutator.h" 28 29 #include "GenPoly/DeclMutator.h" 30 #include "GenPoly/ScopedSet.h" 29 31 30 32 namespace InitTweak { … … 55 57 class CtorDtor : public GenPoly::PolyMutator { 56 58 public: 59 typedef GenPoly::PolyMutator Parent; 60 using Parent::mutate; 57 61 /// create constructor and destructor statements for object declarations. 58 62 /// the actual call statements will be added in after the resolver has run … … 65 69 // should not traverse into any of these declarations to find objects 66 70 // that need to be constructed or destructed 67 virtual Declaration* mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }71 virtual Declaration* mutate( StructDecl *aggregateDecl ); 68 72 virtual Declaration* mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; } 69 73 virtual Declaration* mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; } … … 74 78 virtual Type * mutate( FunctionType *funcType ) { return funcType; } 75 79 76 protected: 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; 77 91 }; 78 92 … … 142 156 143 157 DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) { 144 std::list<DeclarationWithType*> oldReturnVals = returnVals;145 std::string oldFuncName = funcName;158 ValueGuard< std::list<DeclarationWithType*> > oldReturnVals( returnVals ); 159 ValueGuard< std::string > oldFuncName( funcName ); 146 160 147 161 FunctionType * type = functionDecl->get_functionType(); … … 149 163 funcName = functionDecl->get_name(); 150 164 DeclarationWithType * decl = Mutator::mutate( functionDecl ); 151 returnVals = oldReturnVals;152 funcName = oldFuncName;153 165 return decl; 154 166 } … … 197 209 198 210 DeclarationWithType * HoistArrayDimension::mutate( FunctionDecl *functionDecl ) { 199 bool oldInFunc = inFunction;211 ValueGuard< bool > oldInFunc( inFunction ); 200 212 inFunction = true; 201 213 DeclarationWithType * decl = Parent::mutate( functionDecl ); 202 inFunction = oldInFunc;203 214 return decl; 204 215 } … … 209 220 } 210 221 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 211 240 DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) { 212 // hands off if designated, if @=, or if extern 213 if ( tryConstruct( 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 214 249 // call into genImplicitCall from Autogen.h to generate calls to ctor/dtor 215 250 // for each constructable object … … 241 276 } 242 277 } 243 return Mutator::mutate( objDecl );278 return Parent::mutate( objDecl ); 244 279 } 245 280 246 281 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/dtors 289 for ( TypeDecl * tyDecl : functionDecl->get_functionType()->get_forall() ) { 290 for ( DeclarationWithType *& assertion : tyDecl->get_assertions() ) { 291 assertion = assertion->acceptMutator( *this ); 292 } 293 } 247 294 // parameters should not be constructed and destructed, so don't mutate FunctionType 248 295 mutateAll( functionDecl->get_oldDecls(), *this ); 249 296 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); 297 298 managedTypes.endScope(); 250 299 return functionDecl; 251 300 } 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 managed 305 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 252 323 } // namespace InitTweak 253 324 -
TabularUnified src/InitTweak/InitTweak.cc ¶
r85517ddb r1ba88a0 232 232 return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) && 233 233 (objDecl->get_init() == NULL || 234 ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )) && 235 ! isDesignated( objDecl->get_init() ) 234 ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )) 236 235 && objDecl->get_storageClass() != DeclarationNode::Extern; 237 236 } … … 391 390 virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; } 392 391 virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; } 393 virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; } 394 virtual void visit( CastExpr *castExpr ) { isConstExpr = false; } 392 virtual void visit( NameExpr *nameExpr ) { 393 // xxx - temporary hack, because 0 and 1 really should be constexprs, even though they technically aren't in Cforall today 394 if ( nameExpr->get_name() != "0" && nameExpr->get_name() != "1" ) isConstExpr = false; 395 } 396 // virtual void visit( CastExpr *castExpr ) { isConstExpr = false; } 397 virtual void visit( AddressExpr *addressExpr ) { 398 // address of a variable or member expression is constexpr 399 Expression * arg = addressExpr->get_arg(); 400 if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false; 401 } 395 402 virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; } 396 403 virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; } 397 404 virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; } 398 405 virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; } 399 virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ }400 406 // these might be okay? 401 407 // virtual void visit( SizeofExpr *sizeofExpr );
Note: See TracChangeset
for help on using the changeset viewer.