Changeset a5a71d0 for src/SynTree/CompoundStmt.cc
- Timestamp:
- Apr 6, 2016, 5:11:32 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- eab39cd
- Parents:
- 39786813 (diff), 3aba311 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/CompoundStmt.cc
r39786813 ra5a71d0 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc -- 7 // XXX.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Tue Jun 23 11:37:49 201511 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Apr 06 14:35:37 2016 13 13 // Update Count : 3 14 14 // … … 18 18 #include <algorithm> 19 19 #include <functional> 20 #include "Expression.h" 21 #include "Declaration.h" 20 22 21 23 using std::string; 22 24 using std::endl; 25 26 class VarExprReplacer : public Visitor { 27 public: 28 typedef std::map< DeclarationWithType *, DeclarationWithType * > DeclMap; 29 private: 30 const DeclMap & declMap; 31 public: 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 23 42 24 43 CompoundStmt::CompoundStmt( std::list<Label> labels ) : Statement( labels ) { … … 27 46 CompoundStmt::CompoundStmt( const CompoundStmt &other ) : Statement( other ) { 28 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 } 29 79 } 30 80
Note: See TracChangeset
for help on using the changeset viewer.