cpp_initializer_list.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/sem_ir/cpp_initializer_list.h"
  5. #include "toolchain/sem_ir/file.h"
  6. #include "toolchain/sem_ir/inst_categories.h"
  7. #include "toolchain/sem_ir/typed_insts.h"
  8. namespace Carbon::SemIR {
  9. auto GetStdInitializerListLayout(const File& sem_ir, TypeId type_id)
  10. -> StdInitializerListLayout {
  11. auto repr_id = sem_ir.types().GetObjectRepr(type_id);
  12. if (!repr_id.has_value()) {
  13. // An incomplete type doesn't have a recognized layout.
  14. return StdInitializerListLayout{};
  15. }
  16. auto struct_type = sem_ir.types().TryGetAs<AnyStructType>(repr_id);
  17. if (!struct_type) {
  18. return StdInitializerListLayout{};
  19. }
  20. auto fields = sem_ir.struct_type_fields().Get(struct_type->fields_id);
  21. if (fields.size() != 2) {
  22. return StdInitializerListLayout{};
  23. }
  24. // The first field must be a pointer.
  25. auto field0_type_id = sem_ir.types().GetTransitiveUnqualifiedAdaptedType(
  26. sem_ir.types().GetTypeIdForTypeInstId(fields[0].type_inst_id));
  27. if (!sem_ir.types().Is<PointerType>(field0_type_id.first)) {
  28. return StdInitializerListLayout{};
  29. }
  30. // The second field can be either a pointer or an integer.
  31. auto field1_type_id = sem_ir.types().GetTransitiveUnqualifiedAdaptedType(
  32. sem_ir.types().GetTypeIdForTypeInstId(fields[1].type_inst_id));
  33. if (sem_ir.types().Is<PointerType>(field1_type_id.first)) {
  34. return StdInitializerListLayout{
  35. .kind = StdInitializerListLayout::PointerPointer};
  36. }
  37. if (sem_ir.types().TryGetIntTypeInfo(field1_type_id.first)) {
  38. return StdInitializerListLayout{
  39. .kind = StdInitializerListLayout::PointerInt,
  40. .size_type_id = field1_type_id.first};
  41. }
  42. return StdInitializerListLayout{};
  43. }
  44. } // namespace Carbon::SemIR