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