handle_tuple_literal.cpp 1.4 KB

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