1 /* 2 SPDX-FileCopyrightText: 2007 Hamish Rodda <rodda@kde.org> 3 SPDX-FileCopyrightText: 2007-2009 David Nolden <david.nolden.kdevelop@art-master.de> 4 5 SPDX-License-Identifier: LGPL-2.0-or-later 6 */ 7 8 #ifndef KDEVPLATFORM_DUCHAINUTILS_H 9 #define KDEVPLATFORM_DUCHAINUTILS_H 10 11 #include <QUrl> 12 13 #include <language/languageexport.h> 14 #include <language/duchain/problem.h> 15 #include <language/duchain/topducontext.h> 16 17 #include <KTextEditor/CodeCompletionModel> 18 19 class QIcon; 20 21 namespace KTextEditor { 22 class Cursor; 23 } 24 25 namespace KDevelop { 26 class Declaration; 27 class DUChainBase; 28 class DUContext; 29 class IndexedString; 30 class TopDUContext; 31 class IndexedDeclaration; 32 33 /** 34 * A namespace which contains convenience utilities for navigating definition-use chains. 35 */ 36 namespace DUChainUtils { 37 KDEVPLATFORMLANGUAGE_EXPORT KTextEditor::CodeCompletionModel::CompletionProperties completionProperties( 38 const Declaration* dec); 39 KDEVPLATFORMLANGUAGE_EXPORT QIcon iconForProperties(KTextEditor::CodeCompletionModel::CompletionProperties p); 40 KDEVPLATFORMLANGUAGE_EXPORT QIcon iconForDeclaration(const Declaration* dec); 41 /** Asks the language-plugins for standard-contexts for the given url, and returns one if available. 42 * 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. 43 * NOTE: The DUChain needs to be read or write locked when you call this. 44 * @param preferProxyContext Whether the returned context should be a proxy context. When no proxy-context is found, a normal context is returned. 45 * 46 * FIXME: this should operate on IndexedString 47 */ 48 KDEVPLATFORMLANGUAGE_EXPORT KDevelop::TopDUContext* standardContextForUrl(const QUrl& url, 49 bool preferProxyContext = false); 50 /** 51 * Returns the content-context associated to the given proxy-contex. 52 * Returns the same context if it is not a proxy-context. 53 * Returns zero if no content-context could be acquired. 54 * */ 55 KDEVPLATFORMLANGUAGE_EXPORT TopDUContext* contentContextFromProxyContext(TopDUContext* top); 56 struct KDEVPLATFORMLANGUAGE_EXPORT ItemUnderCursor 57 { 58 Declaration* declaration; // found declaration (either declared/defined or used) 59 DUContext* context; // context in which the declaration, definition, or use was found 60 KTextEditor::Range range; // range of the declaration/definition/use 61 }; 62 /** Returns 1. the Declaration/Definition either declared or used under the cursor, 63 * or zero; and 2. the context in which the declaration, definition, or use was found. 64 * DUChain must be locked. 65 * Must only be called from the foreground or with the foreground lock held. */ 66 KDEVPLATFORMLANGUAGE_EXPORT ItemUnderCursor itemUnderCursor(const QUrl& url, const KTextEditor::Cursor& cursor); 67 /**If the given declaration is a definition, and has a real declaration 68 * attached, returns that declarations. Else returns the given argument. */ 69 KDEVPLATFORMLANGUAGE_EXPORT Declaration* declarationForDefinition(Declaration* definition, 70 TopDUContext* topContext = nullptr); 71 ///Returns the first declaration in the given line. Searches the given context and all sub-contexts. 72 ///Must only be called from the foreground or with the foreground lock held. 73 KDEVPLATFORMLANGUAGE_EXPORT Declaration* declarationInLine(const KTextEditor::Cursor& cursor, KDevelop::DUContext* ctx); 74 75 class KDEVPLATFORMLANGUAGE_EXPORT DUChainItemFilter 76 { 77 public: 78 virtual bool accept(Declaration* decl) = 0; 79 //Should return whether processing should be deepened into the given context 80 virtual bool accept(DUContext* ctx) = 0; 81 virtual ~DUChainItemFilter(); 82 }; 83 ///walks a context, all its sub-contexts, and all its declarations in exactly the order they appear in the file. 84 ///Re-implement DUChainItemFilter to do something with the items. 85 KDEVPLATFORMLANGUAGE_EXPORT void collectItems(DUContext* context, DUChainItemFilter& filter); 86 87 KDEVPLATFORMLANGUAGE_EXPORT DUContext* argumentContext(Declaration* decl); 88 89 ///Uses the persistent symbol table to find all occurrences of this declaration, based on its identifier. 90 ///The result should be filtered to make sure that the declaration is actually useful to you. 91 KDEVPLATFORMLANGUAGE_EXPORT QList<IndexedDeclaration> collectAllVersions(Declaration* decl); 92 93 ///If the given declaration is a class, this gets all classes that inherit this one 94 ///@param collectVersions If this is true, the persistent symbol table is used to first find all registered 95 /// versions of this class, and then get the inheriters from them all together. This is needed for C++. 96 ///@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 97 /// If you really want _all_ inheriters, you should initialize it with a very large value. 98 KDEVPLATFORMLANGUAGE_EXPORT QList<Declaration*> inheriters(const Declaration* decl, uint& maxAllowedSteps, 99 bool collectVersions = true); 100 101 ///Gets all functions that override the function @p overriddenDeclaration, starting the search at @p currentClass 102 ///@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 103 KDEVPLATFORMLANGUAGE_EXPORT QList<Declaration*> overriders(const Declaration* currentClass, 104 const Declaration* overriddenDeclaration, 105 uint& maxAllowedSteps); 106 107 ///Returns whether the given context or any of its child-contexts contain a use of the given declaration. This is relatively expensive. 108 KDEVPLATFORMLANGUAGE_EXPORT bool contextHasUse(DUContext* context, Declaration* declaration); 109 110 ///Returns the total count of uses of the given declaration under the given context 111 KDEVPLATFORMLANGUAGE_EXPORT uint contextCountUses(DUContext* context, Declaration* declaration); 112 113 ///Returns the declaration that is overridden by the given one, or zero. 114 KDEVPLATFORMLANGUAGE_EXPORT Declaration* overridden(const Declaration* decl); 115 116 ///If the given declaration is a function-declaration, this follows the context-structure up to the function-context that contains the arguments, 117 ///and returns it. 118 KDEVPLATFORMLANGUAGE_EXPORT DUContext* functionContext(Declaration* decl); 119 120 KDEVPLATFORMLANGUAGE_EXPORT QVector<KDevelop::Problem::Ptr> allProblemsForContext(const ReferencedTopDUContext& top); 121 } 122 } 123 124 #endif // KDEVPLATFORM_DUCHAINUTILS_H 125