forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIdentifier.cpp
168 lines (140 loc) · 4.27 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
//===--- Identifier.cpp - Uniqued Identifier --------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the Identifier interface.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/Identifier.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ConvertUTF.h"
using namespace swift;
raw_ostream &llvm::operator<<(raw_ostream &OS, Identifier I) {
if (I.get() == 0) return OS << "_";
return OS << I.get();
}
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;
}
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<UTF8 const *>(data.begin()),
*end = reinterpret_cast<UTF8 const *>(data.end());
UTF32 codePoint;
ConversionResult res = llvm::convertUTF8Sequence(&s, end, &codePoint,
strictConversion);
assert(res == 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;
}
void DeclName::dump() const {
llvm::errs() << *this << "\n";
}
llvm::raw_ostream &DeclName::printPretty(llvm::raw_ostream &os) const {
// Print the base name.
os << getBaseName();
// If this is a simple name, we're done.
if (isSimpleName())
return os;
// If there is more than one argument yet none of them have names,
// we're done.
if (getArgumentNames().size() > 0) {
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;
}
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);
}
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";
}