Changes in src/InitTweak/GenInit.cc [18ca28e:ac74057]
- File:
-
- 1 edited
-
src/InitTweak/GenInit.cc (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/GenInit.cc
r18ca28e rac74057 85 85 // should not have a ConstructorInit generated. 86 86 87 ManagedTypes managedTypes; 87 bool isManaged( ObjectDecl * objDecl ) const ; // determine if object is managed 88 bool isManaged( Type * type ) const; // determine if type is managed 89 void handleDWT( DeclarationWithType * dwt ); // add type to managed if ctor/dtor 90 GenPoly::ScopedSet< std::string > managedTypes; 88 91 bool inFunction = false; 89 92 }; … … 126 129 // hands off if the function returns a reference - we don't want to allocate a temporary if a variable's address 127 130 // is being returned 128 if ( returnStmt-> expr&& returnVals.size() == 1 && isConstructable( returnVals.front()->get_type() ) ) {131 if ( returnStmt->get_expr() && returnVals.size() == 1 && isConstructable( returnVals.front()->get_type() ) ) { 129 132 // explicitly construct the return value using the return expression and the retVal object 130 assertf( returnVals.front()->name != "", "Function %s has unnamed return value\n", funcName.c_str() ); 131 132 ObjectDecl * retVal = strict_dynamic_cast< ObjectDecl * >( returnVals.front() ); 133 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( returnStmt->expr ) ) { 134 // return statement has already been mutated - don't need to do it again 135 if ( varExpr->var == retVal ) return; 136 } 137 stmtsToAddBefore.push_back( genCtorDtor( "?{}", retVal, returnStmt->get_expr() ) ); 133 assertf( returnVals.front()->get_name() != "", "Function %s has unnamed return value\n", funcName.c_str() ); 134 135 stmtsToAddBefore.push_back( genCtorDtor( "?{}", dynamic_cast< ObjectDecl *>( returnVals.front() ), returnStmt->get_expr() ) ); 138 136 139 137 // return the retVal object 140 returnStmt-> expr = new VariableExpr( returnVals.front() );138 returnStmt->set_expr( new VariableExpr( returnVals.front() ) ); 141 139 } // if 142 140 } … … 201 199 } 202 200 203 bool ManagedTypes::isManaged( Type * type ) const {201 bool CtorDtor::isManaged( Type * type ) const { 204 202 // references are never constructed 205 203 if ( dynamic_cast< ReferenceType * >( type ) ) return false; … … 217 215 } 218 216 219 bool ManagedTypes::isManaged( ObjectDecl * objDecl ) const {217 bool CtorDtor::isManaged( ObjectDecl * objDecl ) const { 220 218 Type * type = objDecl->get_type(); 221 219 while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { … … 225 223 } 226 224 227 void ManagedTypes::handleDWT( DeclarationWithType * dwt ) {225 void CtorDtor::handleDWT( DeclarationWithType * dwt ) { 228 226 // if this function is a user-defined constructor or destructor, mark down the type as "managed" 229 227 if ( ! LinkageSpec::isOverridable( dwt->get_linkage() ) && CodeGen::isCtorDtor( dwt->get_name() ) ) { … … 235 233 } 236 234 } 237 238 void ManagedTypes::handleStruct( StructDecl * aggregateDecl ) {239 // don't construct members, but need to take note if there is a managed member,240 // because that means that this type is also managed241 for ( Declaration * member : aggregateDecl->get_members() ) {242 if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) {243 if ( isManaged( field ) ) {244 StructInstType inst( Type::Qualifiers(), aggregateDecl );245 managedTypes.insert( SymTab::Mangler::mangle( &inst ) );246 break;247 }248 }249 }250 }251 252 void ManagedTypes::beginScope() { managedTypes.beginScope(); }253 void ManagedTypes::endScope() { managedTypes.endScope(); }254 235 255 236 ImplicitCtorDtorStmt * genCtorDtor( const std::string & fname, ObjectDecl * objDecl, Expression * arg ) { … … 296 277 297 278 void CtorDtor::previsit( ObjectDecl * objDecl ) { 298 managedTypes.handleDWT( objDecl );279 handleDWT( objDecl ); 299 280 // hands off if @=, extern, builtin, etc. 300 281 // even if unmanaged, try to construct global or static if initializer is not constexpr, since this is not legal C 301 if ( tryConstruct( objDecl ) && ( managedTypes.isManaged( objDecl ) || ((! inFunction || objDecl->get_storageClasses().is_static ) && ! isConstExpr( objDecl->get_init() ) ) ) ) {282 if ( tryConstruct( objDecl ) && ( isManaged( objDecl ) || ((! inFunction || objDecl->get_storageClasses().is_static ) && ! isConstExpr( objDecl->get_init() ) ) ) ) { 302 283 // constructed objects cannot be designated 303 284 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 @=.\n", objDecl ); … … 314 295 inFunction = true; 315 296 316 managedTypes.handleDWT( functionDecl );297 handleDWT( functionDecl ); 317 298 318 299 GuardScope( managedTypes ); … … 320 301 for ( auto & tyDecl : functionDecl->get_functionType()->get_forall() ) { 321 302 for ( DeclarationWithType *& assertion : tyDecl->get_assertions() ) { 322 managedTypes.handleDWT( assertion );303 handleDWT( assertion ); 323 304 } 324 305 } … … 330 311 visit_children = false; // do not try to construct and destruct aggregate members 331 312 332 managedTypes.handleStruct( aggregateDecl ); 313 // don't construct members, but need to take note if there is a managed member, 314 // because that means that this type is also managed 315 for ( Declaration * member : aggregateDecl->get_members() ) { 316 if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) { 317 if ( isManaged( field ) ) { 318 StructInstType inst( Type::Qualifiers(), aggregateDecl ); 319 managedTypes.insert( SymTab::Mangler::mangle( &inst ) ); 320 break; 321 } 322 } 323 } 333 324 } 334 325
Note:
See TracChangeset
for help on using the changeset viewer.