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, 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 Node.h
12  *
13  * Defines the AST abstract node class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "parser/SrcLocation.h"
20 #include <iosfwd>
21 #include <string>
22 #include <typeinfo>
23 #include <utility>
24 #include <vector>
25 
26 namespace souffle::ast {
27 
28 class NodeMapper;
29 
30 /**
31  * @class Node
32  * @brief Abstract class for syntactic elements in an input program.
33  */
34 class Node {
35 public:
36  Node(SrcLocation loc = {}) : location(std::move(loc)){};
37  virtual ~Node() = default;
38 
39  /** Return source location of the Node */
40  const SrcLocation& getSrcLoc() const {
41  return location;
42  }
43 
44  /** Set source location for the Node */
45  void setSrcLoc(SrcLocation l) {
46  location = std::move(l);
47  }
48 
49  /** Return source location of the syntactic element */
50  std::string extloc() const {
51  return location.extloc();
52  }
53 
54  /** Equivalence check for two AST nodes */
55  bool operator==(const Node& other) const {
56  if (this == &other) {
57  return true;
58  } else if (typeid(*this) == typeid(*&other)) {
59  return equal(other);
60  }
61  return false;
62  }
63 
64  /** Inequality check for two AST nodes */
65  bool operator!=(const Node& other) const {
66  return !(*this == other);
67  }
68 
69  /** Create a clone (i.e. deep copy) of this node */
70  virtual Node* clone() const = 0;
71 
72  /** Apply the mapper to all child nodes */
73  virtual void apply(const NodeMapper& /* mapper */) {}
74 
75  /** Obtain a list of all embedded AST child nodes */
76  virtual std::vector<const Node*> getChildNodes() const {
77  return {};
78  }
79 
80  /** Print node onto an output stream */
81  friend std::ostream& operator<<(std::ostream& out, const Node& node) {
82  node.print(out);
83  return out;
84  }
85 
86 protected:
87  /** Output to a given output stream */
88  virtual void print(std::ostream& os) const = 0;
89 
90  /** Abstract equality check for two AST nodes */
91  virtual bool equal(const Node& /* other */) const {
92  return true;
93  }
94 
95 private:
96  /** Source location of a syntactic element */
98 };
99 
100 } // namespace souffle::ast
souffle::ast::Node::operator==
bool operator==(const Node &other) const
Equivalence check for two AST nodes.
Definition: Node.h:61
SrcLocation.h
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
souffle::ast::Node::location
SrcLocation location
Source location of a syntactic element.
Definition: Node.h:103
souffle::ast::Node::print
virtual void print(std::ostream &os) const =0
Output to a given output stream.
souffle::ast::Node::extloc
std::string extloc() const
Return source location of the syntactic element.
Definition: Node.h:56
l
var l
Definition: htmlJsChartistMin.h:15
souffle::ast::Node::operator!=
bool operator!=(const Node &other) const
Inequality check for two AST nodes.
Definition: Node.h:71
souffle::ast::Node::operator<<
friend std::ostream & operator<<(std::ostream &out, const Node &node)
Print node onto an output stream.
Definition: Node.h:87
souffle::ast::Node::clone
virtual Node * clone() const =0
Create a clone (i.e.
souffle::ast::Node::~Node
virtual ~Node()=default
souffle::ast::Node::getChildNodes
virtual std::vector< const Node * > getChildNodes() const
Obtain a list of all embedded AST child nodes.
Definition: Node.h:82
souffle::ast::Node::equal
virtual bool equal(const Node &) const
Abstract equality check for two AST nodes.
Definition: Node.h:97
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
souffle::SrcLocation
A class describing a range in an input file.
Definition: SrcLocation.h:32
souffle::ast::Node::setSrcLoc
void setSrcLoc(SrcLocation l)
Set source location for the Node.
Definition: Node.h:51
souffle::ast::Node::Node
Node(SrcLocation loc={})
Definition: Node.h:42
souffle::ast::Node::getSrcLoc
const SrcLocation & getSrcLoc() const
Return source location of the Node.
Definition: Node.h:46
souffle::ast
Definition: Aggregator.h:35
souffle::ast::Node::apply
virtual void apply(const NodeMapper &)
Apply the mapper to all child nodes.
Definition: Node.h:79
souffle::SrcLocation::extloc
std::string extloc() const
An extended string describing this location in a end-user friendly way.
Definition: SrcLocation.cpp:93