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

Superficial support for template modifier on symbolic bindings. #4948

Merged

Conversation

zygoloid
Copy link
Contributor

Change parse tree from template (T:! type) to (template T):! type, so that we have information about whether a binding is a template binding available when forming the representation of the binding pattern. This incidentally fixes a bug that we would accept template addr A:! B instead of the intended addr template A:! B.

Track whether a symbolic binding is a template binding on the EntityName object. I'm borrowing a bit from the CompileTimeBindIndex for this in order to avoid making EntityNames larger. Longer-term, we should think about using a different representation for symbolic bindings, to avoid including these fields in all EntityNames, but that's out of scope for this change.

So far, template bindings are treated as having the same phase as checked bindings, but that will change in a future PR.

Change parse tree from `template (T:! type)` to `(template T):! type`,
so that we have information about whether a binding is a template
binding available when forming the representation of the binding
pattern. This incidentally fixes a bug that we would accept
`template addr A:! B` instead of the intended `addr template A:! B`.

Track whether a symbolic binding is a template binding on the
`EntityName` object. I'm borrowing a bit from the `CompileTimeBindIndex`
for this in order to avoid making `EntityName`s larger. Longer-term, we
should think about using a different representation for symbolic
bindings, to avoid including these fields in all `EntityName`s, but
that's out of scope for this change.

So far, template bindings are treated as having the same phase as
checked bindings, but that will change in a future PR.
toolchain/sem_ir/inst_fingerprinter.cpp Show resolved Hide resolved
Comment on lines 50 to 52
(is_generic ? context.scope_stack().AddCompileTimeBinding()
: SemIR::CompileTimeBindIndex::None)
.index,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to switch to a constructor, to encapsulate away the .index?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encapsulated into a distinct Add function on the value store. (This gets us slightly closer to using distinct storage for this case, and also keeps the field names for the remaining cases.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, SG, particularly since we're talking about refactoring anyways.

// The bind_index() value, unwrapped so it can be stored in a bit-field.
int32_t bind_index_value : 31 = CompileTimeBindIndex::None.index;
// Whether this binding is a template parameter.
bool is_template : 1 = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're spending time on bit packing, would it make sense to static_assert on sizeof(EntityName)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hashing library will assert if it contains any padding because its check for unique object representations will fail. (We're not caring that much about the size here because we're wasting one id per EntityName on names that aren't symbolic bindings...)

Comment on lines 27 to 29
// The index for a compile-time binding. Invalid for a runtime binding, or
// for a symbolic binding, like `.Self`, that does not correspond to a generic
// parameter (and therefore has no index).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm making suggestions even though this is pre-existing.

Suggested change
// The index for a compile-time binding. Invalid for a runtime binding, or
// for a symbolic binding, like `.Self`, that does not correspond to a generic
// parameter (and therefore has no index).
// The index for a compile-time binding. Invalid for runtime bindings. Also invalid
// for a symbolic binding, like `.Self`, that does not correspond to a generic
// parameter (and therefore has no index).

But, invalid or None?

Maybe (more changes):

Suggested change
// The index for a compile-time binding. Invalid for a runtime binding, or
// for a symbolic binding, like `.Self`, that does not correspond to a generic
// parameter (and therefore has no index).
// The index for a compile-time binding. `None` for runtime bindings, and
// also for symbolic bindings that don't correspond to a generic parameter
// (and therefore have no index), such as `.Self`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made even more changes :)

toolchain/sem_ir/inst_fingerprinter.cpp Show resolved Hide resolved
@jonmeow
Copy link
Contributor

jonmeow commented Feb 13, 2025

LG except for the tests. :)

@zygoloid zygoloid enabled auto-merge February 13, 2025 00:49
@zygoloid zygoloid added this pull request to the merge queue Feb 13, 2025
Merged via the queue into carbon-language:trunk with commit 6dda094 Feb 13, 2025
8 checks passed
@zygoloid zygoloid deleted the toolchain-template-param-flag branch February 13, 2025 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants