Skip to content

Commit

Permalink
Allow implicit conversion of brace-lists to type list
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDodd committed Apr 4, 2024
1 parent 3e3726a commit 1f6c99c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
18 changes: 13 additions & 5 deletions frontends/p4/typeChecking/typeUnification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,19 @@ bool TypeUnification::unify(const BinaryConstraint *constraint) {
}
return true;
}
} else if (dest->is<IR::Type_P4List>() && src->is<IR::Type_P4List>()) {
auto dvec = dest->to<IR::Type_P4List>();
auto svec = src->to<IR::Type_P4List>();
constraints->add(constraint->create(dvec->elementType, svec->elementType));
return true;
} else if (auto dvec = dest->to<IR::Type_P4List>()) {
if (auto svec = src->to<IR::Type_P4List>()) {
constraints->add(constraint->create(dvec->elementType, svec->elementType));
return true;
} else if (auto svec = src->to<IR::Type_List>()) {
std::set<const IR::Type *> elTypes;
for (auto *t : svec->components) {
if (elTypes.count(t)) continue;
elTypes.insert(t);
constraints->add(constraint->create(dvec->elementType, t));
}
return true;
}
}

return constraint->reportError(constraints->getCurrentSubstitution());
Expand Down
14 changes: 14 additions & 0 deletions testdata/p4_16_samples/listinit.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

extern test<T1, T2> {
test(list<tuple<T1, T2>> pairs);
}

test<bit<16>, _>({
{ 16w0, "foo" },
{ 16w1, "bar" },
{ 16w2, "baz" }
}) obj;

test<bit<16>, _>({}) o2;

test<bit<16>, bit<8>>({ { 0, 10}, { 1, 12 }, { 2, 15 } }) o3;
7 changes: 7 additions & 0 deletions testdata/p4_16_samples_outputs/listinit-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern test<T1, T2> {
test(list<tuple<T1, T2>> pairs);
}

test<bit<16>, _>({ { 16w0, "foo" }, { 16w1, "bar" }, { 16w2, "baz" } }) obj;
test<bit<16>, _>({ }) o2;
test<bit<16>, bit<8>>({ { 0, 10 }, { 1, 12 }, { 2, 15 } }) o3;
7 changes: 7 additions & 0 deletions testdata/p4_16_samples_outputs/listinit-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern test<T1, T2> {
test(list<tuple<T1, T2>> pairs);
}

test<bit<16>, _>({ { 16w0, "foo" }, { 16w1, "bar" }, { 16w2, "baz" } }) obj;
test<bit<16>, _>({ }) o2;
test<bit<16>, bit<8>>({ { 0, 10 }, { 1, 12 }, { 2, 15 } }) o3;
7 changes: 7 additions & 0 deletions testdata/p4_16_samples_outputs/listinit.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern test<T1, T2> {
test(list<tuple<T1, T2>> pairs);
}

test<bit<16>, _>({ { 16w0, "foo" }, { 16w1, "bar" }, { 16w2, "baz" } }) obj;
test<bit<16>, _>({ }) o2;
test<bit<16>, bit<8>>({ { 0, 10 }, { 1, 12 }, { 2, 15 } }) o3;
10 changes: 10 additions & 0 deletions testdata/p4_16_samples_outputs/listinit.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
listinit.p4(10): [--Wwarn=unused] warning: obj: unused instance
}) obj;
^^^
listinit.p4(12): [--Wwarn=unused] warning: o2: unused instance
test<bit<16>, _>({}) o2;
^^
listinit.p4(14): [--Wwarn=unused] warning: o3: unused instance
test<bit<16>, bit<8>>({ { 0, 10}, { 1, 12 }, { 2, 15 } }) o3;
^^
[--Wwarn=missing] warning: Program does not contain a `main' module

0 comments on commit 1f6c99c

Please sign in to comment.