handle_modifier.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/context.h"
  5. #include "toolchain/check/decl_introducer_state.h"
  6. #include "toolchain/check/handle.h"
  7. #include "toolchain/lex/token_kind.h"
  8. namespace Carbon::Check {
  9. CARBON_DIAGNOSTIC(ModifierPrevious, Note, "`{0}` previously appeared here",
  10. Lex::TokenKind);
  11. static auto DiagnoseRepeated(Context& context, Parse::NodeId first_node,
  12. Parse::NodeId second_node) -> void {
  13. CARBON_DIAGNOSTIC(ModifierRepeated, Error, "`{0}` repeated on declaration",
  14. Lex::TokenKind);
  15. context.emitter()
  16. .Build(second_node, ModifierRepeated, context.token_kind(second_node))
  17. .Note(first_node, ModifierPrevious, context.token_kind(first_node))
  18. .Emit();
  19. }
  20. static auto DiagnoseNotAllowedWith(Context& context, Parse::NodeId first_node,
  21. Parse::NodeId second_node) -> void {
  22. CARBON_DIAGNOSTIC(ModifierNotAllowedWith, Error,
  23. "`{0}` not allowed on declaration with `{1}`",
  24. Lex::TokenKind, Lex::TokenKind);
  25. context.emitter()
  26. .Build(second_node, ModifierNotAllowedWith,
  27. context.token_kind(second_node), context.token_kind(first_node))
  28. .Note(first_node, ModifierPrevious, context.token_kind(first_node))
  29. .Emit();
  30. }
  31. // Handles the keyword that starts a modifier. This may a standalone keyword,
  32. // such as `private`, or the first in a complex modifier, such as `extern` in
  33. // `extern library ...`. If valid, adds it to the modifier set and returns true.
  34. // Otherwise, diagnoses and returns false.
  35. static auto HandleModifier(Context& context, Parse::NodeId node_id,
  36. KeywordModifierSet keyword) -> bool {
  37. auto& s = context.decl_introducer_state_stack().innermost();
  38. ModifierOrder order;
  39. KeywordModifierSet later_modifiers;
  40. if (keyword.HasAnyOf(KeywordModifierSet::Access)) {
  41. order = ModifierOrder::Access;
  42. later_modifiers = KeywordModifierSet::Extern | KeywordModifierSet::Decl |
  43. KeywordModifierSet::Evaluation;
  44. } else if (keyword.HasAnyOf(KeywordModifierSet::Extern)) {
  45. order = ModifierOrder::Extern;
  46. later_modifiers = KeywordModifierSet::Decl | KeywordModifierSet::Evaluation;
  47. } else if (keyword.HasAnyOf(KeywordModifierSet::Extend)) {
  48. order = ModifierOrder::Extend;
  49. later_modifiers = KeywordModifierSet::Decl;
  50. } else if (keyword.HasAnyOf(KeywordModifierSet::Decl)) {
  51. order = ModifierOrder::Decl;
  52. later_modifiers = KeywordModifierSet::Evaluation;
  53. } else if (keyword.HasAnyOf(KeywordModifierSet::Evaluation)) {
  54. order = ModifierOrder::Evaluation;
  55. later_modifiers = KeywordModifierSet::None;
  56. } else {
  57. CARBON_FATAL("Unexpected modifier keyword.");
  58. }
  59. auto current_modifier_node_id = s.modifier_node_id(order);
  60. if (s.modifier_set.HasAnyOf(keyword)) {
  61. DiagnoseRepeated(context, current_modifier_node_id, node_id);
  62. return false;
  63. }
  64. if (current_modifier_node_id.has_value()) {
  65. DiagnoseNotAllowedWith(context, current_modifier_node_id, node_id);
  66. return false;
  67. }
  68. if (auto later_modifier_set = s.modifier_set & later_modifiers;
  69. !later_modifier_set.empty()) {
  70. // At least one later modifier is present. Diagnose using the closest.
  71. Parse::NodeId closest_later_modifier = Parse::NodeId::None;
  72. for (auto later_order = static_cast<int8_t>(order) + 1;
  73. later_order <= static_cast<int8_t>(ModifierOrder::Last);
  74. ++later_order) {
  75. if (s.ordered_modifier_node_ids[later_order].has_value()) {
  76. closest_later_modifier = s.ordered_modifier_node_ids[later_order];
  77. break;
  78. }
  79. }
  80. CARBON_CHECK(closest_later_modifier.has_value());
  81. CARBON_DIAGNOSTIC(ModifierMustAppearBefore, Error,
  82. "`{0}` must appear before `{1}`", Lex::TokenKind,
  83. Lex::TokenKind);
  84. context.emitter()
  85. .Build(node_id, ModifierMustAppearBefore, context.token_kind(node_id),
  86. context.token_kind(closest_later_modifier))
  87. .Note(closest_later_modifier, ModifierPrevious,
  88. context.token_kind(closest_later_modifier))
  89. .Emit();
  90. return false;
  91. }
  92. s.modifier_set.Add(keyword);
  93. s.set_modifier_node_id(order, node_id);
  94. return true;
  95. }
  96. #define CARBON_PARSE_NODE_KIND(Name)
  97. #define CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name) \
  98. auto HandleParseNode(Context& context, Parse::Name##ModifierId node_id) \
  99. -> bool { \
  100. HandleModifier(context, node_id, KeywordModifierSet::Name); \
  101. return true; \
  102. }
  103. #include "toolchain/parse/node_kind.def"
  104. auto HandleParseNode(Context& context,
  105. Parse::ExternModifierWithLibraryId node_id) -> bool {
  106. auto name_literal_id = context.node_stack().Pop<SemIR::LibraryNameId>();
  107. if (HandleModifier(context, node_id, KeywordModifierSet::Extern)) {
  108. auto& s = context.decl_introducer_state_stack().innermost();
  109. s.extern_library = name_literal_id;
  110. }
  111. return true;
  112. }
  113. auto HandleParseNode(Context& context, Parse::ExternModifierId node_id)
  114. -> bool {
  115. return HandleModifier(context, node_id, KeywordModifierSet::Extern);
  116. }
  117. auto HandleParseNode(Context& context, Parse::ReturnedModifierId node_id)
  118. -> bool {
  119. return HandleModifier(context, node_id, KeywordModifierSet::Returned);
  120. }
  121. } // namespace Carbon::Check