//
// 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.
//
// Initializer.h --
//
// Author           : Richard C. Bilson
// Created On       : Mon May 18 07:44:20 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Thu Mar 23 16:12:42 2017
// Update Count     : 20
//

#ifndef INITIALIZER_H
#define INITIALIZER_H

#include <cassert>

#include "BaseSyntaxNode.h"
#include "Mutator.h"
#include "SynTree.h"
#include "Type.h"
#include "Visitor.h"

const std::list<Expression*> noDesignators;

// Initializer: base class for object initializers (provide default values)
class Initializer : public BaseSyntaxNode {
  public:
	//	Initializer( std::string _name = std::string(""), int _pos = 0 );
	Initializer( bool maybeConstructed );
	Initializer( const Initializer & other );
	virtual ~Initializer();

	static std::string designator_name( Expression *designator );

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

	//	void set_pos( int newValue ) { pos = newValue; }
	//	int get_pos() const { return pos; }
	virtual void set_designators( std::list<Expression *> & ) { assert(false); }
	virtual std::list<Expression *> &get_designators() {
		assert(false);
		std::list<Expression *> *ret = 0; return *ret;	// never reached
	}

	bool get_maybeConstructed() { return maybeConstructed; }

	virtual Initializer *clone() const = 0;
	virtual void accept( Visitor &v ) = 0;
	virtual Initializer *acceptMutator( Mutator &m ) = 0;
	virtual void print( std::ostream &os, int indent = 0 );
  private:
	//	std::string name;
	//	int pos;
	bool maybeConstructed;
};

// SingleInit represents an initializer for a common object (e.g., int x = 4)
class SingleInit : public Initializer {
  public:
	SingleInit( Expression *value, const std::list< Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false );
	SingleInit( const SingleInit &other );
	virtual ~SingleInit();

	Expression *get_value() { return value; }
	void set_value( Expression *newValue ) { value = newValue; }

	std::list<Expression *> &get_designators() { return designators; }
	void set_designators( std::list<Expression *> &newValue ) { designators = newValue; }

	virtual SingleInit *clone() const { return new SingleInit( *this); }
	virtual void accept( Visitor &v ) { v.visit( this ); }
	virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
	virtual void print( std::ostream &os, int indent = 0 );
  private:
	//Constant *value;
	Expression *value;	// has to be a compile-time constant
	std::list< Expression * > designators;
};

// ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize
// an array or aggregate
class ListInit : public Initializer {
  public:
	ListInit( const std::list<Initializer*> &initializers,
			  const std::list<Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false );
	ListInit( const ListInit & other );
	virtual ~ListInit();

	void set_designators( std::list<Expression *> &newValue ) { designators = newValue; }
	std::list<Expression *> &get_designators() { return designators; }
	void set_initializers( std::list<Initializer*> &newValue ) { initializers = newValue; }
	std::list<Initializer*> &get_initializers() { return initializers; }

	typedef std::list<Initializer*>::iterator iterator;
	iterator begin() { return initializers.begin(); }
	iterator end() { return initializers.end(); }

	virtual ListInit *clone() const { return new ListInit( *this ); }
	virtual void accept( Visitor &v ) { v.visit( this ); }
	virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
	virtual void print( std::ostream &os, int indent = 0 );
  private:
	std::list<Initializer*> initializers;  // order *is* important
	std::list<Expression *> designators;
};

// ConstructorInit represents an initializer that is either a constructor expression or
// a C-style initializer.
// It should not be necessary to create ConstructorInit nodes manually. Instead, set maybeConstructed
// to true on SingleInit or ListInit constructors if object should be constructed.
class ConstructorInit : public Initializer {
  public:
	ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init );
	ConstructorInit( const ConstructorInit &other );
	virtual ~ConstructorInit();

	void set_ctor( Statement * newValue ) { ctor = newValue; }
	Statement * get_ctor() const { return ctor; }
	void set_dtor( Statement * newValue ) { dtor = newValue; }
	Statement * get_dtor() const { return dtor; }
	void set_init( Initializer * newValue ) { init = newValue; }
	Initializer * get_init() const { return init; }

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

  private:
	Statement * ctor;
	Statement * dtor;
	// C-style initializer made up of SingleInit and ListInit nodes to use as a fallback
	// if an appropriate constructor definition is not found by the resolver
	Initializer * init;
};

std::ostream & operator<<( std::ostream & out, Initializer * init );

#endif // INITIALIZER_H

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