17#ifndef P4_PARSEANNOTATIONS_H_
18#define P4_PARSEANNOTATIONS_H_
20#include "frontends/p4/typeChecking/typeChecker.h"
21#include "frontends/parsers/parserDriver.h"
30#define PARSE_SKIP(aname) \
31 { aname, &P4::ParseAnnotations::parseSkip }
34#define PARSE_EMPTY(aname) \
35 { aname, &P4::ParseAnnotations::parseEmpty }
38#define PARSE(aname, tname) \
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); \
46 return parsed != nullptr; \
51#define PARSE_CONSTANT_OR_STRING_LITERAL(aname) \
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); \
59 return parsed != nullptr; \
64#define PARSE_PAIR(aname, tname) \
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); \
72 return parsed != nullptr; \
77#define PARSE_TRIPLE(aname, tname) \
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); \
85 return parsed != nullptr; \
90#define PARSE_EXPRESSION_LIST(aname) \
91 { aname, &P4::ParseAnnotations::parseExpressionList }
94#define PARSE_KV_LIST(aname) \
95 { aname, &P4::ParseAnnotations::parseKvList }
98#define PARSE_CONSTANT_LIST(aname) \
99 { aname, &P4::ParseAnnotations::parseConstantList }
103#define PARSE_CONSTANT_OR_STRING_LITERAL_LIST(aname) \
104 { aname, &P4::ParseAnnotations::parseConstantOrStringLiteralList }
107#define PARSE_STRING_LITERAL_LIST(aname) \
108 { aname, &P4::ParseAnnotations::parseStringLiteralList }
111#define PARSE_P4RUNTIME_TRANSLATION(aname) \
112 { aname, &P4::ParseAnnotations::parseP4rtTranslationAnnotation }
116 using Modifier::postorder;
120 typedef std::function<bool(IR::Annotation *)>
Handler;
126 explicit ParseAnnotations(
bool warn =
false) : warnUnknown(warn), handlers(standardHandlers()) {
127 setName(
"ParseAnnotations");
133 : warnUnknown(warn) {
134 std::string buf = targetName;
135 buf +=
"__ParseAnnotations";
136 setName(buf.c_str());
138 if (includeStandard) {
139 this->handlers = standardHandlers();
140 this->handlers.insert(handlers.begin(), handlers.end());
142 this->handlers = handlers;
146 void postorder(IR::Annotation *annotation)
final;
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);
158 static bool parseP4rtTranslationAnnotation(IR::Annotation *annotation);
160 void addHandler(
cstring name,
Handler h) { handlers.insert({name, h}); }
164 const bool warnUnknown;
168 std::set<cstring> warned;
177 passes.push_back(pa);
179 setName(
"ParseAnnotationBodies");
Definition typeChecker.h:32
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
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24