souffle  2.0.2-371-g6315b36
Context.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file Context.h
12  *
13  * Defines Interpreter interpreter context
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "interpreter/Index.h"
20 #include "interpreter/Relation.h"
21 #include "souffle/RamTypes.h"
22 #include <cassert>
23 #include <cstddef>
24 #include <memory>
25 #include <utility>
26 #include <vector>
27 
28 namespace souffle::interpreter {
29 
30 /**
31  * Evaluation context for Interpreter operations
32  */
33 class Context {
34  using ViewPtr = Own<ViewWrapper>;
35 
36 public:
37  Context(size_t size = 0) : data(size) {}
38 
39  /** This constructor is used when program enter a new scope.
40  * Only Subroutine value needs to be copied */
41  Context(Context& ctxt) : returnValues(ctxt.returnValues), args(ctxt.args) {}
42  virtual ~Context() = default;
43 
44  const RamDomain*& operator[](size_t index) {
45  if (index >= data.size()) {
46  data.resize((index + 1));
47  }
48  return data[index];
49  }
50 
51  const RamDomain* const& operator[](size_t index) const {
52  return data[index];
53  }
54 
55  /** @brief Allocate a tuple.
56  * allocatedDataContainer has the ownership of those tuples. */
58  Own<RamDomain[]> newTuple(new RamDomain[size]);
59  allocatedDataContainer.push_back(std::move(newTuple));
60 
61  // Return the reference as raw pointer.
62  return allocatedDataContainer.back().get();
63  }
64 
65  /** @brief Get subroutine return value */
66  std::vector<RamDomain>& getReturnValues() const {
67  return *returnValues;
68  }
69 
70  /** @brief Set subroutine return value */
71  void setReturnValues(std::vector<RamDomain>& retVals) {
72  returnValues = &retVals;
73  }
74 
75  /** @brief Add subroutine return value */
76  void addReturnValue(RamDomain val) {
77  assert(returnValues != nullptr);
78  returnValues->push_back(val);
79  }
80 
81  /** @brief Get subroutine Arguments */
82  const std::vector<RamDomain>& getArguments() const {
83  return *args;
84  }
85 
86  /** @brief Set subroutine Arguments */
87  void setArguments(const std::vector<RamDomain>& a) {
88  args = &a;
89  }
90 
91  /** @brief Get subroutine Arguments */
92  RamDomain getArgument(size_t i) const {
93  assert(args != nullptr && i < args->size() && "argument out of range");
94  return (*args)[i];
95  }
96 
97  /** @brief Create a view in the environment */
98  void createView(const RelationWrapper& rel, size_t indexPos, size_t viewPos) {
99  ViewPtr view;
100  if (views.size() < viewPos + 1) {
101  views.resize(viewPos + 1);
102  }
103  views[viewPos] = rel.createView(indexPos);
104  }
105 
106  /** @brief Return a view */
107  ViewWrapper* getView(size_t id) {
108  assert(id < views.size());
109  return views[id].get();
110  }
111 
112 private:
113  /** @brief Run-time value */
114  std::vector<const RamDomain*> data;
115  /** @brief Subroutine return value */
116  std::vector<RamDomain>* returnValues = nullptr;
117  /** @brief Subroutine arguments */
118  const std::vector<RamDomain>* args = nullptr;
119  /** @bref Allocated data */
121  /** @brief Views */
123 };
124 
125 } // namespace souffle::interpreter
souffle::interpreter::Context::getArguments
const std::vector< RamDomain > & getArguments() const
Get subroutine Arguments.
Definition: Context.h:94
TCB_SPAN_NAMESPACE_NAME::detail::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.h:198
souffle::interpreter::Context::setArguments
void setArguments(const std::vector< RamDomain > &a)
Set subroutine Arguments.
Definition: Context.h:99
souffle::interpreter::Context::data
std::vector< const RamDomain * > data
Run-time value.
Definition: Context.h:126
souffle::interpreter::Context::ViewPtr
Own< ViewWrapper > ViewPtr
Definition: Context.h:46
souffle::interpreter::Context::Context
Context(size_t size=0)
Definition: Context.h:49
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
Relation.h
souffle::interpreter::Context::args
const std::vector< RamDomain > * args
Subroutine arguments.
Definition: Context.h:130
souffle::interpreter::Context::createView
void createView(const RelationWrapper &rel, size_t indexPos, size_t viewPos)
Create a view in the environment.
Definition: Context.h:110
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
Index.h
souffle::interpreter::Context::operator[]
const RamDomain *& operator[](size_t index)
Definition: Context.h:56
souffle::interpreter::Context::returnValues
std::vector< RamDomain > * returnValues
Subroutine return value.
Definition: Context.h:128
souffle::interpreter::Context::~Context
virtual ~Context()=default
souffle::interpreter::Context::setReturnValues
void setReturnValues(std::vector< RamDomain > &retVals)
Set subroutine return value.
Definition: Context.h:83
souffle::interpreter::Context::getView
ViewWrapper * getView(size_t id)
Return a view.
Definition: Context.h:119
souffle::interpreter::Context::getReturnValues
std::vector< RamDomain > & getReturnValues() const
Get subroutine return value.
Definition: Context.h:78
souffle::interpreter::Context::addReturnValue
void addReturnValue(RamDomain val)
Add subroutine return value.
Definition: Context.h:88
souffle::interpreter::index
The index class is utilized as a template-meta-programming structure to specify and realize indices.
Definition: Util.h:176
souffle::interpreter::ViewWrapper
A dummy wrapper for indexViews.
Definition: Index.h:144
souffle::interpreter::Context::allocatedDataContainer
VecOwn< RamDomain[]> allocatedDataContainer
@bref Allocated data
Definition: Context.h:132
souffle::interpreter
Definition: BrieIndex.cpp:22
i
size_t i
Definition: json11.h:663
souffle::interpreter::Context::allocateNewTuple
RamDomain * allocateNewTuple(size_t size)
Allocate a tuple.
Definition: Context.h:69
souffle::interpreter::Context::views
VecOwn< ViewWrapper > views
Views.
Definition: Context.h:134
RamTypes.h
souffle::interpreter::Context::getArgument
RamDomain getArgument(size_t i) const
Get subroutine Arguments.
Definition: Context.h:104
souffle::interpreter::RelationWrapper
Wrapper for InterpreterRelation.
Definition: Relation.h:48
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
id
void id(std::string col)
Definition: Tui.h:1124
souffle::interpreter::Context
Evaluation context for Interpreter operations.
Definition: Context.h:39