Last change
on this file since f2898df was ac939461, checked in by JiadaL <j82liang@…>, 20 months ago |
Add replacePseudoFunc
|
-
Property mode
set to
100644
|
File size:
1.6 KB
|
Rev | Line | |
---|
[ac939461] | 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 |
|
---|
| 8 | namespace Validate {
|
---|
| 9 |
|
---|
| 10 | namespace {
|
---|
| 11 |
|
---|
| 12 | struct ReplacePseudoFuncCore {
|
---|
| 13 | ast::Expr const * postvisit( ast::ApplicationExpr const * decl );
|
---|
| 14 | };
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | ast::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 |
|
---|
| 46 | void replacePseudoFunc( ast::TranslationUnit & translationUnit ) {
|
---|
| 47 | ast::Pass<ReplacePseudoFuncCore>::run( translationUnit );
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.