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 : Thr Nov 11 9:28:00 2021
|
---|
13 | // Update Count : 0
|
---|
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 MemberExpr * node ) {
|
---|
80 | readonlyInsert( &node->member );
|
---|
81 | }
|
---|
82 |
|
---|
83 | void postvisit( const VariableExpr * node ) {
|
---|
84 | readonlyInsert( &node->var );
|
---|
85 | }
|
---|
86 |
|
---|
87 | void postvisit( const OffsetofExpr * node ) {
|
---|
88 | readonlyInsert( &node->member );
|
---|
89 | }
|
---|
90 |
|
---|
91 | void postvisit( const DeletedExpr * node ) {
|
---|
92 | readonlyInsert( &node->deleteStmt );
|
---|
93 | }
|
---|
94 |
|
---|
95 | void readonlyUpdates() {
|
---|
96 | for ( readonly<Node> * ptr : readonlyCache ) {
|
---|
97 | auto it = nodeCache.find( ptr->get() );
|
---|
98 | if ( nodeCache.end() != it ) {
|
---|
99 | *ptr = it->second;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | };
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | template<>
|
---|
108 | Node * deepCopy<Node>( const Node * localRoot ) {
|
---|
109 | Pass< DeepCopyCore > dc;
|
---|
110 | Node const * newRoot = localRoot->accept( dc );
|
---|
111 | dc.core.readonlyUpdates();
|
---|
112 | return const_cast< Node * >( newRoot );
|
---|
113 | }
|
---|
114 |
|
---|
115 | }
|
---|
116 |
|
---|
117 | // Local Variables: //
|
---|
118 | // tab-width: 4 //
|
---|
119 | // mode: c++ //
|
---|
120 | // compile-command: "make install" //
|
---|
121 | // End: //
|
---|