LookupContext.cpp 26.8 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30

#include "LookupContext.h"
31
32
#include "ResolveExpression.h"
#include "Overview.h"
33
#include "CppBindings.h"
34

con's avatar
con committed
35
36
37
38
39
40
#include <CoreTypes.h>
#include <Symbols.h>
#include <Literals.h>
#include <Names.h>
#include <Scope.h>
#include <Control.h>
hjk's avatar
hjk committed
41

con's avatar
con committed
42
43
#include <QtDebug>

44
45
uint CPlusPlus::qHash(const CPlusPlus::LookupItem &key)
{
Roberto Raggi's avatar
Roberto Raggi committed
46
47
48
    const uint h1 = QT_PREPEND_NAMESPACE(qHash)(key.type().type());
    const uint h2 = QT_PREPEND_NAMESPACE(qHash)(key.lastVisibleSymbol());
    return ((h1 << 16) | (h1 >> 16)) ^ h2;
49
50
51
52
}

using namespace CPlusPlus;

con's avatar
con committed
53
54
55
56
57
58
59
60
61
62
63
/////////////////////////////////////////////////////////////////////
// LookupContext
/////////////////////////////////////////////////////////////////////
LookupContext::LookupContext(Control *control)
    : _control(control),
      _symbol(0)
{ }

LookupContext::LookupContext(Symbol *symbol,
                             Document::Ptr expressionDocument,
                             Document::Ptr thisDocument,
Roberto Raggi's avatar
Roberto Raggi committed
64
                             const Snapshot &snapshot)
con's avatar
con committed
65
66
67
    : _symbol(symbol),
      _expressionDocument(expressionDocument),
      _thisDocument(thisDocument),
Roberto Raggi's avatar
Roberto Raggi committed
68
      _snapshot(snapshot)
con's avatar
con committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{
    _control = _expressionDocument->control();
    _visibleScopes = buildVisibleScopes();
}

bool LookupContext::isValid() const
{ return _control != 0; }

Control *LookupContext::control() const
{ return _control; }

Symbol *LookupContext::symbol() const
{ return _symbol; }

Document::Ptr LookupContext::expressionDocument() const
{ return _expressionDocument; }

Document::Ptr LookupContext::thisDocument() const
{ return _thisDocument; }

Document::Ptr LookupContext::document(const QString &fileName) const
90
{ return _snapshot.document(fileName); }
con's avatar
con committed
91

Roberto Raggi's avatar
Roberto Raggi committed
92
93
Snapshot LookupContext::snapshot() const
{ return _snapshot; }
con's avatar
con committed
94

95
96
97
98
bool LookupContext::maybeValidSymbol(Symbol *symbol,
                                     ResolveMode mode,
                                     const QList<Symbol *> &candidates)
{
99
100
101
102
    if (((mode & ResolveNamespace)    && symbol->isNamespace())    ||
        ((mode & ResolveClass)        && symbol->isClass())        ||
        ((mode & ResolveObjCClass)    && symbol->isObjCClass())    ||
        ((mode & ResolveObjCProtocol) && symbol->isObjCProtocol()) ||
103
104
105
106
107
108
109
         (mode & ResolveSymbol)) {
        return ! candidates.contains(symbol);
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
110
111
QList<Scope *> LookupContext::resolveNestedNameSpecifier(const QualifiedNameId *q,
                                                         const QList<Scope *> &visibleScopes) const
con's avatar
con committed
112
{
113
    QList<Symbol *> candidates;
114
    QList<Scope *> scopes = visibleScopes;
115

116
    for (unsigned i = 0; i < q->nameCount() - 1; ++i) {
Roberto Raggi's avatar
Roberto Raggi committed
117
        const Name *name = q->nameAt(i);
118

119
        candidates = resolveClassOrNamespace(name, scopes);
120

121
        if (candidates.isEmpty())
122
            break;
123

124
        scopes.clear();
125

126
        foreach (Symbol *candidate, candidates) {
127
128
            ScopedSymbol *scoped = candidate->asScopedSymbol();
            Scope *members = scoped->members();
129

130
131
            if (! scopes.contains(members))
                scopes.append(members);
132
        }
133
134
    }

135
136
137
    return scopes;
}

Roberto Raggi's avatar
Roberto Raggi committed
138
QList<Symbol *> LookupContext::resolveQualifiedNameId(const QualifiedNameId *q,
139
140
141
                                                      const QList<Scope *> &visibleScopes,
                                                      ResolveMode mode) const
{
142
143
    QList<Symbol *> candidates;

144
145
146
    if (true || mode & ResolveClass) {
        for (int i = 0; i < visibleScopes.size(); ++i) {
            Scope *scope = visibleScopes.at(i);
147

148
149
150
151
152
            for (Symbol *symbol = scope->lookat(q); symbol; symbol = symbol->next()) {
                if (! symbol->name())
                    continue;
                else if (! symbol->isClass())
                    continue;
153

Roberto Raggi's avatar
Roberto Raggi committed
154
                const QualifiedNameId *qq = symbol->name()->asQualifiedNameId();
155

156
157
158
159
                if (! qq)
                    continue;
                else if (! maybeValidSymbol(symbol, mode, candidates))
                    continue;
160

161
162
                if (! q->unqualifiedNameId()->isEqualTo(qq->unqualifiedNameId()))
                    continue;
163

164
165
                else if (qq->nameCount() == q->nameCount()) {
                    unsigned j = 0;
166

167
                    for (; j < q->nameCount(); ++j) {
Roberto Raggi's avatar
Roberto Raggi committed
168
169
                        const Name *classOrNamespaceName1 = q->nameAt(j);
                        const Name *classOrNamespaceName2 = qq->nameAt(j);
170

171
172
173
                        if (! classOrNamespaceName1->isEqualTo(classOrNamespaceName2))
                            break;
                    }
174

175
176
177
                    if (j == q->nameCount())
                        candidates.append(symbol);
                }
178
179
180
181
            }
        }
    }

182
    QList<Scope *> scopes;
183

184
185
186
187
    if (q->nameCount() == 1)
        scopes = visibleScopes;     // ### handle global scope lookup
    else
        scopes = resolveNestedNameSpecifier(q, visibleScopes);
188

189
190
191
    QList<Scope *> expanded;
    foreach (Scope *scope, scopes) {
        expanded.append(scope);
192

193
194
        for (unsigned i = 0; i < scope->symbolCount(); ++i) {
            Symbol *member = scope->symbolAt(i);
195

196
197
            if (ScopedSymbol *scopedSymbol = member->asScopedSymbol())
                expandEnumOrAnonymousSymbol(scopedSymbol, &expanded);
198
199
200
        }
    }

201
202
    candidates += resolve(q->unqualifiedNameId(), expanded, mode);

203
    return candidates;
204
}
205

Roberto Raggi's avatar
Roberto Raggi committed
206
QList<Symbol *> LookupContext::resolveOperatorNameId(const OperatorNameId *opId,
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
                                                     const QList<Scope *> &visibleScopes,
                                                     ResolveMode) const
{
    QList<Symbol *> candidates;

    for (int scopeIndex = 0; scopeIndex < visibleScopes.size(); ++scopeIndex) {
        Scope *scope = visibleScopes.at(scopeIndex);

        for (Symbol *symbol = scope->lookat(opId->kind()); symbol; symbol = symbol->next()) {
            if (! opId->isEqualTo(symbol->name()))
                continue;

            if (! candidates.contains(symbol))
                candidates.append(symbol);
        }
    }

    return candidates;
}

Roberto Raggi's avatar
Roberto Raggi committed
227
QList<Symbol *> LookupContext::resolve(const Name *name, const QList<Scope *> &visibleScopes,
228
229
230
231
232
                                       ResolveMode mode) const
{
    QList<Symbol *> candidates;

    if (!name)
233
        return candidates; // nothing to do, the symbol is anonymous.
234

Roberto Raggi's avatar
Roberto Raggi committed
235
    else if (const QualifiedNameId *q = name->asQualifiedNameId())
236
        return resolveQualifiedNameId(q, visibleScopes, mode);
con's avatar
con committed
237

Roberto Raggi's avatar
Roberto Raggi committed
238
    else if (const OperatorNameId *opId = name->asOperatorNameId())
239
240
        return resolveOperatorNameId(opId, visibleScopes, mode);

Roberto Raggi's avatar
Roberto Raggi committed
241
    else if (const Identifier *id = name->identifier()) {
con's avatar
con committed
242
243
        for (int scopeIndex = 0; scopeIndex < visibleScopes.size(); ++scopeIndex) {
            Scope *scope = visibleScopes.at(scopeIndex);
244

con's avatar
con committed
245
            for (Symbol *symbol = scope->lookat(id); symbol; symbol = symbol->next()) {
246
247
248
249
250
251
                if (! symbol->name())
                    continue; // nothing to do, the symbol is anonymous.

                else if (! maybeValidSymbol(symbol, mode, candidates))
                    continue; // skip it, we're not looking for this kind of symbols

Roberto Raggi's avatar
Roberto Raggi committed
252
                else if (const Identifier *symbolId = symbol->identifier()) {
253
254
255
256
                    if (! symbolId->isEqualTo(id))
                        continue; // skip it, the symbol's id is not compatible with this lookup.
                }

Roberto Raggi's avatar
Roberto Raggi committed
257
                if (const QualifiedNameId *q = symbol->name()->asQualifiedNameId()) {
258
259

                    if (name->isDestructorNameId() != q->unqualifiedNameId()->isDestructorNameId())
260
261
                        continue;

262
                    else if (q->nameCount() > 1) {
Roberto Raggi's avatar
Roberto Raggi committed
263
264
                        const Name *classOrNamespaceName = control()->qualifiedNameId(q->names(),
                                                                                      q->nameCount() - 1);
265

Roberto Raggi's avatar
Roberto Raggi committed
266
                        if (const Identifier *classOrNamespaceNameId = identifier(classOrNamespaceName)) {
267
268
269
                            if (classOrNamespaceNameId->isEqualTo(id))
                                continue;
                        }
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

                        const QList<Symbol *> resolvedClassOrNamespace =
                                resolveClassOrNamespace(classOrNamespaceName, visibleScopes);

                        bool good = false;
                        foreach (Symbol *classOrNamespace, resolvedClassOrNamespace) {
                            ScopedSymbol *scoped = classOrNamespace->asScopedSymbol();
                            if (visibleScopes.contains(scoped->members())) {
                                good = true;
                                break;
                            }
                        }

                        if (! good)
                            continue;
                    }
                } else if (symbol->name()->isDestructorNameId() != name->isDestructorNameId()) {
287
                    // ### FIXME: this is wrong!
con's avatar
con committed
288
289
                    continue;
                }
290

291
292
                if (! candidates.contains(symbol))
                    candidates.append(symbol);
con's avatar
con committed
293
294
295
296
297
298
299
            }
        }
    }

    return candidates;
}

Roberto Raggi's avatar
Roberto Raggi committed
300
const Identifier *LookupContext::identifier(const Name *name) const
Roberto Raggi's avatar
Roberto Raggi committed
301
302
303
304
305
306
307
{
    if (name)
        return name->identifier();

    return 0;
}

308
309
310
311
312
313
314
315
316
317
void LookupContext::buildVisibleScopes_helper(Document::Ptr doc, QList<Scope *> *scopes,
                                              QSet<QString> *processed)
{
    if (doc && ! processed->contains(doc->fileName())) {
        processed->insert(doc->fileName());

        if (doc->globalSymbolCount())
            scopes->append(doc->globalSymbols());

        foreach (const Document::Include &incl, doc->includes()) {
318
            buildVisibleScopes_helper(_snapshot.document(incl.fileName()),
319
320
321
322
323
                                      scopes, processed);
        }
    }
}

con's avatar
con committed
324
325
326
327
328
QList<Scope *> LookupContext::buildVisibleScopes()
{
    QList<Scope *> scopes;

    if (_symbol) {
329
330
331
332
333
334
        Scope *scope = _symbol->scope();

        if (Function *fun = _symbol->asFunction())
            scope = fun->members(); // handle ctor initializers.

        for (; scope; scope = scope->enclosingScope()) {
335
336
337
            if (scope == _thisDocument->globalSymbols())
                break;

con's avatar
con committed
338
339
340
341
342
            scopes.append(scope);
        }
    }

    QSet<QString> processed;
343
    buildVisibleScopes_helper(_thisDocument, &scopes, &processed);
con's avatar
con committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357

    while (true) {
        QList<Scope *> expandedScopes;
        expand(scopes, &expandedScopes);

        if (expandedScopes.size() == scopes.size())
            return expandedScopes;

        scopes = expandedScopes;
    }

    return scopes;
}

358
359
QList<Scope *> LookupContext::visibleScopes(const LookupItem &result) const
{ return visibleScopes(result.lastVisibleSymbol()); }
360
361

QList<Scope *> LookupContext::visibleScopes(Symbol *symbol) const
con's avatar
con committed
362
363
364
365
366
367
368
369
370
{
    QList<Scope *> scopes;
    for (Scope *scope = symbol->scope(); scope; scope = scope->enclosingScope())
        scopes.append(scope);
    scopes += visibleScopes();
    scopes = expand(scopes);
    return scopes;
}

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
void LookupContext::expandEnumOrAnonymousSymbol(ScopedSymbol *scopedSymbol,
                                                QList<Scope *> *expandedScopes) const
{
    if (! scopedSymbol || expandedScopes->contains(scopedSymbol->members()))
        return;

    Scope *members = scopedSymbol->members();

    if (scopedSymbol->isEnum())
        expandedScopes->append(members);
    else if (! scopedSymbol->name() && (scopedSymbol->isClass() || scopedSymbol->isNamespace())) {
        // anonymous class or namespace

        expandedScopes->append(members);

        for (unsigned i = 0; i < members->symbolCount(); ++i) {
            Symbol *member = members->symbolAt(i);

            if (ScopedSymbol *nested = member->asScopedSymbol()) {
                expandEnumOrAnonymousSymbol(nested, expandedScopes);
            }
        }
    }
}

con's avatar
con committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
QList<Scope *> LookupContext::expand(const QList<Scope *> &scopes) const
{
    QList<Scope *> expanded;
    expand(scopes, &expanded);
    return expanded;
}

void LookupContext::expand(const QList<Scope *> &scopes, QList<Scope *> *expandedScopes) const
{
    for (int i = 0; i < scopes.size(); ++i) {
        expand(scopes.at(i), scopes, expandedScopes);
    }
}

410
void LookupContext::expandNamespace(Namespace *ns,
411
412
                                    const QList<Scope *> &visibleScopes,
                                    QList<Scope *> *expandedScopes) const
con's avatar
con committed
413
{
414
415
416
417
418
    //qDebug() << "*** expand namespace:" << ns->fileName() << ns->line() << ns->column();

    if (Scope *encl = ns->enclosingNamespaceScope())
        expand(encl, visibleScopes, expandedScopes);

Roberto Raggi's avatar
Roberto Raggi committed
419
    if (const Name *nsName = ns->name()) {
420
421
422
423
424
425
426
        const QList<Symbol *> namespaceList = resolveNamespace(nsName, visibleScopes);
        foreach (Symbol *otherNs, namespaceList) {
            if (otherNs == ns)
                continue;
            expand(otherNs->asNamespace()->members(), visibleScopes, expandedScopes);
        }
    }
con's avatar
con committed
427

428
429
430
431
    for (unsigned i = 0; i < ns->memberCount(); ++i) { // ### make me fast
        Symbol *symbol = ns->memberAt(i);
        if (Namespace *otherNs = symbol->asNamespace()) {
            if (! otherNs->name()) {
432
                expand(otherNs->members(), visibleScopes, expandedScopes);
con's avatar
con committed
433
            }
434
435
436
437
438
        } else if (UsingNamespaceDirective *u = symbol->asUsingNamespaceDirective()) {
            const QList<Symbol *> candidates = resolveNamespace(u->name(), visibleScopes);
            for (int j = 0; j < candidates.size(); ++j) {
                expand(candidates.at(j)->asNamespace()->members(),
                       visibleScopes, expandedScopes);
con's avatar
con committed
439
            }
440
441
        } else if (Enum *e = symbol->asEnum()) {
            expand(e->members(), visibleScopes, expandedScopes);
con's avatar
con committed
442
        }
443
444
445
    }
}

446
void LookupContext::expandClass(Class *klass,
447
448
449
                                const QList<Scope *> &visibleScopes,
                                QList<Scope *> *expandedScopes) const
{
450
451
452
    for (TemplateParameters *params = klass->templateParameters(); params; params = params->previous())
        expand(params->scope(), visibleScopes, expandedScopes);

453
454
    for (unsigned i = 0; i < klass->memberCount(); ++i) {
        Symbol *symbol = klass->memberAt(i);
455
456
457
        if (Class *nestedClass = symbol->asClass()) {
            if (! nestedClass->name()) {
                expand(nestedClass->members(), visibleScopes, expandedScopes);
con's avatar
con committed
458
            }
459
460
        } else if (Enum *e = symbol->asEnum()) {
            expand(e->members(), visibleScopes, expandedScopes);
con's avatar
con committed
461
        }
462
    }
con's avatar
con committed
463

464
465
466
467
468
469
470
471
472
473
474
    if (klass->baseClassCount()) {
        QList<Scope *> classVisibleScopes = visibleScopes;
        for (Scope *scope = klass->scope(); scope; scope = scope->enclosingScope()) {
            if (scope->isNamespaceScope()) {
                Namespace *enclosingNamespace = scope->owner()->asNamespace();
                if (enclosingNamespace->name()) {
                    const QList<Symbol *> nsList = resolveNamespace(enclosingNamespace->name(),
                                                                    visibleScopes);
                    foreach (Symbol *ns, nsList) {
                        expand(ns->asNamespace()->members(), classVisibleScopes,
                               &classVisibleScopes);
con's avatar
con committed
475
476
477
478
                    }
                }
            }
        }
479
480
481

        for (unsigned i = 0; i < klass->baseClassCount(); ++i) {
            BaseClass *baseClass = klass->baseClassAt(i);
Roberto Raggi's avatar
Roberto Raggi committed
482
            const Name *baseClassName = baseClass->name();
483
484
            const QList<Symbol *> baseClassCandidates = resolveClass(baseClassName,
                                                                     classVisibleScopes);
485

486
            for (int j = 0; j < baseClassCandidates.size(); ++j) {
487
488
                if (Class *baseClassSymbol = baseClassCandidates.at(j)->asClass())
                    expand(baseClassSymbol->members(), visibleScopes, expandedScopes);
489
            }
con's avatar
con committed
490
        }
491
492
493
    }
}

494
void LookupContext::expandBlock(Block *blockSymbol,
495
496
497
                                const QList<Scope *> &visibleScopes,
                                QList<Scope *> *expandedScopes) const
{
498
499
    for (unsigned i = 0; i < blockSymbol->memberCount(); ++i) {
        Symbol *symbol = blockSymbol->memberAt(i);
500
501
502
        if (UsingNamespaceDirective *u = symbol->asUsingNamespaceDirective()) {
            const QList<Symbol *> candidates = resolveNamespace(u->name(),
                                                                visibleScopes);
con's avatar
con committed
503
            for (int j = 0; j < candidates.size(); ++j) {
504
                expand(candidates.at(j)->asNamespace()->members(),
con's avatar
con committed
505
506
507
                       visibleScopes, expandedScopes);
            }
        }
508
509
510
511

    }
}

512
void LookupContext::expandFunction(Function *function,
513
514
515
                                   const QList<Scope *> &visibleScopes,
                                   QList<Scope *> *expandedScopes) const
{
516
517
518
    for (TemplateParameters *params = function->templateParameters(); params; params = params->previous())
        expand(params->scope(), visibleScopes, expandedScopes);

519
520
    if (! expandedScopes->contains(function->arguments()))
        expandedScopes->append(function->arguments());
521

Roberto Raggi's avatar
Roberto Raggi committed
522
523
    if (const QualifiedNameId *q = function->name()->asQualifiedNameId()) {
        const Name *nestedNameSpec = 0;
524
        if (q->nameCount() == 1)
525
526
527
528
529
530
            nestedNameSpec = q->nameAt(0);
        else
            nestedNameSpec = control()->qualifiedNameId(q->names(), q->nameCount() - 1,
                                                        q->isGlobal());
        const QList<Symbol *> candidates = resolveClassOrNamespace(nestedNameSpec, visibleScopes);
        for (int j = 0; j < candidates.size(); ++j) {
531
532
            if (ScopedSymbol *scopedSymbol = candidates.at(j)->asScopedSymbol())
                expand(scopedSymbol->members(), visibleScopes, expandedScopes);
533
534
535
536
        }
    }
}

537
void LookupContext::expandObjCMethod(ObjCMethod *method,
538
                                     const QList<Scope *> &,
539
540
541
542
543
544
                                     QList<Scope *> *expandedScopes) const
{
    if (! expandedScopes->contains(method->arguments()))
        expandedScopes->append(method->arguments());
}

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
void LookupContext::expandObjCClass(ObjCClass *klass,
                                    const QList<Scope *> &visibleScopes,
                                    QList<Scope *> *expandedScopes) const
{
    {// expand other @interfaces, @implementations and categories for this class:
        const QList<Symbol *> classList = resolveObjCClass(klass->name(), visibleScopes);
        foreach (Symbol *otherClass, classList) {
            if (otherClass == klass)
                continue;
            expand(otherClass->asObjCClass()->members(), visibleScopes, expandedScopes);
        }
    }

    // expand definitions in the currect class:
    for (unsigned i = 0; i < klass->memberCount(); ++i) {
        Symbol *symbol = klass->memberAt(i);
        if (Class *nestedClass = symbol->asClass()) {
            if (! nestedClass->name()) {
                expand(nestedClass->members(), visibleScopes, expandedScopes);
            }
        } else if (Enum *e = symbol->asEnum()) {
            expand(e->members(), visibleScopes, expandedScopes);
        }
    }

    // expand the base class:
    if (ObjCBaseClass *baseClass = klass->baseClass()) {
Roberto Raggi's avatar
Roberto Raggi committed
572
        const Name *baseClassName = baseClass->name();
573
574
575
576
577
578
579
580
581
582
583
        const QList<Symbol *> baseClassCandidates = resolveObjCClass(baseClassName,
                                                                     visibleScopes);

        for (int j = 0; j < baseClassCandidates.size(); ++j) {
            if (ObjCClass *baseClassSymbol = baseClassCandidates.at(j)->asObjCClass())
                expand(baseClassSymbol->members(), visibleScopes, expandedScopes);
        }
    }

    // expand the protocols:
    for (unsigned i = 0; i < klass->protocolCount(); ++i) {
Roberto Raggi's avatar
Roberto Raggi committed
584
        const Name *protocolName = klass->protocolAt(i)->name();
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
        const QList<Symbol *> protocolCandidates = resolveObjCProtocol(protocolName, visibleScopes);
        for (int j = 0; j < protocolCandidates.size(); ++j) {
            if (ObjCProtocol *protocolSymbol = protocolCandidates.at(j)->asObjCProtocol())
                expandObjCProtocol(protocolSymbol, visibleScopes, expandedScopes);
        }
    }
}

void LookupContext::expandObjCProtocol(ObjCProtocol *protocol, const QList<Scope *> &visibleScopes, QList<Scope *> *expandedScopes) const
{
    // First expand the protocol itself
    expand(protocol->members(), visibleScopes, expandedScopes);

    // Then do the same for any incorporated protocol
    for (unsigned i = 0; i < protocol->protocolCount(); ++i) {
        ObjCBaseProtocol *baseProtocol = protocol->protocolAt(i);
        const QList<Symbol *> protocolList = resolveObjCProtocol(baseProtocol->name(), visibleScopes);
        foreach (Symbol *symbol, protocolList)
            if (ObjCProtocol *protocolSymbol = symbol->asObjCProtocol())
                expandObjCProtocol(protocolSymbol, visibleScopes, expandedScopes);
    }
}

608
609
610
611
612
613
614
615
616
void LookupContext::expand(Scope *scope,
                           const QList<Scope *> &visibleScopes,
                           QList<Scope *> *expandedScopes) const
{
    if (expandedScopes->contains(scope))
        return;

    expandedScopes->append(scope);

617
618
619
620
621
622
623
624
    if (Namespace *ns = scope->owner()->asNamespace()) {
        expandNamespace(ns, visibleScopes, expandedScopes);
    } else if (Class *klass = scope->owner()->asClass()) {
        expandClass(klass, visibleScopes, expandedScopes);
    } else if (Block *block = scope->owner()->asBlock()) {
        expandBlock(block, visibleScopes, expandedScopes);
    } else if (Function *fun = scope->owner()->asFunction()) {
        expandFunction(fun, visibleScopes, expandedScopes);
625
626
    } else if (ObjCMethod *meth = scope->owner()->asObjCMethod()) {
        expandObjCMethod(meth, visibleScopes, expandedScopes);
627
628
    } else if (ObjCClass *objcKlass = scope->owner()->asObjCClass()) {
        expandObjCClass(objcKlass, visibleScopes, expandedScopes);
con's avatar
con committed
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
static void visibleClassBindings_helper(ClassBinding *classBinding,
                                        QList<ClassBinding *> *allClassBindings,
                                        QSet<ClassBinding *> *processed)
{
    if (! classBinding)
        return;

    else if (processed->contains(classBinding))
        return;

    processed->insert(classBinding);

    foreach (ClassBinding *baseClassBinding, classBinding->baseClassBindings)
        visibleClassBindings_helper(baseClassBinding, allClassBindings, processed);

    allClassBindings->append(classBinding);
}

static QList<ClassBinding *> visibleClassBindings(Symbol *symbol, NamespaceBinding *globalNamespace)
{
    QList<ClassBinding *> classBindings;

    if (! symbol)
        return classBindings;

    else if (Class *klass = symbol->asClass()) {
        QSet<ClassBinding *> processed;

        visibleClassBindings_helper(NamespaceBinding::find(klass, globalNamespace),
                                    &classBindings, &processed);
    }

    return classBindings;
}

Symbol *LookupContext::canonicalSymbol(Symbol *symbol,
                                       NamespaceBinding *globalNamespace)
{
    Symbol *canonicalSymbol = LookupContext::canonicalSymbol(symbol);
671
672
    if (! canonicalSymbol)
        return 0;
673

Roberto Raggi's avatar
Roberto Raggi committed
674
    if (const Identifier *symbolId = canonicalSymbol->identifier()) {
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
        if (symbolId && canonicalSymbol->type()->isFunctionType()) {
            Class *enclosingClass = canonicalSymbol->scope()->owner()->asClass();
            const QList<ClassBinding *> classBindings = visibleClassBindings(enclosingClass, globalNamespace);

            foreach (ClassBinding *baseClassBinding, classBindings) {
                if (! baseClassBinding)
                    continue;

                foreach (Class *baseClass, baseClassBinding->symbols) {
                    if (! baseClass)
                        continue;

                    for (Symbol *c = baseClass->members()->lookat(symbolId); c; c = c->next()) {
                        if (! symbolId->isEqualTo(c->identifier()))
                            continue;
                        else if (Function *f = c->type()->asFunctionType()) {
                            if (f->isVirtual())
                                return LookupContext::canonicalSymbol(f);
                        }
                    }
                }
            }
        }
    }

    return canonicalSymbol;
}

Symbol *LookupContext::canonicalSymbol(const QList<Symbol *> &candidates,
                                       NamespaceBinding *globalNamespaceBinding)
{
    if (candidates.isEmpty())
        return 0;

    return canonicalSymbol(candidates.first(), globalNamespaceBinding);
}

712
Symbol *LookupContext::canonicalSymbol(const QList<LookupItem> &results,
713
714
715
716
                                       NamespaceBinding *globalNamespaceBinding)
{
    QList<Symbol *> candidates;

717
718
    foreach (const LookupItem &result, results)
        candidates.append(result.lastVisibleSymbol()); // ### not exacly.
719
720
721
722
723

    return canonicalSymbol(candidates, globalNamespaceBinding);
}


724
725
726
Symbol *LookupContext::canonicalSymbol(Symbol *symbol)
{
    Symbol *canonical = symbol;
727
    Class *canonicalClass = 0;
728

729
    for (; symbol; symbol = symbol->next()) {
730
        if (symbol->identifier() == canonical->identifier()) {
731
            canonical = symbol;
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749

            if (Class *klass = symbol->asClass())
                canonicalClass = klass;
        }
    }

    if (canonicalClass) {
        Q_ASSERT(canonical != 0);

        if (canonical->isForwardClassDeclaration())
            return canonicalClass; // prefer class declarations when available.
    }

    if (canonical && canonical->scope()->isClassScope()) {
        Class *enclosingClass = canonical->scope()->owner()->asClass();

        if (enclosingClass->identifier() == canonical->identifier())
            return enclosingClass;
750
    }
751

752
753
    return canonical;
}