type.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/type.h"
  5. #include "toolchain/sem_ir/file.h"
  6. namespace Carbon::SemIR {
  7. auto TypeStore::GetTypeIdForTypeConstantId(SemIR::ConstantId constant_id) const
  8. -> SemIR::TypeId {
  9. CARBON_CHECK(constant_id.is_constant(),
  10. "Canonicalizing non-constant type: {0}", constant_id);
  11. auto type_id = file_->insts()
  12. .Get(file_->constant_values().GetInstId(constant_id))
  13. .type_id();
  14. CARBON_CHECK(type_id == SemIR::TypeType::SingletonTypeId ||
  15. constant_id == SemIR::ErrorInst::SingletonConstantId,
  16. "Forming type ID for non-type constant of type {0}",
  17. GetAsInst(type_id));
  18. return SemIR::TypeId::ForTypeConstant(constant_id);
  19. }
  20. auto TypeStore::GetTypeIdForTypeInstId(SemIR::InstId inst_id) const
  21. -> SemIR::TypeId {
  22. return GetTypeIdForTypeConstantId(file_->constant_values().Get(inst_id));
  23. }
  24. auto TypeStore::GetInstId(TypeId type_id) const -> InstId {
  25. return file_->constant_values().GetInstId(GetConstantId(type_id));
  26. }
  27. auto TypeStore::GetAsInst(TypeId type_id) const -> Inst {
  28. return file_->insts().Get(GetInstId(type_id));
  29. }
  30. auto TypeStore::GetObjectRepr(TypeId type_id) const -> TypeId {
  31. type_id = GetUnqualifiedType(type_id);
  32. auto class_type = TryGetAs<ClassType>(type_id);
  33. if (!class_type) {
  34. return type_id;
  35. }
  36. const auto& class_info = file_->classes().Get(class_type->class_id);
  37. if (!class_info.is_defined()) {
  38. return TypeId::None;
  39. }
  40. return class_info.GetObjectRepr(*file_, class_type->specific_id);
  41. }
  42. auto TypeStore::GetUnqualifiedType(TypeId type_id) const -> TypeId {
  43. if (auto const_type = TryGetAs<ConstType>(type_id)) {
  44. return const_type->inner_id;
  45. }
  46. return type_id;
  47. }
  48. static auto TryGetIntTypeInfo(const File& file, TypeId type_id)
  49. -> std::optional<TypeStore::IntTypeInfo> {
  50. auto object_repr_id = file.types().GetObjectRepr(type_id);
  51. if (!object_repr_id.has_value()) {
  52. return std::nullopt;
  53. }
  54. auto inst_id = file.types().GetInstId(object_repr_id);
  55. if (inst_id == IntLiteralType::SingletonInstId) {
  56. // `Core.IntLiteral` has an unknown bit-width.
  57. return TypeStore::IntTypeInfo{.is_signed = true, .bit_width = IntId::None};
  58. }
  59. auto int_type = file.insts().TryGetAs<IntType>(inst_id);
  60. if (!int_type) {
  61. return std::nullopt;
  62. }
  63. auto bit_width_inst = file.insts().TryGetAs<IntValue>(int_type->bit_width_id);
  64. return TypeStore::IntTypeInfo{
  65. .is_signed = int_type->int_kind.is_signed(),
  66. .bit_width = bit_width_inst ? bit_width_inst->int_id : IntId::None};
  67. }
  68. auto TypeStore::IsSignedInt(TypeId int_type_id) const -> bool {
  69. auto int_info = TryGetIntTypeInfo(*file_, int_type_id);
  70. return int_info && int_info->is_signed;
  71. }
  72. auto TypeStore::GetIntTypeInfo(TypeId int_type_id) const -> IntTypeInfo {
  73. auto int_info = TryGetIntTypeInfo(*file_, int_type_id);
  74. CARBON_CHECK(int_info, "Type {0} is not an integer type", int_type_id);
  75. return *int_info;
  76. }
  77. } // namespace Carbon::SemIR