type.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <optional>
  6. #include "toolchain/sem_ir/file.h"
  7. namespace Carbon::SemIR {
  8. CARBON_DEFINE_ENUM_MASK_NAMES(TypeQualifiers) {
  9. CARBON_TYPE_QUALIFIERS(CARBON_ENUM_MASK_NAME_STRING)
  10. };
  11. // Verify that the constant value's type is `TypeType` (or an error).
  12. static void CheckTypeOfConstantIsTypeType(File& file, ConstantId constant_id) {
  13. CARBON_CHECK(constant_id.is_constant(),
  14. "Canonicalizing non-constant type: {0}", constant_id);
  15. auto type_id =
  16. file.insts().Get(file.constant_values().GetInstId(constant_id)).type_id();
  17. CARBON_CHECK(
  18. type_id == TypeType::TypeId || constant_id == ErrorInst::ConstantId,
  19. "Forming type ID for non-type constant of type {0}",
  20. file.types().GetAsInst(type_id));
  21. }
  22. auto TypeStore::GetTypeIdForTypeConstantId(ConstantId constant_id) const
  23. -> TypeId {
  24. CheckTypeOfConstantIsTypeType(*file_, constant_id);
  25. return TypeId::ForTypeConstant(constant_id);
  26. }
  27. auto TypeStore::TryGetTypeIdForTypeConstantId(ConstantId constant_id) const
  28. -> TypeId {
  29. if (constant_id == SemIR::ErrorInst::ConstantId) {
  30. return SemIR::ErrorInst::TypeId;
  31. }
  32. auto type_id = file_->insts()
  33. .Get(file_->constant_values().GetInstId(constant_id))
  34. .type_id();
  35. if (type_id != SemIR::TypeType::TypeId) {
  36. return TypeId::None;
  37. }
  38. return TypeId::ForTypeConstant(constant_id);
  39. }
  40. auto TypeStore::GetTypeIdForTypeInstId(InstId inst_id) const -> TypeId {
  41. auto constant_id = file_->constant_values().Get(inst_id);
  42. CheckTypeOfConstantIsTypeType(*file_, constant_id);
  43. return TypeId::ForTypeConstant(constant_id);
  44. }
  45. auto TypeStore::GetTypeIdForTypeInstId(TypeInstId inst_id) const -> TypeId {
  46. auto constant_id = file_->constant_values().Get(inst_id);
  47. return TypeId::ForTypeConstant(constant_id);
  48. }
  49. auto TypeStore::TryGetTypeIdForTypeInstId(InstId inst_id) const -> TypeId {
  50. auto constant_id = file_->constant_values().Get(inst_id);
  51. return TryGetTypeIdForTypeConstantId(constant_id);
  52. }
  53. auto TypeStore::GetAsTypeInstId(InstId inst_id) const -> TypeInstId {
  54. auto constant_id = file_->constant_values().Get(inst_id);
  55. CheckTypeOfConstantIsTypeType(*file_, constant_id);
  56. return TypeInstId::UnsafeMake(inst_id);
  57. }
  58. auto TypeStore::GetTypeInstId(TypeId type_id) const -> TypeInstId {
  59. // The instruction for a TypeId has a value of that TypeId.
  60. return TypeInstId::UnsafeMake(
  61. file_->constant_values().GetInstId(GetConstantId(type_id)));
  62. }
  63. auto TypeStore::GetAsInst(TypeId type_id) const -> Inst {
  64. return file_->insts().Get(GetTypeInstId(type_id));
  65. }
  66. auto TypeStore::GetUnattachedType(TypeId type_id) const -> TypeId {
  67. return TypeId::ForTypeConstant(
  68. file_->constant_values().GetUnattachedConstant(type_id.AsConstantId()));
  69. }
  70. auto TypeStore::GetObjectRepr(TypeId type_id) const -> TypeId {
  71. type_id = GetUnqualifiedType(type_id);
  72. auto class_type = TryGetAs<ClassType>(type_id);
  73. if (!class_type) {
  74. return type_id;
  75. }
  76. const auto& class_info = file_->classes().Get(class_type->class_id);
  77. if (!class_info.is_complete()) {
  78. return TypeId::None;
  79. }
  80. return class_info.GetObjectRepr(*file_, class_type->specific_id);
  81. }
  82. auto TypeStore::GetAdaptedType(TypeId type_id) const -> TypeId {
  83. if (auto class_type = TryGetAs<ClassType>(type_id)) {
  84. return file_->classes()
  85. .Get(class_type->class_id)
  86. .GetAdaptedType(*file_, class_type->specific_id);
  87. }
  88. return TypeId::None;
  89. }
  90. auto TypeStore::GetTransitiveAdaptedType(TypeId type_id) const -> TypeId {
  91. while (true) {
  92. auto adapted_type_id = GetAdaptedType(type_id);
  93. if (!adapted_type_id.has_value()) {
  94. break;
  95. }
  96. type_id = adapted_type_id;
  97. }
  98. return type_id;
  99. }
  100. auto TypeStore::GetUnqualifiedTypeAndQualifiers(TypeId type_id) const
  101. -> std::pair<TypeId, TypeQualifiers> {
  102. TypeQualifiers quals = TypeQualifiers::None;
  103. while (true) {
  104. if (auto qualified_type = TryGetAs<AnyQualifiedType>(type_id)) {
  105. type_id = file_->types().GetTypeIdForTypeInstId(qualified_type->inner_id);
  106. switch (qualified_type->kind) {
  107. case ConstType::Kind:
  108. quals.Add(TypeQualifiers::Const);
  109. break;
  110. case MaybeUnformedType::Kind:
  111. quals.Add(TypeQualifiers::MaybeUnformed);
  112. break;
  113. case PartialType::Kind:
  114. quals.Add(TypeQualifiers::Partial);
  115. break;
  116. default:
  117. CARBON_FATAL("Unknown type qualifier {0}", qualified_type->kind);
  118. }
  119. } else {
  120. return {type_id, quals};
  121. }
  122. }
  123. }
  124. auto TypeStore::GetTransitiveUnqualifiedAdaptedType(TypeId type_id) const
  125. -> std::pair<TypeId, TypeQualifiers> {
  126. TypeQualifiers quals = TypeQualifiers::None;
  127. while (true) {
  128. type_id = GetTransitiveAdaptedType(type_id);
  129. auto [unqual_type_id, inner_quals] =
  130. GetUnqualifiedTypeAndQualifiers(type_id);
  131. if (unqual_type_id == type_id) {
  132. return {type_id, quals};
  133. }
  134. type_id = unqual_type_id;
  135. quals.Add(inner_quals);
  136. }
  137. }
  138. auto TypeStore::TryGetIntTypeInfo(TypeId int_type_id) const
  139. -> std::optional<IntTypeInfo> {
  140. auto object_repr_id = file_->types().GetObjectRepr(int_type_id);
  141. if (!object_repr_id.has_value()) {
  142. return std::nullopt;
  143. }
  144. auto inst_id = file_->types().GetTypeInstId(object_repr_id);
  145. if (inst_id == IntLiteralType::TypeInstId) {
  146. // `Core.IntLiteral` has an unknown bit-width.
  147. return TypeStore::IntTypeInfo{.is_signed = true, .bit_width = IntId::None};
  148. }
  149. auto int_type = file_->insts().TryGetAs<IntType>(inst_id);
  150. if (!int_type) {
  151. return std::nullopt;
  152. }
  153. auto bit_width_inst =
  154. file_->insts().TryGetAs<IntValue>(int_type->bit_width_id);
  155. return TypeStore::IntTypeInfo{
  156. .is_signed = int_type->int_kind.is_signed(),
  157. .bit_width = bit_width_inst ? bit_width_inst->int_id : IntId::None};
  158. }
  159. auto TypeStore::IsSignedInt(TypeId int_type_id) const -> bool {
  160. auto int_info = TryGetIntTypeInfo(int_type_id);
  161. return int_info && int_info->is_signed;
  162. }
  163. auto TypeStore::GetIntTypeInfo(TypeId int_type_id) const -> IntTypeInfo {
  164. auto int_info = TryGetIntTypeInfo(int_type_id);
  165. CARBON_CHECK(int_info, "Type {0} is not an integer type", int_type_id);
  166. return *int_info;
  167. }
  168. auto ExtractScrutineeType(const File& sem_ir, TypeId type_id) -> TypeId {
  169. if (auto pattern_type = sem_ir.types().TryGetAs<PatternType>(type_id)) {
  170. return sem_ir.types().GetTypeIdForTypeInstId(
  171. pattern_type->scrutinee_type_inst_id);
  172. }
  173. CARBON_CHECK(type_id == ErrorInst::TypeId,
  174. "Inst kind doesn't have scrutinee type: {0}",
  175. sem_ir.types().GetAsInst(type_id).kind());
  176. return type_id;
  177. }
  178. } // namespace Carbon::SemIR