//
// 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.
//
// Expression.h --
//
// Author           : Richard C. Bilson
// Created On       : Mon May 18 07:44:20 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Sun Sep  3 19:23:46 2017
// Update Count     : 48
//

#pragma once

#include <iosfwd>                 // for ostream
#include <list>                   // for list, list<>::iterator
#include <map>                    // for map, map<>::value_compare
#include <memory>                 // for allocator, unique_ptr
#include <string>                 // for string

#include "BaseSyntaxNode.h"       // for BaseSyntaxNode
#include "Constant.h"             // for Constant
#include "Initializer.h"          // for Designation (ptr only), Initializer
#include "Label.h"                // for Label
#include "Mutator.h"              // for Mutator
#include "SynTree.h"              // for UniqueId
#include "Visitor.h"              // for Visitor


struct ParamEntry;

typedef std::map< UniqueId, ParamEntry > InferredParams;

/// ParamEntry contains the i.d. of a declaration and a type that is derived from that declaration,
/// but subject to decay-to-pointer and type parameter renaming
struct ParamEntry {
	ParamEntry(): decl( 0 ), actualType( 0 ), formalType( 0 ), expr( 0 ), inferParams( new InferredParams ) {}
	ParamEntry( UniqueId decl, Type * actualType, Type * formalType, Expression* expr ): decl( decl ), actualType( actualType ), formalType( formalType ), expr( expr ), inferParams( new InferredParams ) {}
	ParamEntry( const ParamEntry & other );
	ParamEntry & operator=( const ParamEntry & other );

	UniqueId decl;
	Type * actualType;
	Type * formalType;
	Expression * expr;
	std::unique_ptr< InferredParams > inferParams;
};

/// Expression is the root type for all expressions
class Expression : public BaseSyntaxNode {
  protected:
	virtual ~Expression();

  public:
	Type * result;
	TypeSubstitution * env;
	bool extension = false;
	InferredParams inferParams;

	Expression();
	Expression( const Expression & other );

	Type *& get_result() { return result; }
	const Type * get_result() const { return result; }
	void set_result( Type * newValue ) { result = newValue; }

	TypeSubstitution * get_env() const { return env; }
	void set_env( TypeSubstitution * newValue ) { env = newValue; }
	bool get_extension() const { return extension; }
	Expression * set_extension( bool exten ) { extension = exten; return this; }

	InferredParams & get_inferParams() { return inferParams; }

	virtual Expression * clone() const override = 0;
	virtual void accept( Visitor & v ) override = 0;
	virtual Expression * acceptMutator( Mutator & m ) override = 0;
	virtual void print( std::ostream & os, Indenter indent = {} ) const override;
};

/// ApplicationExpr represents the application of a function to a set of parameters.  This is the result of running an
/// UntypedExpr through the expression analyzer.
class ApplicationExpr : public Expression {
  public:
	Expression * function;
	std::list<Expression *> args;

	ApplicationExpr( Expression * function, const std::list<Expression *> & args = std::list< Expression * >() );
	ApplicationExpr( const ApplicationExpr & other );

	Expression * get_function() const { return function; }
	void set_function( Expression * newValue ) { function = newValue; }
	std::list<Expression *>& get_args() { return args; }

	virtual ApplicationExpr * clone() const { return new ApplicationExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// UntypedExpr represents the application of a function to a set of parameters, but where the particular overload for
/// the function name has not yet been determined.  Most operators are converted into functional form automatically, to
/// permit operator overloading.
class UntypedExpr : public Expression {
  public:
	Expression * function;
	std::list<Expression*> args;

	UntypedExpr( Expression * function, const std::list<Expression *> & args = std::list< Expression * >() );
	UntypedExpr( const UntypedExpr & other );

	Expression * get_function() const { return function; }
	void set_function( Expression * newValue ) { function = newValue; }

	std::list<Expression*>::iterator begin_args() { return args.begin(); }
	std::list<Expression*>::iterator end_args() { return args.end(); }
	std::list<Expression*>& get_args() { return args; }

	static UntypedExpr * createDeref( Expression * arg );
	static UntypedExpr * createAssign( Expression * arg1, Expression * arg2 );

	virtual UntypedExpr * clone() const { return new UntypedExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// NameExpr contains a name whose meaning is still not determined
class NameExpr : public Expression {
  public:
	std::string name;

	NameExpr( std::string name );
	NameExpr( const NameExpr & other );

	const std::string & get_name() const { return name; }
	void set_name( std::string newValue ) { name = newValue; }

	virtual NameExpr * clone() const { return new NameExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

// The following classes are used to represent expression types that cannot be converted into
// function-call format.

/// AddressExpr represents a address-of expression, e.g. & e
class AddressExpr : public Expression {
  public:
	Expression * arg;

	AddressExpr( Expression * arg );
	AddressExpr( const AddressExpr & other );

	Expression * get_arg() const { return arg; }
	void set_arg(Expression * newValue ) { arg = newValue; }

	virtual AddressExpr * clone() const { return new AddressExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

// GCC &&label
// https://gcc.gnu.org/onlinedocs/gcc-3.4.2/gcc/Labels-as-Values.html
class LabelAddressExpr : public Expression {
  public:
	Label arg;

	LabelAddressExpr( const Label &arg );
	LabelAddressExpr( const LabelAddressExpr & other );

	virtual LabelAddressExpr * clone() const { return new LabelAddressExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// CastExpr represents a type cast expression, e.g. (int)e
class CastExpr : public Expression {
  public:
	Expression * arg;
	bool isGenerated = true; // whether this cast appeared in the source program

	CastExpr( Expression * arg, bool isGenerated = true );
	CastExpr( Expression * arg, Type * toType, bool isGenerated = true );
	CastExpr( Expression * arg, void * ) = delete; // prevent accidentally passing pointers for isGenerated in the first constructor
	CastExpr( const CastExpr & other );

	Expression * get_arg() const { return arg; }
	void set_arg( Expression * newValue ) { arg = newValue; }

	virtual CastExpr * clone() const { return new CastExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// KeywordCastExpr represents a cast to 'keyword types', e.g. (thread &)t
class KeywordCastExpr : public Expression {
public:
	Expression * arg;
	enum Target {
		Coroutine, Thread, Monitor, NUMBER_OF_TARGETS
	} target;

	KeywordCastExpr( Expression * arg, Target target );
	KeywordCastExpr( const KeywordCastExpr & other );
	virtual ~KeywordCastExpr();

	const std::string & targetString() const;

	virtual KeywordCastExpr * clone() const { return new KeywordCastExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// VirtualCastExpr repersents a virtual dynamic cast, e.g. (virtual exception)e
class VirtualCastExpr : public Expression {
  public:
	Expression * arg;

	VirtualCastExpr( Expression * arg, Type * toType );
	VirtualCastExpr( const VirtualCastExpr & other );

	Expression * get_arg() const { return arg; }
	void set_arg( Expression * newValue ) { arg = newValue; }

	virtual VirtualCastExpr * clone() const { return new VirtualCastExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// UntypedMemberExpr represents a member selection operation, e.g. q.p before processing by the expression analyzer
class UntypedMemberExpr : public Expression {
  public:
	Expression * member;
	Expression * aggregate;

	UntypedMemberExpr( Expression * member, Expression * aggregate );
	UntypedMemberExpr( const UntypedMemberExpr & other );

	Expression * get_member() const { return member; }
	void set_member( Expression * newValue ) { member = newValue; }
	Expression * get_aggregate() const { return aggregate; }
	void set_aggregate( Expression * newValue ) { aggregate = newValue; }

	virtual UntypedMemberExpr * clone() const { return new UntypedMemberExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// MemberExpr represents a member selection operation, e.g. q.p after processing by the expression analyzer.
/// Does not take ownership of member.
class MemberExpr : public Expression {
  public:
	DeclarationWithType * member;
	Expression * aggregate;

	MemberExpr( DeclarationWithType * member, Expression * aggregate );
	MemberExpr( const MemberExpr & other );

	DeclarationWithType * get_member() const { return member; }
	void set_member( DeclarationWithType * newValue ) { member = newValue; }
	Expression * get_aggregate() const { return aggregate; }
	void set_aggregate( Expression * newValue ) { aggregate = newValue; }

	virtual MemberExpr * clone() const { return new MemberExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// VariableExpr represents an expression that simply refers to the value of a named variable.
/// Does not take ownership of var.
class VariableExpr : public Expression {
  public:
	DeclarationWithType * var;

	VariableExpr( DeclarationWithType * var );
	VariableExpr( const VariableExpr & other );

	DeclarationWithType * get_var() const { return var; }
	void set_var( DeclarationWithType * newValue ) { var = newValue; }

	static VariableExpr * functionPointer( FunctionDecl * decl );

	virtual VariableExpr * clone() const { return new VariableExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// ConstantExpr represents an expression that simply refers to the value of a constant
class ConstantExpr : public Expression {
  public:
	Constant constant;

	ConstantExpr( Constant constant );
	ConstantExpr( const ConstantExpr & other );

	Constant * get_constant() { return & constant; }
	const Constant * get_constant() const { return & constant; }
	void set_constant( const Constant & newValue ) { constant = newValue; }

	long long int intValue() const;

	virtual ConstantExpr * clone() const { return new ConstantExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// SizeofExpr represents a sizeof expression (could be sizeof(int) or sizeof 3+4)
class SizeofExpr : public Expression {
  public:
	Expression * expr;
	Type * type;
	bool isType;

	SizeofExpr( Expression * expr );
	SizeofExpr( const SizeofExpr & other );
	SizeofExpr( Type * type );

	Expression * get_expr() const { return expr; }
	void set_expr( Expression * newValue ) { expr = newValue; }
	Type * get_type() const { return type; }
	void set_type( Type * newValue ) { type = newValue; }
	bool get_isType() const { return isType; }
	void set_isType( bool newValue ) { isType = newValue; }

	virtual SizeofExpr * clone() const { return new SizeofExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// AlignofExpr represents an alignof expression
class AlignofExpr : public Expression {
  public:
	Expression * expr;
	Type * type;
	bool isType;

	AlignofExpr( Expression * expr );
	AlignofExpr( const AlignofExpr & other );
	AlignofExpr( Type * type );

	Expression * get_expr() const { return expr; }
	void set_expr( Expression * newValue ) { expr = newValue; }
	Type * get_type() const { return type; }
	void set_type( Type * newValue ) { type = newValue; }
	bool get_isType() const { return isType; }
	void set_isType( bool newValue ) { isType = newValue; }

	virtual AlignofExpr * clone() const { return new AlignofExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// UntypedOffsetofExpr represents an offsetof expression before resolution
class UntypedOffsetofExpr : public Expression {
  public:
	Type * type;
	std::string member;

	UntypedOffsetofExpr( Type * type, const std::string & member );
	UntypedOffsetofExpr( const UntypedOffsetofExpr & other );

	std::string get_member() const { return member; }
	void set_member( const std::string & newValue ) { member = newValue; }
	Type * get_type() const { return type; }
	void set_type( Type * newValue ) { type = newValue; }

	virtual UntypedOffsetofExpr * clone() const { return new UntypedOffsetofExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// OffsetofExpr represents an offsetof expression
class OffsetofExpr : public Expression {
  public:
	Type * type;
	DeclarationWithType * member;

	OffsetofExpr( Type * type, DeclarationWithType * member );
	OffsetofExpr( const OffsetofExpr & other );

	Type * get_type() const { return type; }
	void set_type( Type * newValue ) { type = newValue; }
	DeclarationWithType * get_member() const { return member; }
	void set_member( DeclarationWithType * newValue ) { member = newValue; }

	virtual OffsetofExpr * clone() const { return new OffsetofExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// Expression representing a pack of field-offsets for a generic type
class OffsetPackExpr : public Expression {
public:
	StructInstType * type;

	OffsetPackExpr( StructInstType * type );
	OffsetPackExpr( const OffsetPackExpr & other );

	StructInstType * get_type() const { return type; }
	void set_type( StructInstType * newValue ) { type = newValue; }

	virtual OffsetPackExpr * clone() const { return new OffsetPackExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// AttrExpr represents an @attribute expression (like sizeof, but user-defined)
class AttrExpr : public Expression {
  public:
	Expression * attr;
	Expression * expr;
	Type * type;
	bool isType;

	AttrExpr(Expression * attr, Expression * expr );
	AttrExpr( const AttrExpr & other );
	AttrExpr( Expression * attr, Type * type );

	Expression * get_attr() const { return attr; }
	void set_attr( Expression * newValue ) { attr = newValue; }
	Expression * get_expr() const { return expr; }
	void set_expr( Expression * newValue ) { expr = newValue; }
	Type * get_type() const { return type; }
	void set_type( Type * newValue ) { type = newValue; }
	bool get_isType() const { return isType; }
	void set_isType( bool newValue ) { isType = newValue; }

	virtual AttrExpr * clone() const { return new AttrExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// LogicalExpr represents a short-circuit boolean expression (&& or ||)
class LogicalExpr : public Expression {
  public:
	Expression * arg1;
	Expression * arg2;

	LogicalExpr( Expression * arg1, Expression * arg2, bool andp = true );
	LogicalExpr( const LogicalExpr & other );

	bool get_isAnd() const { return isAnd; }
	Expression * get_arg1() { return arg1; }
	void set_arg1( Expression * newValue ) { arg1 = newValue; }
	Expression * get_arg2() const { return arg2; }
	void set_arg2( Expression * newValue ) { arg2 = newValue; }

	virtual LogicalExpr * clone() const { return new LogicalExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;

  private:
	bool isAnd;
};

/// ConditionalExpr represents the three-argument conditional ( p ? a : b )
class ConditionalExpr : public Expression {
  public:
	Expression * arg1;
	Expression * arg2;
	Expression * arg3;

	ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 );
	ConditionalExpr( const ConditionalExpr & other );

	Expression * get_arg1() const { return arg1; }
	void set_arg1( Expression * newValue ) { arg1 = newValue; }
	Expression * get_arg2() const { return arg2; }
	void set_arg2( Expression * newValue ) { arg2 = newValue; }
	Expression * get_arg3() const { return arg3; }
	void set_arg3( Expression * newValue ) { arg3 = newValue; }

	virtual ConditionalExpr * clone() const { return new ConditionalExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// CommaExpr represents the sequence operator ( a, b )
class CommaExpr : public Expression {
  public:
	Expression * arg1;
	Expression * arg2;

	CommaExpr( Expression * arg1, Expression * arg2 );
	CommaExpr( const CommaExpr & other );

	Expression * get_arg1() const { return arg1; }
	void set_arg1( Expression * newValue ) { arg1 = newValue; }
	Expression * get_arg2() const { return arg2; }
	void set_arg2( Expression * newValue ) { arg2 = newValue; }

	virtual CommaExpr * clone() const { return new CommaExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// TypeExpr represents a type used in an expression (e.g. as a type generator parameter)
class TypeExpr : public Expression {
  public:
	Type * type;

	TypeExpr( Type * type );
	TypeExpr( const TypeExpr & other );

	Type * get_type() const { return type; }
	void set_type( Type * newValue ) { type = newValue; }

	virtual TypeExpr * clone() const { return new TypeExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// AsmExpr represents a GCC 'asm constraint operand' used in an asm statement: [output] "=f" (result)
class AsmExpr : public Expression {
  public:
	Expression * inout;
	Expression * constraint;
	Expression * operand;

	AsmExpr( Expression * inout, Expression * constraint, Expression * operand ) : inout( inout ), constraint( constraint ), operand( operand ) {}
	AsmExpr( const AsmExpr & other );

	Expression * get_inout() const { return inout; }
	void set_inout( Expression * newValue ) { inout = newValue; }

	Expression * get_constraint() const { return constraint; }
	void set_constraint( Expression * newValue ) { constraint = newValue; }

	Expression * get_operand() const { return operand; }
	void set_operand( Expression * newValue ) { operand = newValue; }

	virtual AsmExpr * clone() const { return new AsmExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;

	// https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints
};

/// ImplicitCopyCtorExpr represents the application of a function to a set of parameters,
/// along with a set of copy constructor calls, one for each argument.
class ImplicitCopyCtorExpr : public Expression {
protected:
	virtual ~ImplicitCopyCtorExpr();

public:
	ApplicationExpr * callExpr;
	std::list< ObjectDecl * > tempDecls;
	std::list< ObjectDecl * > returnDecls;
	std::list< Expression * > dtors;

	ImplicitCopyCtorExpr( ApplicationExpr * callExpr );
	ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other );

	ApplicationExpr * get_callExpr() const { return callExpr; }
	void set_callExpr( ApplicationExpr * newValue ) { callExpr = newValue; }

	std::list< ObjectDecl * > & get_tempDecls() { return tempDecls; }
	std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; }
	std::list< Expression * > & get_dtors() { return dtors; }

	virtual ImplicitCopyCtorExpr * clone() const { return new ImplicitCopyCtorExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// ConstructorExpr represents the use of a constructor in an expression context, e.g. int * x = malloc() { 5 };
class ConstructorExpr : public Expression {
public:
	Expression * callExpr;

	ConstructorExpr( Expression * callExpr );
	ConstructorExpr( const ConstructorExpr & other );

	Expression * get_callExpr() const { return callExpr; }
	void set_callExpr( Expression * newValue ) { callExpr = newValue; }

	virtual ConstructorExpr * clone() const { return new ConstructorExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// CompoundLiteralExpr represents a C99 'compound literal'
class CompoundLiteralExpr : public Expression {
  public:
	Initializer * initializer;

	CompoundLiteralExpr( Type * type, Initializer * initializer );
	CompoundLiteralExpr( const CompoundLiteralExpr & other );

	Initializer * get_initializer() const { return initializer; }
	void set_initializer( Initializer * i ) { initializer = i; }

	virtual CompoundLiteralExpr * clone() const { return new CompoundLiteralExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// RangeExpr represents a range e.g. '3 ... 5' or '1~10'
class RangeExpr : public Expression {
  public:
	Expression * low, * high;

	RangeExpr( Expression * low, Expression * high );
	RangeExpr( const RangeExpr & other );

	Expression * get_low() const { return low; }
	Expression * get_high() const { return high; }
	RangeExpr * set_low( Expression * low ) { RangeExpr::low = low; return this; }
	RangeExpr * set_high( Expression * high ) { RangeExpr::high = high; return this; }

	virtual RangeExpr * clone() const { return new RangeExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// UntypedTupleExpr represents a tuple expression ( [a, b, c] ) before resolution
class UntypedTupleExpr : public Expression {
  public:
	std::list<Expression*> exprs;

	UntypedTupleExpr( const std::list< Expression * > & exprs );
	UntypedTupleExpr( const UntypedTupleExpr & other );

	std::list<Expression*>& get_exprs() { return exprs; }

	virtual UntypedTupleExpr * clone() const { return new UntypedTupleExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// TupleExpr represents a tuple expression ( [a, b, c] )
class TupleExpr : public Expression {
  public:
	std::list<Expression*> exprs;

	TupleExpr( const std::list< Expression * > & exprs );
	TupleExpr( const TupleExpr & other );

	std::list<Expression*>& get_exprs() { return exprs; }

	virtual TupleExpr * clone() const { return new TupleExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// TupleIndexExpr represents an element selection operation on a tuple value, e.g. t.3 after processing by the expression analyzer
class TupleIndexExpr : public Expression {
  public:
	Expression * tuple;
	unsigned int index;

	TupleIndexExpr( Expression * tuple, unsigned int index );
	TupleIndexExpr( const TupleIndexExpr & other );

	Expression * get_tuple() const { return tuple; }
	int get_index() const { return index; }
	TupleIndexExpr * set_tuple( Expression * newValue ) { tuple = newValue; return this; }
	TupleIndexExpr * set_index( unsigned int newValue ) { index = newValue; return this; }

	virtual TupleIndexExpr * clone() const { return new TupleIndexExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// TupleAssignExpr represents a multiple assignment operation, where both sides of the assignment have tuple type, e.g. [a, b, c] = [d, e, f];, a mass assignment operation, where the left hand side has tuple type and the right hand side does not, e.g. [a, b, c] = 5.0;, or a tuple ctor/dtor expression
class TupleAssignExpr : public Expression {
  public:
	StmtExpr * stmtExpr = nullptr;

	TupleAssignExpr( const std::list< Expression * > & assigns, const std::list< ObjectDecl * > & tempDecls );
	TupleAssignExpr( const TupleAssignExpr & other );

	TupleAssignExpr * set_stmtExpr( StmtExpr * newValue ) { stmtExpr = newValue; return this; }
	StmtExpr * get_stmtExpr() const { return stmtExpr; }

	virtual TupleAssignExpr * clone() const { return new TupleAssignExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// StmtExpr represents a GCC 'statement expression', e.g. ({ int x = 5; x; })
class StmtExpr : public Expression {
public:
	CompoundStmt * statements;
	std::list< ObjectDecl * > returnDecls; // return variable(s) for stmt expression
	std::list< Expression * > dtors; // destructor(s) for return variable(s)

	StmtExpr( CompoundStmt * statements );
	StmtExpr( const StmtExpr & other );

	CompoundStmt * get_statements() const { return statements; }
	StmtExpr * set_statements( CompoundStmt * newValue ) { statements = newValue; return this; }

	// call to set the result type of this StmtExpr based on its body
	void computeResult();

	std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; }
	std::list< Expression * > & get_dtors() { return dtors; }

	virtual StmtExpr * clone() const { return new StmtExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

class UniqueExpr : public Expression {
public:
	Expression * expr;
	ObjectDecl * object;
	VariableExpr * var;

	UniqueExpr( Expression * expr, long long idVal = -1 );
	UniqueExpr( const UniqueExpr & other );

	Expression * get_expr() const { return expr; }
	UniqueExpr * set_expr( Expression * newValue ) { expr = newValue; return this; }

	ObjectDecl * get_object() const { return object; }
	UniqueExpr * set_object( ObjectDecl * newValue ) { object = newValue; return this; }

	VariableExpr * get_var() const { return var; }
	UniqueExpr * set_var( VariableExpr * newValue ) { var = newValue; return this; }

	int get_id() const { return id; }

	virtual UniqueExpr * clone() const { return new UniqueExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;

private:
	int id;
	static long long count;
};

struct InitAlternative {
public:
	Type * type = nullptr;
	Designation * designation = nullptr;
	InitAlternative( Type * type, Designation * designation );
	InitAlternative( const InitAlternative & other );
	InitAlternative & operator=( const Initializer & other ) = delete; // at the moment this isn't used, and I don't want to implement it
};

class UntypedInitExpr : public Expression {
public:
	Expression * expr;
	std::list<InitAlternative> initAlts;

	UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts );
	UntypedInitExpr( const UntypedInitExpr & other );

	Expression * get_expr() const { return expr; }
	UntypedInitExpr * set_expr( Expression * newValue ) { expr = newValue; return this; }

	std::list<InitAlternative> & get_initAlts() { return initAlts; }

	virtual UntypedInitExpr * clone() const { return new UntypedInitExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

class InitExpr : public Expression {
public:
	Expression * expr;
	Designation * designation;

	InitExpr( Expression * expr, Designation * designation );
	InitExpr( const InitExpr & other );

	Expression * get_expr() const { return expr; }
	InitExpr * set_expr( Expression * newValue ) { expr = newValue; return this; }

	Designation * get_designation() const { return designation; }
	InitExpr * set_designation( Designation * newValue ) { designation = newValue; return this; }

	virtual InitExpr * clone() const { return new InitExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

/// expression that contains a deleted identifier - should never make it past the resolver.
class DeletedExpr : public Expression {
public:
	Expression * expr;
	BaseSyntaxNode * deleteStmt;

	DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt );
	DeletedExpr( const DeletedExpr & other );

	virtual DeletedExpr * clone() const { return new DeletedExpr( * this ); }
	virtual void accept( Visitor & v ) { v.visit( this ); }
	virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
	virtual void print( std::ostream & os, Indenter indent = {} ) const;
};

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