forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRustDemangle.cpp
295 lines (246 loc) · 7.2 KB
/
RustDemangle.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
//===--- RustDemangle.cpp ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines a demangler for Rust v0 mangled symbols as specified in
// https://rust-lang.github.io/rfcs/2603-rust-symbol-name-mangling-v0.html
//
//===----------------------------------------------------------------------===//
#include "llvm/Demangle/RustDemangle.h"
#include "llvm/Demangle/Demangle.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <limits>
using namespace llvm;
using namespace rust_demangle;
char *llvm::rustDemangle(const char *MangledName, char *Buf, size_t *N,
int *Status) {
if (MangledName == nullptr || (Buf != nullptr && N == nullptr)) {
if (Status != nullptr)
*Status = demangle_invalid_args;
return nullptr;
}
// Return early if mangled name doesn't look like a Rust symbol.
StringView Mangled(MangledName);
if (!Mangled.startsWith("_R")) {
if (Status != nullptr)
*Status = demangle_invalid_mangled_name;
return nullptr;
}
Demangler D;
if (!initializeOutputStream(nullptr, nullptr, D.Output, 1024)) {
if (Status != nullptr)
*Status = demangle_memory_alloc_failure;
return nullptr;
}
if (!D.demangle(Mangled)) {
if (Status != nullptr)
*Status = demangle_invalid_mangled_name;
std::free(D.Output.getBuffer());
return nullptr;
}
D.Output += '\0';
char *Demangled = D.Output.getBuffer();
size_t DemangledLen = D.Output.getCurrentPosition();
if (Buf != nullptr) {
if (DemangledLen <= *N) {
std::memcpy(Buf, Demangled, DemangledLen);
std::free(Demangled);
Demangled = Buf;
} else {
std::free(Buf);
}
}
if (N != nullptr)
*N = DemangledLen;
if (Status != nullptr)
*Status = demangle_success;
return Demangled;
}
Demangler::Demangler(size_t MaxRecursionLevel)
: MaxRecursionLevel(MaxRecursionLevel) {}
static inline bool isDigit(const char C) { return '0' <= C && C <= '9'; }
static inline bool isLower(const char C) { return 'a' <= C && C <= 'z'; }
static inline bool isUpper(const char C) { return 'A' <= C && C <= 'Z'; }
/// Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
static inline bool isValid(const char C) {
return isDigit(C) || isLower(C) || isUpper(C) || C == '_';
}
// Demangles Rust v0 mangled symbol. Returns true when successful, and false
// otherwise. The demangled symbol is stored in Output field. It is
// responsibility of the caller to free the memory behind the output stream.
//
// <symbol-name> = "_R" <path> [<instantiating-crate>]
bool Demangler::demangle(StringView Mangled) {
Position = 0;
Error = false;
RecursionLevel = 0;
if (!Mangled.consumeFront("_R")) {
Error = true;
return false;
}
Input = Mangled;
demanglePath();
// FIXME parse optional <instantiating-crate>.
if (Position != Input.size())
Error = true;
return !Error;
}
// <path> = "C" <identifier> // crate root
// | "M" <impl-path> <type> // <T> (inherent impl)
// | "X" <impl-path> <type> <path> // <T as Trait> (trait impl)
// | "Y" <type> <path> // <T as Trait> (trait definition)
// | "N" <ns> <path> <identifier> // ...::ident (nested path)
// | "I" <path> {<generic-arg>} "E" // ...<T, U> (generic args)
// | <backref>
// <identifier> = [<disambiguator>] <undisambiguated-identifier>
// <ns> = "C" // closure
// | "S" // shim
// | <A-Z> // other special namespaces
// | <a-z> // internal namespaces
void Demangler::demanglePath() {
if (Error || RecursionLevel >= MaxRecursionLevel) {
Error = true;
return;
}
SwapAndRestore<size_t> SaveRecursionLevel(RecursionLevel, RecursionLevel + 1);
switch (consume()) {
case 'C': {
parseOptionalBase62Number('s');
Identifier Ident = parseIdentifier();
print(Ident.Name);
break;
}
case 'N': {
char NS = consume();
if (!isLower(NS) && !isUpper(NS)) {
Error = true;
break;
}
demanglePath();
uint64_t Disambiguator = parseOptionalBase62Number('s');
Identifier Ident = parseIdentifier();
if (isUpper(NS)) {
// Special namespaces
print("::{");
if (NS == 'C')
print("closure");
else if (NS == 'S')
print("shim");
else
print(NS);
if (!Ident.empty()) {
print(":");
print(Ident.Name);
}
print('#');
printDecimalNumber(Disambiguator);
print('}');
} else {
// Implementation internal namespaces.
if (!Ident.empty()) {
print("::");
print(Ident.Name);
}
}
break;
}
default:
// FIXME parse remaining productions.
Error = true;
break;
}
}
// <undisambiguated-identifier> = ["u"] <decimal-number> ["_"] <bytes>
Identifier Demangler::parseIdentifier() {
bool Punycode = consumeIf('u');
uint64_t Bytes = parseDecimalNumber();
// Underscore resolves the ambiguity when identifier starts with a decimal
// digit or another underscore.
consumeIf('_');
if (Error || Bytes > Input.size() - Position) {
Error = true;
return {};
}
StringView S = Input.substr(Position, Bytes);
Position += Bytes;
if (!std::all_of(S.begin(), S.end(), isValid)) {
Error = true;
return {};
}
return {S, Punycode};
}
// Parses optional base 62 number. The presence of a number is determined using
// Tag. Returns 0 when tag is absent and parsed value + 1 otherwise.
uint64_t Demangler::parseOptionalBase62Number(char Tag) {
if (!consumeIf(Tag))
return 0;
uint64_t N = parseBase62Number();
if (Error || !addAssign(N, 1))
return 0;
return N;
}
// Parses base 62 number with <0-9a-zA-Z> as digits. Number is terminated by
// "_". All values are offset by 1, so that "_" encodes 0, "0_" encodes 1,
// "1_" encodes 2, etc.
//
// <base-62-number> = {<0-9a-zA-Z>} "_"
uint64_t Demangler::parseBase62Number() {
if (consumeIf('_'))
return 0;
uint64_t Value = 0;
while (true) {
uint64_t Digit;
char C = consume();
if (C == '_') {
break;
} else if (isDigit(C)) {
Digit = C - '0';
} else if (isLower(C)) {
Digit = 10 + (C - 'a');
} else if (isUpper(C)) {
Digit = 10 + 26 + (C - 'A');
} else {
Error = true;
return 0;
}
if (!mulAssign(Value, 62))
return 0;
if (!addAssign(Value, Digit))
return 0;
}
if (!addAssign(Value, 1))
return 0;
return Value;
}
// Parses a decimal number that had been encoded without any leading zeros.
//
// <decimal-number> = "0"
// | <1-9> {<0-9>}
uint64_t Demangler::parseDecimalNumber() {
char C = look();
if (!isDigit(C)) {
Error = true;
return 0;
}
if (C == '0') {
consume();
return 0;
}
uint64_t Value = 0;
while (isDigit(look())) {
if (!mulAssign(Value, 10)) {
Error = true;
return 0;
}
uint64_t D = consume() - '0';
if (!addAssign(Value, D))
return 0;
}
return Value;
}