Changes in src/SynTree/DeclReplacer.cc [0ce063b:7862059]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/DeclReplacer.cc
r0ce063b r7862059 30 30 bool debug; 31 31 public: 32 size_t replaced;33 34 public:35 32 DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug = false ); 36 33 … … 41 38 void previsit( TypeInstType * inst ); 42 39 }; 43 44 /// Mutator that replaces uses of declarations with arbitrary expressions, according to the supplied mapping45 struct ExprDeclReplacer {46 private:47 const ExprMap & exprMap;48 bool debug;49 public:50 size_t replaced;51 52 public:53 ExprDeclReplacer( const ExprMap & exprMap, bool debug = false );54 55 // replace variable with new node from expr map56 Expression * postmutate( VariableExpr * varExpr );57 };58 40 } 59 41 60 size_treplace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) {42 void replace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) { 61 43 PassVisitor<DeclReplacer> replacer( declMap, typeMap, debug ); 62 44 maybeAccept( node, replacer ); 63 return replacer.pass.replaced;64 45 } 65 46 66 size_treplace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) {47 void replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) { 67 48 TypeMap typeMap; 68 re turn replace( node, declMap, typeMap, debug );49 replace( node, declMap, typeMap, debug ); 69 50 } 70 51 71 size_treplace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug ) {52 void replace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug ) { 72 53 DeclMap declMap; 73 return replace( node, declMap, typeMap, debug ); 74 } 75 76 size_t replace( BaseSyntaxNode *& node, const ExprMap & exprMap, bool debug ) { 77 PassVisitor<ExprDeclReplacer> replacer( exprMap, debug ); 78 node = maybeMutate( node, replacer ); 79 return replacer.pass.replaced; 54 replace( node, declMap, typeMap, debug ); 80 55 } 81 56 82 57 namespace { 83 DeclReplacer::DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug ) : declMap( declMap ), typeMap( typeMap ) , debug( debug ) , replaced( 0 ){}58 DeclReplacer::DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug ) : declMap( declMap ), typeMap( typeMap ) , debug( debug ) {} 84 59 85 60 // replace variable with new node from decl map … … 87 62 // 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) 88 63 if ( declMap.count( varExpr->var ) ) { 89 replaced++;90 64 auto replacement = declMap.at( varExpr->var ); 91 65 if ( debug ) { … … 98 72 void DeclReplacer::previsit( TypeInstType * inst ) { 99 73 if ( typeMap.count( inst->baseType ) ) { 100 replaced++;101 74 auto replacement = typeMap.at( inst->baseType ); 102 75 if ( debug ) { … … 106 79 } 107 80 } 108 109 ExprDeclReplacer::ExprDeclReplacer( const ExprMap & exprMap, bool debug ) : exprMap( exprMap ), debug( debug ), replaced( 0 ) {}110 111 Expression * ExprDeclReplacer::postmutate( VariableExpr * varExpr ) {112 if ( exprMap.count( varExpr->var ) ) {113 replaced++;114 Expression * replacement = exprMap.at( varExpr->var )->clone();115 if ( debug ) {116 std::cerr << "replacing variable reference: " << (void*)varExpr->var << " " << varExpr->var << " with " << (void*)replacement << " " << replacement << std::endl;117 }118 std::swap( varExpr->env, replacement->env );119 delete varExpr;120 return replacement;121 }122 return varExpr;123 }124 81 } 125 82 } // namespace VarExprReplacer
Note: See TracChangeset
for help on using the changeset viewer.