@@ -2872,6 +2872,7 @@ expression
2872
2872
| expression '[' expression ']'
2873
2873
| expression '[' expression ':' expression ']'
2874
2874
| '{' expressionList '}'
2875
+ | '{' kvList '}'
2875
2876
| '(' expression ')'
2876
2877
| '!' expression
2877
2878
| '~' expression
@@ -3754,13 +3755,49 @@ field access, written using dot (".") notation---e.g., `s.field`. If
3754
3755
`s` is an l-value, then `s.field` is also an l-value. P4 also allows
3755
3756
copying `struct`s using assignment when the source and target of the
3756
3757
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.
3758
3762
3759
3763
Two structs can be compared for equality (==) or inequality (!=) only
3760
3764
if they have the same type and all of their fields can be recursively
3761
3765
compared for equality. Two structures are equal if and only if all
3762
3766
their corresponding fields are equal.
3763
3767
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
+
3764
3801
See Section [#sec-uninitialized-values-and-writing-invalid-headers]
3765
3802
for a description of the behavior if `struct` fields are read without
3766
3803
being initialized.
@@ -3796,10 +3833,17 @@ a `struct`---the list fields are assigned to the header fields in the
3796
3833
order they appear. In this case the header automatically becomes
3797
3834
valid:
3798
3835
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
+
3799
3842
~ Begin P4Example
3800
3843
header H { bit<32> x; bit<32> y; }
3801
3844
H h;
3802
3845
h = { 10, 12 }; // This also makes the header h valid
3846
+ h = { y = 12, x = 10 }; // Same effect as above
3803
3847
~ End P4Example
3804
3848
3805
3849
Two headers can be compared for equality (==) or inequality (!=) only
0 commit comments