Support for functions with a self parameter. (#3338)

So far, such functions can only be defined; calls are not supported yet.
This commit is contained in:
Richard Smith
2023-10-26 21:01:46 +00:00
committed by GitHub
parent e7b3d395b3
commit 04ae5a0531
26 changed files with 510 additions and 66 deletions
+13 -14
View File
@@ -22,25 +22,24 @@ auto HandleVariableDeclaration(Context& context, Parse::Node parse_node)
}
// Extract the name binding.
SemIR::NodeId bind_name_id =
context.node_stack().Pop<Parse::NodeKind::PatternBinding>();
auto bind_name = context.nodes().GetAs<SemIR::BindName>(bind_name_id);
// Form a corresponding name in the current context, and bind the name to the
// variable.
context.declaration_name_stack().AddNameToLookup(
context.declaration_name_stack().MakeUnqualifiedName(bind_name.parse_node,
bind_name.name_id),
bind_name_id);
auto value_id = context.node_stack().Pop<Parse::NodeKind::PatternBinding>();
if (auto bind_name = context.nodes().Get(value_id).TryAs<SemIR::BindName>()) {
// Form a corresponding name in the current context, and bind the name to
// the variable.
context.declaration_name_stack().AddNameToLookup(
context.declaration_name_stack().MakeUnqualifiedName(
bind_name->parse_node, bind_name->name_id),
value_id);
value_id = bind_name->value_id;
}
// If there was an initializer, assign it to the storage.
if (has_init) {
auto var_id = bind_name.value_id;
if (context.nodes().Get(var_id).Is<SemIR::VarStorage>()) {
init_id = Initialize(context, parse_node, var_id, init_id);
if (context.nodes().Get(value_id).Is<SemIR::VarStorage>()) {
init_id = Initialize(context, parse_node, value_id, init_id);
// TODO: Consider using different node kinds for assignment versus
// initialization.
context.AddNode(SemIR::Assign{parse_node, var_id, init_id});
context.AddNode(SemIR::Assign{parse_node, value_id, init_id});
} else {
// TODO: In a class scope, we should instead save the initializer
// somewhere so that we can use it as a default.