handle_tuple_literal.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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::TypeId> type_ids;
  27. type_ids.reserve(inst_block.size());
  28. for (auto inst : inst_block) {
  29. type_ids.push_back(context.insts().Get(inst).type_id());
  30. }
  31. auto type_id = GetTupleType(context, type_ids);
  32. auto value_id = AddInst<SemIR::TupleLiteral>(
  33. context, node_id, {.type_id = type_id, .elements_id = refs_id});
  34. context.node_stack().Push(node_id, value_id);
  35. return true;
  36. }
  37. } // namespace Carbon::Check