18
18
19
19
#include " swift/Demangling/Demangler.h"
20
20
#include " swift/Demangling/Punycode.h"
21
+ #include " swift/Demangling/ManglingUtils.h"
21
22
#include " swift/AST/Ownership.h"
22
23
#include " swift/Strings.h"
23
24
#include " RemanglerBase.h"
@@ -33,88 +34,6 @@ static void unreachable(const char *Message) {
33
34
std::abort ();
34
35
}
35
36
36
- // / Translate the given operator character into its mangled form.
37
- // /
38
- // / Current operator characters: @/=-+*%<>!&|^~ and the special operator '..'
39
- static char mangleOperatorChar (char op) {
40
- switch (op) {
41
- case ' &' : return ' a' ; // 'and'
42
- case ' @' : return ' c' ; // 'commercial at sign'
43
- case ' /' : return ' d' ; // 'divide'
44
- case ' =' : return ' e' ; // 'equal'
45
- case ' >' : return ' g' ; // 'greater'
46
- case ' <' : return ' l' ; // 'less'
47
- case ' *' : return ' m' ; // 'multiply'
48
- case ' !' : return ' n' ; // 'negate'
49
- case ' |' : return ' o' ; // 'or'
50
- case ' +' : return ' p' ; // 'plus'
51
- case ' ?' : return ' q' ; // 'question'
52
- case ' %' : return ' r' ; // 'remainder'
53
- case ' -' : return ' s' ; // 'subtract'
54
- case ' ~' : return ' t' ; // 'tilde'
55
- case ' ^' : return ' x' ; // 'xor'
56
- case ' .' : return ' z' ; // 'zperiod' (the z is silent)
57
- default :
58
- return op;
59
- }
60
- }
61
-
62
- static bool isNonAscii (StringRef str) {
63
- for (unsigned char c : str) {
64
- if (c >= 0x80 )
65
- return true ;
66
- }
67
- return false ;
68
- }
69
-
70
- static char mangleOperatorKind (OperatorKind operatorKind) {
71
- switch (operatorKind) {
72
- case OperatorKind::NotOperator: unreachable (" invalid" );
73
- case OperatorKind::Infix: return ' i' ;
74
- case OperatorKind::Prefix: return ' p' ;
75
- case OperatorKind::Postfix: return ' P' ;
76
- }
77
- unreachable (" invalid" );
78
- }
79
-
80
- static void mangleIdentifier (StringRef ident, OperatorKind operatorKind,
81
- bool usePunycode, RemanglerBuffer &Buffer) {
82
- std::string punycodeBuf;
83
- if (usePunycode) {
84
- // If the identifier contains non-ASCII character, we mangle
85
- // with an initial X and Punycode the identifier string.
86
- if (isNonAscii (ident)) {
87
- Buffer << ' X' ;
88
- Punycode::encodePunycodeUTF8 (ident, punycodeBuf);
89
- ident = punycodeBuf;
90
- }
91
- }
92
-
93
- // Mangle normal identifiers as
94
- // count identifier-char+
95
- // where the count is the number of characters in the identifier,
96
- // and where individual identifier characters represent themselves.
97
- if (operatorKind == OperatorKind::NotOperator) {
98
- Buffer << ident.size () << ident;
99
- return ;
100
- }
101
-
102
- // Mangle operator identifiers as
103
- // operator ::= 'o' operator-fixity count operator-char+
104
- // operator-fixity ::= 'p' // prefix
105
- // operator-fixity ::= 'P' // postfix
106
- // operator-fixity ::= 'i' // infix
107
- // where the count is the number of characters in the operator,
108
- // and where the individual operator characters are translated.
109
- Buffer << ' o' << mangleOperatorKind (operatorKind);
110
-
111
- // Mangle ASCII operators directly.
112
- Buffer << ident.size ();
113
- for (char ch : ident) {
114
- Buffer << mangleOperatorChar (ch);
115
- }
116
- }
117
-
118
37
namespace {
119
38
class Remangler : public RemanglerBase {
120
39
public:
@@ -388,7 +307,35 @@ void Remangler::mangleInfixOperator(Node *node) {
388
307
mangleIdentifier (node->getText (), OperatorKind::Infix);
389
308
}
390
309
void Remangler::mangleIdentifier (StringRef ident, OperatorKind operatorKind) {
391
- ::mangleIdentifier (ident, operatorKind, /* usePunycode*/ false , Buffer);
310
+ // Mangle normal identifiers as
311
+ // count identifier-char+
312
+ // where the count is the number of characters in the identifier,
313
+ // and where individual identifier characters represent themselves.
314
+ if (operatorKind == OperatorKind::NotOperator) {
315
+ Buffer << ident.size () << ident;
316
+ return ;
317
+ }
318
+
319
+ // Mangle operator identifiers as
320
+ // operator ::= 'o' operator-fixity count operator-char+
321
+ // operator-fixity ::= 'p' // prefix
322
+ // operator-fixity ::= 'P' // postfix
323
+ // operator-fixity ::= 'i' // infix
324
+ // where the count is the number of characters in the operator,
325
+ // and where the individual operator characters are translated.
326
+ Buffer << ' o' ;
327
+ switch (operatorKind) {
328
+ case OperatorKind::NotOperator: unreachable (" invalid" );
329
+ case OperatorKind::Infix: Buffer << ' i' ; break ;
330
+ case OperatorKind::Prefix: Buffer << ' p' ; break ;
331
+ case OperatorKind::Postfix: Buffer << ' P' ; break ;
332
+ }
333
+
334
+ // Mangle ASCII operators directly.
335
+ Buffer << ident.size ();
336
+ for (char ch : ident) {
337
+ Buffer << Mangle::translateOperatorChar (ch);
338
+ }
392
339
}
393
340
394
341
void Remangler::mangleNumber (Node *node) {
@@ -983,9 +930,7 @@ void Remangler::mangleNamedEntity(Node *node, char basicKind,
983
930
if (!privateDiscriminator.empty ()
984
931
&& name->getKind () == Node::Kind::Identifier) {
985
932
Buffer << ' P' ;
986
- ::mangleIdentifier (privateDiscriminator,
987
- OperatorKind::NotOperator,
988
- /* punycode*/ false , Buffer);
933
+ mangleIdentifier (privateDiscriminator, OperatorKind::NotOperator);
989
934
}
990
935
mangle (name);
991
936
}
0 commit comments