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