LookupContext.cpp 25.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
33
/**************************************************************************
**
** 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"
#include "CppBindings.h"
34
#include "DeprecatedGenTemplateInstance.h"
35
36
37
38
39
40
41
42
43
44

#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
45
46
47
48
namespace {
    const bool debug = ! qgetenv("CPLUSPLUS_LOOKUPCONTEXT_DEBUG").isEmpty();
}

49
50
using namespace CPlusPlus;

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

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

58
59
60
61
62
63
64
65
66
    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());
            }
67
68
69
70
        } else if (symbol->isObjCClass() || symbol->isObjCBaseClass() || symbol->isObjCProtocol()
                || symbol->isObjCForwardClassDeclaration() || symbol->isObjCForwardProtocolDeclaration()) {
            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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/////////////////////////////////////////////////////////////////////
// LookupContext
/////////////////////////////////////////////////////////////////////
LookupContext::LookupContext()
{ }

LookupContext::LookupContext(Document::Ptr thisDocument,
                             const Snapshot &snapshot)
    : _expressionDocument(Document::create("<LookupContext>")),
      _thisDocument(thisDocument),
      _snapshot(snapshot)
{
}

LookupContext::LookupContext(Document::Ptr expressionDocument,
                             Document::Ptr thisDocument,
                             const Snapshot &snapshot)
    : _expressionDocument(expressionDocument),
      _thisDocument(thisDocument),
      _snapshot(snapshot)
{
}

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

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

129
130
131
132
133
134
135
QList<const Name *> LookupContext::fullyQualifiedName(Symbol *symbol)
{
    QList<const Name *> names;
    fullyQualifiedName_helper(symbol, &names);
    return names;
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
QSharedPointer<CreateBindings> LookupContext::bindings() const
{
    if (! _bindings)
        _bindings = QSharedPointer<CreateBindings>(new CreateBindings(_thisDocument, _snapshot));

    return _bindings;
}

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

Control *LookupContext::control() const
150
{ return bindings()->control(); }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

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

169
ClassOrNamespace *LookupContext::lookupType(const Name *name, Scope *scope) const
170
{
Roberto Raggi's avatar
Roberto Raggi committed
171
    if (ClassOrNamespace *b = bindings()->lookupType(scope->owner()))
172
        return b->lookupType(name);
173
174
175
176

    return 0;
}

177
ClassOrNamespace *LookupContext::lookupType(Symbol *symbol) const
178
{
Roberto Raggi's avatar
Roberto Raggi committed
179
    return bindings()->lookupType(symbol);
180
181
182
183
184
185
186
187
188
189
190
191
192
}

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

    if (! name)
        return candidates;

    const Identifier *id = name->identifier();

    for (; scope; scope = scope->enclosingScope()) {
        if (id && scope->isBlockScope()) {
Roberto Raggi's avatar
Roberto Raggi committed
193
            bindings()->lookupInScope(name, scope, &candidates, /*templateId = */ 0);
194
195
196
197
198
199
200
201

            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()) {
202
                    if (Namespace *enclosingNamespace = u->enclosingNamespaceScope()->owner()->asNamespace()) {
Roberto Raggi's avatar
Roberto Raggi committed
203
                        if (ClassOrNamespace *b = bindings()->lookupType(enclosingNamespace)) {
204
                            if (ClassOrNamespace *uu = b->lookupType(u->name())) {
205
206
207
208
209
210
                                candidates = uu->lookup(name);

                                if (! candidates.isEmpty())
                                    return candidates;
                            }
                        }
211
212
213
214
215
216
                    }
                }
            }

        } else if (scope->isFunctionScope()) {
            Function *fun = scope->owner()->asFunction();
Roberto Raggi's avatar
Roberto Raggi committed
217
            bindings()->lookupInScope(name, fun->arguments(), &candidates, /*templateId = */ 0);
218
219
220
221

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

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

            if (fun->name() && fun->name()->isQualifiedNameId()) {
226
227
228
229
230
231
                if (ClassOrNamespace *binding = bindings()->lookupType(fun)) {
                    candidates = binding->lookup(name);

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

234
235
            // contunue, and look at the enclosing scope.

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

240
241
242
            if (! candidates.isEmpty())
                break; // it's a formal argument.

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
        } 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)) {
                candidates = binding->lookup(name);

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

        } else if (scope->isNamespaceScope() || scope->isObjCClassScope() || scope->isObjCProtocolScope()) {
Roberto Raggi's avatar
Roberto Raggi committed
260
            if (ClassOrNamespace *binding = bindings()->lookupType(scope->owner()))
261
262
263
264
265
266
267
268
269
270
                return binding->lookup(name);

            break;
        }
    }

    return candidates;
}

ClassOrNamespace::ClassOrNamespace(CreateBindings *factory, ClassOrNamespace *parent)
271
    : _factory(factory), _parent(parent), _templateId(0)
272
273
274
{
}

275
276
277
278
279
const TemplateNameId *ClassOrNamespace::templateId() const
{
    return _templateId;
}

280
281
282
283
284
ClassOrNamespace *ClassOrNamespace::parent() const
{
    return _parent;
}

285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
{
299
300
301
    if (_templateId && ! _usings.isEmpty())
        return _usings.first()->symbols(); // ask to the base implementation

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    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;
}

320
321
322
323
324
QList<Symbol *> ClassOrNamespace::find(const Name *name)
{
    return lookup_helper(name, false);
}

325
QList<Symbol *> ClassOrNamespace::lookup(const Name *name)
326
327
328
329
330
{
    return lookup_helper(name, true);
}

QList<Symbol *> ClassOrNamespace::lookup_helper(const Name *name, bool searchInEnclosingScope)
331
332
333
{
    QList<Symbol *> result;

334
335
336
    if (name) {
        if (const QualifiedNameId *q = name->asQualifiedNameId()) {
            ClassOrNamespace *binding = this;
337

338
339
            if (q->isGlobal())
                binding = globalNamespace();
340

341
342
            if (q->nameCount() == 1)
                return binding->find(q->unqualifiedNameId());
Roberto Raggi's avatar
Roberto Raggi committed
343

344
            binding = binding->lookupType(q->nameAt(0));
345

346
            for (unsigned index = 1; binding && index < q->nameCount() - 1; ++index)
347
                binding = binding->findType(q->nameAt(index));
348

349
350
            if (binding)
                result = binding->find(q->unqualifiedNameId());
351

352
353
            return result;
        }
354

355
356
357
358
359
360
361
        QSet<ClassOrNamespace *> processed;
        ClassOrNamespace *binding = this;
        do {
            lookup_helper(name, binding, &result, &processed, /*templateId = */ 0);
            binding = binding->_parent;
        } while (searchInEnclosingScope && binding);
    }
362
363
364
365
366
367

    return result;
}

void ClassOrNamespace::lookup_helper(const Name *name, ClassOrNamespace *binding,
                                     QList<Symbol *> *result,
368
369
                                     QSet<ClassOrNamespace *> *processed,
                                     const TemplateNameId *templateId)
370
{
Roberto Raggi's avatar
Roberto Raggi committed
371
    if (binding && ! processed->contains(binding)) {
372
373
374
375
        processed->insert(binding);

        foreach (Symbol *s, binding->symbols()) {
            if (ScopedSymbol *scoped = s->asScopedSymbol())
Roberto Raggi's avatar
Roberto Raggi committed
376
                _factory->lookupInScope(name, scoped->members(), result, templateId);
377
378
379
        }

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

        foreach (ClassOrNamespace *u, binding->usings())
383
            lookup_helper(name, u, result, processed, binding->_templateId);
384
385
386
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
387
void CreateBindings::lookupInScope(const Name *name, Scope *scope,
388
389
                                   QList<Symbol *> *result,
                                   const TemplateNameId *templateId)
390
{
391
392
    Q_UNUSED(templateId);

393
394
395
396
397
398
399
400
401
    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;
402

403
404
405
406
407
            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
408
            if (! id->isEqualTo(s->identifier()))
409
                continue;
Roberto Raggi's avatar
Roberto Raggi committed
410
411
            else if (s->name()->isQualifiedNameId())
                continue; // skip qualified ids.
412

413
#if 0
414
            if (templateId && (s->isDeclaration() || s->isFunction())) {
415

416
                FullySpecifiedType ty = GenTemplateInstance::instantiate(templateId, s, _control);
417

418
                if (debug) {
Roberto Raggi's avatar
Roberto Raggi committed
419
420
421
                    Overview oo;
                    oo.setShowFunctionSignatures(true);
                    oo.setShowReturnTypes(true);
422
423
                    qDebug() << "instantiate:" << oo(s->type(), s->name()) << "using:" << oo(templateId) << oo(ty);
                }
424

425
426
427
428
429
430
431
432
433
434
435
                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
436
                }
437
            }
438
#endif
439

440
441
442
443
444
445
            result->append(s);
        }

    }
}

446
ClassOrNamespace *ClassOrNamespace::lookupType(const Name *name)
447
448
449
450
451
{
    if (! name)
        return 0;

    QSet<ClassOrNamespace *> processed;
452
    return lookupType_helper(name, &processed, /*searchInEnclosingScope =*/ true);
453
454
}

455
ClassOrNamespace *ClassOrNamespace::findType(const Name *name)
456
457
{
    QSet<ClassOrNamespace *> processed;
458
    return lookupType_helper(name, &processed, /*searchInEnclosingScope =*/ false);
459
460
}

Roberto Raggi's avatar
Roberto Raggi committed
461
ClassOrNamespace *ClassOrNamespace::lookupType_helper(const Name *name,
462
463
                                                      QSet<ClassOrNamespace *> *processed,
                                                      bool searchInEnclosingScope)
464
{
465
466
    if (const QualifiedNameId *q = name->asQualifiedNameId()) {
        ClassOrNamespace *e = this;
467

468
469
        if (q->isGlobal())
            e = globalNamespace();
470

471
        e = e->lookupType(q->nameAt(0));
472

473
474
        for (unsigned index = 1; e && index < q->nameCount(); ++index)
            e = e->findType(q->nameAt(index));
475

476
        return e;
477

478
479
    } else if (! processed->contains(this)) {
        processed->insert(this);
480

481
482
        if (name->isNameId() || name->isTemplateNameId()) {
            flush();
483

Roberto Raggi's avatar
Roberto Raggi committed
484
            if (ClassOrNamespace *e = nestedType(name))
485
486
                return e;

487
488
489
490
491
492
493
494
            else if (_templateId) {
                Q_ASSERT(_usings.size() == 1);
                ClassOrNamespace *delegate = _usings.first();

                if (ClassOrNamespace *r = delegate->lookupType_helper(name, processed, /*searchInEnclosingScope = */ true))
                    return r;
            }

495
            foreach (ClassOrNamespace *u, usings()) {
496
                if (ClassOrNamespace *r = u->lookupType_helper(name, processed, /*searchInEnclosingScope =*/ false))
497
498
499
500
                    return r;
            }
        }

501
502
        if (_parent && searchInEnclosingScope)
            return _parent->lookupType_helper(name, processed, searchInEnclosingScope);
503
504
505
506
507
    }

    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
508
ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name) const
509
{
510
511
    Q_ASSERT(name != 0);
    Q_ASSERT(name->isNameId() || name->isTemplateNameId());
512

513
    const_cast<ClassOrNamespace *>(this)->flush();
514

515
    Table::const_iterator it = _classOrNamespaces.find(name);
516

517
518
    if (it == _classOrNamespaces.end())
        return 0;
519

520
521
522
523
    ClassOrNamespace *c = it->second;

    if (const TemplateNameId *templId = name->asTemplateNameId()) {
        foreach (ClassOrNamespace *i, c->_instantiations) {
524
            if (templId->isEqualTo(i->_templateId))
525
526
527
528
529
530
531
532
533
534
535
                return i;
        }

        ClassOrNamespace *i = _factory->allocClassOrNamespace(c);
        i->_templateId = templId;
        i->_usings.append(c);
        c->_instantiations.append(i);
        return i;
    }

    return c;
536
537
538
539
}

void ClassOrNamespace::flush()
{
Roberto Raggi's avatar
Roberto Raggi committed
540
541
542
    if (! _todo.isEmpty()) {
        const QList<Symbol *> todo = _todo;
        _todo.clear();
543

Roberto Raggi's avatar
Roberto Raggi committed
544
        foreach (Symbol *member, todo)
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
            _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
569
void ClassOrNamespace::addNestedType(const Name *alias, ClassOrNamespace *e)
570
{
571
    _classOrNamespaces[alias] = e;
572
573
}

574
ClassOrNamespace *ClassOrNamespace::findOrCreateType(const Name *name)
575
576
577
578
579
580
581
582
{
    if (! name)
        return this;

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

        for (unsigned i = 0; e && i < q->nameCount(); ++i)
583
            e = e->findOrCreateType(q->nameAt(i));
584
585
586

        return e;

587
    } else if (name->isNameId() || name->isTemplateNameId()) {
Roberto Raggi's avatar
Roberto Raggi committed
588
        ClassOrNamespace *e = nestedType(name);
589
590
591

        if (! e) {
            e = _factory->allocClassOrNamespace(this);
592
            _classOrNamespaces[name] = e;
593
594
595
596
597
598
599
600
601
602
603
        }

        return e;
    }

    return 0;
}

CreateBindings::CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot)
    : _snapshot(snapshot)
{
604
    _control = new Control();
605
606
607
608
609
610
611
612
613
    _globalNamespace = allocClassOrNamespace(/*parent = */ 0);
    _currentClassOrNamespace = _globalNamespace;

    process(thisDocument);
}

CreateBindings::~CreateBindings()
{
    qDeleteAll(_entities);
614
    delete _control;
615
616
}

Roberto Raggi's avatar
Roberto Raggi committed
617
ClassOrNamespace *CreateBindings::switchCurrentClassOrNamespace(ClassOrNamespace *classOrNamespace)
618
619
620
621
622
623
624
625
626
627
628
{
    ClassOrNamespace *previous = _currentClassOrNamespace;
    _currentClassOrNamespace = classOrNamespace;
    return previous;
}

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

Roberto Raggi's avatar
Roberto Raggi committed
629
ClassOrNamespace *CreateBindings::lookupType(Symbol *symbol)
630
{
631
    const QList<const Name *> names = LookupContext::fullyQualifiedName(symbol);
632

633
634
    if (names.isEmpty())
        return _globalNamespace;
635

636
    ClassOrNamespace *b = _globalNamespace->lookupType(names.at(0));
637
638

    for (int i = 1; b && i < names.size(); ++i)
639
        b = b->findType(names.at(i));
640
641

    return b;
642
643
644
645
}

void CreateBindings::process(Symbol *s, ClassOrNamespace *classOrNamespace)
{
Roberto Raggi's avatar
Roberto Raggi committed
646
    ClassOrNamespace *previous = switchCurrentClassOrNamespace(classOrNamespace);
647
    accept(s);
Roberto Raggi's avatar
Roberto Raggi committed
648
    (void) switchCurrentClassOrNamespace(previous);
649
650
651
652
653
654
655
}

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

656
657
658
659
660
Control *CreateBindings::control() const
{
    return _control;
}

661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
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);
        }
    }
}

687
ClassOrNamespace *CreateBindings::enterClassOrNamespaceBinding(Symbol *symbol)
688
{
689
    ClassOrNamespace *entity = _currentClassOrNamespace->findOrCreateType(symbol->name());
690
691
    entity->addSymbol(symbol);

Roberto Raggi's avatar
Roberto Raggi committed
692
    return switchCurrentClassOrNamespace(entity);
693
694
}

695
ClassOrNamespace *CreateBindings::enterGlobalClassOrNamespace(Symbol *symbol)
696
{
697
    ClassOrNamespace *entity = _globalNamespace->findOrCreateType(symbol->name());
698
699
    entity->addSymbol(symbol);

Roberto Raggi's avatar
Roberto Raggi committed
700
    return switchCurrentClassOrNamespace(entity);
701
702
703
704
}

bool CreateBindings::visit(Namespace *ns)
{
705
    ClassOrNamespace *previous = enterClassOrNamespaceBinding(ns);
706
707
708
709
710
711
712
713
714
715

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

    _currentClassOrNamespace = previous;
    return false;
}

bool CreateBindings::visit(Class *klass)
{
716
717
718
719
    ClassOrNamespace *previous = _currentClassOrNamespace;
    ClassOrNamespace *binding = 0;

    if (klass->name() && klass->name()->isQualifiedNameId())
720
        binding = _currentClassOrNamespace->lookupType(klass->name());
721
722

    if (! binding)
723
        binding = _currentClassOrNamespace->findOrCreateType(klass->name());
724
725
726

    _currentClassOrNamespace = binding;
    _currentClassOrNamespace->addSymbol(klass);
727
728
729
730
731
732
733
734
735
736
737
738
739

    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)
{
740
    if (! klass->isFriend()) {
741
        ClassOrNamespace *previous = enterClassOrNamespaceBinding(klass);
742
743
744
        _currentClassOrNamespace = previous;
    }

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
    return false;
}

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

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

        if (typedefId && ! (ty.isConst() || ty.isVolatile())) {
            if (const NamedType *namedTy = ty->asNamedType()) {
762
                if (ClassOrNamespace *e = _currentClassOrNamespace->lookupType(namedTy->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
763
                    _currentClassOrNamespace->addNestedType(decl->name(), e);
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
                } else if (false) {
                    Overview oo;
                    qDebug() << "found entity not found for" << oo(namedTy->name());
                }
            }
        }
    }

    return false;
}

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

bool CreateBindings::visit(BaseClass *b)
{
782
    if (ClassOrNamespace *base = _currentClassOrNamespace->lookupType(b->name())) {
783
784
785
786
787
788
789
790
791
792
        _currentClassOrNamespace->addUsing(base);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for:" << oo(b->name());
    }
    return false;
}

bool CreateBindings::visit(UsingNamespaceDirective *u)
{
793
    if (ClassOrNamespace *e = _currentClassOrNamespace->lookupType(u->name())) {
794
795
796
797
798
799
800
801
802
803
804
805
806
        _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;

807
    } else if (ClassOrNamespace *e = _currentClassOrNamespace->lookupType(a->namespaceName())) {
808
        if (a->name()->isNameId() || a->name()->isTemplateNameId())
Roberto Raggi's avatar
Roberto Raggi committed
809
            _currentClassOrNamespace->addNestedType(a->name(), e);
810
811
812
813
814
815
816
817
818
819
820

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

    return false;
}

bool CreateBindings::visit(ObjCClass *klass)
{
821
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(klass);
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836

    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)
{
837
    if (ClassOrNamespace *base = _globalNamespace->lookupType(b->name())) {
838
839
840
841
842
843
844
845
846
847
        _currentClassOrNamespace->addUsing(base);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for:" << oo(b->name());
    }
    return false;
}

bool CreateBindings::visit(ObjCForwardClassDeclaration *klass)
{
848
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(klass);
849
850
851
852
853
854
    _currentClassOrNamespace = previous;
    return false;
}

bool CreateBindings::visit(ObjCProtocol *proto)
{
855
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(proto);
856
857
858
859
860
861
862
863
864
865
866
867
868

    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)
{
869
    if (ClassOrNamespace *base = _globalNamespace->lookupType(b->name())) {
870
871
872
873
874
875
876
877
878
879
        _currentClassOrNamespace->addUsing(base);
    } else if (false) {
        Overview oo;
        qDebug() << "no entity for:" << oo(b->name());
    }
    return false;
}

bool CreateBindings::visit(ObjCForwardProtocolDeclaration *proto)
{
880
    ClassOrNamespace *previous = enterGlobalClassOrNamespace(proto);
881
882
883
884
885
886
887
888
    _currentClassOrNamespace = previous;
    return false;
}

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