Split Semantics into Check and SemIR namespaces (#3138)

Splits IR files into SemIR, and logic files into Check. These will be
split into separate directories as part of a later move; the namespaces
are being done first in order to vet the switch, and hopefully make
conflicts a little easier to manage due to the substantial renames.

A lot of this is just automated removal of Semantics prefixes from
names, adding namespace references where needed. A few special-cases
are:

- SemanticsIR -> SemIR::File
- A few things were discussed, like Unit, CompileUnit, or CompiledUnit.
Unit was too vague for chandlerc, and I thought CompileUnit might lead
to incorrect inferences (CompilationUnit would be more precise, but
typically written as SemIR::CompilationUnit which is pretty long). File
seemed to be a short name that we could agree on.
- SemanticsIRFormatter -> SemIR::Formatter
- FormatSemanticsIR -> SemIR::FormatFile
- SemanticsFileTest -> CheckFileTest
- It remains in the Testing namespace, where just "FileTest" might be
too broad a name.
- SemanticsDeclarationNameStack::Context ->
Check::DeclarationNameStack::NameContext
  - This avoids a Check::Context name shadowing.

Changes check_internal.h to include ostream.h to improve finding of
Print/operator<< (otherwise it didn't compile).

This is part of #3070
This commit is contained in:
Jon Ross-Perkins
2023-08-23 22:51:23 +00:00
committed by GitHub
parent b2db2ca7cf
commit 67da700dd5
53 changed files with 1337 additions and 1466 deletions
+137 -154
View File
@@ -18,13 +18,12 @@
#include "toolchain/semantics/semantics_node_block_stack.h"
#include "toolchain/semantics/semantics_node_kind.h"
namespace Carbon {
namespace Carbon::Check {
SemanticsContext::SemanticsContext(const TokenizedBuffer& tokens,
DiagnosticEmitter<ParseTree::Node>& emitter,
const ParseTree& parse_tree,
SemanticsIR& semantics_ir,
llvm::raw_ostream* vlog_stream)
Context::Context(const TokenizedBuffer& tokens,
DiagnosticEmitter<ParseTree::Node>& emitter,
const ParseTree& parse_tree, SemIR::File& semantics_ir,
llvm::raw_ostream* vlog_stream)
: tokens_(&tokens),
emitter_(&emitter),
parse_tree_(&parse_tree),
@@ -37,21 +36,19 @@ SemanticsContext::SemanticsContext(const TokenizedBuffer& tokens,
declaration_name_stack_(this) {
// Inserts the "Error" and "Type" types as "used types" so that
// canonicalization can skip them. We don't emit either for lowering.
canonical_types_.insert({SemIR::NodeId::BuiltinError, SemIR::TypeId::Error});
canonical_types_.insert(
{SemanticsNodeId::BuiltinError, SemanticsTypeId::Error});
canonical_types_.insert(
{SemanticsNodeId::BuiltinTypeType, SemanticsTypeId::TypeType});
{SemIR::NodeId::BuiltinTypeType, SemIR::TypeId::TypeType});
}
auto SemanticsContext::TODO(ParseTree::Node parse_node, std::string label)
-> bool {
auto Context::TODO(ParseTree::Node parse_node, std::string label) -> bool {
CARBON_DIAGNOSTIC(SemanticsTodo, Error, "Semantics TODO: `{0}`.",
std::string);
emitter_->Emit(parse_node, SemanticsTodo, std::move(label));
return false;
}
auto SemanticsContext::VerifyOnFinish() -> void {
auto Context::VerifyOnFinish() -> void {
// Information in all the various context objects should be cleaned up as
// various pieces of context go out of scope. At this point, nothing should
// remain.
@@ -62,25 +59,24 @@ auto SemanticsContext::VerifyOnFinish() -> void {
CARBON_CHECK(params_or_args_stack_.empty()) << params_or_args_stack_.size();
}
auto SemanticsContext::AddNode(SemanticsNode node) -> SemanticsNodeId {
auto Context::AddNode(SemIR::Node node) -> SemIR::NodeId {
return AddNodeToBlock(node_block_stack_.PeekForAdd(), node);
}
auto SemanticsContext::AddNodeToBlock(SemanticsNodeBlockId block,
SemanticsNode node) -> SemanticsNodeId {
auto Context::AddNodeToBlock(SemIR::NodeBlockId block, SemIR::Node node)
-> SemIR::NodeId {
CARBON_VLOG() << "AddNode " << block << ": " << node << "\n";
return semantics_ir_->AddNode(block, node);
}
auto SemanticsContext::AddNodeAndPush(ParseTree::Node parse_node,
SemanticsNode node) -> void {
auto Context::AddNodeAndPush(ParseTree::Node parse_node, SemIR::Node node)
-> void {
auto node_id = AddNode(node);
node_stack_.Push(parse_node, node_id);
}
auto SemanticsContext::DiagnoseDuplicateName(ParseTree::Node parse_node,
SemanticsNodeId prev_def_id)
-> void {
auto Context::DiagnoseDuplicateName(ParseTree::Node parse_node,
SemIR::NodeId prev_def_id) -> void {
CARBON_DIAGNOSTIC(NameDeclarationDuplicate, Error,
"Duplicate name being declared in the same scope.");
CARBON_DIAGNOSTIC(NameDeclarationPrevious, Note,
@@ -91,16 +87,16 @@ auto SemanticsContext::DiagnoseDuplicateName(ParseTree::Node parse_node,
.Emit();
}
auto SemanticsContext::DiagnoseNameNotFound(ParseTree::Node parse_node,
SemanticsStringId name_id) -> void {
auto Context::DiagnoseNameNotFound(ParseTree::Node parse_node,
SemIR::StringId name_id) -> void {
CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.",
llvm::StringRef);
emitter_->Emit(parse_node, NameNotFound, semantics_ir_->GetString(name_id));
}
auto SemanticsContext::AddNameToLookup(ParseTree::Node name_node,
SemanticsStringId name_id,
SemanticsNodeId target_id) -> void {
auto Context::AddNameToLookup(ParseTree::Node name_node,
SemIR::StringId name_id, SemIR::NodeId target_id)
-> void {
if (current_scope().names.insert(name_id).second) {
name_lookup_[name_id].push_back(target_id);
} else {
@@ -108,17 +104,16 @@ auto SemanticsContext::AddNameToLookup(ParseTree::Node name_node,
}
}
auto SemanticsContext::LookupName(ParseTree::Node parse_node,
SemanticsStringId name_id,
SemanticsNameScopeId scope_id,
bool print_diagnostics) -> SemanticsNodeId {
if (scope_id == SemanticsNameScopeId::Invalid) {
auto Context::LookupName(ParseTree::Node parse_node, SemIR::StringId name_id,
SemIR::NameScopeId scope_id, bool print_diagnostics)
-> SemIR::NodeId {
if (scope_id == SemIR::NameScopeId::Invalid) {
auto it = name_lookup_.find(name_id);
if (it == name_lookup_.end()) {
if (print_diagnostics) {
DiagnoseNameNotFound(parse_node, name_id);
}
return SemanticsNodeId::BuiltinError;
return SemIR::NodeId::BuiltinError;
}
CARBON_CHECK(!it->second.empty())
<< "Should have been erased: " << semantics_ir_->GetString(name_id);
@@ -132,16 +127,16 @@ auto SemanticsContext::LookupName(ParseTree::Node parse_node,
if (print_diagnostics) {
DiagnoseNameNotFound(parse_node, name_id);
}
return SemanticsNodeId::BuiltinError;
return SemIR::NodeId::BuiltinError;
}
return it->second;
}
}
auto SemanticsContext::PushScope() -> void { scope_stack_.push_back({}); }
auto Context::PushScope() -> void { scope_stack_.push_back({}); }
auto SemanticsContext::PopScope() -> void {
auto Context::PopScope() -> void {
auto scope = scope_stack_.pop_back_val();
for (const auto& str_id : scope.names) {
auto it = name_lookup_.find(str_id);
@@ -155,83 +150,81 @@ auto SemanticsContext::PopScope() -> void {
}
template <typename BranchNode, typename... Args>
static auto AddDominatedBlockAndBranchImpl(SemanticsContext& context,
static auto AddDominatedBlockAndBranchImpl(Context& context,
ParseTree::Node parse_node,
Args... args)
-> SemanticsNodeBlockId {
Args... args) -> SemIR::NodeBlockId {
if (!context.node_block_stack().is_current_block_reachable()) {
return SemanticsNodeBlockId::Unreachable;
return SemIR::NodeBlockId::Unreachable;
}
auto block_id = context.semantics_ir().AddNodeBlock();
context.AddNode(BranchNode::Make(parse_node, block_id, args...));
return block_id;
}
auto SemanticsContext::AddDominatedBlockAndBranch(ParseTree::Node parse_node)
-> SemanticsNodeBlockId {
return AddDominatedBlockAndBranchImpl<SemanticsNode::Branch>(*this,
parse_node);
auto Context::AddDominatedBlockAndBranch(ParseTree::Node parse_node)
-> SemIR::NodeBlockId {
return AddDominatedBlockAndBranchImpl<SemIR::Node::Branch>(*this, parse_node);
}
auto SemanticsContext::AddDominatedBlockAndBranchWithArg(
ParseTree::Node parse_node, SemanticsNodeId arg_id)
-> SemanticsNodeBlockId {
return AddDominatedBlockAndBranchImpl<SemanticsNode::BranchWithArg>(
auto Context::AddDominatedBlockAndBranchWithArg(ParseTree::Node parse_node,
SemIR::NodeId arg_id)
-> SemIR::NodeBlockId {
return AddDominatedBlockAndBranchImpl<SemIR::Node::BranchWithArg>(
*this, parse_node, arg_id);
}
auto SemanticsContext::AddDominatedBlockAndBranchIf(ParseTree::Node parse_node,
SemanticsNodeId cond_id)
-> SemanticsNodeBlockId {
return AddDominatedBlockAndBranchImpl<SemanticsNode::BranchIf>(
auto Context::AddDominatedBlockAndBranchIf(ParseTree::Node parse_node,
SemIR::NodeId cond_id)
-> SemIR::NodeBlockId {
return AddDominatedBlockAndBranchImpl<SemIR::Node::BranchIf>(
*this, parse_node, cond_id);
}
auto SemanticsContext::AddConvergenceBlockAndPush(
auto Context::AddConvergenceBlockAndPush(
ParseTree::Node parse_node,
std::initializer_list<SemanticsNodeBlockId> blocks) -> void {
std::initializer_list<SemIR::NodeBlockId> blocks) -> void {
CARBON_CHECK(blocks.size() >= 2) << "no convergence";
SemanticsNodeBlockId new_block_id = SemanticsNodeBlockId::Unreachable;
for (SemanticsNodeBlockId block_id : blocks) {
if (block_id != SemanticsNodeBlockId::Unreachable) {
if (new_block_id == SemanticsNodeBlockId::Unreachable) {
SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
for (SemIR::NodeBlockId block_id : blocks) {
if (block_id != SemIR::NodeBlockId::Unreachable) {
if (new_block_id == SemIR::NodeBlockId::Unreachable) {
new_block_id = semantics_ir().AddNodeBlock();
}
AddNodeToBlock(block_id,
SemanticsNode::Branch::Make(parse_node, new_block_id));
SemIR::Node::Branch::Make(parse_node, new_block_id));
}
}
node_block_stack().Push(new_block_id);
}
auto SemanticsContext::AddConvergenceBlockWithArgAndPush(
auto Context::AddConvergenceBlockWithArgAndPush(
ParseTree::Node parse_node,
std::initializer_list<std::pair<SemanticsNodeBlockId, SemanticsNodeId>>
blocks_and_args) -> SemanticsNodeId {
std::initializer_list<std::pair<SemIR::NodeBlockId, SemIR::NodeId>>
blocks_and_args) -> SemIR::NodeId {
CARBON_CHECK(blocks_and_args.size() >= 2) << "no convergence";
SemanticsNodeBlockId new_block_id = SemanticsNodeBlockId::Unreachable;
SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
for (auto [block_id, arg_id] : blocks_and_args) {
if (block_id != SemanticsNodeBlockId::Unreachable) {
if (new_block_id == SemanticsNodeBlockId::Unreachable) {
if (block_id != SemIR::NodeBlockId::Unreachable) {
if (new_block_id == SemIR::NodeBlockId::Unreachable) {
new_block_id = semantics_ir().AddNodeBlock();
}
AddNodeToBlock(block_id, SemanticsNode::BranchWithArg::Make(
AddNodeToBlock(block_id, SemIR::Node::BranchWithArg::Make(
parse_node, new_block_id, arg_id));
}
}
node_block_stack().Push(new_block_id);
// Acquire the result value.
SemanticsTypeId result_type_id =
SemIR::TypeId result_type_id =
semantics_ir().GetNode(blocks_and_args.begin()->second).type_id();
return AddNode(
SemanticsNode::BlockArg::Make(parse_node, result_type_id, new_block_id));
SemIR::Node::BlockArg::Make(parse_node, result_type_id, new_block_id));
}
// Add the current code block to the enclosing function.
auto SemanticsContext::AddCurrentCodeBlockToFunction() -> void {
auto Context::AddCurrentCodeBlockToFunction() -> void {
CARBON_CHECK(!node_block_stack().empty()) << "no current code block";
CARBON_CHECK(!return_scope_stack().empty()) << "no current function";
@@ -248,12 +241,12 @@ auto SemanticsContext::AddCurrentCodeBlockToFunction() -> void {
.body_block_ids.push_back(node_block_stack().PeekForAdd());
}
auto SemanticsContext::is_current_position_reachable() -> bool {
auto Context::is_current_position_reachable() -> bool {
switch (auto block_id = node_block_stack().Peek(); block_id.index) {
case SemanticsNodeBlockId::Unreachable.index: {
case SemIR::NodeBlockId::Unreachable.index: {
return false;
}
case SemanticsNodeBlockId::Invalid.index: {
case SemIR::NodeBlockId::Invalid.index: {
return true;
}
default: {
@@ -265,19 +258,19 @@ auto SemanticsContext::is_current_position_reachable() -> bool {
}
const auto& last_node = semantics_ir().GetNode(block_contents.back());
return last_node.kind().terminator_kind() !=
SemanticsTerminatorKind::Terminator;
SemIR::TerminatorKind::Terminator;
}
}
}
auto SemanticsContext::ImplicitAsForArgs(
SemanticsNodeBlockId arg_refs_id, ParseTree::Node param_parse_node,
SemanticsNodeBlockId param_refs_id,
auto Context::ImplicitAsForArgs(
SemIR::NodeBlockId arg_refs_id, ParseTree::Node param_parse_node,
SemIR::NodeBlockId param_refs_id,
DiagnosticEmitter<ParseTree::Node>::DiagnosticBuilder* diagnostic) -> bool {
// If both arguments and parameters are empty, return quickly. Otherwise,
// we'll fetch both so that errors are consistent.
if (arg_refs_id == SemanticsNodeBlockId::Empty &&
param_refs_id == SemanticsNodeBlockId::Empty) {
if (arg_refs_id == SemIR::NodeBlockId::Empty &&
param_refs_id == SemIR::NodeBlockId::Empty) {
return true;
}
@@ -320,11 +313,10 @@ auto SemanticsContext::ImplicitAsForArgs(
return true;
}
auto SemanticsContext::ImplicitAsRequired(ParseTree::Node parse_node,
SemanticsNodeId value_id,
SemanticsTypeId as_type_id)
-> SemanticsNodeId {
SemanticsNodeId output_value_id = value_id;
auto Context::ImplicitAsRequired(ParseTree::Node parse_node,
SemIR::NodeId value_id,
SemIR::TypeId as_type_id) -> SemIR::NodeId {
SemIR::NodeId output_value_id = value_id;
if (ImplicitAsImpl(value_id, as_type_id, &output_value_id) ==
ImplicitAsKind::Incompatible) {
// Only error when the system is trying to use the result.
@@ -341,37 +333,34 @@ auto SemanticsContext::ImplicitAsRequired(ParseTree::Node parse_node,
return output_value_id;
}
auto SemanticsContext::ImplicitAsBool(ParseTree::Node parse_node,
SemanticsNodeId value_id)
-> SemanticsNodeId {
auto Context::ImplicitAsBool(ParseTree::Node parse_node, SemIR::NodeId value_id)
-> SemIR::NodeId {
return ImplicitAsRequired(parse_node, value_id,
CanonicalizeType(SemanticsNodeId::BuiltinBoolType));
CanonicalizeType(SemIR::NodeId::BuiltinBoolType));
}
auto SemanticsContext::ImplicitAsImpl(SemanticsNodeId value_id,
SemanticsTypeId as_type_id,
SemanticsNodeId* output_value_id)
-> ImplicitAsKind {
auto Context::ImplicitAsImpl(SemIR::NodeId value_id, SemIR::TypeId as_type_id,
SemIR::NodeId* output_value_id) -> ImplicitAsKind {
// Start by making sure both sides are valid. If any part is invalid, the
// result is invalid and we shouldn't error.
if (value_id == SemanticsNodeId::BuiltinError) {
if (value_id == SemIR::NodeId::BuiltinError) {
// If the value is invalid, we can't do much, but do "succeed".
return ImplicitAsKind::Identical;
}
auto value = semantics_ir_->GetNode(value_id);
auto value_type_id = value.type_id();
if (value_type_id == SemanticsTypeId::Error) {
if (value_type_id == SemIR::TypeId::Error) {
// Although the source type is invalid, this still changes the value.
if (output_value_id != nullptr) {
*output_value_id = SemanticsNodeId::BuiltinError;
*output_value_id = SemIR::NodeId::BuiltinError;
}
return ImplicitAsKind::Compatible;
}
if (as_type_id == SemanticsTypeId::Error) {
if (as_type_id == SemIR::TypeId::Error) {
// Although the target type is invalid, this still changes the value.
if (output_value_id != nullptr) {
*output_value_id = SemanticsNodeId::BuiltinError;
*output_value_id = SemIR::NodeId::BuiltinError;
}
return ImplicitAsKind::Compatible;
}
@@ -383,13 +372,13 @@ auto SemanticsContext::ImplicitAsImpl(SemanticsNodeId value_id,
auto as_type = semantics_ir_->GetTypeAllowBuiltinTypes(as_type_id);
auto as_type_node = semantics_ir_->GetNode(as_type);
if (as_type_node.kind() == SemanticsNodeKind::ArrayType) {
if (as_type_node.kind() == SemIR::NodeKind::ArrayType) {
auto [bound_node_id, element_type_id] = as_type_node.GetAsArrayType();
// To resolve lambda issue.
auto element_type = element_type_id;
auto value_type_node = semantics_ir_->GetNode(
semantics_ir_->GetTypeAllowBuiltinTypes(value_type_id));
if (value_type_node.kind() == SemanticsNodeKind::TupleType) {
if (value_type_node.kind() == SemIR::NodeKind::TupleType) {
auto tuple_type_block_id = value_type_node.GetAsTupleType();
const auto& type_block = semantics_ir_->GetTypeBlock(tuple_type_block_id);
if (type_block.size() ==
@@ -397,7 +386,7 @@ auto SemanticsContext::ImplicitAsImpl(SemanticsNodeId value_id,
std::all_of(type_block.begin(), type_block.end(),
[&](auto type) { return type == element_type; })) {
if (output_value_id != nullptr) {
*output_value_id = AddNode(SemanticsNode::ArrayValue::Make(
*output_value_id = AddNode(SemIR::Node::ArrayValue::Make(
value.parse_node(), as_type_id, value_id));
}
return ImplicitAsKind::Compatible;
@@ -405,12 +394,12 @@ auto SemanticsContext::ImplicitAsImpl(SemanticsNodeId value_id,
}
}
if (as_type_id == SemanticsTypeId::TypeType) {
if (value.kind() == SemanticsNodeKind::TupleValue) {
if (as_type_id == SemIR::TypeId::TypeType) {
if (value.kind() == SemIR::NodeKind::TupleValue) {
auto tuple_block_id = value.GetAsTupleValue();
llvm::SmallVector<SemanticsTypeId> type_ids;
llvm::SmallVector<SemIR::TypeId> type_ids;
// If it is empty tuple type, we don't fetch anything.
if (tuple_block_id != SemanticsNodeBlockId::Empty) {
if (tuple_block_id != SemIR::NodeBlockId::Empty) {
const auto& tuple_block = semantics_ir_->GetNodeBlock(tuple_block_id);
for (auto tuple_node_id : tuple_block) {
// TODO: Eventually ExpressionAsType will insert implicit cast
@@ -429,8 +418,8 @@ auto SemanticsContext::ImplicitAsImpl(SemanticsNodeId value_id,
return ImplicitAsKind::Compatible;
}
// When converting `{}` to a type, the result is `{} as Type`.
if (value.kind() == SemanticsNodeKind::StructValue &&
value.GetAsStructValue() == SemanticsNodeBlockId::Empty) {
if (value.kind() == SemIR::NodeKind::StructValue &&
value.GetAsStructValue() == SemIR::NodeBlockId::Empty) {
if (output_value_id != nullptr) {
*output_value_id = semantics_ir_->GetType(value_type_id);
}
@@ -441,34 +430,32 @@ auto SemanticsContext::ImplicitAsImpl(SemanticsNodeId value_id,
// TODO: Handle ImplicitAs for compatible structs and tuples.
if (output_value_id != nullptr) {
*output_value_id = SemanticsNodeId::BuiltinError;
*output_value_id = SemIR::NodeId::BuiltinError;
}
return ImplicitAsKind::Incompatible;
}
auto SemanticsContext::ParamOrArgStart() -> void {
params_or_args_stack_.Push();
}
auto Context::ParamOrArgStart() -> void { params_or_args_stack_.Push(); }
auto SemanticsContext::ParamOrArgComma(bool for_args) -> void {
auto Context::ParamOrArgComma(bool for_args) -> void {
ParamOrArgSave(for_args);
}
auto SemanticsContext::ParamOrArgEnd(bool for_args, ParseNodeKind start_kind)
-> SemanticsNodeBlockId {
auto Context::ParamOrArgEnd(bool for_args, ParseNodeKind start_kind)
-> SemIR::NodeBlockId {
if (parse_tree_->node_kind(node_stack_.PeekParseNode()) != start_kind) {
ParamOrArgSave(for_args);
}
return params_or_args_stack_.Pop();
}
auto SemanticsContext::ParamOrArgSave(bool for_args) -> void {
auto Context::ParamOrArgSave(bool for_args) -> void {
auto [entry_parse_node, entry_node_id] =
node_stack_.PopExpressionWithParseNode();
if (for_args) {
// For an argument, we add a stub reference to the expression on the top of
// the stack. There may not be anything on the IR prior to this.
entry_node_id = AddNode(SemanticsNode::StubReference::Make(
entry_node_id = AddNode(SemIR::Node::StubReference::Make(
entry_parse_node, semantics_ir_->GetNode(entry_node_id).type_id(),
entry_node_id));
}
@@ -479,10 +466,10 @@ auto SemanticsContext::ParamOrArgSave(bool for_args) -> void {
params_or_args.push_back(entry_node_id);
}
auto SemanticsContext::CanonicalizeTypeImpl(
SemanticsNodeKind kind,
auto Context::CanonicalizeTypeImpl(
SemIR::NodeKind kind,
llvm::function_ref<void(llvm::FoldingSetNodeID& canonical_id)> profile_type,
llvm::function_ref<SemanticsNodeId()> make_node) -> SemanticsTypeId {
llvm::function_ref<SemIR::NodeId()> make_node) -> SemIR::TypeId {
llvm::FoldingSetNodeID canonical_id;
kind.Profile(canonical_id);
profile_type(canonical_id);
@@ -514,7 +501,7 @@ auto SemanticsContext::CanonicalizeTypeImpl(
}
// Compute a fingerprint for a tuple type, for use as a key in a folding set.
static auto ProfileTupleType(const llvm::SmallVector<SemanticsTypeId>& type_ids,
static auto ProfileTupleType(const llvm::SmallVector<SemIR::TypeId>& type_ids,
llvm::FoldingSetNodeID& canonical_id) -> void {
for (const auto& type_id : type_ids) {
canonical_id.AddInteger(type_id.index);
@@ -522,20 +509,20 @@ static auto ProfileTupleType(const llvm::SmallVector<SemanticsTypeId>& type_ids,
}
// Compute a fingerprint for a type, for use as a key in a folding set.
static auto ProfileType(SemanticsContext& semantics_context, SemanticsNode node,
static auto ProfileType(Context& semantics_context, SemIR::Node node,
llvm::FoldingSetNodeID& canonical_id) -> void {
switch (node.kind()) {
case SemanticsNodeKind::ArrayType: {
case SemIR::NodeKind::ArrayType: {
auto [bound_id, element_type_id] = node.GetAsArrayType();
canonical_id.AddInteger(
semantics_context.semantics_ir().GetArrayBoundValue(bound_id));
canonical_id.AddInteger(element_type_id.index);
break;
}
case SemanticsNodeKind::Builtin:
case SemIR::NodeKind::Builtin:
canonical_id.AddInteger(node.GetAsBuiltin().AsInt());
break;
case SemanticsNodeKind::CrossReference: {
case SemIR::NodeKind::CrossReference: {
// TODO: Cross-references should be canonicalized by looking at their
// target rather than treating them as new unique types.
auto [xref_id, node_id] = node.GetAsCrossReference();
@@ -543,14 +530,14 @@ static auto ProfileType(SemanticsContext& semantics_context, SemanticsNode node,
canonical_id.AddInteger(node_id.index);
break;
}
case SemanticsNodeKind::ConstType:
case SemIR::NodeKind::ConstType:
canonical_id.AddInteger(
semantics_context.GetUnqualifiedType(node.GetAsConstType()).index);
break;
case SemanticsNodeKind::PointerType:
case SemIR::NodeKind::PointerType:
canonical_id.AddInteger(node.GetAsPointerType().index);
break;
case SemanticsNodeKind::StructType: {
case SemIR::NodeKind::StructType: {
auto refs =
semantics_context.semantics_ir().GetNodeBlock(node.GetAsStructType());
for (const auto& ref_id : refs) {
@@ -561,17 +548,17 @@ static auto ProfileType(SemanticsContext& semantics_context, SemanticsNode node,
}
break;
}
case SemanticsNodeKind::StubReference: {
case SemIR::NodeKind::StubReference: {
// We rely on stub references not referring to each other to ensure we
// only recurse once here.
auto inner =
semantics_context.semantics_ir().GetNode(node.GetAsStubReference());
CARBON_CHECK(inner.kind() != SemanticsNodeKind::StubReference)
CARBON_CHECK(inner.kind() != SemIR::NodeKind::StubReference)
<< "A stub reference should never refer to another stub reference.";
ProfileType(semantics_context, inner, canonical_id);
break;
}
case SemanticsNodeKind::TupleType:
case SemIR::NodeKind::TupleType:
ProfileTupleType(
semantics_context.semantics_ir().GetTypeBlock(node.GetAsTupleType()),
canonical_id);
@@ -581,8 +568,8 @@ static auto ProfileType(SemanticsContext& semantics_context, SemanticsNode node,
}
}
auto SemanticsContext::CanonicalizeTypeAndAddNodeIfNew(SemanticsNode node)
-> SemanticsTypeId {
auto Context::CanonicalizeTypeAndAddNodeIfNew(SemIR::Node node)
-> SemIR::TypeId {
auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
ProfileType(*this, node, canonical_id);
};
@@ -590,8 +577,7 @@ auto SemanticsContext::CanonicalizeTypeAndAddNodeIfNew(SemanticsNode node)
return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
}
auto SemanticsContext::CanonicalizeType(SemanticsNodeId node_id)
-> SemanticsTypeId {
auto Context::CanonicalizeType(SemIR::NodeId node_id) -> SemIR::TypeId {
auto it = canonical_types_.find(node_id);
if (it != canonical_types_.end()) {
return it->second;
@@ -605,17 +591,17 @@ auto SemanticsContext::CanonicalizeType(SemanticsNodeId node_id)
return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
}
auto SemanticsContext::CanonicalizeStructType(ParseTree::Node parse_node,
SemanticsNodeBlockId refs_id)
-> SemanticsTypeId {
return CanonicalizeTypeAndAddNodeIfNew(SemanticsNode::StructType::Make(
parse_node, SemanticsTypeId::TypeType, refs_id));
auto Context::CanonicalizeStructType(ParseTree::Node parse_node,
SemIR::NodeBlockId refs_id)
-> SemIR::TypeId {
return CanonicalizeTypeAndAddNodeIfNew(SemIR::Node::StructType::Make(
parse_node, SemIR::TypeId::TypeType, refs_id));
}
auto SemanticsContext::CanonicalizeTupleType(
ParseTree::Node parse_node, llvm::SmallVector<SemanticsTypeId>&& type_ids)
-> SemanticsTypeId {
// Defer allocating a SemanticsTypeBlockId until we know this is a new type.
auto Context::CanonicalizeTupleType(ParseTree::Node parse_node,
llvm::SmallVector<SemIR::TypeId>&& type_ids)
-> SemIR::TypeId {
// Defer allocating a SemIR::TypeBlockId until we know this is a new type.
auto profile_tuple = [&](llvm::FoldingSetNodeID& canonical_id) {
ProfileTupleType(type_ids, canonical_id);
};
@@ -623,36 +609,33 @@ auto SemanticsContext::CanonicalizeTupleType(
auto type_block_id = semantics_ir_->AddTypeBlock();
auto& type_block = semantics_ir_->GetTypeBlock(type_block_id);
type_block = std::move(type_ids);
return AddNode(SemanticsNode::TupleType::Make(
parse_node, SemanticsTypeId::TypeType, type_block_id));
return AddNode(SemIR::Node::TupleType::Make(
parse_node, SemIR::TypeId::TypeType, type_block_id));
};
return CanonicalizeTypeImpl(SemanticsNodeKind::TupleType, profile_tuple,
return CanonicalizeTypeImpl(SemIR::NodeKind::TupleType, profile_tuple,
make_tuple_node);
}
auto SemanticsContext::GetPointerType(ParseTree::Node parse_node,
SemanticsTypeId pointee_type_id)
-> SemanticsTypeId {
return CanonicalizeTypeAndAddNodeIfNew(SemanticsNode::PointerType::Make(
parse_node, SemanticsTypeId::TypeType, pointee_type_id));
auto Context::GetPointerType(ParseTree::Node parse_node,
SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
return CanonicalizeTypeAndAddNodeIfNew(SemIR::Node::PointerType::Make(
parse_node, SemIR::TypeId::TypeType, pointee_type_id));
}
auto SemanticsContext::GetUnqualifiedType(SemanticsTypeId type_id)
-> SemanticsTypeId {
SemanticsNode type_node =
auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
SemIR::Node type_node =
semantics_ir_->GetNode(semantics_ir_->GetTypeAllowBuiltinTypes(type_id));
if (type_node.kind() == SemanticsNodeKind::ConstType) {
if (type_node.kind() == SemIR::NodeKind::ConstType) {
return type_node.GetAsConstType();
}
return type_id;
}
auto SemanticsContext::PrintForStackDump(llvm::raw_ostream& output) const
-> void {
auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
node_stack_.PrintForStackDump(output);
node_block_stack_.PrintForStackDump(output);
params_or_args_stack_.PrintForStackDump(output);
args_type_info_stack_.PrintForStackDump(output);
}
} // namespace Carbon
} // namespace Carbon::Check