Skip to content

Commit 3b07eda

Browse files
committed
[Mangler] Use '_' to represent nameless entities.
This can show up when trying to generate USRs for a document with errors in it. This isn't a great answer because the names it generates aren't unique (there may be more than one nameless entity with the same type), but it at least generates valid mangled names. When generating mangled names for purposes other than USRs, nameless entities are now checked for by an assertion.
1 parent 5c80f1e commit 3b07eda

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class ASTMangler : public Mangler {
3838
/// If enabled, Arche- and Alias types are mangled with context.
3939
bool DWARFMangling;
4040

41+
/// If enabled, entities that ought to have names but don't get a placeholder.
42+
///
43+
/// If disabled, it is an error to try to mangle such an entity.
44+
bool AllowNamelessEntities = false;
45+
4146
public:
4247
enum class SymbolKind {
4348
Default,

lib/AST/ASTMangler.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,14 @@ std::string ASTMangler::mangleObjCRuntimeName(const NominalTypeDecl *Nominal) {
327327
}
328328

329329
std::string ASTMangler::mangleTypeAsContextUSR(const NominalTypeDecl *type) {
330+
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
330331
appendContext(type);
331332
return finalize();
332333
}
333334

334-
std::string ASTMangler::mangleDeclAsUSR(ValueDecl *Decl, StringRef USRPrefix) {
335+
std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
336+
StringRef USRPrefix) {
337+
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
335338
Buffer << USRPrefix;
336339
bindGenericParameters(Decl->getDeclContext());
337340

@@ -354,6 +357,7 @@ std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
354357
AddressorKind addressorKind,
355358
const ValueDecl *decl,
356359
StringRef USRPrefix) {
360+
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
357361
Buffer << USRPrefix;
358362
appendAccessorEntity(kind, addressorKind, decl, /*isStatic*/ false);
359363
return finalize();
@@ -444,8 +448,13 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) {
444448
appendOperator("oi");
445449
break;
446450
}
447-
} else {
451+
} else if (decl->hasName()) {
448452
appendIdentifier(decl->getName().str());
453+
} else {
454+
assert(AllowNamelessEntities && "attempt to mangle unnamed decl");
455+
// Fall back to an unlikely name, so that we still generate a valid
456+
// mangled name.
457+
appendIdentifier("_");
449458
}
450459

451460
if (decl->getDeclContext()->isLocalContext()) {

test/Demangle/Inputs/manglings.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,4 @@ _T08mangling14varargsVsArrayySaySiG3arrd_SS1ntF ---> mangling.varargsVsArray(arr
250250
_T08mangling14varargsVsArrayySaySiG3arrd_tF ---> mangling.varargsVsArray(arr: Swift.Int...) -> ()
251251
_T0s13_UnicodeViewsVss22RandomAccessCollectionRzs0A8EncodingR_11SubSequence_5IndexQZAFRtzsAcERpzAE_AEQZAIRSs15UnsignedInteger8Iterator_7ElementRPzAE_AlMQZANRS13EncodedScalar_AlMQY_AORSr0_lE13CharacterViewVyxq__G ---> (extension in Swift):Swift._UnicodeViews<A, B><A, B where A: Swift.RandomAccessCollection, B: Swift.UnicodeEncoding, A.Index == A.SubSequence.Index, A.SubSequence: Swift.RandomAccessCollection, A.SubSequence == A.SubSequence.SubSequence, A.Iterator.Element: Swift.UnsignedInteger, A.Iterator.Element == A.SubSequence.Iterator.Element, A.SubSequence.Iterator.Element == B.EncodedScalar.Iterator.Element>.CharacterView
252252
_T010Foundation11MeasurementV12SimulatorKitSo9UnitAngleCRszlE11OrientationO2eeoiSbAcDEAGOyAF_G_AKtFZ ---> static (extension in SimulatorKit):Foundation.Measurement<A where A == __ObjC.UnitAngle>.Orientation.== infix((extension in SimulatorKit):Foundation.Measurement<__ObjC.UnitAngle>.Orientation, (extension in SimulatorKit):Foundation.Measurement<__ObjC.UnitAngle>.Orientation) -> Swift.Bool
253+
_T04main1_yyF ---> main._() -> ()

test/Demangle/Inputs/simplified-manglings.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,4 @@ _TFE1a ---> _TFE1a
202202
_TFC4testP33_83378C430F65473055F1BD53F3ADCDB71C5doFoofT_T_ ---> C.doFoo()
203203
_TTRXFo_oCSo13SKPhysicsBodydVSC7CGPointdVSC8CGVectordGSpV10ObjectiveC8ObjCBool___XFdCb_dS_dS0_dS1_dGSpS3____ ---> thunk for @callee_owned (@owned SKPhysicsBody, @unowned CGPoint, @unowned CGVector, @unowned UnsafeMutablePointer<ObjCBool>) -> ()
204204
_T0So13SKPhysicsBodyCSC7CGPointVSC8CGVectorVSpy10ObjectiveC8ObjCBoolVGIxxyyy_AbdFSpyAIGIyByyyy_TR ---> thunk for @callee_owned (@owned SKPhysicsBody, @unowned CGPoint, @unowned CGVector, @unowned UnsafeMutablePointer<ObjCBool>) -> ()
205+
_T04main1_yyF ---> _()

0 commit comments

Comments
 (0)