/* 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);

  static ImplementationType getImplementation(const Expression& aggrE, AST* ast);
  static llvm::Type* getRawType(const Expression& aggrE, const ExpandedType& aggrT, LLVMLayer* llvm);
  static llvm::Type* getRawTypeByHint(const Expression& hintE, const ExpandedType& aggrT, LLVMLayer* llvm);
  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* init(std::forward_list<llvm::Type*> fields);
  llvm::Value* update(llvm::Value* aggrRaw, const ExpandedType& aggrT, const Expression& updE);

private:
  compilation::Context __context;
};

class FlyIR{
public:
  FlyIR(typehints::FlyHint hint, compilation::Context context);

  llvm::Value* create(llvm::Value* sourceRaw, CodeScope* body, const std::string& hintAlias);
  llvm::Value* getSourceAggr(llvm::Value* aggrRaw);
  llvm::Value* getTransformFn(llvm::Value* aggrRaw);
  llvm::Value* operatorMap(const Expression& expr, const std::string& hintAlias);
  static llvm::Type *getRawType(const ExpandedType& aggrT, const typehints::FlyHint& hint, LLVMLayer* llvm);

private:
  typehints::FlyHint __hint;
  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 &hintAlias="") = 0;
  virtual llvm::Value* advance(llvm::Value* idxRaw, const std::string& hintAlias="") = 0;

  static IFwdIteratorIR* create(const Expression& aggrE, const ExpandedType& aggrT, const compilation::Context& context);
  static IFwdIteratorIR* createByHint(const Expression& hintE, const ExpandedType& aggrT, const compilation::Context& context);
};

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 {
public:
  FwdIteratorIR<ON_THE_FLY>(typehints::FlyHint hint, const ExpandedType &aggrT, compilation::Context context)
    : __aggrT(aggrT), __hint(hint), __context(context){}

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

private:
  ExpandedType __aggrT;
  typehints::FlyHint __hint;
  compilation::Context __context;
};
}}

#endif //CODEINSTRUCTIONS_H
