Skip to content

Should we allow generic parser/control declarations? #1380

Description

@jaehyun1ee

The current spec disallows generic parser/control declarations, in sections 13.2 and 14. For example, Section 13.2. Parser declarations states:

parserTypeDeclaration
    : optAnnotations PARSER name optTypeParameters
      "(" parameterList ")"
    ;

parserDeclaration
    : parserTypeDeclaration optConstructorParameters
      "{" parserLocalElements parserStates "}"
    ;

Unlike parser type declarations, parser declarations may not be generic---e.g., the following declaration is illegal:

parser P<H>(inout H data) { /* body omitted */ }

Hence, used in the context of a parserDeclaration the production rule parserTypeDeclaration should not yield type parameters.

Yet, it seems reasonable to amend the spec to allow generic parsers/controls because: (i) p4c accepts these as valid, and (ii) there are already other parts of the current spec that expects generic parsers/controls.

First, in p4c, this restriction does not exist. It is also reflected in some of the test programs in p4c/testdata/p4_16_samples:

Additionally, Section 15.1. Direct type invocation expects generic parsers and controls where it states:

directApplication
    : typeName "." APPLY "(" argumentList ")" ";"
    | specializedType "." APPLY "(" argumentList ")" ";"
    ;

This feature is intended to streamline the common case where a type is instantiated exactly once.
The second production in the grammar allows direct calls for generic controls or parsers:

control Callee<T>(/* parameters omitted */) { /* body omitted */ }

control Caller(/* parameters omitted */)(/* parameters omitted */) {
    apply {
        // Callee<bit<32>> is treated as an instance
        Callee<bit<32>>.apply(/* arguments omitted */);
    }
}

This part of the spec has been added after discussion in #1069. So following section 15.1, we can invoke generic parsers and controls, but according to section 13.2 and 14, we cannot define them at the first place, which I think is contradictory.

With these, I believe sections 13.2 and 14 don't accurately capture how P4 is used at the current state, and think that these should be revised to allow generic parsers and controls. Though I am also not aware of the initial motivations for disallowing generic parsers and controls. I believe it would be helpful to be discussed in LDWG :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions