source: src/SynTree/CompoundStmt.cc @ a5a71d0

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglergc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since a5a71d0 was a5a71d0, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Merge branch 'fix-memory-error' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/Makefile.in
src/Parser/DeclarationNode.cc
src/Parser/ParseNode.h
src/Parser/TypeData.cc
src/Parser/parser.cc
src/Parser/parser.yy
src/ResolvExpr/Resolver.cc
src/SymTab/Validate.cc
src/SynTree/Declaration.h
src/SynTree/Mutator.cc
src/SynTree/Mutator.h
src/SynTree/SynTree.h
src/SynTree/Visitor.cc
src/SynTree/Visitor.h
src/libcfa/prelude.cf

  • 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// 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 : Wed Apr 06 14:35:37 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 << string( indent, ' ' ) << "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.