/*
 * 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/.
 */

/*
 * Author: pgess <v.melnychenko@xreate.org>
 * Created on June 23, 2018, 2:51 PM
 *
 * \file    latex.h
 * \brief   latex
 */

#ifndef LATEX_H
#define LATEX_H

#include "compilation/interpretation-instructions.h"
#include "query/latex.h"
#include "pass/compilepass.h"
#include "analysis/interpretation.h"

namespace xreate{
namespace latex{

ExpandedType
getSubjectDomain(const std::string& subject, LatexQuery* query);

/** \brief Latex(Late Context)-enabled decorator for IFunctionUnit
 *  \extends IFunctionUnit
 */
template<class Parent>
class LatexBruteFunctionDecorator: public Parent{
public:

    LatexBruteFunctionDecorator(ManagedFnPtr f, CompilePass* p)
    : Parent(f, p){
        __query = reinterpret_cast<LatexQuery*> (Parent::pass->man->transcend->getQuery(QueryId::LatexQuery));
    }

protected:

    std::vector<llvm::Type*>
    prepareSignature(){
        std::vector<llvm::Type*>&& signature = Parent::prepareSignature();

        const Demand& demand = __query->getFnDemand(Parent::function->getName());
        signature.reserve(signature.size() + demand.size());

        for(const std::string& subject: demand){
            llvm::Type* subjectTRaw = Parent::pass->man->llvm->toLLVMType(
                getSubjectDomain(subject, __query));
            signature.push_back(subjectTRaw);
        }

        return signature;
    }

    llvm::Function::arg_iterator
    prepareBindings(){
        llvm::Function::arg_iterator fargsI = Parent::prepareBindings();

        const Demand& demand = __query->getFnDemand(Parent::function->getName());
        for(const std::string& subject: demand){
            std::string argTitle = std::string("latex_") + subject;
            fargsI->setName(argTitle);
            ++fargsI;
        }

        return fargsI;
    }

public:
    LatexQuery* __query;
};

class ExtraArgsCallStatement: public compilation::ICallStatement{
public:

    ExtraArgsCallStatement(std::vector<llvm::Value*> argsLatex, compilation::ICallStatement* parent)
    : __argsLatex(argsLatex), __parent(parent){ }

    llvm::Value* operator()(std::vector<llvm::Value *>&& args, const std::string& hintDecl = "");

private:
    std::vector<llvm::Value*> __argsLatex;
    compilation::ICallStatement* __parent;
};

template<class Parent>
class LatexBruteScopeDecorator: public Parent{
public:

    LatexBruteScopeDecorator(const CodeScope * const codeScope, compilation::IFunctionUnit* f, CompilePass* compilePass)
    : Parent(codeScope, f, compilePass){ }

    compilation::ICallStatement*
    findFunction(const Expression& opCall){
        compilation::ICallStatement* stmntCallParent = Parent::findFunction(opCall);
        const std::string& calleeName = opCall.getValueString();
        LatexQuery* query = reinterpret_cast<LatexQuery*> (Parent::pass->man->transcend->getQuery(QueryId::LatexQuery));

        const Demand& fnCalleeDemand = query->getFnDemand(calleeName);
        if(!fnCalleeDemand.size()) return stmntCallParent;

        //prepare latex arguments
        std::vector<llvm::Value*> argsLatex;
        argsLatex.reserve(fnCalleeDemand.size());

        for(const std::string& subject: fnCalleeDemand){
            ExpandedType subjectT = getSubjectDomain(subject, query);

            Gringo::Symbol decision = query->getDecision(subject, Parent::scope);
            const Expression& decisionE = interpretation::representTransExpression(
                decision, subjectT, Parent::pass->man->transcend);
            Attachments::put<TypeInferred>(decisionE, subjectT);
            argsLatex.push_back(Parent::process(decisionE, subject));
        }

        return new ExtraArgsCallStatement(std::move(argsLatex), stmntCallParent);
    }
};

}
} //end of namespace xreate::context

#endif /* LATEX_H */

