source: src/AST/Copy.hpp @ 276f105

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 276f105 was 276f105, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Changed deep copy to highlight a questionable case. Even if it is correct it is clearer now.

  • 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"
23
24namespace ast {
25
26template<typename node_t>
[99da267]27node_t * shallowCopy( const node_t * node );
[1f1c102]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>
[99da267]35node_t * deepCopy( const node_t * localRoot );
[1f1c102]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
[276f105]47        template<typename node_t>
48        void readonlyInsert( const readonly<node_t> * ptrptr ) {
49                readonlyCache.insert( (readonly<Node> *) ptrptr );
50        }
51
[1f1c102]52public:
53        template<typename node_t>
54        const node_t * previsit( const node_t * node ) {
55                const node_t * copy = shallowCopy( node );
56                nodeCache.insert( std::make_pair( node, copy ) );
57                return copy;
58        }
59
60        void postvisit( const AggregateDecl * node ) {
[276f105]61                readonlyInsert( &node->parent );
[1f1c102]62        }
63
64        void postvisit( const StructInstType * node ) {
[276f105]65                readonlyInsert( &node->base );
[1f1c102]66        }
67
68        void postvisit( const UnionInstType * node ) {
[276f105]69                readonlyInsert( &node->base );
[1f1c102]70        }
71
72        void postvisit( const EnumInstType * node ) {
[276f105]73                readonlyInsert( &node->base );
[1f1c102]74        }
75
76        void postvisit( const TraitInstType * node ) {
[276f105]77                readonlyInsert( &node->base );
[1f1c102]78        }
79
80        void postvisit( const TypeInstType * node ) {
[276f105]81                readonlyInsert( &node->base );
[1f1c102]82        }
83
84        void postvisit( const ImplicitCtorDtorStmt * node ) {
[276f105]85                readonlyInsert( (const readonly<Stmt> *) &node->callStmt );
[1f1c102]86        }
87
88        void postvisit( const MemberExpr * node ) {
[276f105]89                readonlyInsert( &node->member );
[1f1c102]90        }
91
92        void postvisit( const VariableExpr * node ) {
[276f105]93                readonlyInsert( &node->var );
[1f1c102]94        }
95
96        void postvisit( const OffsetofExpr * node ) {
[276f105]97                readonlyInsert( &node->member );
[1f1c102]98        }
99
100        void postvisit( const DeletedExpr * node ) {
[276f105]101                readonlyInsert( &node->deleteStmt );
[1f1c102]102        }
103
104        void readonlyUpdates() {
105                for ( readonly<Node> * ptr : readonlyCache ) {
106                        auto it = nodeCache.find( ptr->get() );
107                        if ( nodeCache.end() != it ) {
108                                *ptr = it->second;
109                        }
110                }
111        }
112};
113
114template<typename node_t>
[99da267]115node_t * shallowCopy( const node_t * localRoot ) {
[1f1c102]116        return localRoot->clone();
117}
118
119template<typename node_t>
[99da267]120node_t * deepCopy( const node_t * localRoot ) {
[1f1c102]121        Pass< DeepCopyCore > dc;
122        node_t const * newRoot = localRoot->accept( dc );
123        dc.pass.readonlyUpdates();
124        return const_cast< node_t * >( newRoot );
125}
126
127}
128
129// Local Variables: //
130// tab-width: 4 //
131// mode: c++ //
132// compile-command: "make install" //
133// End: //
Note: See TracBrowser for help on using the repository browser.