modifiers.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_state.h"
  6. namespace Carbon::Check {
  7. static auto ReportNotAllowed(Context& context, Parse::NodeId modifier_node,
  8. Lex::TokenKind decl_kind,
  9. llvm::StringRef context_string,
  10. Parse::NodeId context_node) -> 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_node.is_valid()) {
  18. CARBON_DIAGNOSTIC(ModifierNotInContext, Note,
  19. "Containing definition here.");
  20. diag.Note(context_node, 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, KeywordModifierSet forbidden,
  36. Lex::TokenKind decl_kind,
  37. llvm::StringRef context_string,
  38. Parse::NodeId context_node) -> void {
  39. auto& s = context.decl_state_stack().innermost();
  40. auto not_allowed = s.modifier_set & forbidden;
  41. if (!not_allowed) {
  42. return;
  43. }
  44. for (auto order_index = 0;
  45. order_index <= static_cast<int8_t>(ModifierOrder::Last); ++order_index) {
  46. auto order = static_cast<ModifierOrder>(order_index);
  47. if (!!(not_allowed & ModifierOrderAsSet(order))) {
  48. ReportNotAllowed(context, s.modifier_node_id(order), decl_kind,
  49. context_string, context_node);
  50. s.set_modifier_node_id(order, Parse::NodeId::Invalid);
  51. }
  52. }
  53. s.modifier_set &= ~forbidden;
  54. }
  55. // Returns the instruction that owns the given scope, or Invalid if the scope is
  56. // not associated with an instruction.
  57. static auto GetScopeInstId(Context& context, SemIR::NameScopeId scope_id)
  58. -> SemIR::InstId {
  59. if (!scope_id.is_valid()) {
  60. return SemIR::InstId::Invalid;
  61. }
  62. return context.name_scopes().Get(scope_id).inst_id;
  63. }
  64. // Returns the instruction that owns the given scope, or Invalid if the scope is
  65. // not associated with an instruction.
  66. static auto GetScopeInst(Context& context, SemIR::NameScopeId scope_id)
  67. -> std::optional<SemIR::Inst> {
  68. auto inst_id = GetScopeInstId(context, scope_id);
  69. if (!inst_id.is_valid()) {
  70. return std::nullopt;
  71. }
  72. return context.insts().Get(inst_id);
  73. }
  74. auto CheckAccessModifiersOnDecl(Context& context, Lex::TokenKind decl_kind,
  75. SemIR::NameScopeId target_scope_id) -> void {
  76. auto target = GetScopeInst(context, target_scope_id);
  77. if (target && target->Is<SemIR::Namespace>()) {
  78. // TODO: This assumes that namespaces can only be declared at file scope. If
  79. // we add support for non-file-scope namespaces, we will need to check the
  80. // parents of the target scope to determine whether we're at file scope.
  81. ForbidModifiersOnDecl(
  82. context, KeywordModifierSet::Protected, decl_kind,
  83. " at file scope, `protected` is only allowed on class members");
  84. return;
  85. }
  86. if (target && target->Is<SemIR::ClassDecl>()) {
  87. // Both `private` and `protected` allowed in a class definition.
  88. return;
  89. }
  90. // Otherwise neither `private` nor `protected` allowed.
  91. ForbidModifiersOnDecl(context, KeywordModifierSet::Protected, decl_kind,
  92. ", `protected` is only allowed on class members");
  93. ForbidModifiersOnDecl(
  94. context, KeywordModifierSet::Private, decl_kind,
  95. ", `private` is only allowed on class members and at file scope");
  96. }
  97. auto CheckMethodModifiersOnFunction(Context& context,
  98. SemIR::NameScopeId target_scope_id)
  99. -> void {
  100. const Lex::TokenKind decl_kind = Lex::TokenKind::Fn;
  101. auto target_id = GetScopeInstId(context, target_scope_id);
  102. if (target_id.is_valid()) {
  103. if (auto class_decl =
  104. context.insts().TryGetAs<SemIR::ClassDecl>(target_id)) {
  105. auto inheritance_kind =
  106. context.classes().Get(class_decl->class_id).inheritance_kind;
  107. if (inheritance_kind == SemIR::Class::Final) {
  108. ForbidModifiersOnDecl(context, KeywordModifierSet::Virtual, decl_kind,
  109. " in a non-abstract non-base `class` definition",
  110. context.insts().GetNodeId(target_id));
  111. }
  112. if (inheritance_kind != SemIR::Class::Abstract) {
  113. ForbidModifiersOnDecl(context, KeywordModifierSet::Abstract, decl_kind,
  114. " in a non-abstract `class` definition",
  115. context.insts().GetNodeId(target_id));
  116. }
  117. return;
  118. }
  119. }
  120. ForbidModifiersOnDecl(context, KeywordModifierSet::Method, decl_kind,
  121. " outside of a class");
  122. }
  123. auto RequireDefaultFinalOnlyInInterfaces(Context& context,
  124. Lex::TokenKind decl_kind,
  125. SemIR::NameScopeId target_scope_id)
  126. -> void {
  127. auto target = GetScopeInst(context, target_scope_id);
  128. if (target && target->Is<SemIR::InterfaceDecl>()) {
  129. // Both `default` and `final` allowed in an interface definition.
  130. return;
  131. }
  132. ForbidModifiersOnDecl(context, KeywordModifierSet::Interface, decl_kind,
  133. " outside of an interface");
  134. }
  135. } // namespace Carbon::Check