type.h 12 KB

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