P4C
The P4 Compiler
Loading...
Searching...
No Matches
validateMatchAnnotations.h
1/*
2Copyright 2019 VMware, 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_VALIDATEMATCHANNOTATIONS_H_
18#define P4_VALIDATEMATCHANNOTATIONS_H_
19
20#include "frontends/p4/typeMap.h"
21#include "ir/ir.h"
22#include "lib/error.h"
23
24namespace P4 {
25
29class ValidateMatchAnnotations final : public Inspector {
30 TypeMap *typeMap;
31
32 public:
33 explicit ValidateMatchAnnotations(TypeMap *typeMap) : typeMap(typeMap) {
34 setName("ValidateMatchAnnotations");
35 }
36 void postorder(const IR::Annotation *annotation) override {
37 if (annotation->name != IR::Annotation::matchAnnotation) return;
38 if (!findContext<IR::StructField>()) return;
39 if (annotation->expr.size() != 1)
40 ::P4::error(ErrorType::ERR_INVALID, "%1%: annotation must have exactly 1 argument",
41 annotation);
42 auto e0 = annotation->expr.at(0);
43 auto type = typeMap->getType(e0, true);
44 if (type == nullptr) return;
45 if (!type->is<IR::Type_MatchKind>())
46 ::P4::error(ErrorType::ERR_TYPE_ERROR, "%1%: value must be a match_kind", e0);
47 }
48};
49
50} // namespace P4
51
52#endif /* P4_VALIDATEMATCHANNOTATIONS_H_ */
Definition visitor.h:400
Definition typeMap.h:41
Definition validateMatchAnnotations.h:29
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