name_component.cpp 3.7 KB

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