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

Experimental: Allow local typedef declarations #3027

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mihaibudiu
Copy link
Contributor

This should be discussed by the language design working group.
This feature would be useful to make the typeof operator from #3017 much more useful.
With this change one can write:

control C(out bit<16> result) {
    apply {
        typedef bit<32> T;
        T x = 5;
        result = x; }}

All standard scoping rules apply to the typedef. No other types can be declared locally, only typedef. This means that there are no issues with types that may not be visible globally - only names that may not be visible globally.

Unfortunately this required a relatively important change in the IR representation, which only supports single inheritance: the replacement of the StatOrDecl abstract class with its base class Node. So now block statements contain a Vector<Node> instead of a Vector<StatOrDecl>. This may require some changes in third-party backends.

@jnfoster
Copy link
Contributor

Haven't studied the feature carefully, but to avoid backend chaos, can we write a front-or-midend pass that eliminates these local type definitions?

@mihaibudiu
Copy link
Contributor Author

We already eliminate typedefs. The problem is that the compiler code itself may not typecheck anymore, because for backwards compatibility I have #define StatOrDecl Node, and this means that Vector<StatOrDecl> and Vector<Node> are now the same type. I only had to change a couple of places in the whole compiler and no test broke.

@mihaibudiu
Copy link
Contributor Author

This was a problem in the toP4 pass which had two overloads: postoder(const IR::Vector<IR::Node>*) and postoder(const IR::Vector<IR::StatOrDecl>*)

@fruffy fruffy added the p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). label Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants