35 bool removeStatement =
false;
38 const cstring staticAssertMethodName =
"static_assert"_cs;
43 setName(
"DoStaticAssert");
45 const IR::Node *postorder(IR::MethodCallExpression *method)
override {
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%",
56 auto param = params->getCurrent();
58 auto arg = subst.lookup(param);
60 if (
auto bl = arg->expression->to<IR::BoolLiteral>()) {
62 std::string_view message =
"static_assert failed";
63 if (params->moveNext()) {
64 param = params->getCurrent();
66 auto msg = subst.lookup(param);
68 if (
const auto *sl = msg->expression->to<IR::StringLiteral>()) {
69 message = sl->value.string_view();
72 ::P4::error(ErrorType::ERR_EXPECTED,
"%1%: %2%", method, message);
75 if (getContext()->node->is<IR::MethodCallStatement>()) {
76 removeStatement =
true;
79 return new IR::BoolLiteral(method->srcInfo,
true);
82 "Could not evaluate static_assert to a constant: %1%", arg);
90 const IR::Node *postorder(IR::MethodCallStatement *statement)
override {
91 if (removeStatement) {
92 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
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
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