mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Split parse nodes out from instructions because they're rarely used. (#3590)
The parse nodes are still tracked as part of the same value store interface in order to ensure parity, but they're split out from Inst itself in order to reduce the size of Inst -- the expectation is that they don't need to be passed around quite as much. This change doesn't actually reduce the passing very much, although there are hints of it: AddInstAndPush doesn't typically need a separate parse node from the one on the Inst itself, for example. In a couple spots I changed code to rely a little more on the InstId until the ParseNode is needed, but it's very low hanging fruit where done. I think convert could do more to not eagerly fetch the parse node before its use, but more cleanup felt it would be easier to handle separately. I'm currently viewing this as making such cleanup _possible_ rather than executing on it up-front. But also, I want to make sure there's a consensus to head in this direction before pulling the trigger. We speculated that this would result in the parse node being passed around less, and I do think that's the case, although it's a bit fuzzy in the change. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
Richard Smith
parent
a3154356f0
commit
f197219c10
@@ -6,6 +6,7 @@
|
||||
#include "toolchain/check/convert.h"
|
||||
#include "toolchain/check/return.h"
|
||||
#include "toolchain/sem_ir/inst.h"
|
||||
#include "toolchain/sem_ir/value_stores.h"
|
||||
|
||||
namespace Carbon::Check {
|
||||
|
||||
@@ -24,17 +25,17 @@ auto HandleAnyBindingPattern(Context& context, Parse::NodeId parse_node,
|
||||
// Create the appropriate kind of binding for this pattern.
|
||||
auto make_bind_name = [&, name_node = name_node, name_id = name_id](
|
||||
SemIR::TypeId type_id,
|
||||
SemIR::InstId value_id) -> SemIR::Inst {
|
||||
SemIR::InstId value_id) -> SemIR::ParseNodeAndInst {
|
||||
// TODO: Eventually the name will need to support associations with other
|
||||
// scopes, but right now we don't support qualified names here.
|
||||
auto bind_name_id = context.bind_names().Add(
|
||||
{.name_id = name_id, .enclosing_scope_id = context.current_scope_id()});
|
||||
if (is_generic) {
|
||||
// TODO: Create a `BindTemplateName` instead inside a `template` pattern.
|
||||
return SemIR::BindSymbolicName{name_node, type_id, bind_name_id,
|
||||
value_id};
|
||||
return {name_node,
|
||||
SemIR::BindSymbolicName{type_id, bind_name_id, value_id}};
|
||||
} else {
|
||||
return SemIR::BindName{name_node, type_id, bind_name_id, value_id};
|
||||
return {name_node, SemIR::BindName{type_id, bind_name_id, value_id}};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -90,22 +91,25 @@ auto HandleAnyBindingPattern(Context& context, Parse::NodeId parse_node,
|
||||
} else if (enclosing_class_decl) {
|
||||
auto& class_info =
|
||||
context.classes().Get(enclosing_class_decl->class_id);
|
||||
auto field_type_inst_id = context.AddInst(SemIR::UnboundElementType{
|
||||
binding_id, context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
|
||||
class_info.self_type_id, cast_type_id});
|
||||
auto field_type_inst_id = context.AddInst(
|
||||
{binding_id,
|
||||
SemIR::UnboundElementType{
|
||||
context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
|
||||
class_info.self_type_id, cast_type_id}});
|
||||
value_type_id = context.CanonicalizeType(field_type_inst_id);
|
||||
value_id = context.AddInst(
|
||||
SemIR::FieldDecl{binding_id, value_type_id, name_id,
|
||||
{binding_id, SemIR::FieldDecl{
|
||||
value_type_id, name_id,
|
||||
SemIR::ElementIndex(context.args_type_info_stack()
|
||||
.PeekCurrentBlockContents()
|
||||
.size())});
|
||||
.size())}});
|
||||
|
||||
// Add a corresponding field to the object representation of the class.
|
||||
context.args_type_info_stack().AddInst(
|
||||
SemIR::StructTypeField{binding_id, name_id, cast_type_id});
|
||||
{binding_id, SemIR::StructTypeField{name_id, cast_type_id}});
|
||||
} else {
|
||||
value_id = context.AddInst(
|
||||
SemIR::VarStorage{name_node, value_type_id, name_id});
|
||||
{name_node, SemIR::VarStorage{value_type_id, name_id}});
|
||||
}
|
||||
auto bind_id = context.AddInst(make_bind_name(value_type_id, value_id));
|
||||
context.node_stack().Push(parse_node, bind_id);
|
||||
@@ -123,9 +127,9 @@ auto HandleAnyBindingPattern(Context& context, Parse::NodeId parse_node,
|
||||
// TODO: A tuple pattern can appear in other places than function
|
||||
// parameters.
|
||||
auto param_id =
|
||||
context.AddInst(SemIR::Param{name_node, cast_type_id, name_id});
|
||||
context.AddInstAndPush(parse_node,
|
||||
make_bind_name(cast_type_id, param_id));
|
||||
context.AddInst({name_node, SemIR::Param{cast_type_id, name_id}});
|
||||
auto bind_id = context.AddInst(make_bind_name(cast_type_id, param_id));
|
||||
context.node_stack().Push(parse_node, bind_id);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -175,8 +179,7 @@ auto HandleAddr(Context& context, Parse::AddrId parse_node) -> bool {
|
||||
// TODO: The type of an `addr_pattern` should probably be the non-pointer
|
||||
// type, because that's the type that the pattern matches.
|
||||
context.AddInstAndPush(
|
||||
parse_node,
|
||||
SemIR::AddrPattern{parse_node, self_param->type_id, self_param_id});
|
||||
{parse_node, SemIR::AddrPattern{self_param->type_id, self_param_id}});
|
||||
} else {
|
||||
CARBON_DIAGNOSTIC(AddrOnNonSelfParam, Error,
|
||||
"`addr` can only be applied to a `self` parameter.");
|
||||
|
||||
Reference in New Issue
Block a user