name_component.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_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_node_id, param_patterns_id] =
  14. context.node_stack()
  15. .PopWithNodeIdIf<Parse::NodeKind::ExplicitParamList>();
  16. if (param_patterns_id) {
  17. first_param_node_id =
  18. context.node_stack()
  19. .PopForSoloNodeId<Parse::NodeKind::ExplicitParamListStart>();
  20. last_param_node_id = params_node_id;
  21. } else {
  22. param_patterns_id = SemIR::InstBlockId::None;
  23. }
  24. // Implicit params.
  25. auto [implicit_params_node_id, implicit_param_patterns_id] =
  26. context.node_stack()
  27. .PopWithNodeIdIf<Parse::NodeKind::ImplicitParamList>();
  28. if (implicit_param_patterns_id) {
  29. // Implicit params always come before explicit params.
  30. first_param_node_id =
  31. context.node_stack()
  32. .PopForSoloNodeId<Parse::NodeKind::ImplicitParamListStart>();
  33. // Only use the end of implicit params if there weren't explicit params.
  34. if (!last_param_node_id.has_value()) {
  35. last_param_node_id = implicit_params_node_id;
  36. }
  37. } else {
  38. implicit_param_patterns_id = SemIR::InstBlockId::None;
  39. }
  40. auto call_param_patterns_id = SemIR::InstBlockId::None;
  41. auto call_params_id = SemIR::InstBlockId::None;
  42. auto param_ranges = SemIR::Function::CallParamIndexRanges::Empty;
  43. auto pattern_block_id = SemIR::InstBlockId::None;
  44. if (param_patterns_id->has_value() ||
  45. implicit_param_patterns_id->has_value() ||
  46. return_pattern_id.has_value()) {
  47. auto results = CalleePatternMatch(context, *implicit_param_patterns_id,
  48. *param_patterns_id, return_pattern_id);
  49. call_param_patterns_id = results.call_param_patterns_id;
  50. call_params_id = results.call_params_id;
  51. param_ranges = results.param_ranges;
  52. pattern_block_id = context.pattern_block_stack().Pop();
  53. context.full_pattern_stack().PopFullPattern();
  54. }
  55. auto [name_loc_id, name_id] =
  56. context.node_stack().PopWithNodeId<Parse::NodeCategory::NonExprName>();
  57. return {
  58. .name_loc_id = name_loc_id,
  59. .name_id = name_id,
  60. .first_param_node_id = first_param_node_id,
  61. .last_param_node_id = last_param_node_id,
  62. .implicit_params_loc_id = implicit_params_node_id,
  63. .implicit_param_patterns_id = *implicit_param_patterns_id,
  64. .params_loc_id = params_node_id,
  65. .param_patterns_id = *param_patterns_id,
  66. .call_param_patterns_id = call_param_patterns_id,
  67. .call_params_id = call_params_id,
  68. .param_ranges = param_ranges,
  69. .pattern_block_id = pattern_block_id,
  70. };
  71. }
  72. // Pop the name of a declaration from the node stack, and diagnose if it has
  73. // parameters.
  74. auto PopNameComponentWithoutParams(Context& context, Lex::TokenKind introducer)
  75. -> NameComponent {
  76. NameComponent name = PopNameComponent(context);
  77. if (name.call_params_id.has_value()) {
  78. CARBON_DIAGNOSTIC(UnexpectedDeclNameParams, Error,
  79. "`{0}` declaration cannot have parameters",
  80. Lex::TokenKind);
  81. // Point to the lexically first parameter list in the diagnostic.
  82. context.emitter().Emit(name.implicit_param_patterns_id.has_value()
  83. ? name.implicit_params_loc_id
  84. : name.params_loc_id,
  85. UnexpectedDeclNameParams, introducer);
  86. name.call_params_id = SemIR::InstBlockId::None;
  87. }
  88. return name;
  89. }
  90. } // namespace Carbon::Check