[2bb4a01] | 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 | // DeclReplacer.cpp --
|
---|
| 8 | //
|
---|
| 9 | // Author : Aaron B. Moss
|
---|
| 10 | // Created On : Wed May 8 13:00:00 2019
|
---|
[747d0fa] | 11 | // Last Modified By : Andrew Beach
|
---|
| 12 | // Last Modified On : Thr Sep 15 11:55:00 2022
|
---|
| 13 | // Update Count : 2
|
---|
[2bb4a01] | 14 | //
|
---|
| 15 |
|
---|
[e9b44489] | 16 | #include "DeclReplacer.hpp"
|
---|
| 17 |
|
---|
[747d0fa] | 18 | #include "Expr.hpp"
|
---|
[e9b44489] | 19 | #include "Pass.hpp"
|
---|
[747d0fa] | 20 | #include "Type.hpp"
|
---|
[e9b44489] | 21 |
|
---|
| 22 | namespace ast {
|
---|
| 23 |
|
---|
| 24 | namespace DeclReplacer {
|
---|
| 25 |
|
---|
[747d0fa] | 26 | namespace {
|
---|
| 27 | struct DeclReplacer {
|
---|
| 28 | private:
|
---|
| 29 | const DeclMap & declMap;
|
---|
| 30 | const TypeMap & typeMap;
|
---|
| 31 | bool debug;
|
---|
| 32 |
|
---|
| 33 | public:
|
---|
| 34 | DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug )
|
---|
| 35 | : declMap( declMap ), typeMap( typeMap ), debug( debug )
|
---|
| 36 | {}
|
---|
| 37 |
|
---|
| 38 | const ast::VariableExpr * previsit( const ast::VariableExpr * );
|
---|
| 39 | const ast::TypeInstType * previsit( const ast::TypeInstType * );
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | struct VarExprReplacer {
|
---|
| 43 | private:
|
---|
| 44 | const ExprMap & exprMap;
|
---|
| 45 |
|
---|
| 46 | public:
|
---|
| 47 | VarExprReplacer( const ExprMap & exprMap ) : exprMap( exprMap ) {}
|
---|
| 48 |
|
---|
| 49 | const Expr * postvisit( const VariableExpr * );
|
---|
| 50 | };
|
---|
| 51 | } // namespace
|
---|
| 52 |
|
---|
| 53 | const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) {
|
---|
| 54 | if(!node) return nullptr;
|
---|
| 55 | Pass<DeclReplacer> replacer = { declMap, typeMap, debug };
|
---|
| 56 | return node->accept( replacer );
|
---|
| 57 | }
|
---|
[e9b44489] | 58 |
|
---|
[747d0fa] | 59 | const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, bool debug ) {
|
---|
| 60 | TypeMap typeMap;
|
---|
| 61 | return replace( node, declMap, typeMap, debug );
|
---|
| 62 | }
|
---|
[e9b44489] | 63 |
|
---|
[747d0fa] | 64 | const ast::Node * replace( const ast::Node * node, const TypeMap & typeMap, bool debug ) {
|
---|
| 65 | DeclMap declMap;
|
---|
| 66 | return replace( node, declMap, typeMap, debug );
|
---|
| 67 | }
|
---|
[490fb92e] | 68 |
|
---|
[747d0fa] | 69 | const ast::Node * replace( const ast::Node * node, const ExprMap & exprMap ) {
|
---|
| 70 | Pass<VarExprReplacer> replacer = {exprMap};
|
---|
| 71 | return node->accept( replacer );
|
---|
| 72 | }
|
---|
[e9b44489] | 73 |
|
---|
[747d0fa] | 74 | namespace {
|
---|
| 75 | // replace variable with new node from decl map
|
---|
| 76 | const ast::VariableExpr * DeclReplacer::previsit( const VariableExpr * varExpr ) {
|
---|
| 77 | // 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)
|
---|
| 78 | if ( !declMap.count( varExpr->var ) ) return varExpr;
|
---|
| 79 |
|
---|
| 80 | auto replacement = declMap.at( varExpr->var );
|
---|
| 81 | if ( debug ) {
|
---|
| 82 | std::cerr << "replacing variable reference: "
|
---|
| 83 | << (void*)varExpr->var.get() << " " << varExpr->var
|
---|
| 84 | << " with " << (void*)replacement << " " << replacement
|
---|
| 85 | << std::endl;
|
---|
[e9b44489] | 86 | }
|
---|
[747d0fa] | 87 | auto nexpr = mutate(varExpr);
|
---|
| 88 | nexpr->var = replacement;
|
---|
| 89 | return nexpr;
|
---|
| 90 | }
|
---|
[490fb92e] | 91 |
|
---|
[747d0fa] | 92 | const TypeInstType * DeclReplacer::previsit( const TypeInstType * inst ) {
|
---|
| 93 | if ( !typeMap.count( inst->base ) ) return inst;
|
---|
[490fb92e] | 94 |
|
---|
[747d0fa] | 95 | auto replacement = typeMap.at( inst->base );
|
---|
| 96 | if ( debug ) {
|
---|
| 97 | std::cerr << "replacing type reference: "
|
---|
| 98 | << (void*)inst->base.get() << " " << inst->base
|
---|
| 99 | << " with " << (void*)replacement << " " << replacement
|
---|
| 100 | << std::endl;
|
---|
[490fb92e] | 101 | }
|
---|
[747d0fa] | 102 | auto ninst = mutate(inst);
|
---|
| 103 | ninst->base = replacement;
|
---|
| 104 | return ninst;
|
---|
| 105 | }
|
---|
[490fb92e] | 106 |
|
---|
[747d0fa] | 107 | const Expr * VarExprReplacer::postvisit( const VariableExpr * expr ) {
|
---|
| 108 | if ( !exprMap.count( expr->var ) ) return expr;
|
---|
| 109 | return exprMap.at( expr->var );
|
---|
[e9b44489] | 110 | }
|
---|
[747d0fa] | 111 | } // namespace
|
---|
[e9b44489] | 112 |
|
---|
[747d0fa] | 113 | } // namespace DeclReplacer
|
---|
| 114 |
|
---|
| 115 | } // namespace ast
|
---|
[2bb4a01] | 116 |
|
---|
| 117 | // Local Variables: //
|
---|
| 118 | // tab-width: 4 //
|
---|
| 119 | // mode: c++ //
|
---|
| 120 | // compile-command: "make install" //
|
---|
| 121 | // End: //
|
---|