Changeset a55ebcc


Ignore:
Timestamp:
Feb 5, 2024, 2:17:33 AM (3 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
47bd204
Parents:
020fa10
Message:
  1. Add debug print option for replacePseudoFunc; 2. Change resolver handling enum types; 3. change QualifiedNameExpr? representation pre-resolver; 4. Disable able a test that currently doesn't work
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/CompilationState.cc

    r020fa10 ra55ebcc  
    4343        codegenp = false,
    4444        prettycodegenp = false,
    45         linemarks = false;
     45        linemarks = false,
     46        reppseu = false;
    4647
    4748// Local Variables: //
  • src/CompilationState.h

    r020fa10 ra55ebcc  
    4242        codegenp,
    4343        prettycodegenp,
    44         linemarks;
     44        linemarks,
     45        reppseu;
    4546
    4647// is the compiler building prelude or libcfa?
  • src/ResolvExpr/CandidateFinder.cpp

    r020fa10 ra55ebcc  
    672672                void postvisit( const ast::StmtExpr * stmtExpr );
    673673                void postvisit( const ast::UntypedInitExpr * initExpr );
     674                void postvisit( const ast::QualifiedNameExpr * qualifiedExpr );
    674675
    675676                void postvisit( const ast::InitExpr * ) {
     
    890891                } else if ( auto unionInst = aggrExpr->result.as< ast::UnionInstType >() ) {
    891892                        addAggMembers( unionInst, aggrExpr, *cand, Cost::unsafe, "" );
    892                 } else if ( auto enumInst = aggrExpr->result.as< ast::EnumInstType >() ) {
     893                }
     894                else if ( auto enumInst = aggrExpr->result.as< ast::EnumInstType >() ) {
    893895                        // The Attribute Arrays are not yet generated, need to proxy them
    894896                        // as attribute function call
     
    14011403        void Finder::postvisit( const ast::VariableExpr * variableExpr ) {
    14021404                // not sufficient to just pass `variableExpr` here, type might have changed since
    1403                 // creation
    1404                 if ( auto obj = dynamic_cast<const ast::ObjectDecl *>( variableExpr->var.get() )) {
    1405                         if ( auto enumInstType = dynamic_cast<const ast::EnumInstType *>( obj->type.get() ) ) {
    1406                                 if ( enumInstType->base && enumInstType->base->base ) {
    1407                                         const CodeLocation & location = variableExpr->location;
    1408                                         auto ids = symtab.lookupId( "valueE" );
    1409                                                 for ( ast::SymbolTable::IdData & id : ids ) {
    1410                                                         if ( auto func = id.id.as<ast::FunctionDecl>() ) {
    1411                                                                 if ( func->params.size() == 1 ) {
    1412                                                                         ast::ptr<ast::DeclWithType> valueEParam = func->params.front();
    1413                                                                         auto valueEParamType = valueEParam->get_type();
    1414                                                                         ast::OpenVarSet funcOpen;
    1415                                                                         ast::AssertionSet funcNeed, funcHave;
    1416                                                                         ast::TypeEnvironment funcEnv{ tenv };
    1417                                                                         ast::ptr<ast::Type> common;
    1418                                                                         if ( unifyInexact( valueEParamType, enumInstType, funcEnv, funcNeed, funcHave, funcOpen, WidenMode{ true, true }, common ) ) {
    1419                                                                                 auto appl = new ast::ApplicationExpr( location,
    1420                                                                                         ast::VariableExpr::functionPointer( location,  func), { variableExpr } );
    1421                                                                                 // addCandidate( appl, copy( tenv ),  );
    1422                                                                                 Candidate cand {appl, copy( tenv )};
    1423                                                                                 addCandidate( cand, appl, Cost::safe );
    1424                                                                         }
    1425                                                                 }
    1426                                                         }
    1427                                                 }
    1428                                 }
    1429                        
    1430                         }
    1431                 }
    1432                 addCandidate( variableExpr, tenv );
    1433                
     1405                addCandidate( variableExpr, tenv );             
    14341406        }
    14351407
     
    18071779        }
    18081780
     1781        void Finder::postvisit( const ast::QualifiedNameExpr * expr ) {
     1782                std::vector< ast::SymbolTable::IdData > declList = symtab.lookupId( expr->name );
     1783                if ( declList.empty() ) return;
     1784
     1785                for ( ast::SymbolTable::IdData & data: declList ) {
     1786                        const ast::Type * t = data.id->get_type()->stripReferences();
     1787                        if ( const ast::EnumInstType * enumInstType =
     1788                                dynamic_cast<const ast::EnumInstType *>( t ) ) {
     1789                                if ( enumInstType->base->name == expr->type_decl->name ) {
     1790                                        Cost cost = Cost::zero;
     1791                                        ast::Expr * newExpr = data.combine( expr->location, cost );
     1792                                        CandidateRef newCand =
     1793                                                std::make_shared<Candidate>(
     1794                                                        newExpr, copy( tenv ), ast::OpenVarSet{},
     1795                                                        ast::AssertionSet{}, Cost::zero, cost
     1796                                                );
     1797                                       
     1798                                        if (newCand->expr->env) {
     1799                                                newCand->env.add(*newCand->expr->env);
     1800                                                auto mutExpr = newCand->expr.get_and_mutate();
     1801                                                mutExpr->env  = nullptr;
     1802                                                newCand->expr = mutExpr;
     1803                                        }
     1804
     1805                                        newCand->expr = ast::mutate_field(
     1806                                                newCand->expr.get(), &ast::Expr::result,
     1807                                                renameTyVars( newCand->expr->result ) );
     1808                                        addAnonConversions( newCand );
     1809                                        candidates.emplace_back( std::move( newCand ) );
     1810                                }
     1811                        }
     1812                }
     1813        }
    18091814        // size_t Finder::traceId = Stats::Heap::new_stacktrace_id("Finder");
    18101815        /// Prunes a list of candidates down to those that have the minimum conversion cost for a given
  • src/Validate/FixQualifiedTypes.cpp

    r020fa10 ra55ebcc  
    8989        }
    9090
    91         ast::Expr const * postvisit( ast::QualifiedNameExpr const * t ) {
    92                 assert( location );
    93                 if ( !t->type_decl ) return t;
    94 
    95                 auto enumName = t->type_decl->name;
    96                 const ast::EnumDecl * enumDecl = symtab.lookupEnum( enumName );
    97                 for ( ast::ptr<ast::Decl> const & member : enumDecl->members ) {
    98                         if ( auto memberAsObj = member.as<ast::ObjectDecl>() ) {
    99                                 if ( memberAsObj->name == t->name ) {
    100                                         return new ast::VariableExpr( t->location, memberAsObj );
    101                                 }
    102                         } else {
    103                                 assertf( false, "unhandled qualified child type" );
    104                         }
    105                 }
    106 
    107                 auto var = new ast::ObjectDecl( t->location, t->name,
    108                         new ast::EnumInstType( enumDecl, ast::CV::Const ),
    109                         nullptr, {}, ast::Linkage::Cforall );
    110                 var->mangleName = Mangle::mangle( var );
    111                 return new ast::VariableExpr( t->location, var );
    112         }
    113 
    11491};
    11592
  • src/Validate/ReplacePseudoFunc.cpp

    r020fa10 ra55ebcc  
    99#include "Common/utility.h"
    1010#include "ResolvExpr/Resolver.h"
    11 #include "SymTab/Mangler.h"  // for Mangler
     11#include "SymTab/Mangler.h"
     12
     13#include "AST/Print.hpp"
     14
     15#include "ResolvExpr/CandidateFinder.hpp"
     16
    1217namespace Validate {
    1318
     
    1621std::set<std::string> queryLabels;
    1722std::set<std::string> queryValues;
     23
     24// struct AutoInit {
     25//     ast::EnumDecl const* postvisit( const ast::EnumDecl* expr );
     26// };
    1827
    1928struct WrapEnumValueExpr final : public ast::WithShortCircuiting,
     
    2332    void previsit(const ast::ApplicationExpr* expr);
    2433    void previsit(const ast::CastExpr* expr);
     34    void previsit(const ast::VariableExpr* ) { visit_children = false; }
    2535
    2636    ast::Expr const* postvisit(const ast::VariableExpr* expr);
     
    3343struct PseudoFuncGenerateRoutine final : public ast::WithDeclsToAdd<>,
    3444                                         public ast::WithSymbolTable,
    35                                          public ast::WithShortCircuiting {
     45                                         public ast::WithShortCircuiting,
     46                                         public ast::WithConstTranslationUnit {
    3647    void previsit(const ast::EnumDecl* enumDecl);
    3748};
     
    4354};
    4455
     56// ast::EnumDecl const * AutoInit::postvisit( const ast::EnumDecl * expr ) {
     57//     for ( size_t i = 0; i < expr->members.size(); i++ ) {
     58//         auto mem = expr->members[i].as<ast::ObjectDecl>();
     59//         assert( mem );
     60//         if ( mem->init )
     61//     }
     62//     return expr;
     63// }
     64
    4565void WrapEnumValueExpr::previsit(const ast::ApplicationExpr* expr) {
    46 
    4766    auto varExpr = expr->func.as<ast::VariableExpr>();
    4867    auto fname = ast::getFunctionName(expr);
     
    5271        }
    5372
    54     if (fname == "labelE" || fname == "valueE" || fname == "posE")
     73    if (fname == "labelE" || fname == "valueE" || fname == "posE") {
    5574        visit_children = false;
     75    }
    5676}
    5777
     
    6787
    6888ast::Expr const* WrapEnumValueExpr::postvisit(const ast::VariableExpr* expr) {
    69     visit_children = false;
    7089    if (!expr->result) {
    7190        return expr;
     
    7594            auto untyped = new ast::UntypedExpr(
    7695                expr->location, new ast::NameExpr(expr->location, "valueE"),
    77                 {new ast::VariableExpr(*expr)});
     96                { std::move( expr ) });
    7897            ResolvExpr::ResolveContext context{symtab, transUnit().global};
    7998            auto result = ResolvExpr::findVoidExpression(untyped, context);
    80             if (result.get()) {
    81                 ast::ptr<ast::ApplicationExpr> ret =
     99            ast::ptr<ast::ApplicationExpr> ret =
    82100                    result.strict_as<ast::ApplicationExpr>();
    83                 return new ast::ApplicationExpr(*ret);
    84             }
     101            return ast::deepCopy( ret );
    85102        }
    86103    }
     
    115132}
    116133
     134const ast::Init * getAutoInit( const CodeLocation & location,
     135    const ast::Type * type, ResolvExpr::ResolveContext context, const ast::Init * prev ) {
     136    if ( auto prevInit = dynamic_cast< const ast::SingleInit * >( prev ) ) {
     137        auto prevInitExpr = prevInit->value;
     138        if ( auto constInit = prevInitExpr.as< ast::ConstantExpr >() ) {
     139            // Assume no string literal for now
     140            return new ast::SingleInit(
     141                location,
     142                ast::ConstantExpr::from_int(
     143                    location, constInit->intValue() + 1 )
     144            );
     145        } else {
     146            auto untypedThisInit = new ast::UntypedExpr(
     147                    location,
     148                    new ast::NameExpr( location, "?++" ),
     149                    { prevInitExpr }
     150                );
     151            auto typedInit = ResolvExpr::findSingleExpression(untypedThisInit, type,
     152                context );
     153            return new ast::SingleInit( location, typedInit );
     154        }
     155    }
     156    SemanticError( prev, "Auto Init a List is not implemented" );
     157    return prev;
     158}
     159
    117160void PseudoFuncGenerateRoutine::previsit(const ast::EnumDecl* enumDecl) {
    118161    visit_children = false;
     
    122165    std::vector<ast::ptr<ast::Init>> inits;
    123166    std::vector<ast::ptr<ast::Init>> labels;
    124     for (const ast::Decl* mem : enumDecl->members) {
    125         auto memAsObjectDecl = dynamic_cast<const ast::ObjectDecl*>(mem);
    126         inits.emplace_back(memAsObjectDecl->init);
     167    auto type = enumDecl->base;
     168
     169    for ( size_t i = 0; i < enumDecl->members.size(); i++ ) {
     170        ast::ptr<ast::Decl> mem = enumDecl->members.at( i );
     171        auto memAsObjectDecl = mem.as< ast::ObjectDecl >();
     172        assert( memAsObjectDecl );
     173        if ( memAsObjectDecl->init ) {
     174            inits.emplace_back( memAsObjectDecl->init );
     175        } else {
     176            const CodeLocation & location = mem->location;
     177            if ( i == 0 ) {
     178                inits.emplace_back( new ast::SingleInit(
     179                    location,
     180                    ast::ConstantExpr::from_int( mem->location, 0 )
     181                ) );
     182            } else {
     183                inits.emplace_back( getAutoInit( location, enumDecl->base,
     184                    ResolvExpr::ResolveContext{symtab, transUnit().global},
     185                    inits.at( i - 1 ).as<ast::SingleInit>()) );
     186            }
     187        }
    127188        labels.emplace_back(new ast::SingleInit(
    128             location, ast::ConstantExpr::from_string(location, mem->name)));
    129     }
     189        location, ast::ConstantExpr::from_string(location, mem->name)));
     190    } 
    130191    if (queryValues.count(enumDecl->name)) {
    131192        auto init = new ast::ListInit(location, std::move(inits));
     
    206267                    return ast::ConstantExpr::from_string(expr->location,
    207268                                                          referredName);
    208                 else
     269                else {
    209270                    return getPseudoFuncApplication(location, context, arg.get(),
    210                                                base, "values_");
     271                                               base, "values_");                   
     272                }
     273
    211274            }
    212275        }
  • src/main.cc

    r020fa10 ra55ebcc  
    383383                DUMP( exprp, std::move( transUnit ) );
    384384                PASS( "Replace Pseudo Func", Validate::replacePseudoFunc, transUnit );
    385 
     385                DUMP( reppseu, std::move( transUnit ) );
    386386                PASS( "Fix Init", InitTweak::fix, transUnit, buildingLibrary() );
    387387                PASS( "Erase With", ResolvExpr::eraseWith, transUnit );
     
    536536        { "bbox", bboxp, true, "print AST before box pass" },
    537537        { "bcodegen", bcodegenp, true, "print AST before code generation" },
     538        { "reppseu", reppseu, true, "print AST after replacing pseudo functions" }
    538539};
    539540enum { printoptsSize = sizeof( printopts ) / sizeof( printopts[0] ) };
  • tests/enum_tests/structEnum.cfa

    r020fa10 ra55ebcc  
    1717};
    1818
    19 PointEnum foo(PointEnum in) {
    20      return in;
    21 }
     19// PointEnum foo(PointEnum in) {
     20//      return in;
     21// }
    2222
    2323// The only valid usage
Note: See TracChangeset for help on using the changeset viewer.