p4lang / p4-spec

Apache License 2.0
178 stars 80 forks source link

Type nesting rule for extern, parser, control, and package types #1324

Open jaehyun1ee opened 2 weeks ago

jaehyun1ee commented 2 weeks ago

Section 7.2.8 defines the type nesting rules of P4. But it is not explicitly defined for extern, parser, control, and package types.

How about adding the following to the spec? I marked them as (new).

Element type \ Container type header header_union struct or tuple list header stack
bit<W> allowed error allowed allowed error
int<W> allowed error allowed allowed error
varbit<W> allowed error allowed allowed error
int error error error allowed error
void error error error error error
string error error error allowed error
error error error allowed allowed error
match_kind error error error allowed error
bool allowed error allowed allowed error
enumeration types allowed error allowed allowed error
header types error allowed allowed allowed allowed
header stacks error error allowed allowed error
header unions error error allowed allowed allowed
struct types allowed error allowed allowed error
tuple types error error allowed allowed error
list types error error error allowed error
(new) extern types error error error error error
(new) parser types error error error error error
(new) control types error error error error error
(new) package types error error error error error

And for typedef and type,

Base type B typedef B <name> type B <name>
bit<W> allowed allowed
int<W> allowed allowed
varbit<W> allowed error
int allowed error
void error error
string allowed error
error allowed error
match_kind error error
bool allowed allowed
enumeration types allowed error
header types allowed error
header stacks allowed error
header unions allowed error
struct types allowed error
tuple types allowed error
list types allowed error
(new) extern types allowed error
(new) parser types allowed error
(new) control types allowed error
(new) package types allowed error
a typedef name allowed allowed
a type name allowed allowed

Allowing typedef for extern, parser, control, and package type is reflected on the following use cases in the p4c compiler test suite. (extern-inst-as-param.p4, issue2735-bmv2.p4, issue2735.p4, and typedef-constructor.p4)

extern MyCounter<I> {
    MyCounter(bit<32> size);
    void count(in I index);
}
typedef bit<10> my_counter_index_t;
typedef MyCounter<my_counter_index_t> my_counter_t;

And when those types are aliased, it must be that all the type variables are bound.

jafingerhut commented 2 weeks ago

Adding the new entries you describe in those tables looks reasonable to me. Thanks for the links to relevant test cases in the existing p4c test suite.