type.cpp 6.1 KB

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