Changeset d908563 for src/Common
- Timestamp:
- May 27, 2019, 11:08:54 AM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- f88a252
- Parents:
- 933f32f (diff), 21a44ca (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src/Common
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/Eval.cc
r933f32f rd908563 17 17 18 18 #include "Common/PassVisitor.h" 19 #include "AST/Pass.hpp" 19 20 #include "InitTweak/InitTweak.h" 20 21 #include "SynTree/Expression.h" 21 22 22 struct Eval : public WithShortCircuiting { 23 //------------------------------------------------------------- 24 // Old AST 25 struct EvalOld : public WithShortCircuiting { 23 26 long long int value = 0; 24 27 bool valid = true; … … 80 83 }; 81 84 85 //------------------------------------------------------------- 86 // New AST 87 struct EvalNew : public ast::WithShortCircuiting { 88 long long int value = 0; 89 bool valid = true; 90 91 void previsit( const ast::Node * ) { visit_children = false; } 92 void postvisit( const ast::Node * ) { valid = false; } 93 94 void postvisit( const ast::ConstantExpr * expr ) { 95 value = expr->intValue(); 96 } 97 98 void postvisit( const ast::CastExpr * expr ) { 99 auto arg = eval(expr->arg); 100 valid = arg.second; 101 value = arg.first; 102 // TODO: perform type conversion on value if valid 103 } 104 105 void postvisit( const ast::VariableExpr * expr ) { 106 if ( const ast::EnumInstType * inst = dynamic_cast<const ast::EnumInstType *>(expr->result.get()) ) { 107 if ( const ast::EnumDecl * decl = inst->base ) { 108 if ( decl->valueOf( expr->var, value ) ) { // value filled by valueOf 109 return; 110 } 111 } 112 } 113 valid = false; 114 } 115 116 void postvisit( const ast::ApplicationExpr * expr ) { 117 const ast::DeclWithType * function = InitTweak::getFunction(expr); 118 if ( ! function || function->linkage != ast::Linkage::Intrinsic ) { valid = false; return; } 119 const std::string & fname = function->name; 120 assertf( expr->args.size() == 1 || expr->args.size() == 2, "Intrinsic function with %zd arguments: %s", expr->args.size(), fname.c_str() ); 121 std::pair<long long int, bool> arg1, arg2; 122 arg1 = eval(expr->args.front()); 123 valid = valid && arg1.second; 124 if ( ! valid ) return; 125 if ( expr->args.size() == 2 ) { 126 arg2 = eval(expr->args.back()); 127 valid = valid && arg2.second; 128 if ( ! valid ) return; 129 } 130 if (fname == "?+?") { 131 value = arg1.first + arg2.first; 132 } else if (fname == "?-?") { 133 value = arg1.first - arg2.first; 134 } else if (fname == "?*?") { 135 value = arg1.first * arg2.first; 136 } else if (fname == "?/?") { 137 value = arg1.first / arg2.first; 138 } else if (fname == "?%?") { 139 value = arg1.first % arg2.first; 140 } else { 141 valid = false; 142 } 143 // TODO: implement other intrinsic functions 144 } 145 }; 146 82 147 std::pair<long long int, bool> eval(Expression * expr) { 83 PassVisitor<Eval> ev; 148 PassVisitor<EvalOld> ev; 149 if (expr) { 150 expr->accept(ev); 151 return std::make_pair(ev.pass.value, ev.pass.valid); 152 } else { 153 return std::make_pair(0, false); 154 } 155 } 156 157 std::pair<long long int, bool> eval(const ast::Expr * expr) { 158 ast::Pass<EvalNew> ev; 84 159 if (expr) { 85 160 expr->accept(ev); -
src/Common/PassVisitor.impl.h
r933f32f rd908563 23 23 assert( __return ); \ 24 24 return __return; 25 26 27 #define VISIT_BODY( node ) \28 VISIT_START( node ); \29 if( children_guard ) { \30 Visitor::visit( node ); \31 } \32 VISIT_END( node ); \33 34 35 #define MUTATE_BODY( type, node ) \36 MUTATE_START( node ); \37 if( children_guard ) { \38 Mutator::mutate( node ); \39 } \40 MUTATE_END( type, node ); \41 42 25 43 26 … … 2756 2739 MUTATE_END( TypeSubstitution, node ); 2757 2740 } 2741 2742 #undef VISIT_START 2743 #undef VISIT_END 2744 2745 #undef MUTATE_START 2746 #undef MUTATE_END -
src/Common/utility.h
r933f32f rd908563 74 74 75 75 template< typename Container > 76 void deleteAll( Container &container ) {77 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i) {78 delete *i;76 void deleteAll( const Container &container ) { 77 for ( const auto &i : container ) { 78 delete i; 79 79 } // for 80 80 }
Note:
See TracChangeset
for help on using the changeset viewer.