/* 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/.
 * 
 * File:   targetstatic.h
 * Author: pgess  <v.melnychenko@xreate.org>
 *
 * Created on July 2, 2016, 1:25 PM
 */

#ifndef TARGETSTATIC_H
#define TARGETSTATIC_H

#include "ast.h"
#include "pass/compilepass.h"
#include "compilation/targets.h"
#include "pass/interpretationpass.h"
#include "transcendlayer.h"

namespace xreate{ namespace interpretation{
    class TargetInterpretation;
    class InterpretationScope;
    class InterpretationFunction;
}}

namespace xreate{ namespace compilation{
    template <>
    struct TargetInfo<interpretation::TargetInterpretation> {
        typedef Expression Result;
        typedef interpretation::InterpretationScope Scope;
        typedef interpretation::InterpretationFunction Function;
    };    
}}

namespace xreate{ namespace interpretation{
    
/** \brief Encapsulates interpretation of a single code scope */
class InterpretationScope: public  compilation::Scope<TargetInterpretation>{
    typedef Scope<TargetInterpretation> Parent;
    
public:
    InterpretationScope(const CodeScope* scope, compilation::Function<TargetInterpretation>* f): Parent(scope, f) {}
    Expression process(const Expression& expression) override;
    
    llvm::Value* compile(const Expression& expression, const compilation::Context& context, const std::string& hintAlias);
    
private:
    llvm::Value* processLate(const InterpretationOperator& op, const Expression& expression, const compilation::Context& context, const std::string& hintAlias);
    //llvm::Value* compilePartialFnCall(const Expression& expression, const Context& context);
    
    CodeScope* processOperatorIf(const Expression& expression);
    CodeScope* processOperatorSwitch(const Expression& expression);
    CodeScope* processOperatorSwitchVariant(const Expression& expression);
};

/** \brief Encapsulates interpretation of a single function */
class InterpretationFunction: public compilation::Function<TargetInterpretation>{
public:
    InterpretationFunction(const ManagedFnPtr& function, compilation::Target<TargetInterpretation>* target);
    Expression process(const std::vector<Expression>& args);
};

/** \brief Signature of a partially interpreted function */
struct PIFnSignature{
    ManagedFnPtr declaration;
    std::vector<Expression> bindings;
};

class PIBruteFunction;

/** \brief Encapsulates partially interpreted function */
class PIFunction: public InterpretationFunction{
public:
    PIBruteFunction* fnRaw;
    PIFnSignature instance;
    
    PIFunction(PIFnSignature&& sig, size_t id, TargetInterpretation* target);
    llvm::Function* compile();
};

bool operator<(const PIFnSignature& lhs, PIFunction* const rhs);
bool operator<(PIFunction* const lhs, const PIFnSignature& rhs);

/** \brief Encapsulates interpretation process based on analysis results from \ref InterpretationPass
 * \extends compilation::Target
 * \sa InterpretationPass
 */
class TargetInterpretation: public compilation::Target<TargetInterpretation>{
  typedef compilation::Target<TargetInterpretation> Parent;

public:    
  TargetInterpretation(PassManager *man, CompilePass* passCompilation): Parent(man), pass(passCompilation){}
  InterpretationFunction* getFunction(compilation::IBruteFunction* unit);
  PIFunction* getFunction(PIFnSignature&& sig);
   
private:
  std::map<PIFnSignature, PIFunction*> __pifunctions;
  std::map<compilation::IBruteFunction*, InterpretationFunction*> __dictFunctionsByUnit;

  //self:
public:
  CompilePass* pass;
  llvm::Value* compile(const Expression& expression, const compilation::Context& ctx, const std::string& hintAlias);
  InterpretationScope* transformContext(const compilation::Context& c);
    
private:
};

/**\brief Interpretation-aware Code Scope decorator
 * \extends xreate::compilation::IBruteScope
 */
template<class Parent>
class InterpretationScopeDecorator: public Parent{
public:
    InterpretationScopeDecorator(const CodeScope* const codeScope, compilation::IBruteFunction* f, CompilePass* compilePass): Parent(codeScope, f, compilePass){}
    
    virtual llvm::Value* process(const Expression& expr, const std::string& hintAlias, const TypeAnnotation& expectedT = TypeAnnotation()){
        const InterpretationData& data = Attachments::get<InterpretationData>(expr, {ANY, NONE});
        bool flagInterpretationEligible = (data.resolution == INTR_ONLY || data.op != InterpretationOperator::NONE);

        if (flagInterpretationEligible){
            compilation::Context ctx{this, this->function, this->pass};
            return Parent::pass->targetInterpretation->compile(expr, ctx, hintAlias);
        }
        
        return Parent::process(expr, hintAlias, expectedT);
    }
};

}} //end of xreate:: interpretation

#endif /* TARGETSTATIC_H */