P4C
The P4 Compiler
Loading...
Searching...
No Matches
inode.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef IR_INODE_H_
9#define IR_INODE_H_
10
11#include "ir/gen-tree-macro.h"
12#include "lib/castable.h"
13#include "lib/cstring.h"
14#include "lib/exceptions.h"
15#include "lib/rtti.h"
16#include "lib/source_file.h"
17
18namespace P4 {
19class JSONGenerator;
20class JSONLoader;
21} // namespace P4
22
23namespace P4::IR {
24
25class Node;
26class Annotation;
27template <class>
28class Vector;
29
32template <class, class = void>
33struct has_static_type_name : std::false_type {};
34
35template <class T>
36struct has_static_type_name<T, std::void_t<decltype(T::static_type_name())>> : std::true_type {};
37
38template <class T>
39inline constexpr bool has_static_type_name_v = has_static_type_name<T>::value;
40
41// node interface
42class INode : public Util::IHasSourceInfo, public IHasDbPrint, public ICastable {
43 public:
44 virtual ~INode() {}
45 virtual const Node *getNode() const = 0;
46 virtual Node *getNode() = 0;
47 virtual void toJSON(JSONGenerator &) const = 0;
48 virtual cstring node_type_name() const = 0;
49 virtual void validate() const {}
50
51 // default checkedTo implementation for nodes: just fallback to generic ICastable method
52 template <typename T>
53 std::enable_if_t<!has_static_type_name_v<T>, const T *> checkedTo() const {
55 }
56
57 // alternative checkedTo implementation that produces slightly better error message
58 // due to node_type_name() / static_type_name() being available
59 template <typename T>
60 std::enable_if_t<has_static_type_name_v<T>, const T *> checkedTo() const {
61 const auto *result = to<T>();
62 BUG_CHECK(result, "Cast failed: %1% with type %2% is not a %3%.", this, node_type_name(),
63 T::static_type_name());
64 return result;
65 }
66
67 // helpers for getting annotations
68 bool hasAnnotation(cstring) const;
69 const Annotation *getAnnotation(cstring) const;
70 const Vector<Annotation> &getAnnotations() const;
71
72 DECLARE_TYPEINFO_WITH_TYPEID(INode, NodeKind::INode);
73};
74
75} // namespace P4::IR
76
77#endif /* IR_INODE_H_ */
Definition castable.h:27
const T * checkedTo() const
Performs a checked cast. A BUG occurs if the cast fails.
Definition castable.h:45
Definition stringify.h:33
Definition inode.h:42
Definition node.h:53
Definition ir/vector.h:59
Definition json_generator.h:30
Definition json_loader.h:32
Definition source_file.h:223
Definition cstring.h:85
Definition constantParsing.h:22
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
STL namespace.
Definition inode.h:33
T * to() noexcept
Definition rtti.h:226