[7862059] | 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;
|
---|
[0ce063b] | 31 | public:
|
---|
| 32 | size_t replaced;
|
---|
| 33 |
|
---|
[7862059] | 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 | };
|
---|
[90152a4] | 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;
|
---|
[0ce063b] | 49 | public:
|
---|
| 50 | size_t replaced;
|
---|
| 51 |
|
---|
[90152a4] | 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 | };
|
---|
[7862059] | 58 | }
|
---|
| 59 |
|
---|
[0ce063b] | 60 | size_t replace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) {
|
---|
[7862059] | 61 | PassVisitor<DeclReplacer> replacer( declMap, typeMap, debug );
|
---|
| 62 | maybeAccept( node, replacer );
|
---|
[0ce063b] | 63 | return replacer.pass.replaced;
|
---|
[7862059] | 64 | }
|
---|
| 65 |
|
---|
[0ce063b] | 66 | size_t replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) {
|
---|
[7862059] | 67 | TypeMap typeMap;
|
---|
[0ce063b] | 68 | return replace( node, declMap, typeMap, debug );
|
---|
[7862059] | 69 | }
|
---|
| 70 |
|
---|
[0ce063b] | 71 | size_t replace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug ) {
|
---|
[7862059] | 72 | DeclMap declMap;
|
---|
[0ce063b] | 73 | return replace( node, declMap, typeMap, debug );
|
---|
[7862059] | 74 | }
|
---|
| 75 |
|
---|
[0ce063b] | 76 | size_t replace( BaseSyntaxNode *& node, const ExprMap & exprMap, bool debug ) {
|
---|
[90152a4] | 77 | PassVisitor<ExprDeclReplacer> replacer( exprMap, debug );
|
---|
| 78 | node = maybeMutate( node, replacer );
|
---|
[0ce063b] | 79 | return replacer.pass.replaced;
|
---|
[90152a4] | 80 | }
|
---|
| 81 |
|
---|
[7862059] | 82 | namespace {
|
---|
[0ce063b] | 83 | DeclReplacer::DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug ) : declMap( declMap ), typeMap( typeMap ) , debug( debug ), replaced( 0 ) {}
|
---|
[7862059] | 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 ) ) {
|
---|
[0ce063b] | 89 | replaced++;
|
---|
[7862059] | 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 ) ) {
|
---|
[0ce063b] | 100 | replaced++;
|
---|
[7862059] | 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 | }
|
---|
[90152a4] | 108 |
|
---|
[0ce063b] | 109 | ExprDeclReplacer::ExprDeclReplacer( const ExprMap & exprMap, bool debug ) : exprMap( exprMap ), debug( debug ), replaced( 0 ) {}
|
---|
[90152a4] | 110 |
|
---|
| 111 | Expression * ExprDeclReplacer::postmutate( VariableExpr * varExpr ) {
|
---|
| 112 | if ( exprMap.count( varExpr->var ) ) {
|
---|
[0ce063b] | 113 | replaced++;
|
---|
[90152a4] | 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 | }
|
---|
[7862059] | 124 | }
|
---|
| 125 | } // namespace VarExprReplacer
|
---|