Changeset 8984003 for src/InitTweak/GenInit.cc
- Timestamp:
- Nov 21, 2023, 4:47:58 PM (5 months ago)
- Branches:
- master
- Children:
- 53dac82
- Parents:
- c36a419
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/GenInit.cc
rc36a419 r8984003 206 206 }; 207 207 208 209 210 211 208 struct ReturnFixer final : 212 209 public ast::WithStmtsToAdd<>, ast::WithGuards, ast::WithShortCircuiting { … … 262 259 } // namespace 263 260 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 261 void genInit( ast::TranslationUnit & transUnit ) { 262 ast::Pass<HoistArrayDimension_NoResolve>::run( transUnit ); 263 ast::Pass<ReturnFixer>::run( transUnit ); 264 } 265 266 void fixReturnStatements( ast::TranslationUnit & transUnit ) { 267 ast::Pass<ReturnFixer>::run( transUnit ); 268 } 269 270 bool ManagedTypes::isManaged( const ast::Type * type ) const { 271 // references are never constructed 272 if ( dynamic_cast< const ast::ReferenceType * >( type ) ) return false; 273 if ( auto tupleType = dynamic_cast< const ast::TupleType * > ( type ) ) { 274 // tuple is also managed if any of its components are managed 275 for (auto & component : tupleType->types) { 276 if (isManaged(component)) return true; 277 } 278 } 279 // need to clear and reset qualifiers when determining if a type is managed 280 // ValueGuard< Type::Qualifiers > qualifiers( type->get_qualifiers() ); 281 auto tmp = shallowCopy(type); 282 tmp->qualifiers = {}; 283 // delete tmp at return 284 ast::ptr<ast::Type> guard = tmp; 285 // a type is managed if it appears in the map of known managed types, or if it contains any polymorphism (is a type variable or generic type containing a type variable) 286 return managedTypes.find( Mangle::mangle( tmp, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ) != managedTypes.end() || GenPoly::isPolyType( tmp ); 287 } 288 289 bool ManagedTypes::isManaged( const ast::ObjectDecl * objDecl ) const { 290 const ast::Type * type = objDecl->type; 291 while ( auto at = dynamic_cast< const ast::ArrayType * >( type ) ) { 292 // must always construct VLAs with an initializer, since this is an error in C 293 if ( at->isVarLen && objDecl->init ) return true; 294 type = at->base; 295 } 296 return isManaged( type ); 297 } 298 299 void ManagedTypes::handleDWT( const ast::DeclWithType * dwt ) { 300 // if this function is a user-defined constructor or destructor, mark down the type as "managed" 301 if ( ! dwt->linkage.is_overrideable && CodeGen::isCtorDtor( dwt->name ) ) { 302 auto & params = GenPoly::getFunctionType( dwt->get_type())->params; 303 assert( ! params.empty() ); 304 // Type * type = InitTweak::getPointerBase( params.front() ); 305 // assert( type ); 306 managedTypes.insert( Mangle::mangle( params.front(), {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ); 307 } 308 } 309 310 void ManagedTypes::handleStruct( const ast::StructDecl * aggregateDecl ) { 311 // don't construct members, but need to take note if there is a managed member, 312 // because that means that this type is also managed 313 for ( auto & member : aggregateDecl->members ) { 314 if ( auto field = member.as<ast::ObjectDecl>() ) { 315 if ( isManaged( field ) ) { 316 // generic parameters should not play a role in determining whether a generic type is constructed - construct all generic types, so that 317 // polymorphic constructors make generic types managed types 318 ast::StructInstType inst( aggregateDecl ); 319 managedTypes.insert( Mangle::mangle( &inst, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ); 320 break; 321 } 322 } 323 } 324 } 325 326 void ManagedTypes::beginScope() { managedTypes.beginScope(); } 327 void ManagedTypes::endScope() { managedTypes.endScope(); } 328 329 ast::ptr<ast::Stmt> genCtorDtor (const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * objDecl, const ast::Expr * arg) { 330 assertf(objDecl, "genCtorDtor passed null objDecl"); 331 InitExpander srcParam(arg); 332 return SymTab::genImplicitCall(srcParam, new ast::VariableExpr(loc, objDecl), loc, fname, objDecl); 333 } 337 334 338 335 ast::ConstructorInit * genCtorInit( const CodeLocation & loc, const ast::ObjectDecl * objDecl ) {
Note: See TracChangeset
for help on using the changeset viewer.