For modifiers, get the TokenKind from DeclKind instead of argument. (#4028)

This commit is contained in:
Jon Ross-Perkins
2024-06-05 16:23:36 +00:00
committed by GitHub
parent a910eda020
commit 0ffa5bf659
13 changed files with 83 additions and 78 deletions
+47 -16
View File
@@ -8,6 +8,43 @@
namespace Carbon::Check {
// Returns the TokenKind for a DeclKind.
static auto DeclKindToken(DeclIntroducerState::DeclKind decl_kind)
-> Lex::TokenKind {
switch (decl_kind) {
case DeclIntroducerState::Adapt:
return Lex::TokenKind::Adapt;
case DeclIntroducerState::Alias:
return Lex::TokenKind::Alias;
case DeclIntroducerState::Base:
return Lex::TokenKind::Base;
case DeclIntroducerState::Class:
return Lex::TokenKind::Class;
case DeclIntroducerState::Constraint:
return Lex::TokenKind::Constraint;
case DeclIntroducerState::Export:
return Lex::TokenKind::Export;
case DeclIntroducerState::Fn:
return Lex::TokenKind::Fn;
case DeclIntroducerState::Import:
return Lex::TokenKind::Import;
case DeclIntroducerState::Impl:
return Lex::TokenKind::Impl;
case DeclIntroducerState::Interface:
return Lex::TokenKind::Interface;
case DeclIntroducerState::Let:
return Lex::TokenKind::Let;
case DeclIntroducerState::Library:
return Lex::TokenKind::Library;
case DeclIntroducerState::Namespace:
return Lex::TokenKind::Namespace;
case DeclIntroducerState::Package:
return Lex::TokenKind::Package;
case DeclIntroducerState::Var:
return Lex::TokenKind::Var;
}
}
static auto DiagnoseNotAllowed(Context& context, Parse::NodeId modifier_node,
Lex::TokenKind decl_kind,
llvm::StringRef context_string,
@@ -40,7 +77,6 @@ static auto ModifierOrderAsSet(ModifierOrder order) -> KeywordModifierSet {
auto ForbidModifiersOnDecl(Context& context, DeclIntroducerState& introducer,
KeywordModifierSet forbidden,
Lex::TokenKind decl_kind,
llvm::StringRef context_string,
SemIR::LocId context_loc_id) -> void {
auto not_allowed = introducer.modifier_set & forbidden;
@@ -52,8 +88,9 @@ auto ForbidModifiersOnDecl(Context& context, DeclIntroducerState& introducer,
order_index <= static_cast<int8_t>(ModifierOrder::Last); ++order_index) {
auto order = static_cast<ModifierOrder>(order_index);
if (not_allowed.HasAnyOf(ModifierOrderAsSet(order))) {
DiagnoseNotAllowed(context, introducer.modifier_node_id(order), decl_kind,
context_string, context_loc_id);
DiagnoseNotAllowed(context, introducer.modifier_node_id(order),
DeclKindToken(introducer.kind), context_string,
context_loc_id);
introducer.set_modifier_node_id(order, Parse::NodeId::Invalid);
}
}
@@ -63,7 +100,6 @@ auto ForbidModifiersOnDecl(Context& context, DeclIntroducerState& introducer,
auto CheckAccessModifiersOnDecl(Context& context,
DeclIntroducerState& introducer,
Lex::TokenKind decl_kind,
std::optional<SemIR::Inst> parent_scope_inst)
-> void {
if (parent_scope_inst) {
@@ -73,7 +109,7 @@ auto CheckAccessModifiersOnDecl(Context& context,
// the parents of the target scope to determine whether we're at file
// scope.
ForbidModifiersOnDecl(
context, introducer, KeywordModifierSet::Protected, decl_kind,
context, introducer, KeywordModifierSet::Protected,
" at file scope, `protected` is only allowed on class members");
return;
}
@@ -86,10 +122,9 @@ auto CheckAccessModifiersOnDecl(Context& context,
// Otherwise neither `private` nor `protected` allowed.
ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Protected,
decl_kind,
", `protected` is only allowed on class members");
ForbidModifiersOnDecl(
context, introducer, KeywordModifierSet::Private, decl_kind,
context, introducer, KeywordModifierSet::Private,
", `private` is only allowed on class members and at file scope");
}
@@ -97,20 +132,17 @@ auto CheckMethodModifiersOnFunction(
Context& context, DeclIntroducerState& introducer,
SemIR::InstId parent_scope_inst_id,
std::optional<SemIR::Inst> parent_scope_inst) -> void {
const Lex::TokenKind decl_kind = Lex::TokenKind::Fn;
if (parent_scope_inst) {
if (auto class_decl = parent_scope_inst->TryAs<SemIR::ClassDecl>()) {
auto inheritance_kind =
context.classes().Get(class_decl->class_id).inheritance_kind;
if (inheritance_kind == SemIR::Class::Final) {
ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Virtual,
decl_kind,
" in a non-abstract non-base `class` definition",
context.insts().GetLocId(parent_scope_inst_id));
}
if (inheritance_kind != SemIR::Class::Abstract) {
ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Abstract,
decl_kind,
" in a non-abstract `class` definition",
context.insts().GetLocId(parent_scope_inst_id));
}
@@ -119,33 +151,32 @@ auto CheckMethodModifiersOnFunction(
}
ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Method,
decl_kind, " outside of a class");
" outside of a class");
}
auto RestrictExternModifierOnDecl(Context& context,
DeclIntroducerState& introducer,
Lex::TokenKind decl_kind,
std::optional<SemIR::Inst> parent_scope_inst,
bool is_definition) -> void {
if (is_definition) {
ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Extern,
decl_kind, " that provides a definition");
" that provides a definition");
}
if (parent_scope_inst && !parent_scope_inst->Is<SemIR::Namespace>()) {
ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Extern,
decl_kind, " that is a member");
" that is a member");
}
}
auto RequireDefaultFinalOnlyInInterfaces(
Context& context, DeclIntroducerState& introducer, Lex::TokenKind decl_kind,
Context& context, DeclIntroducerState& introducer,
std::optional<SemIR::Inst> parent_scope_inst) -> void {
if (parent_scope_inst && parent_scope_inst->Is<SemIR::InterfaceDecl>()) {
// Both `default` and `final` allowed in an interface definition.
return;
}
ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Interface,
decl_kind, " outside of an interface");
" outside of an interface");
}
} // namespace Carbon::Check