26class DoStaticAssert :
public Transform,
public ResolutionContext {
28 bool removeStatement =
false;
32 explicit DoStaticAssert(
TypeMap *typeMap,
bool isFinal) : typeMap(typeMap), isFinal(isFinal) {
34 setName(
"DoStaticAssert");
36 const IR::Node *postorder(IR::MethodCallExpression *method)
override {
39 if (ef->method->name == staticAssertMethodName) {
40 auto subst = ef->substitution;
41 auto params = subst.getParametersInOrder();
42 if (!params->moveNext()) {
43 ::P4::warning(ErrorType::WARN_INVALID,
"static_assert with no arguments: %1%",
47 auto param = params->getCurrent();
49 auto arg = subst.lookup(param);
51 if (
auto bl = arg->expression->to<IR::BoolLiteral>()) {
53 std::string_view message =
"static_assert failed";
54 if (params->moveNext()) {
55 param = params->getCurrent();
57 auto msg = subst.lookup(param);
59 if (
const auto *sl = msg->expression->to<IR::StringLiteral>()) {
63 ::P4::error(ErrorType::ERR_EXPECTED,
"%1%: %2%", method, message);
66 if (getContext()->node->is<IR::MethodCallStatement>()) {
67 removeStatement =
true;
70 return new IR::BoolLiteral(method->srcInfo,
true);
71 }
else if (!isFinal) {
75 "Could not evaluate static_assert to a constant: %1%", arg);
83 const IR::Node *postorder(IR::MethodCallStatement *statement)
override {
84 if (removeStatement) {
85 removeStatement =
false;
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
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
void warning(const char *format, Args &&...args)
Report a warning with the given message.
Definition lib/error.h:128
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58