| 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 | // Copy.cpp -- Provides functions to copy the AST. | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Andrew Beach | 
|---|
| 10 | // Created On       : Thr Nov 11  9:16:00 2019 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Tue May  3 16:28:00 2022 | 
|---|
| 13 | // Update Count     : 1 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "Copy.hpp" | 
|---|
| 17 |  | 
|---|
| 18 | #include "Decl.hpp" | 
|---|
| 19 | #include "Expr.hpp" | 
|---|
| 20 | #include "Pass.hpp" | 
|---|
| 21 | #include "Stmt.hpp" | 
|---|
| 22 | #include "Type.hpp" | 
|---|
| 23 | #include <unordered_set> | 
|---|
| 24 | #include <unordered_map> | 
|---|
| 25 |  | 
|---|
| 26 | namespace ast { | 
|---|
| 27 |  | 
|---|
| 28 | namespace { | 
|---|
| 29 |  | 
|---|
| 30 | class DeepCopyCore { | 
|---|
| 31 | std::unordered_map< const Node *, const Node * > nodeCache; | 
|---|
| 32 | std::unordered_set< readonly<Node> * > readonlyCache; | 
|---|
| 33 |  | 
|---|
| 34 | template<typename node_t> | 
|---|
| 35 | void readonlyInsert( const readonly<node_t> * ptrptr ) { | 
|---|
| 36 | readonlyCache.insert( (readonly<Node> *) ptrptr ); | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | public: | 
|---|
| 40 | template<typename node_t> | 
|---|
| 41 | const node_t * previsit( const node_t * node ) { | 
|---|
| 42 | const node_t * copy = shallowCopy( node ); | 
|---|
| 43 | nodeCache.insert( std::make_pair( node, copy ) ); | 
|---|
| 44 | return copy; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | void postvisit( const AggregateDecl * node ) { | 
|---|
| 48 | readonlyInsert( &node->parent ); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | void postvisit( const StructInstType * node ) { | 
|---|
| 52 | readonlyInsert( &node->base ); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | void postvisit( const UnionInstType * node ) { | 
|---|
| 56 | readonlyInsert( &node->base ); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | void postvisit( const EnumInstType * node ) { | 
|---|
| 60 | readonlyInsert( &node->base ); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | void postvisit( const TraitInstType * node ) { | 
|---|
| 64 | readonlyInsert( &node->base ); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void postvisit( const TypeInstType * node ) { | 
|---|
| 68 | readonlyInsert( &node->base ); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void postvisit( const ImplicitCtorDtorStmt * node ) { | 
|---|
| 72 | readonlyInsert( (const readonly<Stmt> *) &node->callStmt ); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | void postvisit( const StmtExpr * node ) { | 
|---|
| 76 | readonlyInsert( &node->resultExpr ); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void postvisit( const UniqueExpr * node ) { | 
|---|
| 80 | readonlyInsert( &node->object ); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void postvisit( const MemberExpr * node ) { | 
|---|
| 84 | readonlyInsert( &node->member ); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | void postvisit( const VariableExpr * node ) { | 
|---|
| 88 | readonlyInsert( &node->var ); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | void postvisit( const OffsetofExpr * node ) { | 
|---|
| 92 | readonlyInsert( &node->member ); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | void postvisit( const DeletedExpr * node ) { | 
|---|
| 96 | readonlyInsert( &node->deleteStmt ); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void readonlyUpdates() { | 
|---|
| 100 | for ( readonly<Node> * ptr : readonlyCache ) { | 
|---|
| 101 | auto it = nodeCache.find( ptr->get() ); | 
|---|
| 102 | if ( nodeCache.end() != it ) { | 
|---|
| 103 | *ptr = it->second; | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|
| 107 | }; | 
|---|
| 108 |  | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | template<> | 
|---|
| 112 | Node * deepCopy<Node>( const Node * localRoot ) { | 
|---|
| 113 | Pass< DeepCopyCore > dc; | 
|---|
| 114 | Node const * newRoot = localRoot->accept( dc ); | 
|---|
| 115 | dc.core.readonlyUpdates(); | 
|---|
| 116 | return const_cast< Node * >( newRoot ); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | // Local Variables: // | 
|---|
| 122 | // tab-width: 4 // | 
|---|
| 123 | // mode: c++ // | 
|---|
| 124 | // compile-command: "make install" // | 
|---|
| 125 | // End: // | 
|---|