Skip to content

Commit 172972d

Browse files
authored
Merge pull request #17357 from DougGregor/mangle-more-standard-substitutions-4.2
[4.2] [Mangling] Mangle more standard substitutions
2 parents 255e747 + 46ae13b commit 172972d

File tree

71 files changed

+423
-334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+423
-334
lines changed

docs/ABI/Mangling.rst

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,27 +329,63 @@ Types
329329
any-generic-type ::= protocol 'P' // nominal protocol type
330330
any-generic-type ::= context decl-name 'a' // typealias type (used in DWARF and USRs)
331331

332-
any-generic-type ::= 'S' KNOWN-TYPE-KIND // known nominal type substitution
333-
any-generic-type ::= 'S' NATURAL KNOWN-TYPE-KIND // repeated known type substitutions of the same kind
332+
any-generic-type ::= standard-substitutions
333+
334+
standard-substitutions ::= 'S' KNOWN-TYPE-KIND // known nominal type substitution
335+
standard-substitutions ::= 'S' NATURAL KNOWN-TYPE-KIND // repeated known type substitutions of the same kind
334336

337+
KNOWN-TYPE-KIND ::= 'A' // Swift.AutoreleasingUnsafeMutablePointer
335338
KNOWN-TYPE-KIND ::= 'a' // Swift.Array
339+
KNOWN-TYPE-KIND ::= 'B' // Swift.BinaryFloatingPoint
336340
KNOWN-TYPE-KIND ::= 'b' // Swift.Bool
337341
KNOWN-TYPE-KIND ::= 'c' // Swift.UnicodeScalar
342+
KNOWN-TYPE-KIND ::= 'd' // Swift.Dictionary
338343
KNOWN-TYPE-KIND ::= 'd' // Swift.Float64
344+
KNOWN-TYPE-KIND ::= 'E' // Swift.Encodable
345+
KNOWN-TYPE-KIND ::= 'e' // Swift.Decodable
346+
KNOWN-TYPE-KIND ::= 'F' // Swift.FloatingPoint
339347
KNOWN-TYPE-KIND ::= 'f' // Swift.Float32
348+
KNOWN-TYPE-KIND ::= 'G' // Swift.RandomNumberGenerator
349+
KNOWN-TYPE-KIND ::= 'H' // Swift.Hashable
350+
KNOWN-TYPE-KIND ::= 'h' // Swift.Set
351+
KNOWN-TYPE-KIND ::= 'I' // Swift.DefaultIndices
340352
KNOWN-TYPE-KIND ::= 'i' // Swift.Int
341-
KNOWN-TYPE-KIND ::= 'V' // Swift.UnsafeRawPointer
342-
KNOWN-TYPE-KIND ::= 'v' // Swift.UnsafeMutableRawPointer
353+
KNOWN-TYPE-KIND ::= 'J' // Swift.Character
354+
KNOWN-TYPE-KIND ::= 'j' // Swift.Numeric
355+
KNOWN-TYPE-KIND ::= 'K' // Swift.BidirectionalCollection
356+
KNOWN-TYPE-KIND ::= 'k' // Swift.RandomAccessCollection
357+
KNOWN-TYPE-KIND ::= 'L' // Swift.Comparable
358+
KNOWN-TYPE-KIND ::= 'l' // Swift.Collection
359+
KNOWN-TYPE-KIND ::= 'M' // Swift.MutableCollection
360+
KNOWN-TYPE-KIND ::= 'm' // Swift.RangeReplaceableCollection
361+
KNOWN-TYPE-KIND ::= 'N' // Swift.ClosedRange
362+
KNOWN-TYPE-KIND ::= 'n' // Swift.Range
363+
KNOWN-TYPE-KIND ::= 'O' // Swift.ObjectIdentifier
343364
KNOWN-TYPE-KIND ::= 'P' // Swift.UnsafePointer
344365
KNOWN-TYPE-KIND ::= 'p' // Swift.UnsafeMutablePointer
345-
KNOWN-TYPE-KIND ::= 'Q' // Swift.ImplicitlyUnwrappedOptional
366+
KNOWN-TYPE-KIND ::= 'Q' // Swift.Equatable
346367
KNOWN-TYPE-KIND ::= 'q' // Swift.Optional
347368
KNOWN-TYPE-KIND ::= 'R' // Swift.UnsafeBufferPointer
348369
KNOWN-TYPE-KIND ::= 'r' // Swift.UnsafeMutableBufferPointer
349370
KNOWN-TYPE-KIND ::= 'S' // Swift.String
371+
KNOWN-TYPE-KIND ::= 's' // Swift.Substring
372+
KNOWN-TYPE-KIND ::= 'T' // Swift.Sequence
373+
KNOWN-TYPE-KIND ::= 't' // Swift.IteratorProtocol
374+
KNOWN-TYPE-KIND ::= 'U' // Swift.UnsignedInteger
350375
KNOWN-TYPE-KIND ::= 'u' // Swift.UInt
376+
KNOWN-TYPE-KIND ::= 'V' // Swift.UnsafeRawPointer
377+
KNOWN-TYPE-KIND ::= 'v' // Swift.UnsafeMutableRawPointer
378+
KNOWN-TYPE-KIND ::= 'W' // Swift.UnsafeRawBufferPointer
379+
KNOWN-TYPE-KIND ::= 'w' // Swift.UnsafeMutableRawBufferPointer
380+
KNOWN-TYPE-KIND ::= 'X' // Swift.RangeExpression
381+
KNOWN-TYPE-KIND ::= 'x' // Swift.Strideable
382+
KNOWN-TYPE-KIND ::= 'Y' // Swift.RawRepresentable
383+
KNOWN-TYPE-KIND ::= 'y' // Swift.StringProtocol
384+
KNOWN-TYPE-KIND ::= 'Z' // Swift.SignedInteger
385+
KNOWN-TYPE-KIND ::= 'z' // Swift.BinaryInteger
351386

352387
protocol ::= context decl-name
388+
protocol ::= standard-substitutions
353389

354390
type ::= 'Bb' // Builtin.BridgeObject
355391
type ::= 'BB' // Builtin.UnsafeValueBuffer

include/swift/AST/ASTMangler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ class ASTMangler : public Mangler {
208208

209209
void appendModule(const ModuleDecl *module);
210210

211-
void appendProtocolName(const ProtocolDecl *protocol);
211+
void appendProtocolName(const ProtocolDecl *protocol,
212+
bool allowStandardSubstitution = true);
212213

213214
void appendAnyGenericType(const GenericTypeDecl *decl);
214215

include/swift/Demangling/StandardTypesMangling.def

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,56 @@
1313
/// STANDARD_TYPE(KIND, MANGLING, TYPENAME)
1414
/// The 1-character MANGLING for a known TYPENAME of KIND.
1515

16+
STANDARD_TYPE(Structure, A, AutoreleasingUnsafeMutablePointer)
1617
STANDARD_TYPE(Structure, a, Array)
1718
STANDARD_TYPE(Structure, b, Bool)
1819
STANDARD_TYPE(Structure, c, UnicodeScalar)
20+
STANDARD_TYPE(Structure, D, Dictionary)
1921
STANDARD_TYPE(Structure, d, Double)
2022
STANDARD_TYPE(Structure, f, Float)
23+
STANDARD_TYPE(Structure, h, Set)
24+
STANDARD_TYPE(Structure, I, DefaultIndices)
2125
STANDARD_TYPE(Structure, i, Int)
22-
STANDARD_TYPE(Structure, V, UnsafeRawPointer)
23-
STANDARD_TYPE(Structure, v, UnsafeMutableRawPointer)
26+
STANDARD_TYPE(Structure, J, Character)
27+
STANDARD_TYPE(Structure, N, ClosedRange)
28+
STANDARD_TYPE(Structure, n, Range)
29+
STANDARD_TYPE(Structure, O, ObjectIdentifier)
2430
STANDARD_TYPE(Structure, P, UnsafePointer)
2531
STANDARD_TYPE(Structure, p, UnsafeMutablePointer)
2632
STANDARD_TYPE(Structure, R, UnsafeBufferPointer)
2733
STANDARD_TYPE(Structure, r, UnsafeMutableBufferPointer)
2834
STANDARD_TYPE(Structure, S, String)
35+
STANDARD_TYPE(Structure, s, Substring)
2936
STANDARD_TYPE(Structure, u, UInt)
37+
STANDARD_TYPE(Structure, V, UnsafeRawPointer)
38+
STANDARD_TYPE(Structure, v, UnsafeMutableRawPointer)
39+
STANDARD_TYPE(Structure, W, UnsafeRawBufferPointer)
40+
STANDARD_TYPE(Structure, w, UnsafeMutableRawBufferPointer)
3041

3142
STANDARD_TYPE(Enum, q, Optional)
32-
STANDARD_TYPE(Enum, Q, ImplicitlyUnwrappedOptional)
43+
44+
STANDARD_TYPE(Protocol, B, BinaryFloatingPoint)
45+
STANDARD_TYPE(Protocol, E, Encodable)
46+
STANDARD_TYPE(Protocol, e, Decodable)
47+
STANDARD_TYPE(Protocol, F, FloatingPoint)
48+
STANDARD_TYPE(Protocol, G, RandomNumberGenerator)
49+
STANDARD_TYPE(Protocol, H, Hashable)
50+
STANDARD_TYPE(Protocol, j, Numeric)
51+
STANDARD_TYPE(Protocol, K, BidirectionalCollection)
52+
STANDARD_TYPE(Protocol, k, RandomAccessCollection)
53+
STANDARD_TYPE(Protocol, L, Comparable)
54+
STANDARD_TYPE(Protocol, l, Collection)
55+
STANDARD_TYPE(Protocol, M, MutableCollection)
56+
STANDARD_TYPE(Protocol, m, RangeReplaceableCollection)
57+
STANDARD_TYPE(Protocol, Q, Equatable)
58+
STANDARD_TYPE(Protocol, T, Sequence)
59+
STANDARD_TYPE(Protocol, t, IteratorProtocol)
60+
STANDARD_TYPE(Protocol, U, UnsignedInteger)
61+
STANDARD_TYPE(Protocol, X, RangeExpression)
62+
STANDARD_TYPE(Protocol, x, Strideable)
63+
STANDARD_TYPE(Protocol, Y, RawRepresentable)
64+
STANDARD_TYPE(Protocol, y, StringProtocol)
65+
STANDARD_TYPE(Protocol, Z, SignedInteger)
66+
STANDARD_TYPE(Protocol, z, BinaryInteger)
3367

3468
#undef STANDARD_TYPE

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,11 @@ void ASTMangler::appendModule(const ModuleDecl *module) {
14921492
}
14931493

14941494
/// Mangle the name of a protocol as a substitution candidate.
1495-
void ASTMangler::appendProtocolName(const ProtocolDecl *protocol) {
1495+
void ASTMangler::appendProtocolName(const ProtocolDecl *protocol,
1496+
bool allowStandardSubstitution) {
1497+
if (allowStandardSubstitution && tryAppendStandardSubstitution(protocol))
1498+
return;
1499+
14961500
appendContextOf(protocol);
14971501
auto *clangDecl = protocol->getClangDecl();
14981502
if (auto *clangProto = cast_or_null<clang::ObjCProtocolDecl>(clangDecl))

lib/Demangling/Demangler.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,17 @@ NodePointer Demangler::popTypeList() {
11951195
}
11961196

11971197
NodePointer Demangler::popProtocol() {
1198+
if (NodePointer Type = popNode(Node::Kind::Type)) {
1199+
if (Type->getNumChildren() < 1)
1200+
return nullptr;
1201+
1202+
NodePointer Proto = Type->getChild(0);
1203+
if (Proto->getKind() != Node::Kind::Protocol)
1204+
return nullptr;
1205+
1206+
return Type;
1207+
}
1208+
11981209
NodePointer Name = popNode(isDeclName);
11991210
NodePointer Ctx = popContext();
12001211
NodePointer Proto = createWithChildren(Node::Kind::Protocol, Ctx, Name);

lib/Demangling/OldRemangler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,9 @@ void Remangler::mangleProtocol(Node *node, EntityContext &ctx) {
17541754
}
17551755

17561756
void Remangler::mangleProtocolWithoutPrefix(Node *node) {
1757+
if (mangleStandardSubstitution(node))
1758+
return;
1759+
17571760
if (node->getKind() == Node::Kind::Type) {
17581761
assert(node->getNumChildren() == 1);
17591762
node = node->begin()[0];

lib/Demangling/Remangler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ class Remangler {
248248
}
249249

250250
void manglePureProtocol(Node *Proto) {
251+
if (mangleStandardSubstitution(Proto))
252+
return;
253+
251254
Proto = skipType(Proto);
252255
mangleChildNodes(Proto);
253256
}
@@ -354,7 +357,8 @@ void Remangler::mangleIdentifierImpl(Node *node, bool isOperator) {
354357

355358
bool Remangler::mangleStandardSubstitution(Node *node) {
356359
if (node->getKind() != Node::Kind::Structure
357-
&& node->getKind() != Node::Kind::Enum)
360+
&& node->getKind() != Node::Kind::Enum
361+
&& node->getKind() != Node::Kind::Protocol)
358362
return false;
359363

360364
Node *context = node->getFirstChild();

lib/IRGen/IRGenMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ std::string IRGenMangler::mangleTypeForLLVMTypeName(CanType Ty) {
120120
// don't start with a digit and don't need to be quoted.
121121
Buffer << 'T';
122122
if (auto P = dyn_cast<ProtocolType>(Ty)) {
123-
appendProtocolName(P->getDecl());
123+
appendProtocolName(P->getDecl(), /*allowStandardSubstitution=*/false);
124124
appendOperator("P");
125125
} else {
126126
appendType(Ty);

lib/IRGen/IRGenMangler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class IRGenMangler : public Mangle::ASTMangler {
109109

110110
std::string mangleBareProtocol(const ProtocolDecl *Decl) {
111111
beginMangling();
112-
appendProtocolName(Decl);
112+
appendProtocolName(Decl, /*allowStandardSubstitution=*/false);
113113
appendOperator("P");
114114
return finalize();
115115
}
@@ -344,7 +344,7 @@ class IRGenMangler : public Mangle::ASTMangler {
344344

345345
std::string mangleForProtocolDescriptor(ProtocolType *Proto) {
346346
beginMangling();
347-
appendProtocolName(Proto->getDecl());
347+
appendProtocolName(Proto->getDecl(), /*allowStandardSubstitution=*/false);
348348
appendOperator("P");
349349
return finalize();
350350
}

stdlib/public/SDK/Foundation/NSError.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
MANGLE_SYM(So10CFErrorRefas5Error10FoundationWa)();
2222

2323
extern "C" const SWIFT_CC(swift) hashable_support::HashableWitnessTable *
24-
MANGLE_SYM(So8NSObjectCs8Hashable10ObjectiveCWa)();
24+
MANGLE_SYM(So8NSObjectCSH10ObjectiveCWa)();
2525

2626
extern "C" SWIFT_CC(swift)
2727
NSDictionary *MANGLE_SYM(10Foundation24_getErrorDefaultUserInfoyyXlSgxs0C0RzlF)(
@@ -37,7 +37,7 @@
3737
// Define the bridging info struct.
3838
extern "C" ErrorBridgingInfo ERROR_BRIDGING_SYMBOL_NAME = {
3939
MANGLE_SYM(So10CFErrorRefas5Error10FoundationWa),
40-
MANGLE_SYM(So8NSObjectCs8Hashable10ObjectiveCWa),
40+
MANGLE_SYM(So8NSObjectCSH10ObjectiveCWa),
4141
MANGLE_SYM(10Foundation24_getErrorDefaultUserInfoyyXlSgxs0C0RzlF),
4242
MANGLE_SYM(10Foundation21_bridgeNSErrorToError_3outSbSo0C0C_SpyxGtAA021_ObjectiveCBridgeableE0RzlF),
4343
&MANGLE_SYM(10Foundation26_ObjectiveCBridgeableErrorMp)

0 commit comments

Comments
 (0)