modifiers.cpp 9.6 KB

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