forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Identifier.cpp
309 lines (257 loc) · 8.84 KB
/
Identifier.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
55
56
57
58
59
60
61
62
63
64
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//===--- Identifier.cpp - Uniqued Identifier ------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the Identifier interface.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTContext.h"
#include "swift/AST/Identifier.h"
#include "swift/Parse/Lexer.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ConvertUTF.h"
#include "clang/Basic/CharInfo.h"
using namespace swift;
constexpr const Identifier::Aligner DeclBaseName::SubscriptIdentifierData{};
constexpr const Identifier::Aligner DeclBaseName::ConstructorIdentifierData{};
constexpr const Identifier::Aligner DeclBaseName::DestructorIdentifierData{};
raw_ostream &llvm::operator<<(raw_ostream &OS, Identifier I) {
if (I.get() == nullptr)
return OS << "_";
return OS << I.get();
}
raw_ostream &llvm::operator<<(raw_ostream &OS, DeclBaseName D) {
return OS << D.userFacingName();
}
raw_ostream &llvm::operator<<(raw_ostream &OS, DeclName I) {
if (I.isSimpleName())
return OS << I.getBaseName();
OS << I.getBaseName() << "(";
for (auto c : I.getArgumentNames()) {
OS << c << ':';
}
OS << ")";
return OS;
}
void swift::simple_display(llvm::raw_ostream &out, DeclName name) {
out << "'" << name << "'";
}
raw_ostream &llvm::operator<<(raw_ostream &OS, DeclNameRef I) {
OS << I.getFullName();
return OS;
}
void swift::simple_display(llvm::raw_ostream &out, DeclNameRef name) {
out << "'" << name << "'";
}
raw_ostream &llvm::operator<<(raw_ostream &OS, swift::ObjCSelector S) {
unsigned n = S.getNumArgs();
if (n == 0) {
OS << S.getSelectorPieces()[0];
return OS;
}
for (auto piece : S.getSelectorPieces()) {
if (!piece.empty())
OS << piece;
OS << ":";
}
return OS;
}
bool Identifier::isOperatorSlow() const {
StringRef data = str();
auto *s = reinterpret_cast<llvm::UTF8 const *>(data.begin()),
*end = reinterpret_cast<llvm::UTF8 const *>(data.end());
llvm::UTF32 codePoint;
llvm::ConversionResult res =
llvm::convertUTF8Sequence(&s, end, &codePoint, llvm::strictConversion);
assert(res == llvm::conversionOK && "invalid UTF-8 in identifier?!");
(void)res;
return !empty() && isOperatorStartCodePoint(codePoint);
}
int Identifier::compare(Identifier other) const {
// Handle empty identifiers.
if (empty() || other.empty()) {
if (empty() != other.empty()) {
return other.empty() ? -1 : 1;
}
return 0;
}
return str().compare(other.str());
}
int DeclName::compare(DeclName other) const {
// Compare base names.
if (int result = getBaseName().compare(other.getBaseName()))
return result;
// Compare argument names.
auto argNames = getArgumentNames();
auto otherArgNames = other.getArgumentNames();
for (unsigned i = 0, n = std::min(argNames.size(), otherArgNames.size());
i != n; ++i) {
if (int result = argNames[i].compare(otherArgNames[i]))
return result;
}
if (argNames.size() == otherArgNames.size())
return 0;
return argNames.size() < otherArgNames.size() ? -1 : 1;
}
static bool equals(ArrayRef<Identifier> idents, ArrayRef<StringRef> strings) {
if (idents.size() != strings.size())
return false;
for (size_t i = 0, e = idents.size(); i != e; ++i) {
if (!idents[i].is(strings[i]))
return false;
}
return true;
}
bool DeclName::isCompoundName(DeclBaseName baseName,
ArrayRef<StringRef> argNames) const {
return (isCompoundName() &&
getBaseName() == baseName &&
equals(getArgumentNames(), argNames));
}
bool DeclName::isCompoundName(StringRef baseName,
ArrayRef<StringRef> argNames) const {
return (isCompoundName() &&
getBaseName() == baseName &&
equals(getArgumentNames(), argNames));
}
void DeclName::dump() const {
llvm::errs() << *this << "\n";
}
StringRef DeclName::getString(llvm::SmallVectorImpl<char> &scratch,
bool skipEmptyArgumentNames) const {
{
llvm::raw_svector_ostream out(scratch);
print(out, skipEmptyArgumentNames);
}
return StringRef(scratch.data(), scratch.size());
}
llvm::raw_ostream &DeclName::print(llvm::raw_ostream &os,
bool skipEmptyArgumentNames) const {
// Print the base name.
os << getBaseName();
// If this is a simple name, we're done.
if (isSimpleName())
return os;
if (skipEmptyArgumentNames) {
// If there is more than one argument yet none of them have names,
// we're done.
if (!getArgumentNames().empty()) {
bool anyNonEmptyNames = false;
for (auto c : getArgumentNames()) {
if (!c.empty()) {
anyNonEmptyNames = true;
break;
}
}
if (!anyNonEmptyNames)
return os;
}
}
// Print the argument names.
os << "(";
for (auto c : getArgumentNames()) {
os << c << ':';
}
os << ")";
return os;
}
llvm::raw_ostream &DeclName::printPretty(llvm::raw_ostream &os) const {
return print(os, /*skipEmptyArgumentNames=*/!isSpecial());
}
void DeclNameRef::dump() const {
llvm::errs() << *this << "\n";
}
StringRef DeclNameRef::getString(llvm::SmallVectorImpl<char> &scratch,
bool skipEmptyArgumentNames) const {
return FullName.getString(scratch, skipEmptyArgumentNames);
}
llvm::raw_ostream &DeclNameRef::print(llvm::raw_ostream &os,
bool skipEmptyArgumentNames) const {
return FullName.print(os, skipEmptyArgumentNames);
}
llvm::raw_ostream &DeclNameRef::printPretty(llvm::raw_ostream &os) const {
return FullName.printPretty(os);
}
ObjCSelector::ObjCSelector(ASTContext &ctx, unsigned numArgs,
ArrayRef<Identifier> pieces) {
if (numArgs == 0) {
assert(pieces.size() == 1 && "No-argument selector requires one piece");
Storage = DeclName(pieces[0]);
return;
}
assert(numArgs == pieces.size() && "Wrong number of selector pieces");
Storage = DeclName(ctx, Identifier(), pieces);
}
llvm::Optional<ObjCSelector>
ObjCSelector::parse(ASTContext &ctx, StringRef string) {
// Find the first colon.
auto colonPos = string.find(':');
// If there is no colon, we have a nullary selector.
if (colonPos == StringRef::npos) {
if (string.empty() || !Lexer::isIdentifier(string)) return None;
return ObjCSelector(ctx, 0, { ctx.getIdentifier(string) });
}
SmallVector<Identifier, 2> pieces;
do {
// Check whether we have a valid selector piece.
auto piece = string.substr(0, colonPos);
if (piece.empty()) {
pieces.push_back(Identifier());
} else {
if (!Lexer::isIdentifier(piece)) return None;
pieces.push_back(ctx.getIdentifier(piece));
}
// Move to the next piece.
string = string.substr(colonPos+1);
colonPos = string.find(':');
} while (colonPos != StringRef::npos);
// If anything remains of the string, it's not a selector.
if (!string.empty()) return None;
return ObjCSelector(ctx, pieces.size(), pieces);
}
ObjCSelectorFamily ObjCSelector::getSelectorFamily() const {
StringRef text = getSelectorPieces().front().get();
while (!text.empty() && text[0] == '_') text = text.substr(1);
// Does the given selector start with the given string as a prefix, in the
// sense of the selector naming conventions?
// This implementation matches the one used by
// clang::Selector::getMethodFamily, to make sure we behave the same as
// Clang ARC. We're not just calling that method here because it means
// allocating a clang::IdentifierInfo, which requires a Clang ASTContext.
auto hasPrefix = [](StringRef text, StringRef prefix) {
if (!text.startswith(prefix)) return false;
if (text.size() == prefix.size()) return true;
assert(text.size() > prefix.size());
return !clang::isLowercase(text[prefix.size()]);
};
if (false) /*for #define purposes*/;
#define OBJC_SELECTOR_FAMILY(LABEL, PREFIX) \
else if (hasPrefix(text, PREFIX)) return ObjCSelectorFamily::LABEL;
#include "swift/AST/ObjCSelectorFamily.def"
else return ObjCSelectorFamily::None;
}
StringRef ObjCSelector::getString(llvm::SmallVectorImpl<char> &scratch) const {
// Fast path for zero-argument selectors.
if (getNumArgs() == 0) {
auto name = getSelectorPieces()[0];
if (name.empty())
return "";
return name.str();
}
scratch.clear();
llvm::raw_svector_ostream os(scratch);
os << *this;
return os.str();
}
void ObjCSelector::dump() const {
llvm::errs() << *this << "\n";
}