handle_paren.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <utility>
  5. #include "toolchain/check/context.h"
  6. namespace Carbon::Check {
  7. auto HandleParenExpression(Context& context, Parse::Node parse_node) -> bool {
  8. auto value_id = context.node_stack().PopExpression();
  9. // ParamOrArgStart was called for tuple handling; clean up the ParamOrArg
  10. // support for non-tuple cases.
  11. context.ParamOrArgEnd(
  12. /*for_args=*/true, Parse::NodeKind::ParenExpressionOrTupleLiteralStart);
  13. context.node_stack()
  14. .PopAndDiscardSoloParseNode<
  15. Parse::NodeKind::ParenExpressionOrTupleLiteralStart>();
  16. context.node_stack().Push(parse_node, value_id);
  17. return true;
  18. }
  19. auto HandleParenExpressionOrTupleLiteralStart(Context& context,
  20. Parse::Node parse_node) -> bool {
  21. context.node_stack().Push(parse_node);
  22. context.ParamOrArgStart();
  23. return true;
  24. }
  25. static auto HandleTupleLiteralElement(Context& context) -> void {
  26. // Convert the operand to a value.
  27. // TODO: We need to decide how tuple literals interact with expression
  28. // categories.
  29. auto [value_node, value_id] =
  30. context.node_stack().PopExpressionWithParseNode();
  31. value_id = context.ConvertToValueExpression(value_id);
  32. context.node_stack().Push(value_node, value_id);
  33. }
  34. auto HandleTupleLiteralComma(Context& context, Parse::Node /*parse_node*/)
  35. -> bool {
  36. HandleTupleLiteralElement(context);
  37. context.ParamOrArgComma(/*for_args=*/true);
  38. return true;
  39. }
  40. auto HandleTupleLiteral(Context& context, Parse::Node parse_node) -> bool {
  41. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
  42. Parse::NodeKind::ParenExpressionOrTupleLiteralStart) {
  43. HandleTupleLiteralElement(context);
  44. }
  45. auto refs_id = context.ParamOrArgEnd(
  46. /*for_args=*/true, Parse::NodeKind::ParenExpressionOrTupleLiteralStart);
  47. context.node_stack()
  48. .PopAndDiscardSoloParseNode<
  49. Parse::NodeKind::ParenExpressionOrTupleLiteralStart>();
  50. const auto& node_block = context.semantics_ir().GetNodeBlock(refs_id);
  51. llvm::SmallVector<SemIR::TypeId> type_ids;
  52. type_ids.reserve(node_block.size());
  53. for (auto node : node_block) {
  54. type_ids.push_back(context.semantics_ir().GetNode(node).type_id());
  55. }
  56. auto type_id = context.CanonicalizeTupleType(parse_node, std::move(type_ids));
  57. auto value_id = context.AddNode(
  58. SemIR::Node::TupleValue::Make(parse_node, type_id, refs_id));
  59. context.node_stack().Push(parse_node, value_id);
  60. return true;
  61. }
  62. } // namespace Carbon::Check