Changeset dd900b5


Ignore:
Timestamp:
Dec 1, 2023, 2:37:02 PM (13 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
c4b9fa9
Parents:
c4570af3
Message:

Implicit and layout parameters are now marked as const. This is primarily a safety feature to catch bad writes.

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cpp

    rc4570af3 rdd900b5  
    4040namespace {
    4141
     42/// The layout type is used to represent sizes, alignments and offsets.
     43ast::BasicType * makeLayoutType() {
     44        return new ast::BasicType( ast::BasicType::LongUnsignedInt );
     45}
     46
     47/// Fixed version of layout type (just adding a 'C' in C++ style).
     48ast::BasicType * makeLayoutCType() {
     49        return new ast::BasicType( ast::BasicType::LongUnsignedInt,
     50                ast::CV::Qualifiers( ast::CV::Const ) );
     51}
     52
    4253// --------------------------------------------------------------------------
    4354/// Adds layout-generation functions to polymorphic types.
     
    6071        }
    6172        return sizedParams;
    62 }
    63 
    64 ast::BasicType * makeSizeAlignType() {
    65         return new ast::BasicType( ast::BasicType::LongUnsignedInt );
    6673}
    6774
     
    7683                        sizedParam->location,
    7784                        sizeofName( paramName ),
    78                         makeSizeAlignType()
     85                        makeLayoutCType()
    7986                ) );
    8087                params.emplace_back( new ast::ObjectDecl(
    8188                        sizedParam->location,
    8289                        alignofName( paramName ),
    83                         makeSizeAlignType()
     90                        makeLayoutCType()
    8491                ) );
    8592        }
    8693}
    8794
    88 ast::Type * makeSizeAlignOutType() {
    89         return new ast::PointerType( makeSizeAlignType() );
     95ast::Type * makeLayoutOutType() {
     96        return new ast::PointerType( makeLayoutType() );
    9097}
    9198
     
    104111                location,
    105112                sizeofName( aggr->name ),
    106                 makeSizeAlignOutType()
     113                makeLayoutOutType()
    107114        );
    108115        ast::ObjectDecl * alignParam = new ast::ObjectDecl(
    109116                location,
    110117                alignofName( aggr->name ),
    111                 makeSizeAlignOutType()
     118                makeLayoutOutType()
    112119        );
    113120        ast::ObjectDecl * offsetParam = nullptr;
     
    117124                        location,
    118125                        offsetofName( aggr->name ),
    119                         makeSizeAlignOutType()
     126                        makeLayoutOutType()
    120127                );
    121128                params.push_back( offsetParam );
     
    13721379};
    13731380
    1374 // size/align/offset parameters may not be used, so add the unused attribute.
    13751381ast::ObjectDecl * makeObj(
    13761382                CodeLocation const & location, std::string const & name ) {
     1383        // The size/align parameters may be unused, so add the unused attribute.
    13771384        return new ast::ObjectDecl( location, name,
    1378                 makeSizeAlignType(),
     1385                makeLayoutCType(),
    13791386                nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr,
    13801387                { new ast::Attribute( "unused" ) } );
     1388}
     1389
     1390/// A modified and specialized version of ast::add_qualifiers.
     1391ast::Type const * addConst( ast::Type const * type ) {
     1392        ast::CV::Qualifiers cvq = { ast::CV::Const };
     1393        if ( ( type->qualifiers & cvq ) != 0 ) return type;
     1394        auto mutType = ast::mutate( type );
     1395        mutType->qualifiers |= cvq;
     1396        return mutType;
    13811397}
    13821398
     
    14181434        }
    14191435        for ( ast::ptr<ast::DeclWithType> & assert : mutDecl->assertions ) {
     1436                ast::DeclWithType * mutAssert = ast::mutate( assert.get() );
    14201437                // Assertion parameters may not be used in body,
    14211438                // pass along with unused attribute.
    1422                 assert.get_and_mutate()->attributes.push_back(
    1423                         new ast::Attribute( "unused" ) );
    1424                 inferredParams.push_back( assert );
     1439                mutAssert->attributes.push_back( new ast::Attribute( "unused" ) );
     1440                mutAssert->set_type( addConst( mutAssert->get_type() ) );
     1441                inferredParams.emplace_back( mutAssert );
    14251442        }
    14261443        mutDecl->assertions.clear();
     
    16451662        ast::TypeInstType inst( decl->name, decl );
    16461663        std::string typeName = Mangle::mangleType( &inst );
    1647         ast::Type * layoutType = new ast::BasicType(
    1648                 ast::BasicType::LongUnsignedInt );
    16491664
    16501665        ast::ObjectDecl * sizeDecl = new ast::ObjectDecl( decl->location,
    1651                 sizeofName( typeName ), layoutType,
     1666                sizeofName( typeName ), makeLayoutCType(),
    16521667                new ast::SingleInit( decl->location,
    16531668                        new ast::SizeofExpr( decl->location, deepCopy( base ) )
     
    16551670        );
    16561671        ast::ObjectDecl * alignDecl = new ast::ObjectDecl( decl->location,
    1657                 alignofName( typeName ), layoutType,
     1672                alignofName( typeName ), makeLayoutCType(),
    16581673                new ast::SingleInit( decl->location,
    16591674                        new ast::AlignofExpr( decl->location, deepCopy( base ) )
     
    19121927        knownOffsets.insert( offsetName );
    19131928
    1914         auto baseMembers = type->base->members;
    1915         ast::Type const * offsetType = new ast::BasicType(
    1916                 ast::BasicType::LongUnsignedInt );
    1917 
    19181929        // Build initializer list for offset array.
    19191930        ast::vector<ast::Init> inits;
    1920         for ( ast::ptr<ast::Decl> & member : baseMembers ) {
     1931        for ( ast::ptr<ast::Decl> const & member : type->base->members ) {
    19211932                auto memberDecl = member.as<ast::DeclWithType>();
    19221933                assertf( memberDecl, "Requesting offset of non-DWT member: %s",
     
    19321943        auto offsetArray = makeVar( expr->location, offsetName,
    19331944                new ast::ArrayType(
    1934                         offsetType,
    1935                         ast::ConstantExpr::from_ulong( expr->location, baseMembers.size() ),
     1945                        makeLayoutType(),
     1946                        ast::ConstantExpr::from_ulong( expr->location, inits.size() ),
    19361947                        ast::FixedLen,
    19371948                        ast::DynamicDim
     
    20122023                // parameters to the layout call.
    20132024                knownLayouts.insert( typeName );
    2014                 ast::Type const * layoutType = makeSizeAlignType();
    20152025
    20162026                int memberCount = inst->base->members.size();
     
    20182028                        // All empty structures have the same layout (size 1, align 1).
    20192029                        makeVar( location,
    2020                                 sizeofName( typeName ), layoutType,
     2030                                sizeofName( typeName ), makeLayoutType(),
    20212031                                new ast::SingleInit( location,
    20222032                                                ast::ConstantExpr::from_ulong( location, 1 ) ) );
    20232033                        makeVar( location,
    2024                                 alignofName( typeName ), ast::deepCopy( layoutType ),
     2034                                alignofName( typeName ), makeLayoutType(),
    20252035                                new ast::SingleInit( location,
    20262036                                                ast::ConstantExpr::from_ulong( location, 1 ) ) );
     
    20282038                } else {
    20292039                        ast::ObjectDecl const * sizeofVar = makeVar( location,
    2030                                 sizeofName( typeName ), deepCopy( layoutType ), nullptr );
     2040                                sizeofName( typeName ), makeLayoutType(), nullptr );
    20312041                        ast::ObjectDecl const * alignofVar = makeVar( location,
    2032                                 alignofName( typeName ), deepCopy( layoutType ), nullptr );
     2042                                alignofName( typeName ), makeLayoutType(), nullptr );
    20332043                        ast::ObjectDecl const * offsetofVar = makeVar( location,
    20342044                                offsetofName( typeName ),
    20352045                                new ast::ArrayType(
    2036                                         layoutType,
     2046                                        makeLayoutType(),
    20372047                                        ast::ConstantExpr::from_int( location, memberCount ),
    20382048                                        ast::FixedLen,
     
    20782088                // parameters to the layout call.
    20792089                knownLayouts.insert( typeName );
    2080                 ast::Type const * layoutType = makeSizeAlignType();
    20812090
    20822091                ast::ObjectDecl * sizeofVar = makeVar( location,
    2083                         sizeofName( typeName ), layoutType );
     2092                        sizeofName( typeName ), makeLayoutType() );
    20842093                ast::ObjectDecl * alignofVar = makeVar( location,
    2085                         alignofName( typeName ), ast::deepCopy( layoutType ) );
     2094                        alignofName( typeName ), makeLayoutType() );
    20862095
    20872096                ast::UntypedExpr * layoutCall = new ast::UntypedExpr( location,
  • tests/.expect/functions.arm64.txt

    rc4570af3 rdd900b5  
    105105struct _tuple2_ {
    106106};
    107 static inline void _layoutof__tuple2_(unsigned long int *_sizeof__tuple2_, unsigned long int *_alignof__tuple2_, unsigned long int *_offsetof__tuple2_, unsigned long int _sizeof_Y15tuple_param_2_0, unsigned long int _alignof_Y15tuple_param_2_0, unsigned long int _sizeof_Y15tuple_param_2_1, unsigned long int _alignof_Y15tuple_param_2_1){
     107static 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, const unsigned long int _alignof_Y15tuple_param_2_0, const unsigned long int _sizeof_Y15tuple_param_2_1, const unsigned long int _alignof_Y15tuple_param_2_1){
    108108    ((void)((*_sizeof__tuple2_)=0));
    109109    ((void)((*_alignof__tuple2_)=1));
     
    136136struct _tuple3_ {
    137137};
    138 static inline void _layoutof__tuple3_(unsigned long int *_sizeof__tuple3_, unsigned long int *_alignof__tuple3_, unsigned long int *_offsetof__tuple3_, unsigned long int _sizeof_Y15tuple_param_3_0, unsigned long int _alignof_Y15tuple_param_3_0, unsigned long int _sizeof_Y15tuple_param_3_1, unsigned long int _alignof_Y15tuple_param_3_1, unsigned long int _sizeof_Y15tuple_param_3_2, unsigned long int _alignof_Y15tuple_param_3_2){
     138static 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, const unsigned long int _alignof_Y15tuple_param_3_0, const unsigned long int _sizeof_Y15tuple_param_3_1, const unsigned long int _alignof_Y15tuple_param_3_1, const unsigned long int _sizeof_Y15tuple_param_3_2, const unsigned long int _alignof_Y15tuple_param_3_2){
    139139    ((void)((*_sizeof__tuple3_)=0));
    140140    ((void)((*_alignof__tuple3_)=1));
  • tests/.expect/functions.x64.txt

    rc4570af3 rdd900b5  
    105105struct _tuple2_ {
    106106};
    107 static inline void _layoutof__tuple2_(unsigned long int *_sizeof__tuple2_, unsigned long int *_alignof__tuple2_, unsigned long int *_offsetof__tuple2_, unsigned long int _sizeof_Y15tuple_param_2_0, unsigned long int _alignof_Y15tuple_param_2_0, unsigned long int _sizeof_Y15tuple_param_2_1, unsigned long int _alignof_Y15tuple_param_2_1){
     107static 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, const unsigned long int _alignof_Y15tuple_param_2_0, const unsigned long int _sizeof_Y15tuple_param_2_1, const unsigned long int _alignof_Y15tuple_param_2_1){
    108108    ((void)((*_sizeof__tuple2_)=0));
    109109    ((void)((*_alignof__tuple2_)=1));
     
    136136struct _tuple3_ {
    137137};
    138 static inline void _layoutof__tuple3_(unsigned long int *_sizeof__tuple3_, unsigned long int *_alignof__tuple3_, unsigned long int *_offsetof__tuple3_, unsigned long int _sizeof_Y15tuple_param_3_0, unsigned long int _alignof_Y15tuple_param_3_0, unsigned long int _sizeof_Y15tuple_param_3_1, unsigned long int _alignof_Y15tuple_param_3_1, unsigned long int _sizeof_Y15tuple_param_3_2, unsigned long int _alignof_Y15tuple_param_3_2){
     138static 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, const unsigned long int _alignof_Y15tuple_param_3_0, const unsigned long int _sizeof_Y15tuple_param_3_1, const unsigned long int _alignof_Y15tuple_param_3_1, const unsigned long int _sizeof_Y15tuple_param_3_2, const unsigned long int _alignof_Y15tuple_param_3_2){
    139139    ((void)((*_sizeof__tuple3_)=0));
    140140    ((void)((*_alignof__tuple3_)=1));
  • tests/.expect/functions.x86.txt

    rc4570af3 rdd900b5  
    105105struct _tuple2_ {
    106106};
    107 static inline void _layoutof__tuple2_(unsigned long int *_sizeof__tuple2_, unsigned long int *_alignof__tuple2_, unsigned long int *_offsetof__tuple2_, unsigned long int _sizeof_Y15tuple_param_2_0, unsigned long int _alignof_Y15tuple_param_2_0, unsigned long int _sizeof_Y15tuple_param_2_1, unsigned long int _alignof_Y15tuple_param_2_1){
     107static 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, const unsigned long int _alignof_Y15tuple_param_2_0, const unsigned long int _sizeof_Y15tuple_param_2_1, const unsigned long int _alignof_Y15tuple_param_2_1){
    108108    ((void)((*_sizeof__tuple2_)=0));
    109109    ((void)((*_alignof__tuple2_)=1));
     
    136136struct _tuple3_ {
    137137};
    138 static inline void _layoutof__tuple3_(unsigned long int *_sizeof__tuple3_, unsigned long int *_alignof__tuple3_, unsigned long int *_offsetof__tuple3_, unsigned long int _sizeof_Y15tuple_param_3_0, unsigned long int _alignof_Y15tuple_param_3_0, unsigned long int _sizeof_Y15tuple_param_3_1, unsigned long int _alignof_Y15tuple_param_3_1, unsigned long int _sizeof_Y15tuple_param_3_2, unsigned long int _alignof_Y15tuple_param_3_2){
     138static 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, const unsigned long int _alignof_Y15tuple_param_3_0, const unsigned long int _sizeof_Y15tuple_param_3_1, const unsigned long int _alignof_Y15tuple_param_3_1, const unsigned long int _sizeof_Y15tuple_param_3_2, const unsigned long int _alignof_Y15tuple_param_3_2){
    139139    ((void)((*_sizeof__tuple3_)=0));
    140140    ((void)((*_alignof__tuple3_)=1));
Note: See TracChangeset for help on using the changeset viewer.