Skip to content

Commit 0528d8a

Browse files
committed
OldRemangler: cleanup and simplification of mangleIdentifier
NFC
1 parent abccbd8 commit 0528d8a

File tree

1 file changed

+31
-86
lines changed

1 file changed

+31
-86
lines changed

lib/Demangling/OldRemangler.cpp

Lines changed: 31 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "swift/Demangling/Demangler.h"
2020
#include "swift/Demangling/Punycode.h"
21+
#include "swift/Demangling/ManglingUtils.h"
2122
#include "swift/AST/Ownership.h"
2223
#include "swift/Strings.h"
2324
#include "RemanglerBase.h"
@@ -33,88 +34,6 @@ static void unreachable(const char *Message) {
3334
std::abort();
3435
}
3536

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-
11837
namespace {
11938
class Remangler : public RemanglerBase {
12039
public:
@@ -388,7 +307,35 @@ void Remangler::mangleInfixOperator(Node *node) {
388307
mangleIdentifier(node->getText(), OperatorKind::Infix);
389308
}
390309
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+
}
392339
}
393340

394341
void Remangler::mangleNumber(Node *node) {
@@ -983,9 +930,7 @@ void Remangler::mangleNamedEntity(Node *node, char basicKind,
983930
if (!privateDiscriminator.empty()
984931
&& name->getKind() == Node::Kind::Identifier) {
985932
Buffer << 'P';
986-
::mangleIdentifier(privateDiscriminator,
987-
OperatorKind::NotOperator,
988-
/*punycode*/ false, Buffer);
933+
mangleIdentifier(privateDiscriminator, OperatorKind::NotOperator);
989934
}
990935
mangle(name);
991936
}

0 commit comments

Comments
 (0)