source: src/AST/Copy.hpp@ 7030dab

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 7030dab was 99da267, checked in by Michael Brooks <mlbrooks@…>, 6 years ago

Running a deep-copy on FunctionType at RenameVars time. This manual action addresses the currently-problematic occurrence of 'the transitivity problem.' Resolution of bootloader (and thus builtins) is now completing. The change on RenameVars.cc makes this happen. Changes on AST/*.hpp finish making the deep-copy framework compile.

  • 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#include "Decl.hpp"
17#include "Expr.hpp"
18#include "Pass.hpp"
19#include "Stmt.hpp"
20#include "Type.hpp"
21
22namespace ast {
23
24template<typename node_t>
25node_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
32template<typename node_t>
33node_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
41class DeepCopyCore {
42 std::unordered_map< const Node *, const Node * > nodeCache;
43 std::unordered_set< readonly<Node> * > readonlyCache;
44
45public:
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
107template<typename node_t>
108node_t * shallowCopy( const node_t * localRoot ) {
109 return localRoot->clone();
110}
111
112template<typename node_t>
113node_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: //
Note: See TracBrowser for help on using the repository browser.