source: src/SynTree/CompoundStmt.cc @ 6ece306

ADT
Last change on this file since 6ece306 was 7862059, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Change VarExprReplacer? to DeclReplacer? and implement TypeInstType? replacement

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[0dd3a2f]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//
[a5a71d0]7// XXX.cc --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[a5a71d0]11// Last Modified By : Rob Schluntz
[d7903b1]12// Last Modified On : Mon May 02 15:19:17 2016
[de62360d]13// Update Count     : 3
[0dd3a2f]14//
[51b7345]15
[e3e16bc]16#include <cassert>                    // for assert, strict_dynamic_cast
[ea6332d]17#include <list>                       // for list, _List_const_iterator, lis...
18#include <ostream>                    // for operator<<, ostream, basic_ostream
19#include <string>                     // for operator==, string
20
21#include "Common/utility.h"           // for cloneAll, deleteAll, printAll
22#include "Declaration.h"              // for DeclarationWithType, Declaration
23#include "Statement.h"                // for CompoundStmt, Statement, DeclStmt
24#include "SynTree/Label.h"            // for Label
[7862059]25#include "SynTree/DeclReplacer.h"     // for DeclReplacer
[51b7345]26
[c11e31c]27using std::string;
28using std::endl;
[51b7345]29
[ba3706f]30CompoundStmt::CompoundStmt() : Statement() {
[51b7345]31}
32
[ba3706f]33CompoundStmt::CompoundStmt( std::list<Statement *> stmts ) : Statement(), kids( stmts ) {
[a2dbad10]34}
35
[4bf5298]36CompoundStmt::CompoundStmt( const CompoundStmt &other ) : Statement( other ) {
[0dd3a2f]37        cloneAll( other.kids, kids );
[a5a71d0]38
[23bb1b9]39        // when cloning a compound statement, we may end up cloning declarations which
40        // are referred to by VariableExprs throughout the block. Cloning a VariableExpr
41        // does a shallow copy, so the VariableExpr will end up pointing to the original
42        // declaration. If the original declaration is deleted, e.g. because the original
43        // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case,
44        // find all DeclarationWithType nodes (since a VariableExpr must point to a
45        // DeclarationWithType) in the original CompoundStmt and map them to the cloned
46        // node in the new CompoundStmt ('this'), then replace the Declarations referred to
47        // by each VariableExpr according to the constructed map. Note that only the declarations
48        // in the current level are collected into the map, because child CompoundStmts will
49        // recursively execute this routine. There may be more efficient ways of doing
50        // this.
[7862059]51        DeclReplacer::DeclMap declMap;
[23bb1b9]52        std::list< Statement * >::const_iterator origit = other.kids.begin();
53        for ( Statement * s : kids ) {
54                assert( origit != other.kids.end() );
55                Statement * origStmt = *origit++;
56                if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( s ) ) {
[e3e16bc]57                        DeclStmt * origDeclStmt = strict_dynamic_cast< DeclStmt * >( origStmt );
[23bb1b9]58                        if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * > ( declStmt->get_decl() ) ) {
[e3e16bc]59                                DeclarationWithType * origdwt = strict_dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() );
[23bb1b9]60                                assert( dwt->get_name() == origdwt->get_name() );
61                                declMap[ origdwt ] = dwt;
[fc638d2]62                        } else assert( ! dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() ) );
63                } else assert( ! dynamic_cast< DeclStmt * > ( s ) );
[23bb1b9]64        }
65        if ( ! declMap.empty() ) {
[7862059]66                DeclReplacer::replace( this, declMap );
[23bb1b9]67        }
[51b7345]68}
69
[4bf5298]70CompoundStmt::~CompoundStmt() {
[0dd3a2f]71        deleteAll( kids );
[51b7345]72}
73
[50377a4]74void CompoundStmt::print( std::ostream &os, Indenter indent ) const {
75        os << "CompoundStmt" << endl;
76        printAll( kids, os, indent+1 );
[51b7345]77}
78
[0dd3a2f]79// Local Variables: //
80// tab-width: 4 //
81// mode: c++ //
82// compile-command: "make install" //
83// End: //
Note: See TracBrowser for help on using the repository browser.