modifiers.cpp 9.9 KB

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