modifiers.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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}", Lex::TokenKind,
  13. 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, "containing definition here");
  19. diag.Note(context_loc_id, ModifierNotInContext);
  20. }
  21. diag.Emit();
  22. }
  23. // Returns the KeywordModifierSet corresponding to the ModifierOrder entry.
  24. static auto ModifierOrderAsSet(ModifierOrder order) -> KeywordModifierSet {
  25. switch (order) {
  26. case ModifierOrder::Access:
  27. return KeywordModifierSet::Access;
  28. case ModifierOrder::Extern:
  29. return KeywordModifierSet::Extern;
  30. case ModifierOrder::Decl:
  31. return KeywordModifierSet::Decl;
  32. }
  33. }
  34. auto ForbidModifiersOnDecl(Context& context, DeclIntroducerState& introducer,
  35. KeywordModifierSet forbidden,
  36. llvm::StringRef context_string,
  37. SemIR::LocId context_loc_id) -> void {
  38. auto not_allowed = introducer.modifier_set & forbidden;
  39. if (not_allowed.empty()) {
  40. return;
  41. }
  42. for (auto order_index = 0;
  43. order_index <= static_cast<int8_t>(ModifierOrder::Last); ++order_index) {
  44. auto order = static_cast<ModifierOrder>(order_index);
  45. if (not_allowed.HasAnyOf(ModifierOrderAsSet(order))) {
  46. DiagnoseNotAllowed(context, introducer.modifier_node_id(order),
  47. introducer.kind, context_string, context_loc_id);
  48. introducer.set_modifier_node_id(order, Parse::NodeId::Invalid);
  49. }
  50. }
  51. introducer.modifier_set.Remove(forbidden);
  52. }
  53. auto CheckAccessModifiersOnDecl(Context& context,
  54. DeclIntroducerState& introducer,
  55. std::optional<SemIR::Inst> parent_scope_inst)
  56. -> void {
  57. if (parent_scope_inst) {
  58. if (parent_scope_inst->Is<SemIR::Namespace>()) {
  59. // TODO: This assumes that namespaces can only be declared at file scope.
  60. // If we add support for non-file-scope namespaces, we will need to check
  61. // the parents of the target scope to determine whether we're at file
  62. // scope.
  63. ForbidModifiersOnDecl(
  64. context, introducer, KeywordModifierSet::Protected,
  65. " at file scope, `protected` is only allowed on class members");
  66. return;
  67. }
  68. if (parent_scope_inst->Is<SemIR::ClassDecl>()) {
  69. // Both `private` and `protected` allowed in a class definition.
  70. return;
  71. }
  72. }
  73. // Otherwise neither `private` nor `protected` allowed.
  74. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Protected,
  75. ", `protected` is only allowed on class members");
  76. ForbidModifiersOnDecl(
  77. context, introducer, KeywordModifierSet::Private,
  78. ", `private` is only allowed on class members and at file scope");
  79. }
  80. auto CheckMethodModifiersOnFunction(
  81. Context& context, DeclIntroducerState& introducer,
  82. SemIR::InstId parent_scope_inst_id,
  83. std::optional<SemIR::Inst> parent_scope_inst) -> void {
  84. if (parent_scope_inst) {
  85. if (auto class_decl = parent_scope_inst->TryAs<SemIR::ClassDecl>()) {
  86. auto inheritance_kind =
  87. context.classes().Get(class_decl->class_id).inheritance_kind;
  88. if (inheritance_kind == SemIR::Class::Final) {
  89. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Virtual,
  90. " in a non-abstract non-base `class` definition",
  91. context.insts().GetLocId(parent_scope_inst_id));
  92. }
  93. if (inheritance_kind != SemIR::Class::Abstract) {
  94. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Abstract,
  95. " in a non-abstract `class` definition",
  96. context.insts().GetLocId(parent_scope_inst_id));
  97. }
  98. return;
  99. }
  100. }
  101. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Method,
  102. " outside of a class");
  103. }
  104. auto RestrictExternModifierOnDecl(Context& context,
  105. DeclIntroducerState& introducer,
  106. std::optional<SemIR::Inst> parent_scope_inst,
  107. bool is_definition) -> void {
  108. if (!introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extern)) {
  109. return;
  110. }
  111. if (parent_scope_inst && !parent_scope_inst->Is<SemIR::Namespace>()) {
  112. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Extern,
  113. " that is a member");
  114. // Treat as unset.
  115. introducer.extern_library = SemIR::LibraryNameId::Invalid;
  116. return;
  117. }
  118. if (introducer.extern_library == context.sem_ir().library_id()) {
  119. // This prints an error for `extern library`, but doesn't drop it because we
  120. // assume there is some other, correct value that we just don't know here.
  121. CARBON_DIAGNOSTIC(ExternLibraryIsCurrentLibrary, Error,
  122. "`extern library` cannot specify the current library");
  123. context.emitter().Emit(introducer.modifier_node_id(ModifierOrder::Extern),
  124. ExternLibraryIsCurrentLibrary);
  125. introducer.extern_library = SemIR::LibraryNameId::Error;
  126. // Right now this can produce both this and the below diagnostic.
  127. }
  128. if (is_definition && introducer.extern_library.is_valid()) {
  129. CARBON_DIAGNOSTIC(ExternLibraryOnDefinition, Error,
  130. "a library cannot be provided for an `extern` modifier "
  131. "on a definition");
  132. context.emitter().Emit(introducer.modifier_node_id(ModifierOrder::Extern),
  133. ExternLibraryOnDefinition);
  134. }
  135. }
  136. auto RequireDefaultFinalOnlyInInterfaces(
  137. Context& context, DeclIntroducerState& introducer,
  138. std::optional<SemIR::Inst> parent_scope_inst) -> void {
  139. if (parent_scope_inst && parent_scope_inst->Is<SemIR::InterfaceDecl>()) {
  140. // Both `default` and `final` allowed in an interface definition.
  141. return;
  142. }
  143. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Interface,
  144. " outside of an interface");
  145. }
  146. } // namespace Carbon::Check