/* $Id$ 
 * DotVisitor: create a .dot graph representation of the abstract syntax
 * tree.
 *
 * Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#ifndef __DOT_VISITOR_HPP_INCLUDED
#define __DOT_VISITOR_HPP_INCLUDED

#include <list>
#include "frontend/visitor/TopDownVisitor.hpp"

namespace ast {

//! convert AST to a dot graph.
/** This visitor will convert the abstract syntax tree to a dot graph. 
 *  The output can then be converted to e.g. a postscript file with
 *    dot -Tps output.dot > output.ps
 *  This visitor is safe to be used during all frontend stages.
 */
class DotVisitor : public TopDownVisitor {
public:
	//! c'tor
	DotVisitor();

	//! d'tor
	~DotVisitor();
private:
	/** visit a ElementAssociation
         *  @param node node that get's visited.
         */
	virtual void visit(ElementAssociation& node);

	/** visit a ConstInteger
         *  @param node node that get's visited.
         */
	virtual void visit(ConstInteger& node);

	/** visit a ConstReal
         *  @param node node that get's visited.
         */
	virtual void visit(ConstReal& node);

	/** visit a ConstArray
         *  @param node node that get's visited.
         */
	virtual void visit(ConstArray &node);

	/** visit a SimpleName
         *  @param node node that get's visited.
         */
	virtual void visit(SimpleName& node);

	/** visit a SelectedName
         *  @param node node that get's visited.
         */
	virtual void visit(SelectedName& node);

	/** visit a AttributeName
         *  @param node node that get's visited.
         */
	virtual void visit(AttributeName& node);

	/** visit a TemporaryName
         *  @param node node that get's visited.
         */
	virtual void visit(TemporaryName& node);

	/** Visit an Entity declaration.
	 *  @param node Entity Declaration node that get's visited.
	 */
	virtual void visit(Entity& node);

	/** Visit an Signal declaration.
	 *  @param node SignalDeclaration node that get's visited.
	 */
	virtual void visit(SignalDeclaration& node);

	/** Visit an Constant declaration.
	 *  @param node ConstantDeclaration node that get's visited.
	 */
	virtual void visit(ConstantDeclaration& node);

	/** Visit an FunctionCall.
	 *  @param node FunctionCall node that get's visited.
	 */
	virtual void visit(FunctionCall& node);

	/** Visit an IfStat.
	 *  @param node IfStat node that get's visited.
	 */
	virtual void visit(IfStat& node);

	/** Visit an NullStat.
	 *  @param node IfStat node that get's visited.
	 */
	virtual void visit(NullStat& node);

	/** Visit a ForLoopStat
	 *  @param node ForLoopStat node that get's visited.
	 */
	virtual void visit(ForLoopStat& node);

	/** Visit a WhileLoopStat
	 *  @param node WhileLoopStat node that get's visited.
	 */
	virtual void visit(WhileLoopStat& node);

	/** Visit a NextStat
	 *  @param node NextStat node that get's visited.
	 */
	virtual void visit(NextStat& node);

	/** Visit a VarAssignStat
	 *  @param node VarAssignStat node that get's visited.
	 */
	virtual void visit(VarAssignStat& node);

	/** Visit a WaitStat
	 *  @param node WaitStat node that get's visited.
	 */
	virtual void visit(WaitStat& node);

	/** Visit a ExitStat
	 *  @param node ExitStat node that get's visited.
	 */
	virtual void visit(ExitStat& node);

	/** Visit a SigAssignStat
	 *  @param node SigAssignStat node that get's visited.
	 */
	virtual void visit(SigAssignStat& node);

	/** Visit a WaveFormElem
	 *  @param node WaveFormElem node that get's visited.
	 */
	virtual void visit(WaveFormElem& node);

	/** Visit a ReturnStat
	 *  @param node ReturnStat node that get's visited.
	 */
	virtual void visit(ReturnStat& node);

	/** Visit a ProcCallStat
	 *  @param node ReturnStat node that get's visited.
	 */
	virtual void visit(ProcCallStat& node);

	/** Visit a AssertStat
	 *  @param node AssertStat node that get's visited.
	 */
	virtual void visit(AssertStat& node);

	/** Visit a VarDeclaration
	 *  @param node VarDeclaration node that get's visited.
	 */
	virtual void visit(VarDeclaration& node);

	/** Visit a DiscreteRange
	 *  @param node DiscreteRange node that get's visited.
	 */
	virtual void visit(DiscreteRange& node);

	/** Visit a CaseStat
	 *  @param node CaseStat node that get's visited.
	 */
	virtual void visit(CaseStat& node);

	/** Visit a CaseAlternative
	 *  @param node CaseAlternative node that get's visited.
	 */
	virtual void visit(CaseAlternative& node);

	/** Visit a Others node.
	 *  @param node Others node that get's visited.
	 */
	virtual void visit(Others& node);

	/** Visit a Architecture node.
	 *  @param node Architecture node that get's visited.
	 */
	virtual void visit(Architecture& node);

	/** Visit a AssociationElement node.
	 *  @param node AssociationElement node that get's visited.
	 */
	virtual void visit(AssociationElement& node);

	/** Visit a FunctionDeclaration node.
	 *  @param node FunctionDeclaration node that get's visited.
	 */
	virtual void visit(FunctionDeclaration& node);

	/** Visit a ProcedureDeclaration node.
	 *  @param node ProcedureDeclaration node that get's visited.
	 */
	virtual void visit(ProcedureDeclaration& node);

	/** Visit a CompInstStat node.
	 *  @param node CompInstStat node that get's visited.
	 */
	virtual void visit(CompInstStat& node);

	/** Visit a Package node.
	 *  @param node Package node that get's visited.
	 */
	virtual void visit(Package& node);

	/** Visit a PackageBody node.
	 *  @param node PackageBody node that get's visited.
	 */
	virtual void visit(PackageBody& node);

	/** Visit a Process node.
	 *  @param node Process node that get's visited.
	 */
	virtual void visit(Process& node);

	/** Visit a SubprogBody node.
	 *  @param node SubprogBody node that get's visited.
	 */
	virtual void visit(SubprogBody& node);

	/** Visit a CondalSigAssign node.
	 *  @param node CondalSigAssign node that get's visited.
	 */
	virtual void visit(CondalSigAssign& node);

	/** Visit an EnumerationType node.
	 *  @param node EnumerationType node that get's visited.
	 */
	virtual void visit(EnumerationType& node);

	/** Visit an PhysicalType node.
	 *  @param node PhysicalType node that get's visited.
	 */
	virtual void visit(PhysicalType& node);

	/** Visit an PhysicalTypeUnit node.
	 *  @param node PhysicalTypeUnit node that get's visited.
	 */
	virtual void visit(PhysicalTypeUnit& node);

	/** Visit an RangeConstraintType node.
	 *  @param node RangeConstraintType node that get's visited.
	 */
	virtual void visit(RangeConstraintType& node);

	/** Visit an UnconstrainedArrayType node.
	 *  @param node UnconstrainedArrayType node that get's visited.
	 */
	virtual void visit(UnconstrainedArrayType& node);

	/** Visit an RecordType node.
	 *  @param node RecordType node that get's visited.
	 */
	virtual void visit(RecordType& node);

	/** Visit an RecordTypeElement node.
	 *  @param node RecordTypeElement node that get's visited.
	 */
	virtual void visit(RecordTypeElement &node);

	/** Visit an Aggregate node.
	 *  @param node Aggregate node that get's visited.
	 */
	virtual void visit(Aggregate &node);

	/** Visit a SubtypeIndication node.
	 *  @param node SubtypeIndication node that get's visited.
	 */
	virtual void visit(SubtypeIndication &node);

	/** Visit a Library node.
	 *  @param node Library node that get's visited.
	 */
	virtual void visit(Library &node);

	/** Visit a LibraryList node.
	 *  @param node LibraryList node that get's visited.
	 */
	virtual void visit(LibraryList &node);

	/** Visit a Subscript node.
	 *  @param node Subscript node that get's visited.
	 */
	virtual void visit(Subscript &node);

	/** Visit a Slice node.
	 *  @param node Slice node that get's visited.
	 */
	virtual void visit(Slice &node);

	/** Visit a TypeConversion node.
	 *  @param node TypeConversion node that get's visited.
	 */
	virtual void visit(TypeConversion &node);

	/** Visit an AttributeDeclaration node.
	 *  @param node AttributeDeclaration node that gets visited.
	 */
	virtual void visit(AttributeDeclaration &node);

	/** Visit an AttributeSpecification node.
	 *  @param node AttributeSpecification node that gets visited.
	 */
	virtual void visit(AttributeSpecification &node);

public:
	//! write the dot-graph to stream.
	/** @param stream to which the dot graph should get written to.
          */
	void put(std::ostream &stream) const;

private:
	using TopDownVisitor::process;

	//! Process a generic ValDeclaration.
        /** This function will get called for each ValDeclaration (or class 
         *  derived from ValDeclaration) that get's visited.
         *
         *  @param node ValDeclaration instance.
         */
	virtual void process(ValDeclaration& node);

	//! Process a generic SymbolDeclaration.
        /** This function will get called for each SymbolDeclaration (or class
         *  derived from SymbolDeclaration) that get's visited.
         *
         *  @param node SymbolDeclaration instance.
         */
	virtual void process(SymbolDeclaration& node);

	//! Process a generic Expression.
        /** This function will get called for each Expression (or class
         *  derived from Expression) that get's visited.
         *
         *  @param node Expression instance.
         */
	virtual void process(Expression& node);

	//! Process a generic SeqStat.
        /** This function will get called for each SeqStat (or class
         *  derived from SeqStat) that get's visited.
         *
         *  @param node SeqStat instance.
         */
	virtual void process(SeqStat& node);

	//! Process a generic LoopStat.
        /** This function will get called for each LoopStat (or class
         *  derived from LoopStat) that get's visited.
         *
         *  @param node LoopStat instance.
         */
	virtual void process(LoopStat& node);

	//! Process a generic ConditionedStat.
        /** This function will get called for each ConditionedStat (or class
         *  derived from ConditionedStat) that get's visited.
         *
         *  @param node ConditionedStat instance.
         */
	virtual void process(ConditionedStat& node);

	//! Process a generic Callable.
        /** This function will get called for each Callable (or class
         *  derived from Callable) that get's visited.
         *
         *  @param node Callable instance.
         */
	virtual void process(Callable& node);

	//! Process a generic LibUnit.
        /** This function will get called for each LibUnit (or class
         *  derived from LibUnit) that get's visited.
         *
         *  @param node LibUnit instance.
         */
	virtual void process(LibUnit& node);

	//! Process a generic TypeDeclaration.
        /** This function will get called for each TypeDeclaration (or class
         *  derived from TypeDeclaration) that get's visited.
         *
         *  @param node TypeDeclaration instance.
         */
	virtual void process(TypeDeclaration& node);

	//! Process a generic Name.
        /** This function will get called for each Name (or class
         *  derived from Name) that get's visited.
         *
         *  @param node Name instance.
         */
	virtual void process(Name &node);

	//! Process a generic PrefixedName.
        /** This function will get called for each PrefixedName (or class
         *  derived from PrefixedName) that get's visited.
         *
         *  @param node PrefixedName instance.
         */
	virtual void process(PrefixedName &node);

	/** one edge in the graph */
	class Edge {
	public:
		/** @param source source number of node.
		 *  @param destination destination number of node.
		 */
		Edge(
			int source, 
			int destination
			) : 	from(source), 
				to(destination),
				label(NULL) {}

		/** @param source source number of node.
		 *  @param destination destination number of node.
		 *  @param lbl label of the edge.
		 */
		Edge(
			int source, 
			int destination,
			const char* lbl
			) : 	from(source), 
				to(destination),
				label(new std::string(lbl)) {}

		//! copy c'tor
		/** need to copy the string as well.
		 *  @param other other Edge instance */
		Edge(const Edge& other) : from(other.from), to(other.to) {
			if (other.label) {
				this->label = new std::string(*other.label);
			} else {
				this->label = NULL;
			}
		}

		~Edge();

		/** put the edge to stream.
		 *  @param stream to which the edge should be written. */
		void put(std::ostream& stream) const;

	private:
		/** origin of the edge */
		int from;	
		/** destination of the edge */
		int to;
		/** optional label of the edge */
		std::string* label;
	};

	/** one description of a node. */
	class NodeDescription {
	public:
		/** @param num unique number of the Description
		 *  @param n name of the node.
		 */
		NodeDescription(int num, std::string n) : id(num), name(n) {}

		/** add a name/value pair to the description.
		 *  @param n name of the name/value-pair.
		 *  @param value value of the name/value-pair.
		 */
		void add(std::string n, std::string value);

		/** add a simple string to the description.
                  * @param s string to add.
                  */
		void add(std::string s);

		/** put the node to a stream.
		 *  @param stream stream on which the node should get written.
		 */
		void put(std::ostream& stream) const;

		/** unique number of the node. */
		int id;
	private:
		/** name of the node. */
		std::string name;
		/** list of attributes */
		std::list<std::string> attributes;
	};
	
	/** generate edges for list of children.
          * @param fromId id of parent node.
          * @param l list of pointers to child nodes.
          */
	template <typename T>
	void makeEdges(int fromId, T l);

	/** generate edges for list of children.
          * @param fromId id of parent node.
          * @param l list of pointers to child nodes.
	  * @param s label of the edge.
          */
	template <typename T>
	void makeEdges(int fromId, T l, const char* s);

	/** create a new node description, assign it an id and set it to be 
	 *  the current description.
	 *  @param node currently visited AstNode.
	 *  @param desc descriptive string.
	 *  @return newly created NodeDescription.
	 */
	NodeDescription* makeDescription(AstNode& node, const char* desc);


	/** assign a new Id to an AstNode.
          * @param node node to which the number should get assigned to.
          * @return newly created id.
          */
	int assignId(AstNode& node);

	/** edges of the graph */
	std::list<Edge> edges;
	/** node of the graph */
	std::list<NodeDescription*> nodes;

	/** last unique id that was handed out */
	int idCounter;

	/** current NodeDescription */
	NodeDescription* currentDesc;
};
	
}; /* namespace ast */

#include "DotVisitor.tpp"

#endif /* __DOT_VISITOR_HPP_INCLUDED */
