//
// 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.
//
// TupleExpr.cc --
//
// Author           : Richard C. Bilson
// Created On       : Mon May 18 07:44:20 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Mon May 18 10:59:19 2015
// Update Count     : 1
//

#include "Expression.h"
#include "Common/utility.h"
#include "Type.h"
#include "Declaration.h"
#include "Tuples/Tuples.h"
#include "VarExprReplacer.h"

UntypedTupleExpr::UntypedTupleExpr( const std::list< Expression * > & exprs, Expression *_aname ) : Expression( _aname ), exprs( exprs ) {
}

UntypedTupleExpr::UntypedTupleExpr( const UntypedTupleExpr &other ) : Expression( other ) {
	cloneAll( other.exprs, exprs );
}

UntypedTupleExpr::~UntypedTupleExpr() {
	deleteAll( exprs );
}

void UntypedTupleExpr::print( std::ostream &os, int indent ) const {
	os << "Untyped Tuple:" << std::endl;
	printAll( exprs, os, indent+2 );
	Expression::print( os, indent );
}

TupleExpr::TupleExpr( const std::list< Expression * > & exprs, Expression *_aname ) : Expression( _aname ), exprs( exprs ) {
	set_result( Tuples::makeTupleType( exprs ) );
}

TupleExpr::TupleExpr( const TupleExpr &other ) : Expression( other ) {
	cloneAll( other.exprs, exprs );
}

TupleExpr::~TupleExpr() {
	deleteAll( exprs );
}

void TupleExpr::print( std::ostream &os, int indent ) const {
	os << "Tuple:" << std::endl;
	printAll( exprs, os, indent+2 );
	Expression::print( os, indent );
}

TupleIndexExpr::TupleIndexExpr( Expression * tuple, unsigned int index ) : tuple( tuple ), index( index )  {
	TupleType * type = safe_dynamic_cast< TupleType * >( tuple->get_result() );
	assertf( type->size() > index, "TupleIndexExpr index out of bounds: tuple size %d, requested index %d in expr %s", type->size(), index, toString( tuple ).c_str() );
	set_result( (*std::next( type->get_types().begin(), index ))->clone() );
	get_result()->set_isLvalue( type->get_isLvalue() );
}

TupleIndexExpr::TupleIndexExpr( const TupleIndexExpr &other ) : Expression( other ), tuple( other.tuple->clone() ), index( other.index ) {
}

TupleIndexExpr::~TupleIndexExpr() {
	delete tuple;
}

void TupleIndexExpr::print( std::ostream &os, int indent ) const {
	os << "Tuple Index Expression, with tuple:" << std::endl;
	os << std::string( indent+2, ' ' );
	tuple->print( os, indent+2 );
	os << std::string( indent+2, ' ' ) << "with index: " << index << std::endl;
	Expression::print( os, indent );
}

MemberTupleExpr::MemberTupleExpr( Expression * member, Expression * aggregate, Expression * _aname ) : Expression( _aname ) {
	set_result( maybeClone( member->get_result() ) ); // xxx - ???
}

MemberTupleExpr::MemberTupleExpr( const MemberTupleExpr &other ) : Expression( other ), member( other.member->clone() ), aggregate( other.aggregate->clone() ) {
}

MemberTupleExpr::~MemberTupleExpr() {
	delete member;
	delete aggregate;
}

void MemberTupleExpr::print( std::ostream &os, int indent ) const {
	os << "Member Tuple Expression, with aggregate:" << std::endl;
	os << std::string( indent+2, ' ' );
	aggregate->print( os, indent+2 );
	os << std::string( indent+2, ' ' ) << "with member: " << std::endl;
	os << std::string( indent+2, ' ' );
	member->print( os, indent+2 );
	Expression::print( os, indent );
}

TupleAssignExpr::TupleAssignExpr( const std::list< Expression * > & assigns, const std::list< ObjectDecl * > & tempDecls, Expression * _aname ) : Expression( _aname ) {
	// convert internally into a StmtExpr which contains the declarations and produces the tuple of the assignments
	set_result( Tuples::makeTupleType( assigns ) );
	CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
	std::list< Statement * > & stmts = compoundStmt->get_kids();
	for ( ObjectDecl * obj : tempDecls ) {
		stmts.push_back( new DeclStmt( noLabels, obj ) );
	}
	TupleExpr * tupleExpr = new TupleExpr( assigns );
	assert( tupleExpr->get_result() );
	stmts.push_back( new ExprStmt( noLabels, tupleExpr ) );
	stmtExpr = new StmtExpr( compoundStmt );
}

TupleAssignExpr::TupleAssignExpr( const TupleAssignExpr &other ) : Expression( other ) {
	assert( other.stmtExpr );
	stmtExpr = other.stmtExpr->clone();
}

TupleAssignExpr::~TupleAssignExpr() {
	delete stmtExpr;
}

void TupleAssignExpr::print( std::ostream &os, int indent ) const {
	os << "Tuple Assignment Expression, with stmt expr:" << std::endl;
	os << std::string( indent+2, ' ' );
	stmtExpr->print( os, indent+4 );
	Expression::print( os, indent );
}



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