Use the scope stack instead of the decl state stack for context (#3460)

This commit is contained in:
josh11b
2023-12-06 17:59:22 +00:00
committed by GitHub
parent 2de79a6db0
commit f4677eea8d
4 changed files with 44 additions and 41 deletions
+22 -23
View File
@@ -64,34 +64,33 @@ auto ForbidModifiersOnDecl(Context& context, KeywordModifierSet forbidden,
auto CheckAccessModifiersOnDecl(Context& context, Lex::TokenKind decl_kind)
-> void {
switch (context.decl_state_stack().containing().kind) {
case DeclState::FileScope:
ForbidModifiersOnDecl(
context, KeywordModifierSet::Protected, decl_kind,
" at file scope, `protected` is only allowed on class members");
break;
case DeclState::Class:
// Both `private` and `protected` allowed in a class definition.
break;
default:
// Otherwise neither `private` nor `protected` allowed.
ForbidModifiersOnDecl(context, KeywordModifierSet::Protected, decl_kind,
", `protected` is only allowed on class members");
ForbidModifiersOnDecl(
context, KeywordModifierSet::Private, decl_kind,
", `private` is only allowed on class members and at file scope");
break;
if (context.at_file_scope()) {
ForbidModifiersOnDecl(
context, KeywordModifierSet::Protected, decl_kind,
" at file scope, `protected` is only allowed on class members");
return;
}
if (auto kind = context.current_scope_kind()) {
if (*kind == SemIR::ClassDecl::Kind) {
// Both `private` and `protected` allowed in a class definition.
return;
}
}
// Otherwise neither `private` nor `protected` allowed.
ForbidModifiersOnDecl(context, KeywordModifierSet::Protected, decl_kind,
", `protected` is only allowed on class members");
ForbidModifiersOnDecl(
context, KeywordModifierSet::Private, decl_kind,
", `private` is only allowed on class members and at file scope");
}
auto RequireDefaultFinalOnlyInInterfaces(Context& context,
Lex::TokenKind decl_kind) -> void {
if (context.decl_state_stack().containing().kind != DeclState::Interface) {
ForbidModifiersOnDecl(context, KeywordModifierSet::Interface, decl_kind,
" outside of an interface");
}
// TODO: Skip this if *context.current_scope_kind() == SemIR::InterfaceDecl
ForbidModifiersOnDecl(context, KeywordModifierSet::Interface, decl_kind,
" outside of an interface");
}
} // namespace Carbon::Check