[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 | };
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | void replace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) {
|
---|
| 43 | PassVisitor<DeclReplacer> replacer( declMap, typeMap, debug );
|
---|
| 44 | maybeAccept( node, replacer );
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | void replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) {
|
---|
| 48 | TypeMap typeMap;
|
---|
| 49 | replace( node, declMap, typeMap, debug );
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | void replace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug ) {
|
---|
| 53 | DeclMap declMap;
|
---|
| 54 | replace( node, declMap, typeMap, debug );
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | namespace {
|
---|
| 58 | DeclReplacer::DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug ) : declMap( declMap ), typeMap( typeMap ) , debug( debug ) {}
|
---|
| 59 |
|
---|
| 60 | // replace variable with new node from decl map
|
---|
| 61 | void DeclReplacer::previsit( VariableExpr * varExpr ) {
|
---|
| 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)
|
---|
| 63 | if ( declMap.count( varExpr->var ) ) {
|
---|
| 64 | auto replacement = declMap.at( varExpr->var );
|
---|
| 65 | if ( debug ) {
|
---|
| 66 | std::cerr << "replacing variable reference: " << (void*)varExpr->var << " " << varExpr->var << " with " << (void*)replacement << " " << replacement << std::endl;
|
---|
| 67 | }
|
---|
| 68 | varExpr->var = replacement;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void DeclReplacer::previsit( TypeInstType * inst ) {
|
---|
| 73 | if ( typeMap.count( inst->baseType ) ) {
|
---|
| 74 | auto replacement = typeMap.at( inst->baseType );
|
---|
| 75 | if ( debug ) {
|
---|
| 76 | std::cerr << "replacing type reference: " << (void*)inst->baseType << " " << inst->baseType << " with " << (void*)replacement << " " << replacement << std::endl;
|
---|
| 77 | }
|
---|
| 78 | inst->baseType = replacement;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | } // namespace VarExprReplacer
|
---|