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.hpp -- Provides functions to copy the AST. |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Wed Jul 10 16:13:00 2019 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Fri Jun 19 16:43:00 2020 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include "Decl.hpp" |
---|
19 | #include "Expr.hpp" |
---|
20 | #include "Pass.hpp" |
---|
21 | #include "Stmt.hpp" |
---|
22 | #include "Type.hpp" |
---|
23 | |
---|
24 | namespace ast { |
---|
25 | |
---|
26 | template<typename node_t> |
---|
27 | node_t * shallowCopy( const node_t * node ); |
---|
28 | /* Create a shallow copy of the node given. |
---|
29 | * |
---|
30 | * The new node has all the same primitive field values and points to the |
---|
31 | * same children nodes as the parent. |
---|
32 | */ |
---|
33 | |
---|
34 | template<typename node_t> |
---|
35 | node_t * deepCopy( const node_t * localRoot ); |
---|
36 | /* Create a deep copy of the tree rooted at localRoot. |
---|
37 | * |
---|
38 | * This creates a copy of every node in the sub-tree (reachable by strong |
---|
39 | * reference from local_root) and updates any readonly pointers on those nodes |
---|
40 | * that point to another node in the sub-tree to the new version of that node. |
---|
41 | */ |
---|
42 | |
---|
43 | class DeepCopyCore { |
---|
44 | std::unordered_map< const Node *, const Node * > nodeCache; |
---|
45 | std::unordered_set< readonly<Node> * > readonlyCache; |
---|
46 | |
---|
47 | template<typename node_t> |
---|
48 | void readonlyInsert( const readonly<node_t> * ptrptr ) { |
---|
49 | readonlyCache.insert( (readonly<Node> *) ptrptr ); |
---|
50 | } |
---|
51 | |
---|
52 | public: |
---|
53 | template<typename node_t> |
---|
54 | const node_t * previsit( const node_t * node ) { |
---|
55 | const node_t * copy = shallowCopy( node ); |
---|
56 | nodeCache.insert( std::make_pair( node, copy ) ); |
---|
57 | return copy; |
---|
58 | } |
---|
59 | |
---|
60 | void postvisit( const AggregateDecl * node ) { |
---|
61 | readonlyInsert( &node->parent ); |
---|
62 | } |
---|
63 | |
---|
64 | void postvisit( const StructInstType * node ) { |
---|
65 | readonlyInsert( &node->base ); |
---|
66 | } |
---|
67 | |
---|
68 | void postvisit( const UnionInstType * node ) { |
---|
69 | readonlyInsert( &node->base ); |
---|
70 | } |
---|
71 | |
---|
72 | void postvisit( const EnumInstType * node ) { |
---|
73 | readonlyInsert( &node->base ); |
---|
74 | } |
---|
75 | |
---|
76 | void postvisit( const TraitInstType * node ) { |
---|
77 | readonlyInsert( &node->base ); |
---|
78 | } |
---|
79 | |
---|
80 | void postvisit( const TypeInstType * node ) { |
---|
81 | readonlyInsert( &node->base ); |
---|
82 | } |
---|
83 | |
---|
84 | void postvisit( const ImplicitCtorDtorStmt * node ) { |
---|
85 | readonlyInsert( (const readonly<Stmt> *) &node->callStmt ); |
---|
86 | } |
---|
87 | |
---|
88 | void postvisit( const MemberExpr * node ) { |
---|
89 | readonlyInsert( &node->member ); |
---|
90 | } |
---|
91 | |
---|
92 | void postvisit( const VariableExpr * node ) { |
---|
93 | readonlyInsert( &node->var ); |
---|
94 | } |
---|
95 | |
---|
96 | void postvisit( const OffsetofExpr * node ) { |
---|
97 | readonlyInsert( &node->member ); |
---|
98 | } |
---|
99 | |
---|
100 | void postvisit( const DeletedExpr * node ) { |
---|
101 | readonlyInsert( &node->deleteStmt ); |
---|
102 | } |
---|
103 | |
---|
104 | void readonlyUpdates() { |
---|
105 | for ( readonly<Node> * ptr : readonlyCache ) { |
---|
106 | auto it = nodeCache.find( ptr->get() ); |
---|
107 | if ( nodeCache.end() != it ) { |
---|
108 | *ptr = it->second; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | }; |
---|
113 | |
---|
114 | template<typename node_t> |
---|
115 | node_t * shallowCopy( const node_t * localRoot ) { |
---|
116 | return localRoot->clone(); |
---|
117 | } |
---|
118 | |
---|
119 | template<typename node_t> |
---|
120 | node_t * deepCopy( const node_t * localRoot ) { |
---|
121 | Pass< DeepCopyCore > dc; |
---|
122 | node_t const * newRoot = localRoot->accept( dc ); |
---|
123 | dc.core.readonlyUpdates(); |
---|
124 | return const_cast< node_t * >( newRoot ); |
---|
125 | } |
---|
126 | |
---|
127 | } |
---|
128 | |
---|
129 | // Local Variables: // |
---|
130 | // tab-width: 4 // |
---|
131 | // mode: c++ // |
---|
132 | // compile-command: "make install" // |
---|
133 | // End: // |
---|