source: src/AST/Copy.hpp @ 468c5aa

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 468c5aa was d3aa64f1, checked in by Fangren Yu <f37yu@…>, 4 years ago

pure visitor interface for new ast

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