LookupContext.cpp 27.3 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
42
43

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

#include <QtDebug>

Roberto Raggi's avatar
Roberto Raggi committed
44
45
46
47
namespace {
    const bool debug = ! qgetenv("CPLUSPLUS_LOOKUPCONTEXT_DEBUG").isEmpty();
}

48
49
using namespace CPlusPlus;

50
static void fullyQualifiedName_helper(Symbol *symbol, QList<const Name *> *names)
51
52
53
54
{
    if (! symbol)
        return;

55
    fullyQualifiedName_helper(symbol->enclosingSymbol(), names);
56

57
58
59
60
61
62
63
64
65
    if (symbol->name()) {
        if (symbol->isClass() || symbol->isNamespace()) {
            if (const QualifiedNameId *q = symbol->name()->asQualifiedNameId()) {
                for (unsigned i = 0; i < q->nameCount(); ++i)
                    names->append(q->nameAt(i));

            } else if (symbol->name()->isNameId() || symbol->name()->isTemplateNameId()) {
                names->append(symbol->name());
            }
66
        } else if (symbol->isObjCClass() || symbol->isObjCBaseClass() || symbol->isObjCProtocol()
67
68
                || symbol->isObjCForwardClassDeclaration() || symbol->isObjCForwardProtocolDeclaration()
                || symbol->isForwardClassDeclaration()) {
69
70
            if (symbol->name())
                names->append(symbol->name());
71
72
73
74
75
        } else if (symbol->isFunction()) {
            if (const QualifiedNameId *q = symbol->name()->asQualifiedNameId()) {
                for (unsigned i = 0; i < q->nameCount() - 1; ++i)
                    names->append(q->nameAt(i));
            }
76
77
78
79
        }
    }
}

80
81
82
83
bool ClassOrNamespace::CompareName::operator()(const Name *name, const Name *other) const
{
    Q_ASSERT(name != 0);
    Q_ASSERT(other != 0);
84

85
86
    const Identifier *id = name->identifier();
    const Identifier *otherId = other->identifier();
87
    return strcmp(id->chars(), otherId->chars()) < 0;
88
89
}

90
91
92
93
/////////////////////////////////////////////////////////////////////
// LookupContext
/////////////////////////////////////////////////////////////////////
LookupContext::LookupContext()
94
    : _control(new Control())
95
96
97
98
99
100
{ }

LookupContext::LookupContext(Document::Ptr thisDocument,
                             const Snapshot &snapshot)
    : _expressionDocument(Document::create("<LookupContext>")),
      _thisDocument(thisDocument),
101
102
      _snapshot(snapshot),
      _control(new Control())
103
104
105
106
107
108
109
110
{
}

LookupContext::LookupContext(Document::Ptr expressionDocument,
                             Document::Ptr thisDocument,
                             const Snapshot &snapshot)
    : _expressionDocument(expressionDocument),
      _thisDocument(thisDocument),
111
112
      _snapshot(snapshot),
      _control(new Control())
113
114
115
116
{
}

LookupContext::LookupContext(const LookupContext &other)
117
    : _expressionDocument(other._expressionDocument),
118
119
      _thisDocument(other._thisDocument),
      _snapshot(other._snapshot),
120
121
      _bindings(other._bindings),
      _control(other._control)
122
123
124
125
126
127
128
129
{ }

LookupContext &LookupContext::operator = (const LookupContext &other)
{
    _expressionDocument = other._expressionDocument;
    _thisDocument = other._thisDocument;
    _snapshot = other._snapshot;
    _bindings = other._bindings;
130
    _control = other._control;
131
132
133
    return *this;
}

134
135
136
137
138
139
140
QList<const Name *> LookupContext::fullyQualifiedName(Symbol *symbol)
{
    QList<const Name *> names;
    fullyQualifiedName_helper(symbol, &names);
    return names;
}

141
142
143
QSharedPointer<CreateBindings> LookupContext::bindings() const
{
    if (! _bindings)
144
        _bindings = QSharedPointer<CreateBindings>(new CreateBindings(_thisDocument, _snapshot, control()));
145
146
147
148
149
150
151
152
153

    return _bindings;
}

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

154
155
156
157
QSharedPointer<Control> LookupContext::control() const
{
    return _control;
}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

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();
}

176
ClassOrNamespace *LookupContext::lookupType(const Name *name, Scope *scope) const
177
{
Roberto Raggi's avatar
Roberto Raggi committed
178
    if (ClassOrNamespace *b = bindings()->lookupType(scope->owner()))
179
        return b->lookupType(name);
180
181
182
183

    return 0;
}

184
ClassOrNamespace *LookupContext::lookupType(Symbol *symbol) const
185
{
Roberto Raggi's avatar
Roberto Raggi committed
186
    return bindings()->lookupType(symbol);
187
188
189
190
191
192
193
194
195
196
}

QList<Symbol *> LookupContext::lookup(const Name *name, Scope *scope) const
{
    QList<Symbol *> candidates;

    if (! name)
        return candidates;

    for (; scope; scope = scope->enclosingScope()) {
Roberto Raggi's avatar
Roberto Raggi committed
197
        if ((name->isNameId() || name->isTemplateNameId()) && scope->isBlockScope()) {
Roberto Raggi's avatar
Roberto Raggi committed
198
            bindings()->lookupInScope(name, scope, &candidates, /*templateId = */ 0);
199
200
201
202
203
204
205
206

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

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

                if (UsingNamespaceDirective *u = member->asUsingNamespaceDirective()) {
207
                    if (Namespace *enclosingNamespace = u->enclosingNamespaceScope()->owner()->asNamespace()) {
Roberto Raggi's avatar
Roberto Raggi committed
208
                        if (ClassOrNamespace *b = bindings()->lookupType(enclosingNamespace)) {
209
                            if (ClassOrNamespace *uu = b->lookupType(u->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
210
                                candidates = uu->find(name);
211
212
213
214
215

                                if (! candidates.isEmpty())
                                    return candidates;
                            }
                        }
216
217
218
219
220
221
                    }
                }
            }

        } else if (scope->isFunctionScope()) {
            Function *fun = scope->owner()->asFunction();
Roberto Raggi's avatar
Roberto Raggi committed
222
            bindings()->lookupInScope(name, fun->arguments(), &candidates, /*templateId = */ 0);
223
224
225
226

            for (TemplateParameters *it = fun->templateParameters(); it && candidates.isEmpty(); it = it->previous())
                bindings()->lookupInScope(name, it->scope(), &candidates, /* templateId = */ 0);

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

            if (fun->name() && fun->name()->isQualifiedNameId()) {
231
                if (ClassOrNamespace *binding = bindings()->lookupType(fun)) {
Roberto Raggi's avatar
Roberto Raggi committed
232
                    candidates = binding->find(name);
233
234
235
236

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

239
240
            // contunue, and look at the enclosing scope.

241
242
        } else if (scope->isObjCMethodScope()) {
            ObjCMethod *method = scope->owner()->asObjCMethod();
Roberto Raggi's avatar
Roberto Raggi committed
243
            bindings()->lookupInScope(name, method->arguments(), &candidates, /*templateId = */ 0);
244

245
246
247
            if (! candidates.isEmpty())
                break; // it's a formal argument.

248
249
250
251
252
253
254
255
256
257
        } else if (scope->isClassScope()) {
            Class *klass = scope->owner()->asClass();

            for (TemplateParameters *it = klass->templateParameters(); it && candidates.isEmpty(); it = it->previous())
                bindings()->lookupInScope(name, it->scope(), &candidates, /* templateId = */ 0);

            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
258
                candidates = binding->find(name);
259
260
261
262
263

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

Roberto Raggi's avatar
Roberto Raggi committed
264
        } else if (scope->isNamespaceScope()) {
Roberto Raggi's avatar
Roberto Raggi committed
265
            if (ClassOrNamespace *binding = bindings()->lookupType(scope->owner()))
Roberto Raggi's avatar
Roberto Raggi committed
266
                candidates = binding->find(name);
267

Roberto Raggi's avatar
Roberto Raggi committed
268
269
270
271
272
273
274
275
276
                if (! candidates.isEmpty())
                    return candidates;

        } else if (scope->isObjCClassScope() || scope->isObjCProtocolScope()) {
            if (ClassOrNamespace *binding = bindings()->lookupType(scope->owner()))
                candidates = binding->find(name);

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

    return candidates;
}

ClassOrNamespace::ClassOrNamespace(CreateBindings *factory, ClassOrNamespace *parent)
284
    : _factory(factory), _parent(parent), _templateId(0)
285
286
287
{
}

288
289
290
291
292
const TemplateNameId *ClassOrNamespace::templateId() const
{
    return _templateId;
}

293
294
295
296
297
ClassOrNamespace *ClassOrNamespace::parent() const
{
    return _parent;
}

298
299
300
301
302
303
304
305
306
307
308
309
310
311
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
{
312
313
314
    if (_templateId && ! _usings.isEmpty())
        return _usings.first()->symbols(); // ask to the base implementation

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    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;
}

333
334
335
336
337
QList<Symbol *> ClassOrNamespace::find(const Name *name)
{
    return lookup_helper(name, false);
}

338
QList<Symbol *> ClassOrNamespace::lookup(const Name *name)
339
340
341
342
343
{
    return lookup_helper(name, true);
}

QList<Symbol *> ClassOrNamespace::lookup_helper(const Name *name, bool searchInEnclosingScope)
344
345
346
{
    QList<Symbol *> result;

347
348
349
    if (name) {
        if (const QualifiedNameId *q = name->asQualifiedNameId()) {
            ClassOrNamespace *binding = this;
350

351
352
            if (q->isGlobal())
                binding = globalNamespace();
353

354
355
            if (q->nameCount() == 1)
                return binding->find(q->unqualifiedNameId());
Roberto Raggi's avatar
Roberto Raggi committed
356

357
            binding = binding->lookupType(q->nameAt(0));
358

359
            for (unsigned index = 1; binding && index < q->nameCount() - 1; ++index)
360
                binding = binding->findType(q->nameAt(index));
361

362
363
            if (binding)
                result = binding->find(q->unqualifiedNameId());
364

365
366
            return result;
        }
367

368
369
370
371
372
373
374
        QSet<ClassOrNamespace *> processed;
        ClassOrNamespace *binding = this;
        do {
            lookup_helper(name, binding, &result, &processed, /*templateId = */ 0);
            binding = binding->_parent;
        } while (searchInEnclosingScope && binding);
    }
375
376
377
378
379
380

    return result;
}

void ClassOrNamespace::lookup_helper(const Name *name, ClassOrNamespace *binding,
                                     QList<Symbol *> *result,
381
382
                                     QSet<ClassOrNamespace *> *processed,
                                     const TemplateNameId *templateId)
383
{
Roberto Raggi's avatar
Roberto Raggi committed
384
    if (binding && ! processed->contains(binding)) {
385
386
        processed->insert(binding);

387
388
        const Identifier *nameId = name->identifier();

389
        foreach (Symbol *s, binding->symbols()) {
390
391
392
393
394
395
396
            if (ScopedSymbol *scoped = s->asScopedSymbol()) {
                if (Class *klass = scoped->asClass()) {
                    if (const Identifier *id = klass->identifier()) {
                        if (nameId && nameId->isEqualTo(id))
                            result->append(klass);
                    }
                }
Roberto Raggi's avatar
Roberto Raggi committed
397
                _factory->lookupInScope(name, scoped->members(), result, templateId);
398
            }
399
400
401
        }

        foreach (Enum *e, binding->enums())
Roberto Raggi's avatar
Roberto Raggi committed
402
            _factory->lookupInScope(name, e->members(), result, templateId);
403
404

        foreach (ClassOrNamespace *u, binding->usings())
405
            lookup_helper(name, u, result, processed, binding->_templateId);
406
407
408
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
409
void CreateBindings::lookupInScope(const Name *name, Scope *scope,
410
411
                                   QList<Symbol *> *result,
                                   const TemplateNameId *templateId)
412
{
413
414
    Q_UNUSED(templateId);

415
416
417
418
419
420
421
422
423
    if (! name) {
        return;

    } else if (const OperatorNameId *op = name->asOperatorNameId()) {
        for (Symbol *s = scope->lookat(op->kind()); s; s = s->next()) {
            if (! s->name())
                continue;
            else if (! s->name()->isEqualTo(op))
                continue;
424

425
426
427
428
429
            result->append(s);
        }

    } else if (const Identifier *id = name->identifier()) {
        for (Symbol *s = scope->lookat(id); s; s = s->next()) {
Roberto Raggi's avatar
Roberto Raggi committed
430
            if (! id->isEqualTo(s->identifier()))
431
                continue;
Roberto Raggi's avatar
Roberto Raggi committed
432
433
            else if (s->name()->isQualifiedNameId())
                continue; // skip qualified ids.
434

435
#if 0
436
            if (templateId && (s->isDeclaration() || s->isFunction())) {
437

Roberto Raggi's avatar
Roberto Raggi committed
438
                FullySpecifiedType ty = DeprecatedGenTemplateInstance::instantiate(templateId, s, _control);
439

440
                if (debug) {
Roberto Raggi's avatar
Roberto Raggi committed
441
442
443
                    Overview oo;
                    oo.setShowFunctionSignatures(true);
                    oo.setShowReturnTypes(true);
444
445
                    qDebug() << "instantiate:" << oo(s->type(), s->name()) << "using:" << oo(templateId) << oo(ty);
                }
446

447
448
449
450
451
452
453
454
455
456
457
                if (Declaration *decl = s->asDeclaration()) {
                    Declaration *d = _control->newDeclaration(0, 0);
                    d->copy(decl);
                    d->setType(ty);
                    result->append(d);
                    continue;
                } else if (Function *fun = s->asFunction()) {
                    Function *d = ty->asFunctionType();
                    d->copy(fun);
                    result->append(d);
                    continue;
Roberto Raggi's avatar
Roberto Raggi committed
458
                }
459
            }
460
#endif
461

462
463
464
465
466
467
            result->append(s);
        }

    }
}

468
ClassOrNamespace *ClassOrNamespace::lookupType(const Name *name)
469
470
471
472
473
{
    if (! name)
        return 0;

    QSet<ClassOrNamespace *> processed;
474
    return lookupType_helper(name, &processed, /*searchInEnclosingScope =*/ true);
475
476
}

477
ClassOrNamespace *ClassOrNamespace::findType(const Name *name)
478
479
{
    QSet<ClassOrNamespace *> processed;
480
    return lookupType_helper(name, &processed, /*searchInEnclosingScope =*/ false);
481
482
}

Roberto Raggi's avatar
Roberto Raggi committed
483
ClassOrNamespace *ClassOrNamespace::lookupType_helper(const Name *name,
484
485
                                                      QSet<ClassOrNamespace *> *processed,
                                                      bool searchInEnclosingScope)
486
{
487
488
    if (const QualifiedNameId *q = name->asQualifiedNameId()) {
        ClassOrNamespace *e = this;
489

490
491
        if (q->isGlobal())
            e = globalNamespace();
492

493
        e = e->lookupType(q->nameAt(0));
494

495
496
        for (unsigned index = 1; e && index < q->nameCount(); ++index)
            e = e->findType(q->nameAt(index));
497

498
        return e;
499

500
501
    } else if (! processed->contains(this)) {
        processed->insert(this);
502

503
504
        if (name->isNameId() || name->isTemplateNameId()) {
            flush();
505

506
507
508
509
510
511
512
            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
513
            if (ClassOrNamespace *e = nestedType(name))
514
515
                return e;

516
            else if (_templateId) {
517
518
519
520
521
522
523
524
525
                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();
                }
526
527
            }

528
            foreach (ClassOrNamespace *u, usings()) {
529
                if (ClassOrNamespace *r = u->lookupType_helper(name, processed, /*searchInEnclosingScope =*/ false))
530
531
532
533
                    return r;
            }
        }

534
535
        if (_parent && searchInEnclosingScope)
            return _parent->lookupType_helper(name, processed, searchInEnclosingScope);
536
537
538
539
540
    }

    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
541
ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name) const
542
{
543
544
    Q_ASSERT(name != 0);
    Q_ASSERT(name->isNameId() || name->isTemplateNameId());
545

546
    const_cast<ClassOrNamespace *>(this)->flush();
547

548
    Table::const_iterator it = _classOrNamespaces.find(name);
549

550
551
    if (it == _classOrNamespaces.end())
        return 0;
552

553
554
555
556
557
558
559
560
561
562
563
    ClassOrNamespace *c = it->second;

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

    return c;
564
565
566
567
}

void ClassOrNamespace::flush()
{
Roberto Raggi's avatar
Roberto Raggi committed
568
569
570
    if (! _todo.isEmpty()) {
        const QList<Symbol *> todo = _todo;
        _todo.clear();
571

Roberto Raggi's avatar
Roberto Raggi committed
572
        foreach (Symbol *member, todo)
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
            _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
597
void ClassOrNamespace::addNestedType(const Name *alias, ClassOrNamespace *e)
598
{
599
    _classOrNamespaces[alias] = e;
600
601
}

602
ClassOrNamespace *ClassOrNamespace::findOrCreateType(const Name *name)
603
604
605
606
607
608
609
610
{
    if (! name)
        return this;

    if (const QualifiedNameId *q = name->asQualifiedNameId()) {
        ClassOrNamespace *e = this;

        for (unsigned i = 0; e && i < q->nameCount(); ++i)
611
            e = e->findOrCreateType(q->nameAt(i));
612
613
614

        return e;

615
    } else if (name->isNameId() || name->isTemplateNameId()) {
Roberto Raggi's avatar
Roberto Raggi committed
616
        ClassOrNamespace *e = nestedType(name);
617
618
619

        if (! e) {
            e = _factory->allocClassOrNamespace(this);
620
            _classOrNamespaces[name] = e;
621
622
623
624
625
626
627
628
        }

        return e;
    }

    return 0;
}

629
630
CreateBindings::CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot, QSharedPointer<Control> control)
    : _snapshot(snapshot), _control(control)
631
632
633
634
635
636
637
638
639
640
641
642
{
    _globalNamespace = allocClassOrNamespace(/*parent = */ 0);
    _currentClassOrNamespace = _globalNamespace;

    process(thisDocument);
}

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

Roberto Raggi's avatar
Roberto Raggi committed
643
ClassOrNamespace *CreateBindings::switchCurrentClassOrNamespace(ClassOrNamespace *classOrNamespace)
644
645
646
647
648
649
650
651
652
653
654
{
    ClassOrNamespace *previous = _currentClassOrNamespace;
    _currentClassOrNamespace = classOrNamespace;
    return previous;
}

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

Roberto Raggi's avatar
Roberto Raggi committed
655
ClassOrNamespace *CreateBindings::lookupType(Symbol *symbol)
656
{
657
    const QList<const Name *> names = LookupContext::fullyQualifiedName(symbol);
658

659
660
    if (names.isEmpty())
        return _globalNamespace;
661

662
    ClassOrNamespace *b = _globalNamespace->lookupType(names.at(0));
663
664

    for (int i = 1; b && i < names.size(); ++i)
665
        b = b->findType(names.at(i));
666
667

    return b;
668
669
670
671
}

void CreateBindings::process(Symbol *s, ClassOrNamespace *classOrNamespace)
{
Roberto Raggi's avatar
Roberto Raggi committed
672
    ClassOrNamespace *previous = switchCurrentClassOrNamespace(classOrNamespace);
673
    accept(s);
Roberto Raggi's avatar
Roberto Raggi committed
674
    (void) switchCurrentClassOrNamespace(previous);
675
676
677
678
679
680
681
}

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

682
QSharedPointer<Control> CreateBindings::control() const
683
684
685
686
{
    return _control;
}

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
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);
        }
    }
}

713
ClassOrNamespace *CreateBindings::enterClassOrNamespaceBinding(Symbol *symbol)
714
{
715
    ClassOrNamespace *entity = _currentClassOrNamespace->findOrCreateType(symbol->name());
716
717
    entity->addSymbol(symbol);

Roberto Raggi's avatar
Roberto Raggi committed
718
    return switchCurrentClassOrNamespace(entity);
719
720
}

721
ClassOrNamespace *CreateBindings::enterGlobalClassOrNamespace(Symbol *symbol)
722
{
723
    ClassOrNamespace *entity = _globalNamespace->findOrCreateType(symbol->name());
724
725
    entity->addSymbol(symbol);

Roberto Raggi's avatar
Roberto Raggi committed
726
    return switchCurrentClassOrNamespace(entity);
727
728
729
730
}

bool CreateBindings::visit(Namespace *ns)
{
731
    ClassOrNamespace *previous = enterClassOrNamespaceBinding(ns);
732
733
734
735
736
737
738
739
740
741

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

    _currentClassOrNamespace = previous;
    return false;
}

bool CreateBindings::visit(Class *klass)
{
742
743
744
745
    ClassOrNamespace *previous = _currentClassOrNamespace;
    ClassOrNamespace *binding = 0;

    if (klass->name() && klass->name()->isQualifiedNameId())
746
        binding = _currentClassOrNamespace->lookupType(klass->name());
747
748

    if (! binding)
749
        binding = _currentClassOrNamespace->findOrCreateType(klass->name());
750
751
752

    _currentClassOrNamespace = binding;
    _currentClassOrNamespace->addSymbol(klass);
753
754
755
756
757
758
759
760
761
762
763
764
765

    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)
{
766
    if (! klass->isFriend()) {
767
        ClassOrNamespace *previous = enterClassOrNamespaceBinding(klass);
768
769
770
        _currentClassOrNamespace = previous;
    }

771
772
773
774
775
776
777
778
779
780
781
782
    return false;
}

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

bool CreateBindings::visit(Declaration *decl)
{
    if (decl->isTypedef()) {
783
        FullySpecifiedType ty = decl->type();
784
785
786
787
        const Identifier *typedefId = decl->identifier();

        if (typedefId && ! (ty.isConst() || ty.isVolatile())) {
            if (const NamedType *namedTy = ty->asNamedType()) {
788
                if (ClassOrNamespace *e = _currentClassOrNamespace->lookupType(namedTy->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
789
                    _currentClassOrNamespace->addNestedType(decl->name(), e);
790
791
792
793
                } else if (false) {
                    Overview oo;
                    qDebug() << "found entity not found for" << oo(namedTy->name());
                }
794
795
796
797
798
            } else if (Class *klass = ty->asClassType()) {
                if (const NameId *nameId = decl->name()->asNameId()) {
                    ClassOrNamespace *binding = _currentClassOrNamespace->findOrCreateType(nameId);
                    binding->addSymbol(klass);
                }
799
800
801
802
803
804
805
806
807
808
809
810
811
812
            }
        }
    }

    return false;
}

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

bool CreateBindings::visit(BaseClass *b)
{
813
    if (ClassOrNamespace *base = _currentClassOrNamespace->lookupType(b->name())) {
814
815
816
817
818
819
820
821
        _currentClassOrNamespace->addUsing(base);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for:" << oo(b->name());
    }
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
bool CreateBindings::visit(UsingDeclaration *u)
{
    if (u->name()) {
        if (const QualifiedNameId *q = u->name()->asQualifiedNameId()) {
            if (const NameId *unqualifiedId = q->unqualifiedNameId()->asNameId()) {
                if (ClassOrNamespace *delegate = _currentClassOrNamespace->lookupType(q)) {
                    ClassOrNamespace *b = _currentClassOrNamespace->findOrCreateType(unqualifiedId);
                    b->addUsing(delegate);
                }
            }
        }
    }
    return false;
}

837
838
bool CreateBindings::visit(UsingNamespaceDirective *u)
{
839
    if (ClassOrNamespace *e = _currentClassOrNamespace->lookupType(u->name())) {
840
841
842
843
844
845
846
847
848
849
850
851
852
        _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;

853
    } else if (ClassOrNamespace *e = _currentClassOrNamespace->lookupType(a->namespaceName())) {
854
        if (a->name()->isNameId() || a->name()->isTemplateNameId())
Roberto Raggi's avatar
Roberto Raggi committed
855
            _currentClassOrNamespace->addNestedType(a->name(), e);
856
857
858
859
860
861
862
863
864
865
866

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

    return false;
}

bool CreateBindings::visit(ObjCClass *klass)
{
867
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(klass);
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882

    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)
{
883
    if (ClassOrNamespace *base = _globalNamespace->lookupType(b->name())) {
884
885
886
887
888
889
890
891
892
893
        _currentClassOrNamespace->addUsing(base);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for:" << oo(b->name());
    }
    return false;
}

bool CreateBindings::visit(ObjCForwardClassDeclaration *klass)
{
894
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(klass);
895
896
897
898
899
900
    _currentClassOrNamespace = previous;
    return false;
}

bool CreateBindings::visit(ObjCProtocol *proto)
{
901
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(proto);
902
903
904
905
906
907
908
909
910
911
912
913
914

    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)
{
915
    if (ClassOrNamespace *base = _globalNamespace->lookupType(b->name())) {
916
917
918
919
920
921
922
923
924
925
        _currentClassOrNamespace->addUsing(base);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for:" << oo(b->name());
    }
    return false;
}

bool CreateBindings::visit(ObjCForwardProtocolDeclaration *proto)
{
926
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(proto);
927
928
929
930
931
932
933
934
    _currentClassOrNamespace = previous;
    return false;
}

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