impl.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_IMPL_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_IMPL_H_
  6. #include "common/map.h"
  7. #include "toolchain/base/value_store.h"
  8. #include "toolchain/sem_ir/entity_with_params_base.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::SemIR {
  11. struct ImplFields {
  12. // The following members always have values, and do not change throughout the
  13. // lifetime of the interface.
  14. // The type for which the impl is implementing a constraint.
  15. InstId self_id;
  16. // The constraint that the impl implements.
  17. InstId constraint_id;
  18. // The following members are set at the `{` of the impl definition.
  19. // The impl scope.
  20. NameScopeId scope_id = NameScopeId::None;
  21. // The first block of the impl body.
  22. // TODO: Handle control flow in the impl body, such as if-expressions.
  23. InstBlockId body_block_id = InstBlockId::None;
  24. // The witness for the impl. This can be `BuiltinErrorInst` or an import
  25. // reference. Note that the entries in the witness are updated at the end of
  26. // the impl definition.
  27. InstId witness_id = InstId::None;
  28. // The following members are set at the `}` of the impl definition.
  29. bool defined = false;
  30. };
  31. // An implementation of a constraint. See EntityWithParamsBase regarding the
  32. // inheritance here.
  33. struct Impl : public EntityWithParamsBase,
  34. public ImplFields,
  35. public Printable<Impl> {
  36. auto Print(llvm::raw_ostream& out) const -> void {
  37. out << "{self: " << self_id << ", constraint: " << constraint_id << "}";
  38. }
  39. // Determines whether this impl has been fully defined. This is false until we
  40. // reach the `}` of the impl definition.
  41. auto is_defined() const -> bool { return defined; }
  42. // Determines whether this impl's definition has begun but not yet ended.
  43. auto is_being_defined() const -> bool {
  44. return has_definition_started() && !is_defined();
  45. }
  46. };
  47. // A collection of `Impl`s, which can be accessed by the self type and
  48. // constraint implemented.
  49. class ImplStore {
  50. private:
  51. // An ID of either a single impl or a lookup bucket.
  52. class ImplOrLookupBucketId : public IdBase<ImplOrLookupBucketId> {
  53. public:
  54. static constexpr llvm::StringLiteral Label = "impl_or_lookup_bucket";
  55. // An ID with no value, corresponding to to ImplId::None.
  56. static const ImplOrLookupBucketId None;
  57. static auto ForImplId(ImplId impl_id) -> ImplOrLookupBucketId {
  58. return ImplOrLookupBucketId(impl_id.index);
  59. }
  60. static auto ForBucket(int bucket) -> ImplOrLookupBucketId {
  61. return ImplOrLookupBucketId(ImplId::NoneIndex - bucket - 1);
  62. }
  63. // Returns whether this ID represents a bucket index, rather than an ImplId.
  64. // `None` is not a bucket index.
  65. auto is_bucket() const { return index < ImplId::NoneIndex; }
  66. // Returns the bucket index represented by this ID. Requires is_bucket().
  67. auto bucket() const -> int {
  68. CARBON_CHECK(is_bucket());
  69. return ImplId::NoneIndex - index - 1;
  70. }
  71. // Returns the ImplId index represented by this ID. Requires !is_bucket().
  72. auto impl_id() const -> ImplId {
  73. CARBON_CHECK(!is_bucket());
  74. return ImplId(index);
  75. }
  76. private:
  77. explicit constexpr ImplOrLookupBucketId(int index) : IdBase(index) {}
  78. };
  79. public:
  80. // A reference to an impl lookup bucket. This represents a list of impls with
  81. // the same self and constraint type.
  82. //
  83. // The bucket is held indirectly as an `ImplOrLookupBucketId`, in one of three
  84. // states:
  85. //
  86. // - `ImplId::None` represents an empty bucket.
  87. // - An `ImplId` value represents a bucket with exactly one impl. This is
  88. // expected to be by far the most common case.
  89. // - A lookup bucket index represents an index within the `ImplStore`'s
  90. // array of variable-sized lookup buckets.
  91. class LookupBucketRef {
  92. public:
  93. LookupBucketRef(ImplStore& store, ImplOrLookupBucketId& id)
  94. : store_(&store), id_(&id), single_id_storage_(ImplId::None) {
  95. if (!id.is_bucket()) {
  96. single_id_storage_ = id.impl_id();
  97. }
  98. }
  99. auto begin() const -> const ImplId* {
  100. if (id_->is_bucket()) {
  101. return store_->lookup_buckets_[id_->bucket()].begin();
  102. }
  103. return &single_id_storage_;
  104. }
  105. auto end() const -> const ImplId* {
  106. if (id_->is_bucket()) {
  107. return store_->lookup_buckets_[id_->bucket()].end();
  108. }
  109. return &single_id_storage_ + (id_->has_value() ? 1 : 0);
  110. }
  111. // Adds an impl to this lookup bucket. Only impls from the current file and
  112. // its API file should be added in this way. Impls from other files do not
  113. // need to be found by impl redeclaration lookup so should not be added.
  114. auto push_back(ImplId impl_id) -> void {
  115. if (!id_->has_value()) {
  116. *id_ = ImplOrLookupBucketId::ForImplId(impl_id);
  117. single_id_storage_ = impl_id;
  118. } else if (!id_->is_bucket()) {
  119. auto first_id = id_->impl_id();
  120. *id_ = ImplOrLookupBucketId::ForBucket(store_->lookup_buckets_.size());
  121. store_->lookup_buckets_.push_back({first_id, impl_id});
  122. } else {
  123. store_->lookup_buckets_[id_->bucket()].push_back(impl_id);
  124. }
  125. }
  126. private:
  127. ImplStore* store_;
  128. ImplOrLookupBucketId* id_;
  129. // Storage for a single ImplId. Used to support iteration over the contents
  130. // of the bucket when it contains a single ImplId.
  131. ImplId single_id_storage_;
  132. };
  133. explicit ImplStore(File& sem_ir) : sem_ir_(sem_ir) {}
  134. // Returns a reference to the lookup bucket containing the list of impls with
  135. // this self type and constraint, or adds a new bucket if this is the first
  136. // time we've seen an impl of this kind. The lookup bucket reference remains
  137. // valid until this function is called again.
  138. auto GetOrAddLookupBucket(const Impl& impl) -> LookupBucketRef;
  139. // Adds the specified impl to the store. Does not add it to impl lookup.
  140. auto Add(Impl impl) -> ImplId { return values_.Add(impl); }
  141. // Returns a mutable value for an ID.
  142. auto Get(ImplId id) -> Impl& { return values_.Get(id); }
  143. // Returns the value for an ID.
  144. auto Get(ImplId id) const -> const Impl& { return values_.Get(id); }
  145. auto OutputYaml() const -> Yaml::OutputMapping {
  146. return values_.OutputYaml();
  147. }
  148. // Collects memory usage of members.
  149. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  150. -> void {
  151. mem_usage.Collect(MemUsage::ConcatLabel(label, "values_"), values_);
  152. mem_usage.Collect(MemUsage::ConcatLabel(label, "lookup_"), lookup_);
  153. }
  154. auto array_ref() const -> llvm::ArrayRef<Impl> { return values_.array_ref(); }
  155. auto size() const -> size_t { return values_.size(); }
  156. private:
  157. File& sem_ir_;
  158. ValueStore<ImplId> values_;
  159. Map<std::tuple<InstId, InterfaceId, SpecificId>, ImplOrLookupBucketId>
  160. lookup_;
  161. // Buckets with at least 2 entries, which will be rare; see LookupBucketRef.
  162. llvm::SmallVector<llvm::SmallVector<ImplId, 2>> lookup_buckets_;
  163. };
  164. constexpr inline ImplStore::ImplOrLookupBucketId
  165. ImplStore::ImplOrLookupBucketId::None(NoneIndex);
  166. } // namespace Carbon::SemIR
  167. #endif // CARBON_TOOLCHAIN_SEM_IR_IMPL_H_