P4C
The P4 Compiler
Loading...
Searching...
No Matches
validateMatchAnnotations.h
1/*
2 * Copyright 2019 VMware, Inc.
3 * SPDX-FileCopyrightText: 2019 VMware, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_P4_VALIDATEMATCHANNOTATIONS_H_
9#define FRONTENDS_P4_VALIDATEMATCHANNOTATIONS_H_
10
11#include "frontends/p4/typeMap.h"
12#include "ir/ir.h"
13#include "lib/error.h"
14
15namespace P4 {
16
20class ValidateMatchAnnotations final : public Inspector {
21 TypeMap *typeMap;
22
23 public:
24 explicit ValidateMatchAnnotations(TypeMap *typeMap) : typeMap(typeMap) {
25 setName("ValidateMatchAnnotations");
26 }
27 void postorder(const IR::Annotation *annotation) override {
28 if (annotation->name != IR::Annotation::matchAnnotation) return;
29 if (!isInContext<IR::StructField>()) return;
30 // FIXME: Check annotation kind
31 const auto &expr = annotation->getExpr();
32 if (expr.size() != 1)
33 ::P4::error(ErrorType::ERR_INVALID, "%1%: annotation must have exactly 1 argument",
34 annotation);
35 auto e0 = expr.at(0);
36 auto type = typeMap->getType(e0, true);
37 if (type == nullptr) return;
38 if (!type->is<IR::Type_MatchKind>())
39 ::P4::error(ErrorType::ERR_TYPE_ERROR, "%1%: value must be a match_kind", e0);
40 }
41};
42
43} // namespace P4
44
45#endif /* FRONTENDS_P4_VALIDATEMATCHANNOTATIONS_H_ */
Definition visitor.h:418
Definition typeMap.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58