Changeset c75b30a for src/ResolvExpr


Ignore:
Timestamp:
Jan 31, 2024, 6:25:02 PM (5 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
32490deb
Parents:
16afb2a
Message:

Introduce posE, valueE, labelE pseudo language to the language. Rework the internal representation of enumeration.

Location:
src/ResolvExpr
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CandidateFinder.cpp

    r16afb2a rc75b30a  
    891891                } else if ( auto unionInst = aggrExpr->result.as< ast::UnionInstType >() ) {
    892892                        addAggMembers( unionInst, aggrExpr, *cand, Cost::unsafe, "" );
     893                } else if ( auto enumInst = aggrExpr->result.as< ast::EnumInstType >() ) {
     894                        // The Attribute Arrays are not yet generated, need to proxy them
     895                        // as attribute function call
     896                        const CodeLocation & location = cand->expr->location;
     897                        if ( enumInst->base && enumInst->base->base ) {
     898                                auto valueName = new ast::NameExpr(location, "valueE");
     899                                auto untypedValueCall = new ast::UntypedExpr(
     900                                        location, valueName, { aggrExpr } );
     901                                auto result = ResolvExpr::findVoidExpression( untypedValueCall, context );
     902                                assert( result.get() );
     903                                CandidateRef newCand = std::make_shared<Candidate>(
     904                                        *cand, result, Cost::safe );
     905                                candidates.emplace_back( std::move( newCand ) );
     906                        }
    893907                }
    894908        }
     
    961975
    962976                                        if (argType.as<ast::PointerType>()) funcFinder.otypeKeys.insert(Mangle::Encoding::pointer);                                             
    963                                         // else if (const ast::EnumInstType * enumInst = argType.as<ast::EnumInstType>()) {
    964                                         //      const ast::EnumDecl * enumDecl = enumInst->base; // Here
    965                                         //      if ( const ast::Type* enumType = enumDecl->base ) {
    966                                         //              // instance of enum (T) is a instance of type (T)
    967                                         //              funcFinder.otypeKeys.insert(Mangle::mangle(enumType, Mangle::NoGenericParams | Mangle::Type));
    968                                         //      } else {
    969                                         //              // instance of an untyped enum is techically int
    970                                         //              funcFinder.otypeKeys.insert(Mangle::mangle(enumDecl, Mangle::NoGenericParams | Mangle::Type));
    971                                         //      }
    972                                         // }
    973977                                        else funcFinder.otypeKeys.insert(Mangle::mangle(argType, Mangle::NoGenericParams | Mangle::Type));
    974978                                }
     
    13991403                // not sufficient to just pass `variableExpr` here, type might have changed since
    14001404                // creation
    1401                 addCandidate(
    1402                         new ast::VariableExpr{ variableExpr->location, variableExpr->var }, tenv );
     1405                if ( auto obj =  dynamic_cast<const ast::ObjectDecl *>( variableExpr->var.get() )) {
     1406                        if ( auto enumInstType = dynamic_cast<const ast::EnumInstType *>( obj->type.get() ) ) {
     1407                                if ( enumInstType->base && enumInstType->base->base ) {
     1408                                        const CodeLocation & location = variableExpr->location;
     1409                                        auto ids = symtab.lookupId( "valueE" );
     1410                                                for ( ast::SymbolTable::IdData & id : ids ) {
     1411                                                        if ( auto func = id.id.as<ast::FunctionDecl>() ) {
     1412                                                                if ( func->params.size() == 1 ) {
     1413                                                                        ast::ptr<ast::DeclWithType> valueEParam = func->params.front();
     1414                                                                        auto valueEParamType = valueEParam->get_type();
     1415                                                                        ast::OpenVarSet funcOpen;
     1416                                                                        ast::AssertionSet funcNeed, funcHave;
     1417                                                                        ast::TypeEnvironment funcEnv{ tenv };
     1418                                                                        ast::ptr<ast::Type> common;
     1419                                                                        if ( unifyInexact( valueEParamType, enumInstType, funcEnv, funcNeed, funcHave, funcOpen, WidenMode{ true, true }, common ) ) {
     1420                                                                                auto appl = new ast::ApplicationExpr( location,
     1421                                                                                        ast::VariableExpr::functionPointer( location,  func), { variableExpr } );
     1422                                                                                // addCandidate( appl, copy( tenv ),  );
     1423                                                                                Candidate cand {appl, copy( tenv )};
     1424                                                                                addCandidate( cand, appl, Cost::safe );
     1425                                                                        }
     1426                                                                }
     1427                                                        }
     1428                                                }
     1429                                }
     1430                       
     1431                        }
     1432                }
     1433                addCandidate( variableExpr, tenv );
     1434               
    14031435        }
    14041436
  • src/ResolvExpr/ConversionCost.cc

    r16afb2a rc75b30a  
    279279                conversionCostFromBasicToBasic( basicType, dstAsBasic );
    280280        } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    281                 const ast::EnumDecl * enumDecl = enumInst->base.get();
    282                 if ( enumDecl->isTyped && !enumDecl->base.get() ) {
    283                         cost = Cost::infinity;
    284                 } else if ( const ast::Type * enumType = enumDecl->base.get() ) {
    285                         if ( const ast::BasicType * enumTypeAsBasic = dynamic_cast<const ast::BasicType *>(enumType) ) {
    286                                 conversionCostFromBasicToBasic( basicType, enumTypeAsBasic );
    287                         } else {
    288                                 cost = Cost::infinity;
    289                         }
     281                auto enumDecl = enumInst->base;
     282                if ( auto baseType = enumDecl->base.get() ) {
     283                        cost = costCalc( basicType, baseType, srcIsLvalue, symtab, env );
     284                        cost.incUnsafe();
    290285                } else {
    291286            cost = Cost::unsafe;
     
    367362
    368363void ConversionCost::postvisit( const ast::EnumInstType * enumInstType ) {
    369         const ast::EnumDecl * baseEnum = enumInstType->base;
    370         if ( const ast::Type * baseType = baseEnum->base ) {
    371                 costCalc( baseType, dst, srcIsLvalue, symtab, env );
    372         } else {
    373                 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    374                 cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    375         }
     364        //      const ast::EnumDecl * baseEnum = enumInstType->base;
     365        // if ( const ast::Type * baseType = baseEnum->base ) {
     366        //      costCalc( baseType, dst, srcIsLvalue, symtab, env );
     367        // } else {
     368        static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
     369        cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
     370        // }
    376371        if ( cost < Cost::unsafe ) {
    377372                cost.incSafe();
  • src/ResolvExpr/Unify.cc

    r16afb2a rc75b30a  
    7373                ast::Type * newFirst  = shallowCopy( first  );
    7474                ast::Type * newSecond = shallowCopy( second );
    75                 if ( auto temp = dynamic_cast<const ast::EnumInstType *>(first) ) {
    76                         if ( !dynamic_cast< const ast::EnumInstType * >( second ) ) {
    77                                 const ast::EnumDecl * baseEnum = dynamic_cast<const ast::EnumDecl *>(temp->base.get());
    78                                 if ( auto t = baseEnum->base.get() ) {
    79                                         newFirst = ast::shallowCopy( t );
    80                                 }
    81                         }
    82                 } else if ( auto temp = dynamic_cast<const ast::EnumInstType *>(second) ) {
    83                         const ast::EnumDecl * baseEnum = dynamic_cast<const ast::EnumDecl *>(temp->base.get());
    84                         if ( auto t = baseEnum->base.get() ) {
    85                                 newSecond = ast::shallowCopy( t );
    86                         }
    87                 }
    8875
    8976                newFirst ->qualifiers = {};
Note: See TracChangeset for help on using the changeset viewer.