Symbol.cpp 12.2 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)
con's avatar
con committed
104
105
    { _value = operator()(name->unqualifiedNameId()); }

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
113
114
115
116
117
118
119
120
121
122
private:
    unsigned _value;
};

class Symbol::IdentityForName: protected NameVisitor
{
public:
    IdentityForName()
        : _identity(0)
    { }

    virtual ~IdentityForName()
    { }

Roberto Raggi's avatar
Roberto Raggi committed
123
    const Name *operator()(const Name *name)
con's avatar
con committed
124
    {
Roberto Raggi's avatar
Roberto Raggi committed
125
        const Name *previousIdentity = switchIdentity(0);
con's avatar
con committed
126
127
128
129
130
        accept(name);
        return switchIdentity(previousIdentity);
    }

protected:
Roberto Raggi's avatar
Roberto Raggi committed
131
    const Name *switchIdentity(const Name *identity)
con's avatar
con committed
132
    {
Roberto Raggi's avatar
Roberto Raggi committed
133
        const Name *previousIdentity = _identity;
con's avatar
con committed
134
135
136
137
        _identity = identity;
        return previousIdentity;
    }

Roberto Raggi's avatar
Roberto Raggi committed
138
    virtual void visit(const NameId *name)
con's avatar
con committed
139
140
    { _identity = name; }

Roberto Raggi's avatar
Roberto Raggi committed
141
    virtual void visit(const TemplateNameId *name)
con's avatar
con committed
142
143
    { _identity = name; }

Roberto Raggi's avatar
Roberto Raggi committed
144
    virtual void visit(const DestructorNameId *name)
con's avatar
con committed
145
146
    { _identity = name; }

Roberto Raggi's avatar
Roberto Raggi committed
147
    virtual void visit(const OperatorNameId *name)
con's avatar
con committed
148
149
    { _identity = name; }

Roberto Raggi's avatar
Roberto Raggi committed
150
    virtual void visit(const ConversionNameId *name)
con's avatar
con committed
151
152
    { _identity = name; }

Roberto Raggi's avatar
Roberto Raggi committed
153
    virtual void visit(const QualifiedNameId *name)
con's avatar
con committed
154
155
    { _identity = name->unqualifiedNameId(); }

Roberto Raggi's avatar
Roberto Raggi committed
156
    virtual void visit(const SelectorNameId *name)
157
158
    { _identity = name; }

con's avatar
con committed
159
private:
Roberto Raggi's avatar
Roberto Raggi committed
160
    const Name *_identity;
con's avatar
con committed
161
162
};

Roberto Raggi's avatar
Roberto Raggi committed
163
Symbol::Symbol(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
164
    : _translationUnit(translationUnit),
con's avatar
con committed
165
166
      _sourceLocation(sourceLocation),
      _sourceOffset(0),
167
168
      _startOffset(0),
      _endOffset(0),
con's avatar
con committed
169
170
171
172
173
174
      _name(0),
      _hashCode(0),
      _storage(Symbol::NoStorage),
      _visibility(Symbol::Public),
      _scope(0),
      _index(0),
175
176
      _next(0),
      _isGenerated(false)
con's avatar
con committed
177
{
178
    setSourceLocation(sourceLocation);
con's avatar
con committed
179
180
181
182
183
184
185
    setName(name);
}

Symbol::~Symbol()
{ }

Control *Symbol::control() const
186
187
188
189
190
191
{
    if (_translationUnit)
        return _translationUnit->control();

    return 0;
}
con's avatar
con committed
192
193

TranslationUnit *Symbol::translationUnit() const
194
{ return _translationUnit; }
con's avatar
con committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

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

unsigned Symbol::sourceOffset() const
{ return _sourceOffset; }

217
218
219
bool Symbol::isGenerated() const
{ return _isGenerated; }

220
221
222
223
224
225
bool Symbol::isDeprecated() const
{ return _isDeprecated; }

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

226
227
228
229
void Symbol::setSourceLocation(unsigned sourceLocation)
{
    _sourceLocation = sourceLocation;

230
231
    if (! _sourceLocation) {
        _isGenerated = false;
232
        _sourceOffset = 0;
233
234
235
236
237
    } else {
        TranslationUnit *unit = translationUnit();

        const Token &tk = unit->tokenAt(sourceLocation);

238
        _isGenerated = tk.f.generated;
239
240
        _sourceOffset = tk.offset;
    }
241
242
}

con's avatar
con committed
243
244
245
unsigned Symbol::line() const
{
    unsigned line = 0, column = 0;
Roberto Raggi's avatar
Roberto Raggi committed
246
    const StringLiteral *fileId = 0;
con's avatar
con committed
247
248
249
250
251
252
253
    translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId);
    return line;
}

unsigned Symbol::column() const
{
    unsigned line = 0, column = 0;
Roberto Raggi's avatar
Roberto Raggi committed
254
    const StringLiteral *fileId = 0;
con's avatar
con committed
255
256
257
258
    translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId);
    return column;
}

Roberto Raggi's avatar
Roberto Raggi committed
259
const StringLiteral *Symbol::fileId() const
con's avatar
con committed
260
261
{
    unsigned line = 0, column = 0;
Roberto Raggi's avatar
Roberto Raggi committed
262
    const StringLiteral *fileId = 0;
con's avatar
con committed
263
264
265
266
    translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId);
    return fileId;
}

Roberto Raggi's avatar
Roberto Raggi committed
267
void Symbol::getPosition(unsigned *line, unsigned *column, const StringLiteral **fileId) const
268
269
{ translationUnit()->getPosition(_sourceOffset, line, column, fileId); }

Roberto Raggi's avatar
Roberto Raggi committed
270
void Symbol::getStartPosition(unsigned *line, unsigned *column, const StringLiteral **fileId) const
271
272
{ translationUnit()->getPosition(_startOffset, line, column, fileId); }

Roberto Raggi's avatar
Roberto Raggi committed
273
void Symbol::getEndPosition(unsigned *line, unsigned *column, const StringLiteral **fileId) const
274
275
{ translationUnit()->getPosition(_endOffset, line, column, fileId); }

con's avatar
con committed
276
277
278
279
280
281
const char *Symbol::fileName() const
{ return fileId()->chars(); }

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

282
283
284
285
286
287
288
289
290
291
292
293
unsigned Symbol::startOffset() const
{ return _startOffset; }

void Symbol::setStartOffset(unsigned offset)
{ _startOffset = offset; }

unsigned Symbol::endOffset() const
{ return _endOffset; }

void Symbol::setEndOffset(unsigned offset)
{ _endOffset = offset; }

Roberto Raggi's avatar
Roberto Raggi committed
294
const Name *Symbol::identity() const
con's avatar
con committed
295
296
297
298
299
{
    IdentityForName id;
    return id(_name);
}

Roberto Raggi's avatar
Roberto Raggi committed
300
const Name *Symbol::name() const
con's avatar
con committed
301
302
{ return _name; }

Roberto Raggi's avatar
Roberto Raggi committed
303
void Symbol::setName(const Name *name)
con's avatar
con committed
304
305
306
307
308
309
310
311
312
313
314
315
{
    _name = name;

    if (! _name)
        _hashCode = 0;
    else {
        IdentityForName identityForName;
        HashCode hh;
        _hashCode = hh(identityForName(_name));
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
316
const Identifier *Symbol::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
317
318
319
320
321
322
323
{
    if (_name)
        return _name->identifier();

    return 0;
}

con's avatar
con committed
324
325
326
327
328
329
330
331
332
Scope *Symbol::scope() const
{ return _scope; }

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

333
334
335
336
337
338
339
340
Symbol *Symbol::enclosingSymbol() const
{
    if (! _scope)
        return 0;

    return _scope->owner();
}

341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
Scope *Symbol::enclosingNamespaceScope() const
{
    if (! _scope)
        return 0;

    else if (_scope->isNamespaceScope())
        return _scope;

    return _scope->enclosingNamespaceScope();
}

Scope *Symbol::enclosingClassScope() const
{
    if (! _scope)
        return 0;

Roberto Raggi's avatar
typo    
Roberto Raggi committed
357
    else if (_scope->isClassScope())
358
359
        return _scope;

Roberto Raggi's avatar
typo    
Roberto Raggi committed
360
    return _scope->enclosingClassScope();
361
362
363
364
365
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
}

Scope *Symbol::enclosingEnumScope() const
{
    if (! _scope)
        return 0;

    else if (_scope->isEnumScope())
        return _scope;

    return _scope->enclosingEnumScope();
}

Scope *Symbol::enclosingFunctionScope() const
{
    if (! _scope)
        return 0;

    else if (_scope->isFunctionScope())
        return _scope;

    return _scope->enclosingFunctionScope();
}

Scope *Symbol::enclosingBlockScope() const
{
    if (! _scope)
        return 0;

    else if (_scope->isBlockScope())
        return _scope;

    return _scope->enclosingBlockScope();
}

con's avatar
con committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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; }

bool Symbol::isScopedSymbol() const
445
{ return asScopedSymbol() != 0; }
con's avatar
con committed
446
447

bool Symbol::isEnum() const
448
{ return asEnum()  != 0; }
con's avatar
con committed
449
450

bool Symbol::isFunction() const
451
{ return asFunction() != 0; }
con's avatar
con committed
452
453

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

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

459
460
461
bool Symbol::isForwardClassDeclaration() const
{ return asForwardClassDeclaration() != 0; }

con's avatar
con committed
462
bool Symbol::isBlock() const
463
{ return asBlock() != 0; }
con's avatar
con committed
464
465

bool Symbol::isUsingNamespaceDirective() const
466
{ return asUsingNamespaceDirective() != 0; }
con's avatar
con committed
467
468

bool Symbol::isUsingDeclaration() const
469
{ return asUsingDeclaration() != 0; }
con's avatar
con committed
470
471

bool Symbol::isDeclaration() const
472
{ return asDeclaration() != 0; }
con's avatar
con committed
473
474

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

Roberto Raggi's avatar
Roberto Raggi committed
477
478
479
bool Symbol::isTypenameArgument() const
{ return asTypenameArgument() != 0; }

con's avatar
con committed
480
bool Symbol::isBaseClass() const
481
{ return asBaseClass() != 0; }
con's avatar
con committed
482

483
484
485
486
487
488
bool Symbol::isObjCBaseClass() const
{ return asObjCBaseClass() != 0; }

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

489
490
491
492
493
494
495
496
497
498
499
500
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; }

501
502
503
bool Symbol::isObjCMethod() const
{ return asObjCMethod() != 0; }

504
505
bool Symbol::isObjCPropertyDeclaration() const
{ return asObjCPropertyDeclaration() != 0; }