souffle  2.0.2-371-g6315b36
Node.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 Node.h
12  *
13  * Declaration of RAM node and mappers for RAM nodes
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
20 #include <cassert>
21 #include <functional>
22 #include <iostream>
23 #include <memory>
24 #include <typeinfo>
25 #include <utility>
26 #include <vector>
27 
28 namespace souffle::ram {
29 
30 class NodeMapper;
31 
32 /**
33  * @class Node
34  * @brief Node is a superclass for all RAM IR classes.
35  */
36 class Node {
37 public:
38  /*
39  * @brief A virtual destructor for RAM nodes
40  */
41  virtual ~Node() = default;
42 
43  /**
44  * @brief Equivalence check for two RAM nodes
45  */
46  bool operator==(const Node& other) const {
47  return this == &other || (typeid(*this) == typeid(other) && equal(other));
48  }
49 
50  /**
51  * @brief Inequality check for two RAM nodes
52  */
53  bool operator!=(const Node& other) const {
54  return !(*this == other);
55  }
56 
57  /**
58  * @brief Create a clone (i.e. deep copy) of this node
59  */
60  virtual Node* clone() const = 0;
61 
62  /**
63  * @brief Apply the mapper to all child nodes
64  */
65  virtual void apply(const NodeMapper&) {}
66 
67  /**
68  * @brief Rewrite a child node
69  */
70  virtual void rewrite(const Node* oldNode, Own<Node> newNode) {
71  assert(oldNode != nullptr && "old node is a null-pointer");
72  assert(newNode != nullptr && "new node is a null-pointer");
73  std::function<Own<Node>(Own<Node>)> rewriter = [&](Own<Node> node) -> Own<Node> {
74  if (oldNode == node.get()) {
75  return std::move(newNode);
76  } else {
77  node->apply(makeLambdaRamMapper(rewriter));
78  return node;
79  }
80  };
81  apply(makeLambdaRamMapper(rewriter));
82  };
83 
84  /**
85  * @brief Obtain list of all embedded child nodes
86  */
87  virtual std::vector<const Node*> getChildNodes() const {
88  return {};
89  }
90 
91  /**
92  * Print RAM on a stream
93  */
94  friend std::ostream& operator<<(std::ostream& out, const Node& node) {
95  node.print(out);
96  return out;
97  }
98 
99 protected:
100  /**
101  * @brief Print RAM node
102  */
103  virtual void print(std::ostream& out = std::cout) const = 0;
104 
105  /**
106  * @brief Equality check for two RAM nodes.
107  * Default action is that nothing needs to be checked.
108  */
109  virtual bool equal(const Node&) const {
110  return true;
111  }
112 };
113 
114 } // namespace souffle::ram
souffle::ram::Node::operator<<
friend std::ostream & operator<<(std::ostream &out, const Node &node)
Print RAM on a stream.
Definition: Node.h:100
souffle::ram::Node::clone
virtual Node * clone() const =0
Create a clone (i.e.
LambdaNodeMapper.h
souffle::ram::Node::apply
virtual void apply(const NodeMapper &)
Apply the mapper to all child nodes.
Definition: Node.h:71
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ram::Node::getChildNodes
virtual std::vector< const Node * > getChildNodes() const
Obtain list of all embedded child nodes.
Definition: Node.h:93
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::Node::operator!=
bool operator!=(const Node &other) const
Inequality check for two RAM nodes.
Definition: Node.h:59
souffle::ram::Node
Node is a superclass for all RAM IR classes.
Definition: Node.h:42
souffle::ram::NodeMapper
An abstract class for manipulating RAM Nodes by substitution.
Definition: NodeMapper.h:38
souffle::ram::Node::equal
virtual bool equal(const Node &) const
Equality check for two RAM nodes.
Definition: Node.h:115
souffle::ram::Node::~Node
virtual ~Node()=default
souffle::ram::Node::print
virtual void print(std::ostream &out=std::cout) const =0
Print RAM node.
souffle::ram::Node::rewrite
virtual void rewrite(const Node *oldNode, Own< Node > newNode)
Rewrite a child node.
Definition: Node.h:76
souffle::ram::Node::operator==
bool operator==(const Node &other) const
Equivalence check for two RAM nodes.
Definition: Node.h:52
souffle::ram::makeLambdaRamMapper
LambdaNodeMapper< Lambda > makeLambdaRamMapper(const Lambda &lambda)
Creates a node mapper based on a corresponding lambda expression.
Definition: LambdaNodeMapper.h:67