Names.cpp 7.79 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
// 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 "Names.h"
#include "NameVisitor.h"
#include "Literals.h"
#include <cstring>
Roberto Raggi's avatar
Roberto Raggi committed
53
#include <cassert>
con's avatar
con committed
54
55
#include <algorithm>

Roberto Raggi's avatar
Roberto Raggi committed
56
using namespace CPlusPlus;
con's avatar
con committed
57
58

QualifiedNameId::~QualifiedNameId()
Roberto Raggi's avatar
Roberto Raggi committed
59
{ }
con's avatar
con committed
60

Roberto Raggi's avatar
Roberto Raggi committed
61
void QualifiedNameId::accept0(NameVisitor *visitor) const
con's avatar
con committed
62
63
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
64
const Identifier *QualifiedNameId::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
65
{
Roberto Raggi's avatar
Roberto Raggi committed
66
    if (const Name *u = unqualifiedNameId())
Roberto Raggi's avatar
Roberto Raggi committed
67
68
69
70
71
        return u->identifier();

    return 0;
}

con's avatar
con committed
72
unsigned QualifiedNameId::nameCount() const
Roberto Raggi's avatar
Roberto Raggi committed
73
{ return _names.size(); }
con's avatar
con committed
74

Roberto Raggi's avatar
Roberto Raggi committed
75
const Name *QualifiedNameId::nameAt(unsigned index) const
con's avatar
con committed
76
77
78
79
80
{ return _names[index]; }

bool QualifiedNameId::isGlobal() const
{ return _isGlobal; }

Roberto Raggi's avatar
Roberto Raggi committed
81
const Name *QualifiedNameId::unqualifiedNameId() const
con's avatar
con committed
82
{
Roberto Raggi's avatar
Roberto Raggi committed
83
    if (_names.empty())
con's avatar
con committed
84
85
        return 0;

Roberto Raggi's avatar
Roberto Raggi committed
86
    return _names.back();
con's avatar
con committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
}

bool QualifiedNameId::isEqualTo(const Name *other) const
{
    const QualifiedNameId *q = other->asQualifiedNameId();
    if (! q)
        return false;
    else if (isGlobal() != q->isGlobal())
        return false;
    else {
        const unsigned count = nameCount();
        if (count != q->nameCount())
            return false;
        for (unsigned i = 0; i < count; ++i) {
Roberto Raggi's avatar
Roberto Raggi committed
101
102
            const Name *l = nameAt(i);
            const Name *r = q->nameAt(i);
con's avatar
con committed
103
104
105
106
107
108
109
            if (! l->isEqualTo(r))
                return false;
        }
    }
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
110
NameId::NameId(const Identifier *identifier)
con's avatar
con committed
111
112
113
114
115
116
    : _identifier(identifier)
{ }

NameId::~NameId()
{ }

Roberto Raggi's avatar
Roberto Raggi committed
117
void NameId::accept0(NameVisitor *visitor) const
con's avatar
con committed
118
119
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
120
const Identifier *NameId::identifier() const
con's avatar
con committed
121
122
123
124
125
126
127
{ return _identifier; }

bool NameId::isEqualTo(const Name *other) const
{
    const NameId *nameId = other->asNameId();
    if (! nameId)
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
128
129
    const Identifier *l = identifier();
    const Identifier *r = nameId->identifier();
con's avatar
con committed
130
131
132
    return l->isEqualTo(r);
}

Roberto Raggi's avatar
Roberto Raggi committed
133
DestructorNameId::DestructorNameId(const Identifier *identifier)
con's avatar
con committed
134
135
136
137
138
139
    : _identifier(identifier)
{ }

DestructorNameId::~DestructorNameId()
{ }

Roberto Raggi's avatar
Roberto Raggi committed
140
void DestructorNameId::accept0(NameVisitor *visitor) const
con's avatar
con committed
141
142
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
143
const Identifier *DestructorNameId::identifier() const
con's avatar
con committed
144
145
146
147
148
149
150
{ return _identifier; }

bool DestructorNameId::isEqualTo(const Name *other) const
{
    const DestructorNameId *d = other->asDestructorNameId();
    if (! d)
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
151
152
    const Identifier *l = identifier();
    const Identifier *r = d->identifier();
con's avatar
con committed
153
154
155
156
    return l->isEqualTo(r);
}

TemplateNameId::~TemplateNameId()
Roberto Raggi's avatar
Roberto Raggi committed
157
{ }
con's avatar
con committed
158

Roberto Raggi's avatar
Roberto Raggi committed
159
void TemplateNameId::accept0(NameVisitor *visitor) const
con's avatar
con committed
160
161
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
162
const Identifier *TemplateNameId::identifier() const
con's avatar
con committed
163
164
165
{ return _identifier; }

unsigned TemplateNameId::templateArgumentCount() const
Roberto Raggi's avatar
Roberto Raggi committed
166
{ return _templateArguments.size(); }
con's avatar
con committed
167
168
169
170
171
172

const FullySpecifiedType &TemplateNameId::templateArgumentAt(unsigned index) const
{ return _templateArguments[index]; }

bool TemplateNameId::isEqualTo(const Name *other) const
{
173
174
    if (! other)
        return false;
con's avatar
con committed
175
176
177
    const TemplateNameId *t = other->asTemplateNameId();
    if (! t)
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
178
179
    const Identifier *l = identifier();
    const Identifier *r = t->identifier();
con's avatar
con committed
180
181
    if (! l->isEqualTo(r))
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
182
    if (templateArgumentCount() != t->templateArgumentCount())
con's avatar
con committed
183
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
184
    for (unsigned i = 0; i < templateArgumentCount(); ++i) {
con's avatar
con committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
        const FullySpecifiedType &l = _templateArguments[i];
        const FullySpecifiedType &r = t->_templateArguments[i];
        if (! l.isEqualTo(r))
            return false;
    }
    return true;
}

OperatorNameId::OperatorNameId(int kind)
    : _kind(kind)
{ }

OperatorNameId::~OperatorNameId()
{ }

Roberto Raggi's avatar
Roberto Raggi committed
200
void OperatorNameId::accept0(NameVisitor *visitor) const
con's avatar
con committed
201
202
203
204
205
{ visitor->visit(this); }

int OperatorNameId::kind() const
{ return _kind; }

Roberto Raggi's avatar
Roberto Raggi committed
206
const Identifier *OperatorNameId::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
207
208
{ return 0; }

con's avatar
con committed
209
210
211
212
213
214
215
216
bool OperatorNameId::isEqualTo(const Name *other) const
{
    const OperatorNameId *o = other->asOperatorNameId();
    if (! o)
        return false;
    return _kind == o->kind();
}

217
ConversionNameId::ConversionNameId(const FullySpecifiedType &type)
con's avatar
con committed
218
219
220
221
222
223
    : _type(type)
{ }

ConversionNameId::~ConversionNameId()
{ }

Roberto Raggi's avatar
Roberto Raggi committed
224
void ConversionNameId::accept0(NameVisitor *visitor) const
con's avatar
con committed
225
226
227
228
229
{ visitor->visit(this); }

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

Roberto Raggi's avatar
Roberto Raggi committed
230
const Identifier *ConversionNameId::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
231
232
{ return 0; }

con's avatar
con committed
233
234
235
236
237
238
239
240
bool ConversionNameId::isEqualTo(const Name *other) const
{
    const ConversionNameId *c = other->asConversionNameId();
    if (! c)
        return false;
    return _type.isEqualTo(c->type());
}

241
SelectorNameId::~SelectorNameId()
Roberto Raggi's avatar
Roberto Raggi committed
242
{ }
243

Roberto Raggi's avatar
Roberto Raggi committed
244
void SelectorNameId::accept0(NameVisitor *visitor) const
245
246
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
247
const Identifier *SelectorNameId::identifier() const
248
{
Roberto Raggi's avatar
Roberto Raggi committed
249
    if (_names.empty())
250
251
        return 0;

252
253
254
255
    return nameAt(0)->identifier();
}

unsigned SelectorNameId::nameCount() const
Roberto Raggi's avatar
Roberto Raggi committed
256
{ return _names.size(); }
257

Roberto Raggi's avatar
Roberto Raggi committed
258
const Name *SelectorNameId::nameAt(unsigned index) const
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
{ return _names[index]; }

bool SelectorNameId::hasArguments() const
{ return _hasArguments; }

bool SelectorNameId::isEqualTo(const Name *other) const
{
    const SelectorNameId *q = other->asSelectorNameId();
    if (! q)
        return false;
    else if (hasArguments() != q->hasArguments())
        return false;
    else {
        const unsigned count = nameCount();
        if (count != q->nameCount())
            return false;
        for (unsigned i = 0; i < count; ++i) {
Roberto Raggi's avatar
Roberto Raggi committed
276
277
            const Name *l = nameAt(i);
            const Name *r = q->nameAt(i);
278
279
280
281
282
283
            if (! l->isEqualTo(r))
                return false;
        }
    }
    return true;
}
con's avatar
con committed
284

Roberto Raggi's avatar
Roberto Raggi committed
285