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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
|
// Copyright 2026 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
// Based on: https://github.com/encounter/cwdemangle
// Copyright 2024 Luke Street <luke@street.dev>
// SPDX-License-Identifier: CC0-1.0
#include "Common/CWDemangler.h"
#include <algorithm>
#include <cctype>
#include <map>
#include <fmt/format.h>
#include "Common/StringUtil.h"
namespace CWDemangler
{
struct ParseQualifiersResult
{
std::string pre;
std::string post;
std::string_view rest;
};
struct ParseDigitsResult
{
std::size_t digits;
std::string_view rest;
};
inline static bool IsAscii(std::string_view s)
{
return std::ranges::none_of(s, [](u8 c) { return c > 127; });
}
static const std::map<std::string_view, std::string_view> operators = {
{"nw", "operator new"}, {"nwa", "operator new[]"},
{"dl", "operator delete"}, {"dla", "operator delete[]"},
{"pl", "operator+"}, {"mi", "operator-"},
{"ml", "operator*"}, {"dv", "operator/"},
{"md", "operator%"}, {"er", "operator^"},
{"ad", "operator&"}, {"or", "operator|"},
{"co", "operator~"}, {"nt", "operator!"},
{"as", "operator="}, {"lt", "operator<"},
{"gt", "operator>"}, {"apl", "operator+="},
{"ami", "operator-="}, {"amu", "operator*="},
{"adv", "operator/="}, {"amd", "operator%="},
{"aer", "operator^="}, {"aad", "operator&="},
{"aor", "operator|="}, {"ls", "operator<<"},
{"rs", "operator>>"}, {"ars", "operator>>="},
{"als", "operator<<="}, {"eq", "operator=="},
{"ne", "operator!="}, {"le", "operator<="},
{"ge", "operator>="}, {"aa", "operator&&"},
{"oo", "operator||"}, {"pp", "operator++"},
{"mm", "operator--"}, {"cm", "operator,"},
{"rm", "operator->*"}, {"rf", "operator->"},
{"cl", "operator()"}, {"vc", "operator[]"},
{"vt", "__vtable"}};
static const std::map<char, std::string_view> types = {
{'i', "int"}, {'b', "bool"}, {'c', "char"}, {'s', "short"},
{'l', "long"}, {'x', "long long"}, {'f', "float"}, {'d', "double"},
{'w', "wchar_t"}, {'v', "void"}, {'e', "..."}, {'r', "long double"},
{'D', "short double"},
};
// Finds the first double underscore in the string, excluding any that are part of a
// template argument list or operator name.
static std::optional<std::size_t> find_split(std::string_view s, bool special,
DemangleOptions options)
{
std::size_t start = 0;
if (special && s.starts_with("op"))
{
const auto result = demangle_arg(s.substr(2), options);
if (!result)
return std::nullopt;
const std::string_view rest = result->rest;
start = s.length() - rest.length();
}
int depth = 0;
const std::size_t length = s.length();
for (std::size_t i = start; i < length; i++)
{
switch (s[i])
{
case '<':
depth++;
break;
case '>':
depth--;
break;
case '_':
if (i < length - 1 && s[i + 1] == '_' && depth == 0)
{
return i;
}
break;
default:
break;
}
}
return std::nullopt;
}
static ParseQualifiersResult parse_qualifiers(std::string_view str)
{
std::string pre;
std::string post;
std::size_t index = 0;
for (char c : str)
{
bool found_non_qualifier = false;
switch (c)
{
case 'P':
if (pre.empty())
{
post.insert(0, "*");
}
else
{
post.insert(0, fmt::format("* {0}", StripTrailingWhitespace(pre)));
pre.clear();
}
break;
case 'R':
if (pre.empty())
{
post.insert(0, "&");
}
else
{
post.insert(0, fmt::format("& {0}", StripTrailingWhitespace(pre)));
pre.clear();
}
break;
case 'C':
pre += "const ";
break;
case 'V':
pre += "volatile ";
break;
case 'U':
pre += "unsigned ";
break;
case 'S':
pre += "signed ";
break;
default:
found_non_qualifier = true;
break;
}
if (found_non_qualifier)
break;
index++;
}
str.remove_prefix(index);
post = StripTrailingWhitespace(post);
return {pre, post, str};
}
static std::optional<ParseDigitsResult> parse_digits(std::string_view str)
{
if (str.empty())
return std::nullopt;
const char* str_start = str.data();
const char* str_end = str_start + str.size();
std::size_t val = 0;
std::string_view remainder;
const auto result = std::from_chars(str_start, str_end, val);
if (result.ec != std::errc{})
return std::nullopt;
const char* rest_start = result.ptr;
if (rest_start != str_end)
remainder = std::string_view(rest_start, str_end - rest_start);
return {{val, remainder}};
}
std::optional<DemangleTemplateArgsResult> demangle_template_args(std::string_view str,
DemangleOptions options)
{
const std::size_t start_idx = str.find('<');
if (start_idx == std::string::npos)
return {{str, ""}};
const std::size_t end_idx = str.rfind('>');
if (end_idx == std::string::npos || end_idx < start_idx)
{
return std::nullopt;
}
std::string_view args(&str[start_idx + 1], &str[end_idx]);
const std::string_view template_name = str.substr(0, start_idx);
std::string tmpl_args = "<";
while (!args.empty())
{
const auto result = demangle_arg(args, options);
if (!result)
return std::nullopt;
const auto demangled_arg = result.value();
const std::string_view rest = demangled_arg.rest;
tmpl_args += demangled_arg.arg_pre;
tmpl_args += demangled_arg.arg_post;
if (rest.empty())
break;
tmpl_args += ", ";
args = rest.substr(1);
}
tmpl_args += ">";
return {{template_name, tmpl_args}};
}
std::optional<DemangleNameResult> demangle_name(std::string_view str, DemangleOptions options)
{
const auto result = parse_digits(str);
if (!result)
return std::nullopt;
auto [size, rest] = result.value();
if (rest.length() < size)
{
return std::nullopt;
}
auto result1 = demangle_template_args(rest.substr(0, size), options);
if (!result1)
return std::nullopt;
auto [name, args] = result1.value();
const std::string result_name{name};
return {{result_name, fmt::format("{0}{1}", name, args), rest.substr(size)}};
}
std::optional<DemangleNameResult> demangle_qualified_name(std::string_view str,
DemangleOptions options)
{
if (!str.starts_with('Q'))
return demangle_name(str, options);
if (str.length() < 3)
{
return std::nullopt;
}
// MWCC only preserves up to 9 layers of namespace/class depth in symbol names, so we
// can just check one digit
const int digit = static_cast<int>(str[1] - '0');
if (digit < 0 || digit > 9)
return std::nullopt;
const int count = digit;
str = str.substr(2);
std::string last_class;
std::string qualified;
for (int i = 0; i < count; i++)
{
const auto result = demangle_name(str, options);
if (!result)
return std::nullopt;
auto [class_name, full, rest] = *result;
qualified += full;
last_class = class_name;
str = rest;
if (i < count - 1)
{
qualified += "::";
}
}
return {{last_class, qualified, str}};
}
std::optional<DemangleArgResult> demangle_arg(std::string_view str, DemangleOptions options)
{
// Negative constant
if (str.starts_with('-'))
{
const auto parse_result = parse_digits(str.substr(1));
if (!parse_result)
return std::nullopt;
const std::size_t size = parse_result->digits;
const std::string out_val = fmt::format("-{}", size);
return {{out_val, "", parse_result->rest}};
}
std::string result;
const auto parse_qual_result = parse_qualifiers(str);
std::string pre = parse_qual_result.pre;
std::string post = parse_qual_result.post;
std::string_view rest = parse_qual_result.rest;
result += pre;
str = rest;
// Disambiguate arguments starting with a number
if (str.length() > 0 && std::isdigit(static_cast<u8>(str[0])))
{
const auto parse_result = parse_digits(str);
if (!parse_result)
return std::nullopt;
auto& [num, rest_value] = parse_result.value();
rest = rest_value;
// If the number is followed by a comma or the end of the string, it's a template argument
if (rest.empty() || rest.starts_with(','))
{
// ...or a Metrowerks extension type
if (options.mw_extensions)
{
const std::string t = num == 1 ? "__int128" : num == 2 ? "__vec2x32float__" : "";
if (!t.empty())
{
result += t;
return {{result, post, rest}};
}
}
result += fmt::format("{}{}", num, post);
return {{result, "", rest}};
}
// Otherwise, it's (probably) the size of a type
const auto demangle_name_result = demangle_name(str, options);
if (!demangle_name_result)
return std::nullopt;
result += demangle_name_result->full;
result += post;
return {{result, "", demangle_name_result->rest}};
}
// Handle qualified names
if (str.starts_with('Q'))
{
const auto demangle_qual_result = demangle_qualified_name(str, options);
if (!demangle_qual_result)
return std::nullopt;
result += demangle_qual_result->full;
result += post;
return {{result, "", demangle_qual_result->rest}};
}
bool is_member = false;
bool const_member = false;
if (str.starts_with('M'))
{
is_member = true;
const auto demangle_qual_result = demangle_qualified_name(str.substr(1), options);
if (!demangle_qual_result)
return std::nullopt;
rest = demangle_qual_result->rest;
pre = fmt::format("{}::*{}", demangle_qual_result->full, pre);
if (!rest.starts_with('F'))
{
return std::nullopt;
}
str = rest;
}
if (is_member || str.starts_with('F'))
{
str.remove_prefix(1);
if (is_member)
{
// "const void*, const void*" or "const void*, void*"
if (str.starts_with("PCvPCv"))
{
const_member = true;
str.remove_prefix(6);
}
else if (str.starts_with("PCvPv"))
{
str.remove_prefix(5);
}
else
{
return std::nullopt;
}
}
else if (post.starts_with('*'))
{
post = StripLeadingWhitespace(post.substr(1));
pre = fmt::format("*{}", pre);
}
else
{
return std::nullopt;
}
const auto demangle_func_args_result = demangle_function_args(str, options);
if (!demangle_func_args_result)
return std::nullopt;
const auto demangled_func_args = demangle_func_args_result.value();
if (!demangled_func_args.rest.starts_with('_'))
{
return std::nullopt;
}
const auto demangle_arg_result = demangle_arg(demangled_func_args.rest.substr(1), options);
if (!demangle_arg_result)
return std::nullopt;
const std::string_view const_str = const_member ? " const" : "";
const std::string res_pre = fmt::format("{} ({}{}", demangle_arg_result->arg_pre, pre, post);
const std::string res_post = fmt::format(")({}){}{}", demangled_func_args.args, const_str,
demangle_arg_result->arg_post);
return {{res_pre, res_post, demangle_arg_result->rest}};
}
if (str.starts_with('A'))
{
const auto parse_result = parse_digits(str.substr(1));
if (!parse_result)
return std::nullopt;
auto& [count, rest_value] = parse_result.value();
rest = rest_value;
if (!rest.starts_with('_'))
{
return std::nullopt;
}
const auto demangle_arg_result = demangle_arg(rest.substr(1), options);
if (!demangle_arg_result)
return std::nullopt;
if (!post.empty())
{
post = fmt::format("({})", post);
}
result = fmt::format("{}{}{}", pre, demangle_arg_result->arg_pre, post);
const std::string ret_post = fmt::format("[{}]{}", count, demangle_arg_result->arg_post);
return {{result, ret_post, demangle_arg_result->rest}};
}
if (str.length() == 0)
return std::nullopt;
std::string_view type;
const char c = str[0];
if (types.contains(c))
{
type = types.at(c);
}
else
{
// Handle special cases
switch (c)
{
case '1':
if (options.mw_extensions)
type = "__int128";
break;
case '2':
if (options.mw_extensions)
type = "__vec2x32float__";
break;
case '_':
return {{result, "", rest}};
default:
return std::nullopt;
}
}
return {{fmt::format("{}{}{}", result, type, post), "", str.substr(1)}};
}
std::optional<DemangleFunctionArgsResult> demangle_function_args(std::string_view str,
DemangleOptions options)
{
std::string result;
while (!str.empty())
{
if (!result.empty())
{
result += ", ";
}
const auto demangle_arg_result = demangle_arg(str, options);
if (!demangle_arg_result)
return std::nullopt;
result += demangle_arg_result->arg_pre;
result += demangle_arg_result->arg_post;
str = demangle_arg_result->rest;
if (str.starts_with('_') || str.starts_with(','))
{
break;
}
}
return {{result, str}};
}
std::optional<std::string> demangle_special_function(std::string_view str,
std::string_view class_name,
DemangleOptions options)
{
if (str.starts_with("op"))
{
const std::string_view rest = str.substr(2);
const auto demangle_arg_result = demangle_arg(rest, options);
if (!demangle_arg_result)
return std::nullopt;
return fmt::format("operator {}{}", demangle_arg_result->arg_pre,
demangle_arg_result->arg_post);
}
const auto result = demangle_template_args(str, options);
if (!result)
return std::nullopt;
auto& [op, args] = result.value();
std::string_view func_name;
if (op == "dt")
{
return fmt::format("~{}{}", class_name, args);
}
else if (op == "ct")
{
func_name = class_name;
}
else if (operators.contains(op.data()))
{
func_name = operators.at(op.data());
}
else
{
return fmt::format("__{}{}", op, args);
}
return fmt::format("{0}{1}", func_name, args);
}
// Demangle a symbol name.
//
// Returns `std::nullopt` if the input is not a valid mangled name.
std::optional<std::string> demangle(std::string_view str, DemangleOptions options)
{
if (!IsAscii(str))
{
return std::nullopt;
}
bool special = false;
bool cnst = false;
std::string fn_name;
std::string static_var;
// Handle new static function variables (Wii CW)
const bool guard = str.starts_with("@GUARD@");
if (guard || str.starts_with("@LOCAL@"))
{
str = str.substr(7);
std::size_t idx = str.rfind('@');
if (idx == std::string::npos)
return std::nullopt;
const std::string_view rest = str.substr(0, idx);
const std::string_view var = str.substr(idx);
if (guard)
{
static_var = fmt::format("{0} guard", var.substr(1));
}
else
{
static_var = var.substr(1);
}
str = rest;
}
if (str.starts_with("__"))
{
special = true;
str = str.substr(2);
}
const auto idx_temp = find_split(str, special, options);
if (!idx_temp)
return std::nullopt;
std::size_t idx = idx_temp.value();
// Handle any trailing underscores in the function name
while (str[idx + 2] == '_')
{
idx++;
}
const std::string_view fn_name_out = str.substr(0, idx);
std::string_view rest = str.substr(idx);
if (special)
{
if (fn_name_out == "init")
{
// Special case for double __
const std::size_t rest_idx = rest.substr(2).find("__");
if (rest_idx == std::string::npos)
return std::nullopt;
fn_name = str.substr(0, rest_idx + 6);
rest.remove_prefix(rest_idx + 2);
}
else
{
fn_name = fn_name_out;
}
}
else
{
const auto result = demangle_template_args(fn_name_out, options);
if (!result)
return std::nullopt;
const auto template_args = result.value();
fn_name = fmt::format("{}{}", template_args.name, template_args.args);
}
// Handle old static function variables (GC CW)
const std::size_t first_idx = fn_name.find('$');
if (first_idx != std::string::npos)
{
const std::size_t second_idx = fn_name.substr(first_idx + 1).find('$');
if (second_idx == std::string::npos)
return std::nullopt;
const std::string var = fn_name.substr(0, first_idx);
std::string rest_temp = fn_name.substr(first_idx + 1);
const std::string var_type = rest_temp.substr(0, second_idx);
rest_temp = rest_temp.substr(second_idx);
if (!var_type.starts_with("localstatic"))
{
return std::nullopt;
}
if (var == "init")
{
// Sadly, $localstatic doesn't provide the variable name in guard/init
static_var = fmt::format("{} guard", var_type);
}
else
{
static_var = var;
}
fn_name = rest_temp.substr(1);
}
str = rest.substr(2);
std::string class_name;
std::string return_type_pre;
std::string return_type_post;
std::string qualified;
if (!str.starts_with('F'))
{
const auto result = demangle_qualified_name(str, options);
if (!result)
return std::nullopt;
class_name = result->class_name;
qualified = result->full;
str = result->rest;
}
if (special)
{
const auto result = demangle_special_function(fn_name, class_name, options);
if (!result)
return std::nullopt;
fn_name = result.value();
}
if (str.starts_with('C'))
{
str.remove_prefix(1);
cnst = true;
}
if (str.starts_with('F'))
{
str.remove_prefix(1);
const auto result = demangle_function_args(str, options);
if (!result)
return std::nullopt;
if (options.omit_empty_parameters && result->args == "void")
{
fn_name = fmt::format("{}()", fn_name);
}
else
{
fn_name = fmt::format("{}({})", fn_name, result->args);
}
str = result->rest;
}
if (str.starts_with('_'))
{
str.remove_prefix(1);
const auto result = demangle_arg(str, options);
if (!result)
return std::nullopt;
return_type_pre = result->arg_pre;
return_type_post = result->arg_post;
str = result->rest;
}
if (!str.empty())
{
return std::nullopt;
}
if (cnst)
{
fn_name = fmt::format("{} const", fn_name);
}
if (!qualified.empty())
{
fn_name = fmt::format("{}::{}", qualified, fn_name);
}
if (!return_type_pre.empty())
{
fn_name = fmt::format("{} {}{}", return_type_pre, fn_name, return_type_post);
}
if (!static_var.empty())
{
fn_name = fmt::format("{}::{}", fn_name, static_var);
}
return fn_name;
}
} // namespace CWDemangler
|