Changes in src/AST/DeclReplacer.cpp [747d0fa:490fb92e]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/DeclReplacer.cpp
r747d0fa r490fb92e 9 9 // Author : Aaron B. Moss 10 10 // Created On : Wed May 8 13:00:00 2019 11 // Last Modified By : A ndrew Beach12 // Last Modified On : Thr Sep 15 11:55:00 202213 // Update Count : 211 // Last Modified By : Aaron B. Moss 12 // Last Modified On : Wed May 8 13:00:00 2019 13 // Update Count : 1 14 14 // 15 15 16 16 #include "DeclReplacer.hpp" 17 #include "Expr.hpp" 18 #include "Type.hpp" 17 19 18 #include "Expr.hpp"19 20 #include "Pass.hpp" 20 #include "Type.hpp"21 21 22 22 namespace ast { 23 23 24 24 namespace DeclReplacer { 25 namespace { 26 struct DeclReplacer { 27 private: 28 const DeclMap & declMap; 29 const TypeMap & typeMap; 30 bool debug; 25 31 26 namespace { 27 struct DeclReplacer { 28 private: 29 const DeclMap & declMap; 30 const TypeMap & typeMap; 31 bool debug; 32 public: 33 DeclReplacer(const DeclMap & declMap, const TypeMap & typeMap, bool debug) 34 : declMap( declMap ), typeMap( typeMap ), debug( debug ) 35 {} 32 36 33 public: 34 DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug ) 35 : declMap( declMap ), typeMap( typeMap ), debug( debug ) 36 {} 37 const ast::VariableExpr * previsit( const ast::VariableExpr * ); 38 const ast::TypeInstType * previsit( const ast::TypeInstType * ); 39 }; 37 40 38 const ast::VariableExpr * previsit( const ast::VariableExpr * ); 39 const ast::TypeInstType * previsit( const ast::TypeInstType * ); 40 }; 41 struct VarExprReplacer { 42 private: 43 const ExprMap & exprMap; 44 45 public: 46 VarExprReplacer(const ExprMap & exprMap): exprMap (exprMap) {} 41 47 42 struct VarExprReplacer {43 private:44 const ExprMap & exprMap;48 const Expr * postvisit (const VariableExpr *); 49 }; 50 } 45 51 46 public: 47 VarExprReplacer( const ExprMap & exprMap ) : exprMap( exprMap ) {} 52 const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) { 53 if(!node) return nullptr; 54 Pass<DeclReplacer> replacer = { declMap, typeMap, debug }; 55 return node->accept( replacer ); 56 } 48 57 49 const Expr * postvisit( const VariableExpr * ); 50 }; 51 } // namespace 58 const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, bool debug ) { 59 TypeMap typeMap; 60 return replace( node, declMap, typeMap, debug ); 61 } 52 62 53 const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) { 54 if(!node) return nullptr; 55 Pass<DeclReplacer> replacer = { declMap, typeMap, debug }; 56 return node->accept( replacer ); 63 const ast::Node * replace( const ast::Node * node, const TypeMap & typeMap, bool debug ) { 64 DeclMap declMap; 65 return replace( node, declMap, typeMap, debug ); 66 } 67 68 const ast::Node * replace( const ast::Node * node, const ExprMap & exprMap) { 69 Pass<VarExprReplacer> replacer = {exprMap}; 70 return node->accept( replacer ); 71 } 72 73 namespace { 74 // replace variable with new node from decl map 75 const ast::VariableExpr * DeclReplacer::previsit( const VariableExpr * varExpr ) { 76 // xxx - assertions and parameters aren't accounted for in this... (i.e. they aren't inserted into the map when it's made, only DeclStmts are) 77 if ( !declMap.count( varExpr->var ) ) return varExpr; 78 79 auto replacement = declMap.at( varExpr->var ); 80 if ( debug ) { 81 std::cerr << "replacing variable reference: " 82 << (void*)varExpr->var.get() << " " << varExpr->var 83 << " with " << (void*)replacement << " " << replacement 84 << std::endl; 85 } 86 auto nexpr = mutate(varExpr); 87 nexpr->var = replacement; 88 return nexpr; 89 } 90 91 const TypeInstType * DeclReplacer::previsit( const TypeInstType * inst ) { 92 if ( !typeMap.count( inst->base ) ) return inst; 93 94 auto replacement = typeMap.at( inst->base ); 95 if ( debug ) { 96 std::cerr << "replacing type reference: " 97 << (void*)inst->base.get() << " " << inst->base 98 << " with " << (void*)replacement << " " << replacement 99 << std::endl; 100 } 101 auto ninst = mutate(inst); 102 ninst->base = replacement; 103 return ninst; 104 } 105 106 const Expr * VarExprReplacer::postvisit( const VariableExpr * expr ) { 107 if (!exprMap.count(expr->var)) return expr; 108 109 return exprMap.at(expr->var); 110 } 111 112 } 57 113 } 58 114 59 const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, bool debug ) {60 TypeMap typeMap;61 return replace( node, declMap, typeMap, debug );62 115 } 63 64 const ast::Node * replace( const ast::Node * node, const TypeMap & typeMap, bool debug ) {65 DeclMap declMap;66 return replace( node, declMap, typeMap, debug );67 }68 69 const ast::Node * replace( const ast::Node * node, const ExprMap & exprMap ) {70 Pass<VarExprReplacer> replacer = {exprMap};71 return node->accept( replacer );72 }73 74 namespace {75 // replace variable with new node from decl map76 const ast::VariableExpr * DeclReplacer::previsit( const VariableExpr * varExpr ) {77 // xxx - assertions and parameters aren't accounted for in this... (i.e. they aren't inserted into the map when it's made, only DeclStmts are)78 if ( !declMap.count( varExpr->var ) ) return varExpr;79 80 auto replacement = declMap.at( varExpr->var );81 if ( debug ) {82 std::cerr << "replacing variable reference: "83 << (void*)varExpr->var.get() << " " << varExpr->var84 << " with " << (void*)replacement << " " << replacement85 << std::endl;86 }87 auto nexpr = mutate(varExpr);88 nexpr->var = replacement;89 return nexpr;90 }91 92 const TypeInstType * DeclReplacer::previsit( const TypeInstType * inst ) {93 if ( !typeMap.count( inst->base ) ) return inst;94 95 auto replacement = typeMap.at( inst->base );96 if ( debug ) {97 std::cerr << "replacing type reference: "98 << (void*)inst->base.get() << " " << inst->base99 << " with " << (void*)replacement << " " << replacement100 << std::endl;101 }102 auto ninst = mutate(inst);103 ninst->base = replacement;104 return ninst;105 }106 107 const Expr * VarExprReplacer::postvisit( const VariableExpr * expr ) {108 if ( !exprMap.count( expr->var ) ) return expr;109 return exprMap.at( expr->var );110 }111 } // namespace112 113 } // namespace DeclReplacer114 115 } // namespace ast116 116 117 117 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.