Changeset a55ebcc for src/Validate


Ignore:
Timestamp:
Feb 5, 2024, 2:17:33 AM (4 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
Location:
src/Validate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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        }
Note: See TracChangeset for help on using the changeset viewer.