source: src/AST/Copy.hpp@ e068c8a

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since e068c8a was 5c9b20c, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fixed void ptr assignment bug

  • Property mode set to 100644
File size: 3.3 KB
Line 
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#pragma once
17
18#include "Decl.hpp"
19#include "Expr.hpp"
20#include "Pass.hpp"
21#include "Stmt.hpp"
22#include "Type.hpp"
23
24namespace ast {
25
26template<typename node_t>
27node_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
34template<typename node_t>
35node_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
43class DeepCopyCore {
44 std::unordered_map< const Node *, const Node * > nodeCache;
45 std::unordered_set< readonly<Node> * > readonlyCache;
46
47public:
48 template<typename node_t>
49 const node_t * previsit( const node_t * node ) {
50 const node_t * copy = shallowCopy( node );
51 nodeCache.insert( std::make_pair( node, copy ) );
52 return copy;
53 }
54
55 void postvisit( const AggregateDecl * node ) {
56 readonlyCache.insert( (readonly<Node> *) & node->parent );
57 }
58
59 void postvisit( const StructInstType * node ) {
60 readonlyCache.insert( (readonly<Node> *) & node->base );
61 }
62
63 void postvisit( const UnionInstType * node ) {
64 readonlyCache.insert( (readonly<Node> *) & node->base );
65 }
66
67 void postvisit( const EnumInstType * node ) {
68 readonlyCache.insert( (readonly<Node> *) & node->base );
69 }
70
71 void postvisit( const TraitInstType * node ) {
72 readonlyCache.insert( (readonly<Node> *) & node->base );
73 }
74
75 void postvisit( const TypeInstType * node ) {
76 readonlyCache.insert( (readonly<Node> *) & node->base );
77 }
78
79 void postvisit( const ImplicitCtorDtorStmt * node ) {
80 readonlyCache.insert( (readonly<Node> *) & node->callStmt );
81 }
82
83 void postvisit( const MemberExpr * node ) {
84 readonlyCache.insert( (readonly<Node> *) & node->member );
85 }
86
87 void postvisit( const VariableExpr * node ) {
88 readonlyCache.insert( (readonly<Node> *) & node->var );
89 }
90
91 void postvisit( const OffsetofExpr * node ) {
92 readonlyCache.insert( (readonly<Node> *) & node->member );
93 }
94
95 void postvisit( const DeletedExpr * node ) {
96 readonlyCache.insert( (readonly<Node> *) & 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
109template<typename node_t>
110node_t * shallowCopy( const node_t * localRoot ) {
111 return localRoot->clone();
112}
113
114template<typename node_t>
115node_t * deepCopy( const node_t * localRoot ) {
116 Pass< DeepCopyCore > dc;
117 node_t const * newRoot = localRoot->accept( dc );
118 dc.pass.readonlyUpdates();
119 return const_cast< node_t * >( newRoot );
120}
121
122}
123
124// Local Variables: //
125// tab-width: 4 //
126// mode: c++ //
127// compile-command: "make install" //
128// End: //
Note: See TracBrowser for help on using the repository browser.