handle_paren.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. namespace Carbon::Check {
  6. auto HandleParenExpr(Context& context, Parse::NodeId parse_node) -> bool {
  7. auto value_id = context.node_stack().PopExpr();
  8. // ParamOrArgStart was called for tuple handling; clean up the ParamOrArg
  9. // support for non-tuple cases.
  10. context.ParamOrArgEnd(Parse::NodeKind::ParenExprOrTupleLiteralStart);
  11. context.node_stack()
  12. .PopAndDiscardSoloParseNode<
  13. Parse::NodeKind::ParenExprOrTupleLiteralStart>();
  14. context.node_stack().Push(parse_node, value_id);
  15. return true;
  16. }
  17. auto HandleParenExprOrTupleLiteralStart(Context& context,
  18. Parse::NodeId parse_node) -> bool {
  19. context.node_stack().Push(parse_node);
  20. context.ParamOrArgStart();
  21. return true;
  22. }
  23. auto HandleTupleLiteralComma(Context& context, Parse::NodeId /*parse_node*/)
  24. -> bool {
  25. context.ParamOrArgComma();
  26. return true;
  27. }
  28. auto HandleTupleLiteral(Context& context, Parse::NodeId parse_node) -> bool {
  29. auto refs_id =
  30. context.ParamOrArgEnd(Parse::NodeKind::ParenExprOrTupleLiteralStart);
  31. context.node_stack()
  32. .PopAndDiscardSoloParseNode<
  33. Parse::NodeKind::ParenExprOrTupleLiteralStart>();
  34. const auto& inst_block = context.inst_blocks().Get(refs_id);
  35. llvm::SmallVector<SemIR::TypeId> type_ids;
  36. type_ids.reserve(inst_block.size());
  37. for (auto inst : inst_block) {
  38. type_ids.push_back(context.insts().Get(inst).type_id());
  39. }
  40. auto type_id = context.CanonicalizeTupleType(parse_node, type_ids);
  41. auto value_id =
  42. context.AddInst(SemIR::TupleLiteral{parse_node, type_id, refs_id});
  43. context.node_stack().Push(parse_node, value_id);
  44. return true;
  45. }
  46. } // namespace Carbon::Check