Names.cpp 6.06 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
OperatorNameId::OperatorNameId(Kind kind)
con's avatar
con committed
132
133
134
135
136
137
    : _kind(kind)
{ }

OperatorNameId::~OperatorNameId()
{ }

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

141
OperatorNameId::Kind OperatorNameId::kind() const
con's avatar
con committed
142
143
{ return _kind; }

Roberto Raggi's avatar
Roberto Raggi committed
144
const Identifier *OperatorNameId::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
145
146
{ return 0; }

con's avatar
con committed
147
148
bool OperatorNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
149
150
151
152
153
154
155
    if (other) {
        const OperatorNameId *o = other->asOperatorNameId();
        if (! o)
            return false;
        return _kind == o->kind();
    }
    return false;
con's avatar
con committed
156
157
}

158
ConversionNameId::ConversionNameId(const FullySpecifiedType &type)
con's avatar
con committed
159
160
161
162
163
164
    : _type(type)
{ }

ConversionNameId::~ConversionNameId()
{ }

Roberto Raggi's avatar
Roberto Raggi committed
165
void ConversionNameId::accept0(NameVisitor *visitor) const
con's avatar
con committed
166
167
168
169
170
{ visitor->visit(this); }

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

Roberto Raggi's avatar
Roberto Raggi committed
171
const Identifier *ConversionNameId::identifier() const
Roberto Raggi's avatar
Roberto Raggi committed
172
173
{ return 0; }

con's avatar
con committed
174
175
bool ConversionNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
176
177
178
179
180
181
182
    if (other) {
        const ConversionNameId *c = other->asConversionNameId();
        if (! c)
            return false;
        return _type.isEqualTo(c->type());
    }
    return false;
con's avatar
con committed
183
184
}

185
SelectorNameId::~SelectorNameId()
Roberto Raggi's avatar
Roberto Raggi committed
186
{ }
187

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

Roberto Raggi's avatar
Roberto Raggi committed
191
const Identifier *SelectorNameId::identifier() const
192
{
Roberto Raggi's avatar
Roberto Raggi committed
193
    if (_names.empty())
194
195
        return 0;

196
197
198
199
    return nameAt(0)->identifier();
}

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

Roberto Raggi's avatar
Roberto Raggi committed
202
const Name *SelectorNameId::nameAt(unsigned index) const
203
204
205
206
207
208
209
{ return _names[index]; }

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

bool SelectorNameId::isEqualTo(const Name *other) const
{
Roberto Raggi's avatar
Roberto Raggi committed
210
211
212
213
214
    if (other) {
        const SelectorNameId *q = other->asSelectorNameId();
        if (! q)
            return false;
        else if (hasArguments() != q->hasArguments())
215
            return false;
Roberto Raggi's avatar
Roberto Raggi committed
216
217
218
        else {
            const unsigned count = nameCount();
            if (count != q->nameCount())
219
                return false;
Roberto Raggi's avatar
Roberto Raggi committed
220
221
222
223
224
225
            for (unsigned i = 0; i < count; ++i) {
                const Name *l = nameAt(i);
                const Name *r = q->nameAt(i);
                if (! l->isEqualTo(r))
                    return false;
            }
226
227
228
229
        }
    }
    return true;
}
con's avatar
con committed
230