Skip to content

Commit 9dde515

Browse files
committed
disallow duplicate declarations within the same scope
Signed-off-by: jaehyun1ee <99jaehyunlee@kaist.ac.kr>
1 parent 6d02aae commit 9dde515

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

p4-16/spec/P4-16-spec.adoc

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,9 @@ declaration. (This is a departure from P4~14~, which
13611361
allows declarations in any order. This requirement significantly simplifies the implementation of
13621362
compilers for P4, allowing compilers to use additional information
13631363
about declared identifiers to resolve ambiguities.)
1364+
Except for functions and methods, which may be overloaded, duplicate
1365+
declarations of the same name within the same scope are not allowed.
1366+
<<sec-name-resolution>> describes how names are resolved in P4.
13641367

13651368
[#sec-stateful-elems]
13661369
==== Stateful elements
@@ -1769,6 +1772,52 @@ control p() {
17691772
}
17701773
----
17711774

1775+
Declaring a duplicate identifier in the same scope is disallowed. This is
1776+
different from shadowing. Shadowing occurs when an identifier declared in an
1777+
_inner_ scope has the same name as an identifier declared in an _outer_ scope.
1778+
Duplicate declarations occur when two identifiers with the same name are
1779+
declared in the _same_ scope.
1780+
1781+
[source,p4]
1782+
----
1783+
const bit<4> x = 1;
1784+
control p() {
1785+
const bit<8> x = 8; // x declaration shadows global x
1786+
const bit<4> x = 4; // Error: duplicate declaration of x
1787+
apply {}
1788+
}
1789+
----
1790+
1791+
Functions and methods are the only P4 constructs that support overloading:
1792+
there _can_ exist multiple methods with the same name in the same scope. When
1793+
overloading is used, the compiler must be able to disambiguate at compile-time
1794+
which method or function is being called, either by the number of arguments or
1795+
by the names of the arguments, when calls are specifying argument names.
1796+
Argument type information is _not_ used in disambiguating calls.
1797+
1798+
[source,p4]
1799+
----
1800+
extern void f(in bit<8> x);
1801+
extern void f(in bit<8> y); // allowed: different parameter names
1802+
extern void f(in bit<16> x, in bit<8> y); // allowed: different number of parameters
1803+
extern void f(in bit<16> x, in bit<8> x); // Error: duplicate declaration of previous function
1804+
----
1805+
1806+
Notice that overloading of abstract methods, parsers, controls, or packages is
1807+
not allowed:
1808+
1809+
[source,p4]
1810+
----
1811+
parser p(packet_in p, out bit<32> value) {
1812+
...
1813+
}
1814+
1815+
// The following will cause an error about a duplicate declaration
1816+
//parser p(packet_in p, out Headers headers) {
1817+
// ...
1818+
//}
1819+
----
1820+
17721821
[#sec-name-visibility]
17731822
=== Visibility
17741823

@@ -2820,28 +2869,6 @@ control d(packet_out b, in Hdr h) {
28202869
}
28212870
----
28222871

2823-
Functions and methods are the only P4 constructs that support
2824-
overloading: there can exist multiple methods with the same name in
2825-
the same scope. When overloading is used, the compiler must be able to
2826-
disambiguate at compile-time which method or function is being called,
2827-
either by the number of arguments or by the names of the arguments,
2828-
when calls are specifying argument names. Argument type information
2829-
is not used in disambiguating calls.
2830-
2831-
Notice that overloading of abstract methods, parsers, controls, or packages is not allowed:
2832-
2833-
[source,p4]
2834-
----
2835-
parser p(packet_in p, out bit<32> value) {
2836-
...
2837-
}
2838-
2839-
// The following will cause an error about a duplicate declaration
2840-
//parser p(packet_in p, out Headers headers) {
2841-
// ...
2842-
//}
2843-
----
2844-
28452872
[#sec-abstract-methods]
28462873
====== Abstract methods
28472874

0 commit comments

Comments
 (0)