//
// 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.
//
// Indexer.cc --
//
// Author           : Richard C. Bilson
// Created On       : Sun May 17 21:37:33 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Thu Mar 30 16:38:47 2017
// Update Count     : 19
//

#include "Indexer.h"

#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <algorithm>

#include "Mangler.h"

#include "Common/utility.h"

#include "ResolvExpr/typeops.h"

#include "SynTree/Declaration.h"
#include "SynTree/Type.h"
#include "SynTree/Expression.h"
#include "SynTree/Initializer.h"
#include "SynTree/Statement.h"

#include "InitTweak/InitTweak.h"

#define debugPrint(x) if ( doDebug ) { std::cout << x; }

namespace SymTab {
	struct NewScope {
		NewScope( SymTab::Indexer & indexer ) : indexer( indexer ) { indexer.enterScope(); }
		~NewScope() { indexer.leaveScope(); }
		SymTab::Indexer & indexer;
	};

	template< typename TreeType, typename VisitorType >
	inline void acceptNewScope( TreeType *tree, VisitorType &visitor ) {
		visitor.enterScope();
		maybeAccept( tree, visitor );
		visitor.leaveScope();
	}

	typedef std::unordered_map< std::string, DeclarationWithType* > MangleTable;
	typedef std::unordered_map< std::string, MangleTable > IdTable;
	typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable;
	typedef std::unordered_map< std::string, StructDecl* > StructTable;
	typedef std::unordered_map< std::string, EnumDecl* > EnumTable;
	typedef std::unordered_map< std::string, UnionDecl* > UnionTable;
	typedef std::unordered_map< std::string, TraitDecl* > TraitTable;

	void dump( const IdTable &table, std::ostream &os ) {
		for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) {
			for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) {
				os << mangle->second << std::endl;
			}
		}
	}

	template< typename Decl >
	void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) {
		for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) {
			os << it->second << std::endl;
		} // for
	}

	struct Indexer::Impl {
		Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(),
				idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
		Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ),
				idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
		unsigned long refCount;   ///< Number of references to these tables
		unsigned long scope;      ///< Scope these tables are associated with
		unsigned long size;       ///< Number of elements stored in this table
		const Indexer base;       ///< Base indexer this extends

		IdTable idTable;          ///< Identifier namespace
		TypeTable typeTable;      ///< Type namespace
		StructTable structTable;  ///< Struct namespace
		EnumTable enumTable;      ///< Enum namespace
		UnionTable unionTable;    ///< Union namespace
		TraitTable traitTable;    ///< Trait namespace
	};

	Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) {
		if ( ! toClone ) return 0;

		// shorten the search chain by skipping empty links
		Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone;
		if ( ret ) { ++ret->refCount; }

		return ret;
	}

	void Indexer::deleteRef( Indexer::Impl *toFree ) {
		if ( ! toFree ) return;

		if ( --toFree->refCount == 0 ) delete toFree;
	}

	void Indexer::removeSpecialOverrides( const std::string &id, std::list< DeclarationWithType * > & out ) const {
		// only need to perform this step for constructors, destructors, and assignment functions
		if ( ! InitTweak::isCtorDtorAssign( id ) ) return;

		// helpful data structure
		struct ValueType {
			struct DeclBall {
				FunctionDecl * decl;
				bool isUserDefinedFunc; // properties for this particular decl
				bool isDefaultCtor;
				bool isDtor;
				bool isCopyFunc;
			};
			// properties for this type
			bool userDefinedFunc = false; // any user-defined function found
			bool userDefinedCtor = false; // any user-defined constructor found
			bool userDefinedDtor = false; // any user-defined destructor found
			bool userDefinedCopyFunc = false; // user-defined copy ctor found
			std::list< DeclBall > decls;

			// another FunctionDecl for the current type was found - determine
			// if it has special properties and update data structure accordingly
			ValueType & operator+=( FunctionDecl * function ) {
				bool isUserDefinedFunc = ! LinkageSpec::isOverridable( function->get_linkage() );
				bool isDefaultCtor = InitTweak::isDefaultConstructor( function );
				bool isDtor = InitTweak::isDestructor( function );
				bool isCopyFunc = InitTweak::isCopyFunction( function, function->get_name() );
				decls.push_back( DeclBall{ function, isUserDefinedFunc, isDefaultCtor, isDtor, isCopyFunc } );
				userDefinedFunc = userDefinedFunc || isUserDefinedFunc;
				userDefinedCtor = userDefinedCtor || (isUserDefinedFunc && InitTweak::isConstructor( function->get_name() ) );
				userDefinedDtor = userDefinedDtor || (isUserDefinedFunc && isDtor);
				userDefinedCopyFunc = userDefinedCopyFunc || (isUserDefinedFunc && isCopyFunc);
				return *this;
			}
		}; // ValueType

		std::list< DeclarationWithType * > copy;
		copy.splice( copy.end(), out );

		// organize discovered declarations by type
		std::unordered_map< std::string, ValueType > funcMap;
		for ( DeclarationWithType * decl : copy ) {
			if ( FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ) ) {
				std::list< DeclarationWithType * > & params = function->get_functionType()->get_parameters();
				assert( ! params.empty() );
				// use base type of pointer, so that qualifiers on the pointer type aren't considered.
				Type * base = safe_dynamic_cast< PointerType * >( params.front()->get_type() )->get_base();
				funcMap[ Mangler::mangle( base ) ] += function;
			} else {
				out.push_back( decl );
			}
		}

		// if a type contains user defined ctor/dtors, then special rules trigger, which determine
		// the set of ctor/dtors that are seen by the requester. In particular, if the user defines
		// a default ctor, then the generated default ctor should never be seen, likewise for copy ctor
		// and dtor. If the user defines any ctor/dtor, then no generated field ctors should be seen.
		// If the user defines any ctor then the generated default ctor should not be seen.
		for ( std::pair< const std::string, ValueType > & pair : funcMap ) {
			ValueType & val = pair.second;
			for ( ValueType::DeclBall ball : val.decls ) {
				if ( ! val.userDefinedFunc || ball.isUserDefinedFunc || (! val.userDefinedCtor && ball.isDefaultCtor) || (! val.userDefinedCopyFunc && ball.isCopyFunc) || (! val.userDefinedDtor && ball.isDtor) ) {
					// decl conforms to the rules described above, so it should be seen by the requester
					out.push_back( ball.decl );
				}
			}
		}
	}

	void Indexer::makeWritable() {
		if ( ! tables ) {
			// create indexer if not yet set
			tables = new Indexer::Impl( scope );
		} else if ( tables->refCount > 1 || tables->scope != scope ) {
			// make this indexer the base of a fresh indexer at the current scope
			tables = new Indexer::Impl( scope, std::move( *this ) );
		}
	}

	Indexer::Indexer( bool _doDebug ) : tables( 0 ), scope( 0 ), doDebug( _doDebug ) {}

	Indexer::Indexer( const Indexer &that ) : tables( newRef( that.tables ) ), scope( that.scope ), doDebug( that.doDebug ) {}

	Indexer::Indexer( Indexer &&that ) : tables( that.tables ), scope( that.scope ), doDebug( that.doDebug ) {
		that.tables = 0;
	}

	Indexer::~Indexer() {
		deleteRef( tables );
	}

	Indexer& Indexer::operator= ( const Indexer &that ) {
		deleteRef( tables );

		tables = newRef( that.tables );
		scope = that.scope;
		doDebug = that.doDebug;

		return *this;
	}

	Indexer& Indexer::operator= ( Indexer &&that ) {
		deleteRef( tables );

		tables = that.tables;
		scope = that.scope;
		doDebug = that.doDebug;

		that.tables = 0;

		return *this;
	}

	void Indexer::visit( ObjectDecl *objectDecl ) {
		enterScope();
		maybeAccept( objectDecl->get_type(), *this );
		leaveScope();
		maybeAccept( objectDecl->get_init(), *this );
		maybeAccept( objectDecl->get_bitfieldWidth(), *this );
		if ( objectDecl->get_name() != "" ) {
			debugPrint( "Adding object " << objectDecl->get_name() << std::endl );
			addId( objectDecl );
		} // if
	}

	void Indexer::visit( FunctionDecl *functionDecl ) {
		if ( functionDecl->get_name() == "" ) return;
		debugPrint( "Adding function " << functionDecl->get_name() << std::endl );
		addId( functionDecl );
		enterScope();
		maybeAccept( functionDecl->get_functionType(), *this );
		maybeAccept( functionDecl->get_statements(), *this );
		leaveScope();
	}


// A NOTE ON THE ORDER OF TRAVERSAL
//
// Types and typedefs have their base types visited before they are added to the type table.  This is ok, since there is
// no such thing as a recursive type or typedef.
//
//             typedef struct { T *x; } T; // never allowed
//
// for structs/unions, it is possible to have recursion, so the decl should be added as if it's incomplete to begin, the
// members are traversed, and then the complete type should be added (assuming the type is completed by this particular
// declaration).
//
//             struct T { struct T *x; }; // allowed
//
// It is important to add the complete type to the symbol table *after* the members/base has been traversed, since that
// traversal may modify the definition of the type and these modifications should be visible when the symbol table is
// queried later in this pass.
//
// TODO: figure out whether recursive contexts are sensible/possible/reasonable.


	void Indexer::visit( TypeDecl *typeDecl ) {
		// see A NOTE ON THE ORDER OF TRAVERSAL, above
		// note that assertions come after the type is added to the symtab, since they are not part of the type proper
		// and may depend on the type itself
		enterScope();
		acceptAll( typeDecl->get_parameters(), *this );
		maybeAccept( typeDecl->get_base(), *this );
		leaveScope();
		debugPrint( "Adding type " << typeDecl->get_name() << std::endl );
		addType( typeDecl );
		acceptAll( typeDecl->get_assertions(), *this );
	}

	void Indexer::visit( TypedefDecl *typeDecl ) {
		enterScope();
		acceptAll( typeDecl->get_parameters(), *this );
		maybeAccept( typeDecl->get_base(), *this );
		leaveScope();
		debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl );
		addType( typeDecl );
	}

	void Indexer::visit( StructDecl *aggregateDecl ) {
		// make up a forward declaration and add it before processing the members
		// needs to be on the heap because addStruct saves the pointer
		StructDecl &fwdDecl = *new StructDecl( aggregateDecl->get_name() );
		cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
		debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl );
		addStruct( &fwdDecl );

		enterScope();
		acceptAll( aggregateDecl->get_parameters(), *this );
		acceptAll( aggregateDecl->get_members(), *this );
		leaveScope();

		debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl );
		// this addition replaces the forward declaration
		addStruct( aggregateDecl );
	}

	void Indexer::visit( UnionDecl *aggregateDecl ) {
		// make up a forward declaration and add it before processing the members
		UnionDecl fwdDecl( aggregateDecl->get_name() );
		cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
		debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl );
		addUnion( &fwdDecl );

		enterScope();
		acceptAll( aggregateDecl->get_parameters(), *this );
		acceptAll( aggregateDecl->get_members(), *this );
		leaveScope();

		debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl );
		addUnion( aggregateDecl );
	}

	void Indexer::visit( EnumDecl *aggregateDecl ) {
		debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl );
		addEnum( aggregateDecl );
		// unlike structs, contexts, and unions, enums inject their members into the global scope
		acceptAll( aggregateDecl->get_members(), *this );
	}

	void Indexer::visit( TraitDecl *aggregateDecl ) {
		enterScope();
		acceptAll( aggregateDecl->get_parameters(), *this );
		acceptAll( aggregateDecl->get_members(), *this );
		leaveScope();

		debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl );
		addTrait( aggregateDecl );
	}

	void Indexer::visit( CompoundStmt *compoundStmt ) {
		enterScope();
		acceptAll( compoundStmt->get_kids(), *this );
		leaveScope();
	}


	void Indexer::visit( ApplicationExpr *applicationExpr ) {
		acceptNewScope( applicationExpr->get_result(), *this );
		maybeAccept( applicationExpr->get_function(), *this );
		acceptAll( applicationExpr->get_args(), *this );
	}

	void Indexer::visit( UntypedExpr *untypedExpr ) {
		acceptNewScope( untypedExpr->get_result(), *this );
		acceptAll( untypedExpr->get_args(), *this );
	}

	void Indexer::visit( NameExpr *nameExpr ) {
		acceptNewScope( nameExpr->get_result(), *this );
	}

	void Indexer::visit( AddressExpr *addressExpr ) {
		acceptNewScope( addressExpr->get_result(), *this );
		maybeAccept( addressExpr->get_arg(), *this );
	}

	void Indexer::visit( LabelAddressExpr *labAddressExpr ) {
		acceptNewScope( labAddressExpr->get_result(), *this );
		maybeAccept( labAddressExpr->get_arg(), *this );
	}

	void Indexer::visit( CastExpr *castExpr ) {
		acceptNewScope( castExpr->get_result(), *this );
		maybeAccept( castExpr->get_arg(), *this );
	}

	void Indexer::visit( UntypedMemberExpr *memberExpr ) {
		acceptNewScope( memberExpr->get_result(), *this );
		maybeAccept( memberExpr->get_aggregate(), *this );
	}

	void Indexer::visit( MemberExpr *memberExpr ) {
		acceptNewScope( memberExpr->get_result(), *this );
		maybeAccept( memberExpr->get_aggregate(), *this );
	}

	void Indexer::visit( VariableExpr *variableExpr ) {
		acceptNewScope( variableExpr->get_result(), *this );
	}

	void Indexer::visit( ConstantExpr *constantExpr ) {
		acceptNewScope( constantExpr->get_result(), *this );
		maybeAccept( constantExpr->get_constant(), *this );
	}

	void Indexer::visit( SizeofExpr *sizeofExpr ) {
		acceptNewScope( sizeofExpr->get_result(), *this );
		if ( sizeofExpr->get_isType() ) {
			maybeAccept( sizeofExpr->get_type(), *this );
		} else {
			maybeAccept( sizeofExpr->get_expr(), *this );
		}
	}

	void Indexer::visit( AlignofExpr *alignofExpr ) {
		acceptNewScope( alignofExpr->get_result(), *this );
		if ( alignofExpr->get_isType() ) {
			maybeAccept( alignofExpr->get_type(), *this );
		} else {
			maybeAccept( alignofExpr->get_expr(), *this );
		}
	}

	void Indexer::visit( UntypedOffsetofExpr *offsetofExpr ) {
		acceptNewScope( offsetofExpr->get_result(), *this );
		maybeAccept( offsetofExpr->get_type(), *this );
	}

	void Indexer::visit( OffsetofExpr *offsetofExpr ) {
		acceptNewScope( offsetofExpr->get_result(), *this );
		maybeAccept( offsetofExpr->get_type(), *this );
		maybeAccept( offsetofExpr->get_member(), *this );
	}

	void Indexer::visit( OffsetPackExpr *offsetPackExpr ) {
		acceptNewScope( offsetPackExpr->get_result(), *this );
		maybeAccept( offsetPackExpr->get_type(), *this );
	}

	void Indexer::visit( AttrExpr *attrExpr ) {
		acceptNewScope( attrExpr->get_result(), *this );
		if ( attrExpr->get_isType() ) {
			maybeAccept( attrExpr->get_type(), *this );
		} else {
			maybeAccept( attrExpr->get_expr(), *this );
		}
	}

	void Indexer::visit( LogicalExpr *logicalExpr ) {
		acceptNewScope( logicalExpr->get_result(), *this );
		maybeAccept( logicalExpr->get_arg1(), *this );
		maybeAccept( logicalExpr->get_arg2(), *this );
	}

	void Indexer::visit( ConditionalExpr *conditionalExpr ) {
		acceptNewScope( conditionalExpr->get_result(), *this );
		maybeAccept( conditionalExpr->get_arg1(), *this );
		maybeAccept( conditionalExpr->get_arg2(), *this );
		maybeAccept( conditionalExpr->get_arg3(), *this );
	}

	void Indexer::visit( CommaExpr *commaExpr ) {
		acceptNewScope( commaExpr->get_result(), *this );
		maybeAccept( commaExpr->get_arg1(), *this );
		maybeAccept( commaExpr->get_arg2(), *this );
	}

	void Indexer::visit( TypeExpr *typeExpr ) {
		acceptNewScope( typeExpr->get_result(), *this );
		maybeAccept( typeExpr->get_type(), *this );
	}

	void Indexer::visit( AsmExpr *asmExpr ) {
		maybeAccept( asmExpr->get_inout(), *this );
		maybeAccept( asmExpr->get_constraint(), *this );
		maybeAccept( asmExpr->get_operand(), *this );
	}

	void Indexer::visit( ImplicitCopyCtorExpr *impCpCtorExpr ) {
		acceptNewScope( impCpCtorExpr->get_result(), *this );
		maybeAccept( impCpCtorExpr->get_callExpr(), *this );
		acceptAll( impCpCtorExpr->get_tempDecls(), *this );
		acceptAll( impCpCtorExpr->get_returnDecls(), *this );
		acceptAll( impCpCtorExpr->get_dtors(), *this );
	}

	void Indexer::visit( ConstructorExpr * ctorExpr ) {
		acceptNewScope( ctorExpr->get_result(), *this );
		maybeAccept( ctorExpr->get_callExpr(), *this );
	}

	void Indexer::visit( CompoundLiteralExpr *compLitExpr ) {
		acceptNewScope( compLitExpr->get_result(), *this );
		maybeAccept( compLitExpr->get_initializer(), *this );
	}

	void Indexer::visit( UntypedValofExpr *valofExpr ) {
		acceptNewScope( valofExpr->get_result(), *this );
		maybeAccept( valofExpr->get_body(), *this );
	}

	void Indexer::visit( RangeExpr *rangeExpr ) {
		maybeAccept( rangeExpr->get_low(), *this );
		maybeAccept( rangeExpr->get_high(), *this );
	}

	void Indexer::visit( UntypedTupleExpr *tupleExpr ) {
		acceptNewScope( tupleExpr->get_result(), *this );
		acceptAll( tupleExpr->get_exprs(), *this );
	}

	void Indexer::visit( TupleExpr *tupleExpr ) {
		acceptNewScope( tupleExpr->get_result(), *this );
		acceptAll( tupleExpr->get_exprs(), *this );
	}

	void Indexer::visit( TupleIndexExpr *tupleExpr ) {
		acceptNewScope( tupleExpr->get_result(), *this );
		maybeAccept( tupleExpr->get_tuple(), *this );
	}

	void Indexer::visit( MemberTupleExpr *tupleExpr ) {
		acceptNewScope( tupleExpr->get_result(), *this );
		maybeAccept( tupleExpr->get_member(), *this );
		maybeAccept( tupleExpr->get_aggregate(), *this );
	}

	void Indexer::visit( TupleAssignExpr *tupleExpr ) {
		acceptNewScope( tupleExpr->get_result(), *this );
		maybeAccept( tupleExpr->get_stmtExpr(), *this );
	}

	void Indexer::visit( StmtExpr *stmtExpr ) {
		acceptNewScope( stmtExpr->get_result(), *this );
		maybeAccept( stmtExpr->get_statements(), *this );
		acceptAll( stmtExpr->get_returnDecls(), *this );
		acceptAll( stmtExpr->get_dtors(), *this );
	}

	void Indexer::visit( UniqueExpr *uniqueExpr ) {
		acceptNewScope( uniqueExpr->get_result(), *this );
		maybeAccept( uniqueExpr->get_expr(), *this );
	}


	void Indexer::visit( TraitInstType *contextInst ) {
		acceptAll( contextInst->get_parameters(), *this );
		acceptAll( contextInst->get_members(), *this );
	}

	void Indexer::visit( StructInstType *structInst ) {
		if ( ! lookupStruct( structInst->get_name() ) ) {
			debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl );
			addStruct( structInst->get_name() );
		}
		enterScope();
		acceptAll( structInst->get_parameters(), *this );
		leaveScope();
	}

	void Indexer::visit( UnionInstType *unionInst ) {
		if ( ! lookupUnion( unionInst->get_name() ) ) {
			debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl );
			addUnion( unionInst->get_name() );
		}
		enterScope();
		acceptAll( unionInst->get_parameters(), *this );
		leaveScope();
	}

	void Indexer::visit( ForStmt *forStmt ) {
	    // for statements introduce a level of scope
	    enterScope();
	    Visitor::visit( forStmt );
	    leaveScope();
	}



	void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const {
		std::unordered_set< std::string > foundMangleNames;

		Indexer::Impl *searchTables = tables;
		while ( searchTables ) {

			IdTable::const_iterator decls = searchTables->idTable.find( id );
			if ( decls != searchTables->idTable.end() ) {
				const MangleTable &mangleTable = decls->second;
				for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
					// mark the mangled name as found, skipping this insertion if a declaration for that name has already been found
					if ( foundMangleNames.insert( decl->first ).second == false ) continue;

					out.push_back( decl->second );
				}
			}

			// get declarations from base indexers
			searchTables = searchTables->base.tables;
		}

		// some special functions, e.g. constructors and destructors
		// remove autogenerated functions when they are defined so that
		// they can never be matched
		removeSpecialOverrides( id, out );
	}

	NamedTypeDecl *Indexer::lookupType( const std::string &id ) const {
		if ( ! tables ) return 0;

		TypeTable::const_iterator ret = tables->typeTable.find( id );
		return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id );
	}

	StructDecl *Indexer::lookupStruct( const std::string &id ) const {
		if ( ! tables ) return 0;

		StructTable::const_iterator ret = tables->structTable.find( id );
		return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id );
	}

	EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
		if ( ! tables ) return 0;

		EnumTable::const_iterator ret = tables->enumTable.find( id );
		return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id );
	}

	UnionDecl *Indexer::lookupUnion( const std::string &id ) const {
		if ( ! tables ) return 0;

		UnionTable::const_iterator ret = tables->unionTable.find( id );
		return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id );
	}

	TraitDecl *Indexer::lookupTrait( const std::string &id ) const {
		if ( ! tables ) return 0;

		TraitTable::const_iterator ret = tables->traitTable.find( id );
		return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id );
	}

	DeclarationWithType *Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
		if ( ! tables ) return 0;
		if ( tables->scope < scope ) return 0;

		IdTable::const_iterator decls = tables->idTable.find( id );
		if ( decls != tables->idTable.end() ) {
			const MangleTable &mangleTable = decls->second;
			MangleTable::const_iterator decl = mangleTable.find( mangleName );
			if ( decl != mangleTable.end() ) return decl->second;
		}

		return tables->base.lookupIdAtScope( id, mangleName, scope );
	}

	bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
		if ( ! tables ) return false;
		if ( tables->scope < scope ) return false;

		IdTable::const_iterator decls = tables->idTable.find( id );
		if ( decls != tables->idTable.end() ) {
			const MangleTable &mangleTable = decls->second;
			for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
				// check for C decls with the same name, skipping those with a compatible type (by mangleName)
				if ( decl->second->get_linkage() == LinkageSpec::C && decl->first != mangleName ) return true;
			}
		}

		return tables->base.hasIncompatibleCDecl( id, mangleName, scope );
	}

	bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
		if ( ! tables ) return false;
		if ( tables->scope < scope ) return false;

		IdTable::const_iterator decls = tables->idTable.find( id );
		if ( decls != tables->idTable.end() ) {
			const MangleTable &mangleTable = decls->second;
			for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
				// check for C decls with the same name, skipping
				// those with an incompatible type (by mangleName)
				if ( decl->second->get_linkage() == LinkageSpec::C && decl->first == mangleName ) return true;
			}
		}

		return tables->base.hasCompatibleCDecl( id, mangleName, scope );
	}

	NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const {
		if ( ! tables ) return 0;
		if ( tables->scope < scope ) return 0;

		TypeTable::const_iterator ret = tables->typeTable.find( id );
		return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope );
	}

	StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const {
		if ( ! tables ) return 0;
		if ( tables->scope < scope ) return 0;

		StructTable::const_iterator ret = tables->structTable.find( id );
		return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope );
	}

	EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const {
		if ( ! tables ) return 0;
		if ( tables->scope < scope ) return 0;

		EnumTable::const_iterator ret = tables->enumTable.find( id );
		return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope );
	}

	UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const {
		if ( ! tables ) return 0;
		if ( tables->scope < scope ) return 0;

		UnionTable::const_iterator ret = tables->unionTable.find( id );
		return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope );
	}

	TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const {
		if ( ! tables ) return 0;
		if ( tables->scope < scope ) return 0;

		TraitTable::const_iterator ret = tables->traitTable.find( id );
		return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope );
	}

	bool addedIdConflicts( DeclarationWithType *existing, DeclarationWithType *added ) {
		// if we're giving the same name mangling to things of different types then there is something wrong
		assert( (dynamic_cast<ObjectDecl*>( added ) && dynamic_cast<ObjectDecl*>( existing ) )
			|| (dynamic_cast<FunctionDecl*>( added ) && dynamic_cast<FunctionDecl*>( existing ) ) );

		if ( LinkageSpec::isOverridable( existing->get_linkage() ) ) {
			// new definition shadows the autogenerated one, even at the same scope
			return false;
		} else if ( added->get_linkage() != LinkageSpec::C || ResolvExpr::typesCompatible( added->get_type(), existing->get_type(), Indexer() ) ) {
			// typesCompatible doesn't really do the right thing here. When checking compatibility of function types,
			// we should ignore outermost pointer qualifiers, except _Atomic?
			FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( added );
			FunctionDecl *oldentry = dynamic_cast< FunctionDecl* >( existing );
			if ( newentry && oldentry ) {
				if ( newentry->get_statements() && oldentry->get_statements() ) {
					throw SemanticError( "duplicate function definition for ", added );
				} // if
			} else {
				// two objects with the same mangled name defined in the same scope.
				// both objects must be marked extern or both must be intrinsic for this to be okay
				// xxx - perhaps it's actually if either is intrinsic then this is okay?
				//       might also need to be same storage class?
				ObjectDecl *newobj = dynamic_cast< ObjectDecl* >( added );
				ObjectDecl *oldobj = dynamic_cast< ObjectDecl* >( existing );
				if ( ! newobj->get_storageClasses().is_extern && ! oldobj->get_storageClasses().is_extern ) {
					throw SemanticError( "duplicate object definition for ", added );
				} // if
			} // if
		} else {
			throw SemanticError( "duplicate definition for ", added );
		} // if

		return true;
	}

	void Indexer::addId( DeclarationWithType *decl ) {
		makeWritable();

		const std::string &name = decl->get_name();
		std::string mangleName;
		if ( LinkageSpec::isOverridable( decl->get_linkage() ) ) {
			// mangle the name without including the appropriate suffix, so overridable routines are placed into the
			// same "bucket" as their user defined versions.
			mangleName = Mangler::mangle( decl, false );
		} else {
			mangleName = Mangler::mangle( decl );
		} // if

		// this ensures that no two declarations with the same unmangled name at the same scope both have C linkage
		if ( decl->get_linkage() == LinkageSpec::C ) {
			// NOTE this is broken in Richard's original code in such a way that it never triggers (it
			// doesn't check decls that have the same manglename, and all C-linkage decls are defined to
			// have their name as their manglename, hence the error can never trigger).
			// The code here is closer to correct, but name mangling would have to be completely
			// isomorphic to C type-compatibility, which it may not be.
			if ( hasIncompatibleCDecl( name, mangleName, scope ) ) {
				throw SemanticError( "conflicting overload of C function ", decl );
			}
		} else {
			// Check that a Cforall declaration doesn't overload any C declaration
			if ( hasCompatibleCDecl( name, mangleName, scope ) ) {
				throw SemanticError( "Cforall declaration hides C function ", decl );
			}
		}

		// Skip repeat declarations of the same identifier
		DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope );
		if ( existing && addedIdConflicts( existing, decl ) ) return;

		// add to indexer
		tables->idTable[ name ][ mangleName ] = decl;
		++tables->size;
	}

	bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) {
		if ( existing->get_base() == 0 ) {
			return false;
		} else if ( added->get_base() == 0 ) {
			return true;
		} else {
			throw SemanticError( "redeclaration of ", added );
		}
	}

	void Indexer::addType( NamedTypeDecl *decl ) {
		makeWritable();

		const std::string &id = decl->get_name();
		TypeTable::iterator existing = tables->typeTable.find( id );
		if ( existing == tables->typeTable.end() ) {
			NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope );
			if ( ! parent || ! addedTypeConflicts( parent, decl ) ) {
				tables->typeTable.insert( existing, std::make_pair( id, decl ) );
				++tables->size;
			}
		} else {
			if ( ! addedTypeConflicts( existing->second, decl ) ) {
				existing->second = decl;
			}
		}
	}

	bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) {
		if ( existing->get_members().empty() ) {
			return false;
		} else if ( ! added->get_members().empty() ) {
			throw SemanticError( "redeclaration of ", added );
		} // if
		return true;
	}

	void Indexer::addStruct( const std::string &id ) {
		addStruct( new StructDecl( id ) );
	}

	void Indexer::addStruct( StructDecl *decl ) {
		makeWritable();

		const std::string &id = decl->get_name();
		StructTable::iterator existing = tables->structTable.find( id );
		if ( existing == tables->structTable.end() ) {
			StructDecl *parent = tables->base.lookupStructAtScope( id, scope );
			if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
				tables->structTable.insert( existing, std::make_pair( id, decl ) );
				++tables->size;
			}
		} else {
			if ( ! addedDeclConflicts( existing->second, decl ) ) {
				existing->second = decl;
			}
		}
	}

	void Indexer::addEnum( EnumDecl *decl ) {
		makeWritable();

		const std::string &id = decl->get_name();
		EnumTable::iterator existing = tables->enumTable.find( id );
		if ( existing == tables->enumTable.end() ) {
			EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope );
			if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
				tables->enumTable.insert( existing, std::make_pair( id, decl ) );
				++tables->size;
			}
		} else {
			if ( ! addedDeclConflicts( existing->second, decl ) ) {
				existing->second = decl;
			}
		}
	}

	void Indexer::addUnion( const std::string &id ) {
		addUnion( new UnionDecl( id ) );
	}

	void Indexer::addUnion( UnionDecl *decl ) {
		makeWritable();

		const std::string &id = decl->get_name();
		UnionTable::iterator existing = tables->unionTable.find( id );
		if ( existing == tables->unionTable.end() ) {
			UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope );
			if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
				tables->unionTable.insert( existing, std::make_pair( id, decl ) );
				++tables->size;
			}
		} else {
			if ( ! addedDeclConflicts( existing->second, decl ) ) {
				existing->second = decl;
			}
		}
	}

	void Indexer::addTrait( TraitDecl *decl ) {
		makeWritable();

		const std::string &id = decl->get_name();
		TraitTable::iterator existing = tables->traitTable.find( id );
		if ( existing == tables->traitTable.end() ) {
			TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope );
			if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
				tables->traitTable.insert( existing, std::make_pair( id, decl ) );
				++tables->size;
			}
		} else {
			if ( ! addedDeclConflicts( existing->second, decl ) ) {
				existing->second = decl;
			}
		}
	}

	void Indexer::enterScope() {
		++scope;

		if ( doDebug ) {
			std::cout << "--- Entering scope " << scope << std::endl;
		}
	}

	void Indexer::leaveScope() {
		using std::cout;

		assert( scope > 0 && "cannot leave initial scope" );
		--scope;

		while ( tables && tables->scope > scope ) {
			if ( doDebug ) {
				cout << "--- Leaving scope " << tables->scope << " containing" << std::endl;
				dump( tables->idTable, cout );
				dump( tables->typeTable, cout );
				dump( tables->structTable, cout );
				dump( tables->enumTable, cout );
				dump( tables->unionTable, cout );
				dump( tables->traitTable, cout );
			}

			// swap tables for base table until we find one at an appropriate scope
			Indexer::Impl *base = newRef( tables->base.tables );
			deleteRef( tables );
			tables = base;
		}
	}

	void Indexer::print( std::ostream &os, int indent ) const {
	    using std::cerr;

		if ( tables ) {
			os << "--- scope " << tables->scope << " ---" << std::endl;

			os << "===idTable===" << std::endl;
			dump( tables->idTable, os );
			os << "===typeTable===" << std::endl;
			dump( tables->typeTable, os );
			os << "===structTable===" << std::endl;
			dump( tables->structTable, os );
			os << "===enumTable===" << std::endl;
			dump( tables->enumTable, os );
			os << "===unionTable===" << std::endl;
			dump( tables->unionTable, os );
			os << "===contextTable===" << std::endl;
			dump( tables->traitTable, os );

			tables->base.print( os, indent );
		} else {
			os << "--- end ---" << std::endl;
		}

	}
} // namespace SymTab

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