modifiers.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "toolchain/check/decl_state.h"
  6. namespace Carbon::Check {
  7. static auto DiagnoseNotAllowed(Context& context, Parse::NodeId modifier_node,
  8. Lex::TokenKind decl_kind,
  9. llvm::StringRef context_string,
  10. SemIR::LocId context_loc_id) -> void {
  11. CARBON_DIAGNOSTIC(ModifierNotAllowedOn, Error,
  12. "`{0}` not allowed on `{1}` declaration{2}.",
  13. Lex::TokenKind, Lex::TokenKind, std::string);
  14. auto diag = context.emitter().Build(modifier_node, ModifierNotAllowedOn,
  15. context.token_kind(modifier_node),
  16. decl_kind, context_string.str());
  17. if (context_loc_id.is_valid()) {
  18. CARBON_DIAGNOSTIC(ModifierNotInContext, Note,
  19. "Containing definition here.");
  20. diag.Note(context_loc_id, ModifierNotInContext);
  21. }
  22. diag.Emit();
  23. }
  24. // Returns the KeywordModifierSet corresponding to the ModifierOrder entry.
  25. static auto ModifierOrderAsSet(ModifierOrder order) -> KeywordModifierSet {
  26. switch (order) {
  27. case ModifierOrder::Access:
  28. return KeywordModifierSet::Access;
  29. case ModifierOrder::Extern:
  30. return KeywordModifierSet::Extern;
  31. case ModifierOrder::Decl:
  32. return KeywordModifierSet::Decl;
  33. }
  34. }
  35. auto ForbidModifiersOnDecl(Context& context, KeywordModifierSet forbidden,
  36. Lex::TokenKind decl_kind,
  37. llvm::StringRef context_string,
  38. SemIR::LocId context_loc_id) -> void {
  39. auto& s = context.decl_state_stack().innermost();
  40. auto not_allowed = s.modifier_set & forbidden;
  41. if (not_allowed.empty()) {
  42. return;
  43. }
  44. for (auto order_index = 0;
  45. order_index <= static_cast<int8_t>(ModifierOrder::Last); ++order_index) {
  46. auto order = static_cast<ModifierOrder>(order_index);
  47. if (not_allowed.HasAnyOf(ModifierOrderAsSet(order))) {
  48. DiagnoseNotAllowed(context, s.modifier_node_id(order), decl_kind,
  49. context_string, context_loc_id);
  50. s.set_modifier_node_id(order, Parse::NodeId::Invalid);
  51. }
  52. }
  53. s.modifier_set.Remove(forbidden);
  54. }
  55. auto CheckAccessModifiersOnDecl(Context& context, Lex::TokenKind decl_kind,
  56. std::optional<SemIR::Inst> enclosing_scope_inst)
  57. -> void {
  58. if (enclosing_scope_inst) {
  59. if (enclosing_scope_inst->Is<SemIR::Namespace>()) {
  60. // TODO: This assumes that namespaces can only be declared at file scope.
  61. // If we add support for non-file-scope namespaces, we will need to check
  62. // the parents of the target scope to determine whether we're at file
  63. // scope.
  64. ForbidModifiersOnDecl(
  65. context, KeywordModifierSet::Protected, decl_kind,
  66. " at file scope, `protected` is only allowed on class members");
  67. return;
  68. }
  69. if (enclosing_scope_inst->Is<SemIR::ClassDecl>()) {
  70. // Both `private` and `protected` allowed in a class definition.
  71. return;
  72. }
  73. }
  74. // Otherwise neither `private` nor `protected` allowed.
  75. ForbidModifiersOnDecl(context, KeywordModifierSet::Protected, decl_kind,
  76. ", `protected` is only allowed on class members");
  77. ForbidModifiersOnDecl(
  78. context, KeywordModifierSet::Private, decl_kind,
  79. ", `private` is only allowed on class members and at file scope");
  80. }
  81. auto CheckMethodModifiersOnFunction(
  82. Context& context, SemIR::InstId enclosing_scope_inst_id,
  83. std::optional<SemIR::Inst> enclosing_scope_inst) -> void {
  84. const Lex::TokenKind decl_kind = Lex::TokenKind::Fn;
  85. if (enclosing_scope_inst) {
  86. if (auto class_decl = enclosing_scope_inst->TryAs<SemIR::ClassDecl>()) {
  87. auto inheritance_kind =
  88. context.classes().Get(class_decl->class_id).inheritance_kind;
  89. if (inheritance_kind == SemIR::Class::Final) {
  90. ForbidModifiersOnDecl(
  91. context, KeywordModifierSet::Virtual, decl_kind,
  92. " in a non-abstract non-base `class` definition",
  93. context.insts().GetLocId(enclosing_scope_inst_id));
  94. }
  95. if (inheritance_kind != SemIR::Class::Abstract) {
  96. ForbidModifiersOnDecl(
  97. context, KeywordModifierSet::Abstract, decl_kind,
  98. " in a non-abstract `class` definition",
  99. context.insts().GetLocId(enclosing_scope_inst_id));
  100. }
  101. return;
  102. }
  103. }
  104. ForbidModifiersOnDecl(context, KeywordModifierSet::Method, decl_kind,
  105. " outside of a class");
  106. }
  107. auto RestrictExternModifierOnDecl(
  108. Context& context, Lex::TokenKind decl_kind,
  109. std::optional<SemIR::Inst> enclosing_scope_inst, bool is_definition)
  110. -> void {
  111. if (is_definition) {
  112. ForbidModifiersOnDecl(context, KeywordModifierSet::Extern, decl_kind,
  113. " that provides a definition");
  114. }
  115. if (enclosing_scope_inst && !enclosing_scope_inst->Is<SemIR::Namespace>()) {
  116. ForbidModifiersOnDecl(context, KeywordModifierSet::Extern, decl_kind,
  117. " that is a member");
  118. }
  119. }
  120. auto RequireDefaultFinalOnlyInInterfaces(
  121. Context& context, Lex::TokenKind decl_kind,
  122. std::optional<SemIR::Inst> enclosing_scope_inst) -> void {
  123. if (enclosing_scope_inst &&
  124. enclosing_scope_inst->Is<SemIR::InterfaceDecl>()) {
  125. // Both `default` and `final` allowed in an interface definition.
  126. return;
  127. }
  128. ForbidModifiersOnDecl(context, KeywordModifierSet::Interface, decl_kind,
  129. " outside of an interface");
  130. }
  131. } // namespace Carbon::Check