P4C
The P4 Compiler
Loading...
Searching...
No Matches
parseAnnotations.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 P4_PARSEANNOTATIONS_H_
18#define P4_PARSEANNOTATIONS_H_
19
20#include "frontends/p4/typeChecking/typeChecker.h"
21#include "frontends/parsers/parserDriver.h"
22#include "ir/ir.h"
23
24/*
25 * Parses known/predefined annotations used by the compiler.
26 */
27namespace P4 {
28
29// A no-op handler. Useful for avoiding warnings about ignored annotations.
30#define PARSE_SKIP(aname) \
31 { aname, &P4::ParseAnnotations::parseSkip }
32
33// Parses an empty annotation.
34#define PARSE_EMPTY(aname) \
35 { aname, &P4::ParseAnnotations::parseEmpty }
36
37// Parses an annotation with a single-element body.
38#define PARSE(aname, tname) \
39 { \
40 aname, [](IR::Annotation *annotation) { \
41 const IR::tname *parsed = \
42 P4::P4ParserDriver::parse##tname(annotation->srcInfo, annotation->body); \
43 if (parsed != nullptr) { \
44 annotation->expr.push_back(parsed); \
45 } \
46 return parsed != nullptr; \
47 } \
48 }
49
50// Parses an annotation that is either an integer constant or a string literal.
51#define PARSE_CONSTANT_OR_STRING_LITERAL(aname) \
52 { \
53 aname, [](IR::Annotation *annotation) { \
54 const IR::Expression *parsed = P4::P4ParserDriver::parseConstantOrStringLiteral( \
55 annotation->srcInfo, annotation->body); \
56 if (parsed != nullptr) { \
57 annotation->expr.push_back(parsed); \
58 } \
59 return parsed != nullptr; \
60 } \
61 }
62
63// Parses an annotation whose body is a pair.
64#define PARSE_PAIR(aname, tname) \
65 { \
66 aname, [](IR::Annotation *annotation) { \
67 const IR::Vector<IR::Expression> *parsed = \
68 P4::P4ParserDriver::parse##tname##Pair(annotation->srcInfo, annotation->body); \
69 if (parsed != nullptr) { \
70 annotation->expr.append(*parsed); \
71 } \
72 return parsed != nullptr; \
73 } \
74 }
75
76// Parses an annotation whose body is a triple.
77#define PARSE_TRIPLE(aname, tname) \
78 { \
79 aname, [](IR::Annotation *annotation) { \
80 const IR::Vector<IR::Expression> *parsed = \
81 P4::P4ParserDriver::parse##tname##Triple(annotation->srcInfo, annotation->body); \
82 if (parsed != nullptr) { \
83 annotation->expr.append(*parsed); \
84 } \
85 return parsed != nullptr; \
86 } \
87 }
88
89// Parses an annotation whose body is a list of expressions.
90#define PARSE_EXPRESSION_LIST(aname) \
91 { aname, &P4::ParseAnnotations::parseExpressionList }
92
93// Parses an annotation whose body is a list of key-value pairs.
94#define PARSE_KV_LIST(aname) \
95 { aname, &P4::ParseAnnotations::parseKvList }
96
97// Parses an annotation whose body is a list of integer constants.
98#define PARSE_CONSTANT_LIST(aname) \
99 { aname, &P4::ParseAnnotations::parseConstantList }
100
101// Parses an annotation whose body is a list, where each element is an integer constant or a string
102// literal.
103#define PARSE_CONSTANT_OR_STRING_LITERAL_LIST(aname) \
104 { aname, &P4::ParseAnnotations::parseConstantOrStringLiteralList }
105
106// Parses an annotation whose body is a list of string literals.
107#define PARSE_STRING_LITERAL_LIST(aname) \
108 { aname, &P4::ParseAnnotations::parseStringLiteralList }
109
110// Parses a P4Runtime translation which contains both types or expressions.
111#define PARSE_P4RUNTIME_TRANSLATION(aname) \
112 { aname, &P4::ParseAnnotations::parseP4rtTranslationAnnotation }
113
115 public:
116 using Modifier::postorder;
117
120 typedef std::function<bool(IR::Annotation *)> Handler;
121
123 typedef std::unordered_map<cstring, Handler> HandlerMap;
124
126 explicit ParseAnnotations(bool warn = false) : warnUnknown(warn), handlers(standardHandlers()) {
127 setName("ParseAnnotations");
128 }
129
131 ParseAnnotations(const char *targetName, bool includeStandard, HandlerMap handlers,
132 bool warn = false)
133 : warnUnknown(warn) {
134 std::string buf = targetName;
135 buf += "__ParseAnnotations";
136 setName(buf.c_str());
137
138 if (includeStandard) {
139 this->handlers = standardHandlers();
140 this->handlers.insert(handlers.begin(), handlers.end());
141 } else {
142 this->handlers = handlers;
143 }
144 }
145
146 void postorder(IR::Annotation *annotation) final;
147
148 static HandlerMap standardHandlers();
149
150 static bool parseSkip(IR::Annotation *annotation);
151 static bool parseEmpty(IR::Annotation *annotation);
152 static bool parseExpressionList(IR::Annotation *annotation);
153 static bool parseKvList(IR::Annotation *annotation);
154 static bool parseConstantList(IR::Annotation *annotation);
155 static bool parseConstantOrStringLiteralList(IR::Annotation *annotation);
156 static bool parseStringLiteralList(IR::Annotation *annotation);
157 // Parses a `@p4runtime_translation` annotation.
158 static bool parseP4rtTranslationAnnotation(IR::Annotation *annotation);
159
160 void addHandler(cstring name, Handler h) { handlers.insert({name, h}); }
161
162 private:
164 const bool warnUnknown;
165
168 std::set<cstring> warned;
169
170 HandlerMap handlers;
171};
172
174class ParseAnnotationBodies final : public PassManager {
175 public:
177 passes.push_back(pa);
178 passes.push_back(new ClearTypeMap(typeMap));
179 setName("ParseAnnotationBodies");
180 }
181};
182
183} // namespace P4
184
185#endif /* P4_PARSEANNOTATIONS_H_ */
Definition typeChecker.h:32
Definition visitor.h:372
Clears a type map after calling a ParseAnnotations instance.
Definition parseAnnotations.h:174
Definition parseAnnotations.h:114
ParseAnnotations(bool warn=false)
Produces a pass that rewrites the spec-defined annotations.
Definition parseAnnotations.h:126
std::function< bool(IR::Annotation *)> Handler
Definition parseAnnotations.h:120
std::unordered_map< cstring, Handler > HandlerMap
Keyed on annotation names.
Definition parseAnnotations.h:123
ParseAnnotations(const char *targetName, bool includeStandard, HandlerMap handlers, bool warn=false)
Produces a pass that rewrites a custom set of annotations.
Definition parseAnnotations.h:131
Definition ir/pass_manager.h:40
Definition typeMap.h:41
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24