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/p4/methodInstance.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
30class DoStaticAssert : public Transform {
31 ReferenceMap *refMap;
32 TypeMap *typeMap;
33 bool removeStatement;
34
35 public:
36 DoStaticAssert(ReferenceMap *refMap, TypeMap *typeMap)
37 : refMap(refMap), typeMap(typeMap), removeStatement(false) {
38 CHECK_NULL(refMap);
39 CHECK_NULL(typeMap);
40 setName("DoStaticAssert");
41 }
42 const IR::Node *postorder(IR::MethodCallExpression *method) override {
43 MethodInstance *mi = MethodInstance::resolve(method, refMap, typeMap);
44 if (auto ef = mi->to<ExternFunction>()) {
45 if (ef->method->name == "static_assert") {
46 auto subst = ef->substitution;
47 auto params = subst.getParametersInOrder();
48 if (!params->moveNext()) {
49 ::warning(ErrorType::WARN_INVALID, "static_assert with no arguments: %1%",
50 method);
51 return method;
52 }
53 auto param = params->getCurrent();
54 CHECK_NULL(param);
55 auto arg = subst.lookup(param);
56 CHECK_NULL(arg);
57 if (auto bl = arg->expression->to<IR::BoolLiteral>()) {
58 if (!bl->value) {
59 cstring message = "static_assert failed";
60 if (params->moveNext()) {
61 param = params->getCurrent();
62 CHECK_NULL(param);
63 auto msg = subst.lookup(param);
64 CHECK_NULL(msg);
65 if (auto sl = msg->expression->to<IR::StringLiteral>()) {
66 message = sl->value;
67 }
68 }
69 ::error(ErrorType::ERR_EXPECTED, "%1%: %2%", method, message);
70 return method;
71 }
72 if (getContext()->node->is<IR::MethodCallStatement>()) {
73 removeStatement = true;
74 return method;
75 }
76 return new IR::BoolLiteral(method->srcInfo, true);
77 } else {
78 ::error(ErrorType::ERR_UNEXPECTED,
79 "Could not evaluate static_assert to a constant: %1%", arg);
80 return method;
81 }
82 }
83 }
84 return method;
85 }
86
87 const IR::Node *postorder(IR::MethodCallStatement *statement) override {
88 if (removeStatement) {
89 removeStatement = false;
90 return nullptr;
91 }
92 return statement;
93 }
94};
95
96class StaticAssert : public PassManager {
97 public:
98 StaticAssert(ReferenceMap *refMap, TypeMap *typeMap) {
99 passes.push_back(new TypeInference(refMap, typeMap));
100 passes.push_back(new DoStaticAssert(refMap, typeMap));
101 setName("StaticAssert");
102 }
103};
104
105} // namespace P4
106
107#endif /* FRONTENDS_P4_STATICASSERT_H_ */
Definition staticAssert.h:30
Definition methodInstance.h:176
Definition methodInstance.h:56
static MethodInstance * resolve(const IR::MethodCallExpression *mce, DeclarationLookup *refMap, TypeMap *typeMap, bool useExpressionType=false, const Visitor::Context *ctxt=nullptr, bool incomplete=false)
Definition methodInstance.cpp:26
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition staticAssert.h:96
Definition typeChecker.h:83
Definition typeMap.h:42
Definition cstring.h:72
Definition applyOptionsPragmas.cpp:24
T * to() noexcept
Definition rtti.h:226