type.cpp 6.1 KB

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