P4C
The P4 Compiler
Loading...
Searching...
No Matches
staticAssert.h
1/*
2Copyright 2022 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 FRONTENDS_P4_STATICASSERT_H_
18#define FRONTENDS_P4_STATICASSERT_H_
19
20#include "frontends/common/resolveReferences/resolveReferences.h"
21#include "frontends/p4/methodInstance.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
27using namespace literals;
28
34 TypeMap *typeMap;
35 bool removeStatement = false;
36
37 // Cannot go static here as cstring is not constexpr
38 const cstring staticAssertMethodName = "static_assert"_cs;
39
40 public:
41 explicit DoStaticAssert(TypeMap *typeMap) : typeMap(typeMap) {
42 CHECK_NULL(typeMap);
43 setName("DoStaticAssert");
44 }
45 const IR::Node *postorder(IR::MethodCallExpression *method) override {
46 MethodInstance *mi = MethodInstance::resolve(method, this, typeMap);
47 if (auto ef = mi->to<ExternFunction>()) {
48 if (ef->method->name == staticAssertMethodName) {
49 auto subst = ef->substitution;
50 auto params = subst.getParametersInOrder();
51 if (!params->moveNext()) {
52 ::P4::warning(ErrorType::WARN_INVALID, "static_assert with no arguments: %1%",
53 method);
54 return method;
55 }
56 auto param = params->getCurrent();
57 CHECK_NULL(param);
58 auto arg = subst.lookup(param);
59 CHECK_NULL(arg);
60 if (auto bl = arg->expression->to<IR::BoolLiteral>()) {
61 if (!bl->value) {
62 std::string_view message = "static_assert failed";
63 if (params->moveNext()) {
64 param = params->getCurrent();
65 CHECK_NULL(param);
66 auto msg = subst.lookup(param);
67 CHECK_NULL(msg);
68 if (const auto *sl = msg->expression->to<IR::StringLiteral>()) {
69 message = sl->value.string_view();
70 }
71 }
72 ::P4::error(ErrorType::ERR_EXPECTED, "%1%: %2%", method, message);
73 return method;
74 }
75 if (getContext()->node->is<IR::MethodCallStatement>()) {
76 removeStatement = true;
77 return method;
78 }
79 return new IR::BoolLiteral(method->srcInfo, true);
80 } else {
81 ::P4::error(ErrorType::ERR_UNEXPECTED,
82 "Could not evaluate static_assert to a constant: %1%", arg);
83 return method;
84 }
85 }
86 }
87 return method;
88 }
89
90 const IR::Node *postorder(IR::MethodCallStatement *statement) override {
91 if (removeStatement) {
92 removeStatement = false;
93 return nullptr;
94 }
95 return statement;
96 }
97};
98
99class StaticAssert : public PassManager {
100 public:
101 explicit StaticAssert(TypeMap *typeMap) {
102 passes.push_back(new ReadOnlyTypeInference(typeMap));
103 passes.push_back(new DoStaticAssert(typeMap));
104 setName("StaticAssert");
105 }
106};
107
108} // namespace P4
109
110#endif /* FRONTENDS_P4_STATICASSERT_H_ */
Definition staticAssert.h:33
Definition methodInstance.h:194
Definition node.h:95
Definition methodInstance.h:56
static MethodInstance * resolve(const IR::MethodCallExpression *mce, const DeclarationLookup *refMap, TypeMap *typeMap, bool useExpressionType=false, const Visitor::Context *ctxt=nullptr, bool incomplete=false)
Definition methodInstance.cpp:27
Definition ir/pass_manager.h:40
Definition typeChecker.h:349
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition staticAssert.h:99
Definition visitor.h:424
Definition typeMap.h:41
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void warning(const char *format, Args &&...args)
Report a warning with the given message.
Definition lib/error.h:115
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51
T * to() noexcept
Definition rtti.h:226