type.cpp 3.9 KB

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