File indexing completed on 2024-05-12 04:38:03

0001 /*
0002     SPDX-FileCopyrightText: 2008 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KDEVPLATFORM_PERSISTENTSYMBOLTABLE_H
0008 #define KDEVPLATFORM_PERSISTENTSYMBOLTABLE_H
0009 
0010 #include <language/languageexport.h>
0011 
0012 #include "topducontext.h"
0013 
0014 #include <functional>
0015 
0016 class QTextStream;
0017 
0018 namespace KDevelop {
0019 class IndexedDeclaration;
0020 class IndexedQualifiedIdentifier;
0021 
0022 /**
0023  * Global symbol-table that is stored to disk, and allows retrieving declarations that currently are not loaded to memory.
0024  * */
0025 class KDEVPLATFORMLANGUAGE_EXPORT PersistentSymbolTable
0026 {
0027     Q_DISABLE_COPY_MOVE(PersistentSymbolTable)
0028     PersistentSymbolTable();
0029 public:
0030     ~PersistentSymbolTable();
0031 
0032     ///Adds declaration @p declaration with id @p id to the symbol table
0033     ///@warning DUChain must be write locked
0034     void addDeclaration(const IndexedQualifiedIdentifier& id, const IndexedDeclaration& declaration);
0035 
0036     ///Adds declaration @p declaration with id @p id to the symbol table
0037     ///@warning DUChain must be write locked
0038     void removeDeclaration(const IndexedQualifiedIdentifier& id, const IndexedDeclaration& declaration);
0039 
0040     enum class VisitorState {
0041         Break,
0042         Continue,
0043     };
0044     ///@warning The visitor must not call any PersistentSymbolTable API that would mutate its contents
0045     ///         i.e. do not call `addDeclaration` or `removeDeclaration` directly from the visitor.
0046     using DeclarationVisitor = std::function<VisitorState(const IndexedDeclaration&)>;
0047 
0048     /// Iterate over all the declarations for a given IndexedQualifiedIdentifier in an efficient way.
0049     ///@param id The IndexedQualifiedIdentifier for which the declarations should be retrieved
0050     ///@param visitor A callback that gets invoked for every matching declaration
0051     ///@warning DUChain must be read locked
0052     void visitDeclarations(const IndexedQualifiedIdentifier& id, const DeclarationVisitor& visitor) const;
0053 
0054     /// Iterate over all declarations of the given id, filtered by the visibility given through @a visibility
0055     /// This is very efficient since it uses a cache
0056     ///@param id The IndexedQualifiedIdentifier for which the declarations should be retrieved
0057     ///@param visibility A filter for visibility, only matches in one of these imports will be considered
0058     ///@param visitor A callback that gets invoked for every matching declaration
0059     ///@warning DUChain must be read locked
0060     void visitFilteredDeclarations(const IndexedQualifiedIdentifier& id,
0061                                    const TopDUContext::IndexedRecursiveImports& visibility,
0062                                    const DeclarationVisitor& visitor) const;
0063 
0064     static PersistentSymbolTable& self();
0065 
0066     //Very expensive: Checks for problems in the symbol table
0067     void dump(const QTextStream& out);
0068 
0069     //Clears the internal cache. Should be called regularly to save memory
0070     // TODO: currently the cache is never cleared. The sizes of the cache containers grow steadily and never shrink.
0071     // Consider clearing the cache when at least some of its entries become useless, e.g. when a project is closed.
0072     void clearCache();
0073 };
0074 }
0075 
0076 #endif