#ifndef COMPILEPASS_H
#define COMPILEPASS_H

#include "abstractpass.h"

#include "llvm/IR/Function.h"

namespace xreate {
    class ClaspLayer;
    class CompilePass;
    class LLVMLayer;
    
    namespace adhoc{
        class AdhocScheme;
    }
    
    namespace context{
        class ContextQuery;
        class LateContextCompiler2;
    }
    
    namespace interpretation{
        class TargetInterpretation;
    }
}

namespace xreate { namespace compilation {

class AbstractCodeScopeUnit;
class FunctionUnit;
class TransformationsManager;

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);
    virtual ~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);

    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) {}
   ~FunctionUnit();

    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::weak_ptr<AbstractCodeScopeUnit>> __scopes;
    std::list<std::shared_ptr<AbstractCodeScopeUnit>> __orphanedScopes;
};

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 context::LateContextCompiler2;
    friend class compilation::BasicCodeScopeUnit;
    friend class compilation::FunctionUnit;

public:
    compilation::TransformationsManager* managerTransformations;
    interpretation::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;

    context::ContextQuery* queryContext;
    
};

}

#endif // COMPILEPASS_H
