souffle  2.0.2-371-g6315b36
SubroutineReturn.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2019, The Souffle Developers. 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 SubroutineReturn.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "ram/Expression.h"
18 #include "ram/Node.h"
19 #include "ram/Operation.h"
20 #include "ram/utility/NodeMapper.h"
23 #include <cassert>
24 #include <iosfwd>
25 #include <memory>
26 #include <ostream>
27 #include <utility>
28 #include <vector>
29 
30 namespace souffle::ram {
31 
32 /**
33  * @class SubroutineReturn
34  * @brief A statement for returning from a ram subroutine
35  *
36  * For example:
37  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
38  * ...
39  * RETURN (t0.0, t0.1)
40  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
41  */
42 class SubroutineReturn : public Operation {
43 public:
44  SubroutineReturn(VecOwn<Expression> vals) : expressions(std::move(vals)) {
45  for (const auto& expr : expressions) {
46  assert(expr != nullptr && "Expression is a null-pointer");
47  }
48  }
49 
50  /** @brief Getter for expressions */
51  std::vector<Expression*> getValues() const {
52  return toPtrVector(expressions);
53  }
54 
55  std::vector<const Node*> getChildNodes() const override {
56  std::vector<const Node*> res;
57  for (const auto& expr : expressions) {
58  res.push_back(expr.get());
59  }
60  return res;
61  }
62 
63  SubroutineReturn* clone() const override {
64  VecOwn<Expression> newValues;
65  for (auto& expr : expressions) {
66  newValues.emplace_back(expr->clone());
67  }
68  return new SubroutineReturn(std::move(newValues));
69  }
70 
71  void apply(const NodeMapper& map) override {
72  for (auto& expr : expressions) {
73  expr = map(std::move(expr));
74  }
75  }
76 
77 protected:
78  void print(std::ostream& os, int tabpos) const override {
79  os << times(" ", tabpos);
80  os << "RETURN (";
81  for (auto val : getValues()) {
82  os << *val;
83  if (val != *(getValues().end() - 1)) {
84  os << ", ";
85  }
86  }
87  os << ")" << std::endl;
88  }
89 
90  bool equal(const Node& node) const override {
91  const auto& other = static_cast<const SubroutineReturn&>(node);
92  return equal_targets(expressions, other.expressions);
93  }
94 
95  /** Return expressions */
97 };
98 
99 } // namespace souffle::ram
souffle::ram::SubroutineReturn::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: SubroutineReturn.h:75
souffle::ram::SubroutineReturn::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: SubroutineReturn.h:59
souffle::ram::SubroutineReturn::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: SubroutineReturn.h:82
souffle::ram::SubroutineReturn::clone
SubroutineReturn * clone() const override
Create a clone (i.e.
Definition: SubroutineReturn.h:67
souffle::ram::SubroutineReturn::expressions
VecOwn< Expression > expressions
Return expressions.
Definition: SubroutineReturn.h:100
souffle::ram::SubroutineReturn::SubroutineReturn
SubroutineReturn(VecOwn< Expression > vals)
Definition: SubroutineReturn.h:48
souffle::ram::SubroutineReturn::getValues
std::vector< Expression * > getValues() const
Getter for expressions.
Definition: SubroutineReturn.h:55
souffle::map
auto map(const std::vector< A > &xs, F &&f)
Applies a function to each element of a vector and returns the results.
Definition: ContainerUtil.h:158
souffle::ram
Definition: AstToRamTranslator.h:54
NodeMapper.h
souffle::times
detail::multiplying_printer< T > times(const T &value, unsigned num)
A utility printing a given value multiple times.
Definition: StreamUtil.h:322
ContainerUtil.h
souffle::equal_targets
bool equal_targets(const Container &a, const Container &b, const Comparator &comp)
A function testing whether two containers are equal with the given Comparator.
Definition: ContainerUtil.h:433
Node.h
std
Definition: Brie.h:3053
Operation.h
StreamUtil.h
souffle::ram::SubroutineReturn::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: SubroutineReturn.h:94
Expression.h
souffle::toPtrVector
std::vector< T * > toPtrVector(const std::vector< std::unique_ptr< T >> &v)
A utility function enabling the creation of a vector of pointers.
Definition: ContainerUtil.h:146
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45