Ignore:
Timestamp:
Jan 19, 2024, 2:42:58 AM (5 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
f988834
Parents:
8b4faf6
Message:

Draft Implementation for enum position pesudo function (posE). EnumPosExpr? is mostly irrelevant for now. It is used in development/code probing and will be removed later

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CandidateFinder.cpp

    r8b4faf6 r59c8dff  
    5252#include "Common/Stats/Counter.h"
    5353
     54#include "AST/Inspect.hpp"             // for getFunctionName
     55
    5456#define PRINT( text ) if ( resolvep ) { text }
    5557
     
    656658                void postvisit( const ast::OffsetofExpr * offsetofExpr );
    657659                void postvisit( const ast::OffsetPackExpr * offsetPackExpr );
     660                void postvisit( const ast::EnumPosExpr * enumPosExpr );
    658661                void postvisit( const ast::LogicalExpr * logicalExpr );
    659662                void postvisit( const ast::ConditionalExpr * conditionalExpr );
     
    14711474        void Finder::postvisit( const ast::OffsetPackExpr * offsetPackExpr ) {
    14721475                addCandidate( offsetPackExpr, tenv );
     1476        }
     1477
     1478        void Finder::postvisit( const ast::EnumPosExpr * enumPosExpr ) {
     1479                CandidateFinder finder( context, tenv );
     1480                finder.find( enumPosExpr->expr );
     1481                CandidateList winners = findMinCost( finder.candidates );
     1482                if ( winners.size() != 1 ) SemanticError( enumPosExpr->expr.get(), "Ambiguous expression in position. ");
     1483                CandidateRef & choice = winners.front();
     1484                auto refExpr = referenceToRvalueConversion( choice->expr, choice->cost );
     1485                auto refResult = (refExpr->result).as<ast::EnumInstType>();
     1486                if ( !refResult ) {
     1487                        SemanticError( refExpr, "Position for Non enum type is not supported" );
     1488                }
     1489                // determineEnumPosConstant( enumPosExpr, refResult );
     1490
     1491                const ast::NameExpr * const nameExpr = enumPosExpr->expr.strict_as<ast::NameExpr>();
     1492                const ast::EnumDecl * base = refResult->base;
     1493                if ( !base ) {
     1494                        SemanticError( enumPosExpr, "Cannot be reference to a defined enumeration type" );
     1495                }
     1496                auto it = std::find_if( std::begin( base->members ), std::end( base->members ),
     1497                        [nameExpr]( ast::ptr<ast::Decl> decl ) { return decl->name == nameExpr->name; } );
     1498                unsigned position = it - base->members.begin();
     1499                addCandidate( ast::ConstantExpr::from_int( enumPosExpr->location, position ), tenv );
    14731500        }
    14741501
Note: See TracChangeset for help on using the changeset viewer.