Changes in / [3d618a0:d93b813]


Ignore:
Files:
22 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Expr.cpp

    r3d618a0 rd93b813  
    276276// --- SizeofExpr
    277277
     278SizeofExpr::SizeofExpr( const CodeLocation & loc, const Expr * e )
     279: Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), expr( e ), type( nullptr ) {}
     280
    278281SizeofExpr::SizeofExpr( const CodeLocation & loc, const Type * t )
    279 : Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), type( t ) {}
     282: Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), expr( nullptr ), type( t ) {}
    280283
    281284// --- CountExpr
     
    289292// --- AlignofExpr
    290293
     294AlignofExpr::AlignofExpr( const CodeLocation & loc, const Expr * e )
     295: Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), expr( e ), type( nullptr ) {}
     296
    291297AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t )
    292 : Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), type( t ) {}
     298: Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), expr( nullptr ), type( t ) {}
    293299
    294300// --- OffsetofExpr
  • src/AST/Expr.hpp

    r3d618a0 rd93b813  
    480480class SizeofExpr final : public Expr {
    481481public:
    482         ptr<Type> type;
    483 
    484         SizeofExpr( const CodeLocation & loc, const Type * t );
    485 
    486         const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
    487 private:
    488         SizeofExpr * clone() const override { return new SizeofExpr{ *this }; }
    489         MUTATE_FRIEND
    490 };
    491 
    492 class CountExpr final : public Expr {
    493 public:
    494482        ptr<Expr> expr;
    495483        ptr<Type> type;
    496484
     485        SizeofExpr( const CodeLocation & loc, const Expr * e );
     486        SizeofExpr( const CodeLocation & loc, const Type * t );
     487        // deliberately no disambiguating overload for nullptr_t
     488
     489        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
     490private:
     491        SizeofExpr * clone() const override { return new SizeofExpr{ *this }; }
     492        MUTATE_FRIEND
     493};
     494
     495class CountExpr final : public Expr {
     496public:
     497        ptr<Expr> expr;
     498        ptr<Type> type;
     499
    497500        CountExpr( const CodeLocation & loc, const Expr * t );
    498501        CountExpr( const CodeLocation & loc, const Type * t );
     
    507510class AlignofExpr final : public Expr {
    508511public:
     512        ptr<Expr> expr;
    509513        ptr<Type> type;
    510514
     515        AlignofExpr( const CodeLocation & loc, const Expr * e );
    511516        AlignofExpr( const CodeLocation & loc, const Type * t );
     517        // deliberately no disambiguating overload for nullptr_t
    512518
    513519        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
  • src/AST/Pass.hpp

    r3d618a0 rd93b813  
    342342/// set visit_children false of all child nodes should be ignored
    343343struct WithShortCircuiting {
    344         bool visit_children = true;
     344        bool visit_children;
    345345};
    346346
  • src/AST/Pass.impl.hpp

    r3d618a0 rd93b813  
    13191319                        maybe_accept( node, &SizeofExpr::result );
    13201320                }
    1321                 maybe_accept( node, &SizeofExpr::type );
     1321                if ( node->type ) {
     1322                        maybe_accept( node, &SizeofExpr::type );
     1323                } else {
     1324                        maybe_accept( node, &SizeofExpr::expr );
     1325                }
    13221326        }
    13231327
     
    13551359                        maybe_accept( node, &AlignofExpr::result );
    13561360                }
    1357                 maybe_accept( node, &AlignofExpr::type );
     1361                if ( node->type ) {
     1362                        maybe_accept( node, &AlignofExpr::type );
     1363                } else {
     1364                        maybe_accept( node, &AlignofExpr::expr );
     1365                }
    13581366        }
    13591367
  • src/AST/Print.cpp

    r3d618a0 rd93b813  
    11521152                os << "Sizeof Expression on: ";
    11531153                ++indent;
    1154                 node->type->accept( *this );
     1154                if ( node->type ) node->type->accept( *this );
     1155                else safe_print( node->expr );
    11551156                --indent;
    11561157                postprint( node );
     
    11721173                os << "Alignof Expression on: ";
    11731174                ++indent;
    1174                 node->type->accept( *this );
     1175                if ( node->type ) node->type->accept( *this );
     1176                else safe_print( node->expr );
    11751177                --indent;
    11761178                postprint( node );
  • src/AST/Util.cpp

    r3d618a0 rd93b813  
    104104        }
    105105        assertf( false, "Member not found." );
     106}
     107
     108template<typename node_t>
     109void oneOfExprOrType( const node_t * node ) {
     110        if ( node->expr ) {
     111                assertf( node->expr && !node->type, "Exactly one of expr or type should be set." );
     112        } else {
     113                assertf( !node->expr && node->type, "Exactly one of expr or type should be set." );
     114        }
    106115}
    107116
     
    150159                previsit( (const ParseNode *)node );
    151160                memberMatchesAggregate( node );
     161        }
     162
     163        void previsit( const SizeofExpr * node ) {
     164                previsit( (const ParseNode *)node );
     165                oneOfExprOrType( node );
     166        }
     167
     168        void previsit( const AlignofExpr * node ) {
     169                previsit( (const ParseNode *)node );
     170                oneOfExprOrType( node );
    152171        }
    153172
  • src/CodeGen/CodeGenerator.cpp

    r3d618a0 rd93b813  
    744744        extension( expr );
    745745        output << "sizeof(";
    746         if ( auto type = expr->type.as<ast::TypeofType>() ) {
    747                 type->expr->accept( *visitor );
    748         } else {
     746        if ( expr->type ) {
    749747                output << genType( expr->type, "", options );
     748        } else {
     749                expr->expr->accept( *visitor );
    750750        }
    751751        output << ")";
     
    756756        extension( expr );
    757757        output << "__alignof__(";
    758         if ( auto type = expr->type.as<ast::TypeofType>() ) {
    759                 type->expr->accept( *visitor );
    760         } else {
     758        if ( expr->type ) {
    761759                output << genType( expr->type, "", options );
     760        } else {
     761                expr->expr->accept( *visitor );
    762762        }
    763763        output << ")";
  • src/Concurrency/Waitfor.cpp

    r3d618a0 rd93b813  
    230230                        ast::ConstantExpr::from_int( location, 0 ),
    231231                        new ast::SizeofExpr( location,
    232                                 new ast::TypeofType(
    233                                         new ast::VariableExpr( location, acceptables ) ) ),
     232                                new ast::VariableExpr( location, acceptables ) ),
    234233                }
    235234        );
  • src/GenPoly/Box.cpp

    r3d618a0 rd93b813  
    19251925ast::Expr const * PolyGenericCalculator::postvisit(
    19261926                ast::SizeofExpr const * expr ) {
    1927         ast::Expr const * gen = genSizeof( expr->location, expr->type );
     1927        ast::Type const * type = expr->type ? expr->type : expr->expr->result;
     1928        ast::Expr const * gen = genSizeof( expr->location, type );
    19281929        return ( gen ) ? gen : expr;
    19291930}
     
    19311932ast::Expr const * PolyGenericCalculator::postvisit(
    19321933                ast::AlignofExpr const * expr ) {
    1933         ast::Expr const * gen = genAlignof( expr->location, expr->type );
     1934        ast::Type const * type = expr->type ? expr->type : expr->expr->result;
     1935        ast::Expr const * gen = genAlignof( expr->location, type );
    19341936        return ( gen ) ? gen : expr;
    19351937}
  • src/GenPoly/GenPoly.cpp

    r3d618a0 rd93b813  
    299299                ast::SizeofExpr const * r = as<ast::SizeofExpr>(rhs);
    300300
    301                 assert( l->type );
    302                 assert( r->type );
     301                assert((l->type != nullptr) ^ (l->expr != nullptr));
     302                assert((r->type != nullptr) ^ (r->expr != nullptr));
     303                if ( !(l->type && r->type) ) return false;
    303304
    304305                // mutual recursion with type poly compatibility
  • src/GenPoly/Lvalue.cpp

    r3d618a0 rd93b813  
    607607ast::SizeofExpr const * ReferenceTypeElimination::previsit(
    608608                ast::SizeofExpr const * expr ) {
     609        if ( expr->expr ) return expr;
    609610        return ast::mutate_field( expr, &ast::SizeofExpr::type,
    610611                expr->type->stripReferences() );
     
    613614ast::AlignofExpr const * ReferenceTypeElimination::previsit(
    614615                ast::AlignofExpr const * expr ) {
     616        if ( expr->expr ) return expr;
    615617        return ast::mutate_field( expr, &ast::AlignofExpr::type,
    616618                expr->type->stripReferences() );
  • src/Parser/StatementNode.cpp

    r3d618a0 rd93b813  
    1111// Created On       : Sat May 16 14:59:41 2015
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Mon Sep  9 11:28:07 2024
    14 // Update Count     : 430
     13// Last Modified On : Fri Aug 11 11:44:15 2023
     14// Update Count     : 429
    1515//
    1616
     
    208208
    209209ast::Stmt * build_for( const CodeLocation & location, ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ ) {
    210         std::vector<ast::ptr<ast::Stmt>> astinit;                       // maybe empty
     210        std::vector<ast::ptr<ast::Stmt>> astinit;                                               // maybe empty
    211211        buildMoveList( forctl->init, astinit );
    212212
    213213        if ( forctl->range_over ) {
    214214                ast::Expr * range_over = maybeMoveBuild( forctl->range_over );
    215                 auto kind = forctl->kind;                                               // save before delete, used in return
    216215                delete forctl;
    217216                return new ast::ForStmt( location,
    218217                        std::move( astinit ),
    219                         range_over, kind == OperKinds::LEThan,
     218                        range_over, forctl->kind == OperKinds::LEThan,
    220219                        buildMoveSingle( stmt ),
    221220                        buildMoveOptional( else_ )
  • src/Parser/TypeData.cpp

    r3d618a0 rd93b813  
    14761476                } else if ( cur->has_enumeratorValue() ) {
    14771477                        ast::Expr * initValue;
    1478                         if ( ret->isCfa && ret->base ) {
    1479                                 CodeLocation location = cur->enumeratorValue->location;
    1480                                 initValue = new ast::CastExpr( location, maybeMoveBuild( cur->consume_enumeratorValue() ), ret->base );
     1478                        if (ret->isCfa && ret->base) {
     1479                                initValue = new ast::CastExpr( cur->enumeratorValue->location, maybeMoveBuild( cur->consume_enumeratorValue() ), ret->base  );
    14811480                        } else {
    14821481                                initValue = maybeMoveBuild( cur->consume_enumeratorValue() );
  • src/Parser/parser.yy

    r3d618a0 rd93b813  
    927927                { $$ = new ExpressionNode( build_unary_val( yylloc, OperKinds::Decr, $2 ) ); }
    928928        | SIZEOF unary_expression
    929                 { $$ = new ExpressionNode( new ast::SizeofExpr( yylloc, new ast::TypeofType( maybeMoveBuild( $2 ) ) ) ); }
     929                { $$ = new ExpressionNode( new ast::SizeofExpr( yylloc, maybeMoveBuild( $2 ) ) ); }
    930930        | SIZEOF '(' type_no_function ')'
    931931                { $$ = new ExpressionNode( new ast::SizeofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
    932932        | ALIGNOF unary_expression                                                      // GCC, variable alignment
    933                 { $$ = new ExpressionNode( new ast::AlignofExpr( yylloc, new ast::TypeofType( maybeMoveBuild( $2 ) ) ) ); }
     933                { $$ = new ExpressionNode( new ast::AlignofExpr( yylloc, maybeMoveBuild( $2 ) ) ); }
    934934        | ALIGNOF '(' type_no_function ')'                                      // GCC, type alignment
    935935                { $$ = new ExpressionNode( new ast::AlignofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
  • src/ResolvExpr/CandidateFinder.cpp

    r3d618a0 rd93b813  
    1010// Created On       : Wed Jun 5 14:30:00 2019
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep  9 11:30:11 2024
    13 // Update Count     : 5
     12// Last Modified On : Sat Jun 22 08:07:26 2024
     13// Update Count     : 4
    1414//
    1515
     
    12391239                CandidateList matches;
    12401240                Cost minExprCost = Cost::infinity;
    1241                 // Cost minCastCost = Cost::infinity;
     1241                Cost minCastCost = Cost::infinity;
    12421242                for ( CandidateRef & cand : finder.candidates ) {
    12431243                        ast::ptr< ast::Type > fromType = cand->expr->result;
     
    14821482
    14831483        void Finder::postvisit( const ast::SizeofExpr * sizeofExpr ) {
    1484                 addCandidate(
    1485                         new ast::SizeofExpr{
    1486                                 sizeofExpr->location, resolveTypeof( sizeofExpr->type, context ) },
    1487                         tenv );
     1484                if ( sizeofExpr->type ) {
     1485                        addCandidate(
     1486                                new ast::SizeofExpr{
     1487                                        sizeofExpr->location, resolveTypeof( sizeofExpr->type, context ) },
     1488                                tenv );
     1489                } else {
     1490                        // find all candidates for the argument to sizeof
     1491                        CandidateFinder finder( context, tenv );
     1492                        finder.find( sizeofExpr->expr );
     1493                        // find the lowest-cost candidate, otherwise ambiguous
     1494                        CandidateList winners = findMinCost( finder.candidates );
     1495                        if ( winners.size() != 1 ) {
     1496                                SemanticError(
     1497                                        sizeofExpr->expr.get(), "Ambiguous expression in sizeof operand: " );
     1498                        }
     1499                        // return the lowest-cost candidate
     1500                        CandidateRef & choice = winners.front();
     1501                        choice->expr = referenceToRvalueConversion( choice->expr, choice->cost );
     1502                        choice->cost = Cost::zero;
     1503                        addCandidate( *choice, new ast::SizeofExpr{ sizeofExpr->location, choice->expr } );
     1504                }
    14881505        }
    14891506
     
    15201537
    15211538        void Finder::postvisit( const ast::AlignofExpr * alignofExpr ) {
    1522                 addCandidate(
    1523                         new ast::AlignofExpr{
    1524                                 alignofExpr->location, resolveTypeof( alignofExpr->type, context ) },
    1525                         tenv );
     1539                if ( alignofExpr->type ) {
     1540                        addCandidate(
     1541                                new ast::AlignofExpr{
     1542                                        alignofExpr->location, resolveTypeof( alignofExpr->type, context ) },
     1543                                tenv );
     1544                } else {
     1545                        // find all candidates for the argument to alignof
     1546                        CandidateFinder finder( context, tenv );
     1547                        finder.find( alignofExpr->expr );
     1548                        // find the lowest-cost candidate, otherwise ambiguous
     1549                        CandidateList winners = findMinCost( finder.candidates );
     1550                        if ( winners.size() != 1 ) {
     1551                                SemanticError(
     1552                                        alignofExpr->expr.get(), "Ambiguous expression in alignof operand: " );
     1553                        }
     1554                        // return the lowest-cost candidate
     1555                        CandidateRef & choice = winners.front();
     1556                        choice->expr = referenceToRvalueConversion( choice->expr, choice->cost );
     1557                        choice->cost = Cost::zero;
     1558                        addCandidate(
     1559                                *choice, new ast::AlignofExpr{ alignofExpr->location, choice->expr } );
     1560                }
    15261561        }
    15271562
  • src/ResolvExpr/ConversionCost.cpp

    r3d618a0 rd93b813  
    192192
    193193Cost enumCastCost (
    194         const ast::EnumInstType * src, const ast::EnumInstType * dst,
     194        const ast::EnumInstType * src, const ast::EnumInstType * dst, 
    195195        const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
    196196);
     
    488488// (dst) src is safe is src is a subtype of dst, or dst {inline src, ...}
    489489Cost enumCastCost (
    490         const ast::EnumInstType * src, const ast::EnumInstType * dst,
     490        const ast::EnumInstType * src, const ast::EnumInstType * dst, 
    491491        const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
    492492) {
  • src/ResolvExpr/Unify.cpp

    r3d618a0 rd93b813  
    234234                        if ( !e2so ) return;
    235235
     236                        assert((e1->type != nullptr) ^ (e1->expr != nullptr));
     237                        assert((e2so->type != nullptr) ^ (e2so->expr != nullptr));
     238                        if ( !(e1->type && e2so->type) ) return;
     239
    236240                        // expression unification calls type unification (mutual recursion)
    237241                        result = unifyExact( e1->type, e2so->type, tenv, need, have, open, widen );
  • src/Validate/InitializerLength.cpp

    r3d618a0 rd93b813  
    2929///   int x[] = { 1, 2, 3 };
    3030///   int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
    31 ///   char z[] = "hello";
    3231/// here x and y are known at compile-time to have length 3, so change this into
    3332///   int x[3] = { 1, 2, 3 };
    3433///   int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
    35 ///   char z[6] = "hello";
    3634struct InitializerLength {
    3735        const ast::ObjectDecl * previsit( const ast::ObjectDecl * decl );
    3836};
    3937
    40 ast::ConstantExpr * makeDimension( const ast::ObjectDecl * decl ) {
    41         if ( auto init = decl->init.as<ast::ListInit>() ) {
    42                 return ast::ConstantExpr::from_ulong( decl->location, init->size() );
    43         } else if ( auto init = decl->init.as<ast::SingleInit>() ) {
    44                 if ( auto constant = init->value.as<ast::ConstantExpr>() ) {
    45                         if ( auto type = constant->result.as<ast::ArrayType>() ) {
    46                                 if ( auto dim = type->dimension.as<ast::ConstantExpr>() ) {
    47                                         ast::ConstantExpr * dimension = ast::deepCopy( dim );
    48                                         dimension->location = decl->location;
    49                                         return dimension;
    50                                 }
    51                         }
    52                 }
    53         }
    54         return nullptr;
    55 }
    56 
    5738const ast::ObjectDecl * InitializerLength::previsit( const ast::ObjectDecl * decl ) {
    5839        if ( auto type = decl->type.as<ast::ArrayType>() ) {
    5940                if ( type->dimension ) return decl;
    60                 if ( auto dimension = makeDimension( decl ) ) {
     41                if ( auto init = decl->init.as<ast::ListInit>() ) {
    6142                        ast::ObjectDecl * mutDecl = ast::mutate( decl );
    6243                        ast::ArrayType * mutType = ast::mutate( type );
    63                         mutType->dimension = dimension;
     44                        mutType->dimension = ast::ConstantExpr::from_ulong(
     45                                mutDecl->location, init->size() );
    6446                        mutDecl->type = mutType;
    6547                        return mutDecl;
  • tests/.expect/alloc-ERROR.txt

    r3d618a0 rd93b813  
    1111    ...to:
    1212      Name: dim
    13       Sizeof Expression on: type-of expression Applying untyped:
     13      Sizeof Expression on: Applying untyped:
    1414          Name: *?
    1515        ...to:
  • tests/.expect/extension.arm64.txt

    r3d618a0 rd93b813  
    465465    }
    466466    {
    467         ((void)__extension__ sizeof(signed int ));
     467        ((void)__extension__ sizeof(3));
    468468    }
    469469
     
    473473
    474474    {
    475         ((void)__extension__ __alignof__(signed int ));
     475        ((void)__extension__ __alignof__(__extension__ _X1ai_2));
    476476    }
    477477
  • tests/.expect/extension.x64.txt

    r3d618a0 rd93b813  
    465465    }
    466466    {
    467         ((void)__extension__ sizeof(signed int ));
     467        ((void)__extension__ sizeof(3));
    468468    }
    469469
     
    473473
    474474    {
    475         ((void)__extension__ __alignof__(signed int ));
     475        ((void)__extension__ __alignof__(__extension__ _X1ai_2));
    476476    }
    477477
  • tests/.expect/extension.x86.txt

    r3d618a0 rd93b813  
    465465    }
    466466    {
    467         ((void)__extension__ sizeof(signed int ));
     467        ((void)__extension__ sizeof(3));
    468468    }
    469469
     
    473473
    474474    {
    475         ((void)__extension__ __alignof__(signed int ));
     475        ((void)__extension__ __alignof__(__extension__ _X1ai_2));
    476476    }
    477477
Note: See TracChangeset for help on using the changeset viewer.