handle_pattern_list.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/context.h"
  5. #include "toolchain/check/handle.h"
  6. #include "toolchain/check/inst.h"
  7. #include "toolchain/check/pattern.h"
  8. #include "toolchain/check/type.h"
  9. namespace Carbon::Check {
  10. // Handle the start of any kind of pattern list.
  11. static auto HandlePatternListStart(Context& context, Parse::NodeId node_id)
  12. -> bool {
  13. context.node_stack().Push(node_id);
  14. context.param_and_arg_refs_stack().Push();
  15. BeginSubpattern(context);
  16. return true;
  17. }
  18. auto HandleParseNode(Context& context, Parse::ImplicitParamListStartId node_id)
  19. -> bool {
  20. return HandlePatternListStart(context, node_id);
  21. }
  22. auto HandleParseNode(Context& context, Parse::TuplePatternStartId node_id)
  23. -> bool {
  24. return HandlePatternListStart(context, node_id);
  25. }
  26. auto HandleParseNode(Context& context, Parse::ExplicitParamListStartId node_id)
  27. -> bool {
  28. context.full_pattern_stack().EndImplicitParamList();
  29. return HandlePatternListStart(context, node_id);
  30. }
  31. // Handle the end of any kind of parameter list (tuple patterns have separate
  32. // logic).
  33. static auto HandleParamListEnd(Context& context, Parse::NodeId node_id,
  34. Parse::NodeKind start_kind) -> bool {
  35. if (context.node_stack().PeekIs(start_kind)) {
  36. // End the subpattern started by a trailing comma, or the opening delimiter
  37. // of an empty list.
  38. EndSubpatternAsNonExpr(context);
  39. }
  40. // Note the Start node remains on the stack, where the param list handler can
  41. // make use of it.
  42. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(start_kind);
  43. context.node_stack().Push(node_id, refs_id);
  44. return true;
  45. }
  46. auto HandleParseNode(Context& context, Parse::ImplicitParamListId node_id)
  47. -> bool {
  48. return HandleParamListEnd(context, node_id,
  49. Parse::NodeKind::ImplicitParamListStart);
  50. }
  51. auto HandleParseNode(Context& context, Parse::ExplicitParamListId node_id)
  52. -> bool {
  53. return HandleParamListEnd(context, node_id,
  54. Parse::NodeKind::ExplicitParamListStart);
  55. }
  56. auto HandleParseNode(Context& context, Parse::TuplePatternId node_id) -> bool {
  57. if (context.node_stack().PeekIs(Parse::NodeKind::TuplePatternStart)) {
  58. // End the subpattern started by a trailing comma, or the opening delimiter
  59. // of an empty list.
  60. EndSubpatternAsNonExpr(context);
  61. }
  62. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  63. Parse::NodeKind::TuplePatternStart);
  64. context.node_stack()
  65. .PopAndDiscardSoloNodeId<Parse::NodeKind::TuplePatternStart>();
  66. const auto& inst_block = context.inst_blocks().Get(refs_id);
  67. llvm::SmallVector<SemIR::InstId> type_inst_ids;
  68. type_inst_ids.reserve(inst_block.size());
  69. for (auto inst : inst_block) {
  70. auto type_id = ExtractScrutineeType(context.sem_ir(),
  71. context.insts().Get(inst).type_id());
  72. type_inst_ids.push_back(context.types().GetInstId(type_id));
  73. }
  74. auto type_id = GetPatternType(context, GetTupleType(context, type_inst_ids));
  75. context.node_stack().Push(
  76. node_id,
  77. AddPatternInst<SemIR::TuplePattern>(
  78. context, node_id, {.type_id = type_id, .elements_id = refs_id}));
  79. EndSubpatternAsNonExpr(context);
  80. return true;
  81. }
  82. auto HandleParseNode(Context& context, Parse::PatternListCommaId /*node_id*/)
  83. -> bool {
  84. context.param_and_arg_refs_stack().ApplyComma();
  85. BeginSubpattern(context);
  86. return true;
  87. }
  88. } // namespace Carbon::Check