| 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 |  | 
|---|
| 16 | #include <iostream>       // for operator<<, basic_ostream, ostream, basic_o... | 
|---|
| 17 |  | 
|---|
| 18 | #include "Common/PassVisitor.h" | 
|---|
| 19 | #include "Declaration.h"  // for operator<<, DeclarationWithType | 
|---|
| 20 | #include "Expression.h"   // for VariableExpr | 
|---|
| 21 | #include "DeclReplacer.h" | 
|---|
| 22 |  | 
|---|
| 23 | namespace DeclReplacer { | 
|---|
| 24 | namespace { | 
|---|
| 25 | /// Visitor that replaces the declarations that VariableExprs refer to, according to the supplied mapping | 
|---|
| 26 | struct DeclReplacer { | 
|---|
| 27 | private: | 
|---|
| 28 | const DeclMap & declMap; | 
|---|
| 29 | const TypeMap & typeMap; | 
|---|
| 30 | bool debug; | 
|---|
| 31 | public: | 
|---|
| 32 | size_t replaced; | 
|---|
| 33 |  | 
|---|
| 34 | public: | 
|---|
| 35 | DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug = false ); | 
|---|
| 36 |  | 
|---|
| 37 | // replace variable with new node from decl map | 
|---|
| 38 | void previsit( VariableExpr * varExpr ); | 
|---|
| 39 |  | 
|---|
| 40 | // replace type inst with new node from type map | 
|---|
| 41 | void previsit( TypeInstType * inst ); | 
|---|
| 42 | }; | 
|---|
| 43 |  | 
|---|
| 44 | /// Mutator that replaces uses of declarations with arbitrary expressions, according to the supplied mapping | 
|---|
| 45 | 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 map | 
|---|
| 56 | Expression * postmutate( VariableExpr * varExpr ); | 
|---|
| 57 | }; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | size_t replace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) { | 
|---|
| 61 | PassVisitor<DeclReplacer> replacer( declMap, typeMap, debug ); | 
|---|
| 62 | maybeAccept( node, replacer ); | 
|---|
| 63 | return replacer.pass.replaced; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | size_t replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) { | 
|---|
| 67 | TypeMap typeMap; | 
|---|
| 68 | return replace( node, declMap, typeMap, debug ); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | size_t replace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug ) { | 
|---|
| 72 | 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; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | namespace { | 
|---|
| 83 | DeclReplacer::DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug ) : declMap( declMap ), typeMap( typeMap ) , debug( debug ), replaced( 0 ) {} | 
|---|
| 84 |  | 
|---|
| 85 | // replace variable with new node from decl map | 
|---|
| 86 | void DeclReplacer::previsit( VariableExpr * varExpr ) { | 
|---|
| 87 | // 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 | if ( declMap.count( varExpr->var ) ) { | 
|---|
| 89 | replaced++; | 
|---|
| 90 | auto replacement = declMap.at( varExpr->var ); | 
|---|
| 91 | if ( debug ) { | 
|---|
| 92 | std::cerr << "replacing variable reference: " << (void*)varExpr->var << " " << varExpr->var << " with " << (void*)replacement << " " << replacement << std::endl; | 
|---|
| 93 | } | 
|---|
| 94 | varExpr->var = replacement; | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void DeclReplacer::previsit( TypeInstType * inst ) { | 
|---|
| 99 | if ( typeMap.count( inst->baseType ) ) { | 
|---|
| 100 | replaced++; | 
|---|
| 101 | auto replacement = typeMap.at( inst->baseType ); | 
|---|
| 102 | if ( debug ) { | 
|---|
| 103 | std::cerr << "replacing type reference: " << (void*)inst->baseType << " " << inst->baseType << " with " << (void*)replacement << " " << replacement << std::endl; | 
|---|
| 104 | } | 
|---|
| 105 | inst->baseType = replacement; | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 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 | } | 
|---|
| 125 | } // namespace VarExprReplacer | 
|---|