modifiers.cpp 9.6 KB

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