Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 49 additions & 22 deletions p4-16/spec/P4-16-spec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,9 @@ declaration. (This is a departure from P4~14~, which
allows declarations in any order. This requirement significantly simplifies the implementation of
compilers for P4, allowing compilers to use additional information
about declared identifiers to resolve ambiguities.)
Except for functions and methods, which may be overloaded, duplicate
declarations of the same name within the same scope are not allowed.
<<sec-name-resolution>> describes how names are resolved in P4.

[#sec-stateful-elems]
==== Stateful elements
Expand Down Expand Up @@ -1769,6 +1772,52 @@ control p() {
}
----

Declaring a duplicate identifier in the same scope is disallowed. This is
different from shadowing. Shadowing occurs when an identifier declared in an
_inner_ scope has the same name as an identifier declared in an _outer_ scope.
Duplicate declarations occur when two identifiers with the same name are
declared in the _same_ scope.

[source,p4]
----
const bit<4> x = 1;
control p() {
const bit<8> x = 8; // x declaration shadows global x
const bit<4> x = 4; // Error: duplicate declaration of x
apply {}
}
----

Functions and methods are the only P4 constructs that support overloading:
there _can_ exist multiple methods with the same name in the same scope. When
overloading is used, the compiler must be able to disambiguate at compile-time
which method or function is being called, either by the number of arguments or
by the names of the arguments, when calls are specifying argument names.
Argument type information is _not_ used in disambiguating calls.

[source,p4]
----
extern void f(in bit<8> x);
extern void f(in bit<8> y); // allowed: different parameter names
extern void f(in bit<16> x, in bit<8> y); // allowed: different number of parameters
extern void f(in bit<16> x, in bit<8> x); // Error: duplicate declaration of previous function
----

Notice that overloading of abstract methods, parsers, controls, or packages is
not allowed:

[source,p4]
----
parser p(packet_in p, out bit<32> value) {
...
}

// The following will cause an error about a duplicate declaration
//parser p(packet_in p, out Headers headers) {
// ...
//}
----

[#sec-name-visibility]
=== Visibility

Expand Down Expand Up @@ -2820,28 +2869,6 @@ control d(packet_out b, in Hdr h) {
}
----

Functions and methods are the only P4 constructs that support
overloading: there can exist multiple methods with the same name in
the same scope. When overloading is used, the compiler must be able to
disambiguate at compile-time which method or function is being called,
either by the number of arguments or by the names of the arguments,
when calls are specifying argument names. Argument type information
is not used in disambiguating calls.

Notice that overloading of abstract methods, parsers, controls, or packages is not allowed:

[source,p4]
----
parser p(packet_in p, out bit<32> value) {
...
}

// The following will cause an error about a duplicate declaration
//parser p(packet_in p, out Headers headers) {
// ...
//}
----

[#sec-abstract-methods]
====== Abstract methods

Expand Down