/* 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:   context.h
 * Author: pgess <v.melnychenko@xreate.org>
 *
 * Created on 25/05/2020
 */
#ifndef XREATE_CONTEXT_H
#define XREATE_CONTEXT_H

#include "ast.h"
#include "pass/compilepass.h"

namespace llvm {
  class Value;
  class GlobalVariable;
}

namespace xreate { namespace compilation{
class AllocContext {
public:
  AllocContext(std::string id, ContextManager* man, AllocContext* parent)
    : __id(id), __man(man), __parent(parent){}

  void set(const ASTSite& varS, llvm::Value* varRaw);
  llvm::Value* get(const ASTSite& varS);

private:
  std::string __id;
  ContextManager* __man;
  AllocContext* __parent;
};

class ContextManager{
  friend class AllocContext;

public:
  ContextManager(CompilePass* pass): __pass(pass){};
  AllocContext* getContext(const std::string& id);

private:
  std::map<std::string, AllocContext*> __contexts;
  CompilePass* __pass;
};

class GlobalContextTable{
public:
  GlobalContextTable(CompilePass* pass): __pass(pass){}

  void compile();
  llvm::GlobalVariable* getVariable(const ASTSite& site){return __table.at(site);}

private:
  CompilePass* __pass;
  std::map<ASTSite, llvm::GlobalVariable*> __table;
};

}} // xreate::compilation
#endif //XREATE_CONTEXT_H
