name_component.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/name_component.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/pattern_match.h"
  7. namespace Carbon::Check {
  8. auto PopNameComponent(Context& context, SemIR::InstId return_slot_pattern_id)
  9. -> NameComponent {
  10. Parse::NodeId first_param_node_id = Parse::NoneNodeId();
  11. Parse::NodeId last_param_node_id = Parse::NoneNodeId();
  12. // Explicit params.
  13. auto [params_loc_id, param_patterns_id] =
  14. context.node_stack().PopWithNodeIdIf<Parse::NodeKind::TuplePattern>();
  15. if (param_patterns_id) {
  16. first_param_node_id =
  17. context.node_stack()
  18. .PopForSoloNodeId<Parse::NodeKind::TuplePatternStart>();
  19. last_param_node_id = params_loc_id;
  20. } else {
  21. param_patterns_id = SemIR::InstBlockId::None;
  22. }
  23. // Implicit params.
  24. auto [implicit_params_loc_id, implicit_param_patterns_id] =
  25. context.node_stack()
  26. .PopWithNodeIdIf<Parse::NodeKind::ImplicitParamList>();
  27. if (implicit_param_patterns_id) {
  28. // Implicit params always come before explicit params.
  29. first_param_node_id =
  30. context.node_stack()
  31. .PopForSoloNodeId<Parse::NodeKind::ImplicitParamListStart>();
  32. // Only use the end of implicit params if there weren't explicit params.
  33. if (last_param_node_id.has_value()) {
  34. last_param_node_id = params_loc_id;
  35. }
  36. } else {
  37. implicit_param_patterns_id = SemIR::InstBlockId::None;
  38. }
  39. auto call_params_id = SemIR::InstBlockId::None;
  40. auto pattern_block_id = SemIR::InstBlockId::None;
  41. if (param_patterns_id->has_value() ||
  42. implicit_param_patterns_id->has_value()) {
  43. call_params_id =
  44. CalleePatternMatch(context, *implicit_param_patterns_id,
  45. *param_patterns_id, return_slot_pattern_id);
  46. pattern_block_id = context.pattern_block_stack().Pop();
  47. context.full_pattern_stack().PopFullPattern();
  48. }
  49. auto [name_loc_id, name_id] =
  50. context.node_stack()
  51. .PopWithNodeId<Parse::NodeCategory::NonExprIdentifierName>();
  52. return {
  53. .name_loc_id = name_loc_id,
  54. .name_id = name_id,
  55. .first_param_node_id = first_param_node_id,
  56. .last_param_node_id = last_param_node_id,
  57. .implicit_params_loc_id = implicit_params_loc_id,
  58. .implicit_param_patterns_id = *implicit_param_patterns_id,
  59. .params_loc_id = params_loc_id,
  60. .param_patterns_id = *param_patterns_id,
  61. .call_params_id = call_params_id,
  62. .return_slot_pattern_id = return_slot_pattern_id,
  63. .pattern_block_id = pattern_block_id,
  64. };
  65. }
  66. // Pop the name of a declaration from the node stack, and diagnose if it has
  67. // parameters.
  68. auto PopNameComponentWithoutParams(Context& context, Lex::TokenKind introducer)
  69. -> NameComponent {
  70. NameComponent name = PopNameComponent(context);
  71. if (name.call_params_id.has_value()) {
  72. CARBON_DIAGNOSTIC(UnexpectedDeclNameParams, Error,
  73. "`{0}` declaration cannot have parameters",
  74. Lex::TokenKind);
  75. // Point to the lexically first parameter list in the diagnostic.
  76. context.emitter().Emit(name.implicit_param_patterns_id.has_value()
  77. ? name.implicit_params_loc_id
  78. : name.params_loc_id,
  79. UnexpectedDeclNameParams, introducer);
  80. name.call_params_id = SemIR::InstBlockId::None;
  81. }
  82. return name;
  83. }
  84. } // namespace Carbon::Check