P4C
The P4 Compiler
Loading...
Searching...
No Matches
ir-tree-macros.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_IR_TREE_MACROS_H_
18#define IR_IR_TREE_MACROS_H_
19
20/* macro table listing ALL IR subclasses of Node and all their direct and indirect bases
21 * whenever a new IR Node subclass is added, it MUST be added to this table. Target
22 * specific subclasses might be broken out into a separate table and included here
23 * Because we want to be able to forward-declare all these types, the class type MUST
24 * be a simple identifier that will be declared in namespace P4::IR. Aliases can be defined
25 * in other namespaces if desired.
26 * When there's a templated subclass of Node, all of its instantiations need to appear in this
27 * table, and it also needs to be in the IRNODE_ALL_TEMPLATES_AND_DIRECT_AND_INDIRECT_BASES
28 * table below */
29#include "ir/gen-tree-macro.h"
30// clang-format off
31#define IRNODE_ALL_TEMPLATES_AND_DIRECT_AND_INDIRECT_BASES(M, D, B, TDA, ...) \
32 M(Vector, D(Node), template <class T>, <T>, ##__VA_ARGS__) \
33 M(IndexedVector, D(Vector<T>) B(Node), template <class T>, <T>, ##__VA_ARGS__) \
34 M(NameMap, D(Node), \
35 IR_TREE_COPY( \
36 template <class T, template <class, class, class, class> class MAP TDA(= std::map), \
37 class COMP TDA(= std::less<cstring>), \
38 class ALLOC TDA(= std::allocator<std::pair<cstring, const T*>>)>), \
39 IR_TREE_COPY(<T, MAP, COMP, ALLOC>), ##__VA_ARGS__)
40// clang-format on
41
42#define IR_TREE_COPY(...) __VA_ARGS__
43#define IR_TREE_IGNORE(...)
44
45/* all IR classes, including Node */
46#define IRNODE_ALL_CLASSES_AND_BASES(M, B, ...) \
47 M(Node, , ##__VA_ARGS__) \
48 IRNODE_ALL_SUBCLASSES_AND_DIRECT_AND_INDIRECT_BASES(M, M, B, B, ##__VA_ARGS__)
49
50#define IRNODE_ALL_NON_TEMPLATE_CLASSES_AND_BASES(M, B, ...) \
51 M(Node, , ##__VA_ARGS__) \
52 IRNODE_ALL_SUBCLASSES_AND_DIRECT_AND_INDIRECT_BASES(M, IR_TREE_IGNORE, B, B, ##__VA_ARGS__)
53
54/* all the subclasses with just the immediate bases */
55#define IRNODE_ALL_SUBCLASSES(M, ...) \
56 IRNODE_ALL_SUBCLASSES_AND_DIRECT_AND_INDIRECT_BASES(M, M, IR_TREE_COPY, IR_TREE_IGNORE, \
57 ##__VA_ARGS__)
58#define IRNODE_ALL_NON_TEMPLATE_SUBCLASSES(M, ...) \
59 IRNODE_ALL_SUBCLASSES_AND_DIRECT_AND_INDIRECT_BASES(M, IR_TREE_IGNORE, IR_TREE_COPY, \
60 IR_TREE_IGNORE, ##__VA_ARGS__)
61#define IRNODE_ALL_TEMPLATES_AND_BASES(M, ...) \
62 IRNODE_ALL_TEMPLATES_AND_DIRECT_AND_INDIRECT_BASES(M, IR_TREE_COPY, IR_TREE_IGNORE, \
63 IR_TREE_IGNORE, ##__VA_ARGS__)
64
65/* all classes with no bases */
66#define REMOVE_BASES_ARG(CLASS, BASES, M, ...) M(IR_TREE_COPY(CLASS), ##__VA_ARGS__)
67#define IRNODE_ALL_CLASSES(M, ...) \
68 IRNODE_ALL_CLASSES_AND_BASES(REMOVE_BASES_ARG, IR_TREE_IGNORE, M, ##__VA_ARGS__)
69#define IRNODE_ALL_NON_TEMPLATE_CLASSES(M, ...) \
70 IRNODE_ALL_NON_TEMPLATE_CLASSES_AND_BASES(REMOVE_BASES_ARG, IR_TREE_IGNORE, M, ##__VA_ARGS__)
71
72#define REMOVE_TEMPLATE_BASES_ARG(CLASS, BASES, TEMPLATE, TARGS, M, ...) \
73 M(IR_TREE_COPY(CLASS), IR_TREE_COPY(TEMPLATE), IR_TREE_COPY(TARGS), ##__VA_ARGS__)
74#define IRNODE_ALL_TEMPLATES(M, ...) \
75 IRNODE_ALL_TEMPLATES_AND_DIRECT_AND_INDIRECT_BASES(REMOVE_TEMPLATE_BASES_ARG, IR_TREE_IGNORE, \
76 IR_TREE_IGNORE, IR_TREE_IGNORE, M, \
77 ##__VA_ARGS__)
78#define IRNODE_ALL_TEMPLATES_WITH_DEFAULTS(M, ...) \
79 IRNODE_ALL_TEMPLATES_AND_DIRECT_AND_INDIRECT_BASES( \
80 REMOVE_TEMPLATE_BASES_ARG, IR_TREE_IGNORE, IR_TREE_IGNORE, IR_TREE_COPY, M, ##__VA_ARGS__)
81
82#endif /* IR_IR_TREE_MACROS_H_ */