mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 14:32:42 +00:00
This is the final step in landing the _structural_ updates to diagnostic rendering. The key idea here is that we want the primary diagnostic to have a message clearly explaining the problem and attached to the code that has that problem. This should be the starting point for readers working to address the issue, but is rendered attached to the code snippet in question so errors are always "in context" of the code with the error. Everything else in a diagnostic is an additional label attached to code to explain or help the reader understand the problem in the primary message. Previously, `ContextScope` resulted in some confusion here, as diagnostic consumers turned messages into labels and made the message in the context scope the message. In essence, this isn't a scope with context _for_ a diagnostic (with two exceptions that should probably have been an `AnnotationScope`), but a context within `check` where we have a preferred message for a diagnostic emitted by general purpose code we call into. For example, "class was forward declared here" starts as an error, and then consumers render it as a label to make the message from the context scope the error. We also grew an `AnnotationScope` to handle the contextual information we want to show for a diagnostic (what I would have assumed `ContextScope` actually did). Now we have a `Diagnoser`: code in `check` that can better explain the problem than shared logic it is calling into passes a callback that builds a caller-specific diagnostic. The callee then can attach relevant labels and emit it. Diagnostics no longer store `contexts` and consumers no longer need the logic to restructure things. We move all (but two) of the old contexts to be normal messages emitted in this way, and we move some diagnostics to become labels. Each of the new normal messages corresponds to a new diagnostic kind as usual. Common code following this pattern now _requires_ the `Diagnoser` to be passed (or emit nothing), and this uncovered three places where an empty context was used that made the rendered form miss key information. All three are fixed with new diagnostics, one already covered by tests, and two with TODOs to add test coverage. We also completely remove the old `ContextScope` and `CARBON_DIAGNOSTIC_CONTEXT` and rename `AnnotationScope` to `ContextScope`. It is only used to attach information about when/where a diagnostic occurred. Labels also no longer have categories. What were `Primary` are now `ExtraRange`s on the diagnostic rather than labels. Labels are now always informational. `DeclaredName` now narrows the range of a label when the range is a declaration of a named entity to only be the range of that name. For entities that don't have a declared name, the entire declaration remains the range. The code building and emitting these diagnostics can skip them if the declared location isn't distinct from the diagnostic location. Wording is largely unchanged here so we can just review the _structural_ change. #7991 iterates using this to improve locations and adds missing labels. A subsequent PR will iterate on the wording to increasingly fit within this structural model. This is separated out as a way to review incrementally, and so that subsequently we don't have to review wording improvements that only make sense in a new structure that you cannot see yet. It seems easier to look at the structure here even with awkward wording. Assisted-by: Claude Code
530 lines
21 KiB
C++
530 lines
21 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 <optional>
|
|
#include <tuple>
|
|
|
|
#include "toolchain/base/kind_switch.h"
|
|
#include "toolchain/check/class.h"
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/check/convert.h"
|
|
#include "toolchain/check/decl_name_stack.h"
|
|
#include "toolchain/check/diagnostic_helpers.h"
|
|
#include "toolchain/check/eval.h"
|
|
#include "toolchain/check/generic.h"
|
|
#include "toolchain/check/handle.h"
|
|
#include "toolchain/check/impl.h"
|
|
#include "toolchain/check/import.h"
|
|
#include "toolchain/check/import_ref.h"
|
|
#include "toolchain/check/inst.h"
|
|
#include "toolchain/check/merge.h"
|
|
#include "toolchain/check/modifiers.h"
|
|
#include "toolchain/check/name_component.h"
|
|
#include "toolchain/check/name_lookup.h"
|
|
#include "toolchain/check/type.h"
|
|
#include "toolchain/check/type_completion.h"
|
|
#include "toolchain/diagnostics/emitter.h"
|
|
#include "toolchain/parse/node_ids.h"
|
|
#include "toolchain/sem_ir/class.h"
|
|
#include "toolchain/sem_ir/function.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/inst.h"
|
|
#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto HandleParseNode(Context& context, Parse::ClassIntroducerId node_id)
|
|
-> bool {
|
|
// This class is potentially generic.
|
|
StartGenericDecl(context);
|
|
// Create an instruction block to hold the instructions created as part of the
|
|
// class signature, such as generic parameters.
|
|
context.inst_block_stack().Push();
|
|
// Push the bracketing node.
|
|
context.node_stack().Push(node_id);
|
|
// Optional modifiers and the name follow.
|
|
context.decl_introducer_state_stack().Push<Lex::TokenKind::Class>();
|
|
context.decl_name_stack().PushScopeAndStartName();
|
|
return true;
|
|
}
|
|
|
|
static auto BuildClassDecl(Context& context, Parse::AnyClassDeclId node_id,
|
|
bool is_definition)
|
|
-> std::tuple<SemIR::ClassId, SemIR::InstId> {
|
|
auto name = PopNameComponent(context);
|
|
auto name_context = context.decl_name_stack().FinishName(name);
|
|
context.node_stack()
|
|
.PopAndDiscardSoloNodeId<Parse::NodeKind::ClassIntroducer>();
|
|
|
|
// Process modifiers.
|
|
auto [_, parent_scope_inst] =
|
|
context.name_scopes().GetInstIfValid(name_context.parent_scope_id);
|
|
auto introducer =
|
|
context.decl_introducer_state_stack().Pop<Lex::TokenKind::Class>();
|
|
CheckAccessModifiersOnDecl(context, introducer, parent_scope_inst);
|
|
auto always_acceptable_modifiers =
|
|
KeywordModifierSet::Access | KeywordModifierSet::Extern;
|
|
LimitModifiersOnDecl(context, introducer,
|
|
always_acceptable_modifiers | KeywordModifierSet::Class);
|
|
if (!is_definition) {
|
|
LimitModifiersOnNotDefinition(context, introducer,
|
|
always_acceptable_modifiers);
|
|
}
|
|
RestrictExternModifierOnDecl(context, introducer, parent_scope_inst,
|
|
is_definition);
|
|
|
|
bool is_extern = introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extern);
|
|
if (introducer.extern_library.has_value()) {
|
|
context.TODO(node_id, "extern library");
|
|
}
|
|
auto inheritance_kind =
|
|
introducer.modifier_set.ToEnum<SemIR::Class::InheritanceKind>()
|
|
.Case(KeywordModifierSet::Abstract, SemIR::Class::Abstract)
|
|
.Case(KeywordModifierSet::Base, SemIR::Class::Base)
|
|
.Default(SemIR::Class::Final);
|
|
|
|
auto decl_block_id = context.inst_block_stack().Pop();
|
|
|
|
// Add the class declaration.
|
|
auto class_decl = SemIR::ClassDecl{.type_id = SemIR::TypeType::TypeId,
|
|
.class_id = SemIR::ClassId::None,
|
|
.decl_block_id = decl_block_id};
|
|
auto class_decl_id = AddPlaceholderInst(context, node_id, class_decl);
|
|
|
|
// TODO: Store state regarding is_extern.
|
|
SemIR::Class class_info = {
|
|
name_context.MakeEntityWithParamsBase(name, class_decl_id, is_extern,
|
|
SemIR::LibraryNameId::None),
|
|
{// `.self_type_id` depends on the ClassType, so is set below.
|
|
.self_type_id = SemIR::TypeId::None,
|
|
.inheritance_kind = inheritance_kind}};
|
|
|
|
DiagnoseIfGenericMissingExplicitParameters(context, class_info);
|
|
|
|
SemIR::ScopeLookupResult lookup_result =
|
|
context.decl_name_stack().LookupOrAddName(
|
|
name_context, class_decl_id, introducer.modifier_set.GetAccessKind());
|
|
TryMergeRedecl(context, name_context, lookup_result,
|
|
MergeRedeclEntityInfo<SemIR::Class>{
|
|
.new_entity_decl = class_decl, .new_entity = class_info},
|
|
is_definition);
|
|
|
|
// Create a new class if this isn't a valid redeclaration.
|
|
bool is_new_class = !class_decl.class_id.has_value();
|
|
if (is_new_class) {
|
|
// TODO: If this is an invalid redeclaration of a non-class entity or there
|
|
// was an error in the qualifier, we will have lost track of the class name
|
|
// here. We should keep track of it even if the name is invalid.
|
|
class_info.generic_id = BuildGenericDecl(context, class_decl_id);
|
|
class_decl.class_id = context.classes().Add(class_info);
|
|
if (class_info.has_parameters()) {
|
|
class_decl.type_id = GetParameterizedClassType(
|
|
context, class_decl.class_id, context.scope_stack().PeekSpecificId());
|
|
}
|
|
} else {
|
|
auto prev_decl_generic_id =
|
|
context.classes().Get(class_decl.class_id).generic_id;
|
|
FinishGenericRedecl(context, prev_decl_generic_id);
|
|
}
|
|
|
|
// Write the class ID into the ClassDecl.
|
|
ReplaceInstBeforeConstantUse(context, class_decl_id, class_decl);
|
|
|
|
if (is_new_class) {
|
|
// TODO: Form this as part of building the definition, not as part of the
|
|
// declaration.
|
|
SetClassSelfType(context, class_decl.class_id);
|
|
}
|
|
|
|
if (!is_definition && context.sem_ir().is_impl() && !is_extern) {
|
|
context.definitions_required_by_decl().push_back(class_decl_id);
|
|
}
|
|
|
|
return {class_decl.class_id, class_decl_id};
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::ClassDeclId node_id) -> bool {
|
|
BuildClassDecl(context, node_id, /*is_definition=*/false);
|
|
context.decl_name_stack().PopScope();
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::ClassDefinitionStartId node_id)
|
|
-> bool {
|
|
auto [class_id, class_decl_id] =
|
|
BuildClassDecl(context, node_id, /*is_definition=*/true);
|
|
auto& class_info = context.classes().Get(class_id);
|
|
StartClassDefinition(context, class_info, class_decl_id);
|
|
|
|
// Enter the class scope.
|
|
context.scope_stack().PushForEntity(
|
|
class_decl_id, class_info.scope_id,
|
|
context.generics().GetSelfSpecific(class_info.generic_id));
|
|
StartGenericDefinition(context, class_info.generic_id);
|
|
|
|
context.inst_block_stack().Push();
|
|
context.node_stack().Push(node_id, class_id);
|
|
context.field_decls_stack().PushArray();
|
|
context.friend_scopes_stack().PushArray();
|
|
context.vtable_stack().Push();
|
|
|
|
// TODO: Handle the case where there's control flow in the class body. For
|
|
// example:
|
|
//
|
|
// class C {
|
|
// var v: if true then i32 else f64;
|
|
// }
|
|
//
|
|
// We may need to track a list of instruction blocks here, as we do for a
|
|
// function.
|
|
class_info.body_block_id = context.inst_block_stack().PeekOrAdd();
|
|
return true;
|
|
}
|
|
|
|
// Diagnoses a class-specific declaration appearing outside a class.
|
|
static auto DiagnoseClassSpecificDeclOutsideClass(Context& context,
|
|
SemIR::LocId loc_id,
|
|
Lex::TokenKind tok) -> void {
|
|
CARBON_DIAGNOSTIC(ClassSpecificDeclOutsideClass, Error,
|
|
"`{0}` declaration outside class", Lex::TokenKind);
|
|
context.emitter().Emit(loc_id, ClassSpecificDeclOutsideClass, tok);
|
|
}
|
|
|
|
// Returns the current scope's class declaration, or diagnoses if it isn't a
|
|
// class.
|
|
static auto GetCurrentScopeAsClassOrDiagnose(Context& context,
|
|
SemIR::LocId loc_id,
|
|
Lex::TokenKind tok)
|
|
-> std::optional<SemIR::ClassDecl> {
|
|
auto class_scope =
|
|
context.scope_stack().TryGetCurrentScopeAs<SemIR::ClassDecl>();
|
|
if (!class_scope) {
|
|
DiagnoseClassSpecificDeclOutsideClass(context, loc_id, tok);
|
|
}
|
|
return class_scope;
|
|
}
|
|
|
|
// Diagnoses a class-specific declaration that is repeated within a class, but
|
|
// is not permitted to be repeated.
|
|
static auto DiagnoseClassSpecificDeclRepeated(Context& context,
|
|
SemIR::LocId new_loc_id,
|
|
SemIR::LocId prev_loc_id,
|
|
Lex::TokenKind tok) -> void {
|
|
CARBON_DIAGNOSTIC(AdaptDeclRepeated, Error,
|
|
"multiple `adapt` declarations in class");
|
|
CARBON_DIAGNOSTIC(BaseDeclRepeated, Error,
|
|
"multiple `base` declarations in class; multiple "
|
|
"inheritance is not permitted");
|
|
CARBON_DIAGNOSTIC_LABEL(ClassSpecificDeclPrevious,
|
|
"previous `{0}` declaration is here", Lex::TokenKind);
|
|
CARBON_CHECK(tok == Lex::TokenKind::Adapt || tok == Lex::TokenKind::Base);
|
|
context.emitter()
|
|
.Build(new_loc_id, tok == Lex::TokenKind::Adapt ? AdaptDeclRepeated
|
|
: BaseDeclRepeated)
|
|
.Attach(prev_loc_id, ClassSpecificDeclPrevious, tok)
|
|
.Emit();
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::AdaptIntroducerId /*node_id*/)
|
|
-> bool {
|
|
context.decl_introducer_state_stack().Push<Lex::TokenKind::Adapt>();
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::AdaptDeclId node_id) -> bool {
|
|
auto [adapted_type_node, adapted_type_expr_id] =
|
|
context.node_stack().PopExprWithNodeId();
|
|
|
|
// Process modifiers. `extend` is permitted, no others are allowed.
|
|
auto introducer =
|
|
context.decl_introducer_state_stack().Pop<Lex::TokenKind::Adapt>();
|
|
LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Extend);
|
|
|
|
auto parent_class_decl =
|
|
GetCurrentScopeAsClassOrDiagnose(context, node_id, Lex::TokenKind::Adapt);
|
|
if (!parent_class_decl) {
|
|
return true;
|
|
}
|
|
|
|
auto& class_info = context.classes().Get(parent_class_decl->class_id);
|
|
if (class_info.adapt_id.has_value()) {
|
|
DiagnoseClassSpecificDeclRepeated(context, node_id,
|
|
SemIR::LocId(class_info.adapt_id),
|
|
Lex::TokenKind::Adapt);
|
|
return true;
|
|
}
|
|
|
|
auto [adapted_type_inst_id, adapted_type_id] =
|
|
ExprAsType(context, node_id, adapted_type_expr_id);
|
|
if (!RequireConcreteType(
|
|
context, adapted_type_id, node_id,
|
|
[&] {
|
|
CARBON_DIAGNOSTIC(IncompleteTypeInAdaptDecl, Error,
|
|
"adapted type {0} is an incomplete type",
|
|
InstIdAsType);
|
|
return context.emitter().Build(adapted_type_node,
|
|
IncompleteTypeInAdaptDecl,
|
|
adapted_type_inst_id);
|
|
},
|
|
[&] {
|
|
CARBON_DIAGNOSTIC(AbstractTypeInAdaptDecl, Error,
|
|
"adapted type {0} is an abstract type",
|
|
InstIdAsType);
|
|
return context.emitter().Build(adapted_type_node,
|
|
AbstractTypeInAdaptDecl,
|
|
adapted_type_inst_id);
|
|
})) {
|
|
adapted_type_id = SemIR::ErrorInst::TypeId;
|
|
}
|
|
if (adapted_type_id == SemIR::ErrorInst::TypeId) {
|
|
adapted_type_inst_id = SemIR::ErrorInst::TypeInstId;
|
|
}
|
|
|
|
// Build a SemIR representation for the declaration.
|
|
class_info.adapt_id = AddInst<SemIR::AdaptDecl>(
|
|
context, node_id, {.adapted_type_inst_id = adapted_type_inst_id});
|
|
|
|
// Extend the class scope with the adapted type's scope if requested.
|
|
if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extend)) {
|
|
auto& class_scope = context.name_scopes().Get(class_info.scope_id);
|
|
class_scope.AddExtendedScope({adapted_type_inst_id});
|
|
}
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::BaseIntroducerId /*node_id*/)
|
|
-> bool {
|
|
context.decl_introducer_state_stack().Push<Lex::TokenKind::Base>();
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& /*context*/, Parse::BaseColonId /*node_id*/)
|
|
-> bool {
|
|
return true;
|
|
}
|
|
|
|
namespace {
|
|
// Information gathered about a base type specified in a `base` declaration.
|
|
struct BaseInfo {
|
|
// A `BaseInfo` representing an erroneous base.
|
|
static const BaseInfo Error;
|
|
|
|
SemIR::TypeId type_id;
|
|
SemIR::NameScopeId scope_id;
|
|
SemIR::TypeInstId inst_id;
|
|
};
|
|
constexpr BaseInfo BaseInfo::Error = {.type_id = SemIR::ErrorInst::TypeId,
|
|
.scope_id = SemIR::NameScopeId::None,
|
|
.inst_id = SemIR::ErrorInst::TypeInstId};
|
|
} // namespace
|
|
|
|
// Diagnoses an attempt to derive from a final type.
|
|
static auto DiagnoseBaseIsFinal(Context& context, Parse::NodeId node_id,
|
|
SemIR::TypeInstId base_type_inst_id) -> void {
|
|
CARBON_DIAGNOSTIC(BaseIsFinal, Error,
|
|
"deriving from final type {0}; base type must be an "
|
|
"`abstract` or `base` class",
|
|
InstIdAsType);
|
|
context.emitter().Emit(node_id, BaseIsFinal, base_type_inst_id);
|
|
}
|
|
|
|
// Checks that the specified base type is valid.
|
|
static auto CheckBaseType(Context& context, Parse::NodeId node_id,
|
|
SemIR::InstId base_expr_id) -> BaseInfo {
|
|
auto [base_type_inst_id, base_type_id] =
|
|
ExprAsType(context, node_id, base_expr_id);
|
|
if (base_type_id == SemIR::ErrorInst::TypeId) {
|
|
return BaseInfo::Error;
|
|
}
|
|
if (!RequireCompleteType(context, base_type_id, node_id, [&] {
|
|
CARBON_DIAGNOSTIC(IncompleteTypeInBaseDecl, Error,
|
|
"base {0} is an incomplete type", InstIdAsType);
|
|
return context.emitter().Build(node_id, IncompleteTypeInBaseDecl,
|
|
base_type_inst_id);
|
|
})) {
|
|
return BaseInfo::Error;
|
|
}
|
|
|
|
auto class_type = context.types().TryGetAs<SemIR::ClassType>(base_type_id);
|
|
|
|
// The base must not be a final class.
|
|
if (!class_type) {
|
|
// For now, we treat all types that aren't introduced by a `class`
|
|
// declaration as being final classes.
|
|
// TODO: Once we have a better idea of which types are considered to be
|
|
// classes, produce a better diagnostic for deriving from a non-class type.
|
|
DiagnoseBaseIsFinal(context, node_id, base_type_inst_id);
|
|
return BaseInfo::Error;
|
|
}
|
|
|
|
const auto& base_class_info = context.classes().Get(class_type->class_id);
|
|
|
|
if (base_class_info.inheritance_kind == SemIR::Class::Final) {
|
|
DiagnoseBaseIsFinal(context, node_id, base_type_inst_id);
|
|
}
|
|
|
|
CARBON_CHECK(base_class_info.scope_id.has_value(),
|
|
"Complete class should have a scope");
|
|
return {.type_id = base_type_id,
|
|
.scope_id = base_class_info.scope_id,
|
|
.inst_id = base_type_inst_id};
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::BaseDeclId node_id) -> bool {
|
|
auto [base_type_node_id, base_type_expr_id] =
|
|
context.node_stack().PopExprWithNodeId();
|
|
|
|
// Process modifiers. `extend` is required, no others are allowed.
|
|
auto introducer =
|
|
context.decl_introducer_state_stack().Pop<Lex::TokenKind::Base>();
|
|
LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Extend);
|
|
if (!introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extend)) {
|
|
CARBON_DIAGNOSTIC(BaseMissingExtend, Error,
|
|
"missing `extend` before `base` declaration");
|
|
context.emitter().Emit(node_id, BaseMissingExtend);
|
|
}
|
|
|
|
auto parent_class_decl =
|
|
GetCurrentScopeAsClassOrDiagnose(context, node_id, Lex::TokenKind::Base);
|
|
if (!parent_class_decl) {
|
|
return true;
|
|
}
|
|
|
|
auto& class_info = context.classes().Get(parent_class_decl->class_id);
|
|
if (class_info.base_id.has_value()) {
|
|
DiagnoseClassSpecificDeclRepeated(context, node_id,
|
|
SemIR::LocId(class_info.base_id),
|
|
Lex::TokenKind::Base);
|
|
return true;
|
|
}
|
|
|
|
if (!context.field_decls_stack().PeekArray().empty()) {
|
|
// TODO: Add note that includes the first field location as an example.
|
|
CARBON_DIAGNOSTIC(
|
|
BaseDeclAfterFieldDecl, Error,
|
|
"`base` declaration must appear before field declarations");
|
|
context.emitter().Emit(node_id, BaseDeclAfterFieldDecl);
|
|
return true;
|
|
}
|
|
|
|
auto base_info = CheckBaseType(context, base_type_node_id, base_type_expr_id);
|
|
|
|
// TODO: Should we diagnose if there are already any fields?
|
|
|
|
// The `base` value in the class scope has an unbound element type. Instance
|
|
// binding will be performed when it's found by name lookup into an instance.
|
|
auto field_type_id = GetUnboundElementType(
|
|
context, context.types().GetTypeInstId(class_info.self_type_id),
|
|
base_info.inst_id);
|
|
class_info.base_id =
|
|
AddInst<SemIR::BaseDecl>(context, node_id,
|
|
{.type_id = field_type_id,
|
|
.base_type_inst_id = base_info.inst_id,
|
|
.index = SemIR::ElementIndex::None});
|
|
|
|
if (base_info.type_id != SemIR::ErrorInst::TypeId) {
|
|
auto base_class_info = context.classes().Get(
|
|
context.types().GetAs<SemIR::ClassType>(base_info.type_id).class_id);
|
|
class_info.is_dynamic |= base_class_info.is_dynamic;
|
|
}
|
|
|
|
// Bind the name `base` in the class to the base field.
|
|
context.decl_name_stack().AddNameOrDiagnose(
|
|
context.decl_name_stack().MakeUnqualifiedName(node_id,
|
|
SemIR::NameId::Base),
|
|
class_info.base_id, introducer.modifier_set.GetAccessKind());
|
|
|
|
// Extend the class scope with the base class.
|
|
if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extend)) {
|
|
auto& class_scope = context.name_scopes().Get(class_info.scope_id);
|
|
if (base_info.scope_id.has_value()) {
|
|
class_scope.AddExtendedScope({base_info.inst_id});
|
|
} else {
|
|
class_scope.set_has_error();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& /*context*/,
|
|
Parse::FriendIntroducerId /*node_id*/) -> bool {
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::FriendDeclId node_id) -> bool {
|
|
auto [scope_node_id, scope_inst_id] =
|
|
context.node_stack().PopWithNodeId<Parse::NodeKind::IdentifierNameExpr>();
|
|
|
|
auto access_scope_inst_id = context.scope_stack().PeekInstId();
|
|
auto access_scope_inst = context.constant_values().GetInst(
|
|
context.constant_values().Get(access_scope_inst_id));
|
|
CARBON_KIND_SWITCH(access_scope_inst) {
|
|
case SemIR::ClassType::Kind:
|
|
case SemIR::ParameterizedClassType::Kind:
|
|
break;
|
|
default:
|
|
CARBON_DIAGNOSTIC(FriendNotAllowedInScope, Error,
|
|
"`friend` declaration in non-class scope");
|
|
context.emitter().Emit(node_id, FriendNotAllowedInScope);
|
|
return false;
|
|
}
|
|
|
|
auto scope_const_id = context.constant_values().Get(scope_inst_id);
|
|
CARBON_CHECK(scope_const_id.is_constant());
|
|
auto scope_constant = context.constant_values().GetInst(scope_const_id);
|
|
CARBON_KIND_SWITCH(scope_constant) {
|
|
case SemIR::StructValue::Kind: {
|
|
if (context.types().Is<SemIR::FunctionType>(scope_constant.type_id())) {
|
|
return context.TODO(node_id,
|
|
"befriending a function not yet implemented");
|
|
}
|
|
[[fallthrough]];
|
|
}
|
|
case SemIR::ClassType::Kind:
|
|
case SemIR::ParameterizedClassType::Kind:
|
|
case SemIR::FacetType::Kind:
|
|
break;
|
|
default: {
|
|
CARBON_DIAGNOSTIC(
|
|
FriendNotNameScope, Error,
|
|
"friend declaration names entity that is not a name scope");
|
|
context.emitter().Emit(scope_node_id, FriendNotNameScope);
|
|
return false;
|
|
}
|
|
}
|
|
context.friend_scopes_stack().AppendToTop(scope_const_id);
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::ClassDefinitionId node_id)
|
|
-> bool {
|
|
auto class_id =
|
|
context.node_stack().Pop<Parse::NodeKind::ClassDefinitionStart>();
|
|
|
|
// The class type is now fully defined. Compute its object representation.
|
|
ComputeClassObjectRepr(context, node_id, class_id,
|
|
context.field_decls_stack().PeekArray(),
|
|
context.vtable_stack().PeekCurrentBlockContents(),
|
|
context.inst_block_stack().PeekCurrentBlockContents());
|
|
|
|
context.inst_block_stack().Pop();
|
|
context.field_decls_stack().PopArray();
|
|
context.vtable_stack().Pop();
|
|
|
|
auto& class_info = context.classes().Get(class_id);
|
|
class_info.friend_scopes.GrowForInsertCount(
|
|
context.friend_scopes_stack().PeekArray().size());
|
|
for (auto friend_scope_id : context.friend_scopes_stack().PeekArray()) {
|
|
class_info.friend_scopes.Insert(friend_scope_id);
|
|
}
|
|
context.friend_scopes_stack().PopArray();
|
|
|
|
FinishGenericDefinition(context, class_info.generic_id);
|
|
|
|
// The decl_name_stack and scopes are popped by `ProcessNodeIds`.
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|