[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;
|
---|
| 31 | public:
|
---|
| 32 | DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug = false );
|
---|
| 33 |
|
---|
| 34 | // replace variable with new node from decl map
|
---|
| 35 | void previsit( VariableExpr * varExpr );
|
---|
| 36 |
|
---|
| 37 | // replace type inst with new node from type map
|
---|
| 38 | void previsit( TypeInstType * inst );
|
---|
| 39 | };
|
---|
[90152a4] | 40 |
|
---|
| 41 | /// Mutator that replaces uses of declarations with arbitrary expressions, according to the supplied mapping
|
---|
| 42 | struct ExprDeclReplacer {
|
---|
| 43 | private:
|
---|
| 44 | const ExprMap & exprMap;
|
---|
| 45 | bool debug;
|
---|
| 46 | public:
|
---|
| 47 | ExprDeclReplacer( const ExprMap & exprMap, bool debug = false );
|
---|
| 48 |
|
---|
| 49 | // replace variable with new node from expr map
|
---|
| 50 | Expression * postmutate( VariableExpr * varExpr );
|
---|
| 51 | };
|
---|
[7862059] | 52 | }
|
---|
| 53 |
|
---|
| 54 | void replace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) {
|
---|
| 55 | PassVisitor<DeclReplacer> replacer( declMap, typeMap, debug );
|
---|
| 56 | maybeAccept( node, replacer );
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | void replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) {
|
---|
| 60 | TypeMap typeMap;
|
---|
| 61 | replace( node, declMap, typeMap, debug );
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | void replace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug ) {
|
---|
| 65 | DeclMap declMap;
|
---|
| 66 | replace( node, declMap, typeMap, debug );
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[90152a4] | 69 | void replace( BaseSyntaxNode *& node, const ExprMap & exprMap, bool debug ) {
|
---|
| 70 | PassVisitor<ExprDeclReplacer> replacer( exprMap, debug );
|
---|
| 71 | node = maybeMutate( node, replacer );
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[7862059] | 74 | namespace {
|
---|
| 75 | DeclReplacer::DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug ) : declMap( declMap ), typeMap( typeMap ) , debug( debug ) {}
|
---|
| 76 |
|
---|
| 77 | // replace variable with new node from decl map
|
---|
| 78 | void DeclReplacer::previsit( VariableExpr * varExpr ) {
|
---|
| 79 | // 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)
|
---|
| 80 | if ( declMap.count( varExpr->var ) ) {
|
---|
| 81 | auto replacement = declMap.at( varExpr->var );
|
---|
| 82 | if ( debug ) {
|
---|
| 83 | std::cerr << "replacing variable reference: " << (void*)varExpr->var << " " << varExpr->var << " with " << (void*)replacement << " " << replacement << std::endl;
|
---|
| 84 | }
|
---|
| 85 | varExpr->var = replacement;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void DeclReplacer::previsit( TypeInstType * inst ) {
|
---|
| 90 | if ( typeMap.count( inst->baseType ) ) {
|
---|
| 91 | auto replacement = typeMap.at( inst->baseType );
|
---|
| 92 | if ( debug ) {
|
---|
| 93 | std::cerr << "replacing type reference: " << (void*)inst->baseType << " " << inst->baseType << " with " << (void*)replacement << " " << replacement << std::endl;
|
---|
| 94 | }
|
---|
| 95 | inst->baseType = replacement;
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
[90152a4] | 98 |
|
---|
| 99 | ExprDeclReplacer::ExprDeclReplacer( const ExprMap & exprMap, bool debug ) : exprMap( exprMap ), debug( debug ) {}
|
---|
| 100 |
|
---|
| 101 | Expression * ExprDeclReplacer::postmutate( VariableExpr * varExpr ) {
|
---|
| 102 | if ( exprMap.count( varExpr->var ) ) {
|
---|
| 103 | Expression * replacement = exprMap.at( varExpr->var )->clone();
|
---|
| 104 | if ( debug ) {
|
---|
| 105 | std::cerr << "replacing variable reference: " << (void*)varExpr->var << " " << varExpr->var << " with " << (void*)replacement << " " << replacement << std::endl;
|
---|
| 106 | }
|
---|
| 107 | std::swap( varExpr->env, replacement->env );
|
---|
| 108 | delete varExpr;
|
---|
| 109 | return replacement;
|
---|
| 110 | }
|
---|
| 111 | return varExpr;
|
---|
| 112 | }
|
---|
[7862059] | 113 | }
|
---|
| 114 | } // namespace VarExprReplacer
|
---|