Changeset 857b5f9


Ignore:
Timestamp:
Jan 23, 2025, 12:09:40 PM (3 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
9e72bae3
Parents:
829821c
Message:

CountExpr? -> CountofExpr?. Actually the main fix was making countof use the same pattern as sizeof/alignof, using a typeof to combine the two cases and have one field instead of two.

Location:
src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/AST/Expr.cpp

    r829821c r857b5f9  
    283283: Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), type( t ) {}
    284284
    285 // --- CountExpr
    286 
    287 CountExpr::CountExpr( const CodeLocation & loc, const Expr * e )
    288 : Expr( loc, new BasicType( BasicKind::LongUnsignedInt) ), expr(e), type( nullptr ) {}
    289 
    290 CountExpr::CountExpr( const CodeLocation & loc, const Type * t )
    291 : Expr( loc, new BasicType( BasicKind::LongUnsignedInt) ), expr(nullptr), type( t ) {}
    292 
    293285// --- AlignofExpr
    294286
    295287AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t )
    296288: Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), type( t ) {}
     289
     290// --- CountofExpr
     291
     292CountofExpr::CountofExpr( const CodeLocation & loc, const Type * t )
     293: Expr( loc, new BasicType( BasicKind::LongUnsignedInt) ), type( t ) {}
    297294
    298295// --- OffsetofExpr
  • TabularUnified src/AST/Expr.hpp

    r829821c r857b5f9  
    490490};
    491491
    492 class CountExpr final : public Expr {
    493 public:
    494         ptr<Expr> expr;
    495         ptr<Type> type;
    496 
    497         CountExpr( const CodeLocation & loc, const Expr * t );
    498         CountExpr( const CodeLocation & loc, const Type * t );
    499 
    500         const Expr * accept( Visitor & v )const override { return v.visit( this ); }
    501 private:
    502         CountExpr * clone() const override { return new CountExpr( *this ); }
    503         MUTATE_FRIEND
    504 };
    505 
    506492/// alignof expression, e.g. `alignof(int)`, `alignof 3+4`
    507493class AlignofExpr final : public Expr {
     
    514500private:
    515501        AlignofExpr * clone() const override { return new AlignofExpr{ *this }; }
     502        MUTATE_FRIEND
     503};
     504
     505/// countof expression, e.g. `countof(AnEnum)`, `countof pred(Head)`
     506class CountofExpr final : public Expr {
     507public:
     508        ptr<Type> type;
     509
     510        CountofExpr( const CodeLocation & loc, const Type * t );
     511
     512        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
     513private:
     514        CountofExpr * clone() const override { return new CountofExpr( *this ); }
    516515        MUTATE_FRIEND
    517516};
  • TabularUnified src/AST/Fwd.hpp

    r829821c r857b5f9  
    8686class ConstantExpr;
    8787class SizeofExpr;
    88 class CountExpr;
    8988class AlignofExpr;
     89class CountofExpr;
    9090class UntypedOffsetofExpr;
    9191class OffsetofExpr;
  • TabularUnified src/AST/Pass.hpp

    r829821c r857b5f9  
    173173        const ast::Expr *             visit( const ast::ConstantExpr         * ) override final;
    174174        const ast::Expr *             visit( const ast::SizeofExpr           * ) override final;
    175         const ast::Expr *             visit( const ast::CountExpr            * ) override final;
    176175        const ast::Expr *             visit( const ast::AlignofExpr          * ) override final;
     176        const ast::Expr *             visit( const ast::CountofExpr          * ) override final;
    177177        const ast::Expr *             visit( const ast::UntypedOffsetofExpr  * ) override final;
    178178        const ast::Expr *             visit( const ast::OffsetofExpr         * ) override final;
  • TabularUnified src/AST/Pass.impl.hpp

    r829821c r857b5f9  
    13501350
    13511351//--------------------------------------------------------------------------
    1352 // CountExpr
    1353 template< typename core_t >
    1354 const ast::Expr * ast::Pass< core_t >::visit( const ast::CountExpr * node ) {
    1355         VISIT_START( node );
    1356         if ( __visit_children() ) {
    1357                 {
    1358                         guard_symtab guard { *this };
    1359                         maybe_accept( node, &CountExpr::result );
    1360                 }
    1361                 if ( node->type ) {
    1362                         maybe_accept( node, &CountExpr::type );
    1363                 } else {
    1364                         maybe_accept( node, &CountExpr::expr );
    1365                 }
    1366         }
    1367         VISIT_END( Expr, node );
    1368 }
    1369 
    1370 //--------------------------------------------------------------------------
    13711352// AlignofExpr
    13721353template< typename core_t >
     
    13801361                }
    13811362                maybe_accept( node, &AlignofExpr::type );
     1363        }
     1364
     1365        VISIT_END( Expr, node );
     1366}
     1367
     1368//--------------------------------------------------------------------------
     1369// CountofExpr
     1370template< typename core_t >
     1371const ast::Expr * ast::Pass< core_t >::visit( const ast::CountofExpr * node ) {
     1372        VISIT_START( node );
     1373
     1374        if ( __visit_children() ) {
     1375                {
     1376                        guard_symtab guard { *this };
     1377                        maybe_accept( node, &CountofExpr::result );
     1378                }
     1379                maybe_accept( node, &CountofExpr::type );
    13821380        }
    13831381
  • TabularUnified src/AST/Print.cpp

    r829821c r857b5f9  
    12071207        }
    12081208
    1209         virtual const ast::Expr * visit( const ast::CountExpr * node ) override final {
     1209        virtual const ast::Expr * visit( const ast::CountofExpr * node ) override final {
    12101210                os << "Count Expression on: ";
    12111211                ++indent;
    1212                 if ( node->type ) node->type->accept( *this );
    1213                 else safe_print( node->expr );
     1212                safe_print( node->type );
    12141213                --indent;
    12151214                postprint( node );
  • TabularUnified src/AST/Visitor.hpp

    r829821c r857b5f9  
    7676    virtual const ast::Expr *             visit( const ast::ConstantExpr         * ) = 0;
    7777    virtual const ast::Expr *             visit( const ast::SizeofExpr           * ) = 0;
    78     virtual const ast::Expr *             visit( const ast::CountExpr            * ) = 0;
    7978    virtual const ast::Expr *             visit( const ast::AlignofExpr          * ) = 0;
     79    virtual const ast::Expr *             visit( const ast::CountofExpr          * ) = 0;
    8080    virtual const ast::Expr *             visit( const ast::UntypedOffsetofExpr  * ) = 0;
    8181    virtual const ast::Expr *             visit( const ast::OffsetofExpr         * ) = 0;
  • TabularUnified src/Common/CodeLocationTools.cpp

    r829821c r857b5f9  
    154154    macro(ConstantExpr, Expr) \
    155155    macro(SizeofExpr, Expr) \
    156     macro(CountExpr, Expr ) \
    157156    macro(AlignofExpr, Expr) \
     157    macro(CountofExpr, Expr ) \
    158158    macro(UntypedOffsetofExpr, Expr) \
    159159    macro(OffsetofExpr, Expr) \
  • TabularUnified src/Parser/parser.yy

    r829821c r857b5f9  
    946946                }
    947947        | COUNTOF unary_expression
    948                 { $$ = new ExpressionNode( new ast::CountExpr( yylloc, maybeMoveBuild( $2 ) ) ); }
     948                { $$ = new ExpressionNode( new ast::CountofExpr( yylloc, new ast::TypeofType( maybeMoveBuild( $2 ) ) ) ); }
    949949        | COUNTOF '(' type_no_function ')'
    950                 { $$ = new ExpressionNode( new ast::CountExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
     950                { $$ = new ExpressionNode( new ast::CountofExpr( yylloc, maybeMoveBuildType( $3 ) ) ); }
    951951        ;
    952952
  • TabularUnified src/ResolvExpr/CandidateFinder.cpp

    r829821c r857b5f9  
    672672                void postvisit( const ast::SizeofExpr * sizeofExpr );
    673673                void postvisit( const ast::AlignofExpr * alignofExpr );
     674                void postvisit( const ast::CountofExpr * countExpr );
    674675                void postvisit( const ast::AddressExpr * addressExpr );
    675676                void postvisit( const ast::LabelAddressExpr * labelExpr );
     
    697698                void postvisit( const ast::UntypedInitExpr * initExpr );
    698699                void postvisit( const ast::QualifiedNameExpr * qualifiedExpr );
    699                 void postvisit( const ast::CountExpr * countExpr );
    700700
    701701                void postvisit( const ast::InitExpr * ) {
     
    941941                }
    942942        }
    943        
    944943
    945944        /// Adds aggregate member interpretations
     
    12691268                                        ? conversionCost( fromType, toType, cand->expr->get_lvalue(), symtab, cand->env )
    12701269                                        : castCost( fromType, toType, cand->expr->get_lvalue(), symtab, cand->env );
    1271                        
     1270
    12721271                        // Redefine enum cast
    12731272                        auto argAsEnum = fromType.as<ast::EnumInstType>();
     
    14931492        }
    14941493
    1495         void Finder::postvisit( const ast::CountExpr * countExpr ) {
    1496                 const ast::UntypedExpr * untyped = nullptr;
    1497                 if ( countExpr->type ) {
    1498                         auto enumInst = countExpr->type.as<ast::EnumInstType>();
    1499                         if ( enumInst ) {
    1500                                 addCandidate( ast::ConstantExpr::from_ulong(countExpr->location, enumInst->base->members.size()), tenv );
    1501                                 return;
    1502                         }
    1503                         auto untypedFirst = ast::UntypedExpr::createCall( countExpr->location, "lowerBound", {} );
    1504                         auto castFirst = new ast::CastExpr( countExpr->location, untypedFirst , countExpr->type );
    1505                         untyped = ast::UntypedExpr::createCall(
    1506                                 countExpr->location, "Countof", { castFirst }
    1507                         );
    1508                 }
    1509                 if (!untyped) untyped = ast::UntypedExpr::createCall(
    1510                                 countExpr->location, "Countof", { countExpr->expr }
     1494        void Finder::postvisit( const ast::CountofExpr * countExpr ) {
     1495                if ( auto enumInst = countExpr->type.as<ast::EnumInstType>() ) {
     1496                        addCandidate( ast::ConstantExpr::from_ulong( countExpr->location, enumInst->base->members.size()), tenv );
     1497                        return;
     1498                }
     1499                auto untypedFirst = ast::UntypedExpr::createCall( countExpr->location, "lowerBound", {} );
     1500                auto castFirst = new ast::CastExpr( countExpr->location, untypedFirst , countExpr->type );
     1501                const ast::UntypedExpr * untyped = ast::UntypedExpr::createCall(
     1502                        countExpr->location, "Countof", { castFirst }
    15111503                );
    15121504                CandidateFinder finder( context, tenv );
     
    15141506                CandidateList winners = findMinCost( finder.candidates );
    15151507                if ( winners.size() == 0 ) {
    1516                         SemanticError( countExpr->expr, "Countof is not implemented for operand: " );
    1517                 }
    1518                 if ( winners.size() !=  1 ) {
    1519                         SemanticError( countExpr->expr, "Ambiguous expression in countof operand: " );
     1508                        SemanticError( countExpr, "Countof is not implemented: " );
     1509                }
     1510                if ( winners.size() != 1 ) {
     1511                        SemanticError( countExpr, "Ambiguous expression in countof: " );
    15201512                }
    15211513                CandidateRef & choice = winners.front();
Note: See TracChangeset for help on using the changeset viewer.