Symbols.cpp 33.8 KB
Newer Older
con's avatar
con committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "Symbols.h"
#include "Names.h"
#include "TypeVisitor.h"
#include "SymbolVisitor.h"
25
#include "Matcher.h"
con's avatar
con committed
26
#include "Scope.h"
27
#include "Templates.h"
con's avatar
con committed
28

Roberto Raggi's avatar
Roberto Raggi committed
29
using namespace CPlusPlus;
con's avatar
con committed
30 31

UsingNamespaceDirective::UsingNamespaceDirective(TranslationUnit *translationUnit,
Roberto Raggi's avatar
Roberto Raggi committed
32
                                                 unsigned sourceLocation, const Name *name)
con's avatar
con committed
33 34 35
    : Symbol(translationUnit, sourceLocation, name)
{ }

36 37 38 39
UsingNamespaceDirective::UsingNamespaceDirective(Clone *clone, Subst *subst, UsingNamespaceDirective *original)
    : Symbol(clone, subst, original)
{ }

con's avatar
con committed
40 41 42 43 44 45 46 47 48
UsingNamespaceDirective::~UsingNamespaceDirective()
{ }

FullySpecifiedType UsingNamespaceDirective::type() const
{ return FullySpecifiedType(); }

void UsingNamespaceDirective::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

49 50 51 52 53
NamespaceAlias::NamespaceAlias(TranslationUnit *translationUnit,
                               unsigned sourceLocation, const Name *name)
    : Symbol(translationUnit, sourceLocation, name), _namespaceName(0)
{ }

54 55 56 57 58
NamespaceAlias::NamespaceAlias(Clone *clone, Subst *subst, NamespaceAlias *original)
    : Symbol(clone, subst, original)
    , _namespaceName(clone->name(original->_namespaceName, subst))
{ }

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
NamespaceAlias::~NamespaceAlias()
{ }

const Name *NamespaceAlias::namespaceName() const
{ return _namespaceName; }

void NamespaceAlias::setNamespaceName(const Name *namespaceName)
{ _namespaceName = namespaceName; }

FullySpecifiedType NamespaceAlias::type() const
{ return FullySpecifiedType(); }

void NamespaceAlias::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }


con's avatar
con committed
75
UsingDeclaration::UsingDeclaration(TranslationUnit *translationUnit,
Roberto Raggi's avatar
Roberto Raggi committed
76
                                   unsigned sourceLocation, const Name *name)
con's avatar
con committed
77 78 79
    : Symbol(translationUnit, sourceLocation, name)
{ }

80 81 82 83
UsingDeclaration::UsingDeclaration(Clone *clone, Subst *subst, UsingDeclaration *original)
    : Symbol(clone, subst, original)
{ }

con's avatar
con committed
84 85 86 87 88 89 90 91 92
UsingDeclaration::~UsingDeclaration()
{ }

FullySpecifiedType UsingDeclaration::type() const
{ return FullySpecifiedType(); }

void UsingDeclaration::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
93
Declaration::Declaration(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
Roberto Raggi's avatar
Roberto Raggi committed
94
    : Symbol(translationUnit, sourceLocation, name)
95
    , _initializer(0)
con's avatar
con committed
96 97
{ }

98 99 100
Declaration::Declaration(Clone *clone, Subst *subst, Declaration *original)
    : Symbol(clone, subst, original)
    , _type(clone->type(original->_type, subst))
101
    , _initializer(clone->stringLiteral(original->_initializer))
102 103
{ }

con's avatar
con committed
104
Declaration::~Declaration()
Roberto Raggi's avatar
Roberto Raggi committed
105
{ }
con's avatar
con committed
106

107
void Declaration::setType(const FullySpecifiedType &type)
con's avatar
con committed
108 109
{ _type = type; }

110 111 112 113 114
void Declaration::setInitializer(const StringLiteral *initializer)
{
    _initializer = initializer;
}

con's avatar
con committed
115 116 117
FullySpecifiedType Declaration::type() const
{ return _type; }

118 119 120 121 122
const StringLiteral *Declaration::getInitializer() const
{
    return _initializer;
}

con's avatar
con committed
123 124 125
void Declaration::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

126 127 128 129 130 131 132 133 134 135 136 137 138 139
EnumeratorDeclaration::EnumeratorDeclaration(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
    : Declaration(translationUnit, sourceLocation, name)
    , _constantValue(0)
{}

EnumeratorDeclaration::~EnumeratorDeclaration()
{}

const StringLiteral *EnumeratorDeclaration::constantValue() const
{ return _constantValue; }

void EnumeratorDeclaration::setConstantValue(const StringLiteral *constantValue)
{ _constantValue = constantValue; }

Roberto Raggi's avatar
Roberto Raggi committed
140
Argument::Argument(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
con's avatar
con committed
141
    : Symbol(translationUnit, sourceLocation, name),
142
      _initializer(0)
con's avatar
con committed
143 144
{ }

145 146 147 148 149 150
Argument::Argument(Clone *clone, Subst *subst, Argument *original)
    : Symbol(clone, subst, original)
    , _initializer(clone->stringLiteral(original->_initializer))
    , _type(clone->type(original->_type, subst))
{ }

con's avatar
con committed
151 152 153 154
Argument::~Argument()
{ }

bool Argument::hasInitializer() const
155 156 157
{ return _initializer != 0; }

const StringLiteral *Argument::initializer() const
con's avatar
con committed
158 159
{ return _initializer; }

160 161
void Argument::setInitializer(const StringLiteral *initializer)
{ _initializer = initializer; }
con's avatar
con committed
162

163
void Argument::setType(const FullySpecifiedType &type)
con's avatar
con committed
164 165 166 167 168 169 170 171
{ _type = type; }

FullySpecifiedType Argument::type() const
{ return _type; }

void Argument::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
172 173 174 175
TypenameArgument::TypenameArgument(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
    : Symbol(translationUnit, sourceLocation, name)
{ }

176 177 178 179 180
TypenameArgument::TypenameArgument(Clone *clone, Subst *subst, TypenameArgument *original)
    : Symbol(clone, subst, original)
    , _type(clone->type(original->_type, subst))
{ }

Roberto Raggi's avatar
Roberto Raggi committed
181 182 183 184 185 186 187 188 189 190 191 192
TypenameArgument::~TypenameArgument()
{ }

void TypenameArgument::setType(const FullySpecifiedType &type)
{ _type = type; }

FullySpecifiedType TypenameArgument::type() const
{ return _type; }

void TypenameArgument::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
193
Function::Function(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
Roberto Raggi's avatar
Roberto Raggi committed
194
    : Scope(translationUnit, sourceLocation, name),
195 196
      _flags(0)
{ }
con's avatar
con committed
197

198 199 200 201 202 203
Function::Function(Clone *clone, Subst *subst, Function *original)
    : Scope(clone, subst, original)
    , _returnType(clone->type(original->_returnType, subst))
    , _flags(original->_flags)
{ }

con's avatar
con committed
204
Function::~Function()
Roberto Raggi's avatar
Roberto Raggi committed
205
{ }
con's avatar
con committed
206 207

bool Function::isNormal() const
208
{ return f._methodKey == NormalMethod; }
con's avatar
con committed
209 210

bool Function::isSignal() const
211
{ return f._methodKey == SignalMethod; }
con's avatar
con committed
212 213

bool Function::isSlot() const
214
{ return f._methodKey == SlotMethod; }
con's avatar
con committed
215

216 217 218
bool Function::isInvokable() const
{ return f._methodKey == InvokableMethod; }

con's avatar
con committed
219
int Function::methodKey() const
220
{ return f._methodKey; }
con's avatar
con committed
221 222

void Function::setMethodKey(int key)
223
{ f._methodKey = key; }
con's avatar
con committed
224

225
bool Function::isSignatureEqualTo(const Function *other) const
con's avatar
con committed
226
{
227
    if (! other)
con's avatar
con committed
228
        return false;
229
    else if (isConst() != other->isConst())
230
        return false;
231
    else if (isVolatile() != other->isVolatile())
232 233
        return false;

234
    const Name *l = unqualifiedName();
235
    const Name *r = other->unqualifiedName();
con's avatar
con committed
236
    if (l == r || (l && l->isEqualTo(r))) {
237
        const unsigned argc = argumentCount();
238
        if (argc != other->argumentCount())
con's avatar
con committed
239
            return false;
240
        for (unsigned i = 0; i < argc; ++i) {
241
            Symbol *l = argumentAt(i);
242
            Symbol *r = other->argumentAt(i);
con's avatar
con committed
243 244 245 246 247 248 249 250
            if (! l->type().isEqualTo(r->type()))
                return false;
        }
        return true;
    }
    return false;
}

251 252 253 254 255 256 257 258
bool Function::isEqualTo(const Type *other) const
{
    const Function *o = other->asFunctionType();
    if (!isSignatureEqualTo(o))
        return false;
    return _returnType.isEqualTo(o->_returnType);
}

con's avatar
con committed
259 260 261
void Function::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

262
bool Function::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
263 264 265 266 267 268 269
{
    if (const Function *otherTy = otherType->asFunctionType())
        return matcher->match(this, otherTy);

    return false;
}

con's avatar
con committed
270
FullySpecifiedType Function::type() const
271 272 273 274 275 276
{
    FullySpecifiedType ty(const_cast<Function *>(this));
    ty.setConst(isConst());
    ty.setVolatile(isVolatile());
    return ty;
}
con's avatar
con committed
277 278 279 280

FullySpecifiedType Function::returnType() const
{ return _returnType; }

281
void Function::setReturnType(const FullySpecifiedType &returnType)
con's avatar
con committed
282 283
{ _returnType = returnType; }

284 285 286 287 288 289
bool Function::hasReturnType() const
{
    const FullySpecifiedType ty = returnType();
    return ty.isValid() || ty.isSigned() || ty.isUnsigned();
}

con's avatar
con committed
290 291
unsigned Function::argumentCount() const
{
292 293
    const unsigned memCnt = memberCount();
    if (memCnt > 0 && memberAt(0)->type()->isVoidType())
294
        return 0;
295 296 297 298 299 300 301 302

    // Definitions with function-try-blocks will have more than a block, and
    // arguments with a lambda as default argument will also have more blocks.
    unsigned argc = 0;
    for (unsigned it = 0; it < memCnt; ++it)
        if (memberAt(it)->isArgument())
            ++argc;
    return argc;
con's avatar
con committed
303 304 305
}

Symbol *Function::argumentAt(unsigned index) const
306 307 308 309 310 311 312 313 314 315 316 317
{
    for (unsigned it = 0, eit = memberCount(); it < eit; ++it) {
        if (Argument *arg = memberAt(it)->asArgument()) {
            if (index == 0)
                return arg;
            else
                --index;
        }
    }

    return 0;
}
con's avatar
con committed
318

319 320
bool Function::hasArguments() const
{
321 322
    unsigned argc = argumentCount();
    return ! (argc == 0 || (argc == 1 && argumentAt(0)->type()->isVoidType()));
323 324
}

Roberto Raggi's avatar
Roberto Raggi committed
325 326 327 328
unsigned Function::minimumArgumentCount() const
{
    unsigned index = 0;

329
    for (unsigned ei = argumentCount(); index < ei; ++index) {
330
        if (Argument *arg = argumentAt(index)->asArgument()) {
Roberto Raggi's avatar
Roberto Raggi committed
331 332 333 334 335 336 337 338
            if (arg->hasInitializer())
                break;
        }
    }

    return index;
}

339 340 341 342 343 344
bool Function::isVirtual() const
{ return f._isVirtual; }

void Function::setVirtual(bool isVirtual)
{ f._isVirtual = isVirtual; }

345 346 347 348 349 350 351 352 353 354 355 356
bool Function::isOverride() const
{ return f._isOverride; }

void Function::setOverride(bool isOverride)
{ f._isOverride = isOverride; }

bool Function::isFinal() const
{ return f._isFinal; }

void Function::setFinal(bool isFinal)
{ f._isFinal = isFinal; }

con's avatar
con committed
357
bool Function::isVariadic() const
358
{ return f._isVariadic; }
con's avatar
con committed
359 360

void Function::setVariadic(bool isVariadic)
361
{ f._isVariadic = isVariadic; }
con's avatar
con committed
362 363

bool Function::isConst() const
364
{ return f._isConst; }
con's avatar
con committed
365 366

void Function::setConst(bool isConst)
367
{ f._isConst = isConst; }
con's avatar
con committed
368 369

bool Function::isVolatile() const
370
{ return f._isVolatile; }
con's avatar
con committed
371 372

void Function::setVolatile(bool isVolatile)
373
{ f._isVolatile = isVolatile; }
con's avatar
con committed
374 375

bool Function::isPureVirtual() const
376
{ return f._isPureVirtual; }
con's avatar
con committed
377 378

void Function::setPureVirtual(bool isPureVirtual)
379
{ f._isPureVirtual = isPureVirtual; }
con's avatar
con committed
380

381
bool Function::isAmbiguous() const
382
{ return f._isAmbiguous; }
383 384

void Function::setAmbiguous(bool isAmbiguous)
385
{ f._isAmbiguous = isAmbiguous; }
386

con's avatar
con committed
387 388 389 390 391 392 393 394 395
void Function::visitSymbol0(SymbolVisitor *visitor)
{
    if (visitor->visit(this)) {
        for (unsigned i = 0; i < memberCount(); ++i) {
            visitSymbol(memberAt(i), visitor);
        }
    }
}

396 397
bool Function::maybeValidPrototype(unsigned actualArgumentCount) const
{
398
    const unsigned argc = argumentCount();
399 400
    unsigned minNumberArguments = 0;

401 402
    for (; minNumberArguments < argc; ++minNumberArguments) {
        Argument *arg = argumentAt(minNumberArguments)->asArgument();
403

404
        if (! arg)
405 406
            return false;

407 408 409 410 411 412 413 414
        if (arg->hasInitializer())
            break;
    }

    if (actualArgumentCount < minNumberArguments) {
        // not enough arguments.
        return false;

415
    } else if (!isVariadic() && actualArgumentCount > argc) {
416 417 418 419 420 421 422 423
        // too many arguments.
        return false;
    }

    return true;
}


con's avatar
con committed
424
Block::Block(TranslationUnit *translationUnit, unsigned sourceLocation)
Roberto Raggi's avatar
Roberto Raggi committed
425
    : Scope(translationUnit, sourceLocation, /*name = */ 0)
con's avatar
con committed
426 427
{ }

428 429 430 431
Block::Block(Clone *clone, Subst *subst, Block *original)
    : Scope(clone, subst, original)
{ }

con's avatar
con committed
432 433 434 435 436 437 438
Block::~Block()
{ }

FullySpecifiedType Block::type() const
{ return FullySpecifiedType(); }

void Block::visitSymbol0(SymbolVisitor *visitor)
439 440 441 442 443 444 445
{
    if (visitor->visit(this)) {
        for (unsigned i = 0; i < memberCount(); ++i) {
            visitSymbol(memberAt(i), visitor);
        }
    }
}
con's avatar
con committed
446

Roberto Raggi's avatar
Roberto Raggi committed
447
Enum::Enum(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
Roberto Raggi's avatar
Roberto Raggi committed
448
    : Scope(translationUnit, sourceLocation, name)
449
    , _isScoped(false)
con's avatar
con committed
450 451
{ }

452 453
Enum::Enum(Clone *clone, Subst *subst, Enum *original)
    : Scope(clone, subst, original)
454
    , _isScoped(original->isScoped())
455 456
{ }

con's avatar
con committed
457 458 459 460 461 462 463 464
Enum::~Enum()
{ }

FullySpecifiedType Enum::type() const
{ return FullySpecifiedType(const_cast<Enum *>(this)); }

bool Enum::isEqualTo(const Type *other) const
{
465
    const Enum *o = other->asEnumType();
con's avatar
con committed
466 467
    if (! o)
        return false;
468 469
    const Name *l = unqualifiedName();
    const Name *r = o->unqualifiedName();
con's avatar
con committed
470 471 472 473 474 475 476
    if (l == r)
        return true;
    else if (! l)
        return false;
    return l->isEqualTo(r);
}

477 478 479 480 481 482 483 484 485 486
bool Enum::isScoped() const
{
    return _isScoped;
}

void Enum::setScoped(bool scoped)
{
    _isScoped = scoped;
}

con's avatar
con committed
487 488 489
void Enum::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

490
bool Enum::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
491 492 493 494 495 496 497
{
    if (const Enum *otherTy = otherType->asEnumType())
        return matcher->match(this, otherTy);

    return false;
}

con's avatar
con committed
498 499 500 501 502 503 504 505 506
void Enum::visitSymbol0(SymbolVisitor *visitor)
{
    if (visitor->visit(this)) {
        for (unsigned i = 0; i < memberCount(); ++i) {
            visitSymbol(memberAt(i), visitor);
        }
    }
}

507 508 509 510
Template::Template(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
    : Scope(translationUnit, sourceLocation, name)
{ }

511 512 513 514
Template::Template(Clone *clone, Subst *subst, Template *original)
    : Scope(clone, subst, original)
{ }

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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
Template::~Template()
{ }

unsigned Template::templateParameterCount() const
{
    if (declaration() != 0)
        return memberCount() - 1;

    return 0;
}

Symbol *Template::templateParameterAt(unsigned index) const
{ return memberAt(index); }

Symbol *Template::declaration() const
{
    if (isEmpty())
        return 0;

    if (Symbol *s = memberAt(memberCount() - 1)) {
        if (s->isClass() || s->isForwardClassDeclaration() ||
            s->isTemplate() || s->isFunction() || s->isDeclaration())
            return s;
    }

    return 0;
}

FullySpecifiedType Template::type() const
{ return FullySpecifiedType(const_cast<Template *>(this)); }

bool Template::isEqualTo(const Type *other) const
{ return other == this; }

void Template::visitSymbol0(SymbolVisitor *visitor)
{
    if (visitor->visit(this)) {
        for (unsigned i = 0; i < memberCount(); ++i) {
            visitSymbol(memberAt(i), visitor);
        }
    }
}

void Template::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

561
bool Template::match0(const Type *otherType, Matcher *matcher) const
562 563 564 565 566 567
{
    if (const Template *otherTy = otherType->asTemplateType())
        return matcher->match(this, otherTy);
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
568
Namespace::Namespace(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
Roberto Raggi's avatar
Roberto Raggi committed
569
    : Scope(translationUnit, sourceLocation, name)
570
    , _isInline(false)
con's avatar
con committed
571 572
{ }

573 574
Namespace::Namespace(Clone *clone, Subst *subst, Namespace *original)
    : Scope(clone, subst, original)
575
    , _isInline(original->_isInline)
576 577
{ }

con's avatar
con committed
578 579 580 581 582
Namespace::~Namespace()
{ }

bool Namespace::isEqualTo(const Type *other) const
{
583
    const Namespace *o = other->asNamespaceType();
con's avatar
con committed
584 585
    if (! o)
        return false;
586 587
    const Name *l = unqualifiedName();
    const Name *r = o->unqualifiedName();
con's avatar
con committed
588 589 590 591 592 593 594 595
    if (l == r || (l && l->isEqualTo(r)))
        return true;
    return false;
}

void Namespace::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

596
bool Namespace::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
597 598 599 600 601 602 603
{
    if (const Namespace *otherTy = otherType->asNamespaceType())
        return matcher->match(this, otherTy);

    return false;
}

con's avatar
con committed
604 605 606 607 608 609 610 611 612 613 614 615
void Namespace::visitSymbol0(SymbolVisitor *visitor)
{
    if (visitor->visit(this)) {
        for (unsigned i = 0; i < memberCount(); ++i) {
            visitSymbol(memberAt(i), visitor);
        }
    }
}

FullySpecifiedType Namespace::type() const
{ return FullySpecifiedType(const_cast<Namespace *>(this)); }

Roberto Raggi's avatar
Roberto Raggi committed
616
BaseClass::BaseClass(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
con's avatar
con committed
617 618 619 620
    : Symbol(translationUnit, sourceLocation, name),
      _isVirtual(false)
{ }

621 622 623 624 625 626
BaseClass::BaseClass(Clone *clone, Subst *subst, BaseClass *original)
    : Symbol(clone, subst, original)
    , _isVirtual(original->_isVirtual)
    , _type(clone->type(original->_type, subst))
{ }

con's avatar
con committed
627 628 629 630
BaseClass::~BaseClass()
{ }

FullySpecifiedType BaseClass::type() const
Roberto Raggi's avatar
Roberto Raggi committed
631 632 633 634
{ return _type; }

void BaseClass::setType(const FullySpecifiedType &type)
{ _type = type; }
con's avatar
con committed
635 636 637 638 639 640 641 642

bool BaseClass::isVirtual() const
{ return _isVirtual; }

void BaseClass::setVirtual(bool isVirtual)
{ _isVirtual = isVirtual; }

void BaseClass::visitSymbol0(SymbolVisitor *visitor)
643 644 645
{ visitor->visit(this); }

ForwardClassDeclaration::ForwardClassDeclaration(TranslationUnit *translationUnit,
Roberto Raggi's avatar
Roberto Raggi committed
646
                                                 unsigned sourceLocation, const Name *name)
Roberto Raggi's avatar
Roberto Raggi committed
647
    : Symbol(translationUnit, sourceLocation, name)
648 649
{ }

650 651 652 653
ForwardClassDeclaration::ForwardClassDeclaration(Clone *clone, Subst *subst, ForwardClassDeclaration *original)
    : Symbol(clone, subst, original)
{ }

654
ForwardClassDeclaration::~ForwardClassDeclaration()
Roberto Raggi's avatar
Roberto Raggi committed
655
{ }
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

FullySpecifiedType ForwardClassDeclaration::type() const
{ return FullySpecifiedType(const_cast<ForwardClassDeclaration *>(this)); }

bool ForwardClassDeclaration::isEqualTo(const Type *other) const
{
    if (const ForwardClassDeclaration *otherClassFwdTy = other->asForwardClassDeclarationType()) {
        if (name() == otherClassFwdTy->name())
            return true;
        else if (name() && otherClassFwdTy->name())
            return name()->isEqualTo(otherClassFwdTy->name());

        return false;
    }
    return false;
}

void ForwardClassDeclaration::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

void ForwardClassDeclaration::accept0(TypeVisitor *visitor)
con's avatar
con committed
677 678
{ visitor->visit(this); }

679
bool ForwardClassDeclaration::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
680 681 682 683 684 685 686
{
    if (const ForwardClassDeclaration *otherTy = otherType->asForwardClassDeclarationType())
        return matcher->match(this, otherTy);

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
687
Class::Class(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
Roberto Raggi's avatar
Roberto Raggi committed
688
    : Scope(translationUnit, sourceLocation, name),
Roberto Raggi's avatar
Roberto Raggi committed
689
      _key(ClassKey)
con's avatar
con committed
690 691
{ }

692 693 694 695 696 697 698 699
Class::Class(Clone *clone, Subst *subst, Class *original)
    : Scope(clone, subst, original)
    , _key(original->_key)
{
    for (size_t i = 0; i < original->_baseClasses.size(); ++i)
        addBaseClass(clone->symbol(original->_baseClasses.at(i), subst)->asBaseClass());
}

con's avatar
con committed
700
Class::~Class()
Roberto Raggi's avatar
Roberto Raggi committed
701
{ }
con's avatar
con committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720

bool Class::isClass() const
{ return _key == ClassKey; }

bool Class::isStruct() const
{ return _key == StructKey; }

bool Class::isUnion() const
{ return _key == UnionKey; }

Class::Key Class::classKey() const
{ return _key; }

void Class::setClassKey(Key key)
{ _key = key; }

void Class::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

721
bool Class::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
722 723 724 725 726 727 728
{
    if (const Class *otherTy = otherType->asClassType())
        return matcher->match(this, otherTy);

    return false;
}

con's avatar
con committed
729
unsigned Class::baseClassCount() const
730
{ return unsigned(_baseClasses.size()); }
con's avatar
con committed
731 732 733 734 735 736 737 738 739 740 741 742

BaseClass *Class::baseClassAt(unsigned index) const
{ return _baseClasses.at(index); }

void Class::addBaseClass(BaseClass *baseClass)
{ _baseClasses.push_back(baseClass); }

FullySpecifiedType Class::type() const
{ return FullySpecifiedType(const_cast<Class *>(this)); }

bool Class::isEqualTo(const Type *other) const
{
743
    const Class *o = other->asClassType();
con's avatar
con committed
744 745
    if (! o)
        return false;
746 747
    const Name *l = unqualifiedName();
    const Name *r = o->unqualifiedName();
con's avatar
con committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
    if (l == r || (l && l->isEqualTo(r)))
        return true;
    else
        return false;
}

void Class::visitSymbol0(SymbolVisitor *visitor)
{
    if (visitor->visit(this)) {
        for (unsigned i = 0; i < _baseClasses.size(); ++i) {
            visitSymbol(_baseClasses.at(i), visitor);
        }
        for (unsigned i = 0; i < memberCount(); ++i) {
            visitSymbol(memberAt(i), visitor);
        }
    }
}

766 767 768

QtPropertyDeclaration::QtPropertyDeclaration(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
    : Symbol(translationUnit, sourceLocation, name)
769
    , _flags(NoFlags)
770 771
{ }

772 773 774 775 776 777
QtPropertyDeclaration::QtPropertyDeclaration(Clone *clone, Subst *subst, QtPropertyDeclaration *original)
    : Symbol(clone, subst, original)
    , _type(clone->type(original->_type, subst))
    , _flags(original->_flags)
{ }

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
QtPropertyDeclaration::~QtPropertyDeclaration()
{ }

void QtPropertyDeclaration::setType(const FullySpecifiedType &type)
{ _type = type; }

void QtPropertyDeclaration::setFlags(int flags)
{ _flags = flags; }

int QtPropertyDeclaration::flags() const
{ return _flags; }

FullySpecifiedType QtPropertyDeclaration::type() const
{ return _type; }

void QtPropertyDeclaration::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }


797 798 799 800
QtEnum::QtEnum(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
    : Symbol(translationUnit, sourceLocation, name)
{ }

801 802 803 804
QtEnum::QtEnum(Clone *clone, Subst *subst, QtEnum *original)
    : Symbol(clone, subst, original)
{ }

805 806 807 808 809 810 811 812 813 814
QtEnum::~QtEnum()
{ }

FullySpecifiedType QtEnum::type() const
{ return FullySpecifiedType(); }

void QtEnum::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }


Roberto Raggi's avatar
Roberto Raggi committed
815
ObjCBaseClass::ObjCBaseClass(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
816 817 818
    : Symbol(translationUnit, sourceLocation, name)
{ }

819 820 821 822
ObjCBaseClass::ObjCBaseClass(Clone *clone, Subst *subst, ObjCBaseClass *original)
    : Symbol(clone, subst, original)
{ }

823 824 825 826 827 828 829 830 831
ObjCBaseClass::~ObjCBaseClass()
{ }

FullySpecifiedType ObjCBaseClass::type() const
{ return FullySpecifiedType(); }

void ObjCBaseClass::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
832
ObjCBaseProtocol::ObjCBaseProtocol(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
833 834 835
    : Symbol(translationUnit, sourceLocation, name)
{ }

836 837 838 839
ObjCBaseProtocol::ObjCBaseProtocol(Clone *clone, Subst *subst, ObjCBaseProtocol *original)
    : Symbol(clone, subst, original)
{ }

840 841 842 843 844 845 846 847 848
ObjCBaseProtocol::~ObjCBaseProtocol()
{ }

FullySpecifiedType ObjCBaseProtocol::type() const
{ return FullySpecifiedType(); }

void ObjCBaseProtocol::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
849
ObjCClass::ObjCClass(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name):
850 851 852 853
    Scope(translationUnit, sourceLocation, name),
    _categoryName(0),
    _baseClass(0),
    _isInterface(false)
854 855 856 857 858 859 860
{ }

ObjCClass::ObjCClass(Clone *clone, Subst *subst, ObjCClass *original)
    : Scope(clone, subst, original)
    , _categoryName(clone->name(original->_categoryName, subst))
    , _baseClass(0)
    , _isInterface(original->_isInterface)
861
{
862 863 864 865
    if (original->_baseClass)
        _baseClass = clone->symbol(original->_baseClass, subst)->asObjCBaseClass();
    for (size_t i = 0; i < original->_protocols.size(); ++i)
        addProtocol(clone->symbol(original->_protocols.at(i), subst)->asObjCBaseProtocol());
866 867 868 869 870
}

ObjCClass::~ObjCClass()
{}

Roberto Raggi's avatar
Cleanup  
Roberto Raggi committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
bool ObjCClass::isInterface() const
{ return _isInterface; }

void ObjCClass::setInterface(bool isInterface)
{ _isInterface = isInterface; }

bool ObjCClass::isCategory() const
{ return _categoryName != 0; }

const Name *ObjCClass::categoryName() const
{ return _categoryName; }

void ObjCClass::setCategoryName(const Name *categoryName)
{ _categoryName = categoryName; }

ObjCBaseClass *ObjCClass::baseClass() const
{ return _baseClass; }

void ObjCClass::setBaseClass(ObjCBaseClass *baseClass)
{ _baseClass = baseClass; }

unsigned ObjCClass::protocolCount() const
893
{ return unsigned(_protocols.size()); }
Roberto Raggi's avatar
Cleanup  
Roberto Raggi committed
894 895 896 897 898 899 900

ObjCBaseProtocol *ObjCClass::protocolAt(unsigned index) const
{ return _protocols.at(index); }

void ObjCClass::addProtocol(ObjCBaseProtocol *protocol)
{ _protocols.push_back(protocol); }

901 902 903 904 905 906 907 908 909
FullySpecifiedType ObjCClass::type() const
{ return FullySpecifiedType(const_cast<ObjCClass *>(this)); }

bool ObjCClass::isEqualTo(const Type *other) const
{
    const ObjCClass *o = other->asObjCClassType();
    if (!o)
        return false;

910 911
    const Name *l = unqualifiedName();
    const Name *r = o->unqualifiedName();
912 913 914 915 916 917 918 919 920
    if (l == r || (l && l->isEqualTo(r)))
        return true;
    else
        return false;
}

void ObjCClass::visitSymbol0(SymbolVisitor *visitor)
{
    if (visitor->visit(this)) {
921 922 923
        if (_baseClass)
            visitSymbol(_baseClass, visitor);

924 925
        for (unsigned i = 0; i < _protocols.size(); ++i)
            visitSymbol(_protocols.at(i), visitor);
926 927 928

        for (unsigned i = 0; i < memberCount(); ++i)
            visitSymbol(memberAt(i), visitor);
929 930 931 932 933 934
    }
}

void ObjCClass::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

935
bool ObjCClass::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
936 937 938 939 940 941 942
{
    if (const ObjCClass *otherTy = otherType->asObjCClassType())
        return matcher->match(this, otherTy);

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
943
ObjCProtocol::ObjCProtocol(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name):
Roberto Raggi's avatar
Roberto Raggi committed
944
        Scope(translationUnit, sourceLocation, name)
945 946 947
{
}

948 949 950 951 952 953 954
ObjCProtocol::ObjCProtocol(Clone *clone, Subst *subst, ObjCProtocol *original)
    : Scope(clone, subst, original)
{
    for (size_t i = 0; i < original->_protocols.size(); ++i)
        addProtocol(clone->symbol(original->_protocols.at(i), subst)->asObjCBaseProtocol());
}

955 956 957
ObjCProtocol::~ObjCProtocol()
{}

Roberto Raggi's avatar
Cleanup  
Roberto Raggi committed
958
unsigned ObjCProtocol::protocolCount() const
959
{ return unsigned(_protocols.size()); }
Roberto Raggi's avatar
Cleanup  
Roberto Raggi committed
960 961 962 963 964 965 966

ObjCBaseProtocol *ObjCProtocol::protocolAt(unsigned index) const
{ return _protocols.at(index); }

void ObjCProtocol::addProtocol(ObjCBaseProtocol *protocol)
{ _protocols.push_back(protocol); }

967 968 969 970 971 972 973 974 975
FullySpecifiedType ObjCProtocol::type() const
{ return FullySpecifiedType(const_cast<ObjCProtocol *>(this)); }

bool ObjCProtocol::isEqualTo(const Type *other) const
{
    const ObjCProtocol *o = other->asObjCProtocolType();
    if (!o)
        return false;

976 977
    const Name *l = unqualifiedName();
    const Name *r = o->unqualifiedName();
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
    if (l == r || (l && l->isEqualTo(r)))
        return true;
    else
        return false;
}

void ObjCProtocol::visitSymbol0(SymbolVisitor *visitor)
{
    if (visitor->visit(this)) {
        for (unsigned i = 0; i < _protocols.size(); ++i)
            visitSymbol(_protocols.at(i), visitor);
    }
}

void ObjCProtocol::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

995
bool ObjCProtocol::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
996 997 998 999 1000 1001 1002
{
    if (const ObjCProtocol *otherTy = otherType->asObjCProtocolType())
        return matcher->match(this, otherTy);

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
1003 1004
ObjCForwardClassDeclaration::ObjCForwardClassDeclaration(TranslationUnit *translationUnit, unsigned sourceLocation,
                                                         const Name *name):
1005 1006 1007 1008
        Symbol(translationUnit, sourceLocation, name)
{
}

1009 1010 1011 1012
ObjCForwardClassDeclaration::ObjCForwardClassDeclaration(Clone *clone, Subst *subst, ObjCForwardClassDeclaration *original)
    : Symbol(clone, subst, original)
{ }

1013 1014 1015 1016
ObjCForwardClassDeclaration::~ObjCForwardClassDeclaration()
{}

FullySpecifiedType ObjCForwardClassDeclaration::type() const
Erik Verbruggen's avatar
Erik Verbruggen committed
1017
{ return FullySpecifiedType(); }
1018

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
bool ObjCForwardClassDeclaration::isEqualTo(const Type *other) const
{
    if (const ObjCForwardClassDeclaration *otherFwdClass = other->asObjCForwardClassDeclarationType()) {
        if (name() == otherFwdClass->name())
            return true;
        else if (name() && otherFwdClass->name())
            return name()->isEqualTo(otherFwdClass->name());
        else
            return false;
    }

    return false;
}

1033 1034 1035
void ObjCForwardClassDeclaration::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

1036 1037 1038
void ObjCForwardClassDeclaration::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

1039
bool ObjCForwardClassDeclaration::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
1040 1041 1042 1043 1044 1045 1046
{
    if (const ObjCForwardClassDeclaration *otherTy = otherType->asObjCForwardClassDeclarationType())
        return matcher->match(this, otherTy);

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
1047 1048
ObjCForwardProtocolDeclaration::ObjCForwardProtocolDeclaration(TranslationUnit *translationUnit, unsigned sourceLocation,
                                                               const Name *name):
1049 1050 1051 1052
        Symbol(translationUnit, sourceLocation, name)
{
}

1053 1054 1055 1056
ObjCForwardProtocolDeclaration::ObjCForwardProtocolDeclaration(Clone *clone, Subst *subst, ObjCForwardProtocolDeclaration *original)
    : Symbol(clone, subst, original)
{ }

1057 1058 1059 1060
ObjCForwardProtocolDeclaration::~ObjCForwardProtocolDeclaration()
{}

FullySpecifiedType ObjCForwardProtocolDeclaration::type() const
Erik Verbruggen's avatar
Erik Verbruggen committed
1061
{ return FullySpecifiedType(); }
1062

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
bool ObjCForwardProtocolDeclaration::isEqualTo(const Type *other) const
{
    if (const ObjCForwardProtocolDeclaration *otherFwdProtocol = other->asObjCForwardProtocolDeclarationType()) {
        if (name() == otherFwdProtocol->name())
            return true;
        else if (name() && otherFwdProtocol->name())
            return name()->isEqualTo(otherFwdProtocol->name());
        else
            return false;
    }

    return false;
}

1077 1078 1079
void ObjCForwardProtocolDeclaration::visitSymbol0(SymbolVisitor *visitor)
{ visitor->visit(this); }

1080 1081 1082
void ObjCForwardProtocolDeclaration::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

1083
bool ObjCForwardProtocolDeclaration::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
1084 1085 1086 1087 1088 1089 1090
{
    if (const ObjCForwardProtocolDeclaration *otherTy = otherType->asObjCForwardProtocolDeclarationType())
        return matcher->match(this, otherTy);

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
1091
ObjCMethod::ObjCMethod(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
Roberto Raggi's avatar
Roberto Raggi committed
1092
    : Scope(translationUnit, sourceLocation, name),
1093
     _flags(0)
Roberto Raggi's avatar
Roberto Raggi committed
1094
{ }
1095

1096 1097 1098 1099 1100 1101
ObjCMethod::ObjCMethod(Clone *clone, Subst *subst, ObjCMethod *original)
    : Scope(clone, subst, original)
    , _returnType(clone->type(original->_returnType, subst))
    , _flags(original->_flags)
{ }

1102
ObjCMethod::~ObjCMethod()
Roberto Raggi's avatar
Roberto Raggi committed
1103
{ }
1104 1105 1106 1107 1108 1109 1110

bool ObjCMethod::isEqualTo(const Type *other) const
{
    const ObjCMethod *o = other->asObjCMethodType();
    if (! o)
        return false;

1111 1112
    const Name *l = unqualifiedName();
    const Name *r = o->unqualifiedName();
1113
    if (l == r || (l && l->isEqualTo(r))) {
Roberto Raggi's avatar
Roberto Raggi committed
1114
        if (argumentCount() != o->argumentCount())
1115 1116 1117
            return false;
        else if (! _returnType.isEqualTo(o->_returnType))
            return false;
Roberto Raggi's avatar
Roberto Raggi committed
1118 1119 1120
        for (unsigned i = 0; i < argumentCount(); ++i) {
            Symbol *l = argumentAt(i);
            Symbol *r = o->argumentAt(i);
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
            if (! l->type().isEqualTo(r->type()))
                return false;
        }
        return true;
    }
    return false;
}

void ObjCMethod::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }

1132
bool ObjCMethod::match0(const Type *otherType, Matcher *matcher) const
Roberto Raggi's avatar
Roberto Raggi committed
1133 1134 1135 1136 1137 1138 1139
{
    if (const ObjCMethod *otherTy = otherType->asObjCMethodType())
        return matcher->match(this, otherTy);

    return false;
}

1140 1141 1142 1143 1144 1145
FullySpecifiedType ObjCMethod::type() const
{ return FullySpecifiedType(const_cast<ObjCMethod *>(this)); }

FullySpecifiedType ObjCMethod::returnType() const
{ return _returnType; }

1146
void ObjCMethod::setReturnType(const FullySpecifiedType &returnType)
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
{ _returnType = returnType; }

bool ObjCMethod::hasReturnType() const
{
    const FullySpecifiedType ty = returnType();
    return ty.isValid() || ty.isSigned() || ty.isUnsigned();
}

unsigned ObjCMethod::argumentCount() const
{
1157 1158 1159 1160
    const unsigned c = memberCount();
    if (c > 0 && memberAt(c - 1)->isBlock())
        return c - 1;
    return c;
1161 1162 1163
}

Symbol *ObjCMethod::argumentAt(unsigned index) const
Roberto Raggi's avatar
Roberto Raggi committed
1164 1165 1166
{
    return memberAt(index);
}
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

bool ObjCMethod::hasArguments() const
{
    return ! (argumentCount() == 0 ||
              (argumentCount() == 1 && argumentAt(0)->type()->isVoidType()));
}

bool ObjCMethod::isVariadic() const
{ return f._isVariadic; }

void ObjCMethod::setVariadic(bool isVariadic)
{ f._isVariadic = isVariadic; }

void ObjCMethod::visitSymbol0(SymbolVisitor *visitor)
{
    if (visitor->visit(this)) {
        for (unsigned i = 0; i < memberCount(); ++i) {
            visitSymbol(memberAt(i), visitor);
        }
    }
}

1189 1190
ObjCPropertyDeclaration::ObjCPropertyDeclaration(TranslationUnit *translationUnit,
                                                 unsigned sourceLocation,
Roberto Raggi's avatar
Roberto Raggi committed
1191
                                                 const Name *name):
1192 1193
    Symbol(translationUnit, sourceLocation, name),
    _getterName(0),
1194 1195
    _setterName(0),
    _propertyAttributes(None)
1196 1197
{}

1198 1199 1200 1201 1202 1203 1204 1205
ObjCPropertyDeclaration::ObjCPropertyDeclaration(Clone *clone, Subst *subst, ObjCPropertyDeclaration *original)
    : Symbol(clone, subst, original)
    , _getterName(clone->name(original->_getterName, subst))
    , _setterName(clone->name(original->_setterName, subst))
    , _type(clone->type(original->_type, subst))
    , _propertyAttributes(original->_propertyAttributes)
{ }

1206 1207 1208
ObjCPropertyDeclaration::~ObjCPropertyDeclaration()
{}

Roberto Raggi's avatar
Cleanup  
Roberto Raggi committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
bool ObjCPropertyDeclaration::hasAttribute(int attribute) const
{ return _propertyAttributes & attribute; }

void ObjCPropertyDeclaration::setAttributes(int attributes)
{ _propertyAttributes = attributes; }

bool ObjCPropertyDeclaration::hasGetter() const
{ return hasAttribute(Getter); }

bool ObjCPropertyDeclaration::hasSetter() const
{ return hasAttribute(Setter); }

const Name *ObjCPropertyDeclaration::getterName() const
{ return _getterName; }

void ObjCPropertyDeclaration::setGetterName(const Name *getterName)
{ _getterName = getterName; }

const Name *ObjCPropertyDeclaration::setterName() const
{ return _setterName; }

void ObjCPropertyDeclaration::setSetterName(const Name *setterName)
{ _setterName = setterName; }

void ObjCPropertyDeclaration::setType(const FullySpecifiedType &type)
{ _type = type; }

1236 1237
FullySpecifiedType ObjCPropertyDeclaration::type() const
{ return _type; }
Roberto Raggi's avatar
Roberto Raggi committed
1238

1239 1240 1241 1242 1243
void ObjCPropertyDeclaration::visitSymbol0(SymbolVisitor *visitor)
{
    if (visitor->visit(this)) {
    }
}