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

/**
 * \file    compilepass.h
 * \brief   Main compilation routine. See \ref xreate::CompilePass
 */

#include "compilepass.h"
#include "transcendlayer.h"
#include "ast.h"
#include "llvmlayer.h"

#include "compilation/decorators.h"
#include "compilation/pointers.h"
#include "analysis/typeinference.h"
#include "compilation/control.h"
#include "compilation/demand.h"
#include "analysis/resources.h"
#ifdef XREATE_ENABLE_EXTERN
  #include "ExternLayer.h"
#endif

#include "compilation/containers.h"
#include "compilation/containers/arrays.h"

#ifndef XREATE_CONFIG_MIN
  #include "query/containers.h"

  #include "pass/versionspass.h"
  #include "compilation/targetinterpretation.h"
#endif

#include <boost/optional.hpp>
#include <memory>

using namespace std;
using namespace llvm;

namespace xreate{
namespace compilation{

#define DEFAULT(x) (hintAlias.empty()? x: hintAlias)

std::string
BasicBruteFunction::prepareName() {
    AST* ast = IBruteFunction::pass->man->root;

    string name = ast->getFnSpecializations(IBruteFunction::function->__name).size() > 1 ?
                  IBruteFunction::function->__name + std::to_string(IBruteFunction::function.id()) :
                  IBruteFunction::function->__name;

    return name;
}

std::vector<llvm::Type*>
BasicBruteFunction::prepareSignature() {
    LLVMLayer* llvm = IBruteFunction::pass->man->llvm;
    AST* ast = IBruteFunction::pass->man->root;
    CodeScope* entry = IBruteFunction::function->__entry;
    std::vector<llvm::Type*> signature;

    std::transform(entry->__bindings.begin(), entry->__bindings.end(), std::inserter(signature, signature.end()),
        [llvm, ast, entry](const std::string & arg)->llvm::Type* {
            assert(entry->__identifiers.count(arg));

            ScopedSymbol argid{entry->__identifiers.at(arg), versions::VERSION_NONE};
            return llvm->toLLVMType(ast->expandType(entry->__declarations.at(argid).type));
        });

    return signature;
}

llvm::Type*
BasicBruteFunction::prepareResult() {
    LLVMLayer* llvm = IBruteFunction::pass->man->llvm;
    AST* ast = IBruteFunction::pass->man->root;
    CodeScope* entry = IBruteFunction::function->__entry;

    return llvm->toLLVMType(ast->expandType(entry->__declarations.at(ScopedSymbol::RetSymbol).type));
}

llvm::Function::arg_iterator
BasicBruteFunction::prepareBindings() {
    CodeScope* entry = IBruteFunction::function->__entry;
    IBruteScope* entryCompilation = IBruteFunction::getScopeUnit(entry);
    llvm::Function::arg_iterator fargsI = IBruteFunction::raw->arg_begin();

    for (std::string &arg : entry->__bindings) {
        ScopedSymbol argid{entry->__identifiers[arg], versions::VERSION_NONE};

        entryCompilation->bindArg(&*fargsI, argid);
        fargsI->setName(arg);
        ++fargsI;
    }

    return fargsI;
}

IBruteScope::IBruteScope(const CodeScope * const codeScope, IBruteFunction* f, CompilePass* compilePass)
: pass(compilePass), function(f), scope(codeScope), currentBlockRaw(nullptr) { }

llvm::Value*
BruteFnInvocation::operator()(std::vector<llvm::Value *>&& args, const std::string& hintDecl) {
    llvm::Function* calleeInfo = dyn_cast<llvm::Function>(__callee);

    if (calleeInfo) {
        auto argsFormal = calleeInfo->args();
        size_t sizeArgsF = std::distance(argsFormal.begin(), argsFormal.end());
        assert(args.size() >= sizeArgsF);
        assert(calleeInfo->isVarArg() || args.size() == sizeArgsF);
        
        auto argFormal = argsFormal.begin();
        for(size_t argId = 0; argId < args.size(); ++argId){
            if(argFormal != argsFormal.end()){
                args[argId] = typeinference::doAutomaticTypeConversion(
                    args.at(argId), argFormal->getType(), llvm->irBuilder);
                ++argFormal;
            }
        }
    }

    //Do not name function call that returns Void.
    std::string nameStatement = hintDecl;
    if (calleeInfo->getReturnType()->isVoidTy()) {
        nameStatement.clear();
    }

    return llvm->irBuilder.CreateCall(__calleeTy, __callee, args, nameStatement);
}

llvm::Value*
HiddenArgsFnInvocation::operator() (std::vector<llvm::Value *>&& args, const std::string& hintDecl) {
  args.insert(args.end(), __args.begin(), __args.end());
  return __parent->operator ()(std::move(args), hintDecl);
}

class CallStatementInline : public IFnInvocation{
public:

    CallStatementInline(IBruteFunction* caller, IBruteFunction* callee, LLVMLayer* l)
    : __caller(caller), __callee(callee), llvm(l) { }

    llvm::Value* operator()(std::vector<llvm::Value *>&& args, const std::string& hintDecl) {
        return nullptr;
    }

private:
    IBruteFunction* __caller;
    IBruteFunction* __callee;
    LLVMLayer* llvm;

    bool
    isInline() {
        // Symbol ret = Symbol{0, function->__entry};
        // bool flagOnTheFly = SymbolAttachments::get<IsImplementationOnTheFly>(ret, false);
        //TODO consider inlining
        return false;
    }
} ;

BasicBruteScope::BasicBruteScope(const CodeScope * const codeScope, IBruteFunction* f, CompilePass* compilePass)
: IBruteScope(codeScope, f, compilePass) { }

llvm::Value*
BasicBruteScope::processSymbol(const Symbol& s, std::string hintRetVar) {
    Expression declaration = CodeScope::getDefinition(s);
    const CodeScope* scopeExternal = s.scope;
    IBruteScope* scopeBruteExternal = IBruteScope::function->getScopeUnit(scopeExternal);
    assert(scopeBruteExternal->currentBlockRaw);
    
    llvm::Value* resultRaw;
    llvm::BasicBlock* blockOwn = pass->man->llvm->irBuilder.GetInsertBlock();
    
    if (scopeBruteExternal->currentBlockRaw == blockOwn) {
        resultRaw = scopeBruteExternal->process(declaration, hintRetVar);
        scopeBruteExternal->currentBlockRaw = currentBlockRaw =
            pass->man->llvm->irBuilder.GetInsertBlock();

    } else {
        pass->man->llvm->irBuilder.SetInsertPoint(scopeBruteExternal->currentBlockRaw);
        resultRaw = scopeBruteExternal->processSymbol(s, hintRetVar);
        pass->man->llvm->irBuilder.SetInsertPoint(blockOwn);
    }

    return resultRaw;
}

IFnInvocation*
BasicBruteScope::findFunction(const Expression& opCall) {
    const std::string& calleeName = opCall.getValueString();
    LLVMLayer* llvm = pass->man->llvm;
    const std::list<ManagedFnPtr>& specializations = pass->man->root->getFnSpecializations(calleeName);

#ifdef XREATE_ENABLE_EXTERN
  //if no specializations registered - check external function
  if (specializations.size() == 0) {
      llvm::Function* external = llvm->layerExtern->lookupFunction(calleeName);

      llvm::outs() << "Debug/External function: " << calleeName;
      external->getType()->print(llvm::outs(), true);
      llvm::outs() << "\n";

      return new BruteFnInvocation(external, llvm);
  }
#endif

    //There should be only one specialization without any valid guards at this point
    return new BruteFnInvocation(pass->getFunctionUnit(
        pass->man->root->findFunction(calleeName))->compile(),
        llvm);
}

//DISABLEDFEATURE transformations
//    if (pass->transformations->isAcceptable(expr)){
//        return pass->transformations->transform(expr, result, ctx);
//    }

llvm::Value*
BasicBruteScope::process(const Expression& expr, const std::string& hintAlias, const TypeAnnotation& expectedT) {
    llvm::Value *leftRaw;
    llvm::Value *rightRaw;
    LLVMLayer& l = *pass->man->llvm;
    Context ctx{this, function, pass};
    xreate::compilation::ControlIR controlIR = xreate::compilation::ControlIR({this, function, pass});

    switch (expr.op) {
    case Operator::ADD:
    case Operator::SUB: case Operator::MUL:
    case Operator::DIV: case Operator::EQU: case Operator::LSS:
    case Operator::GTR: case Operator::NE: case Operator::LSE:
    case Operator::GTE:
        assert(expr.__state == Expression::COMPOUND);
        assert(expr.operands.size() == 2);

        leftRaw = process(expr.operands[0]);
        rightRaw = process(expr.operands[1]);

        break;

    default:;
    }

    switch (expr.op) {
    case Operator::ADD:
    {
      return l.irBuilder.CreateAdd(leftRaw, rightRaw, DEFAULT("addv"));
    }

    case Operator::SUB:
        return l.irBuilder.CreateSub(leftRaw, rightRaw, DEFAULT("tmp_sub"));
        break;

    case Operator::MUL:
        return l.irBuilder.CreateMul(leftRaw, rightRaw, DEFAULT("tmp_mul"));
        break;

    case Operator::DIV:
        if (leftRaw->getType()->isIntegerTy()) return l.irBuilder.CreateSDiv(leftRaw, rightRaw, DEFAULT("tmp_div"));
        if (leftRaw->getType()->isFloatingPointTy()) return l.irBuilder.CreateFDiv(leftRaw, rightRaw, DEFAULT("tmp_div"));
        break;

    case Operator::EQU: {
        if (leftRaw->getType()->isIntegerTy()) return l.irBuilder.CreateICmpEQ(leftRaw, rightRaw, DEFAULT("tmp_equ"));
        if (leftRaw->getType()->isFloatingPointTy()) return l.irBuilder.CreateFCmpOEQ(leftRaw, rightRaw, DEFAULT("tmp_equ"));

        const ExpandedType& leftT = pass->man->root->getType(expr.operands[0]);
        const ExpandedType& rightT = pass->man->root->getType(expr.operands[1]);

        if(leftT->__operator == TypeOperator::VARIANT && rightT->__operator == TypeOperator::VARIANT){
            llvm::Type* selectorT = llvm::cast<llvm::StructType>(leftRaw->getType())->getElementType(0);
            llvm::Value* leftUnwapped = typeinference::doAutomaticTypeConversion(leftRaw, selectorT, l.irBuilder);
            llvm::Value* rightUnwapped = typeinference::doAutomaticTypeConversion(rightRaw, selectorT, l.irBuilder);
            return l.irBuilder.CreateICmpEQ(leftUnwapped, rightUnwapped, DEFAULT("tmp_equ"));
        }
        break;
    }

    case Operator::NE:
        return l.irBuilder.CreateICmpNE(leftRaw, rightRaw, DEFAULT("tmp_ne"));
        break;

    case Operator::LSS:
        return l.irBuilder.CreateICmpSLT(leftRaw, rightRaw, DEFAULT("tmp_lss"));
        break;

    case Operator::LSE:
        return l.irBuilder.CreateICmpSLE(leftRaw, rightRaw, DEFAULT("tmp_lse"));
        break;

    case Operator::GTR:
        return l.irBuilder.CreateICmpSGT(leftRaw, rightRaw, DEFAULT("tmp_gtr"));
        break;

    case Operator::GTE:
        return l.irBuilder.CreateICmpSGE(leftRaw, rightRaw, DEFAULT("tmp_gte"));
        break;

    case Operator::NEG:
    {
      leftRaw = process(expr.operands[0]);
        ExpandedType leftTy = pass->man->root->getType(expr.operands[0]);
        
        if (leftTy->__value == TypePrimitive::Bool){
          return l.irBuilder.CreateNot(leftRaw, DEFAULT("tmp_not"));
        } else {
          return l.irBuilder.CreateNeg(leftRaw, DEFAULT("tmp_neg"));
        }
        break;
    }

    case Operator::CALL:
    {
        assert(expr.__state == Expression::COMPOUND);
        shared_ptr<IFnInvocation> callee(findFunction(expr));
        const std::string& nameCallee = expr.getValueString();

        //prepare arguments
        std::vector<llvm::Value *> args;
        args.reserve(expr.operands.size());

        std::transform(expr.operands.begin(), expr.operands.end(), std::inserter(args, args.end()),
            [this](const Expression & operand) {
                return process(operand);
            }
        );

        return (*callee)(move(args), DEFAULT("res_" + nameCallee));
    }

    case Operator::IF:
    {
        return controlIR.compileIf(expr, DEFAULT("tmp_if"));
    }

    case Operator::SWITCH:
    {
        return controlIR.compileSwitch(expr, DEFAULT("tmp_switch"));
    }

    case Operator::LOGIC_AND:
    {
        assert(expr.operands.size() == 1);
        return process(expr.operands[0]);
    }

    case Operator::LIST: //init record or array
    {
      ExpandedType exprT = l.ast->getType(expr, expectedT);
      TypesHelper helper(pass->man->llvm);

      enum {RECORD, ARRAY} kind;
      if (helper.isArrayT(exprT)){
        kind = ARRAY;

      } else if (helper.isRecordT(exprT)){
        kind = RECORD;
      } else {
        assert(false && "Inapproriate type");
      }

      #ifdef XREATE_ENABLE_EXTERN
      if (exprT->__operator  == TypeOperator::ALIAS){
          if (l.layerExtern->isArrayType(exprT->__valueCustom)){
              flagIsArray = true;
              break;
          }

          if (l.layerExtern->isRecordType(exprT->__valueCustom)){
              flagIsArray = false;
              break;
          }

          assert(false && "Inapproriate external type");
      }
      #endif

      switch(kind){
        case RECORD:{
          const std::vector<string> fieldsFormal = helper.getRecordFields(exprT);
          containers::RecordIR irRecords(ctx);
          llvm::StructType *recordTRaw = llvm::cast<llvm::StructType>(l.toLLVMType(exprT));
          llvm::Value *resultRaw = irRecords.init(recordTRaw);
          return irRecords.update(resultRaw, exprT, expr);
        }

        case ARRAY: {
          std::unique_ptr<containers::IContainersIR> containerIR(
            containers::IContainersIR::create(expr, expectedT, ctx));
          llvm::Value* aggrRaw = containerIR->init(hintAlias);
          return containerIR->update(aggrRaw, expr, hintAlias);
        }
      }
      break;
    };

    case Operator::LIST_RANGE:
    {
        assert(false); //no compilation phase for a range list
        //  return InstructionList(this).compileConstantArray(expr, l, hintRetVar);
    };

    case Operator::MAP:
    {
        assert(expr.blocks.size());
        return controlIR.compileMapSolidOutput(expr, DEFAULT("map"));
    };

    case Operator::FOLD:
    {
        return controlIR.compileFold(expr, DEFAULT("fold"));
    };

    case Operator::FOLD_INF:
    {
        return controlIR.compileFoldInf(expr, DEFAULT("fold"));
    };

    case Operator::INDEX:
    {
      assert(expr.operands.size() > 1);

      const Expression& aggrE = expr.operands[0];
      const ExpandedType& aggrT = pass->man->root->getType(aggrE);
      llvm::Value* aggrRaw = process(aggrE);
      switch (aggrT->__operator) {
      case TypeOperator::RECORD:
      {
        list<string> fieldsList;
        for(auto opIt = ++expr.operands.begin(); opIt!=expr.operands.end(); ++opIt){
          fieldsList.push_back(getIndexStr(*opIt));
        }

        return controlIR.compileStructIndex(aggrRaw, aggrT, fieldsList);
      };

      case TypeOperator::ARRAY:
      {
        std::vector<llvm::Value*> indexes;
        std::transform(++expr.operands.begin(), expr.operands.end(), std::inserter(indexes, indexes.end()),
            [this] (const Expression & op) {
                return process(op);
            }
        );

        std::unique_ptr<containers::IContainersIR> containersIR(
          containers::IContainersIR::create(aggrE, expectedT, ctx)
        );

        containers::ArrayIR* arraysIR = static_cast<containers::ArrayIR*>(containersIR.get());
        return arraysIR->get(aggrRaw, indexes, hintAlias);
      };

      default:
          assert(false);
      }
    };

    case Operator::CALL_INTRINSIC:
    {
//        const std::string op = expr.getValueString();
//
//        if (op == "copy") {
//            llvm::Value* result = process(expr.getOperands().at(0));
//
//            auto decoratorVersions = Decorators<VersionsScopeDecoratorTag>::getInterface(this);
//            llvm::Value* storage = decoratorVersions->processIntrinsicInit(result->getType());
//            decoratorVersions->processIntrinsicCopy(result, storage);
//
//            return l.irBuilder.CreateLoad(storage, hintAlias);
//        }

        assert(false && "undefined intrinsic");
    }

    case Operator::QUERY:
    case Operator::QUERY_LATE:
    {
        assert(false && "Should be processed by interpretation");
    }

    case Operator::VARIANT:
    {
      const ExpandedType& typResult = pass->man->root->getType(expr);
      llvm::Type* typResultRaw = l.toLLVMType(typResult);
      llvm::Type* typIdRaw = llvm::cast<llvm::StructType>(typResultRaw)->getElementType(0);

      uint64_t id = expr.getValueDouble();
      llvm::Value* resultRaw = llvm::UndefValue::get(typResultRaw);
      resultRaw = l.irBuilder.CreateInsertValue(resultRaw, llvm::ConstantInt::get(typIdRaw, id), llvm::ArrayRef<unsigned>({0}));

      const ExpandedType& typVariant = ExpandedType(typResult->__operands.at(id));
      llvm::Type* typVariantRaw = l.toLLVMType(typVariant);
      llvm::Value* variantRaw = llvm::UndefValue::get(typVariantRaw);
      assert(expr.operands.size() == typVariant->__operands.size() && "Wrong variant arguments count");
      if (!typVariant->__operands.size()) return resultRaw;

      for (unsigned int fieldId = 0; fieldId < expr.operands.size(); ++fieldId) {
        const ExpandedType& typField = ExpandedType(typVariant->__operands.at(fieldId));
        Attachments::put<TypeInferred>(expr.operands.at(fieldId), typField);
        llvm::Value* fieldRaw = process(expr.operands.at(fieldId));
        assert(fieldRaw);

        variantRaw = l.irBuilder.CreateInsertValue(variantRaw, fieldRaw, llvm::ArrayRef<unsigned>({fieldId}));
      }

      llvm::Type* typStorageRaw = llvm::cast<llvm::StructType>(typResultRaw)->getElementType(1);
      llvm::Value* addrAsStorage = l.irBuilder.CreateAlloca(typStorageRaw);
      llvm::Value* addrAsVariant =  l.irBuilder.CreateBitOrPointerCast(addrAsStorage, typVariantRaw->getPointerTo());

      l.irBuilder.CreateStore(variantRaw, addrAsVariant);
      llvm::Value* storageRaw = l.irBuilder.CreateLoad(typStorageRaw, addrAsStorage);
      resultRaw = l.irBuilder.CreateInsertValue(resultRaw, storageRaw, llvm::ArrayRef<unsigned>({1}));

      return resultRaw;
    }

    case Operator::SWITCH_VARIANT:
    {
        return controlIR.compileSwitchVariant(expr, DEFAULT("tmpswitch"));
    }

    case Operator::SWITCH_LATE:
    {
        assert(false && "Instruction's compilation should've been redirected to interpretation");
        return nullptr;
    }

    case Operator::SEQUENCE:
    {
        return controlIR.compileSequence(expr);
    }

    case Operator::UNDEF:
    {
      llvm::Type* typExprUndef = l.toLLVMType(pass->man->root->getType(expr, expectedT));
      return llvm::UndefValue::get(typExprUndef);
    }

    case Operator::UPDATE:
    {
      TypesHelper helper(pass->man->llvm);
      containers::RecordIR irRecords(ctx);

      const Expression& aggrE = expr.operands.at(0);
      const Expression& updE = expr.operands.at(1);
      const ExpandedType& aggrT = pass->man->root->getType(aggrE);
      llvm::Value* aggrRaw = process(aggrE);

      if (helper.isRecordT(aggrT)){
        return irRecords.update(aggrRaw, aggrT, updE);
      }

      if (helper.isArrayT(aggrT)){
        if (updE.op == Operator::LIST_INDEX){

          std::unique_ptr<containers::IContainersIR> containersIR(
            containers::IContainersIR::create(aggrE, TypeAnnotation(), ctx
          ));

          return containersIR->update(aggrRaw, updE, hintAlias);
        }
      }

      assert(false);
      return nullptr;
    }

    case Operator::INVALID:
        assert(expr.__state != Expression::COMPOUND);

        switch (expr.__state) {
        case Expression::IDENT:
        {
            Symbol s = Attachments::get<IdentifierSymbol>(expr);
            return processSymbol(s, expr.getValueString());
        }

        case Expression::NUMBER:
        {
	        llvm::Type* typConst = l.toLLVMType(pass->man->root->getType(expr, expectedT));
	        int literal = expr.getValueDouble();

	        if (typConst->isFloatingPointTy()) return llvm::ConstantFP::get(typConst, literal);
	        if (typConst->isIntegerTy()) return llvm::ConstantInt::get(typConst, literal);
            
	        assert(false && "Can't compile literal");
        }

        case Expression::STRING:
        {
            return controlIR.compileConstantStringAsPChar(expr.getValueString(), DEFAULT("tmp_str"));
        };

        default:
        {
            break;
        }
        };

        break;

    default: break;

    }

    assert(false && "Can't compile expression");
    return 0;
}

llvm::Value*
BasicBruteScope::compile(const std::string& hintBlockDecl) {
    LLVMLayer* llvm = pass->man->llvm;

    if (!hintBlockDecl.empty()) {
        llvm::BasicBlock *block = llvm::BasicBlock::Create(llvm->llvmContext, hintBlockDecl, function->raw);
        pass->man->llvm->irBuilder.SetInsertPoint(block);
    }

    currentBlockRaw = pass->man->llvm->irBuilder.GetInsertBlock();
    Symbol symbScope = Symbol{ScopedSymbol::RetSymbol, scope};
    return processSymbol(symbScope);
}

IBruteScope::~IBruteScope() { }

IBruteFunction::~IBruteFunction() { }

llvm::Function*
IBruteFunction::compile() {
    if (raw != nullptr) return raw;

    LLVMLayer* llvm = pass->man->llvm;
    llvm::IRBuilder<>& builder = llvm->irBuilder;

    string&& functionName = prepareName();
    std::vector<llvm::Type*>&& types = prepareSignature();
    llvm::Type* expectedResultType = prepareResult();

    llvm::FunctionType *ft = llvm::FunctionType::get(expectedResultType, types, false);
    raw = llvm::cast<llvm::Function>(llvm->module->getOrInsertFunction(functionName, ft));
    prepareBindings();

    const std::string&blockName = "entry";
    llvm::BasicBlock* blockCurrent = builder.GetInsertBlock();

    llvm::Value* result = getScopeUnit(function->__entry)->compile(blockName);
    assert(result);

    //SECTIONTAG types/convert function ret value
    builder.CreateRet(typeinference::doAutomaticTypeConversion(result, expectedResultType, llvm->irBuilder));

    if (blockCurrent) {
        builder.SetInsertPoint(blockCurrent);
    }

    llvm->moveToGarbage(ft);
    return raw;
}

IBruteScope*
IBruteFunction::getScopeUnit(const CodeScope * const scope) {
    if (__scopes.count(scope)) {
        auto result = __scopes.at(scope).lock();

        if (result) {
            return result.get();
        }
    }

    std::shared_ptr<IBruteScope> unit(pass->buildCodeScopeUnit(scope, this));

    if (scope->__parent != nullptr) {
        auto parentUnit = Decorators<CachedScopeDecoratorTag>::getInterface(getScopeUnit(scope->__parent));
        parentUnit->registerChildScope(unit);

    } else {
        __orphanedScopes.push_back(unit);
    }

    if (!__scopes.emplace(scope, unit).second) {
        __scopes[scope] = unit;
    }

    return unit.get();
}

IBruteScope*
IBruteFunction::getScopeUnit(ManagedScpPtr scope) {
    return getScopeUnit(&*scope);
}

IBruteScope*
IBruteFunction::getEntry() {
    return getScopeUnit(function->getEntryScope());
}

template<>
compilation::IBruteFunction*
CompilePassCustomDecorators<void, void>
::buildFunctionUnit(const ManagedFnPtr& function) {
    return new BruteFunctionDefault(function, this);
}

template<>
compilation::IBruteScope*
CompilePassCustomDecorators<void, void>
::buildCodeScopeUnit(const CodeScope * const scope, IBruteFunction* function) {
    return new DefaultCodeScopeUnit(scope, function, this);
}

std::string
BasicBruteScope::getIndexStr(const Expression& index){
  switch(index.__state){

//named struct field
    case Expression::STRING:
      return index.getValueString();
      break;

//anonymous struct field
    case Expression::NUMBER:
      return to_string((int) index.getValueDouble());
      break;

    default:
      assert(false && "Wrong index for a struct");
  }
  return "";
}

} // end of compilation

compilation::IBruteFunction*
CompilePass::getFunctionUnit(const ManagedFnPtr& function) {
    unsigned int id = function.id();

    if (!functions.count(id)) {
        compilation::IBruteFunction* unit = buildFunctionUnit(function);
        functions.emplace(id, unit);
        return unit;
    }

    return functions.at(id);
}

void
CompilePass::prepare(){
  //Initialization:
#ifndef XREATE_CONFIG_MIN
#endif
  managerTransformations = new xreate::compilation::TransformationsManager();
  targetInterpretation = new interpretation::TargetInterpretation(man, this);
}

void
CompilePass::run() {
  prepare();

  //Determine entry function:
  StaticModel model = man->transcend->query(analysis::FN_ENTRY_PREDICATE);
  assert(model.size() && "Error: No entry function found");
  assert(model.size() == 1 && "Error: Ambiguous entry function");

  string nameMain = std::get<0>(TranscendLayer::parse<std::string>(model.begin()->second));
  compilation::IBruteFunction* unitMain = getFunctionUnit(man->root->findFunction(nameMain));

  //Compilation itself:
  entry = unitMain->compile();
}

llvm::Function*
CompilePass::getEntryFunction() {
    assert(entry);
    return entry;
}

void
CompilePass::prepareQueries(TranscendLayer* transcend) {
#ifndef XREATE_CONFIG_MIN
  transcend->registerQuery(new latex::LatexQuery(), QueryId::LatexQuery);
#endif

  transcend->registerQuery(new containers::Query(), QueryId::ContainersQuery);
  transcend->registerQuery(new demand::DemandQuery(), QueryId::DemandQuery);
  transcend->registerQuery(new polymorph::PolymorphQuery(), QueryId::PolymorphQuery);
}

} //end of namespace xreate

/**
 * \class xreate::CompilePass
 * \brief The owner of the compilation process. Performs fundamental compilation activities along with the xreate::compilation's routines
 *
 * xreate::CompilePass traverses over xreate::AST tree and produces executable code.
 * The pass performs compilation using the following data sources:
 *   - %Attachments: the data gathered by the previous passes. See \ref xreate::Attachments.
 *   - Transcend solutions accessible via queries. See \ref xreate::IQuery, \ref xreate::TranscendLayer.
 * 
 * The pass generates a bytecode by employing \ref xreate::LLVMLayer(wrapper over LLVM toolchain). 
 * Many compilation activities are delegated to more specific routines. Most notable delegated compilation aspects are:
 *   - Containers support. See \ref xreate::containers.
 *   - Latex compilation. See \ref xreate::latex.
 *   - Interpretation support. See \ref xreate::interpretation.
 *   - Loop saturation support. See \ref xreate::compilation::TransformationsScopeDecorator.
 *   - External code interaction support. See \ref xreate::ExternLayer (wrapper over Clang library).
 *
 * \section adaptability_sect Adaptability
 * xreate::CompilePass's behaviour can be adapted in several ways:
 *   - %Function Decorators to alter function-level compilation. See \ref xreate::compilation::IBruteFunction
 *   - Code Block Decorators to alter code block level compilation. See \ref xreate::compilation::ICodeScopeUnit. 
 *      Default functionality defined by \ref xreate::compilation::DefaultCodeScopeUnit
 *   - Targets to allow more versitile extensions.
 *     Currently only xreate::interpretation::TargetInterpretation  use Targets infrastructure. See \ref xreate::compilation::Target.
 *   - Altering %function invocation. See \ref xreate::compilation::IFnInvocation.
 *
 * Clients are free to construct a compiler instantiation with the desired decorators by using \ref xreate::compilation::CompilePassCustomDecorators.
 * As a handy alias, `CompilePassCustomDecorators<void, void>` constructs the default compiler.
 *
 */
