LookupContext.cpp 28.9 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "LookupContext.h"
#include "ResolveExpression.h"
#include "Overview.h"
33
#include "DeprecatedGenTemplateInstance.h"
34
35
36
37
38
39
40
41

#include <CoreTypes.h>
#include <Symbols.h>
#include <Literals.h>
#include <Names.h>
#include <Scope.h>
#include <Control.h>

42
43
44
45
46
47
#include <QtCore/QStack>
#include <QtCore/QHash>
#include <QtCore/QVarLengthArray>
#include <QtCore/QtDebug>

using namespace CPlusPlus;
48

Roberto Raggi's avatar
Roberto Raggi committed
49
namespace {
50
51
const bool debug = ! qgetenv("CPLUSPLUS_LOOKUPCONTEXT_DEBUG").isEmpty();
} // end of anonymous namespace
Roberto Raggi's avatar
Roberto Raggi committed
52

53

54
static void addNames(const Name *name, QList<const Name *> *names, bool addAllNames = false)
55
56
57
58
59
{
    if (! name)
        return;
    else if (const QualifiedNameId *q = name->asQualifiedNameId()) {
        addNames(q->base(), names);
60
61
        addNames(q->name(), names, addAllNames);
    } else if (addAllNames || name->isNameId() || name->isTemplateNameId()) {
62
        names->append(name);
63
64
65
    }
}

66
static void path_helper(Symbol *symbol, QList<const Name *> *names)
67
68
69
70
{
    if (! symbol)
        return;

Roberto Raggi's avatar
Roberto Raggi committed
71
    path_helper(symbol->scope(), names);
72

73
74
    if (symbol->name()) {
        if (symbol->isClass() || symbol->isNamespace()) {
75
            addNames(symbol->name(), names);
76

77
        } else if (symbol->isObjCClass() || symbol->isObjCBaseClass() || symbol->isObjCProtocol()
78
79
                || symbol->isObjCForwardClassDeclaration() || symbol->isObjCForwardProtocolDeclaration()
                || symbol->isForwardClassDeclaration()) {
80
81
            addNames(symbol->name(), names);

82
        } else if (symbol->isFunction()) {
83
84
            if (const QualifiedNameId *q = symbol->name()->asQualifiedNameId())
                addNames(q->base(), names);
85
86
87
88
        }
    }
}

89
90
91
92
bool ClassOrNamespace::CompareName::operator()(const Name *name, const Name *other) const
{
    Q_ASSERT(name != 0);
    Q_ASSERT(other != 0);
93

94
95
    const Identifier *id = name->identifier();
    const Identifier *otherId = other->identifier();
96
    return strcmp(id->chars(), otherId->chars()) < 0;
97
98
}

99
100
101
102
/////////////////////////////////////////////////////////////////////
// LookupContext
/////////////////////////////////////////////////////////////////////
LookupContext::LookupContext()
103
    : _control(new Control())
104
105
106
107
108
109
{ }

LookupContext::LookupContext(Document::Ptr thisDocument,
                             const Snapshot &snapshot)
    : _expressionDocument(Document::create("<LookupContext>")),
      _thisDocument(thisDocument),
110
111
      _snapshot(snapshot),
      _control(new Control())
112
113
114
115
116
117
118
119
{
}

LookupContext::LookupContext(Document::Ptr expressionDocument,
                             Document::Ptr thisDocument,
                             const Snapshot &snapshot)
    : _expressionDocument(expressionDocument),
      _thisDocument(thisDocument),
120
121
      _snapshot(snapshot),
      _control(new Control())
122
123
124
125
{
}

LookupContext::LookupContext(const LookupContext &other)
126
    : _expressionDocument(other._expressionDocument),
127
128
      _thisDocument(other._thisDocument),
      _snapshot(other._snapshot),
129
130
      _bindings(other._bindings),
      _control(other._control)
131
132
133
134
135
136
137
138
{ }

LookupContext &LookupContext::operator = (const LookupContext &other)
{
    _expressionDocument = other._expressionDocument;
    _thisDocument = other._thisDocument;
    _snapshot = other._snapshot;
    _bindings = other._bindings;
139
    _control = other._control;
140
141
142
    return *this;
}

143
QList<const Name *> LookupContext::fullyQualifiedName(Symbol *symbol)
144
{
Roberto Raggi's avatar
Roberto Raggi committed
145
    QList<const Name *> qualifiedName = path(symbol->scope());
146
147
148
149
150
    addNames(symbol->name(), &qualifiedName, /*add all names*/ true);
    return qualifiedName;
}

QList<const Name *> LookupContext::path(Symbol *symbol)
151
152
{
    QList<const Name *> names;
153
    path_helper(symbol, &names);
154
155
156
    return names;
}

157

158
const Name *LookupContext::minimalName(const Name *name,
159
                                       Scope *scope,
160
161
                                       ClassOrNamespace *target) const
{
162
163
164
165
    Q_UNUSED(name);
    Q_UNUSED(scope);
    Q_UNUSED(target);

166
    qWarning() << "TODO:" << Q_FUNC_INFO;
167
    return name;
168
169

#if 0
170
171
172
173
174
175
176
177
178
179
180
    Q_ASSERT(name);
    Q_ASSERT(source);
    Q_ASSERT(target);

    QList<Symbol *> symbols = lookup(name, source);
    if (symbols.isEmpty())
        return 0;

    Symbol *canonicalSymbol = symbols.first();
    std::vector<const Name *> fqNames = fullyQualifiedName(canonicalSymbol).toVector().toStdVector();
    if (const QualifiedNameId *qId = name->asQualifiedNameId())
181
        fqNames.push_back(qId->name());
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    else
        fqNames.push_back(name);

    const QualifiedNameId *lastWorking = 0;
    for (unsigned i = 0; i < fqNames.size(); ++i) {
        const QualifiedNameId *newName = control()->qualifiedNameId(&fqNames[i],
                                                                    fqNames.size() - i);
        QList<Symbol *> candidates = target->lookup(newName);
        if (candidates.contains(canonicalSymbol))
            lastWorking = newName;
        else
            break;
    }

    if (lastWorking && lastWorking->nameCount() == 1)
        return lastWorking->nameAt(0);
    else
        return lastWorking;
200
#endif
201
202
203
}


204
205
206
QSharedPointer<CreateBindings> LookupContext::bindings() const
{
    if (! _bindings)
207
        _bindings = QSharedPointer<CreateBindings>(new CreateBindings(_thisDocument, _snapshot, control()));
208
209
210
211
212
213
214
215
216

    return _bindings;
}

void LookupContext::setBindings(QSharedPointer<CreateBindings> bindings)
{
    _bindings = bindings;
}

217
218
219
220
QSharedPointer<Control> LookupContext::control() const
{
    return _control;
}
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

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

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

Document::Ptr LookupContext::document(const QString &fileName) const
{ return _snapshot.document(fileName); }

Snapshot LookupContext::snapshot() const
{ return _snapshot; }

ClassOrNamespace *LookupContext::globalNamespace() const
{
    return bindings()->globalNamespace();
}

239
ClassOrNamespace *LookupContext::lookupType(const Name *name, Scope *scope) const
240
{
Roberto Raggi's avatar
Roberto Raggi committed
241
    if (ClassOrNamespace *b = bindings()->lookupType(scope))
242
        return b->lookupType(name);
243
244
245
246

    return 0;
}

247
ClassOrNamespace *LookupContext::lookupType(Symbol *symbol) const
248
{
Roberto Raggi's avatar
Roberto Raggi committed
249
    return bindings()->lookupType(symbol);
250
251
}

252
QList<LookupItem> LookupContext::lookup(const Name *name, Scope *scope) const
253
{
254
    QList<LookupItem> candidates;
255
256
257
258

    if (! name)
        return candidates;

Roberto Raggi's avatar
Roberto Raggi committed
259
260
    for (; scope; scope = scope->scope()) {
        if ((name->isNameId() || name->isTemplateNameId()) && scope->isBlock()) {
261
            bindings()->lookupInScope(name, scope, &candidates, /*templateId = */ 0, /*binding=*/ 0);
262
263
264
265

            if (! candidates.isEmpty())
                break; // it's a local.

Roberto Raggi's avatar
Roberto Raggi committed
266
267
            for (unsigned index = 0; index < scope->memberCount(); ++index) {
                Symbol *member = scope->memberAt(index);
268
269

                if (UsingNamespaceDirective *u = member->asUsingNamespaceDirective()) {
Roberto Raggi's avatar
Roberto Raggi committed
270
                    if (Namespace *enclosingNamespace = u->enclosingNamespace()->asNamespace()) {
Roberto Raggi's avatar
Roberto Raggi committed
271
                        if (ClassOrNamespace *b = bindings()->lookupType(enclosingNamespace)) {
272
                            if (ClassOrNamespace *uu = b->lookupType(u->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
273
                                candidates = uu->find(name);
274
275
276
277
278

                                if (! candidates.isEmpty())
                                    return candidates;
                            }
                        }
279
280
281
282
                    }
                }
            }

Roberto Raggi's avatar
Roberto Raggi committed
283
284
        } else if (Function *fun = scope->asFunction()) {
            bindings()->lookupInScope(name, fun, &candidates, /*templateId = */ 0, /*binding=*/ 0);
285

Roberto Raggi's avatar
Roberto Raggi committed
286
287
#warning robe: lookup in template parameters
#if 0
288
            for (TemplateParameters *it = fun->templateParameters(); it && candidates.isEmpty(); it = it->previous())
289
                bindings()->lookupInScope(name, it->scope(), &candidates, /* templateId = */ 0, /*binding=*/ 0);
Roberto Raggi's avatar
Roberto Raggi committed
290
#endif
291

292
            if (! candidates.isEmpty())
293
                break; // it's an argument or a template parameter.
294
295

            if (fun->name() && fun->name()->isQualifiedNameId()) {
296
                if (ClassOrNamespace *binding = bindings()->lookupType(fun)) {
Roberto Raggi's avatar
Roberto Raggi committed
297
                    candidates = binding->find(name);
298
299
300
301

                    if (! candidates.isEmpty())
                        return candidates;
                }
302
303
            }

304
305
            // contunue, and look at the enclosing scope.

Roberto Raggi's avatar
Roberto Raggi committed
306
307
        } else if (ObjCMethod *method = scope->asObjCMethod()) {
            bindings()->lookupInScope(name, method, &candidates, /*templateId = */ 0, /*binding=*/ 0);
308

309
310
311
            if (! candidates.isEmpty())
                break; // it's a formal argument.

Roberto Raggi's avatar
Roberto Raggi committed
312
        } else if (Class *klass = scope->asClass()) {
Roberto Raggi's avatar
Roberto Raggi committed
313
314
#warning robe: lookup in template parameters
#if 0
315
            for (TemplateParameters *it = klass->templateParameters(); it && candidates.isEmpty(); it = it->previous())
316
                bindings()->lookupInScope(name, it->scope(), &candidates, /* templateId = */ 0, /*binding=*/ 0);
Roberto Raggi's avatar
Roberto Raggi committed
317
#endif
318
319
320
321
322

            if (! candidates.isEmpty())
                break; // it's an argument or a template parameter.

            if (ClassOrNamespace *binding = bindings()->lookupType(klass)) {
Roberto Raggi's avatar
Roberto Raggi committed
323
                candidates = binding->find(name);
324
325
326
327
328

                if (! candidates.isEmpty())
                    return candidates;
            }

Roberto Raggi's avatar
Roberto Raggi committed
329
330
        } else if (Namespace *ns = scope->asNamespace()) {
            if (ClassOrNamespace *binding = bindings()->lookupType(ns))
Roberto Raggi's avatar
Roberto Raggi committed
331
                candidates = binding->find(name);
332

Roberto Raggi's avatar
Roberto Raggi committed
333
334
335
                if (! candidates.isEmpty())
                    return candidates;

Roberto Raggi's avatar
Roberto Raggi committed
336
337
        } else if (scope->isObjCClass() || scope->isObjCProtocol()) {
            if (ClassOrNamespace *binding = bindings()->lookupType(scope))
Roberto Raggi's avatar
Roberto Raggi committed
338
339
340
341
                candidates = binding->find(name);

                if (! candidates.isEmpty())
                    return candidates;
342
343
344
345
346
347
        }
    }

    return candidates;
}

348
349
ClassOrNamespace *LookupContext::lookupParent(Symbol *symbol) const
{
350
    QList<const Name *> fqName = path(symbol);
351
352
353
354
355
356
357
358
359
360
    ClassOrNamespace *binding = globalNamespace();
    foreach (const Name *name, fqName) {
        binding = binding->findType(name);
        if (!binding)
            return 0;
    }

    return binding;
}

361
ClassOrNamespace::ClassOrNamespace(CreateBindings *factory, ClassOrNamespace *parent)
362
    : _factory(factory), _parent(parent), _templateId(0)
363
364
365
{
}

366
367
368
369
370
const TemplateNameId *ClassOrNamespace::templateId() const
{
    return _templateId;
}

371
372
373
374
375
ClassOrNamespace *ClassOrNamespace::parent() const
{
    return _parent;
}

376
377
378
379
380
381
382
383
384
385
386
387
388
389
QList<ClassOrNamespace *> ClassOrNamespace::usings() const
{
    const_cast<ClassOrNamespace *>(this)->flush();
    return _usings;
}

QList<Enum *> ClassOrNamespace::enums() const
{
    const_cast<ClassOrNamespace *>(this)->flush();
    return _enums;
}

QList<Symbol *> ClassOrNamespace::symbols() const
{
390
391
392
    if (_templateId && ! _usings.isEmpty())
        return _usings.first()->symbols(); // ask to the base implementation

393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
    const_cast<ClassOrNamespace *>(this)->flush();
    return _symbols;
}

ClassOrNamespace *ClassOrNamespace::globalNamespace() const
{
    ClassOrNamespace *e = const_cast<ClassOrNamespace *>(this);

    do {
        if (! e->_parent)
            break;

        e = e->_parent;
    } while (e);

    return e;
}

411
QList<LookupItem> ClassOrNamespace::find(const Name *name)
412
413
414
415
{
    return lookup_helper(name, false);
}

416
QList<LookupItem> ClassOrNamespace::lookup(const Name *name)
417
418
419
420
{
    return lookup_helper(name, true);
}

421
QList<LookupItem> ClassOrNamespace::lookup_helper(const Name *name, bool searchInEnclosingScope)
422
{
423
    QList<LookupItem> result;
424

425
426
    if (name) {
        if (const QualifiedNameId *q = name->asQualifiedNameId()) {
427
428
            if (! q->base())
                result = globalNamespace()->find(q->name());
429

430
431
            else if (ClassOrNamespace *binding = lookupType(q->base()))
                result = binding->find(q->name());
432

433
434
            return result;
        }
435

436
437
438
439
440
441
442
        QSet<ClassOrNamespace *> processed;
        ClassOrNamespace *binding = this;
        do {
            lookup_helper(name, binding, &result, &processed, /*templateId = */ 0);
            binding = binding->_parent;
        } while (searchInEnclosingScope && binding);
    }
443
444
445
446
447

    return result;
}

void ClassOrNamespace::lookup_helper(const Name *name, ClassOrNamespace *binding,
448
449
450
                                          QList<LookupItem> *result,
                                          QSet<ClassOrNamespace *> *processed,
                                          const TemplateNameId *templateId)
451
{
Roberto Raggi's avatar
Roberto Raggi committed
452
    if (binding && ! processed->contains(binding)) {
453
454
        processed->insert(binding);

455
456
        const Identifier *nameId = name->identifier();

457
        foreach (Symbol *s, binding->symbols()) {
458
459
460
            if (s->isFriend())
                continue;

Roberto Raggi's avatar
Roberto Raggi committed
461
462
            if (Scope *scope = s->asScope()) {
                if (Class *klass = scope->asClass()) {
463
                    if (const Identifier *id = klass->identifier()) {
464
465
466
467
468
469
                        if (nameId && nameId->isEqualTo(id)) {
                            LookupItem item;
                            item.setDeclaration(klass);
                            item.setBinding(binding);
                            result->append(item);
                        }
470
471
                    }
                }
Roberto Raggi's avatar
Roberto Raggi committed
472
                _factory->lookupInScope(name, scope, result, templateId, binding);
473
            }
474
475
476
        }

        foreach (Enum *e, binding->enums())
Roberto Raggi's avatar
Roberto Raggi committed
477
            _factory->lookupInScope(name, e, result, templateId, binding);
478
479

        foreach (ClassOrNamespace *u, binding->usings())
480
            lookup_helper(name, u, result, processed, binding->_templateId);
481
482
483
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
484
void CreateBindings::lookupInScope(const Name *name, Scope *scope,
485
486
487
                                   QList<LookupItem> *result,
                                   const TemplateNameId *templateId,
                                   ClassOrNamespace *binding)
488
{
489
490
    Q_UNUSED(templateId);

491
492
493
494
    if (! name) {
        return;

    } else if (const OperatorNameId *op = name->asOperatorNameId()) {
Roberto Raggi's avatar
Roberto Raggi committed
495
        for (Symbol *s = scope->find(op->kind()); s; s = s->next()) {
496
497
            if (! s->name())
                continue;
498
499
            else if (s->isFriend())
                continue;
500
501
            else if (! s->name()->isEqualTo(op))
                continue;
502

503
504
505
506
            LookupItem item;
            item.setDeclaration(s);
            item.setBinding(binding);
            result->append(item);
507
508
509
        }

    } else if (const Identifier *id = name->identifier()) {
Roberto Raggi's avatar
Roberto Raggi committed
510
        for (Symbol *s = scope->find(id); s; s = s->next()) {
511
512
513
            if (s->isFriend())
                continue; // skip friends
            else if (! id->isEqualTo(s->identifier()))
514
                continue;
Roberto Raggi's avatar
Roberto Raggi committed
515
516
            else if (s->name()->isQualifiedNameId())
                continue; // skip qualified ids.
517

518
519
520
            LookupItem item;
            item.setDeclaration(s);
            item.setBinding(binding);
521

522
            if (templateId && (s->isDeclaration() || s->isFunction())) {
Roberto Raggi's avatar
Roberto Raggi committed
523
                FullySpecifiedType ty = DeprecatedGenTemplateInstance::instantiate(templateId, s, _control);
524
                item.setType(ty); // override the type.
525
526
            }

527
            result->append(item);
528
529
530
531
        }
    }
}

532
ClassOrNamespace *ClassOrNamespace::lookupType(const Name *name)
533
534
535
536
537
{
    if (! name)
        return 0;

    QSet<ClassOrNamespace *> processed;
538
    return lookupType_helper(name, &processed, /*searchInEnclosingScope =*/ true);
539
540
}

541
ClassOrNamespace *ClassOrNamespace::findType(const Name *name)
542
543
{
    QSet<ClassOrNamespace *> processed;
544
    return lookupType_helper(name, &processed, /*searchInEnclosingScope =*/ false);
545
546
}

Roberto Raggi's avatar
Roberto Raggi committed
547
ClassOrNamespace *ClassOrNamespace::lookupType_helper(const Name *name,
548
549
                                                      QSet<ClassOrNamespace *> *processed,
                                                      bool searchInEnclosingScope)
550
{
551
    if (const QualifiedNameId *q = name->asQualifiedNameId()) {
552

553
554
        if (! q->base())
            return globalNamespace()->findType(q->name());
555

556
557
        else if (ClassOrNamespace *binding = lookupType(q->base()))
            return binding->findType(q->name());
558

559
        return 0;
560

561
562
    } else if (! processed->contains(this)) {
        processed->insert(this);
563

564
565
        if (name->isNameId() || name->isTemplateNameId()) {
            flush();
566

567
568
569
570
571
572
573
            foreach (Symbol *s, symbols()) {
                if (Class *klass = s->asClass()) {
                    if (klass->identifier() && klass->identifier()->isEqualTo(name->identifier()))
                        return this;
                }
            }

Roberto Raggi's avatar
Roberto Raggi committed
574
            if (ClassOrNamespace *e = nestedType(name))
575
576
                return e;

577
            else if (_templateId) {
578
579
580
581
582
583
584
585
586
                if (_usings.size() == 1) {
                    ClassOrNamespace *delegate = _usings.first();

                    if (ClassOrNamespace *r = delegate->lookupType_helper(name, processed, /*searchInEnclosingScope = */ true))
                        return r;
                } else {
                    if (debug)
                        qWarning() << "expected one using declaration. Number of using declarations is:" << _usings.size();
                }
587
588
            }

589
            foreach (ClassOrNamespace *u, usings()) {
590
                if (ClassOrNamespace *r = u->lookupType_helper(name, processed, /*searchInEnclosingScope =*/ false))
591
592
593
594
                    return r;
            }
        }

595
596
        if (_parent && searchInEnclosingScope)
            return _parent->lookupType_helper(name, processed, searchInEnclosingScope);
597
598
599
600
601
    }

    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
602
ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name) const
603
{
604
605
    Q_ASSERT(name != 0);
    Q_ASSERT(name->isNameId() || name->isTemplateNameId());
606

607
    const_cast<ClassOrNamespace *>(this)->flush();
608

609
    Table::const_iterator it = _classOrNamespaces.find(name);
610

611
612
    if (it == _classOrNamespaces.end())
        return 0;
613

614
615
616
617
618
619
620
621
622
623
    ClassOrNamespace *c = it->second;

    if (const TemplateNameId *templId = name->asTemplateNameId()) {
        ClassOrNamespace *i = _factory->allocClassOrNamespace(c);
        i->_templateId = templId;
        i->_usings.append(c);
        return i;
    }

    return c;
624
625
626
627
}

void ClassOrNamespace::flush()
{
Roberto Raggi's avatar
Roberto Raggi committed
628
629
630
    if (! _todo.isEmpty()) {
        const QList<Symbol *> todo = _todo;
        _todo.clear();
631

Roberto Raggi's avatar
Roberto Raggi committed
632
        foreach (Symbol *member, todo)
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
            _factory->process(member, this);
    }
}

void ClassOrNamespace::addSymbol(Symbol *symbol)
{
    _symbols.append(symbol);
}

void ClassOrNamespace::addTodo(Symbol *symbol)
{
    _todo.append(symbol);
}

void ClassOrNamespace::addEnum(Enum *e)
{
    _enums.append(e);
}

void ClassOrNamespace::addUsing(ClassOrNamespace *u)
{
    _usings.append(u);
}

Roberto Raggi's avatar
Roberto Raggi committed
657
void ClassOrNamespace::addNestedType(const Name *alias, ClassOrNamespace *e)
658
{
659
    _classOrNamespaces[alias] = e;
660
661
}

662
ClassOrNamespace *ClassOrNamespace::findOrCreateType(const Name *name)
663
664
665
666
667
{
    if (! name)
        return this;

    if (const QualifiedNameId *q = name->asQualifiedNameId()) {
668
669
        if (! q->base())
            return globalNamespace()->findOrCreateType(q->name());
670

671
        return findOrCreateType(q->base())->findOrCreateType(q->name());
672

673
    } else if (name->isNameId() || name->isTemplateNameId()) {
Roberto Raggi's avatar
Roberto Raggi committed
674
        ClassOrNamespace *e = nestedType(name);
675
676
677

        if (! e) {
            e = _factory->allocClassOrNamespace(this);
678
            _classOrNamespaces[name] = e;
679
680
681
682
683
684
685
686
        }

        return e;
    }

    return 0;
}

687
688
CreateBindings::CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot, QSharedPointer<Control> control)
    : _snapshot(snapshot), _control(control)
689
690
691
692
693
694
695
696
697
698
699
700
{
    _globalNamespace = allocClassOrNamespace(/*parent = */ 0);
    _currentClassOrNamespace = _globalNamespace;

    process(thisDocument);
}

CreateBindings::~CreateBindings()
{
    qDeleteAll(_entities);
}

Roberto Raggi's avatar
Roberto Raggi committed
701
ClassOrNamespace *CreateBindings::switchCurrentClassOrNamespace(ClassOrNamespace *classOrNamespace)
702
703
704
705
706
707
708
709
710
711
712
{
    ClassOrNamespace *previous = _currentClassOrNamespace;
    _currentClassOrNamespace = classOrNamespace;
    return previous;
}

ClassOrNamespace *CreateBindings::globalNamespace() const
{
    return _globalNamespace;
}

Roberto Raggi's avatar
Roberto Raggi committed
713
ClassOrNamespace *CreateBindings::lookupType(Symbol *symbol)
714
{
715
716
717
    const QList<const Name *> path = LookupContext::path(symbol);
    return lookupType(path);
}
718

719
720
721
ClassOrNamespace *CreateBindings::lookupType(const QList<const Name *> &path)
{
    if (path.isEmpty())
722
        return _globalNamespace;
723

724
    ClassOrNamespace *b = _globalNamespace->lookupType(path.at(0));
725

726
727
    for (int i = 1; b && i < path.size(); ++i)
        b = b->findType(path.at(i));
728
729

    return b;
730
731
732
733
}

void CreateBindings::process(Symbol *s, ClassOrNamespace *classOrNamespace)
{
Roberto Raggi's avatar
Roberto Raggi committed
734
    ClassOrNamespace *previous = switchCurrentClassOrNamespace(classOrNamespace);
735
    accept(s);
Roberto Raggi's avatar
Roberto Raggi committed
736
    (void) switchCurrentClassOrNamespace(previous);
737
738
739
740
741
742
743
}

void CreateBindings::process(Symbol *symbol)
{
    _currentClassOrNamespace->addTodo(symbol);
}

744
QSharedPointer<Control> CreateBindings::control() const
745
746
747
748
{
    return _control;
}

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
ClassOrNamespace *CreateBindings::allocClassOrNamespace(ClassOrNamespace *parent)
{
    ClassOrNamespace *e = new ClassOrNamespace(this, parent);
    _entities.append(e);
    return e;
}

void CreateBindings::process(Document::Ptr doc)
{
    if (! doc)
        return;

    else if (Namespace *globalNamespace = doc->globalNamespace()) {
        if (! _processed.contains(globalNamespace)) {
            _processed.insert(globalNamespace);

            foreach (const Document::Include &i, doc->includes()) {
                if (Document::Ptr incl = _snapshot.document(i.fileName()))
                    process(incl);
            }

            accept(globalNamespace);
        }
    }
}

775
ClassOrNamespace *CreateBindings::enterClassOrNamespaceBinding(Symbol *symbol)
776
{
777
    ClassOrNamespace *entity = _currentClassOrNamespace->findOrCreateType(symbol->name());
778
779
    entity->addSymbol(symbol);

Roberto Raggi's avatar
Roberto Raggi committed
780
    return switchCurrentClassOrNamespace(entity);
781
782
}

783
ClassOrNamespace *CreateBindings::enterGlobalClassOrNamespace(Symbol *symbol)
784
{
785
    ClassOrNamespace *entity = _globalNamespace->findOrCreateType(symbol->name());
786
787
    entity->addSymbol(symbol);

Roberto Raggi's avatar
Roberto Raggi committed
788
    return switchCurrentClassOrNamespace(entity);
789
790
}

791
792
bool CreateBindings::visit(Template *templ)
{
Roberto Raggi's avatar
Roberto Raggi committed
793
794
795
    if (Symbol *d = templ->declaration())
        accept(d);

796
797
798
    return false;
}

799
800
bool CreateBindings::visit(Namespace *ns)
{
801
    ClassOrNamespace *previous = enterClassOrNamespaceBinding(ns);
802
803
804
805
806
807
808
809
810
811

    for (unsigned i = 0; i < ns->memberCount(); ++i)
        process(ns->memberAt(i));

    _currentClassOrNamespace = previous;
    return false;
}

bool CreateBindings::visit(Class *klass)
{
812
813
814
815
    ClassOrNamespace *previous = _currentClassOrNamespace;
    ClassOrNamespace *binding = 0;

    if (klass->name() && klass->name()->isQualifiedNameId())
816
        binding = _currentClassOrNamespace->lookupType(klass->name());
817
818

    if (! binding)
819
        binding = _currentClassOrNamespace->findOrCreateType(klass->name());
820
821
822

    _currentClassOrNamespace = binding;
    _currentClassOrNamespace->addSymbol(klass);
823
824
825
826
827
828
829
830
831
832
833
834
835

    for (unsigned i = 0; i < klass->baseClassCount(); ++i)
        process(klass->baseClassAt(i));

    for (unsigned i = 0; i < klass->memberCount(); ++i)
        process(klass->memberAt(i));

    _currentClassOrNamespace = previous;
    return false;
}

bool CreateBindings::visit(ForwardClassDeclaration *klass)
{
836
    if (! klass->isFriend()) {
837
        ClassOrNamespace *previous = enterClassOrNamespaceBinding(klass);
838
839
840
        _currentClassOrNamespace = previous;
    }

841
842
843
844
845
846
847
848
849
850
851
852
    return false;
}

bool CreateBindings::visit(Enum *e)
{
    _currentClassOrNamespace->addEnum(e);
    return false;
}

bool CreateBindings::visit(Declaration *decl)
{
    if (decl->isTypedef()) {
853
        FullySpecifiedType ty = decl->type();
854
855
856
857
        const Identifier *typedefId = decl->identifier();

        if (typedefId && ! (ty.isConst() || ty.isVolatile())) {
            if (const NamedType *namedTy = ty->asNamedType()) {
858
                if (ClassOrNamespace *e = _currentClassOrNamespace->lookupType(namedTy->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
859
                    _currentClassOrNamespace->addNestedType(decl->name(), e);
860
861
862
863
                } else if (false) {
                    Overview oo;
                    qDebug() << "found entity not found for" << oo(namedTy->name());
                }
864
865
866
867
868
            } else if (Class *klass = ty->asClassType()) {
                if (const NameId *nameId = decl->name()->asNameId()) {
                    ClassOrNamespace *binding = _currentClassOrNamespace->findOrCreateType(nameId);
                    binding->addSymbol(klass);
                }
869
870
871
872
873
874
875
876
877
878
879
880
881
882
            }
        }
    }

    return false;
}

bool CreateBindings::visit(Function *)
{
    return false;
}

bool CreateBindings::visit(BaseClass *b)
{
883
    if (ClassOrNamespace *base = _currentClassOrNamespace->lookupType(b->name())) {
884
885
886
887
888
889
890
891
        _currentClassOrNamespace->addUsing(base);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for:" << oo(b->name());
    }
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
892
893
894
895
bool CreateBindings::visit(UsingDeclaration *u)
{
    if (u->name()) {
        if (const QualifiedNameId *q = u->name()->asQualifiedNameId()) {
896
            if (const NameId *unqualifiedId = q->name()->asNameId()) {
Roberto Raggi's avatar
Roberto Raggi committed
897
898
899
900
901
902
903
904
905
906
                if (ClassOrNamespace *delegate = _currentClassOrNamespace->lookupType(q)) {
                    ClassOrNamespace *b = _currentClassOrNamespace->findOrCreateType(unqualifiedId);
                    b->addUsing(delegate);
                }
            }
        }
    }
    return false;
}

907
908
bool CreateBindings::visit(UsingNamespaceDirective *u)
{
909
    if (ClassOrNamespace *e = _currentClassOrNamespace->lookupType(u->name())) {
910
911
912
913
914
915
916
917
918
919
920
921
922
        _currentClassOrNamespace->addUsing(e);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for namespace:" << oo(u->name());
    }
    return false;
}

bool CreateBindings::visit(NamespaceAlias *a)
{
    if (! a->identifier()) {
        return false;

923
    } else if (ClassOrNamespace *e = _currentClassOrNamespace->lookupType(a->namespaceName())) {
924
        if (a->name()->isNameId() || a->name()->isTemplateNameId())
Roberto Raggi's avatar
Roberto Raggi committed
925
            _currentClassOrNamespace->addNestedType(a->name(), e);
926
927
928
929
930
931
932
933
934
935
936

    } else if (false) {
        Overview oo;
        qDebug() << "no entity for namespace:" << oo(a->namespaceName());
    }

    return false;
}

bool CreateBindings::visit(ObjCClass *klass)
{
937
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(klass);
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952

    process(klass->baseClass());

    for (unsigned i = 0; i < klass->protocolCount(); ++i)
        process(klass->protocolAt(i));

    for (unsigned i = 0; i < klass->memberCount(); ++i)
        process(klass->memberAt(i));

    _currentClassOrNamespace = previous;
    return false;
}

bool CreateBindings::visit(ObjCBaseClass *b)
{
953
    if (ClassOrNamespace *base = _globalNamespace->lookupType(b->name())) {
954
955
956
957
958
959
960
961
962
963
        _currentClassOrNamespace->addUsing(base);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for:" << oo(b->name());
    }
    return false;
}

bool CreateBindings::visit(ObjCForwardClassDeclaration *klass)
{
964
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(klass);
965
966
967
968
969
970
    _currentClassOrNamespace = previous;
    return false;
}

bool CreateBindings::visit(ObjCProtocol *proto)
{
971
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(proto);
972
973
974
975
976
977
978
979
980
981
982
983
984

    for (unsigned i = 0; i < proto->protocolCount(); ++i)
        process(proto->protocolAt(i));

    for (unsigned i = 0; i < proto->memberCount(); ++i)
        process(proto->memberAt(i));

    _currentClassOrNamespace = previous;
    return false;
}

bool CreateBindings::visit(ObjCBaseProtocol *b)
{
985
    if (ClassOrNamespace *base = _globalNamespace->lookupType(b->name())) {
986
987
988
989
990
991
992
993
994
995
        _currentClassOrNamespace->addUsing(base);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for:" << oo(b->name());
    }
    return false;
}

bool CreateBindings::visit(ObjCForwardProtocolDeclaration *proto)
{
996
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(proto);
997
998
999
1000
    _currentClassOrNamespace = previous;
    return false;
}

For faster browsing, not all history is shown. View entire blame