CppBindings.cpp 22.5 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
**
** 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
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
**
**************************************************************************/

#include "CppBindings.h"
#include "CppDocument.h"
#include "Overview.h"

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

#include <QtDebug>

using namespace CPlusPlus;


////////////////////////////////////////////////////////////////////////////////
// Location
////////////////////////////////////////////////////////////////////////////////
Location::Location()
    : _fileId(0),
      _sourceLocation(0)
{ }

Location::Location(Symbol *symbol)
    : _fileId(symbol->fileId()),
      _sourceLocation(symbol->sourceLocation())
{ }

Roberto Raggi's avatar
Roberto Raggi committed
60
Location::Location(const StringLiteral *fileId, unsigned sourceLocation)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    : _fileId(fileId), _sourceLocation(sourceLocation)
{ }

////////////////////////////////////////////////////////////////////////////////
// NamespaceBinding
////////////////////////////////////////////////////////////////////////////////

NamespaceBinding::NamespaceBinding(NamespaceBinding *parent)
    : parent(parent),
      anonymousNamespaceBinding(0)
{
    if (parent)
        parent->children.append(this);
}

NamespaceBinding::~NamespaceBinding()
{
    qDeleteAll(children);
    qDeleteAll(classBindings);
}

Roberto Raggi's avatar
Roberto Raggi committed
82
const NameId *NamespaceBinding::name() const
83 84
{
    if (symbols.size()) {
Roberto Raggi's avatar
Roberto Raggi committed
85 86
        if (const Name *name = symbols.first()->name()) {
            const NameId *nameId = name->asNameId();
87 88 89 90 91 92 93 94 95
            Q_ASSERT(nameId != 0);

            return nameId;
        }
    }

    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
96
const Identifier *NamespaceBinding::identifier() const
97
{
Roberto Raggi's avatar
Roberto Raggi committed
98
    if (const NameId *nameId = name())
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        return nameId->identifier();

    return 0;
}

NamespaceBinding *NamespaceBinding::globalNamespaceBinding()
{
    NamespaceBinding *it = this;

    for (; it; it = it->parent) {
        if (! it->parent)
            break;
    }

    return it;
}

Roberto Raggi's avatar
Roberto Raggi committed
116
Binding *NamespaceBinding::findClassOrNamespaceBinding(const Identifier *id, QSet<Binding *> *processed)
Roberto Raggi's avatar
Roberto Raggi committed
117
{
118 119 120 121 122
    if (processed->contains(this))
        return 0;

    processed->insert(this);

Roberto Raggi's avatar
Roberto Raggi committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136
    if (id->isEqualTo(identifier()))
        return const_cast<NamespaceBinding *>(this);

    foreach (NamespaceBinding *nestedNamespaceBinding, children) {
        if (id->isEqualTo(nestedNamespaceBinding->identifier()))
            return nestedNamespaceBinding;
    }

    foreach (ClassBinding *classBinding, classBindings) {
        if (id->isEqualTo(classBinding->identifier()))
            return classBinding;
    }

    foreach (NamespaceBinding *u, usings) {
137
        if (Binding *b = u->findClassOrNamespaceBinding(id, processed))
Roberto Raggi's avatar
Roberto Raggi committed
138 139 140 141
            return b;
    }

    if (parent)
142
        return parent->findClassOrNamespaceBinding(id, processed);
Roberto Raggi's avatar
Roberto Raggi committed
143 144 145 146

    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
147
ClassBinding *NamespaceBinding::findClassBinding(const Name *name, QSet<Binding *> *processed)
Roberto Raggi's avatar
Roberto Raggi committed
148 149 150 151
{
    if (! name)
        return 0;

152 153 154
    if (processed->contains(this))
        return 0;

Roberto Raggi's avatar
Roberto Raggi committed
155 156 157 158
    if (const QualifiedNameId *q = name->asQualifiedNameId()) {
        Binding *current = this;

        for (unsigned i = 0; i < q->nameCount(); ++i) {
Roberto Raggi's avatar
Roberto Raggi committed
159
            const Identifier *nameId = q->nameAt(i)->identifier();
Roberto Raggi's avatar
Roberto Raggi committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173
            if (! nameId)
                return 0;

            QSet<Binding *> visited;
            Binding *binding = current->findClassOrNamespaceBinding(nameId, &visited); // ### TODO: check recursion.
            if (! binding)
                return 0;

            current = binding;
        }

        return current->asClassBinding();
    }

174 175
    processed->insert(this);

Roberto Raggi's avatar
Roberto Raggi committed
176
    const Identifier *id = name->identifier();
Roberto Raggi's avatar
Roberto Raggi committed
177 178 179 180 181 182 183

    foreach (ClassBinding *classBinding, classBindings) {
        if (id->isEqualTo(classBinding->identifier()))
            return classBinding;
    }

    if (parent)
184 185 186 187 188 189
        return parent->findClassBinding(name, processed);

    foreach (NamespaceBinding *u, usings) {
        if (ClassBinding *classBinding = u->findClassBinding(name, processed))
            return classBinding;
    }
Roberto Raggi's avatar
Roberto Raggi committed
190 191 192 193

    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
194
NamespaceBinding *NamespaceBinding::findNamespaceBinding(const Name *name)
195 196 197 198
{
    if (! name)
        return anonymousNamespaceBinding;

Roberto Raggi's avatar
Roberto Raggi committed
199
    else if (const NameId *nameId = name->asNameId())
Roberto Raggi's avatar
Roberto Raggi committed
200 201 202 203 204 205
        return findNamespaceBindingForNameId(nameId, /*lookAtParent = */ true);

    else if (const QualifiedNameId *q = name->asQualifiedNameId()) {
        NamespaceBinding *current = this;

        for (unsigned i = 0; i < q->nameCount(); ++i) {
Roberto Raggi's avatar
Roberto Raggi committed
206
            const NameId *namespaceName = q->nameAt(i)->asNameId();
Roberto Raggi's avatar
Roberto Raggi committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
            if (! namespaceName)
                return 0;

            bool lookAtParent = false;
            if (i == 0)
                lookAtParent = true;

            NamespaceBinding *binding = current->findNamespaceBindingForNameId(namespaceName, lookAtParent);
            if (! binding)
                return 0;

            current = binding;
        }

        return current;
    }
223 224 225 226 227

    // invalid binding
    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
228
NamespaceBinding *NamespaceBinding::findNamespaceBindingForNameId(const NameId *name,
Roberto Raggi's avatar
Roberto Raggi committed
229
                                                                  bool lookAtParentNamespace)
230
{
Roberto Raggi's avatar
Roberto Raggi committed
231 232 233 234
    QSet<NamespaceBinding *> processed;
    return findNamespaceBindingForNameId_helper(name, lookAtParentNamespace, &processed);
}

Roberto Raggi's avatar
Roberto Raggi committed
235
NamespaceBinding *NamespaceBinding::findNamespaceBindingForNameId_helper(const NameId *name,
Roberto Raggi's avatar
Roberto Raggi committed
236 237 238 239 240 241 242 243
                                                                         bool lookAtParentNamespace,
                                                                         QSet<NamespaceBinding *> *processed)
{
    if (processed->contains(this))
        return 0;

    processed->insert(this);

244
    foreach (NamespaceBinding *binding, children) {
Roberto Raggi's avatar
Roberto Raggi committed
245
        const Name *bindingName = binding->name();
246 247 248 249

        if (! bindingName)
            continue;

Roberto Raggi's avatar
Roberto Raggi committed
250
        if (const NameId *bindingNameId = bindingName->asNameId()) {
251 252 253 254 255
            if (name->isEqualTo(bindingNameId))
                return binding;
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
256 257 258 259 260 261 262 263 264
    foreach (NamespaceBinding *u, usings) {
        if (NamespaceBinding *b = u->findNamespaceBindingForNameId_helper(name, lookAtParentNamespace, processed)) {
            return b;
        }
    }

    if (lookAtParentNamespace && parent)
        return parent->findNamespaceBindingForNameId_helper(name, lookAtParentNamespace, processed);

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    return 0;
}

NamespaceBinding *NamespaceBinding::findOrCreateNamespaceBinding(Namespace *symbol)
{
    if (NamespaceBinding *binding = findNamespaceBinding(symbol->name())) {
        int index = 0;

        for (; index < binding->symbols.size(); ++index) {
            Namespace *ns = binding->symbols.at(index);

            if (ns == symbol)
                break;
        }

        if (index == binding->symbols.size())
            binding->symbols.append(symbol);

        return binding;
    }

    NamespaceBinding *binding = new NamespaceBinding(this);
    binding->symbols.append(symbol);

    if (! symbol->name()) {
        Q_ASSERT(! anonymousNamespaceBinding);

        anonymousNamespaceBinding = binding;
    }

    return binding;
}

static void closure(const Location &loc,
Roberto Raggi's avatar
Roberto Raggi committed
299
                    NamespaceBinding *binding, const Name *name,
300 301 302 303 304 305 306 307 308
                    QList<NamespaceBinding *> *bindings)
{
    if (bindings->contains(binding))
        return;

    bindings->append(binding);

    Q_ASSERT(name->isNameId());

Roberto Raggi's avatar
Roberto Raggi committed
309
    const Identifier *id = name->asNameId()->identifier();
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    bool ignoreUsingDirectives = false;

    foreach (Namespace *symbol, binding->symbols) {
        Scope *scope = symbol->members();

        for (Symbol *symbol = scope->lookat(id); symbol; symbol = symbol->next()) {
            if (symbol->name() != name || ! symbol->isNamespace())
                continue;

            const Location l(symbol);

            if (l.fileId() == loc.fileId() && l.sourceLocation() < loc.sourceLocation()) {
                ignoreUsingDirectives = true;
                break;
            }
        }
    }

    if (ignoreUsingDirectives)
        return;

    foreach (NamespaceBinding *u, binding->usings)
        closure(loc, u, name, bindings);
}


NamespaceBinding *NamespaceBinding::resolveNamespace(const Location &loc,
Roberto Raggi's avatar
Roberto Raggi committed
337
                                                     const Name *name,
338 339 340 341 342
                                                     bool lookAtParent)
{
    if (! name)
        return 0;

Roberto Raggi's avatar
Roberto Raggi committed
343
    else if (const NameId *nameId = name->asNameId()) {
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        QList<NamespaceBinding *> bindings;
        closure(loc, this, nameId, &bindings);

        QList<NamespaceBinding *> results;

        foreach (NamespaceBinding *binding, bindings) {
            if (NamespaceBinding *b = binding->findNamespaceBinding(nameId))
                results.append(b);
        }

        if (results.size() == 1)
            return results.at(0);

        else if (results.size() > 1) {
            // ### FIXME: return 0;
            return results.at(0);
        }

        else if (parent && lookAtParent)
            return parent->resolveNamespace(loc, name);

Roberto Raggi's avatar
Roberto Raggi committed
365
    } else if (const QualifiedNameId *q = name->asQualifiedNameId()) {
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
        if (q->nameCount() == 1) {
            Q_ASSERT(q->isGlobal());

            return globalNamespaceBinding()->resolveNamespace(loc, q->nameAt(0));
        }

        NamespaceBinding *current = this;
        if (q->isGlobal())
            current = globalNamespaceBinding();

        current = current->resolveNamespace(loc, q->nameAt(0));
        for (unsigned i = 1; current && i < q->nameCount(); ++i)
            current = current->resolveNamespace(loc, q->nameAt(i), false);

        return current;
    }

    return 0;
}

// ### rewrite me
QByteArray NamespaceBinding::qualifiedId() const
{
    if (! parent)
        return "<root>";

    QByteArray s;

    s.append(parent->qualifiedId());
    s.append("::");

Roberto Raggi's avatar
Roberto Raggi committed
397
    if (const Identifier *id = identifier())
398 399 400 401 402 403 404 405 406 407 408 409 410 411
        s.append(id->chars(), id->size());

    else
        s.append("<anonymous>");

    return s;
}

// ### rewrite me
QByteArray ClassBinding::qualifiedId() const
{
    QByteArray s = parent->qualifiedId();
    s += "::";

Roberto Raggi's avatar
Roberto Raggi committed
412
    if (const Identifier *id = identifier())
413 414 415 416 417 418 419 420
        s.append(id->chars(), id->size());

    else
        s.append("<anonymous>");

    return s;
}

Roberto Raggi's avatar
Roberto Raggi committed
421
Binding *ClassBinding::findClassOrNamespaceBinding(const Identifier *id, QSet<Binding *> *processed)
Roberto Raggi's avatar
Roberto Raggi committed
422 423 424 425
{
    if (id->isEqualTo(identifier()))
        return this;

426 427 428 429 430
    if (processed->contains(this))
        return 0;

    processed->insert(this);

Roberto Raggi's avatar
Roberto Raggi committed
431 432 433 434 435 436 437 438
    foreach (ClassBinding *nestedClassBinding, children) {
        if (id->isEqualTo(nestedClassBinding->identifier()))
            return nestedClassBinding;
    }

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

440
        else if (Binding *b = baseClassBinding->findClassOrNamespaceBinding(id, processed))
Roberto Raggi's avatar
Roberto Raggi committed
441 442 443 444
            return b;
    }

    if (parent)
445
        return parent->findClassOrNamespaceBinding(id, processed);
Roberto Raggi's avatar
Roberto Raggi committed
446 447 448 449

    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
450
ClassBinding *ClassBinding::findClassBinding(const Name *name, QSet<Binding *> *processed)
Roberto Raggi's avatar
Roberto Raggi committed
451 452 453 454
{
    if (! name)
        return 0;

455 456 457 458 459
    if (processed->contains(this))
        return 0;

    processed->insert(this);

Roberto Raggi's avatar
Roberto Raggi committed
460 461 462 463
    if (const QualifiedNameId *q = name->asQualifiedNameId()) {
        Binding *currentBinding = this;

        for (unsigned i = 0; i < q->nameCount() - 1; ++i) {
Roberto Raggi's avatar
Roberto Raggi committed
464
            const Identifier *id = q->nameAt(i)->identifier();
Roberto Raggi's avatar
Roberto Raggi committed
465 466 467
            if (! id)
                return 0;

468
            Binding *classOrNamespaceBinding = currentBinding->findClassOrNamespaceBinding(id, processed);
Roberto Raggi's avatar
Roberto Raggi committed
469 470 471 472 473 474 475 476

            if (! classOrNamespaceBinding)
                return 0;

            currentBinding = classOrNamespaceBinding;
        }

        if (currentBinding)
477
            return currentBinding->findClassBinding(q->unqualifiedNameId(), processed);
Roberto Raggi's avatar
Roberto Raggi committed
478 479 480 481

        return 0;
    }

Roberto Raggi's avatar
Roberto Raggi committed
482
    if (const Identifier *id = name->identifier()) {
Roberto Raggi's avatar
Roberto Raggi committed
483 484 485 486
        if (id->isEqualTo(identifier()))
            return this;

        foreach (ClassBinding *nestedClassBinding, children) {
Roberto Raggi's avatar
Roberto Raggi committed
487
            if (const Identifier *nestedClassId = nestedClassBinding->identifier()) {
Roberto Raggi's avatar
Roberto Raggi committed
488 489 490 491 492 493
                if (nestedClassId->isEqualTo(id))
                    return nestedClassBinding;
            }
        }

        if (parent)
494
            return parent->findClassBinding(name, processed);
Roberto Raggi's avatar
Roberto Raggi committed
495 496 497 498 499
    }

    return 0;
}

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
static int depth;

void NamespaceBinding::dump()
{
    qDebug() << QByteArray(depth, ' ').constData() << "namespace" << qualifiedId().constData()
              << " # " << symbols.size();

    ++depth;

    foreach (ClassBinding *classBinding, classBindings) {
        classBinding->dump();
    }

    foreach (NamespaceBinding *child, children) {
        child->dump();
    }

    --depth;
}

void ClassBinding::dump()
{
    qDebug() << QByteArray(depth, ' ').constData() << "class" << qualifiedId().constData()
              << " # " << symbols.size();

    ++depth;

    foreach (ClassBinding *classBinding, children) {
        classBinding->dump();
    }

    --depth;
}

////////////////////////////////////////////////////////////////////////////////
// ClassBinding
////////////////////////////////////////////////////////////////////////////////
ClassBinding::ClassBinding(NamespaceBinding *parent)
    : parent(parent)
{
    parent->classBindings.append(this);
}

ClassBinding::ClassBinding(ClassBinding *parentClass)
Roberto Raggi's avatar
Roberto Raggi committed
544
    : parent(parentClass)
545 546 547 548 549 550 551
{
    parentClass->children.append(this);
}

ClassBinding::~ClassBinding()
{ qDeleteAll(children); }

Roberto Raggi's avatar
Roberto Raggi committed
552
const Name *ClassBinding::name() const
553
{
Roberto Raggi's avatar
Roberto Raggi committed
554 555
    if (symbols.isEmpty())
        return 0;
556

Roberto Raggi's avatar
Roberto Raggi committed
557
    return symbols.first()->name();
558 559
}

Roberto Raggi's avatar
Roberto Raggi committed
560
const Identifier *ClassBinding::identifier() const
561
{
Roberto Raggi's avatar
Roberto Raggi committed
562
    if (const Name *n = name())
Roberto Raggi's avatar
Roberto Raggi committed
563
        return n->identifier();
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

    return 0;
}

namespace {

////////////////////////////////////////////////////////////////////////////////
// Binder
////////////////////////////////////////////////////////////////////////////////

class Binder: protected SymbolVisitor
{
public:
    Binder(NamespaceBinding *globals);
    virtual ~Binder();

    NamespaceBinding *operator()(Document::Ptr doc, const Snapshot &snapshot)
    {
        namespaceBinding = _globals;
        const Snapshot previousSnapshot = _snapshot;

        _snapshot = snapshot;
        (void) bind(doc);
        _snapshot = previousSnapshot;

        return _globals;
    }

    Snapshot _snapshot;

protected:
    NamespaceBinding *bind(Document::Ptr doc)
    {
        QSet<QString> processed;
        return bind(doc, &processed);
    }

    NamespaceBinding *bind(Document::Ptr doc, QSet<QString> *processed)
    {
        if (processed->contains(doc->fileName()))
            return 0;

        processed->insert(doc->fileName());

        foreach (const Document::Include &i, doc->includes()) {
609
            if (Document::Ptr includedDoc = _snapshot.document(i.fileName())) {
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
                /*NamepaceBinding *binding = */ bind(includedDoc, processed);
            }
        }

        Namespace *ns = doc->globalNamespace();
        _globals->symbols.append(ns);

        for (unsigned i = 0; i < ns->memberCount(); ++i) {
            (void) bind(ns->memberAt(i), _globals);
        }

        return _globals;
    }

    NamespaceBinding *bind(Symbol *symbol, NamespaceBinding *binding);
    NamespaceBinding *findOrCreateNamespaceBinding(Namespace *symbol);
Roberto Raggi's avatar
Roberto Raggi committed
626
    NamespaceBinding *resolveNamespace(const Location &loc, const Name *name);
627 628 629 630

    NamespaceBinding *switchNamespaceBinding(NamespaceBinding *binding);

    ClassBinding *findOrCreateClassBinding(Class *classSymbol);
Roberto Raggi's avatar
Roberto Raggi committed
631
    ClassBinding *findClassBinding(const Name *name);
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667

    ClassBinding *switchClassBinding(ClassBinding *binding);

    using SymbolVisitor::visit;

    virtual bool visit(Namespace *);
    virtual bool visit(UsingNamespaceDirective *);
    virtual bool visit(Class *);
    virtual bool visit(Function *);
    virtual bool visit(Block *);

private:
    NamespaceBinding *_globals;
    NamespaceBinding *namespaceBinding;
    ClassBinding *classBinding;
};

Binder::Binder(NamespaceBinding *globals)
    : _globals(globals),
      namespaceBinding(0),
      classBinding(0)
{ }

Binder::~Binder()
{ }

NamespaceBinding *Binder::bind(Symbol *symbol, NamespaceBinding *binding)
{
    NamespaceBinding *previousBinding = switchNamespaceBinding(binding);
    accept(symbol);
    return switchNamespaceBinding(previousBinding);
}

NamespaceBinding *Binder::findOrCreateNamespaceBinding(Namespace *symbol)
{ return namespaceBinding->findOrCreateNamespaceBinding(symbol); }

Roberto Raggi's avatar
Roberto Raggi committed
668
NamespaceBinding *Binder::resolveNamespace(const Location &loc, const Name *name)
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
{
    if (! namespaceBinding)
        return 0;

    return namespaceBinding->resolveNamespace(loc, name);
}

NamespaceBinding *Binder::switchNamespaceBinding(NamespaceBinding *binding)
{
    NamespaceBinding *previousBinding = namespaceBinding;
    namespaceBinding = binding;
    return previousBinding;
}

ClassBinding *Binder::findOrCreateClassBinding(Class *classSymbol)
{
685
    // ### FINISH ME
686 687 688 689 690 691 692 693 694 695 696
    ClassBinding *binding = 0;

    if (classBinding)
        binding = new ClassBinding(classBinding);
    else
        binding = new ClassBinding(namespaceBinding);

    binding->symbols.append(classSymbol);
    return binding;
}

Roberto Raggi's avatar
Roberto Raggi committed
697
ClassBinding *Binder::findClassBinding(const Name *name)
698
{
699 700
    QSet<Binding *> processed;

Roberto Raggi's avatar
Roberto Raggi committed
701
    if (classBinding) {
702
        if (ClassBinding *k = classBinding->findClassBinding(name, &processed))
Roberto Raggi's avatar
Roberto Raggi committed
703
            return k;
704 705

        processed.clear();
Roberto Raggi's avatar
Roberto Raggi committed
706 707 708
    }

    if (namespaceBinding)
709
        return namespaceBinding->findClassBinding(name, &processed);
Roberto Raggi's avatar
Roberto Raggi committed
710

711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    return 0;
}

ClassBinding *Binder::switchClassBinding(ClassBinding *binding)
{
    ClassBinding *previousClassBinding = classBinding;
    classBinding = binding;
    return previousClassBinding;
}

bool Binder::visit(Namespace *symbol)
{
    NamespaceBinding *binding = findOrCreateNamespaceBinding(symbol);

    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        Symbol *member = symbol->memberAt(i);

        bind(member, binding);
    }

    return false;
}

bool Binder::visit(UsingNamespaceDirective *u)
{
    NamespaceBinding *resolved = resolveNamespace(Location(u), u->name());

    if (! resolved)
        return false;

    namespaceBinding->usings.append(resolved);

    return false;
}

bool Binder::visit(Class *classSymbol)
{
    ClassBinding *binding = findOrCreateClassBinding(classSymbol);
    ClassBinding *previousClassBinding = switchClassBinding(binding);

    for (unsigned i = 0; i < classSymbol->baseClassCount(); ++i) {
        BaseClass *baseClass = classSymbol->baseClassAt(i);
        ClassBinding *baseClassBinding = findClassBinding(baseClass->name());
        binding->baseClassBindings.append(baseClassBinding);
    }

    for (unsigned i = 0; i < classSymbol->memberCount(); ++i)
        accept(classSymbol->memberAt(i));

    (void) switchClassBinding(previousClassBinding);

    return false;
}

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

bool Binder::visit(Block *)
{ return false; }

} // end of anonymous namespace

773
static NamespaceBinding *find_helper(Namespace *symbol, NamespaceBinding *binding,
Roberto Raggi's avatar
Roberto Raggi committed
774
                                     QSet<NamespaceBinding *> *processed)
775
{
Roberto Raggi's avatar
Roberto Raggi committed
776 777
    if (binding && ! processed->contains(binding)) {
        processed->insert(binding);
778 779 780 781 782 783 784 785

        if (binding->symbols.contains(symbol))
            return binding;

        foreach (NamespaceBinding *nestedBinding, binding->children) {
            if (NamespaceBinding *ns = find_helper(symbol, nestedBinding, processed))
                return ns;
        }
Roberto Raggi's avatar
Roberto Raggi committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

        if (NamespaceBinding *a = find_helper(symbol, binding->anonymousNamespaceBinding, processed))
            return a;
    }

    return 0;
}

static ClassBinding *find_helper(Class *symbol, Binding *binding,
                                 QSet<Binding *> *processed)
{
    if (binding && ! processed->contains(binding)) {
        processed->insert(binding);

        if (NamespaceBinding *namespaceBinding = binding->asNamespaceBinding()) {
            foreach (ClassBinding *classBinding, namespaceBinding->classBindings) {
                if (ClassBinding *c = find_helper(symbol, classBinding, processed))
                    return c;
            }

            foreach (NamespaceBinding *nestedBinding, namespaceBinding->children) {
                if (ClassBinding *c = find_helper(symbol, nestedBinding, processed))
                    return c;
            }

            if (ClassBinding *a = find_helper(symbol, namespaceBinding->anonymousNamespaceBinding, processed))
                return a;

        } else if (ClassBinding *classBinding = binding->asClassBinding()) {
            foreach (Class *klass, classBinding->symbols) {
                if (klass == symbol)
                    return classBinding;
            }

            foreach (ClassBinding *nestedClassBinding, classBinding->children) {
                if (ClassBinding *c = find_helper(symbol, nestedClassBinding, processed))
                    return c;
            }

#if 0 // ### FIXME
            if (ClassBinding *a = find_helper(symbol, classBinding->anonymousClassBinding, processed))
                return a;
#endif
        }
830 831 832 833 834 835 836
    }

    return 0;
}

NamespaceBinding *NamespaceBinding::find(Namespace *symbol, NamespaceBinding *binding)
{
Roberto Raggi's avatar
Roberto Raggi committed
837 838 839 840 841 842 843
    QSet<NamespaceBinding *> processed;
    return find_helper(symbol, binding, &processed);
}

ClassBinding *NamespaceBinding::find(Class *symbol, NamespaceBinding *binding)
{
    QSet<Binding *> processed;
844 845 846
    return find_helper(symbol, binding, &processed);
}

Roberto Raggi's avatar
Roberto Raggi committed
847 848 849 850 851 852 853 854 855
NamespaceBindingPtr CPlusPlus::bind(Document::Ptr doc, Snapshot snapshot)
{
    NamespaceBindingPtr global(new NamespaceBinding());

    Binder bind(global.data());
    bind(doc, snapshot);
    return global;
}