Skip to content

Commit

Permalink
Fix for issue p4lang#388
Browse files Browse the repository at this point in the history
  • Loading branch information
mbudiu-vmw committed May 11, 2017
1 parent 9fd3ed3 commit 5d8f0ce
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 67 deletions.
4 changes: 4 additions & 0 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,10 @@ const IR::Node* TypeInference::preorder(IR::Declaration_Instance* decl) {
prune();
return decl;
}
if (!simpleType->is<IR::Type_Package>() && (findContext<IR::IContainer>() == nullptr)) {
::error("%1%: cannot instantiate at top-level", decl);
return decl;
}
auto type = containerInstantiation(decl, decl->arguments, simpleType->to<IR::IContainer>());
if (type == nullptr) {
prune();
Expand Down
22 changes: 7 additions & 15 deletions test/gtest/arch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ TEST(arch, instantiation) {
b.emit(h);
}
}
MyParser() p;
MyIngress() ig;
MyDeparser() dp;
PSA(p, ig, dp) main;
PSA(MyParser(), MyIngress(), MyDeparser()) main;
)");
auto pgm = P4::parseP4String(program, CompilerOptions::FrontendVersion::P4_16);

Expand Down Expand Up @@ -162,8 +159,7 @@ TEST(arch, psa_package_with_body) {
apply {
}
}
MyIngress(2) ig;
PSA(ig) main;
PSA(MyIngress(2)) main;
)");
auto pgm = P4::parseP4String(program, CompilerOptions::FrontendVersion::P4_16);

Expand Down Expand Up @@ -197,9 +193,8 @@ TEST(arch, psa_control_in_control) {
control MyEgress(inout Metadata m) (MyIngress ig) {
apply {}
}
MyIngress() ig;
MyEgress(ig) eg;
PSA(ig) main;
//MyEgress(ig) eg;
PSA(MyIngress()) main;
)");
auto pgm = P4::parseP4String(program, CompilerOptions::FrontendVersion::P4_16);

Expand Down Expand Up @@ -258,8 +253,7 @@ TEST(arch, psa_clone_as_param_to_control) {
control MyIngress (inout ParsedHeaders p, inout Metadata m) (clone<bit<32>> c) {
apply{}
}
MyIngress(clone<bit<32>>()) ig;
PSA(ig) main;
PSA(MyIngress(clone<bit<32>>())) main;
)");
auto pgm = P4::parseP4String(program, CompilerOptions::FrontendVersion::P4_16);

Expand Down Expand Up @@ -301,8 +295,7 @@ TEST(arch, psa_clone_as_param_to_extern) {
apply{}
}
PRE<bit<32>>() pre;
MyIngress(pre) ig;
PSA(ig) main;
PSA(MyIngress(pre)) main;
)");
auto pgm = P4::parseP4String(program, CompilerOptions::FrontendVersion::P4_16);

Expand Down Expand Up @@ -333,8 +326,7 @@ TEST(arch, clone_as_extern_method) {
// invoke clone3 triggers a compiler bug.
}
}
MyIngress() ig;
PSA(ig) main;
PSA(MyIngress()) main;
)");
auto pgm = P4::parseP4String(program, CompilerOptions::FrontendVersion::P4_16);

Expand Down
10 changes: 1 addition & 9 deletions testdata/p4_16_errors/issue306.p4
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,4 @@ control MyDeparser(packet_out b, in my_packet p) {
}
}

/* Instantiate */
MyParser() p;
MyVerifyChecksum() vck;
MyIngress() i;
MyEgress() e;
MyComputeChecksum() cck;
MyDeparser() dp;

V1Switch(p, vck, i, e, cck, dp) main;
V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main;
5 changes: 4 additions & 1 deletion testdata/p4_16_errors/issue344.p4
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ control C<H>() {
apply {}
}

C() c;
control cproto();
package top(cproto _c);

top(C()) c;
47 changes: 47 additions & 0 deletions testdata/p4_16_errors/issue388.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <v1model.p4>

header h_t {
bit<8> f;
}

struct my_packet {
h_t h;
}

struct my_metadata {

}

parser MyParser(packet_in b, out my_packet p, inout my_metadata m, inout standard_metadata_t s) {
state start {
transition accept;
}
}

control MyVerifyChecksum(in my_packet hdr, inout my_metadata meta) {
apply { }
}

control C() {
apply {}
}

C() c;

control MyIngress(inout my_packet p, inout my_metadata m, inout standard_metadata_t s) {
apply { c.apply(); }
}

control MyEgress(inout my_packet p, inout my_metadata m, inout standard_metadata_t s) {
apply { }
}

control MyComputeChecksum(inout my_packet p, inout my_metadata m) {
apply { }
}

control MyDeparser(packet_out b, in my_packet p) {
apply { }
}

V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main;
10 changes: 1 addition & 9 deletions testdata/p4_16_errors/issue401.p4
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,4 @@ control MyDeparser(packet_out b, in my_packet p) {
apply { }
}

/* Instantiate */
MyParser() p;
MyVerifyChecksum() vck;
MyIngress() i;
MyEgress() e;
MyComputeChecksum() cck;
MyDeparser() dp;

V1Switch(p, vck, i, e, cck, dp) main;
V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main;
8 changes: 1 addition & 7 deletions testdata/p4_16_errors_outputs/issue306.p4
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,4 @@ control MyDeparser(packet_out b, in my_packet p) {
}
}

MyParser() p;
MyVerifyChecksum() vck;
MyIngress() i;
MyEgress() e;
MyComputeChecksum() cck;
MyDeparser() dp;
V1Switch(p, vck, i, e, cck, dp) main;
V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main;
4 changes: 3 additions & 1 deletion testdata/p4_16_errors_outputs/issue344.p4
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ control C<H>() {
}
}

C() c;
control cproto();
package top(cproto _c);
top(C()) c;
6 changes: 3 additions & 3 deletions testdata/p4_16_errors_outputs/issue344.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
../testdata/p4_16_errors/issue344.p4(21): error: c: cannot infer type for type parameter H
C() c;
^
../testdata/p4_16_errors/issue344.p4(24): error: C: cannot infer type for type parameter H
top(C()) c;
^^^
../testdata/p4_16_errors/issue344.p4(17)
control C<H>() {
^
53 changes: 53 additions & 0 deletions testdata/p4_16_errors_outputs/issue388.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <core.p4>
#include <v1model.p4>

header h_t {
bit<8> f;
}

struct my_packet {
h_t h;
}

struct my_metadata {
}

parser MyParser(packet_in b, out my_packet p, inout my_metadata m, inout standard_metadata_t s) {
state start {
transition accept;
}
}

control MyVerifyChecksum(in my_packet hdr, inout my_metadata meta) {
apply {
}
}

control C() {
apply {
}
}

C() c;
control MyIngress(inout my_packet p, inout my_metadata m, inout standard_metadata_t s) {
apply {
c.apply();
}
}

control MyEgress(inout my_packet p, inout my_metadata m, inout standard_metadata_t s) {
apply {
}
}

control MyComputeChecksum(inout my_packet p, inout my_metadata m) {
apply {
}
}

control MyDeparser(packet_out b, in my_packet p) {
apply {
}
}

V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main;
12 changes: 12 additions & 0 deletions testdata/p4_16_errors_outputs/issue388.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
../testdata/p4_16_errors/issue388.p4(29): error: c: cannot instantiate at top-level
C() c;
^
../testdata/p4_16_errors/issue388.p4(29): error: Could not find type of c
C() c;
^
../testdata/p4_16_errors/issue388.p4(32): error: Could not find type of c
apply { c.apply(); }
^
../testdata/p4_16_errors/issue388.p4(32): error: Could not find type of c.apply
apply { c.apply(); }
^^^^^^^
8 changes: 1 addition & 7 deletions testdata/p4_16_errors_outputs/issue401.p4
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,4 @@ control MyDeparser(packet_out b, in my_packet p) {
}
}

MyParser() p;
MyVerifyChecksum() vck;
MyIngress() i;
MyEgress() e;
MyComputeChecksum() cck;
MyDeparser() dp;
V1Switch(p, vck, i, e, cck, dp) main;
V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main;
4 changes: 1 addition & 3 deletions testdata/p4_16_samples/parser-arg.p4
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@ parser Inside() {
state start { transition accept; }
}

Parser1(Inside()) p1;
Parser2(Inside()) p2;

Package(p1,p2) main;
Package(Parser1(Inside()), Parser2(Inside())) main;
6 changes: 3 additions & 3 deletions testdata/p4_16_samples_outputs/parser-arg-first.p4
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <core.p4>

parser Parser();
package Package(Parser p1, Parser p2);
parser Parser1()(Parser p) {
Expand All @@ -20,6 +22,4 @@ parser Inside() {
}
}

Parser1(Inside()) p1;
Parser2(Inside()) p2;
Package(p1, p2) main;
Package(Parser1(Inside()), Parser2(Inside())) main;
4 changes: 1 addition & 3 deletions testdata/p4_16_samples_outputs/parser-arg-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ parser Parser1_0() {
}
}

Parser1_0() p1;
parser Parser2_0() {
Inside() inst_0;
state start {
Expand All @@ -25,5 +24,4 @@ parser Parser2_0() {
}
}

Parser2_0() p2;
Package(p1, p2) main;
Package(Parser1_0(), Parser2_0()) main;
4 changes: 1 addition & 3 deletions testdata/p4_16_samples_outputs/parser-arg-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ parser Parser1_0() {
}
}

Parser1_0() p1;
parser Parser2_0() {
state start {
transition accept;
}
}

Parser2_0() p2;
Package(p1, p2) main;
Package(Parser1_0(), Parser2_0()) main;
4 changes: 1 addition & 3 deletions testdata/p4_16_samples_outputs/parser-arg.p4
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@ parser Inside() {
}
}

Parser1(Inside()) p1;
Parser2(Inside()) p2;
Package(p1, p2) main;
Package(Parser1(Inside()), Parser2(Inside())) main;

0 comments on commit 5d8f0ce

Please sign in to comment.