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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.