modifiers.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. static auto DiagnoseNotAllowed(Context& context, Parse::NodeId modifier_node,
  8. Lex::TokenKind decl_kind,
  9. llvm::StringRef context_string,
  10. SemIR::LocId context_loc_id) -> void {
  11. CARBON_DIAGNOSTIC(ModifierNotAllowedOn, Error,
  12. "`{0}` not allowed on `{1}` declaration{2}.",
  13. Lex::TokenKind, Lex::TokenKind, std::string);
  14. auto diag = context.emitter().Build(modifier_node, ModifierNotAllowedOn,
  15. context.token_kind(modifier_node),
  16. decl_kind, context_string.str());
  17. if (context_loc_id.is_valid()) {
  18. CARBON_DIAGNOSTIC(ModifierNotInContext, Note,
  19. "Containing definition here.");
  20. diag.Note(context_loc_id, ModifierNotInContext);
  21. }
  22. diag.Emit();
  23. }
  24. // Returns the KeywordModifierSet corresponding to the ModifierOrder entry.
  25. static auto ModifierOrderAsSet(ModifierOrder order) -> KeywordModifierSet {
  26. switch (order) {
  27. case ModifierOrder::Access:
  28. return KeywordModifierSet::Access;
  29. case ModifierOrder::Extern:
  30. return KeywordModifierSet::Extern;
  31. case ModifierOrder::Decl:
  32. return KeywordModifierSet::Decl;
  33. }
  34. }
  35. auto ForbidModifiersOnDecl(Context& context, DeclIntroducerState& introducer,
  36. KeywordModifierSet forbidden,
  37. llvm::StringRef context_string,
  38. SemIR::LocId context_loc_id) -> void {
  39. auto not_allowed = introducer.modifier_set & forbidden;
  40. if (not_allowed.empty()) {
  41. return;
  42. }
  43. for (auto order_index = 0;
  44. order_index <= static_cast<int8_t>(ModifierOrder::Last); ++order_index) {
  45. auto order = static_cast<ModifierOrder>(order_index);
  46. if (not_allowed.HasAnyOf(ModifierOrderAsSet(order))) {
  47. DiagnoseNotAllowed(context, introducer.modifier_node_id(order),
  48. introducer.kind, context_string, context_loc_id);
  49. introducer.set_modifier_node_id(order, Parse::NodeId::Invalid);
  50. }
  51. }
  52. introducer.modifier_set.Remove(forbidden);
  53. }
  54. auto CheckAccessModifiersOnDecl(Context& context,
  55. DeclIntroducerState& introducer,
  56. std::optional<SemIR::Inst> parent_scope_inst)
  57. -> void {
  58. if (parent_scope_inst) {
  59. if (parent_scope_inst->Is<SemIR::Namespace>()) {
  60. // TODO: This assumes that namespaces can only be declared at file scope.
  61. // If we add support for non-file-scope namespaces, we will need to check
  62. // the parents of the target scope to determine whether we're at file
  63. // scope.
  64. ForbidModifiersOnDecl(
  65. context, introducer, KeywordModifierSet::Protected,
  66. " at file scope, `protected` is only allowed on class members");
  67. return;
  68. }
  69. if (parent_scope_inst->Is<SemIR::ClassDecl>()) {
  70. // Both `private` and `protected` allowed in a class definition.
  71. return;
  72. }
  73. }
  74. // Otherwise neither `private` nor `protected` allowed.
  75. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Protected,
  76. ", `protected` is only allowed on class members");
  77. ForbidModifiersOnDecl(
  78. context, introducer, KeywordModifierSet::Private,
  79. ", `private` is only allowed on class members and at file scope");
  80. }
  81. auto CheckMethodModifiersOnFunction(
  82. Context& context, DeclIntroducerState& introducer,
  83. SemIR::InstId parent_scope_inst_id,
  84. std::optional<SemIR::Inst> parent_scope_inst) -> void {
  85. if (parent_scope_inst) {
  86. if (auto class_decl = parent_scope_inst->TryAs<SemIR::ClassDecl>()) {
  87. auto inheritance_kind =
  88. context.classes().Get(class_decl->class_id).inheritance_kind;
  89. if (inheritance_kind == SemIR::Class::Final) {
  90. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Virtual,
  91. " in a non-abstract non-base `class` definition",
  92. context.insts().GetLocId(parent_scope_inst_id));
  93. }
  94. if (inheritance_kind != SemIR::Class::Abstract) {
  95. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Abstract,
  96. " in a non-abstract `class` definition",
  97. context.insts().GetLocId(parent_scope_inst_id));
  98. }
  99. return;
  100. }
  101. }
  102. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Method,
  103. " outside of a class");
  104. }
  105. auto RestrictExternModifierOnDecl(Context& context,
  106. DeclIntroducerState& introducer,
  107. std::optional<SemIR::Inst> parent_scope_inst,
  108. bool is_definition) -> void {
  109. if (is_definition) {
  110. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Extern,
  111. " that provides a definition");
  112. }
  113. if (parent_scope_inst && !parent_scope_inst->Is<SemIR::Namespace>()) {
  114. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Extern,
  115. " that is a member");
  116. }
  117. }
  118. auto RequireDefaultFinalOnlyInInterfaces(
  119. Context& context, DeclIntroducerState& introducer,
  120. std::optional<SemIR::Inst> parent_scope_inst) -> void {
  121. if (parent_scope_inst && parent_scope_inst->Is<SemIR::InterfaceDecl>()) {
  122. // Both `default` and `final` allowed in an interface definition.
  123. return;
  124. }
  125. ForbidModifiersOnDecl(context, introducer, KeywordModifierSet::Interface,
  126. " outside of an interface");
  127. }
  128. } // namespace Carbon::Check