type.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #ifndef CARBON_TOOLCHAIN_SEM_IR_TYPE_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_TYPE_H_
  6. #include "llvm/ADT/BitmaskEnum.h"
  7. #include "llvm/ADT/STLExtras.h"
  8. #include "toolchain/base/shared_value_stores.h"
  9. #include "toolchain/sem_ir/constant.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/inst.h"
  12. #include "toolchain/sem_ir/type_info.h"
  13. namespace Carbon::SemIR {
  14. LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
  15. // A bitmask of type qualifiers.
  16. enum class TypeQualifiers {
  17. None = 0,
  18. Const = 1 << 0,
  19. MaybeUnformed = 1 << 1,
  20. Partial = 1 << 2,
  21. LLVM_MARK_AS_BITMASK_ENUM(Partial)
  22. };
  23. // Returns whether the type qualifier set `quals` contains `qual`.
  24. inline auto HasTypeQualifier(TypeQualifiers quals, TypeQualifiers qual)
  25. -> bool {
  26. return (quals & qual) != TypeQualifiers::None;
  27. }
  28. // Provides a ValueStore wrapper with an API specific to types.
  29. class TypeStore : public Yaml::Printable<TypeStore> {
  30. public:
  31. // Used to return information about an integer type in `GetIntTypeInfo`.
  32. struct IntTypeInfo {
  33. bool is_signed;
  34. IntId bit_width;
  35. };
  36. explicit TypeStore(File* file) : file_(file) {}
  37. // Returns the ID of the constant used to define the specified type.
  38. auto GetConstantId(TypeId type_id) const -> ConstantId {
  39. if (!type_id.has_value()) {
  40. // TODO: Investigate replacing this with a CHECK or returning `None`.
  41. return ConstantId::NotConstant;
  42. }
  43. return type_id.AsConstantId();
  44. }
  45. // Returns the type ID for a constant that is a type value, i.e. it is a value
  46. // of type `TypeType`.
  47. //
  48. // Facet values are of the same typishness as types, but are not themselves
  49. // types, so they can not be passed here. They should be converted to a type
  50. // through an `as type` conversion, that is, to a value of type `TypeType`.
  51. auto GetTypeIdForTypeConstantId(ConstantId constant_id) const -> TypeId;
  52. // Returns the type ID for an instruction whose constant value is a type
  53. // value, i.e. it is a value of type `TypeType`.
  54. //
  55. // Instructions whose values are facet values (see `FacetValue`) produce a
  56. // value of the same typishness as types, but which are themselves not types,
  57. // so they can not be passed here. They should be converted to a type through
  58. // an `as type` conversion, such as to a `FacetAccessType` instruction whose
  59. // value is of type `TypeType`.
  60. auto GetTypeIdForTypeInstId(InstId inst_id) const -> TypeId;
  61. auto GetTypeIdForTypeInstId(TypeInstId inst_id) const -> TypeId;
  62. // Converts an `InstId` to a `TypeInstId` of the same id value. This process
  63. // involves checking that the type of the instruction's value is `TypeType`,
  64. // and then this check is encoded in the type system via `TypeInstId`.
  65. auto GetAsTypeInstId(InstId inst_id) const -> TypeInstId;
  66. // Returns the ID of the instruction used to define the specified type.
  67. auto GetInstId(TypeId type_id) const -> TypeInstId;
  68. // Returns the instruction used to define the specified type.
  69. auto GetAsInst(TypeId type_id) const -> Inst;
  70. // Returns the unattached form of the given type.
  71. auto GetUnattachedType(TypeId type_id) const -> TypeId;
  72. // Converts an ArrayRef of `InstId`s to a range of `TypeInstId`s via
  73. // GetAsTypeInstId().
  74. auto GetBlockAsTypeInstIds(llvm::ArrayRef<InstId> array
  75. [[clang::lifetimebound]]) const -> auto {
  76. return llvm::map_range(array, [&](InstId type_inst_id) {
  77. return GetAsTypeInstId(type_inst_id);
  78. });
  79. }
  80. // Converts an ArrayRef of `InstId`s to a range of `TypeId`s via
  81. // GetTypeIdForTypeInstId().
  82. auto GetBlockAsTypeIds(llvm::ArrayRef<InstId> array
  83. [[clang::lifetimebound]]) const -> auto {
  84. return llvm::map_range(array, [&](InstId type_inst_id) {
  85. return GetTypeIdForTypeInstId(type_inst_id);
  86. });
  87. }
  88. // Returns whether the specified kind of instruction was used to define the
  89. // type.
  90. template <typename InstT>
  91. auto Is(TypeId type_id) const -> bool {
  92. return GetAsInst(type_id).Is<InstT>();
  93. }
  94. // Returns the instruction used to define the specified type, which is known
  95. // to be a particular kind of instruction.
  96. template <typename InstT>
  97. auto GetAs(TypeId type_id) const -> InstT {
  98. return GetAsInst(type_id).As<InstT>();
  99. }
  100. // Returns the instruction used to define the specified type, if it is of a
  101. // particular kind.
  102. template <typename InstT>
  103. auto TryGetAs(TypeId type_id) const -> std::optional<InstT> {
  104. return GetAsInst(type_id).TryAs<InstT>();
  105. }
  106. // Returns whether two type IDs represent the same type. This includes the
  107. // case where they might be in different generics and thus might have
  108. // different ConstantIds, but are still symbolically equal.
  109. auto AreEqualAcrossDeclarations(TypeId a, TypeId b) const -> bool {
  110. return GetInstId(a) == GetInstId(b);
  111. }
  112. // Gets the value representation to use for a type. This returns an
  113. // `None` type if the given type is not complete.
  114. auto GetValueRepr(TypeId type_id) const -> ValueRepr {
  115. if (auto type_info = complete_type_info_.Lookup(type_id)) {
  116. return type_info.value().value_repr;
  117. }
  118. return {.kind = ValueRepr::Unknown};
  119. }
  120. // Gets the `CompleteTypeInfo` for a type, with an empty value if the type is
  121. // not complete.
  122. auto GetCompleteTypeInfo(TypeId type_id) const -> CompleteTypeInfo {
  123. if (auto type_info = complete_type_info_.Lookup(type_id)) {
  124. return type_info.value();
  125. }
  126. return {.value_repr = {.kind = ValueRepr::Unknown}};
  127. }
  128. // Sets the `CompleteTypeInfo` associated with a type, marking it as complete.
  129. // This can be used with abstract types.
  130. auto SetComplete(TypeId type_id, const CompleteTypeInfo& info) -> void {
  131. CARBON_CHECK(info.value_repr.kind != ValueRepr::Unknown);
  132. auto insert_info = complete_type_info_.Insert(type_id, info);
  133. CARBON_CHECK(insert_info.is_inserted(), "Type {0} completed more than once",
  134. type_id);
  135. complete_types_.push_back(type_id);
  136. CARBON_CHECK(IsComplete(type_id));
  137. }
  138. // Get the object representation associated with a type. For a non-class type,
  139. // this is the type itself. `None` is returned if the object representation
  140. // cannot be determined because the type is not complete.
  141. auto GetObjectRepr(TypeId type_id) const -> TypeId;
  142. // Get the type that the given type adapts, or `None` if the type is not known
  143. // to be an adapter, including the case where the type is an incomplete class.
  144. auto GetAdaptedType(TypeId type_id) const -> TypeId;
  145. // Returns the non-adapter type that is compatible with the specified type.
  146. auto GetTransitiveAdaptedType(TypeId type_id) const -> TypeId;
  147. // Determines whether the given type is known to be complete. This does not
  148. // determine whether the type could be completed, only whether it has been.
  149. auto IsComplete(TypeId type_id) const -> bool {
  150. return complete_type_info_.Contains(type_id);
  151. }
  152. // Removes any top-level qualifiers from a type.
  153. auto GetUnqualifiedType(TypeId type_id) const -> TypeId {
  154. return GetUnqualifiedTypeAndQualifiers(type_id).first;
  155. }
  156. // Removes any top-level qualifiers from a type and returns the unqualified
  157. // type and qualifiers.
  158. auto GetUnqualifiedTypeAndQualifiers(TypeId type_id) const
  159. -> std::pair<TypeId, TypeQualifiers>;
  160. // Returns the non-adapter unqualified type that is compatible with the
  161. // specified type.
  162. auto GetTransitiveUnqualifiedAdaptedType(TypeId type_id) const
  163. -> std::pair<TypeId, TypeQualifiers>;
  164. // Determines whether the given type is a signed integer type. This includes
  165. // the case where the type is `Core.IntLiteral` or a class type whose object
  166. // representation is a signed integer type.
  167. auto IsSignedInt(TypeId int_type_id) const -> bool;
  168. // Returns integer type information from a type ID that is known to represent
  169. // an integer type. Abstracts away the difference between an `IntType`
  170. // instruction defined type, a singleton instruction defined type, and a class
  171. // adapting such a type. Uses IntId::None for types that have a
  172. // non-constant width and for IntLiteral.
  173. auto GetIntTypeInfo(TypeId int_type_id) const -> IntTypeInfo;
  174. // Similar to `GetIntTypeInfo`, except allows non-`IntType` types to be
  175. // handled.
  176. auto TryGetIntTypeInfo(TypeId int_type_id) const
  177. -> std::optional<IntTypeInfo>;
  178. // Returns whether `type_id` represents a facet type.
  179. auto IsFacetType(TypeId type_id) const -> bool {
  180. return type_id == TypeType::TypeId || Is<FacetType>(type_id);
  181. }
  182. // Returns a list of types that were completed in this file, in the order in
  183. // which they were completed. Earlier types in this list cannot contain
  184. // instances of later types.
  185. auto complete_types() const -> llvm::ArrayRef<TypeId> {
  186. return complete_types_;
  187. }
  188. auto OutputYaml() const -> Yaml::OutputMapping {
  189. return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  190. for (auto type_id : complete_types_) {
  191. auto info = GetCompleteTypeInfo(type_id);
  192. map.Add(PrintToString(type_id),
  193. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map2) {
  194. map2.Add("value_repr", Yaml::OutputScalar(info.value_repr));
  195. if (info.abstract_class_id.has_value()) {
  196. map2.Add("abstract_class_id",
  197. Yaml::OutputScalar(info.abstract_class_id));
  198. }
  199. }));
  200. }
  201. });
  202. }
  203. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  204. -> void {
  205. mem_usage.Collect(MemUsage::ConcatLabel(label, "complete_type_info_"),
  206. complete_type_info_);
  207. mem_usage.Collect(MemUsage::ConcatLabel(label, "complete_types_"),
  208. complete_types_);
  209. }
  210. private:
  211. File* file_;
  212. Map<TypeId, CompleteTypeInfo> complete_type_info_;
  213. llvm::SmallVector<TypeId> complete_types_;
  214. };
  215. // Returns the scrutinee type of `type_id`, which must be a `PatternType`.
  216. auto ExtractScrutineeType(const File& sem_ir, SemIR::TypeId type_id)
  217. -> SemIR::TypeId;
  218. } // namespace Carbon::SemIR
  219. #endif // CARBON_TOOLCHAIN_SEM_IR_TYPE_H_