Changeset 0c327ce for src/ResolvExpr


Ignore:
Timestamp:
Jul 12, 2024, 3:30:06 PM (8 days ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
76b507d
Parents:
9c447e2
Message:
  1. Add bound check to Serial function: now compiler generates the unchecked functions in ImplementEnumFunc?, and enum.hfa implements the bound check on top. Todo: Wrapped the checked version into a trait; 2. countof is now works on any type that implement Countof(). Because Countof() is implemented in enum.hfa for all CfaEnum?, we can call countof on { T | CfaEnum?(T) }
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CandidateFinder.cpp

    r9c447e2 r0c327ce  
    14851485
    14861486        void Finder::postvisit( const ast::CountExpr * countExpr ) {
    1487                 assert( countExpr->type );
    1488                 auto enumInst = countExpr->type.as<ast::EnumInstType>();
    1489                 if ( !enumInst ) {
    1490                         SemanticError( countExpr, "Count Expression only supports Enum Type as operand: ");
    1491                 }
    1492                 addCandidate( ast::ConstantExpr::from_ulong(countExpr->location, enumInst->base->members.size()), tenv );
     1487                const ast::UntypedExpr * untyped;
     1488                if ( countExpr->type ) {
     1489                        auto enumInst = countExpr->type.as<ast::EnumInstType>();
     1490                        if ( enumInst ) {
     1491                                addCandidate( ast::ConstantExpr::from_ulong(countExpr->location, enumInst->base->members.size()), tenv );
     1492                                return;
     1493                        }
     1494                        auto untypedFirst = ast::UntypedExpr::createCall( countExpr->location, "lowerBound", {} );
     1495                        auto castFirst = new ast::CastExpr( countExpr->location, untypedFirst , countExpr->type );
     1496                        untyped = ast::UntypedExpr::createCall(
     1497                                countExpr->location, "Countof", { castFirst }
     1498                        );
     1499                }
     1500                if (!untyped) untyped = ast::UntypedExpr::createCall(
     1501                                countExpr->location, "Countof", { countExpr->expr }
     1502                );
     1503                CandidateFinder finder( context, tenv );
     1504                finder.find( untyped );
     1505                CandidateList winners = findMinCost( finder.candidates );
     1506                if ( winners.size() == 0 ) {
     1507                        SemanticError( countExpr->expr, "Countof is not implemented for operand: " );
     1508                }
     1509                if ( winners.size() !=  1 ) {
     1510                        SemanticError( countExpr->expr, "Ambiguous expression in countof operand: " );
     1511                }
     1512                CandidateRef & choice = winners.front();
     1513                choice->expr = referenceToRvalueConversion( choice->expr, choice->cost );
     1514                addCandidate( *choice, choice->expr );
    14931515        }
    14941516
Note: See TracChangeset for help on using the changeset viewer.