modifiers.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_introducer_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, DeclIntroducerState& introducer,
  36. KeywordModifierSet forbidden,
  37. Lex::TokenKind decl_kind,
  38. llvm::StringRef context_string,
  39. SemIR::LocId context_loc_id) -> void {
  40. auto not_allowed = introducer.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, introducer.modifier_node_id(order), decl_kind,
  49. context_string, context_loc_id);
  50. introducer.set_modifier_node_id(order, Parse::NodeId::Invalid);
  51. }
  52. }
  53. introducer.modifier_set.Remove(forbidden);
  54. }
  55. auto CheckAccessModifiersOnDecl(Context& context,
  56. DeclIntroducerState& introducer,
  57. Lex::TokenKind decl_kind,
  58. std::optional<SemIR::Inst> parent_scope_inst)
  59. -> void {
  60. if (parent_scope_inst) {
  61. if (parent_scope_inst->Is<SemIR::Namespace>()) {
  62. // TODO: This assumes that namespaces can only be declared at file scope.
  63. // If we add support for non-file-scope namespaces, we will need to check
  64. // the parents of the target scope to determine whether we're at file
  65. // scope.
  66. ForbidModifiersOnDecl(
  67. context, introducer, KeywordModifierSet::Protected, decl_kind,
  68. " at file scope, `protected` is only allowed on class members");
  69. return;
  70. }
  71. if (parent_scope_inst->Is<SemIR::ClassDecl>()) {
  72. // Both `private` and `protected` allowed in a class definition.
  73. return;
  74. }
  75. }
  76. // Otherwise neither `private` nor `protected` allowed.
  77. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Protected,
  78. decl_kind,
  79. ", `protected` is only allowed on class members");
  80. ForbidModifiersOnDecl(
  81. context, introducer, KeywordModifierSet::Private, decl_kind,
  82. ", `private` is only allowed on class members and at file scope");
  83. }
  84. auto CheckMethodModifiersOnFunction(
  85. Context& context, DeclIntroducerState& introducer,
  86. SemIR::InstId parent_scope_inst_id,
  87. std::optional<SemIR::Inst> parent_scope_inst) -> void {
  88. const Lex::TokenKind decl_kind = Lex::TokenKind::Fn;
  89. if (parent_scope_inst) {
  90. if (auto class_decl = parent_scope_inst->TryAs<SemIR::ClassDecl>()) {
  91. auto inheritance_kind =
  92. context.classes().Get(class_decl->class_id).inheritance_kind;
  93. if (inheritance_kind == SemIR::Class::Final) {
  94. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Virtual,
  95. decl_kind,
  96. " in a non-abstract non-base `class` definition",
  97. context.insts().GetLocId(parent_scope_inst_id));
  98. }
  99. if (inheritance_kind != SemIR::Class::Abstract) {
  100. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Abstract,
  101. decl_kind,
  102. " in a non-abstract `class` definition",
  103. context.insts().GetLocId(parent_scope_inst_id));
  104. }
  105. return;
  106. }
  107. }
  108. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Method,
  109. decl_kind, " outside of a class");
  110. }
  111. auto RestrictExternModifierOnDecl(Context& context,
  112. DeclIntroducerState& introducer,
  113. Lex::TokenKind decl_kind,
  114. std::optional<SemIR::Inst> parent_scope_inst,
  115. bool is_definition) -> void {
  116. if (is_definition) {
  117. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Extern,
  118. decl_kind, " that provides a definition");
  119. }
  120. if (parent_scope_inst && !parent_scope_inst->Is<SemIR::Namespace>()) {
  121. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Extern,
  122. decl_kind, " that is a member");
  123. }
  124. }
  125. auto RequireDefaultFinalOnlyInInterfaces(
  126. Context& context, DeclIntroducerState& introducer, Lex::TokenKind decl_kind,
  127. std::optional<SemIR::Inst> parent_scope_inst) -> void {
  128. if (parent_scope_inst && parent_scope_inst->Is<SemIR::InterfaceDecl>()) {
  129. // Both `default` and `final` allowed in an interface definition.
  130. return;
  131. }
  132. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Interface,
  133. decl_kind, " outside of an interface");
  134. }
  135. } // namespace Carbon::Check