Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4150286

Browse files
committedFeb 20, 2025·
Clean up typeCheckTypes a bit; another test
1 parent 54b142c commit 4150286

File tree

7 files changed

+151
-12
lines changed

7 files changed

+151
-12
lines changed
 

‎frontends/p4/typeChecking/typeCheckTypes.cpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ bool hasVarbitsOrUnions(const TypeMap *typeMap, const IR::Type *type) {
4747

4848
bool TypeInferenceBase::onlyBitsOrBitStructs(const IR::Type *type) const {
4949
// called for a canonical type
50+
while (auto *st = type->to<IR::Type_Stack>()) type = st->elementType;
5051
if (type->is<IR::Type_Bits>() || type->is<IR::Type_Boolean>() || type->is<IR::Type_SerEnum>()) {
5152
return true;
5253
} else if (auto ht = type->to<IR::Type_Struct>()) {
@@ -424,18 +425,9 @@ const IR::Node *TypeInferenceBase::postorder(const IR::StructField *field) {
424425
const IR::Node *TypeInferenceBase::postorder(const IR::Type_Header *type) {
425426
auto canon = setTypeType(type);
426427
auto validator = [this](const IR::Type *t) {
427-
while (1) {
428-
if (t->is<IR::Type_Newtype>())
429-
t = getTypeType(t->to<IR::Type_Newtype>()->type);
430-
else if (auto *st = t->to<IR::Type_Stack>())
431-
t = st->elementType;
432-
else
433-
break;
434-
}
435-
return t->is<IR::Type_Bits>() || t->is<IR::Type_Varbits>() ||
436-
(t->is<IR::Type_Struct>() && onlyBitsOrBitStructs(t)) || t->is<IR::Type_SerEnum>() ||
437-
t->is<IR::Type_Boolean>() || t->is<IR::Type_Var>() ||
438-
t->is<IR::Type_SpecializedCanonical>();
428+
while (t->is<IR::Type_Newtype>()) t = getTypeType(t->to<IR::Type_Newtype>()->type);
429+
return onlyBitsOrBitStructs(t) || t->is<IR::Type_Varbits>() ||
430+
t->is<IR::Type_Var>() || t->is<IR::Type_SpecializedCanonical>();
439431
};
440432
validateFields(canon, validator);
441433
return type;

‎testdata/p4_16_samples/array3.p4

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
control proto<P>(inout P pkt);
2+
package top<P>(proto<P> p);
3+
4+
struct S {
5+
bit<16> f1;
6+
bit<8>[16] nested_arr;
7+
bit<16> f2;
8+
}
9+
10+
header data_t {
11+
S[16] arr;
12+
bit<4> w;
13+
bit<4> x;
14+
bit<4> y;
15+
bit<4> z;
16+
}
17+
18+
struct headers {
19+
data_t data;
20+
}
21+
22+
23+
control c(inout headers hdr) {
24+
apply {
25+
hdr.data.arr[0].nested_arr[1] =
26+
hdr.data.arr[2].nested_arr[3];
27+
}
28+
}
29+
30+
top(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
control proto<P>(inout P pkt);
2+
package top<P>(proto<P> p);
3+
struct S {
4+
bit<16> f1;
5+
bit<8>[16] nested_arr;
6+
bit<16> f2;
7+
}
8+
9+
header data_t {
10+
S[16] arr;
11+
bit<4> w;
12+
bit<4> x;
13+
bit<4> y;
14+
bit<4> z;
15+
}
16+
17+
struct headers {
18+
data_t data;
19+
}
20+
21+
control c(inout headers hdr) {
22+
apply {
23+
hdr.data.arr[0].nested_arr[1] = hdr.data.arr[2].nested_arr[3];
24+
}
25+
}
26+
27+
top<headers>(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
control proto<P>(inout P pkt);
2+
package top<P>(proto<P> p);
3+
struct S {
4+
bit<16> f1;
5+
bit<8>[16] nested_arr;
6+
bit<16> f2;
7+
}
8+
9+
header data_t {
10+
S[16] arr;
11+
bit<4> w;
12+
bit<4> x;
13+
bit<4> y;
14+
bit<4> z;
15+
}
16+
17+
struct headers {
18+
data_t data;
19+
}
20+
21+
control c(inout headers hdr) {
22+
apply {
23+
hdr.data.arr[0].nested_arr[1] = hdr.data.arr[2].nested_arr[3];
24+
}
25+
}
26+
27+
top<headers>(c()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
control proto<P>(inout P pkt);
2+
package top<P>(proto<P> p);
3+
struct S {
4+
bit<16> f1;
5+
bit<8>[16] nested_arr;
6+
bit<16> f2;
7+
}
8+
9+
header data_t {
10+
S[16] arr;
11+
bit<4> w;
12+
bit<4> x;
13+
bit<4> y;
14+
bit<4> z;
15+
}
16+
17+
struct headers {
18+
data_t data;
19+
}
20+
21+
control c(inout headers hdr) {
22+
@hidden action array3l25() {
23+
hdr.data.arr[0].nested_arr[1] = hdr.data.arr[2].nested_arr[3];
24+
}
25+
@hidden table tbl_array3l25 {
26+
actions = {
27+
array3l25();
28+
}
29+
const default_action = array3l25();
30+
}
31+
apply {
32+
tbl_array3l25.apply();
33+
}
34+
}
35+
36+
top<headers>(c()) main;
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
control proto<P>(inout P pkt);
2+
package top<P>(proto<P> p);
3+
struct S {
4+
bit<16> f1;
5+
bit<8>[16] nested_arr;
6+
bit<16> f2;
7+
}
8+
9+
header data_t {
10+
S[16] arr;
11+
bit<4> w;
12+
bit<4> x;
13+
bit<4> y;
14+
bit<4> z;
15+
}
16+
17+
struct headers {
18+
data_t data;
19+
}
20+
21+
control c(inout headers hdr) {
22+
apply {
23+
hdr.data.arr[0].nested_arr[1] = hdr.data.arr[2].nested_arr[3];
24+
}
25+
}
26+
27+
top(c()) main;

‎testdata/p4_16_samples_outputs/array3.p4-stderr

Whitespace-only changes.

0 commit comments

Comments
 (0)
Please sign in to comment.