Index: src/SynTree/CompoundStmt.cc
===================================================================
--- src/SynTree/CompoundStmt.cc	(revision d7903b1e0925fcd512a0f5d5742bfdad5734fb42)
+++ src/SynTree/CompoundStmt.cc	(revision d7bcbf5a448434f2bcfc247bc16c0f111feaca9a)
@@ -20,24 +20,8 @@
 #include "Expression.h"
 #include "Declaration.h"
+#include "SynTree/VarExprReplacer.h"
 
 using std::string;
 using std::endl;
-
-class VarExprReplacer : public Visitor {
-public:
-  typedef std::map< DeclarationWithType *, DeclarationWithType * > DeclMap;
-private:
-  const DeclMap & declMap;
-public:
-  VarExprReplacer( const DeclMap & declMap ) : declMap( declMap ) {}
-
-  // replace variable with new node from decl map
-  virtual void visit( VariableExpr * varExpr ) {
-    if ( declMap.count( varExpr->get_var() ) ) {
-      varExpr->set_var( declMap.at( varExpr->get_var() ) );
-    }
-  }
-};
-
 
 CompoundStmt::CompoundStmt( std::list<Label> labels ) : Statement( labels ) {
@@ -47,34 +31,36 @@
 	cloneAll( other.kids, kids );
 
-  // when cloning a compound statement, we may end up cloning declarations which
-  // are referred to by VariableExprs throughout the block. Cloning a VariableExpr
-  // does a shallow copy, so the VariableExpr will end up pointing to the original
-  // declaration. If the original declaration is deleted, e.g. because the original
-  // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case,
-  // find all DeclarationWithType nodes (since a VariableExpr must point to a
-  // DeclarationWithType) in the original CompoundStmt and map them to the cloned
-  // node in the new CompoundStmt ('this'), then replace the Declarations referred to
-  // by each VariableExpr according to the constructed map. Note that only the declarations
-  // in the current level are collected into the map, because child CompoundStmts will
-  // recursively execute this routine. There may be more efficient ways of doing
-  // this.
-  VarExprReplacer::DeclMap declMap;
-  std::list< Statement * >::const_iterator origit = other.kids.begin();
-  for ( Statement * s : kids ) {
-    assert( origit != other.kids.end() );
-    if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( s ) ) {
-      DeclStmt * origDeclStmt = dynamic_cast< DeclStmt * >( *origit );
-      assert( origDeclStmt );
-      if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * > ( declStmt->get_decl() ) ) {
-        DeclarationWithType * origdwt = dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() );
-        assert( origdwt );
-        declMap[ origdwt ] = dwt;
-      }
-    }
-  }
-  if ( ! declMap.empty() ) {
-    VarExprReplacer replacer( declMap );
-    accept( replacer );
-  }
+	// when cloning a compound statement, we may end up cloning declarations which
+	// are referred to by VariableExprs throughout the block. Cloning a VariableExpr
+	// does a shallow copy, so the VariableExpr will end up pointing to the original
+	// declaration. If the original declaration is deleted, e.g. because the original
+	// CompoundStmt is deleted, then we have a dangling pointer. To avoid this case,
+	// find all DeclarationWithType nodes (since a VariableExpr must point to a
+	// DeclarationWithType) in the original CompoundStmt and map them to the cloned
+	// node in the new CompoundStmt ('this'), then replace the Declarations referred to
+	// by each VariableExpr according to the constructed map. Note that only the declarations
+	// in the current level are collected into the map, because child CompoundStmts will
+	// recursively execute this routine. There may be more efficient ways of doing
+	// this.
+	VarExprReplacer::DeclMap declMap;
+	std::list< Statement * >::const_iterator origit = other.kids.begin();
+	for ( Statement * s : kids ) {
+		assert( origit != other.kids.end() );
+		Statement * origStmt = *origit++;
+		if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( s ) ) {
+			DeclStmt * origDeclStmt = dynamic_cast< DeclStmt * >( origStmt );
+			assert( origDeclStmt );
+			if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * > ( declStmt->get_decl() ) ) {
+				DeclarationWithType * origdwt = dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() );
+				assert( origdwt );
+				assert( dwt->get_name() == origdwt->get_name() );
+				declMap[ origdwt ] = dwt;
+			}
+		}
+	}
+	if ( ! declMap.empty() ) {
+		VarExprReplacer replacer( declMap );
+		accept( replacer );
+	}
 }
 
