Scope.cpp 8.24 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 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
// 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 "Scope.h"
#include "Symbols.h"
#include "Names.h"
#include "Literals.h"
#include <cassert>
#include <cstring>
55

Roberto Raggi's avatar
Roberto Raggi committed
56
using namespace CPlusPlus;
con's avatar
con committed
57
58
59
60
61
62
63

Scope::Scope(ScopedSymbol *owner)
    : _owner(owner),
      _symbols(0),
      _allocatedSymbols(0),
      _symbolCount(-1),
      _hash(0),
Roberto Raggi's avatar
Roberto Raggi committed
64
      _hashSize(0)
con's avatar
con committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{ }

Scope::~Scope()
{
    if (_symbols)
        free(_symbols);
    if (_hash)
        free(_hash);
}

ScopedSymbol *Scope::owner() const
{ return _owner; }

void Scope::setOwner(ScopedSymbol *owner)
{ _owner = owner; }

Scope *Scope::enclosingScope() const
{
    if (! _owner)
        return 0;

    return _owner->scope();
}

Scope *Scope::enclosingNamespaceScope() const
{
    Scope *scope = enclosingScope();
    for (; scope; scope = scope->enclosingScope()) {
        if (scope->owner()->isNamespace())
            break;
    }
    return scope;
}

Scope *Scope::enclosingClassScope() const
{
    Scope *scope = enclosingScope();
    for (; scope; scope = scope->enclosingScope()) {
        if (scope->owner()->isClass())
            break;
    }
    return scope;
}

Scope *Scope::enclosingEnumScope() const
{
    Scope *scope = enclosingScope();
    for (; scope; scope = scope->enclosingScope()) {
        if (scope->owner()->isEnum())
            break;
    }
    return scope;
}

Scope *Scope::enclosingFunctionScope() const
{
    Scope *scope = enclosingScope();
    for (; scope; scope = scope->enclosingScope()) {
        if (scope->owner()->isFunction())
            break;
    }
    return scope;
}

Scope *Scope::enclosingBlockScope() const
{
    Scope *scope = enclosingScope();
    for (; scope; scope = scope->enclosingScope()) {
        if (scope->owner()->isBlock())
            break;
    }
    return scope;
}

bool Scope::isNamespaceScope() const
140
141
142
143
144
{
    if (_owner)
        return _owner->isNamespace();
    return false;
}
con's avatar
con committed
145
146

bool Scope::isClassScope() const
147
148
149
150
151
{
    if (_owner)
        return _owner->isClass();
    return false;
}
con's avatar
con committed
152
153

bool Scope::isEnumScope() const
154
155
156
157
158
{
    if (_owner)
        return _owner->isEnum();
    return false;
}
con's avatar
con committed
159
160

bool Scope::isBlockScope() const
161
162
163
164
165
{
    if (_owner)
        return _owner->isBlock();
    return false;
}
con's avatar
con committed
166
167
168

bool Scope::isPrototypeScope() const
{
169
170
    Function *f = 0;
    if (_owner && 0 != (f = _owner->asFunction()))
con's avatar
con committed
171
172
173
174
        return f->arguments() == this;
    return false;
}

175
176
177
178
179
180
181
bool Scope::isObjCClassScope() const
{
    if (_owner)
        return _owner->isObjCClass();
    return false;
}

con's avatar
con committed
182
183
bool Scope::isFunctionScope() const
{
184
185
    Function *f = 0;
    if (_owner && 0 != (f = _owner->asFunction()))
con's avatar
con committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
        return f->arguments() != this;
    return false;
}

void Scope::enterSymbol(Symbol *symbol)
{
    if (++_symbolCount == _allocatedSymbols) {
        _allocatedSymbols <<= 1;
        if (! _allocatedSymbols)
            _allocatedSymbols = DefaultInitialSize;

        _symbols = reinterpret_cast<Symbol **>(realloc(_symbols, sizeof(Symbol *) * _allocatedSymbols));
    }

    assert(! symbol->_scope || symbol->scope() == this);
    symbol->_index = _symbolCount;
    symbol->_scope = this;
    _symbols[_symbolCount] = symbol;

    if (_symbolCount >= _hashSize * 0.6)
        rehash();
    else {
        const unsigned h = hashValue(symbol);
        symbol->_next = _hash[h];
        _hash[h] = symbol;
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
214
Symbol *Scope::lookat(const Name *name) const
215
216
217
218
{
    if (! name)
        return 0;

Roberto Raggi's avatar
Roberto Raggi committed
219
    else if (const OperatorNameId *opId = name->asOperatorNameId())
220
221
        return lookat(opId->kind());

Roberto Raggi's avatar
Roberto Raggi committed
222
    else if (const Identifier *id = name->identifier())
223
224
225
226
227
228
        return lookat(id);

    else
        return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
229
Symbol *Scope::lookat(const Identifier *id) const
con's avatar
con committed
230
{
231
    if (! _hash || ! id)
con's avatar
con committed
232
233
234
235
236
        return 0;

    const unsigned h = id->hashCode() % _hashSize;
    Symbol *symbol = _hash[h];
    for (; symbol; symbol = symbol->_next) {
Roberto Raggi's avatar
Roberto Raggi committed
237
        const Name *identity = symbol->identity();
238
239
        if (! identity) {
            continue;
Roberto Raggi's avatar
Roberto Raggi committed
240
        } else if (const NameId *nameId = identity->asNameId()) {
con's avatar
con committed
241
242
            if (nameId->identifier()->isEqualTo(id))
                break;
Roberto Raggi's avatar
Roberto Raggi committed
243
        } else if (const TemplateNameId *t = identity->asTemplateNameId()) {
con's avatar
con committed
244
245
            if (t->identifier()->isEqualTo(id))
                break;
Roberto Raggi's avatar
Roberto Raggi committed
246
        } else if (const DestructorNameId *d = identity->asDestructorNameId()) {
con's avatar
con committed
247
248
249
            if (d->identifier()->isEqualTo(id))
                break;
        } else if (identity->isQualifiedNameId()) {
Roberto Raggi's avatar
Roberto Raggi committed
250
251
            return 0;
        } else if (const SelectorNameId *selectorNameId = identity->asSelectorNameId()) {
252
253
            if (selectorNameId->identifier()->isEqualTo(id))
                break;
con's avatar
con committed
254
255
256
257
258
259
260
261
262
263
264
265
266
        }
    }
    return symbol;
}

Symbol *Scope::lookat(int operatorId) const
{
    if (! _hash)
        return 0;

    const unsigned h = operatorId % _hashSize;
    Symbol *symbol = _hash[h];
    for (; symbol; symbol = symbol->_next) {
Roberto Raggi's avatar
Roberto Raggi committed
267
268
        const Name *identity = symbol->identity();
        if (const OperatorNameId *op = identity->asOperatorNameId()) {
con's avatar
con committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
            if (op->kind() == operatorId)
                break;
        }
    }
    return symbol;
}

void Scope::rehash()
{
    _hashSize <<= 1;

    if (! _hashSize)
        _hashSize = DefaultInitialSize;

    _hash = reinterpret_cast<Symbol **>(realloc(_hash, sizeof(Symbol *) * _hashSize));
Roberto Raggi's avatar
Roberto Raggi committed
284
    std::memset(_hash, 0, sizeof(Symbol *) * _hashSize);
con's avatar
con committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

    for (int index = 0; index < _symbolCount + 1; ++index) {
        Symbol *symbol = _symbols[index];
        const unsigned h = hashValue(symbol);
        symbol->_next = _hash[h];
        _hash[h] = symbol;
    }
}

unsigned Scope::hashValue(Symbol *symbol) const
{
    if (! symbol)
        return 0;

    return symbol->hashCode() % _hashSize;
}

bool Scope::isEmpty() const
{ return _symbolCount == -1; }

unsigned Scope::symbolCount() const
{ return _symbolCount + 1; }

Symbol *Scope::symbolAt(unsigned index) const
{
    if (! _symbols)
        return 0;
    return _symbols[index];
}

Scope::iterator Scope::firstSymbol() const
{ return _symbols; }

Scope::iterator Scope::lastSymbol() const
{ return _symbols + _symbolCount + 1; }

Roberto Raggi's avatar
Roberto Raggi committed
321