source: src/AST/Copy.hpp @ 4a8f150

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 4a8f150 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
RevLine 
[1f1c102]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
[276f105]12// Last Modified On : Fri Jun 19 16:43:00 2020
13// Update Count     : 1
[1f1c102]14//
15
[5c9b20c]16#pragma once
17
[1f1c102]18#include "Decl.hpp"
19#include "Expr.hpp"
20#include "Pass.hpp"
21#include "Stmt.hpp"
22#include "Type.hpp"
[d3aa64f1]23#include <unordered_set>
24#include <unordered_map>
[1f1c102]25
26namespace ast {
27
28template<typename node_t>
[99da267]29node_t * shallowCopy( const node_t * node );
[1f1c102]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>
[99da267]37node_t * deepCopy( const node_t * localRoot );
[1f1c102]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
[276f105]49        template<typename node_t>
50        void readonlyInsert( const readonly<node_t> * ptrptr ) {
51                readonlyCache.insert( (readonly<Node> *) ptrptr );
52        }
53
[1f1c102]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 ) {
[276f105]63                readonlyInsert( &node->parent );
[1f1c102]64        }
65
66        void postvisit( const StructInstType * node ) {
[276f105]67                readonlyInsert( &node->base );
[1f1c102]68        }
69
70        void postvisit( const UnionInstType * node ) {
[276f105]71                readonlyInsert( &node->base );
[1f1c102]72        }
73
74        void postvisit( const EnumInstType * node ) {
[276f105]75                readonlyInsert( &node->base );
[1f1c102]76        }
77
78        void postvisit( const TraitInstType * node ) {
[276f105]79                readonlyInsert( &node->base );
[1f1c102]80        }
81
82        void postvisit( const TypeInstType * node ) {
[276f105]83                readonlyInsert( &node->base );
[1f1c102]84        }
85
86        void postvisit( const ImplicitCtorDtorStmt * node ) {
[276f105]87                readonlyInsert( (const readonly<Stmt> *) &node->callStmt );
[1f1c102]88        }
89
90        void postvisit( const MemberExpr * node ) {
[276f105]91                readonlyInsert( &node->member );
[1f1c102]92        }
93
94        void postvisit( const VariableExpr * node ) {
[276f105]95                readonlyInsert( &node->var );
[1f1c102]96        }
97
98        void postvisit( const OffsetofExpr * node ) {
[276f105]99                readonlyInsert( &node->member );
[1f1c102]100        }
101
102        void postvisit( const DeletedExpr * node ) {
[276f105]103                readonlyInsert( &node->deleteStmt );
[1f1c102]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>
[99da267]117node_t * shallowCopy( const node_t * localRoot ) {
[1f1c102]118        return localRoot->clone();
119}
120
121template<typename node_t>
[99da267]122node_t * deepCopy( const node_t * localRoot ) {
[1f1c102]123        Pass< DeepCopyCore > dc;
124        node_t const * newRoot = localRoot->accept( dc );
[7ff3e522]125        dc.core.readonlyUpdates();
[1f1c102]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.