#ifndef COMPILEPASS_H
#define COMPILEPASS_H

#include "abstractpass.h"

#include "llvm/IR/Function.h"

namespace xreate {
	class AdhocScheme;
	class ClaspLayer;
	class ContextQuery;
        class LLVMLayer;
}

//namespace llvm {
//    class Function;
//    class Value;
//    class Type;
//}

namespace xreate {

class CompilePass;

namespace compilation {

class AbstractCodeScopeUnit;
class FunctionUnit;

class TargetInterpretation;


struct Context{
    AbstractCodeScopeUnit* scope;
    FunctionUnit* function;
    CompilePass* pass;
};

class CallStatement {
public:
    virtual llvm::Value* operator() (std::vector<llvm::Value *>&& args, const std::string& hintDecl="") = 0;
};

class CallStatementRaw: public CallStatement{
public:
    CallStatementRaw(llvm::Function* callee, LLVMLayer* l)
        : __callee(callee), __calleeTy(callee->getFunctionType()), llvm(l) {}
    CallStatementRaw(llvm::Value* callee, llvm::FunctionType* ty, LLVMLayer* l)
        : __callee(callee), __calleeTy(ty), llvm(l) {}
    llvm::Value* operator() (std::vector<llvm::Value *>&& args, const std::string& hintDecl="");
        
private:
    llvm::Value* __callee;        
    llvm::FunctionType* __calleeTy;
    
    LLVMLayer* llvm;
};

class AbstractCodeScopeUnit{
public:
    CompilePass* const pass;
    FunctionUnit* const function;
    CodeScope* const scope;
    
    AbstractCodeScopeUnit(CodeScope* codeScope, FunctionUnit* f, CompilePass* compilePass);
    ~AbstractCodeScopeUnit(){}

    virtual llvm::Value* compile(const std::string& hintBlockDecl="")=0;
    virtual llvm::Value* processSymbol(const Symbol& s, std::string hintRetVar="")=0;
    virtual llvm::Value* process(const Expression& expr, const std::string& hintVarDecl="")=0;
    
    virtual void bindArg(llvm::Value* value, std::string&& alias)=0;
    virtual void bindArg(llvm::Value* value, const ScopedSymbol& s)=0;

protected:
    virtual CallStatement* findFunction(const std::string& callee)=0;
};

class BasicCodeScopeUnit: public AbstractCodeScopeUnit{
public:
    BasicCodeScopeUnit(CodeScope* codeScope, FunctionUnit* f, CompilePass* compilePass);
    ~BasicCodeScopeUnit(){}

    llvm::Value* processSymbol(const Symbol& s, std::string hintRetVar="");
    llvm::Value* process(const Expression& expr, const std::string& hintVarDecl="");
    llvm::Value* compile(const std::string& hintBlockDecl="");
    
protected:
    CallStatement* findFunction(const std::string& callee);
};



class IFunctionDecorator {
protected:
    virtual std::string prepareName() = 0;
    virtual std::vector<llvm::Type*>  prepareArguments() = 0;
    virtual llvm::Type* prepareResult() = 0;
    virtual llvm::Function::arg_iterator prepareBindings() = 0;
    virtual ~IFunctionDecorator(){}
};

class FunctionUnit: public IFunctionDecorator{
public:
   FunctionUnit(ManagedFnPtr f, CompilePass* p)
    : function(f), pass(p) {}

    llvm::Function* compile();

    AbstractCodeScopeUnit* getEntry();
    AbstractCodeScopeUnit* getScopeUnit(CodeScope* scope);
    AbstractCodeScopeUnit* getScopeUnit(ManagedScpPtr scope);

    ManagedFnPtr function;
    llvm::Function* raw = nullptr;
    
protected:
    CompilePass* pass=nullptr;
    
private:
    std::map<CodeScope*, std::unique_ptr<AbstractCodeScopeUnit>> scopes;
};

class BasicFunctionDecorator: public FunctionUnit{
public:
    BasicFunctionDecorator(ManagedFnPtr f, CompilePass* p)
    : FunctionUnit(f, p) {}

protected:
    std::string prepareName();
    virtual std::vector<llvm::Type*>  prepareArguments();
    virtual llvm::Type* prepareResult();
    virtual llvm::Function::arg_iterator prepareBindings();
};

} // end of namespace `xreate::compilation`

class CompilePass : public AbstractPass<void> {
    friend class LateContextCompiler;
    friend class LateContextCompiler2;
    friend class compilation::BasicCodeScopeUnit;
    friend class compilation::FunctionUnit;

public:
//    compilation::Transformations* transformations;
    compilation::TargetInterpretation* targetInterpretation;
    
    CompilePass(PassManager* manager): AbstractPass<void>(manager) {}
    compilation::FunctionUnit* getFunctionUnit(const ManagedFnPtr& function);
    void run() override;
    llvm::Function* getEntryFunction();
    static void prepareQueries(ClaspLayer* clasp);
    
    
private:
    //TODO free `functions` in destructor
    std::map<unsigned int, compilation::FunctionUnit*> functions;
    llvm::Function* entry = 0;

    ContextQuery* queryContext;
    
};

}

#endif // COMPILEPASS_H
