modifiers.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <optional>
  6. #include "toolchain/check/decl_introducer_state.h"
  7. namespace Carbon::Check {
  8. // Builds the diagnostic for DiagnoseNotAllowed.
  9. template <typename... TokenKinds>
  10. static auto StartDiagnoseNotAllowed(
  11. Context& context,
  12. const Diagnostics::DiagnosticBase<TokenKinds...>& diagnostic_base,
  13. Parse::NodeId modifier_node, Lex::TokenKind declaration_kind)
  14. -> DiagnosticBuilder {
  15. if constexpr (sizeof...(TokenKinds) == 0) {
  16. return context.emitter().Build(modifier_node, diagnostic_base);
  17. } else if constexpr (sizeof...(TokenKinds) == 1) {
  18. return context.emitter().Build(modifier_node, diagnostic_base,
  19. context.token_kind(modifier_node));
  20. } else {
  21. static_assert(sizeof...(TokenKinds) == 2);
  22. return context.emitter().Build(modifier_node, diagnostic_base,
  23. context.token_kind(modifier_node),
  24. declaration_kind);
  25. }
  26. }
  27. // Diagnoses that a modifier wasn't allowed. Handles adding context when
  28. // possible.
  29. //
  30. // The diagnostic can take up to two TokenKinds: the modifier kind, and the
  31. // declaration kind.
  32. template <typename... TokenKinds>
  33. static auto DiagnoseNotAllowed(
  34. Context& context,
  35. const Diagnostics::DiagnosticBase<TokenKinds...>& diagnostic_base,
  36. Parse::NodeId modifier_node, Lex::TokenKind decl_kind,
  37. SemIR::LocId context_loc_id) -> void {
  38. auto diag = StartDiagnoseNotAllowed(context, diagnostic_base, modifier_node,
  39. decl_kind);
  40. if (context_loc_id.has_value()) {
  41. CARBON_DIAGNOSTIC(ModifierNotInContext, Note, "containing definition here");
  42. diag.Note(context_loc_id, ModifierNotInContext);
  43. }
  44. diag.Emit();
  45. }
  46. // Returns the KeywordModifierSet corresponding to the ModifierOrder entry.
  47. static auto ModifierOrderAsSet(ModifierOrder order) -> KeywordModifierSet {
  48. switch (order) {
  49. case ModifierOrder::Access:
  50. return KeywordModifierSet::Access;
  51. case ModifierOrder::Extern:
  52. return KeywordModifierSet::Extern;
  53. case ModifierOrder::Decl:
  54. return KeywordModifierSet::Decl;
  55. }
  56. }
  57. // Like `LimitModifiersOnDecl`, except says which modifiers are forbidden, and a
  58. // `context_string` (and optional `context_loc_id`) specifying the context in
  59. // which those modifiers are forbidden.
  60. //
  61. // See DiagnoseNotAllowed for details regarding diagnostic_base.
  62. template <typename DiagnosticBaseT>
  63. static auto ForbidModifiersOnDecl(
  64. Context& context, const DiagnosticBaseT& diagnostic_base,
  65. DeclIntroducerState& introducer, KeywordModifierSet forbidden,
  66. SemIR::LocId context_loc_id = SemIR::LocId::None) -> void {
  67. auto not_allowed = introducer.modifier_set & forbidden;
  68. if (not_allowed.empty()) {
  69. return;
  70. }
  71. for (auto order_index = 0;
  72. order_index <= static_cast<int8_t>(ModifierOrder::Last); ++order_index) {
  73. auto order = static_cast<ModifierOrder>(order_index);
  74. if (not_allowed.HasAnyOf(ModifierOrderAsSet(order))) {
  75. DiagnoseNotAllowed(context, diagnostic_base,
  76. introducer.modifier_node_id(order), introducer.kind,
  77. context_loc_id);
  78. introducer.set_modifier_node_id(order, Parse::NodeId::None);
  79. }
  80. }
  81. introducer.modifier_set.Remove(forbidden);
  82. }
  83. auto LimitModifiersOnDecl(Context& context, DeclIntroducerState& introducer,
  84. KeywordModifierSet allowed) -> void {
  85. CARBON_DIAGNOSTIC(ModifierNotAllowedOnDeclaration, Error,
  86. "`{0}` not allowed on `{1}` declaration", Lex::TokenKind,
  87. Lex::TokenKind);
  88. ForbidModifiersOnDecl(context, ModifierNotAllowedOnDeclaration, introducer,
  89. ~allowed);
  90. }
  91. auto LimitModifiersOnNotDefinition(Context& context,
  92. DeclIntroducerState& introducer,
  93. KeywordModifierSet allowed) -> void {
  94. CARBON_DIAGNOSTIC(
  95. ModifierOnlyAllowedOnDefinition, Error,
  96. "`{0}` not allowed on `{1}` forward declaration, only definition",
  97. Lex::TokenKind, Lex::TokenKind);
  98. ForbidModifiersOnDecl(context, ModifierOnlyAllowedOnDefinition, introducer,
  99. ~allowed);
  100. }
  101. auto CheckAccessModifiersOnDecl(Context& context,
  102. DeclIntroducerState& introducer,
  103. std::optional<SemIR::Inst> parent_scope_inst)
  104. -> void {
  105. CARBON_DIAGNOSTIC(ModifierProtectedNotAllowed, Error,
  106. "`protected` not allowed; requires class scope");
  107. if (parent_scope_inst) {
  108. if (parent_scope_inst->Is<SemIR::Namespace>()) {
  109. // TODO: This assumes that namespaces can only be declared at file scope.
  110. // If we add support for non-file-scope namespaces, we will need to check
  111. // the parents of the target scope to determine whether we're at file
  112. // scope.
  113. ForbidModifiersOnDecl(context, ModifierProtectedNotAllowed, introducer,
  114. KeywordModifierSet::Protected);
  115. return;
  116. }
  117. if (parent_scope_inst->Is<SemIR::ClassDecl>()) {
  118. // Both `private` and `protected` allowed in a class definition.
  119. return;
  120. }
  121. }
  122. // Otherwise neither `private` nor `protected` allowed.
  123. ForbidModifiersOnDecl(context, ModifierProtectedNotAllowed, introducer,
  124. KeywordModifierSet::Protected);
  125. CARBON_DIAGNOSTIC(ModifierPrivateNotAllowed, Error,
  126. "`private` not allowed; requires class or file scope");
  127. ForbidModifiersOnDecl(context, ModifierPrivateNotAllowed, introducer,
  128. KeywordModifierSet::Private);
  129. }
  130. auto CheckMethodModifiersOnFunction(
  131. Context& context, DeclIntroducerState& introducer,
  132. SemIR::InstId parent_scope_inst_id,
  133. std::optional<SemIR::Inst> parent_scope_inst) -> void {
  134. if (parent_scope_inst) {
  135. if (auto class_decl = parent_scope_inst->TryAs<SemIR::ClassDecl>()) {
  136. auto inheritance_kind =
  137. context.classes().Get(class_decl->class_id).inheritance_kind;
  138. if (inheritance_kind == SemIR::Class::Final) {
  139. CARBON_DIAGNOSTIC(
  140. ModifierVirtualNotAllowed, Error,
  141. "`virtual` not allowed; requires `abstract` or `base` class scope");
  142. ForbidModifiersOnDecl(context, ModifierVirtualNotAllowed, introducer,
  143. KeywordModifierSet::Virtual,
  144. context.insts().GetLocId(parent_scope_inst_id));
  145. }
  146. if (inheritance_kind != SemIR::Class::Abstract) {
  147. CARBON_DIAGNOSTIC(
  148. ModifierAbstractNotAllowed, Error,
  149. "`abstract` not allowed; requires `abstract` class scope");
  150. ForbidModifiersOnDecl(context, ModifierAbstractNotAllowed, introducer,
  151. KeywordModifierSet::Abstract,
  152. context.insts().GetLocId(parent_scope_inst_id));
  153. }
  154. return;
  155. }
  156. }
  157. CARBON_DIAGNOSTIC(ModifierRequiresClass, Error,
  158. "`{0}` not allowed; requires class scope", Lex::TokenKind);
  159. ForbidModifiersOnDecl(context, ModifierRequiresClass, introducer,
  160. KeywordModifierSet::Method);
  161. }
  162. auto RestrictExternModifierOnDecl(Context& context,
  163. DeclIntroducerState& introducer,
  164. std::optional<SemIR::Inst> parent_scope_inst,
  165. bool is_definition) -> void {
  166. if (!introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extern)) {
  167. return;
  168. }
  169. if (parent_scope_inst && !parent_scope_inst->Is<SemIR::Namespace>()) {
  170. CARBON_DIAGNOSTIC(ModifierExternNotAllowed, Error,
  171. "`{0}` not allowed; requires file or namespace scope",
  172. Lex::TokenKind);
  173. ForbidModifiersOnDecl(context, ModifierExternNotAllowed, introducer,
  174. KeywordModifierSet::Extern);
  175. // Treat as unset.
  176. introducer.extern_library = SemIR::LibraryNameId::None;
  177. return;
  178. }
  179. if (introducer.extern_library == context.sem_ir().library_id()) {
  180. // This prints an error for `extern library`, but doesn't drop it because we
  181. // assume there is some other, correct value that we just don't know here.
  182. CARBON_DIAGNOSTIC(ExternLibraryIsCurrentLibrary, Error,
  183. "`extern library` cannot specify the current library");
  184. context.emitter().Emit(introducer.modifier_node_id(ModifierOrder::Extern),
  185. ExternLibraryIsCurrentLibrary);
  186. introducer.extern_library = SemIR::LibraryNameId::Error;
  187. // Right now this can produce both this and the below diagnostic.
  188. }
  189. if (is_definition && introducer.extern_library.has_value()) {
  190. CARBON_DIAGNOSTIC(ExternLibraryOnDefinition, Error,
  191. "a library cannot be provided for an `extern` modifier "
  192. "on a definition");
  193. context.emitter().Emit(introducer.modifier_node_id(ModifierOrder::Extern),
  194. ExternLibraryOnDefinition);
  195. }
  196. }
  197. auto RequireDefaultFinalOnlyInInterfaces(
  198. Context& context, DeclIntroducerState& introducer,
  199. std::optional<SemIR::Inst> parent_scope_inst) -> void {
  200. if (parent_scope_inst && parent_scope_inst->Is<SemIR::InterfaceDecl>()) {
  201. // Both `default` and `final` allowed in an interface definition.
  202. return;
  203. }
  204. CARBON_DIAGNOSTIC(ModifierRequiresInterface, Error,
  205. "`{0}` not allowed; requires interface scope",
  206. Lex::TokenKind);
  207. ForbidModifiersOnDecl(context, ModifierRequiresInterface, introducer,
  208. KeywordModifierSet::Interface);
  209. }
  210. } // namespace Carbon::Check