P4C
The P4 Compiler
Loading...
Searching...
No Matches
validateStringAnnotations.h
1/*
2Licensed under the Apache License, Version 2.0 (the "License");
3you may not use this file except in compliance with the License.
4You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8Unless required by applicable law or agreed to in writing, software
9distributed under the License is distributed on an "AS IS" BASIS,
10WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11See the License for the specific language governing permissions and
12limitations under the License.
13*/
14
15#ifndef FRONTENDS_P4_VALIDATESTRINGANNOTATIONS_H_
16#define FRONTENDS_P4_VALIDATESTRINGANNOTATIONS_H_
17
18#include "frontends/p4/typeMap.h"
19#include "ir/ir.h"
20#include "lib/error.h"
21
24namespace P4 {
25
29 public:
30 explicit ValidateStringAnnotations() = default;
31
32 void postorder(const IR::Annotation *annotation) override {
33 const auto name = annotation->name;
34 if (name != IR::Annotation::nameAnnotation &&
35 name != IR::Annotation::deprecatedAnnotation &&
36 name != IR::Annotation::noWarnAnnotation) {
37 return;
38 }
39 // FIXME: Validate annotation type
40 const auto &expr = annotation->getExpr();
41 if (expr.size() != 1) {
42 error(ErrorType::ERR_INVALID, "%1%: annotation must have exactly 1 argument",
43 annotation);
44 }
45 const auto *e0 = expr.at(0);
46 if (!e0->is<IR::StringLiteral>()) {
47 error(ErrorType::ERR_TYPE_ERROR, "%1%: @%2% annotation's value must be a string", e0,
48 annotation->name.originalName);
49 }
50 }
51};
52
53} // namespace P4
54
55#endif /* FRONTENDS_P4_VALIDATESTRINGANNOTATIONS_H_ */
Definition visitor.h:400
Definition validateStringAnnotations.h:28
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51