semantics_handle_paren.cpp 2.7 KB

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