type.cpp 6.7 KB

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