//
// 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.
//
// Specialize.cc --
//
// Author           : Richard C. Bilson
// Created On       : Mon May 18 07:44:20 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Thu Mar 16 07:53:59 2017
// Update Count     : 31
//

#include <cassert>

#include "Specialize.h"
#include "GenPoly.h"
#include "PolyMutator.h"

#include "Parser/ParseNode.h"

#include "SynTree/Expression.h"
#include "SynTree/Statement.h"
#include "SynTree/Type.h"
#include "SynTree/Attribute.h"
#include "SynTree/TypeSubstitution.h"
#include "SynTree/Mutator.h"
#include "ResolvExpr/FindOpenVars.h"
#include "Common/UniqueName.h"
#include "Common/utility.h"
#include "InitTweak/InitTweak.h"
#include "Tuples/Tuples.h"

namespace GenPoly {
	class Specialize final : public PolyMutator {
	  public:
		using PolyMutator::mutate;
		virtual Expression * mutate( ApplicationExpr *applicationExpr ) override;
		virtual Expression * mutate( AddressExpr *castExpr ) override;
		virtual Expression * mutate( CastExpr *castExpr ) override;
		// virtual Expression * mutate( LogicalExpr *logicalExpr );
		// virtual Expression * mutate( ConditionalExpr *conditionalExpr );
		// virtual Expression * mutate( CommaExpr *commaExpr );

		void handleExplicitParams( ApplicationExpr *appExpr );
		Expression * createThunkFunction( FunctionType *funType, Expression *actual, InferredParams *inferParams );
		Expression * doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams = nullptr );

		std::string paramPrefix = "_p";
	};

	/// Looks up open variables in actual type, returning true if any of them are bound in the environment or formal type.
	bool needsPolySpecialization( Type *formalType, Type *actualType, TypeSubstitution *env ) {
		if ( env ) {
			using namespace ResolvExpr;
			OpenVarSet openVars, closedVars;
			AssertionSet need, have;
			findOpenVars( formalType, openVars, closedVars, need, have, false );
			findOpenVars( actualType, openVars, closedVars, need, have, true );
			for ( OpenVarSet::const_iterator openVar = openVars.begin(); openVar != openVars.end(); ++openVar ) {
				Type *boundType = env->lookup( openVar->first );
				if ( ! boundType ) continue;
				if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( boundType ) ) {
					if ( closedVars.find( typeInst->get_name() ) == closedVars.end() ) {
						return true;
					} // if
				} else {
					return true;
				} // if
			} // for
			return false;
		} else {
			return false;
		} // if
	}

	/// True if both types have the same structure, but not necessarily the same types.
	/// That is, either both types are tuple types with the same size (recursively), or
	/// both are not tuple types.
	bool matchingTupleStructure( Type * t1, Type * t2 ) {
		TupleType * tuple1 = dynamic_cast< TupleType * >( t1 );
		TupleType * tuple2 = dynamic_cast< TupleType * >( t2 );
		if ( tuple1 && tuple2 ) {
			if ( tuple1->size() != tuple2->size() ) return false;
			for ( auto types : group_iterate( tuple1->get_types(), tuple2->get_types() ) ) {
				if ( ! matchingTupleStructure( std::get<0>( types ), std::get<1>( types ) ) ) return false;
			}
			return true;
		} else if ( ! tuple1 && ! tuple2 ) return true;
		return false;
	}

	bool needsTupleSpecialization( Type *formalType, Type *actualType ) {
		// Needs tuple specialization if the structure of the formal type and actual type do not match.
		// This is the case if the formal type has ttype polymorphism, or if the structure  of tuple types
		// between the function do not match exactly.
		if ( FunctionType * fftype = getFunctionType( formalType ) ) {
			if ( fftype->isTtype() ) return true;
			// conversion of 0 (null) to function type does not require tuple specialization
			if ( dynamic_cast< ZeroType * >( actualType ) ) return false;
			FunctionType * aftype = getFunctionType( actualType );
			assertf( aftype, "formal type is a function type, but actual type is not." );
			if ( fftype->get_parameters().size() != aftype->get_parameters().size() ) return true;
			for ( auto params : group_iterate( fftype->get_parameters(), aftype->get_parameters() ) ) {
				DeclarationWithType * formal = std::get<0>(params);
				DeclarationWithType * actual = std::get<1>(params);
				if ( ! matchingTupleStructure( formal->get_type(), actual->get_type() ) ) return true;
			}
		}
		return false;
	}

	bool needsSpecialization( Type *formalType, Type *actualType, TypeSubstitution *env ) {
		return needsPolySpecialization( formalType, actualType, env ) || needsTupleSpecialization( formalType, actualType );
	}

	Expression * Specialize::doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams ) {
		assertf( actual->has_result(), "attempting to specialize an untyped expression" );
		if ( needsSpecialization( formalType, actual->get_result(), env ) ) {
			if ( FunctionType *funType = getFunctionType( formalType ) ) {
				ApplicationExpr *appExpr;
				VariableExpr *varExpr;
				if ( ( appExpr = dynamic_cast<ApplicationExpr*>( actual ) ) ) {
					return createThunkFunction( funType, appExpr->get_function(), inferParams );
				} else if ( ( varExpr = dynamic_cast<VariableExpr*>( actual ) ) ) {
					return createThunkFunction( funType, varExpr, inferParams );
				} else {
					// This likely won't work, as anything that could build an ApplicationExpr probably hit one of the previous two branches
					return createThunkFunction( funType, actual, inferParams );
				}
			} else {
				return actual;
			} // if
		} else {
			return actual;
		} // if
	}

	/// restructures the arguments to match the structure of the formal parameters of the actual function.
	/// [begin, end) are the exploded arguments.
	template< typename Iterator, typename OutIterator >
	void structureArg( Type * type, Iterator & begin, Iterator end, OutIterator out ) {
		if ( TupleType * tuple = dynamic_cast< TupleType * >( type ) ) {
			std::list< Expression * > exprs;
			for ( Type * t : *tuple ) {
				structureArg( t, begin, end, back_inserter( exprs ) );
			}
			*out++ = new TupleExpr( exprs );
		} else {
			assertf( begin != end, "reached the end of the arguments while structuring" );
			*out++ = *begin++;
		}
	}

	/// explode assuming simple cases: either type is pure tuple (but not tuple expr) or type is non-tuple.
	template< typename OutputIterator >
	void explodeSimple( Expression * expr, OutputIterator out ) {
		if ( TupleType * tupleType = dynamic_cast< TupleType * > ( expr->get_result() ) ) {
			// tuple type, recursively index into its components
			for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
				explodeSimple( new TupleIndexExpr( expr->clone(), i ), out );
			}
			delete expr;
		} else {
			// non-tuple type - output a clone of the expression
			*out++ = expr;
		}
	}

	struct EnvTrimmer : public Visitor {
		TypeSubstitution * env, * newEnv;
		EnvTrimmer( TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){}
		virtual void visit( TypeDecl * tyDecl ) {
			// transfer known bindings for seen type variables
			if ( Type * t = env->lookup( tyDecl->get_name() ) ) {
				newEnv->add( tyDecl->get_name(), t );
			}
		}
	};

	/// reduce environment to just the parts that are referenced in a given expression
	TypeSubstitution * trimEnv( ApplicationExpr * expr, TypeSubstitution * env ) {
		if ( env ) {
			TypeSubstitution * newEnv = new TypeSubstitution();
			EnvTrimmer trimmer( env, newEnv );
			expr->accept( trimmer );
			return newEnv;
		}
		return nullptr;
	}

	/// Generates a thunk that calls `actual` with type `funType` and returns its address
	Expression * Specialize::createThunkFunction( FunctionType *funType, Expression *actual, InferredParams *inferParams ) {
		static UniqueName thunkNamer( "_thunk" );

		FunctionType *newType = funType->clone();
		if ( env ) {
			// it is important to replace only occurrences of type variables that occur free in the
			// thunk's type
			env->applyFree( newType );
		} // if
		// create new thunk with same signature as formal type (C linkage, empty body)
		FunctionDecl *thunkFunc = new FunctionDecl( thunkNamer.newName(), Type::StorageClasses(), LinkageSpec::C, newType, new CompoundStmt( noLabels ) );
		thunkFunc->fixUniqueId();

		// thunks may be generated and not used - silence warning with attribute
		thunkFunc->get_attributes().push_back( new Attribute( "unused" ) );

		// thread thunk parameters into call to actual function, naming thunk parameters as we go
		UniqueName paramNamer( paramPrefix );
		ApplicationExpr *appExpr = new ApplicationExpr( actual );

		FunctionType * actualType = getFunctionType( actual->get_result() )->clone();
		if ( env ) {
			// need to apply the environment to the actual function's type, since it may itself be polymorphic
			env->apply( actualType );
		}
		std::unique_ptr< FunctionType > actualTypeManager( actualType ); // for RAII
		std::list< DeclarationWithType * >::iterator actualBegin = actualType->get_parameters().begin();
		std::list< DeclarationWithType * >::iterator actualEnd = actualType->get_parameters().end();

		std::list< Expression * > args;
		for ( DeclarationWithType* param : thunkFunc->get_functionType()->get_parameters() ) {
			// name each thunk parameter and explode it - these are then threaded back into the actual function call.
			param->set_name( paramNamer.newName() );
			explodeSimple( new VariableExpr( param ), back_inserter( args ) );
		}

		// walk parameters to the actual function alongside the exploded thunk parameters and restructure the arguments to match the actual parameters.
		std::list< Expression * >::iterator argBegin = args.begin(), argEnd = args.end();
		for ( ; actualBegin != actualEnd; ++actualBegin ) {
			structureArg( (*actualBegin)->get_type(), argBegin, argEnd, back_inserter( appExpr->get_args() ) );
		}

		appExpr->set_env( trimEnv( appExpr, env ) );
		if ( inferParams ) {
			appExpr->get_inferParams() = *inferParams;
		} // if

		// handle any specializations that may still be present
		std::string oldParamPrefix = paramPrefix;
		paramPrefix += "p";
		// save stmtsToAdd in oldStmts
		std::list< Statement* > oldStmts;
		oldStmts.splice( oldStmts.end(), stmtsToAdd );
		mutate( appExpr );
		paramPrefix = oldParamPrefix;
		// write any statements added for recursive specializations into the thunk body
		thunkFunc->get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd );
		// restore oldStmts into stmtsToAdd
		stmtsToAdd.splice( stmtsToAdd.end(), oldStmts );

		// add return (or valueless expression) to the thunk
		Statement *appStmt;
		if ( funType->get_returnVals().empty() ) {
			appStmt = new ExprStmt( noLabels, appExpr );
		} else {
			appStmt = new ReturnStmt( noLabels, appExpr );
		} // if
		thunkFunc->get_statements()->get_kids().push_back( appStmt );

		// add thunk definition to queue of statements to add
		stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) );
		// return address of thunk function as replacement expression
		return new AddressExpr( new VariableExpr( thunkFunc ) );
	}

	void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) {
		// create thunks for the explicit parameters
		assert( appExpr->get_function()->has_result() );
		FunctionType *function = getFunctionType( appExpr->get_function()->get_result() );
		assert( function );
		std::list< DeclarationWithType* >::iterator formal;
		std::list< Expression* >::iterator actual;
		for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) {
			*actual = doSpecialization( (*formal )->get_type(), *actual, &appExpr->get_inferParams() );
		}
	}

	Expression * Specialize::mutate( ApplicationExpr *appExpr ) {
		appExpr->get_function()->acceptMutator( *this );
		mutateAll( appExpr->get_args(), *this );

		if ( ! InitTweak::isIntrinsicCallExpr( appExpr ) ) {
			// create thunks for the inferred parameters
			// don't need to do this for intrinsic calls, because they aren't actually passed
			// need to handle explicit params before inferred params so that explicit params do not recieve a changed set of inferParams (and change them again)
			// alternatively, if order starts to matter then copy appExpr's inferParams and pass them to handleExplicitParams.
			handleExplicitParams( appExpr );
			for ( InferredParams::iterator inferParam = appExpr->get_inferParams().begin(); inferParam != appExpr->get_inferParams().end(); ++inferParam ) {
				inferParam->second.expr = doSpecialization( inferParam->second.formalType, inferParam->second.expr, inferParam->second.inferParams.get() );
			}
		}
		return appExpr;
	}

	Expression * Specialize::mutate( AddressExpr *addrExpr ) {
		addrExpr->get_arg()->acceptMutator( *this );
		assert( addrExpr->has_result() );
		addrExpr->set_arg( doSpecialization( addrExpr->get_result(), addrExpr->get_arg() ) );
		return addrExpr;
	}

	Expression * Specialize::mutate( CastExpr *castExpr ) {
		castExpr->get_arg()->acceptMutator( *this );
		if ( castExpr->get_result()->isVoid() ) {
			// can't specialize if we don't have a return value
			return castExpr;
		}
		Expression *specialized = doSpecialization( castExpr->get_result(), castExpr->get_arg() );
		if ( specialized != castExpr->get_arg() ) {
			// assume here that the specialization incorporates the cast
			return specialized;
		} else {
			return castExpr;
		}
	}

	// Removing these for now. Richard put these in for some reason, but it's not clear why.
	// In particular, copy constructors produce a comma expression, and with this code the parts
	// of that comma expression are not specialized, which causes problems.

	// Expression * Specialize::mutate( LogicalExpr *logicalExpr ) {
	// 	return logicalExpr;
	// }

	// Expression * Specialize::mutate( ConditionalExpr *condExpr ) {
	// 	return condExpr;
	// }

	// Expression * Specialize::mutate( CommaExpr *commaExpr ) {
	// 	return commaExpr;
	// }

	void convertSpecializations( std::list< Declaration* >& translationUnit ) {
		Specialize spec;
		mutateAll( translationUnit, spec );
	}
} // namespace GenPoly

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