source: src/SynTree/CompoundStmt.cc @ dae881f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since dae881f was d7903b1, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'fix-tree-printing' into ctor

Conflicts:

src/SynTree/CommaExpr.cc
src/SynTree/CompoundStmt.cc

  • Property mode set to 100644
File size: 3.1 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// XXX.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Mon May 02 15:19:17 2016
13// Update Count     : 3
14//
15
16#include "Statement.h"
17#include "Common/utility.h"
18#include <algorithm>
19#include <functional>
20#include "Expression.h"
21#include "Declaration.h"
22
23using std::string;
24using std::endl;
25
26class VarExprReplacer : public Visitor {
27public:
28  typedef std::map< DeclarationWithType *, DeclarationWithType * > DeclMap;
29private:
30  const DeclMap & declMap;
31public:
32  VarExprReplacer( const DeclMap & declMap ) : declMap( declMap ) {}
33
34  // replace variable with new node from decl map
35  virtual void visit( VariableExpr * varExpr ) {
36    if ( declMap.count( varExpr->get_var() ) ) {
37      varExpr->set_var( declMap.at( varExpr->get_var() ) );
38    }
39  }
40};
41
42
43CompoundStmt::CompoundStmt( std::list<Label> labels ) : Statement( labels ) {
44}
45
46CompoundStmt::CompoundStmt( const CompoundStmt &other ) : Statement( other ) {
47        cloneAll( other.kids, kids );
48
49  // when cloning a compound statement, we may end up cloning declarations which
50  // are referred to by VariableExprs throughout the block. Cloning a VariableExpr
51  // does a shallow copy, so the VariableExpr will end up pointing to the original
52  // declaration. If the original declaration is deleted, e.g. because the original
53  // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case,
54  // find all DeclarationWithType nodes (since a VariableExpr must point to a
55  // DeclarationWithType) in the original CompoundStmt and map them to the cloned
56  // node in the new CompoundStmt ('this'), then replace the Declarations referred to
57  // by each VariableExpr according to the constructed map. Note that only the declarations
58  // in the current level are collected into the map, because child CompoundStmts will
59  // recursively execute this routine. There may be more efficient ways of doing
60  // this.
61  VarExprReplacer::DeclMap declMap;
62  std::list< Statement * >::const_iterator origit = other.kids.begin();
63  for ( Statement * s : kids ) {
64    assert( origit != other.kids.end() );
65    if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( s ) ) {
66      DeclStmt * origDeclStmt = dynamic_cast< DeclStmt * >( *origit );
67      assert( origDeclStmt );
68      if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * > ( declStmt->get_decl() ) ) {
69        DeclarationWithType * origdwt = dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() );
70        assert( origdwt );
71        declMap[ origdwt ] = dwt;
72      }
73    }
74  }
75  if ( ! declMap.empty() ) {
76    VarExprReplacer replacer( declMap );
77    accept( replacer );
78  }
79}
80
81CompoundStmt::~CompoundStmt() {
82        deleteAll( kids );
83}
84
85void CompoundStmt::print( std::ostream &os, int indent ) const {
86        os << "CompoundStmt" << endl ;
87        printAll( kids, os, indent + 2 );
88}
89
90// Local Variables: //
91// tab-width: 4 //
92// mode: c++ //
93// compile-command: "make install" //
94// End: //
Note: See TracBrowser for help on using the repository browser.