generic.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/sem_ir/ids.h"
  8. namespace Carbon::SemIR {
  9. // Information for a generic entity, such as a generic class, a generic
  10. // interface, or generic function.
  11. //
  12. // Note that this includes both checked generics and template generics.
  13. struct Generic : public Printable<Generic> {
  14. auto Print(llvm::raw_ostream& out) const -> void {
  15. out << "{decl: " << decl_id << ", bindings: " << bindings_id << "}";
  16. }
  17. // Returns the eval block for the specified region of the generic. This is a
  18. // block of instructions that should be evaluated to compute the values and
  19. // instructions needed by that region of the generic.
  20. auto GetEvalBlock(GenericInstIndex::Region region) const -> InstBlockId {
  21. return region == GenericInstIndex::Region::Declaration
  22. ? decl_block_id
  23. : definition_block_id;
  24. }
  25. // The following members always have values, and do not change throughout the
  26. // lifetime of the generic.
  27. // The first declaration of the generic entity.
  28. InstId decl_id;
  29. // A block containing the IDs of compile time bindings in this generic scope.
  30. // The index in this block will match the `bind_index` in the name binding
  31. // instruction's `EntityName`.
  32. InstBlockId bindings_id;
  33. // The self specific of this generic, which is a specific where every generic
  34. // parameter's argument is that same parameter. For example, the self specific
  35. // of `Vector(T:! type)` is `Vector(T)`.
  36. SpecificId self_specific_id;
  37. // The following members are set at the end of the corresponding region of the
  38. // generic.
  39. // The eval block for the declaration region of the generic.
  40. InstBlockId decl_block_id = InstBlockId::Invalid;
  41. // The eval block for the definition region of the generic.
  42. InstBlockId definition_block_id = InstBlockId::Invalid;
  43. };
  44. // Provides storage for generics.
  45. class GenericStore : public ValueStore<GenericId> {
  46. public:
  47. // Get the self specific for a generic, or an invalid specific for an invalid
  48. // generic ID.
  49. auto GetSelfSpecific(GenericId id) -> SpecificId {
  50. return id.is_valid() ? Get(id).self_specific_id : SpecificId::Invalid;
  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 the value block for this region of the specific. This is a block
  62. // containing values and instructions produced by evaluating the corresponding
  63. // eval block of the generic within the context of this specific. These are
  64. // the constant values and types and the instantiated template-dependent
  65. // instructions that are used in this region of the specific.
  66. auto GetValueBlock(GenericInstIndex::Region region) const -> InstBlockId {
  67. return region == GenericInstIndex::Region::Declaration
  68. ? decl_block_id
  69. : definition_block_id;
  70. }
  71. // The generic that this is a specific version of.
  72. GenericId generic_id;
  73. // Argument values, corresponding to the bindings in `Generic::bindings_id`.
  74. InstBlockId args_id;
  75. // The following members are set when the corresponding region of the specific
  76. // is resolved.
  77. // The value block for the declaration region of the specific.
  78. InstBlockId decl_block_id = InstBlockId::Invalid;
  79. // The value block for the definition region of the specific.
  80. InstBlockId definition_block_id = InstBlockId::Invalid;
  81. };
  82. // Provides storage for deduplicated specifics, which represent generics plus
  83. // their associated generic argument list.
  84. class SpecificStore : public Yaml::Printable<SpecificStore> {
  85. public:
  86. // Adds a new specific, or gets the existing specific for a specified generic
  87. // and argument list. Returns the ID of the specific. The argument IDs must be
  88. // for instructions in the constant block, and must be a canonical instruction
  89. // block ID.
  90. auto GetOrAdd(GenericId generic_id, InstBlockId args_id) -> SpecificId;
  91. // Gets the specific with the given ID.
  92. auto Get(SpecificId specific_id) const -> const Specific& {
  93. return specifics_.Get(specific_id);
  94. }
  95. // Gets the specific with the given ID.
  96. auto Get(SpecificId specific_id) -> Specific& {
  97. return specifics_.Get(specific_id);
  98. }
  99. // These are to support printable structures, and are not guaranteed.
  100. auto OutputYaml() const -> Yaml::OutputMapping {
  101. return specifics_.OutputYaml();
  102. }
  103. // Collects memory usage of members.
  104. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  105. -> void;
  106. auto array_ref() const -> llvm::ArrayRef<Specific> {
  107. return specifics_.array_ref();
  108. }
  109. auto size() const -> size_t { return specifics_.size(); }
  110. private:
  111. // Context for hashing keys.
  112. class KeyContext;
  113. ValueStore<SpecificId> specifics_;
  114. Carbon::Set<SpecificId, 0, KeyContext> lookup_table_;
  115. };
  116. // Gets the substituted value of a potentially generic constant within a
  117. // specific. Note that this does not perform substitution, and will return
  118. // `Invalid` if the substituted constant value is not yet known.
  119. auto GetConstantInSpecific(const File& sem_ir, SpecificId specific_id,
  120. ConstantId const_id) -> ConstantId;
  121. // Gets the substituted constant value of a potentially generic instruction
  122. // within a specific. Note that this does not perform substitution, and will
  123. // return `Invalid` if the substituted constant value is not yet known.
  124. auto GetConstantValueInSpecific(const File& sem_ir, SpecificId specific_id,
  125. InstId inst_id) -> ConstantId;
  126. // Gets the substituted value of a potentially generic type within a specific.
  127. // Note that this does not perform substitution, and will return `Invalid` if
  128. // the substituted type is not yet known.
  129. auto GetTypeInSpecific(const File& sem_ir, SpecificId specific_id,
  130. TypeId type_id) -> TypeId;
  131. } // namespace Carbon::SemIR
  132. #endif // CARBON_TOOLCHAIN_SEM_IR_GENERIC_H_