//
// 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.
//
// GenPoly.cc -- 
//
// Author           : Richard C. Bilson
// Created On       : Mon May 18 07:44:20 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Tue Dec 15 16:11:18 2015
// Update Count     : 13
//

#include "GenPoly.h"

#include "SymTab/Mangler.h"
#include "SynTree/Expression.h"
#include "SynTree/Type.h"

#include <iostream>
using namespace std;

namespace GenPoly {
	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
		if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
			return true;
		} // if
		for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
			if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
				return true;
			} // if
		} // for
		return false;
	}

	bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
		bool doTransform = false;
		if ( ! function->get_returnVals().empty() ) {
			if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
	
				// figure out if the return type is specified by a type parameter
				for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
					if ( (*tyVar)->get_name() == typeInst->get_name() ) {
						doTransform = true;
						name = typeInst->get_name();
						break;
					} // if
				} // for
				if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
					doTransform = true;
				} // if
			} // if
		} // if
		return doTransform;
	}

	bool isPolyRet( FunctionType *function, std::string &name ) {
		TyVarMap dummyTyVars;
		return isPolyRet( function, name, dummyTyVars );
	}

	bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
		std::string dummyString;
		return isPolyRet( function, dummyString, otherTyVars );
	}

	namespace {
		/// Checks a parameter list for polymorphic parameters
		bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
			for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
				TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
				assert(paramType && "Aggregate parameters should be type expressions");
				if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
			}
			return false;
		}
	}
	
	Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
		if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
			if ( env ) {
				if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
					return isPolyType( newType, tyVars, env );
			} // if
		} // if
			if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
				return type;
	}
		} else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
			if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
		} else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
			if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
		}
		return 0;
	}

	Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
		if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
			return isPolyType( ptr->get_base(), tyVars, env );
		} else if ( env ) {
			if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
				if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
					return isPolyPtr( newType, tyVars, env );
				} // if
			} // if
		} // if
		return 0;
	}

	void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
		for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
			os << i->first << " (" << i->second << ") ";
		} // for
		os << std::endl;
	}

	std::string sizeofName( Type *ty ) {
		return std::string( "_sizeof_" ) + SymTab::Mangler::mangle( ty, false, false );
	}

	std::string alignofName( Type *ty ) {
		return std::string( "_alignof_" ) + SymTab::Mangler::mangle( ty, false, false );
	}
} // namespace GenPoly

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