handle_paren.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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::ExprOpenParen);
  11. context.node_stack()
  12. .PopAndDiscardSoloParseNode<Parse::NodeKind::ExprOpenParen>();
  13. context.node_stack().Push(parse_node, value_id);
  14. return true;
  15. }
  16. auto HandleExprOpenParen(Context& context, Parse::NodeId parse_node) -> bool {
  17. context.node_stack().Push(parse_node);
  18. context.ParamOrArgStart();
  19. return true;
  20. }
  21. auto HandleTupleLiteralComma(Context& context, Parse::NodeId /*parse_node*/)
  22. -> bool {
  23. context.ParamOrArgComma();
  24. return true;
  25. }
  26. auto HandleTupleLiteral(Context& context, Parse::NodeId parse_node) -> bool {
  27. auto refs_id = context.ParamOrArgEnd(Parse::NodeKind::ExprOpenParen);
  28. context.node_stack()
  29. .PopAndDiscardSoloParseNode<Parse::NodeKind::ExprOpenParen>();
  30. const auto& inst_block = context.inst_blocks().Get(refs_id);
  31. llvm::SmallVector<SemIR::TypeId> type_ids;
  32. type_ids.reserve(inst_block.size());
  33. for (auto inst : inst_block) {
  34. type_ids.push_back(context.insts().Get(inst).type_id());
  35. }
  36. auto type_id = context.CanonicalizeTupleType(parse_node, type_ids);
  37. auto value_id =
  38. context.AddInst(SemIR::TupleLiteral{parse_node, type_id, refs_id});
  39. context.node_stack().Push(parse_node, value_id);
  40. return true;
  41. }
  42. } // namespace Carbon::Check