handle_paren.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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(Parse::NodeKind::ParenExpressionOrTupleLiteralStart);
  12. context.node_stack()
  13. .PopAndDiscardSoloParseNode<
  14. Parse::NodeKind::ParenExpressionOrTupleLiteralStart>();
  15. context.node_stack().Push(parse_node, value_id);
  16. return true;
  17. }
  18. auto HandleParenExpressionOrTupleLiteralStart(Context& context,
  19. Parse::Node parse_node) -> bool {
  20. context.node_stack().Push(parse_node);
  21. context.ParamOrArgStart();
  22. return true;
  23. }
  24. auto HandleTupleLiteralComma(Context& context, Parse::Node /*parse_node*/)
  25. -> bool {
  26. context.ParamOrArgComma();
  27. return true;
  28. }
  29. auto HandleTupleLiteral(Context& context, Parse::Node parse_node) -> bool {
  30. auto refs_id = context.ParamOrArgEnd(
  31. Parse::NodeKind::ParenExpressionOrTupleLiteralStart);
  32. context.node_stack()
  33. .PopAndDiscardSoloParseNode<
  34. Parse::NodeKind::ParenExpressionOrTupleLiteralStart>();
  35. const auto& node_block = context.semantics_ir().GetNodeBlock(refs_id);
  36. llvm::SmallVector<SemIR::TypeId> type_ids;
  37. type_ids.reserve(node_block.size());
  38. for (auto node : node_block) {
  39. type_ids.push_back(context.semantics_ir().GetNode(node).type_id());
  40. }
  41. auto type_id = context.CanonicalizeTupleType(parse_node, std::move(type_ids));
  42. auto value_id = context.AddNode(
  43. SemIR::Node::TupleLiteral::Make(parse_node, type_id, refs_id));
  44. context.node_stack().Push(parse_node, value_id);
  45. return true;
  46. }
  47. } // namespace Carbon::Check