handle_modifier.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. } else if (keyword.HasAnyOf(KeywordModifierSet::Extern)) {
  44. order = ModifierOrder::Extern;
  45. later_modifiers = KeywordModifierSet::Decl;
  46. } else if (keyword.HasAnyOf(KeywordModifierSet::Extend)) {
  47. order = ModifierOrder::Extend;
  48. later_modifiers = KeywordModifierSet::Decl;
  49. } else {
  50. order = ModifierOrder::Decl;
  51. later_modifiers = KeywordModifierSet::None;
  52. }
  53. auto current_modifier_node_id = s.modifier_node_id(order);
  54. if (s.modifier_set.HasAnyOf(keyword)) {
  55. DiagnoseRepeated(context, current_modifier_node_id, node_id);
  56. return false;
  57. }
  58. if (current_modifier_node_id.has_value()) {
  59. DiagnoseNotAllowedWith(context, current_modifier_node_id, node_id);
  60. return false;
  61. }
  62. if (auto later_modifier_set = s.modifier_set & later_modifiers;
  63. !later_modifier_set.empty()) {
  64. // At least one later modifier is present. Diagnose using the closest.
  65. Parse::NodeId closest_later_modifier = Parse::NodeId::None;
  66. for (auto later_order = static_cast<int8_t>(order) + 1;
  67. later_order <= static_cast<int8_t>(ModifierOrder::Last);
  68. ++later_order) {
  69. if (s.ordered_modifier_node_ids[later_order].has_value()) {
  70. closest_later_modifier = s.ordered_modifier_node_ids[later_order];
  71. break;
  72. }
  73. }
  74. CARBON_CHECK(closest_later_modifier.has_value());
  75. CARBON_DIAGNOSTIC(ModifierMustAppearBefore, Error,
  76. "`{0}` must appear before `{1}`", Lex::TokenKind,
  77. Lex::TokenKind);
  78. context.emitter()
  79. .Build(node_id, ModifierMustAppearBefore, context.token_kind(node_id),
  80. context.token_kind(closest_later_modifier))
  81. .Note(closest_later_modifier, ModifierPrevious,
  82. context.token_kind(closest_later_modifier))
  83. .Emit();
  84. return false;
  85. }
  86. s.modifier_set.Add(keyword);
  87. s.set_modifier_node_id(order, node_id);
  88. return true;
  89. }
  90. #define CARBON_PARSE_NODE_KIND(Name)
  91. #define CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name) \
  92. auto HandleParseNode(Context& context, Parse::Name##ModifierId node_id) \
  93. -> bool { \
  94. HandleModifier(context, node_id, KeywordModifierSet::Name); \
  95. return true; \
  96. }
  97. #include "toolchain/parse/node_kind.def"
  98. auto HandleParseNode(Context& context,
  99. Parse::ExternModifierWithLibraryId node_id) -> bool {
  100. auto name_literal_id = context.node_stack().Pop<SemIR::LibraryNameId>();
  101. if (HandleModifier(context, node_id, KeywordModifierSet::Extern)) {
  102. auto& s = context.decl_introducer_state_stack().innermost();
  103. s.extern_library = name_literal_id;
  104. }
  105. return true;
  106. }
  107. auto HandleParseNode(Context& context, Parse::ExternModifierId node_id)
  108. -> bool {
  109. return HandleModifier(context, node_id, KeywordModifierSet::Extern);
  110. }
  111. auto HandleParseNode(Context& context, Parse::ReturnedModifierId node_id)
  112. -> bool {
  113. return HandleModifier(context, node_id, KeywordModifierSet::Returned);
  114. }
  115. } // namespace Carbon::Check