name_component.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. CARBON_CHECK(last_param_node_id.has_value(),
  34. "Implicit parameters currently only occur before explicit "
  35. "parameters, update and test when this changes");
  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. return_slot_pattern_id.has_value()) {
  44. call_params_id =
  45. CalleePatternMatch(context, *implicit_param_patterns_id,
  46. *param_patterns_id, return_slot_pattern_id);
  47. pattern_block_id = context.pattern_block_stack().Pop();
  48. context.full_pattern_stack().PopFullPattern();
  49. }
  50. auto [name_loc_id, name_id] =
  51. context.node_stack()
  52. .PopWithNodeId<Parse::NodeCategory::NonExprIdentifierName>();
  53. return {
  54. .name_loc_id = name_loc_id,
  55. .name_id = name_id,
  56. .first_param_node_id = first_param_node_id,
  57. .last_param_node_id = last_param_node_id,
  58. .implicit_params_loc_id = implicit_params_loc_id,
  59. .implicit_param_patterns_id = *implicit_param_patterns_id,
  60. .params_loc_id = params_loc_id,
  61. .param_patterns_id = *param_patterns_id,
  62. .call_params_id = call_params_id,
  63. .return_slot_pattern_id = return_slot_pattern_id,
  64. .pattern_block_id = pattern_block_id,
  65. };
  66. }
  67. // Pop the name of a declaration from the node stack, and diagnose if it has
  68. // parameters.
  69. auto PopNameComponentWithoutParams(Context& context, Lex::TokenKind introducer)
  70. -> NameComponent {
  71. NameComponent name = PopNameComponent(context);
  72. if (name.call_params_id.has_value()) {
  73. CARBON_DIAGNOSTIC(UnexpectedDeclNameParams, Error,
  74. "`{0}` declaration cannot have parameters",
  75. Lex::TokenKind);
  76. // Point to the lexically first parameter list in the diagnostic.
  77. context.emitter().Emit(name.implicit_param_patterns_id.has_value()
  78. ? name.implicit_params_loc_id
  79. : name.params_loc_id,
  80. UnexpectedDeclNameParams, introducer);
  81. name.call_params_id = SemIR::InstBlockId::None;
  82. }
  83. return name;
  84. }
  85. } // namespace Carbon::Check