handle_tuple_literal.cpp 1.4 KB

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