souffle  2.0.2-371-g6315b36
UnpackRecord.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2014, 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 UnpackRecord.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "ram/Expression.h"
18 #include "ram/NestedOperation.h"
19 #include "ram/Node.h"
20 #include "ram/Operation.h"
21 #include "ram/TupleOperation.h"
22 #include "ram/utility/NodeMapper.h"
26 #include <cassert>
27 #include <cstddef>
28 #include <iosfwd>
29 #include <memory>
30 #include <ostream>
31 #include <utility>
32 #include <vector>
33 
34 namespace souffle::ram {
35 
36 /**
37  * @class UnpackRecord
38  * @brief Record lookup
39  *
40  * Looks up a record with respect to an expression
41  *
42  * For example:
43  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
44  * UNPACK t0.0 INTO t1
45  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
46  */
47 class UnpackRecord : public TupleOperation {
48 public:
49  UnpackRecord(Own<Operation> nested, int ident, Own<Expression> expr, size_t arity)
50  : TupleOperation(ident, std::move(nested)), expression(std::move(expr)), arity(arity) {
51  assert(expression != nullptr && "Expression is a null-pointer");
52  }
53 
54  /** @brief Get record expression */
55  const Expression& getExpression() const {
56  assert(expression != nullptr && "Expression of unpack-record is a null-pointer");
57  return *expression;
58  }
59 
60  /** @brief Get arity of record */
61  std::size_t getArity() const {
62  return arity;
63  }
64 
65  std::vector<const Node*> getChildNodes() const override {
66  auto res = TupleOperation::getChildNodes();
67  res.push_back(expression.get());
68  return res;
69  }
70 
71  UnpackRecord* clone() const override {
72  return new UnpackRecord(
74  }
75 
76  void apply(const NodeMapper& map) override {
78  expression = map(std::move(expression));
79  }
80 
81 protected:
82  void print(std::ostream& os, int tabpos) const override {
83  os << times(" ", tabpos);
84  os << "UNPACK t" << getTupleId() << " FROM " << *expression << "\n";
85  NestedOperation::print(os, tabpos + 1);
86  }
87 
88  bool equal(const Node& node) const override {
89  const auto& other = static_cast<const UnpackRecord&>(node);
90  return TupleOperation::equal(other) && equal_ptr(expression, other.expression) &&
91  arity == other.arity;
92  }
93 
94  /** Expression for record reference */
96 
97  /** Arity of the unpacked tuple */
98  const size_t arity;
99 };
100 
101 } // namespace souffle::ram
NestedOperation.h
souffle::ram::UnpackRecord::getArity
std::size_t getArity() const
Get arity of record.
Definition: UnpackRecord.h:65
souffle::ram::UnpackRecord
Record lookup.
Definition: UnpackRecord.h:51
souffle::ram::TupleOperation::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: TupleOperation.h:52
souffle::ram::TupleOperation::TupleOperation
TupleOperation(int ident, Own< Operation > nested, std::string profileText="")
Definition: TupleOperation.h:37
souffle::ram::NestedOperation::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: NestedOperation.h:75
souffle::ram::NestedOperation::getOperation
Operation & getOperation() const
Get nested operation.
Definition: NestedOperation.h:62
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
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
MiscUtil.h
souffle::ram::TupleOperation::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: TupleOperation.h:57
souffle::ram::UnpackRecord::UnpackRecord
UnpackRecord(Own< Operation > nested, int ident, Own< Expression > expr, size_t arity)
Definition: UnpackRecord.h:53
TupleOperation.h
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::Node
Node is a superclass for all RAM IR classes.
Definition: Node.h:42
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
NodeMapper.h
souffle::ram::NodeMapper
An abstract class for manipulating RAM Nodes by substitution.
Definition: NodeMapper.h:38
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::ram::UnpackRecord::arity
const size_t arity
Arity of the unpacked tuple.
Definition: UnpackRecord.h:102
souffle::ram::UnpackRecord::clone
UnpackRecord * clone() const override
Create a clone (i.e.
Definition: UnpackRecord.h:75
souffle::ram::UnpackRecord::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: UnpackRecord.h:86
souffle::ram::UnpackRecord::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: UnpackRecord.h:69
souffle::ram::TupleOperation::getTupleId
int getTupleId() const
Get identifier.
Definition: TupleOperation.h:43
souffle::equal_ptr
bool equal_ptr(const T *a, const T *b)
Compares two values referenced by a pointer where the case where both pointers are null is also consi...
Definition: MiscUtil.h:130
Node.h
std
Definition: Brie.h:3053
Operation.h
StreamUtil.h
souffle::ram::UnpackRecord::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: UnpackRecord.h:92
Expression.h
souffle::ram::NestedOperation::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: NestedOperation.h:80
souffle::ram::Expression
Abstract class for describing scalar values in RAM.
Definition: Expression.h:33
souffle::ram::UnpackRecord::expression
Own< Expression > expression
Expression for record reference.
Definition: UnpackRecord.h:99
souffle::ram::UnpackRecord::getExpression
const Expression & getExpression() const
Get record expression.
Definition: UnpackRecord.h:59
souffle::ram::UnpackRecord::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: UnpackRecord.h:80