handle_paren.cpp 1.8 KB

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