name_component.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. namespace Carbon::Check {
  7. auto PopNameComponent(Context& context) -> NameComponent {
  8. Parse::NodeId first_param_node_id = Parse::InvalidNodeId();
  9. Parse::NodeId last_param_node_id = Parse::InvalidNodeId();
  10. // Explicit params.
  11. auto [params_loc_id, params_id] =
  12. context.node_stack().PopWithNodeIdIf<Parse::NodeKind::TuplePattern>();
  13. if (params_id) {
  14. first_param_node_id =
  15. context.node_stack()
  16. .PopForSoloNodeId<Parse::NodeKind::TuplePatternStart>();
  17. last_param_node_id = params_loc_id;
  18. }
  19. // Implicit params.
  20. auto [implicit_params_loc_id, implicit_params_id] =
  21. context.node_stack()
  22. .PopWithNodeIdIf<Parse::NodeKind::ImplicitParamList>();
  23. if (implicit_params_id) {
  24. // Implicit params always come before explicit params.
  25. first_param_node_id =
  26. context.node_stack()
  27. .PopForSoloNodeId<Parse::NodeKind::ImplicitParamListStart>();
  28. // Only use the end of implicit params if there weren't explicit params.
  29. if (last_param_node_id.is_valid()) {
  30. last_param_node_id = params_loc_id;
  31. }
  32. }
  33. auto [name_loc_id, name_id] = context.node_stack().PopNameWithNodeId();
  34. return {
  35. .name_loc_id = name_loc_id,
  36. .name_id = name_id,
  37. .first_param_node_id = first_param_node_id,
  38. .last_param_node_id = last_param_node_id,
  39. .implicit_params_loc_id = implicit_params_loc_id,
  40. .implicit_params_id =
  41. implicit_params_id.value_or(SemIR::InstBlockId::Invalid),
  42. .params_loc_id = params_loc_id,
  43. .params_id = params_id.value_or(SemIR::InstBlockId::Invalid),
  44. };
  45. }
  46. // Pop the name of a declaration from the node stack, and diagnose if it has
  47. // parameters.
  48. auto PopNameComponentWithoutParams(Context& context, Lex::TokenKind introducer)
  49. -> NameComponent {
  50. NameComponent name = PopNameComponent(context);
  51. if (name.implicit_params_id.is_valid() || name.params_id.is_valid()) {
  52. CARBON_DIAGNOSTIC(UnexpectedDeclNameParams, Error,
  53. "`{0}` declaration cannot have parameters",
  54. Lex::TokenKind);
  55. // Point to the lexically first parameter list in the diagnostic.
  56. context.emitter().Emit(name.implicit_params_id.is_valid()
  57. ? name.implicit_params_loc_id
  58. : name.params_loc_id,
  59. UnexpectedDeclNameParams, introducer);
  60. name.implicit_params_id = SemIR::InstBlockId::Invalid;
  61. name.params_id = SemIR::InstBlockId::Invalid;
  62. }
  63. return name;
  64. }
  65. } // namespace Carbon::Check