Skip to content

Commit 3f360a7

Browse files
committed
[Mangling] Allow standard substitutions in protocol mangling.
Protocol name mangling didn’t always go through a path that allowed the use of standard substitutions. Enable standard substitutions for protocol name manglings where they make sense. Removes ~277k from the standard library binary size. (cherry picked from commit f232af5)
1 parent 7bdece3 commit 3f360a7

26 files changed

+95
-69
lines changed

docs/ABI/Mangling.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,10 @@ 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

335337
KNOWN-TYPE-KIND ::= 'A' // Swift.AutoreleasingUnsafeMutablePointer
336338
KNOWN-TYPE-KIND ::= 'a' // Swift.Array
@@ -383,6 +385,7 @@ Types
383385
KNOWN-TYPE-KIND ::= 'z' // Swift.BinaryInteger
384386

385387
protocol ::= context decl-name
388+
protocol ::= standard-substitutions
386389

387390
type ::= 'Bb' // Builtin.BridgeObject
388391
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

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)

stdlib/public/runtime/SwiftHashableSupport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
namespace swift {
2020
namespace hashable_support {
2121

22-
extern "C" const ProtocolDescriptor PROTOCOL_DESCR_SYM(s8Hashable);
23-
static constexpr auto &HashableProtocolDescriptor = PROTOCOL_DESCR_SYM(s8Hashable);
22+
extern "C" const ProtocolDescriptor PROTOCOL_DESCR_SYM(SH);
23+
static constexpr auto &HashableProtocolDescriptor = PROTOCOL_DESCR_SYM(SH);
2424

2525
struct HashableWitnessTable;
2626

0 commit comments

Comments
 (0)