modifiers.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/modifiers.h"
  5. namespace Carbon::Check {
  6. static auto ReportNotAllowed(Context& context, Parse::NodeId modifier_node,
  7. Lex::TokenKind decl_kind,
  8. llvm::StringRef context_string,
  9. Parse::NodeId context_node) -> void {
  10. CARBON_DIAGNOSTIC(ModifierNotAllowedOn, Error,
  11. "`{0}` not allowed on `{1}` declaration{2}.",
  12. Lex::TokenKind, Lex::TokenKind, std::string);
  13. auto diag = context.emitter().Build(modifier_node, ModifierNotAllowedOn,
  14. context.token_kind(modifier_node),
  15. decl_kind, context_string.str());
  16. if (context_node.is_valid()) {
  17. CARBON_DIAGNOSTIC(ModifierNotInContext, Note,
  18. "Containing definition here.");
  19. diag.Note(context_node, ModifierNotInContext);
  20. }
  21. diag.Emit();
  22. }
  23. auto LimitModifiersOnDecl(Context& context, KeywordModifierSet allowed,
  24. Lex::TokenKind decl_kind) -> void {
  25. auto& s = context.decl_state_stack().innermost();
  26. auto not_allowed = s.modifier_set & ~allowed;
  27. if (!!(not_allowed & KeywordModifierSet::Access)) {
  28. ReportNotAllowed(context, s.saw_access_modifier, decl_kind, "",
  29. Parse::NodeId::Invalid);
  30. not_allowed = not_allowed & ~KeywordModifierSet::Access;
  31. s.saw_access_modifier = Parse::NodeId::Invalid;
  32. }
  33. if (!!not_allowed) {
  34. ReportNotAllowed(context, s.saw_decl_modifier, decl_kind, "",
  35. Parse::NodeId::Invalid);
  36. s.saw_decl_modifier = Parse::NodeId::Invalid;
  37. }
  38. s.modifier_set &= allowed;
  39. }
  40. auto ForbidModifiersOnDecl(Context& context, KeywordModifierSet forbidden,
  41. Lex::TokenKind decl_kind,
  42. llvm::StringRef context_string,
  43. Parse::NodeId context_node) -> void {
  44. auto& s = context.decl_state_stack().innermost();
  45. auto not_allowed = s.modifier_set & forbidden;
  46. if (!!(not_allowed & KeywordModifierSet::Access)) {
  47. ReportNotAllowed(context, s.saw_access_modifier, decl_kind, context_string,
  48. context_node);
  49. not_allowed = not_allowed & ~KeywordModifierSet::Access;
  50. s.saw_access_modifier = Parse::NodeId::Invalid;
  51. }
  52. if (!!not_allowed) {
  53. ReportNotAllowed(context, s.saw_decl_modifier, decl_kind, context_string,
  54. context_node);
  55. s.saw_decl_modifier = Parse::NodeId::Invalid;
  56. }
  57. s.modifier_set = s.modifier_set & ~forbidden;
  58. }
  59. auto CheckAccessModifiersOnDecl(Context& context, Lex::TokenKind decl_kind)
  60. -> void {
  61. if (context.at_file_scope()) {
  62. ForbidModifiersOnDecl(
  63. context, KeywordModifierSet::Protected, decl_kind,
  64. " at file scope, `protected` is only allowed on class members");
  65. return;
  66. }
  67. if (auto kind = context.current_scope_kind()) {
  68. if (*kind == SemIR::ClassDecl::Kind) {
  69. // Both `private` and `protected` allowed in a class definition.
  70. return;
  71. }
  72. }
  73. // Otherwise neither `private` nor `protected` allowed.
  74. ForbidModifiersOnDecl(context, KeywordModifierSet::Protected, decl_kind,
  75. ", `protected` is only allowed on class members");
  76. ForbidModifiersOnDecl(
  77. context, KeywordModifierSet::Private, decl_kind,
  78. ", `private` is only allowed on class members and at file scope");
  79. }
  80. auto RequireDefaultFinalOnlyInInterfaces(Context& context,
  81. Lex::TokenKind decl_kind) -> void {
  82. // TODO: Skip this if *context.current_scope_kind() == SemIR::InterfaceDecl
  83. ForbidModifiersOnDecl(context, KeywordModifierSet::Interface, decl_kind,
  84. " outside of an interface");
  85. }
  86. } // namespace Carbon::Check