Names.cpp 7.49 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 "Names.h"
#include "NameVisitor.h"
#include "Literals.h"
#include <algorithm>
25
#include <cstring>
con's avatar
con committed
26

Roberto Raggi's avatar
Roberto Raggi committed
27
using namespace CPlusPlus;
con's avatar
con committed
28
29

QualifiedNameId::~QualifiedNameId()
Roberto Raggi's avatar
Roberto Raggi committed
30
{ }
con's avatar
con committed
31

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

Roberto Raggi's avatar
Roberto Raggi committed
35
const Identifier *QualifiedNameId::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
36
{
37
    if (const Name *u = name())
Roberto Raggi's avatar
Roberto Raggi committed
38
39
40
41
42
        return u->identifier();

    return 0;
}

43
44
const Name *QualifiedNameId::base() const
{ return _base; }
con's avatar
con committed
45

46
47
const Name *QualifiedNameId::name() const
{ return _name; }
con's avatar
con committed
48
49
50

bool QualifiedNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
51
52
53
54
55
56
    if (other) {
        if (const QualifiedNameId *q = other->asQualifiedNameId()) {
            if (_base == q->_base || (_base && _base->isEqualTo(q->_base))) {
                if (_name == q->_name || (_name && _name->isEqualTo(q->_name))) {
                    return true;
                }
57
            }
con's avatar
con committed
58
59
        }
    }
60
61

    return false;
con's avatar
con committed
62
63
}

64
65
DestructorNameId::DestructorNameId(const Name *name)
    : _name(name)
con's avatar
con committed
66
67
68
69
70
{ }

DestructorNameId::~DestructorNameId()
{ }

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

74
75
76
const Name *DestructorNameId::name() const
{ return _name; }

Roberto Raggi's avatar
Roberto Raggi committed
77
const Identifier *DestructorNameId::identifier() const
78
{ return _name->identifier(); }
con's avatar
con committed
79
80
81

bool DestructorNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
82
83
84
85
    if (other) {
        const DestructorNameId *d = other->asDestructorNameId();
        if (! d)
            return false;
86
87
        const Name *l = name();
        const Name *r = d->name();
Roberto Raggi's avatar
Roberto Raggi committed
88
89
90
        return l->isEqualTo(r);
    }
    return false;
con's avatar
con committed
91
92
93
}

TemplateNameId::~TemplateNameId()
Roberto Raggi's avatar
Roberto Raggi committed
94
{ }
con's avatar
con committed
95

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

Roberto Raggi's avatar
Roberto Raggi committed
99
const Identifier *TemplateNameId::identifier() const
con's avatar
con committed
100
101
102
{ return _identifier; }

unsigned TemplateNameId::templateArgumentCount() const
Roberto Raggi's avatar
Roberto Raggi committed
103
{ return _templateArguments.size(); }
con's avatar
con committed
104
105
106
107
108
109

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

bool TemplateNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
110
111
112
113
114
115
116
117
118
    if (other) {
        const TemplateNameId *t = other->asTemplateNameId();
        if (! t)
            return false;
        const Identifier *l = identifier();
        const Identifier *r = t->identifier();
        if (! l->isEqualTo(r))
            return false;
        if (templateArgumentCount() != t->templateArgumentCount())
con's avatar
con committed
119
            return false;
Roberto Raggi's avatar
Roberto Raggi committed
120
121
122
123
124
125
        for (unsigned i = 0; i < templateArgumentCount(); ++i) {
            const FullySpecifiedType &l = _templateArguments[i];
            const FullySpecifiedType &r = t->_templateArguments[i];
            if (! l.isEqualTo(r))
                return false;
        }
con's avatar
con committed
126
127
128
129
    }
    return true;
}

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
bool TemplateNameId::Compare::operator()(const TemplateNameId *name,
                                         const TemplateNameId *other) const
{
    const Identifier *id = name->identifier();
    const Identifier *otherId = other->identifier();

    if (id == otherId) {
        // we have to differentiate TemplateNameId with respect to specialization or instantiation
        if (name->isSpecialization() == other->isSpecialization()) {
            return std::lexicographical_compare(name->firstTemplateArgument(),
                                                name->lastTemplateArgument(),
                                                other->firstTemplateArgument(),
                                                other->lastTemplateArgument());
        } else {
            return name->isSpecialization();
        }
    }

    return id < otherId;
}

151
OperatorNameId::OperatorNameId(Kind kind)
con's avatar
con committed
152
153
154
155
156
157
    : _kind(kind)
{ }

OperatorNameId::~OperatorNameId()
{ }

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

161
OperatorNameId::Kind OperatorNameId::kind() const
con's avatar
con committed
162
163
{ return _kind; }

Roberto Raggi's avatar
Roberto Raggi committed
164
const Identifier *OperatorNameId::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
165
166
{ return 0; }

con's avatar
con committed
167
168
bool OperatorNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
169
170
171
172
173
174
175
    if (other) {
        const OperatorNameId *o = other->asOperatorNameId();
        if (! o)
            return false;
        return _kind == o->kind();
    }
    return false;
con's avatar
con committed
176
177
}

178
ConversionNameId::ConversionNameId(const FullySpecifiedType &type)
con's avatar
con committed
179
180
181
182
183
184
    : _type(type)
{ }

ConversionNameId::~ConversionNameId()
{ }

Roberto Raggi's avatar
Roberto Raggi committed
185
void ConversionNameId::accept0(NameVisitor *visitor) const
con's avatar
con committed
186
187
188
189
190
{ visitor->visit(this); }

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

Roberto Raggi's avatar
Roberto Raggi committed
191
const Identifier *ConversionNameId::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
192
193
{ return 0; }

con's avatar
con committed
194
195
bool ConversionNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
196
197
198
199
200
201
202
    if (other) {
        const ConversionNameId *c = other->asConversionNameId();
        if (! c)
            return false;
        return _type.isEqualTo(c->type());
    }
    return false;
con's avatar
con committed
203
204
}

205
SelectorNameId::~SelectorNameId()
Roberto Raggi's avatar
Roberto Raggi committed
206
{ }
207

Roberto Raggi's avatar
Roberto Raggi committed
208
void SelectorNameId::accept0(NameVisitor *visitor) const
209
210
{ visitor->visit(this); }

Roberto Raggi's avatar
Roberto Raggi committed
211
const Identifier *SelectorNameId::identifier() const
212
{
Roberto Raggi's avatar
Roberto Raggi committed
213
    if (_names.empty())
214
215
        return 0;

216
217
218
219
    return nameAt(0)->identifier();
}

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

Roberto Raggi's avatar
Roberto Raggi committed
222
const Name *SelectorNameId::nameAt(unsigned index) const
223
224
225
226
227
228
229
{ return _names[index]; }

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

bool SelectorNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
230
231
232
233
234
    if (other) {
        const SelectorNameId *q = other->asSelectorNameId();
        if (! q)
            return false;
        else if (hasArguments() != q->hasArguments())
235
            return false;
Roberto Raggi's avatar
Roberto Raggi committed
236
237
238
        else {
            const unsigned count = nameCount();
            if (count != q->nameCount())
239
                return false;
Roberto Raggi's avatar
Roberto Raggi committed
240
241
242
243
244
245
            for (unsigned i = 0; i < count; ++i) {
                const Name *l = nameAt(i);
                const Name *r = q->nameAt(i);
                if (! l->isEqualTo(r))
                    return false;
            }
246
247
248
249
        }
    }
    return true;
}
con's avatar
con committed
250

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
AnonymousNameId::AnonymousNameId(unsigned classTokenIndex)
    : _classTokenIndex(classTokenIndex)
{ }

AnonymousNameId::~AnonymousNameId()
{ }

unsigned AnonymousNameId::classTokenIndex() const
{
    return _classTokenIndex;
}

void AnonymousNameId::accept0(NameVisitor *visitor) const
{ visitor->visit(this); }

const Identifier *AnonymousNameId::identifier() const
{ return 0; }

bool AnonymousNameId::isEqualTo(const Name *other) const
{
    if (other) {
        const AnonymousNameId *c = other->asAnonymousNameId();
        return (c && this->_classTokenIndex == c->_classTokenIndex);
    }
    return false;
}