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

0001 /*
0002     SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
0003     SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "topducontextutils.h"
0009 
0010 #include "abstractfunctiondeclaration.h"
0011 #include "declaration.h"
0012 
0013 using namespace KDevelop;
0014 
0015 ///Takes a set of conditions in the constructors, and checks with each call to operator() whether these conditions are fulfilled on the given declaration.
0016 ///The import-structure needs to be constructed and locked when this is used
0017 TopDUContext::DeclarationChecker::DeclarationChecker(const TopDUContext* _top, const CursorInRevision& _position,
0018                                                      const AbstractType::Ptr& _dataType, DUContext::SearchFlags _flags,
0019                                                      KDevVarLengthArray<IndexedDeclaration>* _createVisibleCache)
0020     : createVisibleCache(_createVisibleCache)
0021     , top(_top)
0022     , topDFunc(_top->d_func())
0023     , position(_position)
0024     , dataType(_dataType)
0025     , flags(_flags)
0026 {
0027 }
0028 
0029 bool TopDUContext::DeclarationChecker::operator()(const Declaration* decl) const
0030 {
0031     if (!decl)
0032         return false;
0033 
0034     if (top != decl->topContext()) {
0035         if (( flags& DUContext::OnlyFunctions ) && !dynamic_cast<const AbstractFunctionDeclaration*>(decl))
0036             return false;
0037 
0038         if (dataType && decl->abstractType()->indexed() != dataType->indexed())
0039             // The declaration doesn't match the type filter we are applying
0040             return false;
0041     } else {
0042         if (( flags& DUContext::OnlyFunctions ) && !dynamic_cast<const AbstractFunctionDeclaration*>(decl))
0043             return false;
0044 
0045         if (dataType && decl->abstractType() != dataType)
0046             // The declaration doesn't match the type filter we are applying
0047             return false;
0048 
0049         if (decl->range().start >= position)
0050             if (!decl->context() || decl->context()->type() != DUContext::Class)
0051                 return false; // The declaration is behind the position we're searching from, therefore not accessible
0052     }
0053     // Success, this declaration is accessible
0054     return true;
0055 }