modifiers.cpp 9.7 KB

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