name_component.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. auto [params_loc_id, params_id] =
  9. context.node_stack().PopWithNodeIdIf<Parse::NodeKind::TuplePattern>();
  10. auto [implicit_params_loc_id, implicit_params_id] =
  11. context.node_stack()
  12. .PopWithNodeIdIf<Parse::NodeKind::ImplicitParamList>();
  13. auto [name_loc_id, name_id] = context.node_stack().PopNameWithNodeId();
  14. return {
  15. .name_loc_id = name_loc_id,
  16. .name_id = name_id,
  17. .implicit_params_loc_id = implicit_params_loc_id,
  18. .implicit_params_id =
  19. implicit_params_id.value_or(SemIR::InstBlockId::Invalid),
  20. .params_loc_id = params_loc_id,
  21. .params_id = params_id.value_or(SemIR::InstBlockId::Invalid),
  22. };
  23. }
  24. // Pop the name of a declaration from the node stack, and diagnose if it has
  25. // parameters.
  26. auto PopNameComponentWithoutParams(Context& context, Lex::TokenKind introducer)
  27. -> NameComponent {
  28. NameComponent name = PopNameComponent(context);
  29. if (name.implicit_params_id.is_valid() || name.params_id.is_valid()) {
  30. CARBON_DIAGNOSTIC(UnexpectedDeclNameParams, Error,
  31. "`{0}` declaration cannot have parameters.",
  32. Lex::TokenKind);
  33. // Point to the lexically first parameter list in the diagnostic.
  34. context.emitter().Emit(name.implicit_params_id.is_valid()
  35. ? name.implicit_params_loc_id
  36. : name.params_loc_id,
  37. UnexpectedDeclNameParams, introducer);
  38. name.implicit_params_id = SemIR::InstBlockId::Invalid;
  39. name.params_id = SemIR::InstBlockId::Invalid;
  40. }
  41. return name;
  42. }
  43. } // namespace Carbon::Check