generic.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_GENERIC_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_GENERIC_H_
  6. #include "common/set.h"
  7. #include "toolchain/base/value_store.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::SemIR {
  10. // Information for a generic entity, such as a generic class, a generic
  11. // interface, or generic function.
  12. //
  13. // Note that this includes both checked generics and template generics.
  14. struct Generic : public Printable<Generic> {
  15. auto Print(llvm::raw_ostream& out) const -> void {
  16. out << "{decl: " << decl_id << ", bindings: " << bindings_id
  17. << ", self_specific_id: " << self_specific_id
  18. << ", decl_block_id: " << decl_block_id
  19. << ", definition_block_id: " << definition_block_id << "}";
  20. }
  21. // Returns the eval block for the specified region of the generic. This is a
  22. // block of instructions that should be evaluated to compute the values and
  23. // instructions needed by that region of the generic.
  24. auto GetEvalBlock(GenericInstIndex::Region region) const -> InstBlockId {
  25. return region == GenericInstIndex::Region::Declaration
  26. ? decl_block_id
  27. : definition_block_id;
  28. }
  29. // The following members always have values, and do not change throughout the
  30. // lifetime of the generic.
  31. // The first declaration of the generic entity.
  32. InstId decl_id;
  33. // A block containing the IDs of compile time bindings in this generic scope.
  34. // The index in this block will match the `bind_index` in the name binding
  35. // instruction's `EntityName`.
  36. InstBlockId bindings_id;
  37. // The self specific of this generic, which is a specific where every generic
  38. // parameter's argument is that same parameter. For example, the self specific
  39. // of `Vector(T:! type)` is `Vector(T)`.
  40. SpecificId self_specific_id;
  41. // The following members are set at the end of the corresponding region of the
  42. // generic.
  43. // The eval block for the declaration region of the generic.
  44. InstBlockId decl_block_id = InstBlockId::None;
  45. // The eval block for the definition region of the generic.
  46. InstBlockId definition_block_id = InstBlockId::None;
  47. };
  48. // Provides storage for generics.
  49. class GenericStore : public ValueStore<GenericId, Generic, Tag<CheckIRId>> {
  50. public:
  51. using ValueStore::ValueStore;
  52. // Get the self specific for a generic, or `None` if the `id` is `None`.
  53. auto GetSelfSpecific(GenericId id) const -> SpecificId {
  54. return id.has_value() ? Get(id).self_specific_id : SpecificId::None;
  55. }
  56. };
  57. // A specific, which is the combination of a generic and specified generic
  58. // arguments. For each construct that depends on a compile-time parameter in the
  59. // generic entity, this contains the corresponding specific value. This includes
  60. // values for the compile-time parameters themselves.
  61. struct Specific : Printable<Specific> {
  62. auto Print(llvm::raw_ostream& out) const -> void {
  63. auto print_block = [&](llvm::StringLiteral region, InstBlockId id,
  64. bool has_error) {
  65. out << ", " << region << "_block_id: " << id << ", " << region
  66. << "_has_error: " << has_error;
  67. };
  68. out << "{generic: " << generic_id << ", args: " << args_id;
  69. print_block("decl", decl_block_id, decl_block_has_error);
  70. print_block("definition", definition_block_id, definition_block_has_error);
  71. out << "}";
  72. }
  73. // Returns true if this specific has never been resolved. Such specifics are
  74. // used to track non-canonical argument values, for example in a non-canonical
  75. // `ClassType` that describes how the arguments to the class were written.
  76. auto IsUnresolved() const -> bool { return !decl_block_id.has_value(); }
  77. // Returns the value block for this region of the specific. This is a block
  78. // containing values and instructions produced by evaluating the corresponding
  79. // eval block of the generic within the context of this specific. These are
  80. // the constant values and types and the instantiated template-dependent
  81. // instructions that are used in this region of the specific. Each inst in
  82. // the value block corresponds to the inst in the corresponding eval block
  83. // with the same index.
  84. auto GetValueBlock(GenericInstIndex::Region region) const -> InstBlockId {
  85. return region == GenericInstIndex::Region::Declaration
  86. ? decl_block_id
  87. : definition_block_id;
  88. }
  89. // Returns whether either block has an error.
  90. auto HasError() const -> bool {
  91. return decl_block_has_error || definition_block_has_error;
  92. }
  93. // The generic that this is a specific version of.
  94. GenericId generic_id;
  95. // Argument values, corresponding to the bindings in `Generic::bindings_id`.
  96. InstBlockId args_id;
  97. // The following members are set when the corresponding region of the specific
  98. // is resolved.
  99. // The value block for the declaration region of the specific.
  100. InstBlockId decl_block_id = InstBlockId::None;
  101. // The value block for the definition region of the specific.
  102. InstBlockId definition_block_id = InstBlockId::None;
  103. // Whether the corresponding block contains an error. These are stored
  104. // directly on Specific so that they pack together.
  105. bool decl_block_has_error = false;
  106. bool definition_block_has_error = false;
  107. };
  108. // Provides storage for deduplicated specifics, which represent generics plus
  109. // their associated generic argument list.
  110. class SpecificStore : public Yaml::Printable<SpecificStore> {
  111. public:
  112. using IdType = SpecificId;
  113. using ValueStore = ValueStore<SpecificId, Specific, Tag<CheckIRId>>;
  114. explicit SpecificStore(CheckIRId check_ir_id) : specifics_(check_ir_id) {}
  115. // Adds a new specific, or gets the existing specific for a specified generic
  116. // and argument list. Returns the ID of the specific. The argument IDs must be
  117. // for instructions in the constant block, and must be a canonical instruction
  118. // block ID.
  119. auto GetOrAdd(GenericId generic_id, InstBlockId args_id) -> SpecificId;
  120. // Gets the specific with the given ID.
  121. auto Get(SpecificId specific_id) const -> const Specific& {
  122. return specifics_.Get(specific_id);
  123. }
  124. // Gets the specific with the given ID.
  125. auto Get(SpecificId specific_id) -> Specific& {
  126. return specifics_.Get(specific_id);
  127. }
  128. // Gets the arguments of the specified specific, or `Empty` if `None` is
  129. // passed.
  130. auto GetArgsOrEmpty(SpecificId specific_id) const -> InstBlockId {
  131. return specific_id.has_value() ? Get(specific_id).args_id
  132. : InstBlockId::Empty;
  133. }
  134. // These are to support printable structures, and are not guaranteed.
  135. auto OutputYaml() const -> Yaml::OutputMapping {
  136. return specifics_.OutputYaml();
  137. }
  138. // Collects memory usage of members.
  139. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  140. -> void;
  141. auto values() const [[clang::lifetimebound]] -> ValueStore::Range {
  142. return specifics_.values();
  143. }
  144. auto size() const -> size_t { return specifics_.size(); }
  145. auto enumerate() const [[clang::lifetimebound]] -> auto {
  146. return specifics_.enumerate();
  147. }
  148. auto GetIdTag() const { return specifics_.GetIdTag(); }
  149. private:
  150. // Context for hashing keys.
  151. class KeyContext;
  152. ValueStore specifics_;
  153. Carbon::Set<SpecificId, 0, KeyContext> lookup_table_;
  154. };
  155. // Gets the substituted constant value of a potentially generic instruction
  156. // within a specific. Note that this does not perform substitution, and will
  157. // return `None` if the substituted constant value is not yet known.
  158. auto GetConstantValueInSpecific(const File& sem_ir, SpecificId specific_id,
  159. InstId inst_id) -> ConstantId;
  160. // Gets the substituted constant value of a potentially generic instruction
  161. // within a specific, where the generic instruction and the specific may be in
  162. // different files. Returns the file in which the constant value was found and
  163. // the constant ID in that file.
  164. auto GetConstantValueInSpecific(const File& specific_ir, SpecificId specific_id,
  165. const File& inst_ir, InstId inst_id)
  166. -> std::pair<const File*, ConstantId>;
  167. // Gets the substituted type of an instruction within a specific. Note that this
  168. // does not perform substitution, and will return `None` if the substituted type
  169. // is not yet known.
  170. auto GetTypeOfInstInSpecific(const File& sem_ir, SpecificId specific_id,
  171. InstId inst_id) -> TypeId;
  172. // Gets the substituted type of a potentially generic instruction within a
  173. // specific, where the generic instruction and the specific may be in different
  174. // files. Returns the file in which the type was found and the type ID in that
  175. // file.
  176. auto GetTypeOfInstInSpecific(const File& specific_ir, SpecificId specific_id,
  177. const File& inst_ir, InstId inst_id)
  178. -> std::pair<const File*, TypeId>;
  179. } // namespace Carbon::SemIR
  180. #endif // CARBON_TOOLCHAIN_SEM_IR_GENERIC_H_