Skip to content

Commit

Permalink
Improve diagnostics for StructExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
kfcripps committed Jan 24, 2024
1 parent 25f4648 commit bcde4cd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
13 changes: 13 additions & 0 deletions ir/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,16 @@ IR::Constant IR::Constant::operator-() const { return IR::Constant(-value); }
IR::Constant IR::Constant::GetMask(unsigned width) {
return (IR::Constant(1) << width) - IR::Constant(1);
}

cstring IR::StructExpression::toString() const {
cstring str = "{";
if (!components.empty()) {
cstring exprStr = components.at(0)->expression->toString();
str += " " + components.at(0)->toString() + " = " + exprStr;
}
for (unsigned i = 1; i < size(); i++) {
cstring exprStr = components.at(i)->expression->toString();
str += ", " + components.at(i)->toString() + " = " + exprStr;
}
return str + " }";
}
1 change: 1 addition & 0 deletions ir/expression.def
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ class StructExpression : Expression {
size_t size = components.size();
return components.at(size - 1)->is<IR::NamedDots>();
}
cstring toString() const;
}

/// Can be an invalid header or header_union
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/issue2220.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
issue2220.p4(11): [--Werror=type-error] error: StructExpression: values of type 'bit<8>' cannot be implicitly cast to type 'myEnum'
issue2220.p4(11): [--Werror=type-error] error: { val = 8w0 }: values of type 'bit<8>' cannot be implicitly cast to type 'myEnum'
S s1 = { val = (bit<8>)0 };
^^^^^^^^^^^^^^^^^^^
issue2220.p4(3)
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/issue3671-2.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
issue3671-2.p4(17): [--Werror=type-error] error: StructExpression: argument does not match declaration in actions list: StructExpression
issue3671-2.p4(17): [--Werror=type-error] error: { f1 = ext1, f0 = ext2 }: argument does not match declaration in actions list: { f0 = ext1, f1 = ext2 }
default_action = a1({f1 = ext1(),f0 = ext2()});
^^^^^^^^^^^^^^^^^^^^^^^^^
issue3671-2.p4(15)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ structure-valued-expr-errs-1.p4(31)
header h1_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(112): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h1_t'
structure-valued-expr-errs-1.p4(112): Source expression '{ f2 = 5, f1 = 2 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h1_t'
hdr.h1 = {f2=5, f1=2};
^^^^^^^^^^^^
structure-valued-expr-errs-1.p4(112)
Expand All @@ -186,7 +186,7 @@ structure-valued-expr-errs-1.p4(32): No initializer for field f1
bit<8> f1;
^^
---- Originating from:
structure-valued-expr-errs-1.p4(113): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h1_t'
structure-valued-expr-errs-1.p4(113): Source expression '{ f2 = 5 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h1_t'
hdr.h1 = {f2=5};
^^^^^^
structure-valued-expr-errs-1.p4(113)
Expand All @@ -206,7 +206,7 @@ structure-valued-expr-errs-1.p4(43)
struct s1_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(114): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s1_t'
structure-valued-expr-errs-1.p4(114): Source expression '{ f2 = 5, f1 = 2 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s1_t'
hdr.hstructs.s1 = {f2=5, f1=2};
^^^^^^^^^^^^
structure-valued-expr-errs-1.p4(114)
Expand All @@ -223,7 +223,7 @@ structure-valued-expr-errs-1.p4(44): No initializer for field f1
bit<8> f1;
^^
---- Originating from:
structure-valued-expr-errs-1.p4(115): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s1_t'
structure-valued-expr-errs-1.p4(115): Source expression '{ f2 = 5 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s1_t'
hdr.hstructs.s1 = {f2=5};
^^^^^^
structure-valued-expr-errs-1.p4(115)
Expand All @@ -243,7 +243,7 @@ structure-valued-expr-errs-1.p4(35)
header h2_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(120): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h2_t'
structure-valued-expr-errs-1.p4(120): Source expression '{ f2 = 5 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h2_t'
hdr.h2 = {f2=5};
^^^^^^
structure-valued-expr-errs-1.p4(120)
Expand All @@ -263,7 +263,7 @@ structure-valued-expr-errs-1.p4(47)
struct s2_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(121): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s2_t'
structure-valued-expr-errs-1.p4(121): Source expression '{ f2 = 5 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s2_t'
hdr.hstructs.s2 = {f2=5};
^^^^^^
structure-valued-expr-errs-1.p4(121)
Expand Down

0 comments on commit bcde4cd

Please sign in to comment.