Skip to content

Commit 17ddc58

Browse files
authored
Merge pull request #10965 from ahoppen/deinit-special-name
2 parents 23b99ef + ebd701c commit 17ddc58

26 files changed

+117
-49
lines changed

include/swift/AST/Decl.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4866,8 +4866,6 @@ class AbstractFunctionDecl : public ValueDecl, public GenericContext {
48664866
}
48674867

48684868
public:
4869-
Identifier getName() const { return getFullName().getBaseIdentifier(); }
4870-
48714869
/// Returns the string for the base name, or "_" if this is unnamed.
48724870
StringRef getNameStr() const {
48734871
assert(!getFullName().isSpecial() && "Cannot get string for special names");
@@ -5200,6 +5198,8 @@ class FuncDecl final : public AbstractFunctionDecl,
52005198
TypeLoc FnRetType, DeclContext *Parent,
52015199
ClangNode ClangN = ClangNode());
52025200

5201+
Identifier getName() const { return getFullName().getBaseIdentifier(); }
5202+
52035203
bool isStatic() const {
52045204
return FuncDeclBits.IsStatic;
52055205
}
@@ -5648,6 +5648,8 @@ class ConstructorDecl : public AbstractFunctionDecl {
56485648
GenericParamList *GenericParams,
56495649
DeclContext *Parent);
56505650

5651+
Identifier getName() const { return getFullName().getBaseIdentifier(); }
5652+
56515653
void setParameterLists(ParamDecl *selfParam, ParameterList *bodyParams);
56525654

56535655
SourceLoc getConstructorLoc() const { return getNameLoc(); }
@@ -5839,8 +5841,8 @@ class ConstructorDecl : public AbstractFunctionDecl {
58395841
class DestructorDecl : public AbstractFunctionDecl {
58405842
ParameterList *SelfParameter;
58415843
public:
5842-
DestructorDecl(Identifier NameHack, SourceLoc DestructorLoc,
5843-
ParamDecl *selfDecl, DeclContext *Parent);
5844+
DestructorDecl(SourceLoc DestructorLoc, ParamDecl *selfDecl,
5845+
DeclContext *Parent);
58445846

58455847
void setSelfDecl(ParamDecl *selfDecl);
58465848

include/swift/AST/Identifier.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ class DeclBaseName {
214214
public:
215215
enum class Kind: uint8_t {
216216
Normal,
217-
Subscript
217+
Subscript,
218+
Destructor
218219
};
219220

220221
private:
@@ -223,6 +224,8 @@ class DeclBaseName {
223224
/// This is an implementation detail that should never leak outside of
224225
/// DeclName.
225226
static void *SubscriptIdentifierData;
227+
/// As above, for special destructor DeclNames.
228+
static void *DestructorIdentifierData;
226229

227230
Identifier Ident;
228231

@@ -235,9 +238,15 @@ class DeclBaseName {
235238
return DeclBaseName(Identifier((const char *)SubscriptIdentifierData));
236239
}
237240

241+
static DeclBaseName createDestructor() {
242+
return DeclBaseName(Identifier((const char *)DestructorIdentifierData));
243+
}
244+
238245
Kind getKind() const {
239246
if (Ident.get() == SubscriptIdentifierData) {
240247
return Kind::Subscript;
248+
} else if (Ident.get() == DestructorIdentifierData) {
249+
return Kind::Destructor;
241250
} else {
242251
return Kind::Normal;
243252
}
@@ -273,6 +282,8 @@ class DeclBaseName {
273282
return getIdentifier().str();
274283
case Kind::Subscript:
275284
return "subscript";
285+
case Kind::Destructor:
286+
return "deinit";
276287
}
277288
}
278289

include/swift/AST/KnownIdentifiers.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ IDENTIFIER(decode)
4343
IDENTIFIER(decodeIfPresent)
4444
IDENTIFIER(Decoder)
4545
IDENTIFIER(decoder)
46-
IDENTIFIER(deinit)
4746
IDENTIFIER(Element)
4847
IDENTIFIER(Encodable)
4948
IDENTIFIER(encode)

include/swift/Serialization/ModuleFormat.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 353; // Last change: count inherited conformances
57+
const uint16_t VERSION_MINOR = 354; // Last change: special destructor names
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;
@@ -345,7 +345,8 @@ using OptionalTypeKindField = BCFixed<2>;
345345
// VERSION_MAJOR.
346346
enum class DeclNameKind: uint8_t {
347347
Normal,
348-
Subscript
348+
Subscript,
349+
Destructor
349350
};
350351

351352
// These IDs must \em not be renumbered or reordered without incrementing
@@ -359,6 +360,8 @@ enum SpecialIdentifierID : uint8_t {
359360
OBJC_HEADER_MODULE_ID,
360361
/// Special value for the special subscript name
361362
SUBSCRIPT_ID,
363+
/// Special value for the special destructor name
364+
DESTRUCTOR_ID,
362365

363366
/// The number of special Identifier IDs. This value should never be encoded;
364367
/// it should only be used to count the number of names above. As such, it

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) {
548548
case DeclBaseName::Kind::Subscript:
549549
appendIdentifier("subscript");
550550
break;
551+
case DeclBaseName::Kind::Destructor:
552+
llvm_unreachable("Destructors are not mangled using appendDeclName");
551553
}
552554
} else {
553555
assert(AllowNamelessEntities && "attempt to mangle unnamed decl");

lib/AST/Decl.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ bool Decl::isPrivateStdlibDecl(bool whitelistProtocols) const {
420420
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D)) {
421421
// Hide '~>' functions (but show the operator, because it defines
422422
// precedence).
423-
if (AFD->getNameStr() == "~>")
423+
if (isa<FuncDecl>(AFD) && AFD->getNameStr() == "~>")
424424
return true;
425425

426426
// If it's a function with a parameter with leading underscore, it's a
@@ -2557,8 +2557,7 @@ ClassDecl::ClassDecl(SourceLoc ClassLoc, Identifier Name, SourceLoc NameLoc,
25572557
}
25582558

25592559
DestructorDecl *ClassDecl::getDestructor() {
2560-
auto name = getASTContext().Id_deinit;
2561-
auto results = lookupDirect(name);
2560+
auto results = lookupDirect(DeclBaseName::createDestructor());
25622561
assert(!results.empty() && "Class without destructor?");
25632562
assert(results.size() == 1 && "More than one destructor?");
25642563
return cast<DestructorDecl>(results.front());
@@ -4572,7 +4571,20 @@ ObjCSelector AbstractFunctionDecl::getObjCSelector(
45724571
}
45734572

45744573
auto &ctx = getASTContext();
4575-
auto baseName = getName();
4574+
4575+
Identifier baseName;
4576+
if (isa<DestructorDecl>(this)) {
4577+
// Deinitializers are always called "dealloc".
4578+
return ObjCSelector(ctx, 0, ctx.Id_dealloc);
4579+
} else if (auto func = dyn_cast<FuncDecl>(this)) {
4580+
// Otherwise cast this to be able to access getName()
4581+
baseName = func->getName();
4582+
} else if (auto ctor = dyn_cast<ConstructorDecl>(this)) {
4583+
baseName = ctor->getName();
4584+
} else {
4585+
llvm_unreachable("Unknown subclass of AbstractFunctionDecl");
4586+
}
4587+
45764588
auto argNames = getFullName().getArgumentNames();
45774589

45784590
// Use the preferred name if specified
@@ -4596,11 +4608,6 @@ ObjCSelector AbstractFunctionDecl::getObjCSelector(
45964608
}
45974609
}
45984610

4599-
// Deinitializers are always called "dealloc".
4600-
if (isa<DestructorDecl>(this)) {
4601-
return ObjCSelector(ctx, 0, ctx.Id_dealloc);
4602-
}
4603-
46044611

46054612
// If this is a zero-parameter initializer with a long selector
46064613
// name, form that selector.
@@ -4960,9 +4967,10 @@ bool ConstructorDecl::isObjCZeroParameterWithLongSelector() const {
49604967
return params->get(0)->getInterfaceType()->isVoid();
49614968
}
49624969

4963-
DestructorDecl::DestructorDecl(Identifier NameHack, SourceLoc DestructorLoc,
4964-
ParamDecl *selfDecl, DeclContext *Parent)
4965-
: AbstractFunctionDecl(DeclKind::Destructor, Parent, NameHack, DestructorLoc,
4970+
DestructorDecl::DestructorDecl(SourceLoc DestructorLoc, ParamDecl *selfDecl,
4971+
DeclContext *Parent)
4972+
: AbstractFunctionDecl(DeclKind::Destructor, Parent,
4973+
DeclBaseName::createDestructor(), DestructorLoc,
49664974
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
49674975
/*NumParameterLists=*/1, nullptr) {
49684976
setSelfDecl(selfDecl);

lib/AST/DeclContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
806806
break;
807807
case DeclContextKind::AbstractFunctionDecl: {
808808
auto *AFD = cast<AbstractFunctionDecl>(this);
809-
OS << " name=" << AFD->getName();
809+
OS << " name=" << AFD->getFullName();
810810
if (AFD->hasInterfaceType())
811811
OS << " : " << AFD->getInterfaceType();
812812
else

lib/AST/Identifier.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ using namespace swift;
2222

2323
void *DeclBaseName::SubscriptIdentifierData =
2424
&DeclBaseName::SubscriptIdentifierData;
25+
void *DeclBaseName::DestructorIdentifierData =
26+
&DeclBaseName::DestructorIdentifierData;
2527

2628
raw_ostream &llvm::operator<<(raw_ostream &OS, Identifier I) {
2729
if (I.get() == nullptr)

lib/ClangImporter/SwiftLookupTable.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ DeclBaseName SerializedSwiftName::toDeclBaseName(ASTContext &Context) const {
211211
return Context.getIdentifier(Name);
212212
case DeclBaseName::Kind::Subscript:
213213
return DeclBaseName::createSubscript();
214+
case DeclBaseName::Kind::Destructor:
215+
return DeclBaseName::createDestructor();
214216
}
215217
}
216218

@@ -829,6 +831,9 @@ void SwiftLookupTable::dump() const {
829831
case DeclBaseName::Kind::Subscript:
830832
llvm::errs() << " subscript:\n";
831833
break;
834+
case DeclBaseName::Kind::Destructor:
835+
llvm::errs() << " deinit:\n";
836+
break;
832837
}
833838
const auto &entries = LookupTable.find(baseName)->second;
834839
for (const auto &entry : entries) {

lib/ClangImporter/SwiftLookupTable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct SerializedSwiftName {
8282
return Name;
8383
case DeclBaseName::Kind::Subscript:
8484
return "subscript";
85+
case DeclBaseName::Kind::Destructor:
86+
return "deinit";
8587
}
8688
}
8789

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5792,8 +5792,8 @@ parseDeclDeinit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
57925792
auto *SelfDecl = ParamDecl::createUnboundSelf(DestructorLoc, CurDeclContext);
57935793

57945794
Scope S(this, ScopeKind::DestructorBody);
5795-
auto *DD = new (Context) DestructorDecl(Context.Id_deinit, DestructorLoc,
5796-
SelfDecl, CurDeclContext);
5795+
auto *DD = new (Context) DestructorDecl(DestructorLoc, SelfDecl,
5796+
CurDeclContext);
57975797

57985798
// Parse the body.
57995799
if (Tok.is(tok::l_brace)) {

lib/ParseSIL/ParseSIL.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,6 @@ bool SILParser::parseSILIdentifier(Identifier &Result, SourceLoc &Loc,
434434
// A binary operator can be part of a SILDeclRef.
435435
Result = P.Context.getIdentifier(P.Tok.getText());
436436
break;
437-
case tok::kw_deinit:
438-
Result = P.Context.Id_deinit;
439-
break;
440437
case tok::kw_init:
441438
Result = P.Context.Id_init;
442439
break;
@@ -1148,6 +1145,10 @@ bool SILParser::parseSILDottedPathWithoutPound(ValueDecl *&Decl,
11481145
P.consumeToken();
11491146
FullName.push_back(DeclBaseName::createSubscript());
11501147
break;
1148+
case tok::kw_deinit:
1149+
P.consumeToken();
1150+
FullName.push_back(DeclBaseName::createDestructor());
1151+
break;
11511152
default:
11521153
if (parseSILIdentifier(Id, diag::expected_sil_constant))
11531154
return true;

lib/SIL/SILFunctionType.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,8 @@ static SelectorFamily getSelectorFamily(SILDeclRef c) {
16261626
return getSelectorFamily(declName.getIdentifier());
16271627
case DeclBaseName::Kind::Subscript:
16281628
return SelectorFamily::None;
1629+
case DeclBaseName::Kind::Destructor:
1630+
return SelectorFamily::None;
16291631
}
16301632
}
16311633
return SelectorFamily::None;

lib/SIL/SILPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ static void printValueDecl(ValueDecl *Decl, raw_ostream &OS) {
239239

240240
if (Decl->isOperator()) {
241241
OS << '"' << Decl->getBaseName() << '"';
242-
} else if (Decl->getBaseName() == "subscript") {
242+
} else if (Decl->getBaseName() == "subscript" ||
243+
Decl->getBaseName() == "deinit") {
243244
OS << '`' << Decl->getBaseName() << '`';
244245
} else {
245246
OS << Decl->getBaseName();

lib/Sema/CSDiag.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,9 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
25322532
if (memberName.getBaseName().getKind() == DeclBaseName::Kind::Subscript) {
25332533
diagnose(loc, diag::type_not_subscriptable, baseObjTy)
25342534
.highlight(baseRange);
2535+
} else if (memberName.getBaseName() == "deinit") {
2536+
// Specialised diagnostic if trying to access deinitialisers
2537+
diagnose(loc, diag::destructor_not_accessible).highlight(baseRange);
25352538
} else if (auto metatypeTy = baseObjTy->getAs<MetatypeType>()) {
25362539
auto instanceTy = metatypeTy->getInstanceType();
25372540
tryTypoCorrection();
@@ -2691,10 +2694,6 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
26912694

26922695
return;
26932696
}
2694-
case MemberLookupResult::UR_DestructorInaccessible: {
2695-
diagnose(nameLoc, diag::destructor_not_accessible);
2696-
return;
2697-
}
26982697
}
26992698
}
27002699

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,11 +3206,6 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
32063206
// reasonable choice.
32073207
auto addChoice = [&](ValueDecl *cand, bool isBridged,
32083208
bool isUnwrappedOptional) {
3209-
// Destructors cannot be referenced manually
3210-
if (isa<DestructorDecl>(cand)) {
3211-
result.addUnviable(cand, MemberLookupResult::UR_DestructorInaccessible);
3212-
return;
3213-
}
32143209
// If the result is invalid, skip it.
32153210
TC.validateDecl(cand);
32163211
if (cand->isInvalid()) {

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,8 +2185,7 @@ void TypeChecker::addImplicitDestructor(ClassDecl *CD) {
21852185

21862186
auto *selfDecl = ParamDecl::createSelf(CD->getLoc(), CD);
21872187

2188-
auto *DD = new (Context) DestructorDecl(Context.Id_deinit, CD->getLoc(),
2189-
selfDecl, CD);
2188+
auto *DD = new (Context) DestructorDecl(CD->getLoc(), selfDecl, CD);
21902189

21912190
DD->setImplicit();
21922191

lib/Sema/ConstraintSystem.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,6 @@ struct MemberLookupResult {
840840

841841
/// The member is inaccessible (e.g. a private member in another file).
842842
UR_Inaccessible,
843-
844-
// A type's destructor cannot be referenced
845-
UR_DestructorInaccessible,
846843
};
847844

848845
/// This is a list of considered, but rejected, candidates, along with a

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4969,12 +4969,10 @@ static void recordConformanceDependency(DeclContext *DC,
49694969
Conformance->getDeclContext()->getParentModule())
49704970
return;
49714971

4972-
auto &Context = DC->getASTContext();
4973-
49744972
// FIXME: 'deinit' is being used as a dummy identifier here. Really we
49754973
// don't care about /any/ of the type's members, only that it conforms to
49764974
// the protocol.
4977-
tracker->addUsedMember({Adoptee, Context.Id_deinit},
4975+
tracker->addUsedMember({Adoptee, DeclBaseName::createDestructor()},
49784976
DC->isCascadingContextForLookup(InExpression));
49794977
}
49804978

lib/Serialization/Deserialization.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,8 @@ DeclBaseName ModuleFile::getDeclBaseName(IdentifierID IID) {
16231623
llvm_unreachable("Cannot get DeclBaseName of special module id");
16241624
case SUBSCRIPT_ID:
16251625
return DeclBaseName::createSubscript();
1626+
case serialization::DESTRUCTOR_ID:
1627+
return DeclBaseName::createDestructor();
16261628
case NUM_SPECIAL_IDS:
16271629
llvm_unreachable("implementation detail only");
16281630
}
@@ -1805,6 +1807,7 @@ ModuleDecl *ModuleFile::getModule(ModuleID MID) {
18051807
return clangImporter->getImportedHeaderModule();
18061808
}
18071809
case SUBSCRIPT_ID:
1810+
case DESTRUCTOR_ID:
18081811
llvm_unreachable("Modules cannot be named with special names");
18091812
case NUM_SPECIAL_IDS:
18101813
llvm_unreachable("implementation detail only");
@@ -3550,8 +3553,7 @@ ModuleFile::getDeclChecked(DeclID DID, Optional<DeclContext *> ForcedContext) {
35503553
if (declOrOffset.isComplete())
35513554
return declOrOffset;
35523555

3553-
auto dtor = createDecl<DestructorDecl>(ctx.Id_deinit, SourceLoc(),
3554-
/*selfpat*/nullptr, DC);
3556+
auto dtor = createDecl<DestructorDecl>(SourceLoc(), /*selfpat*/nullptr, DC);
35553557
declOrOffset = dtor;
35563558

35573559
configureGenericEnvironment(dtor, genericEnvID);

lib/Serialization/ModuleFile.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ class ModuleFile::DeclTableInfo {
341341
}
342342
case static_cast<uint8_t>(DeclNameKind::Subscript):
343343
return {DeclBaseName::Kind::Subscript, StringRef()};
344+
case static_cast<uint8_t>(DeclNameKind::Destructor):
345+
return {DeclBaseName::Kind::Destructor, StringRef()};
344346
default:
345347
llvm_unreachable("Unknown DeclNameKind");
346348
}

0 commit comments

Comments
 (0)