Changeset 58eb9250
- Timestamp:
- Jan 6, 2025, 12:34:42 PM (2 weeks ago)
- Branches:
- master
- Children:
- 1f6623c, 66e7cc1
- Parents:
- 8893ad4
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cpp
r8893ad4 r58eb9250 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 ); … … 84 98 sizedParam->location, 85 99 sizeofName( paramName ), 86 makeLayoutCType()100 getLayoutCType( transUnit ) 87 101 ) ); 88 102 auto alignParam = new ast::ObjectDecl( 89 103 sizedParam->location, 90 104 alignofName( paramName ), 91 makeLayoutCType()105 getLayoutCType( transUnit ) 92 106 ); 93 107 alignParam->attributes.push_back( new ast::Attribute( "unused" ) ); … … 96 110 } 97 111 98 ast::Type * makeLayoutOutType() {99 return new ast::PointerType( makeLayoutType() );112 ast::Type * getLayoutOutType( const ast::TranslationUnit & transUnit ) { 113 return new ast::PointerType( getLayoutType( transUnit ) ); 100 114 } 101 115 … … 110 124 CodeLocation const & location, ast::AggregateDecl const * aggr, 111 125 ast::vector<ast::TypeDecl> const & sizedParams, 112 bool isInFunction, bool isStruct ) { 126 bool isInFunction, bool isStruct, 127 const ast::TranslationUnit & transUnit ) { 113 128 ast::ObjectDecl * sizeParam = new ast::ObjectDecl( 114 129 location, 115 130 sizeofName( aggr->name ), 116 makeLayoutOutType()131 getLayoutOutType( transUnit ) 117 132 ); 118 133 ast::ObjectDecl * alignParam = new ast::ObjectDecl( 119 134 location, 120 135 alignofName( aggr->name ), 121 makeLayoutOutType()136 getLayoutOutType( transUnit ) 122 137 ); 123 138 ast::ObjectDecl * offsetParam = nullptr; … … 127 142 location, 128 143 offsetofName( aggr->name ), 129 makeLayoutOutType()144 getLayoutOutType( transUnit ) 130 145 ); 131 146 params.push_back( offsetParam ); 132 147 } 133 addSTypeParams( params, sizedParams );148 addSTypeParams( params, sizedParams, transUnit ); 134 149 135 150 // Routines at global scope marked "static" to prevent multiple … … 218 233 // Build layout function signature. 219 234 LayoutData layout = buildLayoutFunction( 220 location, decl, sizedParams, isInFunction(), true );235 location, decl, sizedParams, isInFunction(), true, transUnit() ); 221 236 ast::FunctionDecl * layoutDecl = layout.function; 222 237 // Also return these or extract them from the parameter list? … … 292 307 // Build layout function signature. 293 308 LayoutData layout = buildLayoutFunction( 294 location, decl, sizedParams, isInFunction(), false );309 location, decl, sizedParams, isInFunction(), false, transUnit() ); 295 310 ast::FunctionDecl * layoutDecl = layout.function; 296 311 // Also return these or extract them from the parameter list? … … 1390 1405 /// * Move polymorphic returns in function types to pointer-type parameters. 1391 1406 /// * Adds type size and assertion parameters to parameter lists. 1392 struct DeclAdapter final { 1407 struct DeclAdapter final : 1408 public ast::WithConstTranslationUnit { 1393 1409 ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ); 1394 1410 ast::FunctionDecl const * postvisit( ast::FunctionDecl const * decl ); … … 1398 1414 1399 1415 ast::ObjectDecl * makeObj( 1400 CodeLocation const & location, std::string const & name ) { 1416 CodeLocation const & location, std::string const & name, 1417 const ast::TranslationUnit & transUnit ) { 1401 1418 // The size/align parameters may be unused, so add the unused attribute. 1402 1419 return new ast::ObjectDecl( location, name, 1403 makeLayoutCType(),1420 getLayoutCType( transUnit ), 1404 1421 nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr, 1405 1422 { new ast::Attribute( "unused" ) } ); … … 1441 1458 std::string paramName = Mangle::mangleType( ¶mType ); 1442 1459 1443 auto sizeParam = makeObj( typeParam->location, sizeofName( paramName ) ); 1460 auto sizeParam = makeObj( 1461 typeParam->location, sizeofName( paramName ), transUnit() ); 1444 1462 layoutParams.emplace_back( sizeParam ); 1445 1463 1446 auto alignParam = makeObj( typeParam->location, alignofName( paramName ) ); 1464 auto alignParam = makeObj( 1465 typeParam->location, alignofName( paramName ), transUnit() ); 1447 1466 layoutParams.emplace_back( alignParam ); 1448 1467 } … … 1576 1595 /// * Inserts dynamic calculation of polymorphic type layouts where needed. 1577 1596 struct PolyGenericCalculator final : 1578 public ast::WithConstTypeSubstitution, 1597 public ast::WithConstTranslationUnit, 1598 public ast::WithConstTypeSubstitution, 1579 1599 public ast::WithDeclsToAdd, 1580 1600 public ast::WithGuards, … … 1693 1713 1694 1714 ast::ObjectDecl * sizeDecl = new ast::ObjectDecl( decl->location, 1695 sizeofName( typeName ), makeLayoutCType(),1715 sizeofName( typeName ), getLayoutCType( transUnit() ), 1696 1716 new ast::SingleInit( decl->location, 1697 1717 new ast::SizeofExpr( decl->location, deepCopy( base ) ) … … 1699 1719 ); 1700 1720 ast::ObjectDecl * alignDecl = new ast::ObjectDecl( decl->location, 1701 alignofName( typeName ), makeLayoutCType(),1721 alignofName( typeName ), getLayoutCType( transUnit() ), 1702 1722 new ast::SingleInit( decl->location, 1703 1723 new ast::AlignofExpr( decl->location, deepCopy( base ) ) … … 1987 2007 auto offsetArray = makeVar( expr->location, offsetName, 1988 2008 new ast::ArrayType( 1989 makeLayoutType(),2009 getLayoutType( transUnit() ), 1990 2010 ast::ConstantExpr::from_ulong( expr->location, inits.size() ), 1991 2011 ast::FixedLen, … … 2071 2091 // All empty structures have the same layout (size 1, align 1). 2072 2092 makeVar( location, 2073 sizeofName( typeName ), makeLayoutType(),2093 sizeofName( typeName ), getLayoutType( transUnit() ), 2074 2094 new ast::SingleInit( location, 2075 2095 ast::ConstantExpr::from_ulong( location, 1 ) ) ); 2076 2096 makeVar( location, 2077 alignofName( typeName ), makeLayoutType(),2097 alignofName( typeName ), getLayoutType( transUnit() ), 2078 2098 new ast::SingleInit( location, 2079 2099 ast::ConstantExpr::from_ulong( location, 1 ) ) ); … … 2081 2101 } else { 2082 2102 ast::ObjectDecl const * sizeofVar = makeVar( location, 2083 sizeofName( typeName ), makeLayoutType(), nullptr );2103 sizeofName( typeName ), getLayoutType( transUnit() ), nullptr ); 2084 2104 ast::ObjectDecl const * alignofVar = makeVar( location, 2085 alignofName( typeName ), makeLayoutType(), nullptr );2105 alignofName( typeName ), getLayoutType( transUnit() ), nullptr ); 2086 2106 ast::ObjectDecl const * offsetofVar = makeVar( location, 2087 2107 offsetofName( typeName ), 2088 2108 new ast::ArrayType( 2089 makeLayoutType(),2109 getLayoutType( transUnit() ), 2090 2110 ast::ConstantExpr::from_int( location, memberCount ), 2091 2111 ast::FixedLen, … … 2133 2153 2134 2154 ast::ObjectDecl * sizeofVar = makeVar( location, 2135 sizeofName( typeName ), makeLayoutType() );2155 sizeofName( typeName ), getLayoutType( transUnit() ) ); 2136 2156 ast::ObjectDecl * alignofVar = makeVar( location, 2137 alignofName( typeName ), makeLayoutType() );2157 alignofName( typeName ), getLayoutType( transUnit() ) ); 2138 2158 2139 2159 ast::UntypedExpr * layoutCall = new ast::UntypedExpr( location, -
tests/.expect/functions.x86.txt
r8893ad4 r58eb9250 123 123 struct _tuple2_ { 124 124 }; 125 static inline void _layoutof__tuple2_(unsigned long int *_sizeof__tuple2_, unsigned long int *_alignof__tuple2_, unsigned long int *_offsetof__tuple2_, const unsigned long int _sizeof_Y15tuple_param_2_0, __attribute__ ((unused)) const unsigned long int _alignof_Y15tuple_param_2_0, const unsigned long int _sizeof_Y15tuple_param_2_1, __attribute__ ((unused)) const unsigned longint _alignof_Y15tuple_param_2_1){125 static inline void _layoutof__tuple2_(unsigned int *_sizeof__tuple2_, unsigned int *_alignof__tuple2_, unsigned int *_offsetof__tuple2_, const unsigned int _sizeof_Y15tuple_param_2_0, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_2_0, const unsigned int _sizeof_Y15tuple_param_2_1, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_2_1){ 126 126 ((void)((*_sizeof__tuple2_)=0)); 127 127 ((void)((*_alignof__tuple2_)=1)); … … 160 160 struct _tuple3_ { 161 161 }; 162 static inline void _layoutof__tuple3_(unsigned long int *_sizeof__tuple3_, unsigned long int *_alignof__tuple3_, unsigned long int *_offsetof__tuple3_, const unsigned long int _sizeof_Y15tuple_param_3_0, __attribute__ ((unused)) const unsigned long int _alignof_Y15tuple_param_3_0, const unsigned long int _sizeof_Y15tuple_param_3_1, __attribute__ ((unused)) const unsigned long int _alignof_Y15tuple_param_3_1, const unsigned long int _sizeof_Y15tuple_param_3_2, __attribute__ ((unused)) const unsigned longint _alignof_Y15tuple_param_3_2){162 static inline void _layoutof__tuple3_(unsigned int *_sizeof__tuple3_, unsigned int *_alignof__tuple3_, unsigned int *_offsetof__tuple3_, const unsigned int _sizeof_Y15tuple_param_3_0, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_3_0, const unsigned int _sizeof_Y15tuple_param_3_1, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_3_1, const unsigned int _sizeof_Y15tuple_param_3_2, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_3_2){ 163 163 ((void)((*_sizeof__tuple3_)=0)); 164 164 ((void)((*_alignof__tuple3_)=1)); -
tests/nowarn/zero-thunk.cfa
r8893ad4 r58eb9250 6 6 forall( T ) 7 7 T g( zero_t ) { 8 printf( "% ld\n", sizeof(T) );8 printf( "%zd\n", sizeof(T) ); 9 9 return (T){}; 10 10 }
Note: See TracChangeset
for help on using the changeset viewer.