handle_pattern_list.cpp 3.7 KB

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