type.h 9.7 KB

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