File indexing completed on 2024-05-12 04:38:00
0001 /* 0002 SPDX-FileCopyrightText: 2007 Hamish Rodda <rodda@kde.org> 0003 SPDX-FileCopyrightText: 2007-2009 David Nolden <david.nolden.kdevelop@art-master.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KDEVPLATFORM_DUCHAINUTILS_H 0009 #define KDEVPLATFORM_DUCHAINUTILS_H 0010 0011 #include <QUrl> 0012 0013 #include <language/languageexport.h> 0014 #include <language/duchain/problem.h> 0015 #include <language/duchain/topducontext.h> 0016 0017 #include <KTextEditor/CodeCompletionModel> 0018 0019 class QIcon; 0020 0021 namespace KTextEditor { 0022 class Cursor; 0023 } 0024 0025 namespace KDevelop { 0026 class Declaration; 0027 class DUChainBase; 0028 class DUContext; 0029 class IndexedString; 0030 class TopDUContext; 0031 class IndexedDeclaration; 0032 0033 /** 0034 * A namespace which contains convenience utilities for navigating definition-use chains. 0035 */ 0036 namespace DUChainUtils { 0037 KDEVPLATFORMLANGUAGE_EXPORT KTextEditor::CodeCompletionModel::CompletionProperties completionProperties( 0038 const Declaration* dec); 0039 KDEVPLATFORMLANGUAGE_EXPORT QIcon iconForProperties(KTextEditor::CodeCompletionModel::CompletionProperties p); 0040 KDEVPLATFORMLANGUAGE_EXPORT QIcon iconForDeclaration(const Declaration* dec); 0041 /** Asks the language-plugins for standard-contexts for the given url, and returns one if available. 0042 * If there is no language-plugin registered for the given url, it will just try to get any top-context for the file from the du-chain. 0043 * NOTE: The DUChain needs to be read or write locked when you call this. 0044 * @param preferProxyContext Whether the returned context should be a proxy context. When no proxy-context is found, a normal context is returned. 0045 * 0046 * FIXME: this should operate on IndexedString 0047 */ 0048 KDEVPLATFORMLANGUAGE_EXPORT KDevelop::TopDUContext* standardContextForUrl(const QUrl& url, 0049 bool preferProxyContext = false); 0050 /** 0051 * Returns the content-context associated to the given proxy-context. 0052 * Returns the same context if it is not a proxy-context. 0053 * Returns zero if no content-context could be acquired. 0054 * */ 0055 KDEVPLATFORMLANGUAGE_EXPORT TopDUContext* contentContextFromProxyContext(TopDUContext* top); 0056 struct KDEVPLATFORMLANGUAGE_EXPORT ItemUnderCursor 0057 { 0058 Declaration* declaration; // found declaration (either declared/defined or used) 0059 DUContext* context; // context in which the declaration, definition, or use was found 0060 KTextEditor::Range range; // range of the declaration/definition/use 0061 }; 0062 /** Returns 1. the Declaration/Definition either declared or used under the cursor, 0063 * or zero; and 2. the context in which the declaration, definition, or use was found. 0064 * DUChain must be locked. 0065 * Must only be called from the foreground or with the foreground lock held. */ 0066 KDEVPLATFORMLANGUAGE_EXPORT ItemUnderCursor itemUnderCursor(const QUrl& url, const KTextEditor::Cursor& cursor); 0067 /**If the given declaration is a definition, and has a real declaration 0068 * attached, returns that declarations. Else returns the given argument. */ 0069 KDEVPLATFORMLANGUAGE_EXPORT Declaration* declarationForDefinition(Declaration* definition, 0070 TopDUContext* topContext = nullptr); 0071 ///Returns the first declaration in the given line. Searches the given context and all sub-contexts. 0072 ///Must only be called from the foreground or with the foreground lock held. 0073 KDEVPLATFORMLANGUAGE_EXPORT Declaration* declarationInLine(const KTextEditor::Cursor& cursor, KDevelop::DUContext* ctx); 0074 0075 class KDEVPLATFORMLANGUAGE_EXPORT DUChainItemFilter 0076 { 0077 public: 0078 virtual bool accept(Declaration* decl) = 0; 0079 //Should return whether processing should be deepened into the given context 0080 virtual bool accept(DUContext* ctx) = 0; 0081 virtual ~DUChainItemFilter(); 0082 }; 0083 ///walks a context, all its sub-contexts, and all its declarations in exactly the order they appear in the file. 0084 ///Re-implement DUChainItemFilter to do something with the items. 0085 KDEVPLATFORMLANGUAGE_EXPORT void collectItems(DUContext* context, DUChainItemFilter& filter); 0086 0087 KDEVPLATFORMLANGUAGE_EXPORT DUContext* argumentContext(Declaration* decl); 0088 0089 ///Uses the persistent symbol table to find all occurrences of this declaration, based on its identifier. 0090 ///The result should be filtered to make sure that the declaration is actually useful to you. 0091 KDEVPLATFORMLANGUAGE_EXPORT QList<IndexedDeclaration> collectAllVersions(Declaration* decl); 0092 0093 ///If the given declaration is a class, this gets all classes that inherit this one 0094 ///@param collectVersions If this is true, the persistent symbol table is used to first find all registered 0095 /// versions of this class, and then get the inheriters from them all together. This is needed for C++. 0096 ///@param maxAllowedSteps The maximum of steps allowed. If this is zero in the end, this means the search has been stopped with the max. reached 0097 /// If you really want _all_ inheriters, you should initialize it with a very large value. 0098 KDEVPLATFORMLANGUAGE_EXPORT QList<Declaration*> inheriters(const Declaration* decl, uint& maxAllowedSteps, 0099 bool collectVersions = true); 0100 0101 ///Gets all functions that override the function @p overriddenDeclaration, starting the search at @p currentClass 0102 ///@param maxAllowedSteps The maximum of steps allowed. If this is zero in the end, this means the search has been stopped with the max. reached 0103 KDEVPLATFORMLANGUAGE_EXPORT QList<Declaration*> overriders(const Declaration* currentClass, 0104 const Declaration* overriddenDeclaration, 0105 uint& maxAllowedSteps); 0106 0107 ///Returns whether the given context or any of its child-contexts contain a use of the given declaration. This is relatively expensive. 0108 KDEVPLATFORMLANGUAGE_EXPORT bool contextHasUse(DUContext* context, Declaration* declaration); 0109 0110 ///Returns the total count of uses of the given declaration under the given context 0111 KDEVPLATFORMLANGUAGE_EXPORT uint contextCountUses(DUContext* context, Declaration* declaration); 0112 0113 ///Returns the declaration that is overridden by the given one, or zero. 0114 KDEVPLATFORMLANGUAGE_EXPORT Declaration* overridden(const Declaration* decl); 0115 0116 ///If the given declaration is a function-declaration, this follows the context-structure up to the function-context that contains the arguments, 0117 ///and returns it. 0118 KDEVPLATFORMLANGUAGE_EXPORT DUContext* functionContext(Declaration* decl); 0119 0120 KDEVPLATFORMLANGUAGE_EXPORT QVector<KDevelop::Problem::Ptr> allProblemsForContext(const ReferencedTopDUContext& top); 0121 } 0122 } 0123 0124 #endif // KDEVPLATFORM_DUCHAINUTILS_H