-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Create a Generic
object to represent a generic.
#4081
Create a Generic
object to represent a generic.
#4081
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these changes make sense, but I'd like to confirm whether "Generic" includes template generics.
@@ -208,6 +211,9 @@ class File : public Printable<File> { | |||
// Storage for impls. | |||
ImplStore impls_; | |||
|
|||
// Storage for generics. | |||
ValueStore<GenericId> generics_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be printed as part of the yaml IR?
Maybe we should evaluate getting rid of the yaml given it's not really well maintained, since it's not well tested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added to YAML output.
// Information for a generic entity, such as a generic class, a generic | ||
// interface, or generic function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you say generic, do you mean both checked generics and template generics, or just checked generics? I think we should be precise here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean all generics -- the post-#2138 terminology. I've added to this comment to remind the reader of our terminology choice here.
toolchain/sem_ir/generic.h
Outdated
// The first declaration of the generic entity. | ||
InstId decl_id; | ||
// A block containing the IDs of compile time bindings in this generic scope. | ||
// The index in this block will match the `bind_index` of the instruction. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little uncertain here, we only have bind_index
on BindNameInfo, is that what you mean here?
// The index in this block will match the `bind_index` of the instruction. | |
// In this block, each instruction's index will match its BindNameInfo's | |
// `bind_index`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
toolchain/check/scope_stack.h
Outdated
@@ -210,6 +224,8 @@ struct ScopeStack::SuspendedScope { | |||
// to keep the size of a `SuspendedFunction` reasonable while avoiding heap | |||
// allocations most of the time. | |||
llvm::SmallVector<LexicalLookup::SuspendedResult, 8> suspended_lookups; | |||
// The compile-time bindings that were introduced in this scope. | |||
llvm::SmallVector<SemIR::InstId, 8> compile_time_bindings; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 8? There's a comment on suspended_lookups, but it doesn't clearly apply to this member.
If this is because we expect it to be 1:1 (or thereabout) with LexicalLookup::SuspendedResult, could they be combined? In part, I think there's a benefit to avoiding an additional vector if they're expected to be similar size; for things with a few parameters, even with no compile time bindings, it'll be smaller or equivalent just because of vector overhead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, we could almost avoid storing the compile time bindings entirely, but the list of bindings and the list of lookups can be out of sync due to error recovery. For
fn F(T:! type, T:! type)
we have two compile-time bindings but only one lookup result. I've combined these into a single vector as suggested.
toolchain/check/scope_stack.cpp
Outdated
if (result.entry.scope_id.is_valid()) { | ||
non_lexical_scope_stack_.pop_back(); | ||
} | ||
for (auto name_id : result.entry.names) { | ||
result.suspended_lookups.push_back(lexical_lookup_.Suspend(name_id)); | ||
} | ||
// Move any compile-time bindings into the suspended scope. | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, this extra scope mostly reads weird to me -- you might consider blank lines to separate logic (instead). The separation of the comment is also an odd result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Build a
Generic
object for generic functions. This object tracks the generic parameters that are in scope for the generic entity. Eventually it will track other information about the generic too.Add basic SemIR formatting support for generic functions.