https://github.com/hahnjo created https://github.com/llvm/llvm-project/pull/219188
It is only used there. >From 476c3bfa006b8d64abcf51cd44e3564eaa866eee Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld <[email protected]> Date: Mon, 24 Aug 2026 14:26:40 +0200 Subject: [PATCH] [clang] Move LazyVector to Sema It is only used there. --- clang/include/clang/AST/ExternalASTSource.h | 84 ------------------- clang/include/clang/Sema/ExternalSemaSource.h | 79 +++++++++++++++++ clang/include/clang/Sema/Sema.h | 12 +-- 3 files changed, 85 insertions(+), 90 deletions(-) diff --git a/clang/include/clang/AST/ExternalASTSource.h b/clang/include/clang/AST/ExternalASTSource.h index be88309969715..92d962ed69d86 100644 --- a/clang/include/clang/AST/ExternalASTSource.h +++ b/clang/include/clang/AST/ExternalASTSource.h @@ -537,90 +537,6 @@ struct PointerLikeTypeTraits< namespace clang { -/// Represents a lazily-loaded vector of data. -/// -/// The lazily-loaded vector of data contains data that is partially loaded -/// from an external source and partially added by local translation. The -/// items loaded from the external source are loaded lazily, when needed for -/// iteration over the complete vector. -template<typename T, typename Source, - void (Source::*Loader)(SmallVectorImpl<T>&), - unsigned LoadedStorage = 2, unsigned LocalStorage = 4> -class LazyVector { - SmallVector<T, LoadedStorage> Loaded; - SmallVector<T, LocalStorage> Local; - -public: - /// Iteration over the elements in the vector. - /// - /// In a complete iteration, the iterator walks the range [-M, N), - /// where negative values are used to indicate elements - /// loaded from the external source while non-negative values are used to - /// indicate elements added via \c push_back(). - /// However, to provide iteration in source order (for, e.g., chained - /// precompiled headers), dereferencing the iterator flips the negative - /// values (corresponding to loaded entities), so that position -M - /// corresponds to element 0 in the loaded entities vector, position -M+1 - /// corresponds to element 1 in the loaded entities vector, etc. This - /// gives us a reasonably efficient, source-order walk. - /// - /// We define this as a wrapping iterator around an int. The - /// iterator_adaptor_base class forwards the iterator methods to basic integer - /// arithmetic. - class iterator - : public llvm::iterator_adaptor_base< - iterator, int, std::random_access_iterator_tag, T, int, T *, T &> { - friend class LazyVector; - - LazyVector *Self; - - iterator(LazyVector *Self, int Position) - : iterator::iterator_adaptor_base(Position), Self(Self) {} - - bool isLoaded() const { return this->I < 0; } - - public: - iterator() : iterator(nullptr, 0) {} - - typename iterator::reference operator*() const { - if (isLoaded()) - return Self->Loaded.end()[this->I]; - return Self->Local.begin()[this->I]; - } - }; - - iterator begin(Source *source, bool LocalOnly = false) { - if (LocalOnly) - return iterator(this, 0); - - if (source) - (source->*Loader)(Loaded); - return iterator(this, -(int)Loaded.size()); - } - - iterator end() { - return iterator(this, Local.size()); - } - - void push_back(const T& LocalValue) { - Local.push_back(LocalValue); - } - - void erase(iterator From, iterator To) { - if (From.isLoaded() && To.isLoaded()) { - Loaded.erase(&*From, &*To); - return; - } - - if (From.isLoaded()) { - Loaded.erase(&*From, Loaded.end()); - From = begin(nullptr, true); - } - - Local.erase(&*From, &*To); - } -}; - /// A lazy pointer to a statement. using LazyDeclStmtPtr = LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>; diff --git a/clang/include/clang/Sema/ExternalSemaSource.h b/clang/include/clang/Sema/ExternalSemaSource.h index 03e8e8a66d148..0122beff53df9 100644 --- a/clang/include/clang/Sema/ExternalSemaSource.h +++ b/clang/include/clang/Sema/ExternalSemaSource.h @@ -249,6 +249,85 @@ class ExternalSemaSource : public ExternalASTSource { /// \} }; +/// Represents a lazily-loaded vector of data. +/// +/// The lazily-loaded vector of data contains data that is partially loaded +/// from an external source and partially added by local translation. The +/// items loaded from the external source are loaded lazily, when needed for +/// iteration over the complete vector. +template <typename T, void (ExternalSemaSource::*Loader)(SmallVectorImpl<T> &), + unsigned LoadedStorage = 2, unsigned LocalStorage = 4> +class LazyVector { + SmallVector<T, LoadedStorage> Loaded; + SmallVector<T, LocalStorage> Local; + +public: + /// Iteration over the elements in the vector. + /// + /// In a complete iteration, the iterator walks the range [-M, N), + /// where negative values are used to indicate elements + /// loaded from the external source while non-negative values are used to + /// indicate elements added via \c push_back(). + /// However, to provide iteration in source order (for, e.g., chained + /// precompiled headers), dereferencing the iterator flips the negative + /// values (corresponding to loaded entities), so that position -M + /// corresponds to element 0 in the loaded entities vector, position -M+1 + /// corresponds to element 1 in the loaded entities vector, etc. This + /// gives us a reasonably efficient, source-order walk. + /// + /// We define this as a wrapping iterator around an int. The + /// iterator_adaptor_base class forwards the iterator methods to basic integer + /// arithmetic. + class iterator + : public llvm::iterator_adaptor_base< + iterator, int, std::random_access_iterator_tag, T, int, T *, T &> { + friend class LazyVector; + + LazyVector *Self; + + iterator(LazyVector *Self, int Position) + : iterator::iterator_adaptor_base(Position), Self(Self) {} + + bool isLoaded() const { return this->I < 0; } + + public: + iterator() : iterator(nullptr, 0) {} + + typename iterator::reference operator*() const { + if (isLoaded()) + return Self->Loaded.end()[this->I]; + return Self->Local.begin()[this->I]; + } + }; + + iterator begin(ExternalSemaSource *source, bool LocalOnly = false) { + if (LocalOnly) + return iterator(this, 0); + + if (source) + (source->*Loader)(Loaded); + return iterator(this, -(int)Loaded.size()); + } + + iterator end() { return iterator(this, Local.size()); } + + void push_back(const T &LocalValue) { Local.push_back(LocalValue); } + + void erase(iterator From, iterator To) { + if (From.isLoaded() && To.isLoaded()) { + Loaded.erase(&*From, &*To); + return; + } + + if (From.isLoaded()) { + Loaded.erase(&*From, Loaded.end()); + From = begin(nullptr, true); + } + + Local.erase(&*From, &*To); + } +}; + } // end namespace clang #endif diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index dcf112fd8eaa4..8fdc320562c00 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3617,7 +3617,7 @@ class Sema final : public SemaBase { void getSortedUnusedLocalTypedefNameCandidates( SmallVectorImpl<const TypedefNameDecl *> &Sorted) const; - typedef LazyVector<const DeclaratorDecl *, ExternalSemaSource, + typedef LazyVector<const DeclaratorDecl *, &ExternalSemaSource::ReadUnusedFileScopedDecls, 2, 2> UnusedFileScopedDeclsType; @@ -3625,8 +3625,8 @@ class Sema final : public SemaBase { /// and must warn if not used. Only contains the first declaration. UnusedFileScopedDeclsType UnusedFileScopedDecls; - typedef LazyVector<VarDecl *, ExternalSemaSource, - &ExternalSemaSource::ReadTentativeDefinitions, 2, 2> + typedef LazyVector<VarDecl *, &ExternalSemaSource::ReadTentativeDefinitions, + 2, 2> TentativeDefinitionsType; /// All the tentative definitions encountered in the TU. @@ -4960,8 +4960,8 @@ class Sema final : public SemaBase { /// WeakTopLevelDeclDecls - access to \#pragma weak-generated Decls SmallVectorImpl<Decl *> &WeakTopLevelDecls() { return WeakTopLevelDecl; } - typedef LazyVector<TypedefNameDecl *, ExternalSemaSource, - &ExternalSemaSource::ReadExtVectorDecls, 2, 2> + typedef LazyVector<TypedefNameDecl *, &ExternalSemaSource::ReadExtVectorDecls, + 2, 2> ExtVectorDeclsType; /// ExtVectorDecls - This is a list all the extended vector types. This allows @@ -6537,7 +6537,7 @@ class Sema final : public SemaBase { /// same list more than once. std::unique_ptr<RecordDeclSetTy> PureVirtualClassDiagSet; - typedef LazyVector<CXXConstructorDecl *, ExternalSemaSource, + typedef LazyVector<CXXConstructorDecl *, &ExternalSemaSource::ReadDelegatingConstructors, 2, 2> DelegatingCtorDeclsType; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
