modifiers.cpp 6.3 KB

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