/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * LateContextCompiler2.h
 *
 *  Created on: 02/10, 2016
 *      Author: pgess <v.melnychenko@xreate.org>
 */

//TOTEST compile several context arguments 
#ifndef LATECONTEXTCOMPILER2_H_
#define LATECONTEXTCOMPILER2_H_

#include "serialization.h"

namespace llvm {
	class Value;
	class Function;
}

namespace xreate {
	class CompilePass;

namespace compilation {
	class IFunctionUnit;
}}

namespace xreate {namespace context{

typedef unsigned int ScopePacked;

/** \brief Encapsulates compilation of Late Context
 *  \sa xreate::context::ContextQuery, xreate::compilation::BasicCodeScopeUnit
 */
class LateContextCompiler2 {
public:
	llvm::Value* rawContextArgument = nullptr;

	LateContextCompiler2(compilation::IFunctionUnit* f, CompilePass* p);
        
        /** \brief Finds function with regard to late context*/
	llvm::Value* findFunction(const std::string& calleeName, llvm::Function* specializationDefault, ScopePacked scopeCaller);
	llvm::Value* compileContextArgument(const std::string& callee, ScopePacked scopeCaller);
        size_t getFunctionDemandSize() const;

private:
	//boost::bimap<size_t, std::string> __decisions;
	//std::vector<Domain> __scheme;

	compilation::IFunctionUnit* function;
	CompilePass* pass;
        size_t __sizeOfDemand;
        
        llvm::Value* compileDependentDecision(const Expression& topic, ScopePacked scopeCaller);
        llvm::Value* compileDecisionSelectorAsSwitch(llvm::Value* selector, std::vector<llvm::Function*> vectorVariants, llvm::Function* variantDefault);
};
}} /* namespace xreate::context */

#endif /* LATECONTEXTCOMPILER2_H_ */
