handle_pattern_list.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/subpattern.h"
  7. namespace Carbon::Check {
  8. auto HandleParseNode(Context& context, Parse::ImplicitParamListStartId node_id)
  9. -> bool {
  10. context.node_stack().Push(node_id);
  11. context.param_and_arg_refs_stack().Push();
  12. BeginSubpattern(context);
  13. return true;
  14. }
  15. auto HandleParseNode(Context& context, Parse::ImplicitParamListId node_id)
  16. -> bool {
  17. if (context.node_stack().PeekIs(Parse::NodeKind::ImplicitParamListStart)) {
  18. // End the subpattern started by a trailing comma, or the opening delimiter
  19. // of an empty list.
  20. EndSubpatternAsEmpty(context);
  21. }
  22. // Note the Start node remains on the stack, where the param list handler can
  23. // make use of it.
  24. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  25. Parse::NodeKind::ImplicitParamListStart);
  26. context.node_stack().Push(node_id, refs_id);
  27. // The implicit parameter list's scope extends to the end of the following
  28. // parameter list.
  29. return true;
  30. }
  31. auto HandleParseNode(Context& context, Parse::TuplePatternStartId node_id)
  32. -> bool {
  33. context.node_stack().Push(node_id);
  34. context.param_and_arg_refs_stack().Push();
  35. BeginSubpattern(context);
  36. // TODO: Remove this branch once the parse tree differentiates between
  37. // tuple patterns and param patterns.
  38. if (context.full_pattern_stack().CurrentKind() ==
  39. FullPatternStack::Kind::ImplicitParamList) {
  40. context.full_pattern_stack().EndImplicitParamList();
  41. }
  42. return true;
  43. }
  44. auto HandleParseNode(Context& context, Parse::PatternListCommaId /*node_id*/)
  45. -> bool {
  46. context.param_and_arg_refs_stack().ApplyComma();
  47. BeginSubpattern(context);
  48. return true;
  49. }
  50. auto HandleParseNode(Context& context, Parse::TuplePatternId node_id) -> bool {
  51. if (context.node_stack().PeekIs(Parse::NodeKind::TuplePatternStart)) {
  52. // End the subpattern started by a trailing comma, or the opening delimiter
  53. // of an empty list.
  54. EndSubpatternAsEmpty(context);
  55. }
  56. // Note the Start node remains on the stack, where the param list handler can
  57. // make use of it.
  58. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  59. Parse::NodeKind::TuplePatternStart);
  60. context.node_stack().Push(node_id, refs_id);
  61. return true;
  62. }
  63. } // namespace Carbon::Check