Skip to content

Commit bc50642

Browse files
author
Mihai Budiu
authored
Merge pull request #717 from p4lang/structInitializer
Describe structure initializer expressions
2 parents 03e5539 + 376bc65 commit bc50642

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

p4-16/spec/P4-16-spec.mdk

+45-1
Original file line numberDiff line numberDiff line change
@@ -2872,6 +2872,7 @@ expression
28722872
| expression '[' expression ']'
28732873
| expression '[' expression ':' expression ']'
28742874
| '{' expressionList '}'
2875+
| '{' kvList '}'
28752876
| '(' expression ')'
28762877
| '!' expression
28772878
| '~' expression
@@ -3754,13 +3755,49 @@ field access, written using dot (".") notation---e.g., `s.field`. If
37543755
`s` is an l-value, then `s.field` is also an l-value. P4 also allows
37553756
copying `struct`s using assignment when the source and target of the
37563757
assignment have the same type. Finally, `struct`s can be initialized
3757-
with a list expression, as discussed in Section [#sec-list-exprs].
3758+
with a list expression, as discussed in Section [#sec-list-exprs], or
3759+
with a structure initializer, as described in
3760+
[#sec-structure-initializers]. Both these cases must initialize all
3761+
fields of the structure.
37583762

37593763
Two structs can be compared for equality (==) or inequality (!=) only
37603764
if they have the same type and all of their fields can be recursively
37613765
compared for equality. Two structures are equal if and only if all
37623766
their corresponding fields are equal.
37633767

3768+
## Structure initializers { #sec-structure-initializers }
3769+
3770+
Structures can be initialized using structure initializers, which
3771+
specify explicitly the intialized fields. A structure initializer
3772+
expression evaluates to a struct; it can be used on the right-hand
3773+
side of an initialization of a variable with a struct type.
3774+
3775+
~ Begin P4Grammar
3776+
expression ...
3777+
| '{' kvList '}'
3778+
;
3779+
3780+
kvList
3781+
: kvPair
3782+
| kvList "," kvPair
3783+
;
3784+
3785+
kvPair
3786+
: name "=" expression
3787+
;
3788+
~ End P4Grammar
3789+
3790+
The following example shows a structure initialized using a
3791+
structure initializer:
3792+
3793+
~ Begin P4Example
3794+
struct S {
3795+
bit<32> a;
3796+
bit<32> b;
3797+
}
3798+
const S x = { a = 10, b = 20 };
3799+
~ End P4Example
3800+
37643801
See Section [#sec-uninitialized-values-and-writing-invalid-headers]
37653802
for a description of the behavior if `struct` fields are read without
37663803
being initialized.
@@ -3796,10 +3833,17 @@ a `struct`---the list fields are assigned to the header fields in the
37963833
order they appear. In this case the header automatically becomes
37973834
valid:
37983835

3836+
Similar to a `struct`, a header object can be initialized with a list
3837+
expression [#sec-list-exprs] --- the list fields are assigned to the
3838+
header fields in the order they appear --- or with a structure initializer
3839+
expression [#sec-ops-on-structs]. When initialized the header
3840+
automatically becomes valid:
3841+
37993842
~ Begin P4Example
38003843
header H { bit<32> x; bit<32> y; }
38013844
H h;
38023845
h = { 10, 12 }; // This also makes the header h valid
3846+
h = { y = 12, x = 10 }; // Same effect as above
38033847
~ End P4Example
38043848

38053849
Two headers can be compared for equality (==) or inequality (!=) only

p4-16/spec/grammar.mdk

+10
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,15 @@ argument
580580
| DONTCARE
581581
;
582582

583+
kvList
584+
: kvPair
585+
| kvList ',' kvPair
586+
;
587+
588+
kvPair
589+
: name '=' expression
590+
;
591+
583592
expressionList
584593
: /* empty */
585594
| expression
@@ -723,6 +732,7 @@ expression
723732
| expression '[' expression ']'
724733
| expression '[' expression ':' expression ']'
725734
| '{' expressionList '}'
735+
| '{' kvList '}'
726736
| '(' expression ')'
727737
| '!' expression
728738
| '~' expression

0 commit comments

Comments
 (0)