File: declaration.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (807 lines) | stat: -rw-r--r-- 22,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/*
    SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
    SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "declaration.h"
#include "declarationdata.h"

#include <QByteArray>

#include <limits>

#include "topducontext.h"
#include "topducontextdynamicdata.h"
#include "use.h"
#include "forwarddeclaration.h"
#include "duchain.h"
#include "duchainlock.h"
#include "ducontextdata.h"
#include "declarationid.h"
#include "uses.h"
#include <serialization/indexedstring.h>
#include "duchainregister.h"
#include "persistentsymboltable.h"
#include "types/identifiedtype.h"
#include "types/structuretype.h"
#include "functiondefinition.h"
#include "codemodel.h"
#include "specializationstore.h"
#include "types/typeutils.h"
#include "types/typealiastype.h"
#include "classdeclaration.h"
#include "serialization/stringrepository.h"
#include "ducontextdynamicdata.h"

namespace KDevelop {
REGISTER_DUCHAIN_ITEM(Declaration);

DeclarationData::DeclarationData()
    : m_isDefinition(false)
    , m_inSymbolTable(false)
    , m_isTypeAlias(false)
    , m_anonymousInContext(false)
    , m_isDeprecated(false)
    , m_alwaysForceDirect(false)
    , m_isAutoDeclaration(false)
    , m_isExplicitlyDeleted(false)
    , m_isExplicitlyTyped(false)
{
}

struct DeclarationComment {
};
template <>
class ItemRepositoryFor<DeclarationComment>
{
    friend struct LockedItemRepository;
    static auto& repo()
    {
        static QMutex mutex;
        ///@todo Use reference counting
        static Repositories::StringRepository repo(QStringLiteral("Comment Repository"), &mutex);
        return repo;
    }
};

void initDeclarationRepositories()
{
    LockedItemRepository::initialize<DeclarationComment>();
}

Declaration::Kind Declaration::kind() const
{
    DUCHAIN_D(Declaration);
    return d->m_kind;
}

void Declaration::setKind(Kind kind)
{
    DUCHAIN_D_DYNAMIC(Declaration);
    d->m_kind = kind;
    updateCodeModel();
}

bool Declaration::inDUChain() const
{
    DUCHAIN_D(Declaration);
    if (d->m_anonymousInContext)
        return false;
    if (!context())
        return false;
    TopDUContext* top = topContext();
    return top && top->inDUChain();
}

Declaration::Declaration(const RangeInRevision& range, DUContext* context)
    : DUChainBase(*new DeclarationData, range)
{
    d_func_dynamic()->setClassId(this);
    m_topContext = nullptr;
    m_context = nullptr;
    m_indexInTopContext = 0;

    if (context)
        setContext(context);
}

uint Declaration::ownIndex() const
{
    ENSURE_CAN_READ
    return m_indexInTopContext;
}

Declaration::Declaration(const Declaration& rhs)
    : DUChainBase(*new DeclarationData(*rhs.d_func()))
{
}

Declaration::Declaration(DeclarationData& dd) : DUChainBase(dd)
{
}

Declaration::Declaration(DeclarationData& dd, const RangeInRevision& range)
    : DUChainBase(dd, range)
{
}

bool Declaration::persistentlyDestroying() const
{
    TopDUContext* topContext = this->topContext();
    return !topContext->deleting() || !topContext->isOnDisk();
}

Declaration::~Declaration()
{
    uint oldOwnIndex = m_indexInTopContext;

    TopDUContext* topContext = this->topContext();

    //Only perform the actions when the top-context isn't being deleted, or when it hasn't been stored to disk
    if (persistentlyDestroying()) {
        DUCHAIN_D_DYNAMIC(Declaration);

        // Inserted by the builder after construction has finished.
        if (d->m_internalContext.context())
            d->m_internalContext.context()->setOwner(nullptr);

        setInSymbolTable(false);
    }

    // If the parent-context already has dynamic data, like for example any temporary context,
    // always delete the declaration, to not create crashes within more complex code like C++ template stuff.
    if (context() && !d_func()->m_anonymousInContext) {
        if (!topContext->deleting() || !topContext->isOnDisk() || context()->d_func()->isDynamic())
            context()->m_dynamicData->removeDeclaration(this);
    }

    clearOwnIndex();

    if (!topContext->deleting() || !topContext->isOnDisk()) {
        setContext(nullptr);

        setAbstractType(AbstractType::Ptr());
    }
    Q_ASSERT(d_func()->isDynamic() ==
             (!topContext->deleting() || !topContext->isOnDisk() ||
              topContext->m_dynamicData->isTemporaryDeclarationIndex(oldOwnIndex)));
    Q_UNUSED(oldOwnIndex);
}

QByteArray Declaration::comment() const
{
    DUCHAIN_D(Declaration);
    if (!d->m_comment)
        return QByteArray();

    return LockedItemRepository::read<DeclarationComment>([d](const Repositories::StringRepository& repo) {
        return Repositories::arrayFromItem(repo.itemFromIndex(d->m_comment));
    });
}

void Declaration::setComment(const QByteArray& str)
{
    DUCHAIN_D_DYNAMIC(Declaration);
    if (str.isEmpty()) {
        d->m_comment = 0;
        return;
    }

    const auto request = Repositories::StringRepositoryItemRequest(
        str.constData(), IndexedString::hashString(str.constData(), str.length()), str.length());

    d->m_comment = LockedItemRepository::write<DeclarationComment>(
        [&](Repositories::StringRepository& repo) { return repo.index(request); });
}

void Declaration::setComment(const QString& str)
{
    setComment(str.toUtf8());
}

Identifier Declaration::identifier() const
{
    //ENSURE_CAN_READ Commented out for performance reasons
    return d_func()->m_identifier.identifier();
}

const IndexedIdentifier& Declaration::indexedIdentifier() const
{
    //ENSURE_CAN_READ Commented out for performance reasons
    return d_func()->m_identifier;
}

void Declaration::rebuildDynamicData(DUContext* parent, uint ownIndex)
{
    DUChainBase::rebuildDynamicData(parent, ownIndex);

    m_context = parent;
    m_topContext = parent->topContext();
    m_indexInTopContext = ownIndex;
}

void Declaration::setIdentifier(const Identifier& identifier)
{
    ENSURE_CAN_WRITE
        DUCHAIN_D_DYNAMIC(Declaration);
    bool wasInSymbolTable = d->m_inSymbolTable;

    setInSymbolTable(false);

    d->m_identifier = identifier;

    setInSymbolTable(wasInSymbolTable);
}

IndexedType Declaration::indexedType() const
{
    return d_func()->m_type;
}

AbstractType::Ptr Declaration::abstractType() const
{
    //ENSURE_CAN_READ Commented out for performance reasons
    return d_func()->m_type.abstractType();
}

void Declaration::setAbstractType(AbstractType::Ptr type)
{
    ENSURE_CAN_WRITE
        DUCHAIN_D_DYNAMIC(Declaration);

    d->m_type = type ? type->indexed() : IndexedType();

    updateCodeModel();
}

Declaration* Declaration::specialize(const IndexedInstantiationInformation& /*specialization*/,
                                     const TopDUContext* topContext, int /*upDistance*/)
{
    if (!topContext)
        return nullptr;
    return this;
}

QualifiedIdentifier Declaration::qualifiedIdentifier() const
{
    ENSURE_CAN_READ

    QualifiedIdentifier ret;
    DUContext* ctx = m_context;
    if (ctx)
        ret = ctx->scopeIdentifier(true);
    ret.push(d_func()->m_identifier);
    return ret;
}

DUContext* Declaration::context() const
{
    //ENSURE_CAN_READ Commented out for performance reasons
    return m_context;
}

bool Declaration::isAnonymous() const
{
    return d_func()->m_anonymousInContext;
}

void Declaration::setContext(DUContext* context, bool anonymous)
{
    Q_ASSERT(!context || context->topContext());

    DUCHAIN_D_DYNAMIC(Declaration);

    if (context == m_context && anonymous == d->m_anonymousInContext) {
        // skip costly operations below when the same context is set
        // this happens often when updating a TopDUContext from the cache
        return;
    }

    setInSymbolTable(false);

    //We don't need to clear, because it's not allowed to move from one top-context into another
//   clearOwnIndex();

    if (m_context && context) {
        Q_ASSERT(m_context->topContext() == context->topContext());
    }

    if (m_context) {
        if (!d->m_anonymousInContext) {
            m_context->m_dynamicData->removeDeclaration(this);
        }
    }

    if (context)
        m_topContext = context->topContext();
    else
        m_topContext = nullptr;

    d->m_anonymousInContext = anonymous;
    m_context = context;

    if (context) {
        if (!m_indexInTopContext)
            allocateOwnIndex();

        if (!d->m_anonymousInContext) {
            context->m_dynamicData->addDeclaration(this);
        }

        if (context->inSymbolTable() && !anonymous)
            setInSymbolTable(true);
    }
}

void Declaration::clearOwnIndex()
{
    if (!m_indexInTopContext)
        return;

    if (!context() || (!d_func()->m_anonymousInContext && !context()->isAnonymous())) {
        ENSURE_CAN_WRITE
    }

    if (m_indexInTopContext) {
        Q_ASSERT(m_topContext);
        m_topContext->m_dynamicData->clearDeclarationIndex(this);
    }
    m_indexInTopContext = 0;
}

void Declaration::allocateOwnIndex()
{
    ///@todo Fix multithreading stuff with template instantiation, preferably using some internal mutexes
//   if(context() && (!context()->isAnonymous() && !d_func()->m_anonymousInContext)) {
//     ENSURE_CAN_WRITE
//   }

    Q_ASSERT(m_topContext);

    m_indexInTopContext = m_topContext->m_dynamicData->allocateDeclarationIndex(this,
                                                                                d_func()->m_anonymousInContext || !context() ||
                                                                                context()->isAnonymous());
    Q_ASSERT(m_indexInTopContext);

    if (!m_topContext->m_dynamicData->declarationForIndex(m_indexInTopContext))
        qFatal("Could not re-retrieve declaration\nindex: %d", m_indexInTopContext);
}

const Declaration* Declaration::logicalDeclaration(const TopDUContext* topContext) const
{
    ENSURE_CAN_READ
    if (isForwardDeclaration()) {
        const auto dec = static_cast<const ForwardDeclaration*>(this);
        Declaration* ret = dec->resolve(topContext);
        if (ret)
            return ret;
    }
    return this;
}

Declaration* Declaration::logicalDeclaration(const TopDUContext* topContext)
{
    ENSURE_CAN_READ
    if (isForwardDeclaration()) {
        const auto dec = static_cast<const ForwardDeclaration*>(this);
        Declaration* ret = dec->resolve(topContext);
        if (ret)
            return ret;
    }
    return this;
}

DUContext* Declaration::logicalInternalContext(const TopDUContext* topContext) const
{
    ENSURE_CAN_READ

    if (!isDefinition()) {
        Declaration* def = FunctionDefinition::definition(this);
        if (def)
            return def->internalContext();
    }

    if (d_func()->m_isTypeAlias) {
        ///If this is a type-alias, return the internal context of the actual type.
        TypeAliasType::Ptr t = type<TypeAliasType>();
        if (t) {
            AbstractType::Ptr target = t->type();

            auto* idType = dynamic_cast<IdentifiedType*>(target.data());
            if (idType) {
                Declaration* decl = idType->declaration(topContext);
                if (decl && decl != this) {
                    return decl->logicalInternalContext(topContext);
                }
            }
        }
    }

    return internalContext();
}

DUContext* Declaration::internalContext() const
{
//   ENSURE_CAN_READ
    return d_func()->m_internalContext.context();
}

void Declaration::setInternalContext(DUContext* context)
{
    if (this->context()) {
        ENSURE_CAN_WRITE
    }
    DUCHAIN_D_DYNAMIC(Declaration);

    if (context == d->m_internalContext.context())
        return;

    if (!m_topContext) {
        //Take the top-context from the other side. We need to allocate an index, so we can safely call setOwner(..)
        m_topContext = context->topContext();
        allocateOwnIndex();
    }

    DUContext* oldInternalContext = d->m_internalContext.context();

    d->m_internalContext = context;

    //Q_ASSERT( !oldInternalContext || oldInternalContext->owner() == this );
    if (oldInternalContext && oldInternalContext->owner() == this)
        oldInternalContext->setOwner(nullptr);

    if (context)
        context->setOwner(this);
}

bool Declaration::operator ==(const Declaration& other) const
{
    ENSURE_CAN_READ

    return this == &other;
}

QString Declaration::toString() const
{
    return QStringLiteral("%3 %4").arg(abstractType() ? abstractType()->toString() : QStringLiteral(
                                           "<notype>"), identifier().toString());
}

bool Declaration::isDefinition() const
{
    ENSURE_CAN_READ
        DUCHAIN_D(Declaration);

    return d->m_isDefinition;
}

void Declaration::setDeclarationIsDefinition(bool dd)
{
    ENSURE_CAN_WRITE
        DUCHAIN_D_DYNAMIC(Declaration);
    d->m_isDefinition = dd;
}

bool Declaration::isAutoDeclaration() const
{
    return d_func()->m_isAutoDeclaration;
}

void Declaration::setAutoDeclaration(bool _auto)
{
    d_func_dynamic()->m_isAutoDeclaration = _auto;
}

bool Declaration::isDeprecated() const
{
    return d_func()->m_isDeprecated;
}

void Declaration::setDeprecated(bool deprecated)
{
    d_func_dynamic()->m_isDeprecated = deprecated;
}

bool Declaration::alwaysForceDirect() const
{
    return d_func()->m_alwaysForceDirect;
}

void Declaration::setAlwaysForceDirect(bool direct)
{
    d_func_dynamic()->m_alwaysForceDirect = direct;
}

bool Declaration::isExplicitlyDeleted() const
{
    return d_func()->m_isExplicitlyDeleted;
}

void Declaration::setExplicitlyDeleted(bool deleted)
{
    d_func_dynamic()->m_isExplicitlyDeleted = deleted;
}

bool Declaration::isExplicitlyTyped() const
{
    return d_func()->m_isExplicitlyTyped;
}

void Declaration::setExplicitlyTyped(bool explicitlyTyped)
{
    d_func_dynamic()->m_isExplicitlyTyped = explicitlyTyped;
}

///@todo see whether it would be useful to create an own TypeAliasDeclaration sub-class for this
bool Declaration::isTypeAlias() const
{
    DUCHAIN_D(Declaration);
    return d->m_isTypeAlias;
}

void Declaration::setIsTypeAlias(bool isTypeAlias)
{
    DUCHAIN_D_DYNAMIC(Declaration);
    d->m_isTypeAlias = isTypeAlias;
}

IndexedInstantiationInformation Declaration::specialization() const
{
    return IndexedInstantiationInformation();
}

void Declaration::activateSpecialization()
{
    if (specialization().index()) {
        DeclarationId baseId(id());
        baseId.setSpecialization(IndexedInstantiationInformation());
        SpecializationStore::self().set(baseId, specialization());
    }
}

DeclarationId Declaration::id(bool forceDirect) const
{
    ENSURE_CAN_READ
    if (inSymbolTable() && !forceDirect && !alwaysForceDirect())
        return DeclarationId(qualifiedIdentifier(), additionalIdentity(), specialization());
    else
        return DeclarationId(IndexedDeclaration(const_cast<Declaration*>(this)), specialization());
}

bool Declaration::inSymbolTable() const
{
    DUCHAIN_D(Declaration);
    return d->m_inSymbolTable;
}

CodeModelItem::Kind kindForDeclaration(Declaration* decl)
{
    CodeModelItem::Kind kind = CodeModelItem::Unknown;

    if (decl->kind() == Declaration::Namespace)
        return CodeModelItem::Namespace;

    if (decl->isFunctionDeclaration()) {
        kind = CodeModelItem::Function;
    }

    if (decl->kind() == Declaration::Type && (decl->type<StructureType>() || dynamic_cast<ClassDeclaration*>(decl)))
        kind = CodeModelItem::Class;

    if (kind == CodeModelItem::Unknown && decl->kind() == Declaration::Instance)
        kind = CodeModelItem::Variable;

    if (decl->isForwardDeclaration())
        kind = ( CodeModelItem::Kind )(kind | CodeModelItem::ForwardDeclaration);

    if (decl->context() && decl->context()->type() == DUContext::Class)
        kind = ( CodeModelItem::Kind )(kind | CodeModelItem::ClassMember);

    return kind;
}

void Declaration::updateCodeModel()
{
    DUCHAIN_D(Declaration);
    if (!d->m_identifier.isEmpty() && d->m_inSymbolTable) {
        QualifiedIdentifier id(qualifiedIdentifier());
        CodeModel::self().updateItem(url(), id, kindForDeclaration(this));
    }
}

void Declaration::setInSymbolTable(bool inSymbolTable)
{
    DUCHAIN_D_DYNAMIC(Declaration);
    if (!d->m_identifier.isEmpty()) {
        if (!d->m_inSymbolTable && inSymbolTable) {
            QualifiedIdentifier id(qualifiedIdentifier());
            PersistentSymbolTable::self().addDeclaration(id, this);

            CodeModel::self().addItem(url(), id, kindForDeclaration(this));
        } else if (d->m_inSymbolTable && !inSymbolTable) {
            QualifiedIdentifier id(qualifiedIdentifier());
            PersistentSymbolTable::self().removeDeclaration(id, this);

            CodeModel::self().removeItem(url(), id);
        }
    }
    d->m_inSymbolTable = inSymbolTable;
}

TopDUContext* Declaration::topContext() const
{
    return m_topContext;
}

Declaration* Declaration::clonePrivate() const
{
    return new Declaration(*this);
}

Declaration* Declaration::clone() const
{
    Declaration* ret = clonePrivate();
    ret->d_func_dynamic()->m_inSymbolTable = false;
    return ret;
}

bool Declaration::isForwardDeclaration() const
{
    return false;
}

bool Declaration::isFunctionDeclaration() const
{
    return false;
}

uint Declaration::additionalIdentity() const
{
    return 0;
}

bool Declaration::equalQualifiedIdentifier(const Declaration* rhs) const
{
    ENSURE_CAN_READ
        DUCHAIN_D(Declaration);
    if (d->m_identifier != rhs->d_func()->m_identifier)
        return false;

    return m_context->equalScopeIdentifier(m_context);
}

QMap<IndexedString, QVector<RangeInRevision>> Declaration::uses() const
{
    ENSURE_CAN_READ
    QMap<IndexedString, QMap<RangeInRevision, bool>> tempUses;

    //First, search for uses within the own context
    {
        QMap<RangeInRevision, bool>& ranges(tempUses[topContext()->url()]);
        const auto useRanges = allUses(topContext(), const_cast<Declaration*>(this));
        for (const RangeInRevision range : useRanges) {
            ranges[range] = true;
        }
    }

    DeclarationId _id = id();
    KDevVarLengthArray<IndexedTopDUContext> useContexts = DUChain::uses()->uses(_id);
    if (!_id.isDirect()) { // also check uses based on direct IDs
        KDevVarLengthArray<IndexedTopDUContext> directUseContexts = DUChain::uses()->uses(id(true));
        useContexts.append(directUseContexts.data(), directUseContexts.size());
    }

    for (const IndexedTopDUContext indexedContext : qAsConst(useContexts)) {
        TopDUContext* context = indexedContext.data();
        if (context) {
            QMap<RangeInRevision, bool>& ranges(tempUses[context->url()]);
            const auto useRanges = allUses(context, const_cast<Declaration*>(this));
            for (const RangeInRevision range : useRanges) {
                ranges[range] = true;
            }
        }
    }

    QMap<IndexedString, QVector<RangeInRevision>> ret;

    for (QMap<IndexedString, QMap<RangeInRevision, bool>>::const_iterator it = tempUses.constBegin();
         it != tempUses.constEnd(); ++it) {
        if (!(*it).isEmpty()) {
            auto& list = ret[it.key()];
            list.reserve((*it).size());
            for (QMap<RangeInRevision, bool>::const_iterator it2 = (*it).constBegin(); it2 != (*it).constEnd(); ++it2)
                list << it2.key();
        }
    }

    return ret;
}

bool hasDeclarationUse(DUContext* context, int declIdx)
{
    bool ret = false;
    int usescount = context->usesCount();
    const Use* uses = context->uses();

    for (int i = 0; !ret && i < usescount; ++i) {
        ret = uses[i].m_declarationIndex == declIdx;
    }

    const auto childContexts = context->childContexts();
    for (DUContext* child : childContexts) {
        ret = ret || hasDeclarationUse(child, declIdx);
        if (ret)
            break;
    }

    return ret;
}

bool Declaration::hasUses() const
{
    ENSURE_CAN_READ
    int idx = topContext()->indexForUsedDeclaration(const_cast<Declaration*>(this), false);
    bool ret = idx != std::numeric_limits<int>::max() && (idx >= 0 || hasDeclarationUse(topContext(), idx)); //hasLocalUses
    DeclarationId myId = id();

    if (!ret && DUChain::uses()->hasUses(myId)) {
        ret = true;
    }

    if (!ret && !myId.isDirect() && DUChain::uses()->hasUses(id(true))) {
        ret = true;
    }

    return ret;
}

QMap<IndexedString, QVector<KTextEditor::Range>> Declaration::usesCurrentRevision() const
{
    ENSURE_CAN_READ
    QMap<IndexedString, QMap<KTextEditor::Range, bool>> tempUses;

    //First, search for uses within the own context
    {
        QMap<KTextEditor::Range, bool>& ranges(tempUses[topContext()->url()]);
        const auto useRanges = allUses(topContext(), const_cast<Declaration*>(this));
        for (const RangeInRevision range : useRanges) {
            ranges[topContext()->transformFromLocalRevision(range)] = true;
        }
    }

    DeclarationId _id = id();
    KDevVarLengthArray<IndexedTopDUContext> useContexts = DUChain::uses()->uses(_id);
    if (!_id.isDirect()) { // also check uses based on direct IDs
        KDevVarLengthArray<IndexedTopDUContext> directUseContexts = DUChain::uses()->uses(id(true));
        useContexts.append(directUseContexts.data(), directUseContexts.size());
    }

    for (const IndexedTopDUContext indexedContext : qAsConst(useContexts)) {
        TopDUContext* context = indexedContext.data();
        if (context) {
            QMap<KTextEditor::Range, bool>& ranges(tempUses[context->url()]);
            const auto useRanges = allUses(context, const_cast<Declaration*>(this));
            for (const RangeInRevision range : useRanges) {
                ranges[context->transformFromLocalRevision(range)] = true;
            }
        }
    }

    QMap<IndexedString, QVector<KTextEditor::Range>> ret;

    for (QMap<IndexedString, QMap<KTextEditor::Range, bool>>::const_iterator it = tempUses.constBegin();
         it != tempUses.constEnd(); ++it) {
        if (!(*it).isEmpty()) {
            auto& list = ret[it.key()];
            list.reserve((*it).size());
            for (QMap<KTextEditor::Range, bool>::const_iterator it2 = (*it).constBegin(); it2 != (*it).constEnd();
                 ++it2)
                list << it2.key();
        }
    }

    return ret;
}
}