P4C
The P4 Compiler
Loading...
Searching...
No Matches
inode.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef IR_INODE_H_
18#define IR_INODE_H_
19
20#include "ir/gen-tree-macro.h"
21#include "lib/castable.h"
22#include "lib/cstring.h"
23#include "lib/exceptions.h"
24#include "lib/rtti.h"
25#include "lib/source_file.h"
26
27namespace P4 {
28class JSONGenerator;
29class JSONLoader;
30} // namespace P4
31
32namespace P4::IR {
33
34class Node;
35
38template <class, class = void>
39struct has_static_type_name : std::false_type {};
40
41template <class T>
42struct has_static_type_name<T, std::void_t<decltype(T::static_type_name())>> : std::true_type {};
43
44template <class T>
45inline constexpr bool has_static_type_name_v = has_static_type_name<T>::value;
46
47// node interface
48class INode : public Util::IHasSourceInfo, public IHasDbPrint, public ICastable {
49 public:
50 virtual ~INode() {}
51 virtual const Node *getNode() const = 0;
52 virtual Node *getNode() = 0;
53 virtual void toJSON(JSONGenerator &) const = 0;
54 virtual cstring node_type_name() const = 0;
55 virtual void validate() const {}
56
57 // default checkedTo implementation for nodes: just fallback to generic ICastable method
58 template <typename T>
59 std::enable_if_t<!has_static_type_name_v<T>, const T *> checkedTo() const {
61 }
62
63 // alternative checkedTo implementation that produces slightly better error message
64 // due to node_type_name() / static_type_name() being available
65 template <typename T>
66 std::enable_if_t<has_static_type_name_v<T>, const T *> checkedTo() const {
67 const auto *result = to<T>();
68 BUG_CHECK(result, "Cast failed: %1% with type %2% is not a %3%.", this, node_type_name(),
69 T::static_type_name());
70 return result;
71 }
72
73 DECLARE_TYPEINFO_WITH_TYPEID(INode, NodeKind::INode);
74};
75
76} // namespace P4::IR
77
78#endif /* IR_INODE_H_ */
Definition castable.h:36
const T * checkedTo() const
Performs a checked cast. A BUG occurs if the cast fails.
Definition castable.h:54
Definition stringify.h:33
Definition inode.h:48
Definition node.h:53
Definition json_generator.h:39
Definition json_loader.h:41
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:24
STL namespace.
Definition inode.h:39
T * to() noexcept
Definition rtti.h:226