generic.h 7.8 KB

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