Files
carbon-lang/toolchain/check/handle_variable.cpp
T
Richard Smith c7a9e29a89 Add typed nodes to SemIR. (#3280)
Replace `SemIR::Node::GetAsFoo` and `SemIR::Node::Foo::Make` with
`SemIR::Foo` class that represents a particular kind of node, with named
fields.

Rename `SemIR::IntegerLiteral` and `SemIR::RealLiteral` to
`IntegerValue` / `RealValue` to better reflect their purpose and avoid a
name collision with the corresponding `SemIR` node kinds.

Remove `NodeKind::Invalid` and the `SemIR::Node` default constructor
entirely, as they were not used for anything.
2023-10-11 05:39:59 +00:00

58 lines
1.9 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/sem_ir/node.h"
namespace Carbon::Check {
auto HandleVariableDeclaration(Context& context, Parse::Node parse_node)
-> bool {
// Handle the optional initializer.
auto init_id = SemIR::NodeId::Invalid;
bool has_init =
context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
Parse::NodeKind::PatternBinding;
if (has_init) {
init_id = context.node_stack().PopExpression();
context.node_stack()
.PopAndDiscardSoloParseNode<Parse::NodeKind::VariableInitializer>();
}
// Get the storage and add it to name lookup.
SemIR::NodeId var_id =
context.node_stack().Pop<Parse::NodeKind::PatternBinding>();
auto var = context.semantics_ir().GetNodeAs<SemIR::VarStorage>(var_id);
context.AddNameToLookup(var.parse_node, var.name_id, var_id);
// If there was an initializer, assign it to storage.
if (has_init) {
init_id = Initialize(context, parse_node, var_id, init_id);
// TODO: Consider using different node kinds for assignment versus
// initialization.
context.AddNode(SemIR::Assign(parse_node, var_id, init_id));
}
context.node_stack()
.PopAndDiscardSoloParseNode<Parse::NodeKind::VariableIntroducer>();
return true;
}
auto HandleVariableIntroducer(Context& context, Parse::Node parse_node)
-> bool {
// No action, just a bracketing node.
context.node_stack().Push(parse_node);
return true;
}
auto HandleVariableInitializer(Context& context, Parse::Node parse_node)
-> bool {
// No action, just a bracketing node.
context.node_stack().Push(parse_node);
return true;
}
} // namespace Carbon::Check