type.cpp 6.0 KB

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