class.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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/class.h"
  5. #include "toolchain/sem_ir/file.h"
  6. #include "toolchain/sem_ir/generic.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. namespace Carbon::SemIR {
  10. static auto GetFoundationType(const File& file, SpecificId specific_id,
  11. InstId inst_id) -> TypeId {
  12. if (!inst_id.has_value()) {
  13. return TypeId::None;
  14. }
  15. if (inst_id == SemIR::ErrorInst::SingletonInstId) {
  16. return ErrorInst::SingletonTypeId;
  17. }
  18. return TypeId::ForTypeConstant(GetConstantValueInSpecific(
  19. file, specific_id,
  20. file.insts().GetAs<AnyFoundationDecl>(inst_id).foundation_type_inst_id));
  21. }
  22. auto Class::GetAdaptedType(const File& file, SpecificId specific_id) const
  23. -> TypeId {
  24. return GetFoundationType(file, specific_id, adapt_id);
  25. }
  26. auto Class::GetBaseType(const File& file, SpecificId specific_id) const
  27. -> TypeId {
  28. return GetFoundationType(file, specific_id, base_id);
  29. }
  30. auto Class::GetObjectRepr(const File& file, SpecificId specific_id) const
  31. -> TypeId {
  32. if (!complete_type_witness_id.has_value()) {
  33. return TypeId::None;
  34. }
  35. auto witness_id =
  36. GetConstantValueInSpecific(file, specific_id, complete_type_witness_id);
  37. if (witness_id == ErrorInst::SingletonConstantId) {
  38. return ErrorInst::SingletonTypeId;
  39. }
  40. return file.insts()
  41. .GetAs<CompleteTypeWitness>(file.constant_values().GetInstId(witness_id))
  42. .object_repr_id;
  43. }
  44. } // namespace Carbon::SemIR