P4C
The P4 Compiler
Loading...
Searching...
No Matches
getV1ModelVersion.h
1/*
2 * SPDX-FileCopyrightText: 2024 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef FRONTENDS_P4_GETV1MODELVERSION_H_
8#define FRONTENDS_P4_GETV1MODELVERSION_H_
9
10#include "absl/strings/numbers.h"
11#include "frontends/p4-14/fromv1.0/v1model.h"
12#include "ir/ir.h"
13#include "ir/visitor.h"
14
15namespace P4::P4V1 {
16
20 bool preorder(const IR::Declaration_Constant *dc) override {
21 if (dc->name == "__v1model_version") {
22 const auto *val = dc->initializer->to<IR::Constant>();
23 if (!val || val->value < 0) {
24 P4::error(ErrorType::ERR_EXPECTED,
25 "%1%: expected v1model version to be a non-negative number",
26 dc->initializer);
27 return false;
28 }
29 version = static_cast<unsigned>(val->value);
30
31 unsigned initialVersion;
32 unsigned currentVersion;
33 BUG_CHECK(absl::SimpleAtoi(V1Model::versionInitial, &initialVersion) &&
34 absl::SimpleAtoi(V1Model::versionCurrent, &currentVersion),
35 "Unable to check v1model version");
36 if (version < initialVersion || version > currentVersion) {
38 ErrorType::ERR_UNKNOWN,
39 "%1%: unknown v1model version; initial supported version is %2%, latest is %3%",
40 dc->initializer, initialVersion, currentVersion);
41 return false;
42 }
43 }
44 return false;
45 }
46 bool preorder(const IR::Declaration *) override { return false; }
47
48 public:
49 unsigned version = 0;
50};
51
52} // namespace P4::P4V1
53
54#endif /* FRONTENDS_P4_GETV1MODELVERSION_H_ */
Definition visitor.h:418
Definition getV1ModelVersion.h:19
Definition converters.cpp:17
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58