handle_paren.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 node_id)
  7. -> bool {
  8. context.node_stack().Push(node_id);
  9. context.param_and_arg_refs_stack().Push();
  10. return true;
  11. }
  12. auto HandleParenExpr(Context& context, Parse::ParenExprId node_id) -> 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. .PopAndDiscardSoloNodeId<Parse::NodeKind::ExprOpenParen>();
  19. context.node_stack().Push(node_id, value_id);
  20. return true;
  21. }
  22. auto HandleTupleLiteralComma(Context& context,
  23. Parse::TupleLiteralCommaId /*node_id*/) -> bool {
  24. context.param_and_arg_refs_stack().ApplyComma();
  25. return true;
  26. }
  27. auto HandleTupleLiteral(Context& context, Parse::TupleLiteralId node_id)
  28. -> bool {
  29. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  30. Parse::NodeKind::ExprOpenParen);
  31. context.node_stack()
  32. .PopAndDiscardSoloNodeId<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({node_id, SemIR::TupleLiteral{type_id, refs_id}});
  42. context.node_stack().Push(node_id, value_id);
  43. return true;
  44. }
  45. } // namespace Carbon::Check