22#include "ir-tree-macros.h"
23#include "ir/gen-tree-macro.h"
24#include "lib/castable.h"
25#include "lib/cstring.h"
26#include "lib/exceptions.h"
27#include "lib/source_file.h"
31struct Visitor_Context;
56template <
class,
class =
void>
69 virtual const Node *getNode()
const = 0;
70 virtual Node *getNode() = 0;
72 virtual cstring node_type_name()
const = 0;
73 virtual void validate()
const {}
74 virtual const Annotation *getAnnotation(
cstring)
const {
return nullptr; }
78 std::enable_if_t<!has_static_type_name_v<T>,
const T *> checkedTo()
const {
85 std::enable_if_t<has_static_type_name_v<T>,
const T *> checkedTo()
const {
86 const auto *result =
to<T>();
87 BUG_CHECK(result,
"Cast failed: %1% with type %2% is not a %3%.",
this, node_type_name(),
88 T::static_type_name());
92 DECLARE_TYPEINFO_WITH_TYPEID(
INode, NodeKind::INode);
97 virtual bool apply_visitor_preorder(
Modifier &v);
98 virtual void apply_visitor_postorder(
Modifier &v);
99 virtual void apply_visitor_revisit(
Modifier &v,
const Node *n)
const;
100 virtual void apply_visitor_loop_revisit(
Modifier &v)
const;
101 virtual bool apply_visitor_preorder(
Inspector &v)
const;
102 virtual void apply_visitor_postorder(
Inspector &v)
const;
103 virtual void apply_visitor_revisit(
Inspector &v)
const;
104 virtual void apply_visitor_loop_revisit(
Inspector &v)
const;
107 virtual void apply_visitor_revisit(
Transform &v,
const Node *n)
const;
108 virtual void apply_visitor_loop_revisit(
Transform &v)
const;
109 Node &operator=(
const Node &) =
default;
113 static int currentId;
114 void traceVisit(
const char *visitor)
const;
115 friend class ::P4::Visitor;
116 friend class ::P4::Inspector;
117 friend class ::P4::Modifier;
118 friend class ::P4::Transform;
120 unsigned *columnNumber)
const;
126 void traceCreation()
const;
127 Node() : id(currentId++), clone_id(
id) { traceCreation(); }
131 Node(
const Node &other) : srcInfo(other.srcInfo), id(currentId++), clone_id(other.clone_id) {
137 return apply(v, ctxt);
139 virtual Node *clone()
const = 0;
140 void dbprint(std::ostream &out)
const override;
141 virtual void dump_fields(std::ostream &)
const {}
142 const Node *getNode()
const final {
return this; }
143 Node *getNode()
final {
return this; }
145 cstring node_type_name()
const override {
return "Node"_cs; }
146 static cstring static_type_name() {
return "Node"_cs; }
147 virtual int num_children() {
return 0; }
149 cstring toString()
const override {
return node_type_name(); }
155 virtual bool operator==(
const Node &a)
const {
return this->
typeId() == a.
typeId(); }
158 virtual bool equiv(
const Node &a)
const {
return this->
typeId() == a.
typeId(); }
159#define DEFINE_OPEQ_FUNC(CLASS, BASE) \
160 virtual bool operator==(const CLASS &) const { return false; }
161 IRNODE_ALL_SUBCLASSES(DEFINE_OPEQ_FUNC)
162#undef DEFINE_OPEQ_FUNC
163 virtual void visit_children(
Visitor &) {}
164 virtual void visit_children(
Visitor &)
const {}
166 bool operator!=(
const Node &n)
const {
return !operator==(n); }
168 DECLARE_TYPEINFO_WITH_TYPEID(
Node, NodeKind::Node,
INode);
174inline bool equal(
const Node *a,
const Node *b) {
return a == b || (a && b && *a == *b); }
175inline bool equal(
const INode *a,
const INode *b) {
176 return a == b || (a && b && *a->getNode() == *b->getNode());
178inline bool equiv(
const Node *a,
const Node *b) {
return a == b || (a && b && a->equiv(*b)); }
179inline bool equiv(
const INode *a,
const INode *b) {
180 return a == b || (a && b && a->getNode()->equiv(*b->getNode()));
184#define IRNODE_SUBCLASS(T) \
186 T *clone() const override { return new T(*this); } \
187 IRNODE_COMMON_SUBCLASS(T)
188#define IRNODE_ABSTRACT_SUBCLASS(T) \
190 T *clone() const override = 0; \
191 IRNODE_COMMON_SUBCLASS(T)
194#define IRNODE_COMMON_SUBCLASS(T) \
196 using Node::operator==; \
197 bool apply_visitor_preorder(Modifier &v) override; \
198 void apply_visitor_postorder(Modifier &v) override; \
199 void apply_visitor_revisit(Modifier &v, const Node *n) const override; \
200 void apply_visitor_loop_revisit(Modifier &v) const override; \
201 bool apply_visitor_preorder(Inspector &v) const override; \
202 void apply_visitor_postorder(Inspector &v) const override; \
203 void apply_visitor_revisit(Inspector &v) const override; \
204 void apply_visitor_loop_revisit(Inspector &v) const override; \
205 const Node *apply_visitor_preorder(Transform &v) override; \
206 const Node *apply_visitor_postorder(Transform &v) override; \
207 void apply_visitor_revisit(Transform &v, const Node *n) const override; \
208 void apply_visitor_loop_revisit(Transform &v) const override;
213#define IRNODE_DECLARE_APPLY_OVERLOAD(T) \
214 const T *apply(Visitor &v, const Visitor_Context *ctxt = nullptr) const; \
215 const T *apply(Visitor &&v, const Visitor_Context *ctxt = nullptr) const { \
216 return apply(v, ctxt); \
218#define IRNODE_DEFINE_APPLY_OVERLOAD(CLASS, TEMPLATE, TT) \
220 const IR::CLASS TT *IR::CLASS TT::apply(Visitor &v, const Visitor_Context *ctxt) const { \
221 const CLASS *tmp = this; \
222 auto prof = v.init_apply(tmp, ctxt); \
const T * checkedTo() const
Performs a checked cast. A BUG occurs if the cast fails.
Definition castable.h:54
Definition stringify.h:33
Definition json_generator.h:36
Definition json_loader.h:38
Definition source_file.h:221
Definition source_file.h:125
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
T * to() noexcept
Definition rtti.h:226
virtual TypeId typeId() const noexcept=0