//
// 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.
//
// Statement.cc --
//
// Author           : Richard C. Bilson
// Created On       : Mon May 18 07:44:20 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Fri Aug 12 13:58:48 2016
// Update Count     : 62
//

#include <functional>
#include <algorithm>
#include <iostream>
#include <list>
#include <cassert>

#include "Statement.h"
#include "Expression.h"
#include "Declaration.h"
#include "Common/SemanticError.h"

using std::string;
using std::endl;

Statement::Statement( std::list<Label> _labels ) : labels( _labels ) {}

void Statement::print( std::ostream &, int indent ) const {}

Statement::~Statement() {}

ExprStmt::ExprStmt( std::list<Label> _labels, Expression *_expr ) : Statement( _labels ), expr( _expr ) {}

ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}

ExprStmt::~ExprStmt() {
	delete expr;
}

void ExprStmt::print( std::ostream &os, int indent ) const {
	os << "Expression Statement:" << endl << std::string( indent + 2, ' ' );
	expr->print( os, indent + 2 );
}


AsmStmt::AsmStmt( std::list<Label> labels, bool voltile, ConstantExpr *instruction, std::list<Expression *> output, std::list<Expression *> input, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels ) : Statement( labels ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ), gotolabels( gotolabels ) {}

AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
  cloneAll( other.output, output );
  cloneAll( other.input, input );
  cloneAll( other.clobber, clobber );
}

AsmStmt::~AsmStmt() {
	delete instruction;
	deleteAll( output );
	deleteAll( input );
	deleteAll( clobber );
}

void AsmStmt::print( std::ostream &os, int indent ) const {
	os << "Assembler Statement:" << endl;
	os << std::string( indent, ' ' ) << "instruction: " << endl << std::string( indent, ' ' );
	instruction->print( os, indent + 2 );
	if ( ! output.empty() ) {
		os << endl << std::string( indent, ' ' ) << "output: " << endl;
		printAll( output, os, indent + 2 );
	} // if
	if ( ! input.empty() ) {
		os << std::string( indent, ' ' ) << "input: " << endl << std::string( indent, ' ' );
		printAll( input, os, indent + 2 );
	} // if
	if ( ! clobber.empty() ) {
		os << std::string( indent, ' ' ) << "clobber: " << endl;
		printAll( clobber, os, indent + 2 );
	} // if
}


const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };

BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type ) throw ( SemanticError ) :
	Statement( labels ), originalTarget( _target ), target( _target ), computedTarget( NULL ), type( _type ) {
	//actually this is a syntactic error signaled by the parser
	if ( type == BranchStmt::Goto && target.empty() )
		throw SemanticError("goto without target");
}

BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type ) throw ( SemanticError ) :
	Statement( labels ), computedTarget( _computedTarget ), type( _type ) {
	if ( type != BranchStmt::Goto || computedTarget == 0 )
		throw SemanticError("Computed target not valid in branch statement");
}

void BranchStmt::print( std::ostream &os, int indent ) const {
	os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ;
}

ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *_expr, bool throwP ) : Statement( labels ), expr( _expr ), isThrow( throwP ) {}

ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ), isThrow( other.isThrow ) {}

ReturnStmt::~ReturnStmt() {
	delete expr;
}

void ReturnStmt::print( std::ostream &os, int indent ) const {
	os << string ( isThrow? "Throw":"Return" ) << " Statement, returning: ";
	if ( expr != 0 ) {
		os << endl << string( indent+2, ' ' );
		expr->print( os, indent + 2 );
	}
	os << endl;
}

IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ):
	Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {}

IfStmt::IfStmt( const IfStmt & other ) :
	Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {}

IfStmt::~IfStmt() {
	delete condition;
	delete thenPart;
	delete elsePart;
}

void IfStmt::print( std::ostream &os, int indent ) const {
	os << "If on condition: " << endl ;
	os << string( indent+4, ' ' );
	condition->print( os, indent + 4 );

	os << string( indent+2, ' ' ) << "... then: " << endl;

	os << string( indent+4, ' ' );
	thenPart->print( os, indent + 4 );

	if ( elsePart != 0 ) {
		os << string( indent+2, ' ' ) << "... else: " << endl;
		os << string( indent+4, ' ' );
		elsePart->print( os, indent + 4 );
	} // if
}

SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_statements ):
	Statement( _labels ), condition( _condition ), statements( _statements ) {
}

SwitchStmt::SwitchStmt( const SwitchStmt & other ):
	Statement( other ), condition( maybeClone( other.condition ) ) {
	cloneAll( other.statements, statements );
}

SwitchStmt::~SwitchStmt() {
	delete condition;
	// destroy statements
	deleteAll( statements );
}

void SwitchStmt::print( std::ostream &os, int indent ) const {
	os << "Switch on condition: ";
	condition->print( os );
	os << endl;

	// statements
	std::list<Statement *>::const_iterator i;
	for ( i = statements.begin(); i != statements.end(); i++)
		(*i)->print( os, indent + 4 );

	//for_each( statements.begin(), statements.end(), mem_fun( bind1st(&Statement::print ), os ));
}

CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) :
	Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) {
	if ( isDefault() && condition != 0 )
		throw SemanticError("default with conditions");
}

CaseStmt::CaseStmt( const CaseStmt & other ) :
	Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
	cloneAll( other.stmts, stmts );
}

CaseStmt::~CaseStmt() {
	delete condition;
	deleteAll( stmts );
}

CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> stmts ) {
	return new CaseStmt( labels, 0, stmts, true );
}

void CaseStmt::print( std::ostream &os, int indent ) const {
	os << string( indent, ' ' );

	if ( isDefault() )
		os << "Default ";
	else {
		os << "Case ";
		condition->print( os );
	} // if

	os << endl;

	std::list<Statement *>::const_iterator i;
	for ( i = stmts.begin(); i != stmts.end(); i++)
		(*i )->print( os, indent + 4 );
}

WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ):
	Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) {
}

WhileStmt::WhileStmt( const WhileStmt & other ):
	Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
}

WhileStmt::~WhileStmt() {
	delete body;
	delete condition;
}

void WhileStmt::print( std::ostream &os, int indent ) const {
	os << "While on condition: " << endl ;
	condition->print( os, indent + 4 );

	os << string( indent, ' ' ) << ".... with body: " << endl;

	if ( body != 0 ) body->print( os, indent + 4 );
}

ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ):
	Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) {
}

ForStmt::ForStmt( const ForStmt & other ):
	Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
		cloneAll( other.initialization, initialization );

}

ForStmt::~ForStmt() {
	deleteAll( initialization );
	delete condition;
	delete increment;
	delete body;
}

void ForStmt::print( std::ostream &os, int indent ) const {
	os << "Labels: {";
	for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
		os << *it << ",";
	}
	os << "}" << endl;

	os << string( indent, ' ' ) << "For Statement" << endl ;

	os << string( indent + 2, ' ' ) << "initialization: \n";
	for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
		os << string( indent + 4, ' ' );
		(*it)->print( os, indent + 4 );
	}

	os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
	if ( condition != 0 ) {
		os << string( indent + 4, ' ' );
		condition->print( os, indent + 4 );
	}

	os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
	if ( increment != 0 ) {
		os << string( indent + 4, ' ' );
		increment->print( os, indent + 4 );
	}

	os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
	if ( body != 0 ) {
		os << string( indent + 4, ' ' );
		body->print( os, indent + 4 );
	}

	os << endl;
}

TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) :
	Statement( labels ), block( tryBlock ),  handlers( _handlers ), finallyBlock( _finallyBlock ) {
}

TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
	cloneAll( other.handlers, handlers );
}

TryStmt::~TryStmt() {
	delete block;
	deleteAll( handlers );
	delete finallyBlock;
}

void TryStmt::print( std::ostream &os, int indent ) const {
	os << "Try Statement" << endl;
	os << string( indent + 2, ' ' ) << "with block: " << endl;
	block->print( os, indent + 4 );

	// handlers
	os << string( indent + 2, ' ' ) << "and handlers: " << endl;
	for ( std::list<Statement *>::const_iterator i = handlers.begin(); i != handlers.end(); i++)
		(*i )->print( os, indent + 4 );

	// finally block
	if ( finallyBlock != 0 ) {
		os << string( indent + 2, ' ' ) << "Finally block: " << endl;
		finallyBlock->print( os, indent + 4 );
	} // if
}

CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool catchAny ) :
	Statement( labels ), decl ( _decl ), body( _body ), catchRest ( catchAny ) {
}

CatchStmt::CatchStmt( const CatchStmt & other ) :
	Statement( other ), decl ( maybeClone( other.decl ) ), body( maybeClone( other.body ) ), catchRest ( other.catchRest ) {
}

CatchStmt::~CatchStmt() {
	delete decl;
	delete body;
}

void CatchStmt::print( std::ostream &os, int indent ) const {
	os << "Catch Statement" << endl;

	os << string( indent, ' ' ) << "... catching" << endl;
	if ( decl ) {
		decl->printShort( os, indent + 4 );
		os << endl;
	} else if ( catchRest )
		os << string( indent + 4 , ' ' ) << "the rest" << endl;
	else
		os << string( indent + 4 , ' ' ) << ">>> Error:  this catch clause must have a declaration <<<" << endl;
}


FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) {
	assert( labels.empty() ); // finally statement cannot be labeled
}

FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
}

FinallyStmt::~FinallyStmt() {
	delete block;
}

void FinallyStmt::print( std::ostream &os, int indent ) const {
	os << "Finally Statement" << endl;
	os << string( indent + 2, ' ' ) << "with block: " << endl;
	block->print( os, indent + 4 );
}

NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}

void NullStmt::print( std::ostream &os, int indent ) const {
	os << "Null Statement" << endl ;
}

ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement( std::list<Label>() ), callStmt( callStmt ) {
	assert( callStmt );
}

ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
}

ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
	delete callStmt;
}

void ImplicitCtorDtorStmt::print( std::ostream &os, int indent ) const {
	os << "Implicit Ctor Dtor Statement" << endl;
	os << string( indent + 2, ' ' ) << "with Ctor/Dtor: ";
	callStmt->print( os, indent + 2);
	os << endl;
}

std::ostream & operator<<( std::ostream & out, const Statement * statement ) {
	if ( statement ) {
		statement->print( out );
	} else {
		out << "nullptr";
	}
	return out;
}

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