handle_pattern_list.cpp 2.4 KB

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