handle_tuple_literal.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #include "toolchain/check/handle.h"
  6. #include "toolchain/check/inst.h"
  7. #include "toolchain/check/type.h"
  8. namespace Carbon::Check {
  9. auto HandleParseNode(Context& context, Parse::TupleLiteralStartId node_id)
  10. -> bool {
  11. context.node_stack().Push(node_id);
  12. context.param_and_arg_refs_stack().Push();
  13. return true;
  14. }
  15. auto HandleParseNode(Context& context, Parse::TupleLiteralCommaId /*node_id*/)
  16. -> bool {
  17. context.param_and_arg_refs_stack().ApplyComma();
  18. return true;
  19. }
  20. auto HandleParseNode(Context& context, Parse::TupleLiteralId node_id) -> bool {
  21. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  22. Parse::NodeKind::TupleLiteralStart);
  23. context.node_stack()
  24. .PopAndDiscardSoloNodeId<Parse::NodeKind::TupleLiteralStart>();
  25. const auto& inst_block = context.inst_blocks().Get(refs_id);
  26. llvm::SmallVector<SemIR::InstId> type_inst_ids;
  27. type_inst_ids.reserve(inst_block.size());
  28. for (auto inst : inst_block) {
  29. type_inst_ids.push_back(
  30. context.types().GetInstId(context.insts().Get(inst).type_id()));
  31. }
  32. auto type_id = GetTupleType(context, type_inst_ids);
  33. auto value_id = AddInst<SemIR::TupleLiteral>(
  34. context, node_id, {.type_id = type_id, .elements_id = refs_id});
  35. context.node_stack().Push(node_id, value_id);
  36. return true;
  37. }
  38. } // namespace Carbon::Check