Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added structure-valued expressions #773

Merged
merged 2 commits into from
Apr 6, 2020
Merged
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
70 changes: 48 additions & 22 deletions p4-16/spec/P4-16-spec.mdk
Original file line number Diff line number Diff line change
Expand Up @@ -3677,6 +3677,50 @@ tuple<bit<32>, bool> x = { 10, false };

The empty list expression has type `tuple<>` - a tuple with no components.

## Structure-valued expressions { #sec-structure-expressions}

One can write expressions that evaluate to a structure or header. The
syntax of these expressions is given by:

~ Begin P4Grammar
expression ...
| '{' kvList '}'
| '(' typeRef ')' expression
;

kvList
: kvPair
| kvList "," kvPair
;

kvPair
: name "=" expression
;
~ End P4Grammar

For a structure-valued expression `typeRef` is the name of a `struct`
or `header` type. The `typeRef` can be omitted if it can be inferred
from context, e.g., when initializing a variable with a `struct` type.
The following example shows a structure-valued expression used in an
equality comparison expression:

~ Begin P4Example
struct S {
bit<32> a;
bit<32> b;
}

S s;
...
// Compare s with a structure-valued expression
bool b = s == (S) { a = 1, b = 2 };
~ End P4Example

Structure-valued expressions can be used in the right-hand side of
assignments, in comparisons, in field selection expressions, and as
arguments to functions, method or actions. Structure-valued
expressions are not left values.

## Operations on sets { #sec-set-exprs }

Some P4 expressions denote sets of values (`set<T>`, for some type `T`;
Expand Down Expand Up @@ -3824,35 +3868,17 @@ their corresponding fields are equal.

## Structure initializers { #sec-structure-initializers }

Structures can be initialized using structure initializers, which
specify explicitly the intialized fields. A structure initializer
expression evaluates to a struct; it can be used on the right-hand
side of an initialization of a variable with a struct type.

~ Begin P4Grammar
expression ...
| '{' kvList '}'
;

kvList
: kvPair
| kvList "," kvPair
;

kvPair
: name "=" expression
;
~ End P4Grammar

The following example shows a structure initialized using a
structure initializer:
Structures can be initialized using structure-valued expression
([#sec-structure-expressions]). The following example shows a
structure initialized using a structure-valued expression:

~ Begin P4Example
struct S {
bit<32> a;
bit<32> b;
}
const S x = { a = 10, b = 20 };
const S x = (S){ a = 10, b = 20 }; // equivalent
~ End P4Example

The compiler must raise an error if a field name appears more than
Expand Down