generic.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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::Invalid;
  42. // The eval block for the definition region of the generic.
  43. InstBlockId definition_block_id = InstBlockId::Invalid;
  44. };
  45. // Provides storage for generics.
  46. class GenericStore : public ValueStore<GenericId> {
  47. public:
  48. // Get the self specific for a generic, or an invalid specific for an invalid
  49. // generic ID.
  50. auto GetSelfSpecific(GenericId id) -> SpecificId {
  51. return id.is_valid() ? Get(id).self_specific_id : SpecificId::Invalid;
  52. }
  53. };
  54. // A specific, which is the combination of a generic and specified generic
  55. // arguments. For each construct that depends on a compile-time parameter in the
  56. // generic entity, this contains the corresponding specific value. This includes
  57. // values for the compile-time parameters themselves.
  58. struct Specific : Printable<Specific> {
  59. auto Print(llvm::raw_ostream& out) const -> void {
  60. out << "{generic: " << generic_id << ", args: " << args_id << "}";
  61. }
  62. // Returns the value block for this region of the specific. This is a block
  63. // containing values and instructions produced by evaluating the corresponding
  64. // eval block of the generic within the context of this specific. These are
  65. // the constant values and types and the instantiated template-dependent
  66. // instructions that are used in this region of the specific.
  67. auto GetValueBlock(GenericInstIndex::Region region) const -> InstBlockId {
  68. return region == GenericInstIndex::Region::Declaration
  69. ? decl_block_id
  70. : definition_block_id;
  71. }
  72. // The generic that this is a specific version of.
  73. GenericId generic_id;
  74. // Argument values, corresponding to the bindings in `Generic::bindings_id`.
  75. InstBlockId args_id;
  76. // The following members are set when the corresponding region of the specific
  77. // is resolved.
  78. // The value block for the declaration region of the specific.
  79. InstBlockId decl_block_id = InstBlockId::Invalid;
  80. // The value block for the definition region of the specific.
  81. InstBlockId definition_block_id = InstBlockId::Invalid;
  82. };
  83. // Provides storage for deduplicated specifics, which represent generics plus
  84. // their associated generic argument list.
  85. class SpecificStore : public Yaml::Printable<SpecificStore> {
  86. public:
  87. // Adds a new specific, or gets the existing specific for a specified generic
  88. // and argument list. Returns the ID of the specific. The argument IDs must be
  89. // for instructions in the constant block, and must be a canonical instruction
  90. // block ID.
  91. auto GetOrAdd(GenericId generic_id, InstBlockId args_id) -> SpecificId;
  92. // Gets the specific with the given ID.
  93. auto Get(SpecificId specific_id) const -> const Specific& {
  94. return specifics_.Get(specific_id);
  95. }
  96. // Gets the specific with the given ID.
  97. auto Get(SpecificId specific_id) -> Specific& {
  98. return specifics_.Get(specific_id);
  99. }
  100. // These are to support printable structures, and are not guaranteed.
  101. auto OutputYaml() const -> Yaml::OutputMapping {
  102. return specifics_.OutputYaml();
  103. }
  104. // Collects memory usage of members.
  105. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  106. -> void;
  107. auto array_ref() const -> llvm::ArrayRef<Specific> {
  108. return specifics_.array_ref();
  109. }
  110. auto size() const -> size_t { return specifics_.size(); }
  111. private:
  112. // Context for hashing keys.
  113. class KeyContext;
  114. ValueStore<SpecificId> specifics_;
  115. Carbon::Set<SpecificId, 0, KeyContext> lookup_table_;
  116. };
  117. // Gets the substituted value of a potentially generic constant within a
  118. // specific. Note that this does not perform substitution, and will return
  119. // `Invalid` if the substituted constant value is not yet known.
  120. auto GetConstantInSpecific(const File& sem_ir, SpecificId specific_id,
  121. ConstantId const_id) -> ConstantId;
  122. // Gets the substituted constant value of a potentially generic instruction
  123. // within a specific. Note that this does not perform substitution, and will
  124. // return `Invalid` if the substituted constant value is not yet known.
  125. auto GetConstantValueInSpecific(const File& sem_ir, SpecificId specific_id,
  126. InstId inst_id) -> ConstantId;
  127. // Gets the substituted value of a potentially generic type within a specific.
  128. // Note that this does not perform substitution, and will return `Invalid` if
  129. // the substituted type is not yet known.
  130. auto GetTypeInSpecific(const File& sem_ir, SpecificId specific_id,
  131. TypeId type_id) -> TypeId;
  132. } // namespace Carbon::SemIR
  133. #endif // CARBON_TOOLCHAIN_SEM_IR_GENERIC_H_