#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 CodeScopeUnit;
class FunctionUnit;

class TargetInterpretation;


struct Context{
    CodeScopeUnit* 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 CodeScopeUnit {
public:
    CodeScopeUnit(CodeScope* codeScope, FunctionUnit* f, CompilePass* compilePass);

    void bindArg(llvm::Value* value, std::string&& alias);
    void overrideDeclaration(const Symbol binding, Expression&& declaration);
    std::map<VID,llvm::Value*> __rawVars;

    void reset(){raw = nullptr;}
    llvm::Value* compile(const std::string& hintBlockDecl="");
    llvm::Value* compileSymbol(const Symbol& s, std::string hintRetVar="");
    llvm::Value* process(const Expression& expr, const std::string& hintVarDecl="");
    llvm::Value* processLowlevel(const Expression& expr, const std::string& hintVarDecl="");
    
    CodeScope* scope;

private:
    CompilePass* pass;
    llvm::Value* raw = nullptr;
    FunctionUnit* function;
    std::unordered_map<VID, Expression> __declarationsOverriden;
    

    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();

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

    ManagedFnPtr function;
    llvm::Function* raw = nullptr;
    
protected:
    CompilePass* pass=nullptr;
    
private:
    std::map<CodeScope*, std::unique_ptr<CodeScopeUnit>> 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();
};

    class Transformations;
} // end of namespace `xreate::compilation`

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

public:
    compilation::Transformations* transformations;
    
    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;
    compilation::TargetInterpretation* targetInterpretation;
    
};

}

#endif // COMPILEPASS_H
