handle_paren.cpp 1.9 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 "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.param_and_arg_refs_stack().Push();
  10. return true;
  11. }
  12. auto HandleParenExpr(Context& context, Parse::ParenExprId parse_node) -> bool {
  13. auto value_id = context.node_stack().PopExpr();
  14. // This always is always pushed at the open paren. It's only used for tuples,
  15. // not paren exprs, but we still need to clean up.
  16. context.param_and_arg_refs_stack().PopAndDiscard();
  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.param_and_arg_refs_stack().ApplyComma();
  26. return true;
  27. }
  28. auto HandleTupleLiteral(Context& context, Parse::TupleLiteralId parse_node)
  29. -> bool {
  30. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  31. Parse::NodeKind::ExprOpenParen);
  32. context.node_stack()
  33. .PopAndDiscardSoloParseNode<Parse::NodeKind::ExprOpenParen>();
  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.GetTupleType(type_ids);
  41. auto value_id =
  42. context.AddInst({parse_node, SemIR::TupleLiteral{type_id, refs_id}});
  43. context.node_stack().Push(parse_node, value_id);
  44. return true;
  45. }
  46. } // namespace Carbon::Check