impl.h 6.9 KB

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