Symbol.cpp 10.3 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
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).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25
** 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.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
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
// 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 "Symbol.h"
#include "Symbols.h"
#include "Control.h"
#include "Names.h"
#include "TranslationUnit.h"
#include "Literals.h"
#include "MemoryPool.h"
#include "SymbolVisitor.h"
#include "NameVisitor.h"
58
#include "Scope.h"
con's avatar
con committed
59 60
#include <cassert>

Roberto Raggi's avatar
Roberto Raggi committed
61
using namespace CPlusPlus;
con's avatar
con committed
62 63 64 65 66 67 68 69 70 71 72

class Symbol::HashCode: protected NameVisitor
{
public:
    HashCode()
        : _value(0)
    { }

    virtual ~HashCode()
    { }

Roberto Raggi's avatar
Roberto Raggi committed
73
    unsigned operator()(const Name *name)
con's avatar
con committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    {
        unsigned previousValue = switchValue(0);
        accept(name);
        return switchValue(previousValue);
    }

protected:
    unsigned switchValue(unsigned value)
    {
        unsigned previousValue = _value;
        _value = value;
        return previousValue;
    }

Roberto Raggi's avatar
Roberto Raggi committed
88
    virtual void visit(const NameId *name)
con's avatar
con committed
89 90
    { _value = name->identifier()->hashCode(); }

Roberto Raggi's avatar
Roberto Raggi committed
91
    virtual void visit(const TemplateNameId *name)
con's avatar
con committed
92 93
    { _value = name->identifier()->hashCode(); }

Roberto Raggi's avatar
Roberto Raggi committed
94
    virtual void visit(const DestructorNameId *name)
con's avatar
con committed
95 96
    { _value = name->identifier()->hashCode(); }

Roberto Raggi's avatar
Roberto Raggi committed
97
    virtual void visit(const OperatorNameId *name)
con's avatar
con committed
98 99
    { _value = unsigned(name->kind()); }

Roberto Raggi's avatar
Roberto Raggi committed
100
    virtual void visit(const ConversionNameId *)
con's avatar
con committed
101 102
    { _value = 0; } // ### TODO: implement me

Roberto Raggi's avatar
Roberto Raggi committed
103
    virtual void visit(const QualifiedNameId *name)
104
    { _value = operator()(name->name()); }
con's avatar
con committed
105

Roberto Raggi's avatar
Roberto Raggi committed
106
    virtual void visit(const SelectorNameId *name)
107 108
    { _value = name->identifier()->hashCode(); }

con's avatar
con committed
109 110 111 112
private:
    unsigned _value;
};

Roberto Raggi's avatar
Roberto Raggi committed
113
Symbol::Symbol(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
114
    : _name(0),
con's avatar
con committed
115 116 117 118 119
      _hashCode(0),
      _storage(Symbol::NoStorage),
      _visibility(Symbol::Public),
      _scope(0),
      _index(0),
120
      _next(0),
121 122 123
      _isGenerated(false),
      _isDeprecated(false),
      _isUnavailable(false)
con's avatar
con committed
124
{
125
    setSourceLocation(sourceLocation, translationUnit);
con's avatar
con committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    setName(name);
}

Symbol::~Symbol()
{ }

void Symbol::visitSymbol(SymbolVisitor *visitor)
{
    if (visitor->preVisit(this))
        visitSymbol0(visitor);
    visitor->postVisit(this);
}

void Symbol::visitSymbol(Symbol *symbol, SymbolVisitor *visitor)
{
    if (! symbol)
        return;

    symbol->visitSymbol(visitor);
}

unsigned Symbol::sourceLocation() const
{ return _sourceLocation; }

150 151 152
bool Symbol::isGenerated() const
{ return _isGenerated; }

153 154 155 156 157 158
bool Symbol::isDeprecated() const
{ return _isDeprecated; }

void Symbol::setDeprecated(bool isDeprecated)
{ _isDeprecated = isDeprecated; }

159 160 161 162 163 164
bool Symbol::isUnavailable() const
{ return _isUnavailable; }

void Symbol::setUnavailable(bool isUnavailable)
{ _isUnavailable = isUnavailable; }

165
void Symbol::setSourceLocation(unsigned sourceLocation, TranslationUnit *translationUnit)
166 167 168
{
    _sourceLocation = sourceLocation;

169
    if (translationUnit) {
170
        const Token &tk = translationUnit->tokenAt(sourceLocation);
171
        _isGenerated = tk.f.generated;
172 173 174 175 176 177
        translationUnit->getPosition(tk.offset, &_line, &_column, &_fileId);
    } else {
        _isGenerated = false;
        _line = 0;
        _column = 0;
        _fileId = 0;
178
    }
179 180
}

con's avatar
con committed
181 182
unsigned Symbol::line() const
{
183
    return _line;
con's avatar
con committed
184 185 186 187
}

unsigned Symbol::column() const
{
188
    return _column;
con's avatar
con committed
189 190
}

Roberto Raggi's avatar
Roberto Raggi committed
191
const StringLiteral *Symbol::fileId() const
con's avatar
con committed
192
{
193
    return _fileId;
con's avatar
con committed
194 195 196 197 198 199 200 201
}

const char *Symbol::fileName() const
{ return fileId()->chars(); }

unsigned Symbol::fileNameLength() const
{ return fileId()->size(); }

Roberto Raggi's avatar
Roberto Raggi committed
202
const Name *Symbol::identity() const
con's avatar
con committed
203
{
204 205 206 207 208 209 210
    if (! _name)
        return 0;

    else if (const QualifiedNameId *q = _name->asQualifiedNameId())
        return q->name();

    return _name;
con's avatar
con committed
211 212
}

Roberto Raggi's avatar
Roberto Raggi committed
213
const Name *Symbol::name() const
con's avatar
con committed
214 215
{ return _name; }

Roberto Raggi's avatar
Roberto Raggi committed
216
void Symbol::setName(const Name *name)
con's avatar
con committed
217 218 219 220 221 222 223
{
    _name = name;

    if (! _name)
        _hashCode = 0;
    else {
        HashCode hh;
224
        _hashCode = hh(identity());
con's avatar
con committed
225 226 227
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
228
const Identifier *Symbol::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
229 230 231 232 233 234 235
{
    if (_name)
        return _name->identifier();

    return 0;
}

con's avatar
con committed
236 237 238 239 240 241 242 243 244
Scope *Symbol::scope() const
{ return _scope; }

void Symbol::setScope(Scope *scope)
{
    assert(! _scope);
    _scope = scope;
}

Roberto Raggi's avatar
Roberto Raggi committed
245
Namespace *Symbol::enclosingNamespace() const
246
{
Roberto Raggi's avatar
Roberto Raggi committed
247 248 249 250 251
    for (Scope *s = _scope; s; s = s->scope()) {
        if (Namespace *ns = s->asNamespace())
            return ns;
    }
    return 0;
252 253
}

254 255 256 257 258 259 260 261 262
Template *Symbol::enclosingTemplate() const
{
    for (Scope *s = _scope; s; s = s->scope()) {
        if (Template *templ = s->asTemplate())
            return templ;
    }
    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
263
Class *Symbol::enclosingClass() const
264
{
Roberto Raggi's avatar
Roberto Raggi committed
265 266 267 268 269
    for (Scope *s = _scope; s; s = s->scope()) {
        if (Class *klass = s->asClass())
            return klass;
    }
    return 0;
270 271
}

Roberto Raggi's avatar
Roberto Raggi committed
272
Enum *Symbol::enclosingEnum() const
273
{
Roberto Raggi's avatar
Roberto Raggi committed
274 275 276 277 278
    for (Scope *s = _scope; s; s = s->scope()) {
        if (Enum *e = s->asEnum())
            return e;
    }
    return 0;
279 280
}

Roberto Raggi's avatar
Roberto Raggi committed
281
Function *Symbol::enclosingFunction() const
282
{
Roberto Raggi's avatar
Roberto Raggi committed
283 284 285 286 287
    for (Scope *s = _scope; s; s = s->scope()) {
        if (Function *fun = s->asFunction())
            return fun;
    }
    return 0;
288 289
}

Roberto Raggi's avatar
Roberto Raggi committed
290
Block *Symbol::enclosingBlock() const
291
{
Roberto Raggi's avatar
Roberto Raggi committed
292 293 294 295 296
    for (Scope *s = _scope; s; s = s->scope()) {
        if (Block *block = s->asBlock())
            return block;
    }
    return 0;
297 298
}

con's avatar
con committed
299 300 301 302 303 304 305 306 307 308 309 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 337 338 339 340 341 342 343 344 345 346
unsigned Symbol::index() const
{ return _index; }

Symbol *Symbol::next() const
{ return _next; }

unsigned Symbol::hashCode() const
{ return _hashCode; }

int Symbol::storage() const
{ return _storage; }

void Symbol::setStorage(int storage)
{ _storage = storage; }

int Symbol::visibility() const
{ return _visibility; }

void Symbol::setVisibility(int visibility)
{ _visibility = visibility; }

bool Symbol::isFriend() const
{ return _storage == Friend; }

bool Symbol::isRegister() const
{ return _storage == Register; }

bool Symbol::isStatic() const
{ return _storage == Static; }

bool Symbol::isExtern() const
{ return _storage == Extern; }

bool Symbol::isMutable() const
{ return _storage == Mutable; }

bool Symbol::isTypedef() const
{ return _storage == Typedef; }

bool Symbol::isPublic() const
{ return _visibility == Public; }

bool Symbol::isProtected() const
{ return _visibility == Protected; }

bool Symbol::isPrivate() const
{ return _visibility == Private; }

347
bool Symbol::isScope() const
Roberto Raggi's avatar
Roberto Raggi committed
348
{ return asScope() != 0; }
con's avatar
con committed
349 350

bool Symbol::isEnum() const
351
{ return asEnum()  != 0; }
con's avatar
con committed
352 353

bool Symbol::isFunction() const
354
{ return asFunction() != 0; }
con's avatar
con committed
355 356

bool Symbol::isNamespace() const
357
{ return asNamespace() != 0; }
con's avatar
con committed
358

359
bool Symbol::isTemplate() const
Roberto Raggi's avatar
Roberto Raggi committed
360
{ return asTemplate() != 0; }
361

con's avatar
con committed
362
bool Symbol::isClass() const
363
{ return asClass() != 0; }
con's avatar
con committed
364

365 366 367
bool Symbol::isForwardClassDeclaration() const
{ return asForwardClassDeclaration() != 0; }

con's avatar
con committed
368
bool Symbol::isBlock() const
369
{ return asBlock() != 0; }
con's avatar
con committed
370 371

bool Symbol::isUsingNamespaceDirective() const
372
{ return asUsingNamespaceDirective() != 0; }
con's avatar
con committed
373 374

bool Symbol::isUsingDeclaration() const
375
{ return asUsingDeclaration() != 0; }
con's avatar
con committed
376 377

bool Symbol::isDeclaration() const
378
{ return asDeclaration() != 0; }
con's avatar
con committed
379 380

bool Symbol::isArgument() const
381
{ return asArgument() != 0; }
con's avatar
con committed
382

Roberto Raggi's avatar
Roberto Raggi committed
383 384 385
bool Symbol::isTypenameArgument() const
{ return asTypenameArgument() != 0; }

con's avatar
con committed
386
bool Symbol::isBaseClass() const
387
{ return asBaseClass() != 0; }
con's avatar
con committed
388

389 390 391 392 393 394
bool Symbol::isObjCBaseClass() const
{ return asObjCBaseClass() != 0; }

bool Symbol::isObjCBaseProtocol() const
{ return asObjCBaseProtocol() != 0; }

395 396 397 398 399 400 401 402 403 404 405 406
bool Symbol::isObjCClass() const
{ return asObjCClass() != 0; }

bool Symbol::isObjCForwardClassDeclaration() const
{ return asObjCForwardClassDeclaration() != 0; }

bool Symbol::isObjCProtocol() const
{ return asObjCProtocol() != 0; }

bool Symbol::isObjCForwardProtocolDeclaration() const
{ return asObjCForwardProtocolDeclaration() != 0; }

407 408 409
bool Symbol::isObjCMethod() const
{ return asObjCMethod() != 0; }

410 411
bool Symbol::isObjCPropertyDeclaration() const
{ return asObjCPropertyDeclaration() != 0; }
412 413 414 415 416 417 418 419 420 421 422

void Symbol::copy(Symbol *other)
{
    _sourceLocation = other->_sourceLocation;
    _name = other->_name;
    _hashCode = other->_hashCode;
    _storage = other->_storage;
    _visibility = other->_visibility;
    _scope = other->_scope;
    _index = other->_index;
    _next = other->_next;
423 424 425
    _fileId = other->_fileId;
    _line = other->_line;
    _column = other->_column;
426 427 428 429

    _isGenerated = other->_isGenerated;
    _isDeprecated = other->_isDeprecated;
}