mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 08:11:04 +01:00
Collapse names into VarStorage (#3116)
This removes BindName, putting name information directly on VarStorage.
As a side-effect of updating semantics_ir_test for this change, I also
noted that function bodies were being generated as invalid YAML so am
fixing that (just `{}` to `[]` bracketing, otherwise the test wouldn't
work anymore).
Because names are now available, I've updated lowering to use them for
vars.
In the SemIR formatter, the name is now repeated because it's a
parameter to VarStorage. I believe this is just default behavior, and
we'd have to special-case VarStorage to remove it because it's automatic
argument printing in action. On the balance, it felt like letting it
print was reasonable.
I've noted in places that the name on VarStorage is expected to be
optional, but am not adding support because I'd have no way of testing
it at present.
This commit is contained in:
@@ -326,20 +326,10 @@ class NodeNamer {
|
||||
|
||||
Scope& scope = GetScopeInfo(scope_idx);
|
||||
|
||||
// Use bound names where available. The BindName node appears after the node
|
||||
// that it's giving a name to, so we need to do this before assigning
|
||||
// fallback names.
|
||||
// Use bound names where available. Otherwise, assign a backup name.
|
||||
for (auto node_id : semantics_ir_.GetNodeBlock(block_id)) {
|
||||
auto node = semantics_ir_.GetNode(node_id);
|
||||
switch (node.kind()) {
|
||||
case SemanticsNodeKind::BindName: {
|
||||
auto [name_id, named_node_id] = node.GetAsBindName();
|
||||
nodes[named_node_id.index] = {
|
||||
scope_idx,
|
||||
scope.nodes.AllocateName(*this, node.parse_node(),
|
||||
semantics_ir_.GetString(name_id).str())};
|
||||
break;
|
||||
}
|
||||
case SemanticsNodeKind::Branch: {
|
||||
auto dest_id = node.GetAsBranch();
|
||||
AddBlockLabel(scope_idx, dest_id, node);
|
||||
@@ -355,20 +345,24 @@ class NodeNamer {
|
||||
AddBlockLabel(scope_idx, dest_id, node);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
case SemanticsNodeKind::VarStorage: {
|
||||
// TODO: Eventually this name will be optional, and we'll want to
|
||||
// provide something like `var` as a default. However, that's not
|
||||
// possible right now so cannot be tested.
|
||||
auto name_id = node.GetAsVarStorage();
|
||||
nodes[node_id.index] = {
|
||||
scope_idx,
|
||||
scope.nodes.AllocateName(*this, node.parse_node(),
|
||||
semantics_ir_.GetString(name_id).str())};
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// Sequentially number all remaining values.
|
||||
if (node.kind().value_kind() != SemanticsNodeValueKind::None) {
|
||||
nodes[node_id.index] = {
|
||||
scope_idx, scope.nodes.AllocateName(*this, node.parse_node())};
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sequentially number all remaining values.
|
||||
for (auto node_id : semantics_ir_.GetNodeBlock(block_id)) {
|
||||
auto node = semantics_ir_.GetNode(node_id);
|
||||
if (node.kind() != SemanticsNodeKind::BindName &&
|
||||
node.kind().value_kind() != SemanticsNodeValueKind::None) {
|
||||
auto& name = nodes[node_id.index];
|
||||
if (!name.second) {
|
||||
name = {scope_idx,
|
||||
scope.nodes.AllocateName(*this, node.parse_node())};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -429,11 +423,9 @@ class SemanticsIRFormatter {
|
||||
for (const SemanticsNodeId param_id :
|
||||
semantics_ir_.GetNodeBlock(fn.param_refs_id)) {
|
||||
out_ << sep;
|
||||
auto param = semantics_ir_.GetNode(param_id);
|
||||
auto [name_id, node_id] = param.GetAsBindName();
|
||||
FormatNodeName(node_id);
|
||||
FormatNodeName(param_id);
|
||||
out_ << ": ";
|
||||
FormatType(param.type_id());
|
||||
FormatType(semantics_ir_.GetNode(param_id).type_id());
|
||||
}
|
||||
out_ << ")";
|
||||
if (fn.return_type_id.is_valid()) {
|
||||
@@ -521,15 +513,6 @@ class SemanticsIRFormatter {
|
||||
FormatArgs(Kind::Get(node));
|
||||
}
|
||||
|
||||
// BindName is handled by the NodeNamer and doesn't appear in the output.
|
||||
// These nodes are currently used simply to give a name to another node, and
|
||||
// are never referenced themselves.
|
||||
// TODO: Include BindName nodes in the output if we start referring to them.
|
||||
template <>
|
||||
auto FormatInstruction<SemanticsNode::BindName>(SemanticsNodeId /*node_id*/,
|
||||
SemanticsNode /*node*/)
|
||||
-> void {}
|
||||
|
||||
template <>
|
||||
auto FormatInstructionRHS<SemanticsNode::BlockArg>(SemanticsNode node)
|
||||
-> void {
|
||||
|
||||
Reference in New Issue
Block a user