type.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "toolchain/base/shared_value_stores.h"
  7. #include "toolchain/sem_ir/constant.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/inst.h"
  10. #include "toolchain/sem_ir/type_info.h"
  11. namespace Carbon::SemIR {
  12. // Provides a ValueStore wrapper with an API specific to types.
  13. class TypeStore : public Yaml::Printable<TypeStore> {
  14. public:
  15. // Used to return information about an integer type in `GetIntTypeInfo`.
  16. struct IntTypeInfo {
  17. bool is_signed;
  18. IntId bit_width;
  19. };
  20. explicit TypeStore(File* file) : file_(file) {}
  21. // Returns the ID of the constant used to define the specified type.
  22. auto GetConstantId(TypeId type_id) const -> ConstantId {
  23. if (!type_id.has_value()) {
  24. // TODO: Investigate replacing this with a CHECK or returning `None`.
  25. return ConstantId::NotConstant;
  26. }
  27. return type_id.AsConstantId();
  28. }
  29. // Returns the type ID for a constant that is a type value, i.e. it is a value
  30. // of type `TypeType`.
  31. //
  32. // Facet values are of the same typishness as types, but are not themselves
  33. // types, so they can not be passed here. They should be converted to a type
  34. // through an `as type` conversion, that is, to a value of type `TypeType`.
  35. auto GetTypeIdForTypeConstantId(SemIR::ConstantId constant_id) const
  36. -> SemIR::TypeId;
  37. // Returns the type ID for an instruction whose constant value is a type
  38. // value, i.e. it is a value of type `TypeType`.
  39. //
  40. // Instructions whose values are facet values (see `FacetValue`) produce a
  41. // value of the same typishness as types, but which are themselves not types,
  42. // so they can not be passed here. They should be converted to a type through
  43. // an `as type` conversion, such as to a `FacetAccessType` instruction whose
  44. // value is of type `TypeType`.
  45. auto GetTypeIdForTypeInstId(SemIR::InstId inst_id) const -> SemIR::TypeId;
  46. // Returns the ID of the instruction used to define the specified type.
  47. auto GetInstId(TypeId type_id) const -> InstId;
  48. // Returns the instruction used to define the specified type.
  49. auto GetAsInst(TypeId type_id) const -> Inst;
  50. // Returns whether the specified kind of instruction was used to define the
  51. // type.
  52. template <typename InstT>
  53. auto Is(TypeId type_id) const -> bool {
  54. return GetAsInst(type_id).Is<InstT>();
  55. }
  56. // Returns the instruction used to define the specified type, which is known
  57. // to be a particular kind of instruction.
  58. template <typename InstT>
  59. auto GetAs(TypeId type_id) const -> InstT {
  60. return GetAsInst(type_id).As<InstT>();
  61. }
  62. // Returns the instruction used to define the specified type, if it is of a
  63. // particular kind.
  64. template <typename InstT>
  65. auto TryGetAs(TypeId type_id) const -> std::optional<InstT> {
  66. return GetAsInst(type_id).TryAs<InstT>();
  67. }
  68. // Returns whether two type IDs represent the same type. This includes the
  69. // case where they might be in different generics and thus might have
  70. // different ConstantIds, but are still symbolically equal.
  71. auto AreEqualAcrossDeclarations(TypeId a, TypeId b) const -> bool {
  72. return GetInstId(a) == GetInstId(b);
  73. }
  74. // Gets the value representation to use for a type. This returns an
  75. // `None` type if the given type is not complete.
  76. auto GetValueRepr(TypeId type_id) const -> ValueRepr {
  77. if (auto type_info = complete_type_info_.Lookup(type_id)) {
  78. return type_info.value().value_repr;
  79. }
  80. return {.kind = ValueRepr::Unknown};
  81. }
  82. // Gets the `CompleteTypeInfo` for a type, with an empty value if the type is
  83. // not complete.
  84. auto GetCompleteTypeInfo(TypeId type_id) const -> CompleteTypeInfo {
  85. if (auto type_info = complete_type_info_.Lookup(type_id)) {
  86. return type_info.value();
  87. }
  88. return {.value_repr = {.kind = ValueRepr::Unknown}};
  89. }
  90. // Sets the `CompleteTypeInfo` associated with a type, marking it as complete.
  91. // This can be used with abstract types.
  92. auto SetComplete(TypeId type_id, const CompleteTypeInfo& info) -> void {
  93. CARBON_CHECK(info.value_repr.kind != ValueRepr::Unknown);
  94. auto insert_info = complete_type_info_.Insert(type_id, info);
  95. CARBON_CHECK(insert_info.is_inserted(), "Type {0} completed more than once",
  96. type_id);
  97. complete_types_.push_back(type_id);
  98. CARBON_CHECK(IsComplete(type_id));
  99. }
  100. // Get the object representation associated with a type. For a non-class type,
  101. // this is the type itself. `None` is returned if the object representation
  102. // cannot be determined because the type is not complete.
  103. auto GetObjectRepr(TypeId type_id) const -> TypeId;
  104. // Determines whether the given type is known to be complete. This does not
  105. // determine whether the type could be completed, only whether it has been.
  106. auto IsComplete(TypeId type_id) const -> bool {
  107. return complete_type_info_.Contains(type_id);
  108. }
  109. // Removes any top-level `const` qualifiers from a type.
  110. auto GetUnqualifiedType(TypeId type_id) const -> TypeId;
  111. // Determines whether the given type is a signed integer type. This includes
  112. // the case where the type is `Core.IntLiteral` or a class type whose object
  113. // representation is a signed integer type.
  114. auto IsSignedInt(TypeId int_type_id) const -> bool;
  115. // Returns integer type information from a type ID that is known to represent
  116. // an integer type. Abstracts away the difference between an `IntType`
  117. // instruction defined type, a singleton instruction defined type, and a class
  118. // adapting such a type. Uses IntId::None for types that have a
  119. // non-constant width and for IntLiteral.
  120. auto GetIntTypeInfo(TypeId int_type_id) const -> IntTypeInfo;
  121. // Returns whether `type_id` represents a facet type.
  122. auto IsFacetType(SemIR::TypeId type_id) const -> bool {
  123. return type_id == SemIR::TypeType::SingletonTypeId ||
  124. Is<SemIR::FacetType>(type_id);
  125. }
  126. // Returns a list of types that were completed in this file, in the order in
  127. // which they were completed. Earlier types in this list cannot contain
  128. // instances of later types.
  129. auto complete_types() const -> llvm::ArrayRef<TypeId> {
  130. return complete_types_;
  131. }
  132. auto OutputYaml() const -> Yaml::OutputMapping {
  133. return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  134. for (auto type_id : complete_types_) {
  135. auto info = GetCompleteTypeInfo(type_id);
  136. map.Add(PrintToString(type_id),
  137. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map2) {
  138. map2.Add("value_repr", Yaml::OutputScalar(info.value_repr));
  139. if (info.abstract_class_id.has_value()) {
  140. map2.Add("abstract_class_id",
  141. Yaml::OutputScalar(info.abstract_class_id));
  142. }
  143. }));
  144. }
  145. });
  146. }
  147. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  148. -> void {
  149. mem_usage.Collect(MemUsage::ConcatLabel(label, "complete_type_info_"),
  150. complete_type_info_);
  151. mem_usage.Collect(MemUsage::ConcatLabel(label, "complete_types_"),
  152. complete_types_);
  153. }
  154. private:
  155. File* file_;
  156. Map<TypeId, CompleteTypeInfo> complete_type_info_;
  157. llvm::SmallVector<TypeId> complete_types_;
  158. };
  159. } // namespace Carbon::SemIR
  160. #endif // CARBON_TOOLCHAIN_SEM_IR_TYPE_H_