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