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"
54template <
class,
class =
void>
67 virtual const Node *getNode()
const = 0;
68 virtual Node *getNode() = 0;
70 virtual cstring node_type_name()
const = 0;
71 virtual void validate()
const {}
72 virtual const Annotation *getAnnotation(
cstring)
const {
return nullptr; }
76 std::enable_if_t<!has_static_type_name_v<T>,
const T *> checkedTo()
const {
83 std::enable_if_t<has_static_type_name_v<T>,
const T *> checkedTo()
const {
84 const auto *result =
to<T>();
85 BUG_CHECK(result,
"Cast failed: %1% with type %2% is not a %3%.",
this, node_type_name(),
86 T::static_type_name());
90 DECLARE_TYPEINFO_WITH_TYPEID(
INode, NodeKind::INode);
95 virtual bool apply_visitor_preorder(
Modifier &v);
96 virtual void apply_visitor_postorder(
Modifier &v);
97 virtual void apply_visitor_revisit(
Modifier &v,
const Node *n)
const;
98 virtual void apply_visitor_loop_revisit(
Modifier &v)
const;
99 virtual bool apply_visitor_preorder(
Inspector &v)
const;
100 virtual void apply_visitor_postorder(
Inspector &v)
const;
101 virtual void apply_visitor_revisit(
Inspector &v)
const;
102 virtual void apply_visitor_loop_revisit(
Inspector &v)
const;
105 virtual void apply_visitor_revisit(
Transform &v,
const Node *n)
const;
106 virtual void apply_visitor_loop_revisit(
Transform &v)
const;
107 Node &operator=(
const Node &) =
default;
111 static int currentId;
112 void traceVisit(
const char *visitor)
const;
113 friend class ::Visitor;
114 friend class ::Inspector;
115 friend class ::Modifier;
116 friend class ::Transform;
118 unsigned *columnNumber)
const;
124 void traceCreation()
const;
125 Node() : id(currentId++), clone_id(
id) { traceCreation(); }
129 Node(
const Node &other) : srcInfo(other.srcInfo), id(currentId++), clone_id(other.clone_id) {
135 return apply(v, ctxt);
137 virtual Node *clone()
const = 0;
138 void dbprint(std::ostream &out)
const override;
139 virtual void dump_fields(std::ostream &)
const {}
140 const Node *getNode()
const final {
return this; }
141 Node *getNode()
final {
return this; }
143 cstring node_type_name()
const override {
return "Node"_cs; }
144 static cstring static_type_name() {
return "Node"_cs; }
145 virtual int num_children() {
return 0; }
147 cstring toString()
const override {
return node_type_name(); }
153 virtual bool operator==(
const Node &a)
const {
return this->
typeId() == a.
typeId(); }
156 virtual bool equiv(
const Node &a)
const {
return this->
typeId() == a.
typeId(); }
157#define DEFINE_OPEQ_FUNC(CLASS, BASE) \
158 virtual bool operator==(const CLASS &) const { return false; }
159 IRNODE_ALL_SUBCLASSES(DEFINE_OPEQ_FUNC)
160#undef DEFINE_OPEQ_FUNC
161 virtual void visit_children(
Visitor &) {}
162 virtual void visit_children(
Visitor &)
const {}
164 bool operator!=(
const Node &n)
const {
return !operator==(n); }
166 DECLARE_TYPEINFO_WITH_TYPEID(
Node, NodeKind::Node,
INode);
172inline bool equal(
const Node *a,
const Node *b) {
return a == b || (a && b && *a == *b); }
173inline bool equal(
const INode *a,
const INode *b) {
174 return a == b || (a && b && *a->getNode() == *b->getNode());
176inline bool equiv(
const Node *a,
const Node *b) {
return a == b || (a && b && a->equiv(*b)); }
177inline bool equiv(
const INode *a,
const INode *b) {
178 return a == b || (a && b && a->getNode()->equiv(*b->getNode()));
182#define IRNODE_SUBCLASS(T) \
184 T *clone() const override { return new T(*this); } \
185 IRNODE_COMMON_SUBCLASS(T)
186#define IRNODE_ABSTRACT_SUBCLASS(T) \
188 T *clone() const override = 0; \
189 IRNODE_COMMON_SUBCLASS(T)
192#define IRNODE_COMMON_SUBCLASS(T) \
194 using Node::operator==; \
195 bool apply_visitor_preorder(Modifier &v) override; \
196 void apply_visitor_postorder(Modifier &v) override; \
197 void apply_visitor_revisit(Modifier &v, const Node *n) const override; \
198 void apply_visitor_loop_revisit(Modifier &v) const override; \
199 bool apply_visitor_preorder(Inspector &v) const override; \
200 void apply_visitor_postorder(Inspector &v) const override; \
201 void apply_visitor_revisit(Inspector &v) const override; \
202 void apply_visitor_loop_revisit(Inspector &v) const override; \
203 const Node *apply_visitor_preorder(Transform &v) override; \
204 const Node *apply_visitor_postorder(Transform &v) override; \
205 void apply_visitor_revisit(Transform &v, const Node *n) const override; \
206 void apply_visitor_loop_revisit(Transform &v) const override;
211#define IRNODE_DECLARE_APPLY_OVERLOAD(T) \
212 const T *apply(Visitor &v, const Visitor_Context *ctxt = nullptr) const; \
213 const T *apply(Visitor &&v, const Visitor_Context *ctxt = nullptr) const { \
214 return apply(v, ctxt); \
216#define IRNODE_DEFINE_APPLY_OVERLOAD(CLASS, TEMPLATE, TT) \
218 const IR::CLASS TT *IR::CLASS TT::apply(Visitor &v, const Visitor_Context *ctxt) const { \
219 const CLASS *tmp = this; \
220 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:52
Definition stringify.h:31
Definition json_generator.h:34
Definition json_loader.h:36
Definition json_parser.h:68
Definition source_file.h:210
Definition source_file.h:123
T * to() noexcept
Definition rtti.h:226
virtual TypeId typeId() const noexcept=0