Files
carbon-lang/toolchain/check/handle_variable.cpp
T
Jon Ross-PerkinsandRichard Smith f197219c10 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>
2024-01-12 19:01:51 +00:00

95 lines
3.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/check/context.h"
#include "toolchain/check/convert.h"
#include "toolchain/check/modifiers.h"
#include "toolchain/sem_ir/inst.h"
namespace Carbon::Check {
auto HandleVariableIntroducer(Context& context,
Parse::VariableIntroducerId parse_node) -> bool {
// No action, just a bracketing node.
context.node_stack().Push(parse_node);
context.decl_state_stack().Push(DeclState::Var);
return true;
}
auto HandleReturnedModifier(Context& context,
Parse::ReturnedModifierId parse_node) -> bool {
// No action, just a bracketing node.
context.node_stack().Push(parse_node);
return true;
}
auto HandleVariableInitializer(Context& context,
Parse::VariableInitializerId parse_node)
-> bool {
SemIR::InstId init_id = context.node_stack().PopExpr();
context.node_stack().Push(parse_node, init_id);
return true;
}
auto HandleVariableDecl(Context& context, Parse::VariableDeclId parse_node)
-> bool {
// Handle the optional initializer.
std::optional<SemIR::InstId> init_id =
context.node_stack().PopIf<Parse::NodeKind::VariableInitializer>();
if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
return context.TODO(parse_node, "tuple pattern in var");
}
// Extract the name binding.
auto value_id = context.node_stack().PopPattern();
if (auto bind_name = context.insts().TryGetAs<SemIR::AnyBindName>(value_id)) {
// Form a corresponding name in the current context, and bind the name to
// the variable.
auto name_context = context.decl_name_stack().MakeUnqualifiedName(
context.insts().GetParseNode(value_id),
context.bind_names().Get(bind_name->bind_name_id).name_id);
context.decl_name_stack().AddNameToLookup(name_context, value_id);
value_id = bind_name->value_id;
}
// TODO: Handle other kinds of pattern.
// Pop the `returned` specifier if present.
context.node_stack()
.PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ReturnedModifier>();
// If there was an initializer, assign it to the storage.
if (init_id.has_value()) {
if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
// TODO: In a class scope, we should instead save the initializer
// somewhere so that we can use it as a default.
context.TODO(parse_node, "Field initializer");
} else {
init_id = Initialize(context, parse_node, value_id, *init_id);
// TODO: Consider using different instruction kinds for assignment versus
// initialization.
context.AddInst({parse_node, SemIR::Assign{value_id, *init_id}});
}
}
context.node_stack()
.PopAndDiscardSoloParseNode<Parse::NodeKind::VariableIntroducer>();
// Process declaration modifiers.
CheckAccessModifiersOnDecl(context, Lex::TokenKind::Var);
LimitModifiersOnDecl(context, KeywordModifierSet::Access,
Lex::TokenKind::Var);
auto modifiers = context.decl_state_stack().innermost().modifier_set;
if (!!(modifiers & KeywordModifierSet::Access)) {
context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
"access modifier");
}
context.decl_state_stack().Pop(DeclState::Var);
return true;
}
} // namespace Carbon::Check