//
// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
//
// The contents of this file are covered under the licence agreement in the
// file "LICENCE" distributed with Cforall.
//
// XXX.cc --
//
// Author           : Richard C. Bilson
// Created On       : Mon May 18 07:44:20 2015
// Last Modified By : Rob Schluntz
// Last Modified On : Mon May 02 15:19:17 2016
// Update Count     : 3
//

#include "Statement.h"
#include "Common/utility.h"
#include <algorithm>
#include <functional>
#include "Expression.h"
#include "Declaration.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 ) {
}

CompoundStmt::CompoundStmt( const CompoundStmt &other ) : Statement( other ) {
	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 );
  }
}

CompoundStmt::~CompoundStmt() {
	deleteAll( kids );
}

void CompoundStmt::print( std::ostream &os, int indent ) const {
	os << "CompoundStmt" << endl ;
	printAll( kids, os, indent + 2 );
}

// Local Variables: //
// tab-width: 4 //
// mode: c++ //
// compile-command: "make install" //
// End: //
