| [23bb1b9] | 1 | // | 
|---|
|  | 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 6 | // | 
|---|
|  | 7 | // VarExprReplacer.h -- | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Author           : Rob Schluntz | 
|---|
|  | 10 | // Created On       : Wed Jan 13 16:29:30 2016 | 
|---|
|  | 11 | // Last Modified By : Rob Schluntz | 
|---|
|  | 12 | // Last Modified On : Fri May 13 11:27:52 2016 | 
|---|
|  | 13 | // Update Count     : 5 | 
|---|
|  | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [ea6332d] | 16 | #include <iostream>       // for operator<<, basic_ostream, ostream, basic_o... | 
|---|
|  | 17 |  | 
|---|
| [c0b9f5d] | 18 | #include "Common/PassVisitor.h" | 
|---|
| [ea6332d] | 19 | #include "Declaration.h"  // for operator<<, DeclarationWithType | 
|---|
|  | 20 | #include "Expression.h"   // for VariableExpr | 
|---|
| [23bb1b9] | 21 | #include "VarExprReplacer.h" | 
|---|
|  | 22 |  | 
|---|
| [c0b9f5d] | 23 | namespace VarExprReplacer { | 
|---|
|  | 24 | namespace { | 
|---|
|  | 25 | /// Visitor that replaces the declarations that VariableExprs refer to, according to the supplied mapping | 
|---|
|  | 26 | struct VarExprReplacer { | 
|---|
|  | 27 | private: | 
|---|
|  | 28 | const DeclMap & declMap; | 
|---|
|  | 29 | bool debug; | 
|---|
|  | 30 | public: | 
|---|
|  | 31 | VarExprReplacer( const DeclMap & declMap, bool debug = false ); | 
|---|
| [23bb1b9] | 32 |  | 
|---|
| [c0b9f5d] | 33 | // replace variable with new node from decl map | 
|---|
|  | 34 | void previsit( VariableExpr * varExpr ); | 
|---|
|  | 35 | }; | 
|---|
|  | 36 | } | 
|---|
|  | 37 |  | 
|---|
|  | 38 | void replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) { | 
|---|
|  | 39 | PassVisitor<VarExprReplacer> replacer( declMap, debug ); | 
|---|
|  | 40 | maybeAccept( node, replacer ); | 
|---|
|  | 41 | } | 
|---|
|  | 42 |  | 
|---|
|  | 43 | namespace { | 
|---|
|  | 44 | VarExprReplacer::VarExprReplacer( const DeclMap & declMap, bool debug ) : declMap( declMap ), debug( debug ) {} | 
|---|
|  | 45 |  | 
|---|
|  | 46 | // replace variable with new node from decl map | 
|---|
|  | 47 | void VarExprReplacer::previsit( VariableExpr * varExpr ) { | 
|---|
|  | 48 | // 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) | 
|---|
|  | 49 | if ( declMap.count( varExpr->var ) ) { | 
|---|
|  | 50 | if ( debug ) { | 
|---|
|  | 51 | std::cerr << "replacing variable reference: " << (void*)varExpr->var << " " << varExpr->var << " with " << (void*)declMap.at( varExpr->var ) << " " << declMap.at( varExpr->var ) << std::endl; | 
|---|
|  | 52 | } | 
|---|
|  | 53 | varExpr->var = declMap.at( varExpr->var ); | 
|---|
|  | 54 | } | 
|---|
| [62423350] | 55 | } | 
|---|
|  | 56 | } | 
|---|
| [c0b9f5d] | 57 | } // namespace VarExprReplacer | 
|---|
|  | 58 |  | 
|---|
|  | 59 |  | 
|---|
|  | 60 |  | 
|---|
|  | 61 |  | 
|---|
|  | 62 |  | 
|---|
|  | 63 |  | 
|---|
|  | 64 |  | 
|---|