mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Add tracking of function parameters (#2552)
For parameters (and in the future, arguments too; generally comma-separated lists) track two node blocks: 1. param_ir: The complete IR. 2. param_refs: Nodes within the IR that are the "root" parameter. param_refs should allow quick counting of the # of parameters, and more efficient comparison of call args with function parameters. param_ir should be necessary to generate the actual signature. In order to construct this, this refactors the node_block_stack into its own class, which is reused in params_stack. These carry references to the underlying SmallVector for lazy modification in order to avoid a dependency cycle with SemanticsIR (also see notes on empty node blocks below). When finalized, the block pair is pushed onto finished_params_stack. That's because node_stack only has space for one thing, and this is two things -- so I'm essentially choosing a trade-off of adding another stack in order to avoid consuming more space in the expectation that most parse nodes have 0 or 1 things to return, and 2 will be very rare. As factored, this currently consolidates most empty node blocks into a single canonical empty node block. This is because I think empty blocks, i.e. `()`, will be very common. In order to achieve this, SemanticsNodeBlockStack does lazy creation. An alternative approach would have been to use 1 node block per parameter. We decided against this in order to reduce the number of vectors being created.
This commit is contained in:
@@ -5,18 +5,23 @@
|
||||
#include "toolchain/semantics/semantics_node_stack.h"
|
||||
|
||||
#include "common/vlog.h"
|
||||
#include "llvm/Support/PrettyStackTrace.h"
|
||||
#include "toolchain/semantics/semantics_node.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto SemanticsNodeStack::PushEntry(Entry entry, bool is_node_id) -> void {
|
||||
CARBON_VLOG() << "Push " << stack_.size() << ": "
|
||||
auto SemanticsNodeStack::PushEntry(Entry entry, DebugLog debug_log) -> void {
|
||||
CARBON_VLOG() << "Node Push " << stack_.size() << ": "
|
||||
<< parse_tree_->node_kind(entry.parse_node) << " -> ";
|
||||
if (is_node_id) {
|
||||
CARBON_VLOG() << entry.node_id;
|
||||
} else {
|
||||
CARBON_VLOG() << entry.name_id;
|
||||
switch (debug_log) {
|
||||
case DebugLog::None:
|
||||
CARBON_VLOG() << "<none>";
|
||||
break;
|
||||
case DebugLog::NodeId:
|
||||
CARBON_VLOG() << entry.node_id;
|
||||
break;
|
||||
case DebugLog::NameId:
|
||||
CARBON_VLOG() << entry.name_id;
|
||||
break;
|
||||
}
|
||||
CARBON_VLOG() << "\n";
|
||||
CARBON_CHECK(stack_.size() < (1 << 20))
|
||||
@@ -26,7 +31,7 @@ auto SemanticsNodeStack::PushEntry(Entry entry, bool is_node_id) -> void {
|
||||
|
||||
auto SemanticsNodeStack::PopEntry() -> Entry {
|
||||
auto back = stack_.pop_back_val();
|
||||
CARBON_VLOG() << "Pop " << stack_.size() << ": any ("
|
||||
CARBON_VLOG() << "Node Pop " << stack_.size() << ": any ("
|
||||
<< parse_tree_->node_kind(back.parse_node) << ") -> "
|
||||
<< back.node_id << "\n";
|
||||
return back;
|
||||
@@ -34,8 +39,8 @@ auto SemanticsNodeStack::PopEntry() -> Entry {
|
||||
|
||||
auto SemanticsNodeStack::PopEntry(ParseNodeKind pop_parse_kind) -> Entry {
|
||||
auto back = stack_.pop_back_val();
|
||||
CARBON_VLOG() << "Pop " << stack_.size() << ": " << pop_parse_kind << " -> "
|
||||
<< back.node_id << "\n";
|
||||
CARBON_VLOG() << "Node Pop " << stack_.size() << ": " << pop_parse_kind
|
||||
<< " -> " << back.node_id << "\n";
|
||||
RequireParseKind(back, pop_parse_kind);
|
||||
return back;
|
||||
}
|
||||
@@ -47,16 +52,36 @@ auto SemanticsNodeStack::RequireParseKind(Entry entry,
|
||||
<< "Expected " << require_kind << ", found " << actual_kind;
|
||||
}
|
||||
|
||||
// RequireSoloParseNode and RequireValidId rely on type punning. They read
|
||||
// node_id.is_valid, even though that may not be the active union member.
|
||||
// These asserts enforce standard layout in order to help ensure that works.
|
||||
// TODO: Use is_layout_compatible in C++20.
|
||||
static_assert(std::is_standard_layout_v<SemanticsNodeId>,
|
||||
"Need standard layout for type punning");
|
||||
static_assert(std::is_standard_layout_v<SemanticsStringId>,
|
||||
"Need standard layout for type punning");
|
||||
|
||||
auto SemanticsNodeStack::RequireSoloParseNode(Entry entry) -> void {
|
||||
// See above comment on type punning.
|
||||
CARBON_CHECK(!entry.node_id.is_valid())
|
||||
<< "Expected invalid node_id on "
|
||||
<< parse_tree_->node_kind(entry.parse_node) << ", was " << entry.node_id;
|
||||
<< "Expected invalid id on " << parse_tree_->node_kind(entry.parse_node)
|
||||
<< ", was " << entry.node_id << " (may not be node)";
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::RequireNodeId(Entry entry) -> void {
|
||||
auto SemanticsNodeStack::RequireValidId(Entry entry) -> void {
|
||||
// See above comment on type punning.
|
||||
CARBON_CHECK(entry.node_id.is_valid())
|
||||
<< "Expected valid node_id on "
|
||||
<< parse_tree_->node_kind(entry.parse_node);
|
||||
<< "Expected valid id on " << parse_tree_->node_kind(entry.parse_node);
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::PopAndDiscardId() -> void {
|
||||
auto back = PopEntry();
|
||||
RequireValidId(back);
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::PopAndDiscardId(ParseNodeKind pop_parse_kind) -> void {
|
||||
auto back = PopEntry(pop_parse_kind);
|
||||
RequireValidId(back);
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::PopAndDiscardSoloParseNode(
|
||||
@@ -80,48 +105,48 @@ auto SemanticsNodeStack::PopForSoloParseNode(ParseNodeKind pop_parse_kind)
|
||||
|
||||
auto SemanticsNodeStack::PopForNodeId() -> SemanticsNodeId {
|
||||
auto back = PopEntry();
|
||||
RequireNodeId(back);
|
||||
RequireValidId(back);
|
||||
return back.node_id;
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::PopForNodeId(ParseNodeKind pop_parse_kind)
|
||||
-> SemanticsNodeId {
|
||||
auto back = PopEntry(pop_parse_kind);
|
||||
RequireNodeId(back);
|
||||
RequireValidId(back);
|
||||
return back.node_id;
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::PopForParseNodeAndNodeId()
|
||||
-> std::pair<ParseTree::Node, SemanticsNodeId> {
|
||||
auto back = PopEntry();
|
||||
RequireNodeId(back);
|
||||
RequireValidId(back);
|
||||
return {back.parse_node, back.node_id};
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::PopForParseNodeAndNodeId(ParseNodeKind pop_parse_kind)
|
||||
-> std::pair<ParseTree::Node, SemanticsNodeId> {
|
||||
auto back = PopEntry(pop_parse_kind);
|
||||
RequireNodeId(back);
|
||||
RequireValidId(back);
|
||||
return {back.parse_node, back.node_id};
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::PopForParseNodeAndNameId()
|
||||
-> std::pair<ParseTree::Node, SemanticsStringId> {
|
||||
auto back = PopEntry(ParseNodeKind::PatternBinding);
|
||||
RequireNodeId(back);
|
||||
RequireValidId(back);
|
||||
return {back.parse_node, back.name_id};
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::PeekForNameId() -> SemanticsStringId {
|
||||
auto back = stack_.back();
|
||||
RequireParseKind(back, ParseNodeKind::PatternBinding);
|
||||
RequireNodeId(back);
|
||||
RequireValidId(back);
|
||||
return back.name_id;
|
||||
}
|
||||
|
||||
auto SemanticsNodeStack::PrintForStackDump(llvm::raw_ostream& output) const
|
||||
-> void {
|
||||
output << "node_stack_:\n";
|
||||
output << "SemanticsNodeStack:\n";
|
||||
for (int i = 0; i < static_cast<int>(stack_.size()); ++i) {
|
||||
const auto& entry = stack_[i];
|
||||
auto parse_node_kind = parse_tree_->node_kind(entry.parse_node);
|
||||
|
||||
Reference in New Issue
Block a user