Names.cpp 6.9 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 <cstring>
Roberto Raggi's avatar
Roberto Raggi committed
25
#include <cassert>
con's avatar
con committed
26
27
#include <algorithm>

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

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

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

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

    return 0;
}

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

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

bool QualifiedNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
52
53
54
55
56
57
    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;
                }
58
            }
con's avatar
con committed
59
60
        }
    }
61
62

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

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

DestructorNameId::~DestructorNameId()
{ }

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

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

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

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

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

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

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

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

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

bool TemplateNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
111
112
113
114
115
116
117
118
119
    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
120
            return false;
Roberto Raggi's avatar
Roberto Raggi committed
121
122
123
124
125
126
        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
127
128
129
130
    }
    return true;
}

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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;
}

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

OperatorNameId::~OperatorNameId()
{ }

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

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

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

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

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

ConversionNameId::~ConversionNameId()
{ }

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

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

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

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

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

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

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

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

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

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

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

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