/* 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:   containers.h
 * Author: pgess <v.melnychenko@xreate.org>
 */

#ifndef CODEINSTRUCTIONS_H
#define CODEINSTRUCTIONS_H

#include "ast.h"

#include "llvmlayer.h"
#include "pass/compilepass.h"
#include "compilation/control.h"
#include "query/containers.h"
#include "analysis/typehints.h"

namespace xreate { namespace containers {

class IFwdIteratorIR;

class IContainersIR{
public:
  static IContainersIR *create(
    const Expression &aggrE,
    const TypeAnnotation &expectedT,
    const compilation::Context &context);

  virtual llvm::Value *init(const std::string &hintAlias = "") = 0;
  virtual llvm::Value *update(llvm::Value *aggrRaw, const Expression &updE, const std::string &hintAlias) = 0;
  virtual IFwdIteratorIR* getFwdIterator() = 0;
  virtual ~IContainersIR(){}
};

class RecordIR{
public:
  RecordIR(const compilation::Context& context): __context(context){}
  llvm::Value* init(llvm::StructType* tyAggr);
  llvm::Value* update(llvm::Value* aggrRaw, const ExpandedType& aggrT, const Expression& updE);

private:
  compilation::Context __context;
};

/**
 * \brief A factory to create a concrete iterator based on the solution provided by xreate::containers::Query
 * \sa xreate::containers::Query
 */
class IFwdIteratorIR{
public :
  virtual ~IFwdIteratorIR(){};

  virtual llvm::Value* begin() = 0;
  virtual llvm::Value* end() = 0;
  virtual llvm::Value* get(llvm::Value* aggrRaw, llvm::Value *idxRaw, const std::string &hintRetVar="") = 0;
  virtual llvm::Value* advance(llvm::Value* idxRaw, const std::string& hintAlias="") = 0;
};

template<ImplementationType I>
class FwdIteratorIR;

/** \brief The lazy container implementation. 
 * 
 *  Represents computation on the fly. 
 *  \sa xreate::containers::IFwdIteratorIR, \sa xreate::containers::Query
 */
//template<>
//class FwdIteratorIR<ON_THE_FLY> : public IFwdIteratorIR {
//private:
//    LLVMLayer* llvm;
//    const xreate::Symbol current;
//    const Symbol source;
//    const ImplementationLinkedList linkedlist;
//    const CodeScope* const sourceScope;
//            //TODO initialize and mark as const (three fields)
//    compilation::IBruteScope* sourceUnit;
//    compilation::IBruteFunction* function; //TODO is used somewhere?
//    const Expression& sourceDecl;
//    compilation::Context context;
//    llvm::Type* sourceRawType =nullptr;
//
//public:
//    FwdIteratorIR(const compilation::Context& ctx, const xreate::Symbol& s, const ImplementationRec<ON_THE_FLY>& implementation)
//    :   llvm(ctx.pass->man->llvm),
//        current(s),
//        source(implementation.source),
//        linkedlist(source),
//        sourceScope(source.scope),
//        sourceUnit(ctx.function->getScopeUnit(source.scope)),
//        sourceDecl(CodeScope::getDefinition(source)),
//        context(ctx)
//    {}
//
//    llvm::Value* begin() override;
//    llvm::Value* end() override;
//    llvm::Value* get(llvm::Value* index,const std::string& hintRetVar="") override;
//    llvm::Value* advance(llvm::Value* idxRaw, const std::string& hintAlias="") override;
//};
}}

#endif //CODEINSTRUCTIONS_H
