impl.h 6.9 KB

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