source: src/Validate/ReplacePseudoFunc.cpp @ 45d1ab9

Last change on this file since 45d1ab9 was ac939461, checked in by JiadaL <j82liang@…>, 6 months ago

Add replacePseudoFunc

  • Property mode set to 100644
File size: 1.6 KB
Line 
1#include "AST/Decl.hpp"
2#include "AST/Pass.hpp"
3#include "AST/Stmt.hpp"
4#include "AST/Inspect.hpp"
5#include "Common/utility.h"
6#include "ReplacePseudoFunc.hpp"
7
8namespace Validate {
9
10namespace {
11
12struct ReplacePseudoFuncCore {
13    ast::Expr const * postvisit( ast::ApplicationExpr const * decl );
14};
15}
16
17ast::Expr const * ReplacePseudoFuncCore::postvisit( ast::ApplicationExpr const * expr) {
18    auto fname = ast::getFunctionName( expr );
19    if ( fname == "posE" ) {
20        // std::cerr << "Found App in ReplacePseudoFunc" << std::endl;
21        if ( expr->args.size() != 1 ) {
22            SemanticError( expr, "Position Expression only take one parameter" );
23        }
24        const ast::VariableExpr * arg = expr->args.front().as<const ast::VariableExpr>();
25        if ( !arg ) {
26            SemanticError( expr, "Unimplement Pseudo Function Cases" );
27        }
28        const ast::ObjectDecl * argAsVar = arg->var.as<const ast::ObjectDecl>();
29        const std::string referredName = argAsVar->name;
30        const ast::EnumInstType * argType = argAsVar->type.as<const ast::EnumInstType>();
31        if ( !argType ) {
32            SemanticError( argAsVar, "Position can only be used on an enumeration instance" );
33        }
34        const ast::EnumDecl * base = argType->base;
35        for ( size_t i = 0; i < base->members.size(); i++ ) {
36            if ( base->members[i]->name == referredName ) {
37                return ast::ConstantExpr::from_int( expr->location, i );
38            }
39        }
40    }
41    return expr;
42}
43
44
45
46void replacePseudoFunc( ast::TranslationUnit & translationUnit ) {
47    ast::Pass<ReplacePseudoFuncCore>::run( translationUnit );
48}
49
50}
Note: See TracBrowser for help on using the repository browser.