//
// 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.
//
// TypedefTable.h -- 
//
// Author           : Rodolfo G. Esteves
// Created On       : Sat May 16 15:24:36 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Mon Aug 15 18:25:04 2016
// Update Count     : 28
//

#ifndef TYPEDEFTABLE_H
#define TYPEDEFTABLE_H

#include <map>
#include <list>
#include <string>
#include <stack>

#include "lex.h"
#include "parser.h"

class TypedefTable {
  public:
	enum kind_t { ID = IDENTIFIER, TD = TYPEDEFname, TG = TYPEGENname };
  private:
	struct Entry {
		int scope;
		kind_t kind;
	};
	
	struct DeferredEntry {
		std::string identifier;
		kind_t kind;
	};

	typedef std::map< std::string, std::list< Entry > > tableType;
	tableType table;

	int currentScope;
	std::string currentTrait;
	int contextScope;
	
	typedef std::list< DeferredEntry > deferListType;
	std::stack< deferListType > deferListStack;
	std::map< std::string, deferListType > contexts;
	
	std::stack< std::string > nextIdentifiers;

	void addToScope( const std::string &identifier, kind_t kind, int scope );
  public:
	TypedefTable();

	bool exists( const std::string &identifier );
	int isKind( const std::string &identifier ) const;
	void changeKind( const std::string &identifier, kind_t kind );

	void makeTypedef( const std::string &name );

	// "addToCurrentScope" adds the identifier/type pair to the current scope. This does less than you think it does,
	// since each declaration is within its own scope.  Mostly useful for type parameters.
	void addToCurrentScope( const std::string &identifier, kind_t kind );
	void addToCurrentScope( kind_t kind );			// use nextIdentifiers.top()

	// "addToEnclosingScope" adds the identifier/type pair to the scope that encloses the current one.  This is the
	// right way to handle type and typedef names
	void addToEnclosingScope( const std::string &identifier, kind_t kind );
	void addToEnclosingScope( kind_t kind );		// use nextIdentifiers.top()
	
	// "addToEnclosingScope2" adds the identifier/type pair to the scope that encloses the scope enclosing the the
	// current one.  This is the right way to handle assertion names
	void addToEnclosingScope2( const std::string &identifier, kind_t kind );
	void addToEnclosingScope2( kind_t kind );		// use nextIdentifiers.top()
	
	// set the next identifier to be used by an "add" operation without an identifier parameter within the current scope
	void setNextIdentifier( const std::string &identifier );
	
	// dump the definitions from a pre-defined context into the current scope
	void openTrait( const std::string &contextName );
	
	void enterScope();
	void leaveScope();
	void enterTrait( const std::string &contextName );
	void leaveTrait();

	void print() const;
};

#endif // TYPEDEFTABLE_H

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