Changeset d60a4c2 for src/GenPoly
- Timestamp:
- Jan 11, 2025, 5:48:46 PM (13 months ago)
- Branches:
- master, stuck-waitfor-destruct
- Children:
- f886608
- Parents:
- 7d65715f (diff), 32a119e9 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
src/GenPoly/Box.cpp (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cpp
r7d65715f rd60a4c2 42 42 43 43 /// The layout type is used to represent sizes, alignments and offsets. 44 ast::BasicType * makeLayoutType() { 45 return new ast::BasicType( ast::BasicKind::LongUnsignedInt ); 44 const ast::Type * getLayoutType( const ast::TranslationUnit & transUnit ) { 45 assert( transUnit.global.sizeType.get() ); 46 return transUnit.global.sizeType; 46 47 } 47 48 48 49 /// Fixed version of layout type (just adding a 'C' in C++ style). 49 ast::BasicType * makeLayoutCType() { 50 return new ast::BasicType( ast::BasicKind::LongUnsignedInt, 51 ast::CV::Qualifiers( ast::CV::Const ) ); 50 const ast::Type * getLayoutCType( const ast::TranslationUnit & transUnit ) { 51 // Hack for optimization: don't want to clone every time, but don't want 52 // to hardcode a global-translation-unit assumption here either. So 53 // cache it: will be fast if there is a single translation unit, but 54 // still correct otherwise. 55 static ast::ptr<ast::Type> lastLayoutType = nullptr; 56 static ast::ptr<ast::Type> lastLayoutCType = nullptr; 57 const ast::Type * curLayoutType = getLayoutType( transUnit ); 58 if (lastLayoutType != curLayoutType ) { 59 lastLayoutCType = ast::deepCopy( curLayoutType ); 60 add_qualifiers( 61 lastLayoutCType, ast::CV::Qualifiers{ ast::CV::Const } ); 62 } 63 return lastLayoutCType; 52 64 } 53 65 … … 55 67 /// Adds layout-generation functions to polymorphic types. 56 68 struct LayoutFunctionBuilder final : 69 public ast::WithConstTranslationUnit, 57 70 public ast::WithDeclsToAdd, 58 71 public ast::WithShortCircuiting, … … 77 90 void addSTypeParams( 78 91 ast::vector<ast::DeclWithType> & params, 79 ast::vector<ast::TypeDecl> const & sizedParams ) { 92 ast::vector<ast::TypeDecl> const & sizedParams, 93 const ast::TranslationUnit & transUnit ) { 80 94 for ( ast::ptr<ast::TypeDecl> const & sizedParam : sizedParams ) { 81 95 ast::TypeInstType inst( sizedParam ); 82 96 std::string paramName = Mangle::mangleType( &inst ); 83 params.emplace_back(new ast::ObjectDecl(97 auto sizeofParam = new ast::ObjectDecl( 84 98 sizedParam->location, 85 99 sizeofName( paramName ), 86 makeLayoutCType() 87 ) ); 88 auto alignParam = new ast::ObjectDecl( 100 getLayoutCType( transUnit ) 101 ); 102 sizeofParam->attributes.push_back( new ast::Attribute( "unused" ) ); 103 params.emplace_back( sizeofParam ); 104 auto alignofParam = new ast::ObjectDecl( 89 105 sizedParam->location, 90 106 alignofName( paramName ), 91 makeLayoutCType()107 getLayoutCType( transUnit ) 92 108 ); 93 align Param->attributes.push_back( new ast::Attribute( "unused" ) );94 params.emplace_back( align Param );95 } 96 } 97 98 ast::Type * makeLayoutOutType() {99 return new ast::PointerType( makeLayoutType() );109 alignofParam->attributes.push_back( new ast::Attribute( "unused" ) ); 110 params.emplace_back( alignofParam ); 111 } 112 } 113 114 ast::Type * getLayoutOutType( const ast::TranslationUnit & transUnit ) { 115 return new ast::PointerType( getLayoutType( transUnit ) ); 100 116 } 101 117 … … 110 126 CodeLocation const & location, ast::AggregateDecl const * aggr, 111 127 ast::vector<ast::TypeDecl> const & sizedParams, 112 bool isInFunction, bool isStruct ) { 128 bool isInFunction, bool isStruct, 129 const ast::TranslationUnit & transUnit ) { 113 130 ast::ObjectDecl * sizeParam = new ast::ObjectDecl( 114 131 location, 115 132 sizeofName( aggr->name ), 116 makeLayoutOutType()133 getLayoutOutType( transUnit ) 117 134 ); 118 135 ast::ObjectDecl * alignParam = new ast::ObjectDecl( 119 136 location, 120 137 alignofName( aggr->name ), 121 makeLayoutOutType()138 getLayoutOutType( transUnit ) 122 139 ); 123 140 ast::ObjectDecl * offsetParam = nullptr; … … 127 144 location, 128 145 offsetofName( aggr->name ), 129 makeLayoutOutType()146 getLayoutOutType( transUnit ) 130 147 ); 131 148 params.push_back( offsetParam ); 132 149 } 133 addSTypeParams( params, sizedParams );150 addSTypeParams( params, sizedParams, transUnit ); 134 151 135 152 // Routines at global scope marked "static" to prevent multiple … … 218 235 // Build layout function signature. 219 236 LayoutData layout = buildLayoutFunction( 220 location, decl, sizedParams, isInFunction(), true );237 location, decl, sizedParams, isInFunction(), true, transUnit() ); 221 238 ast::FunctionDecl * layoutDecl = layout.function; 222 239 // Also return these or extract them from the parameter list? … … 292 309 // Build layout function signature. 293 310 LayoutData layout = buildLayoutFunction( 294 location, decl, sizedParams, isInFunction(), false );311 location, decl, sizedParams, isInFunction(), false, transUnit() ); 295 312 ast::FunctionDecl * layoutDecl = layout.function; 296 313 // Also return these or extract them from the parameter list? … … 1390 1407 /// * Move polymorphic returns in function types to pointer-type parameters. 1391 1408 /// * Adds type size and assertion parameters to parameter lists. 1392 struct DeclAdapter final { 1409 struct DeclAdapter final : 1410 public ast::WithConstTranslationUnit { 1393 1411 ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ); 1394 1412 ast::FunctionDecl const * postvisit( ast::FunctionDecl const * decl ); … … 1398 1416 1399 1417 ast::ObjectDecl * makeObj( 1400 CodeLocation const & location, std::string const & name ) { 1418 CodeLocation const & location, std::string const & name, 1419 const ast::TranslationUnit & transUnit ) { 1401 1420 // The size/align parameters may be unused, so add the unused attribute. 1402 1421 return new ast::ObjectDecl( location, name, 1403 makeLayoutCType(),1422 getLayoutCType( transUnit ), 1404 1423 nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr, 1405 1424 { new ast::Attribute( "unused" ) } ); … … 1441 1460 std::string paramName = Mangle::mangleType( ¶mType ); 1442 1461 1443 auto sizeParam = makeObj( typeParam->location, sizeofName( paramName ) ); 1462 auto sizeParam = makeObj( 1463 typeParam->location, sizeofName( paramName ), transUnit() ); 1444 1464 layoutParams.emplace_back( sizeParam ); 1445 1465 1446 auto alignParam = makeObj( typeParam->location, alignofName( paramName ) ); 1466 auto alignParam = makeObj( 1467 typeParam->location, alignofName( paramName ), transUnit() ); 1447 1468 layoutParams.emplace_back( alignParam ); 1448 1469 } … … 1576 1597 /// * Inserts dynamic calculation of polymorphic type layouts where needed. 1577 1598 struct PolyGenericCalculator final : 1578 public ast::WithConstTypeSubstitution, 1599 public ast::WithConstTranslationUnit, 1600 public ast::WithConstTypeSubstitution, 1579 1601 public ast::WithDeclsToAdd, 1580 1602 public ast::WithGuards, … … 1693 1715 1694 1716 ast::ObjectDecl * sizeDecl = new ast::ObjectDecl( decl->location, 1695 sizeofName( typeName ), makeLayoutCType(),1717 sizeofName( typeName ), getLayoutCType( transUnit() ), 1696 1718 new ast::SingleInit( decl->location, 1697 1719 new ast::SizeofExpr( decl->location, deepCopy( base ) ) … … 1699 1721 ); 1700 1722 ast::ObjectDecl * alignDecl = new ast::ObjectDecl( decl->location, 1701 alignofName( typeName ), makeLayoutCType(),1723 alignofName( typeName ), getLayoutCType( transUnit() ), 1702 1724 new ast::SingleInit( decl->location, 1703 1725 new ast::AlignofExpr( decl->location, deepCopy( base ) ) … … 1987 2009 auto offsetArray = makeVar( expr->location, offsetName, 1988 2010 new ast::ArrayType( 1989 makeLayoutType(),2011 getLayoutType( transUnit() ), 1990 2012 ast::ConstantExpr::from_ulong( expr->location, inits.size() ), 1991 2013 ast::FixedLen, … … 2071 2093 // All empty structures have the same layout (size 1, align 1). 2072 2094 makeVar( location, 2073 sizeofName( typeName ), makeLayoutType(),2095 sizeofName( typeName ), getLayoutType( transUnit() ), 2074 2096 new ast::SingleInit( location, 2075 2097 ast::ConstantExpr::from_ulong( location, 1 ) ) ); 2076 2098 makeVar( location, 2077 alignofName( typeName ), makeLayoutType(),2099 alignofName( typeName ), getLayoutType( transUnit() ), 2078 2100 new ast::SingleInit( location, 2079 2101 ast::ConstantExpr::from_ulong( location, 1 ) ) ); … … 2081 2103 } else { 2082 2104 ast::ObjectDecl const * sizeofVar = makeVar( location, 2083 sizeofName( typeName ), makeLayoutType(), nullptr );2105 sizeofName( typeName ), getLayoutType( transUnit() ), nullptr ); 2084 2106 ast::ObjectDecl const * alignofVar = makeVar( location, 2085 alignofName( typeName ), makeLayoutType(), nullptr );2107 alignofName( typeName ), getLayoutType( transUnit() ), nullptr ); 2086 2108 ast::ObjectDecl const * offsetofVar = makeVar( location, 2087 2109 offsetofName( typeName ), 2088 2110 new ast::ArrayType( 2089 makeLayoutType(),2111 getLayoutType( transUnit() ), 2090 2112 ast::ConstantExpr::from_int( location, memberCount ), 2091 2113 ast::FixedLen, … … 2133 2155 2134 2156 ast::ObjectDecl * sizeofVar = makeVar( location, 2135 sizeofName( typeName ), makeLayoutType() );2157 sizeofName( typeName ), getLayoutType( transUnit() ) ); 2136 2158 ast::ObjectDecl * alignofVar = makeVar( location, 2137 alignofName( typeName ), makeLayoutType() );2159 alignofName( typeName ), getLayoutType( transUnit() ) ); 2138 2160 2139 2161 ast::UntypedExpr * layoutCall = new ast::UntypedExpr( location,
Note:
See TracChangeset
for help on using the changeset viewer.