Skip to content

Commit a7b8184

Browse files
authored
Merge pull request #9189 from slavapestov/anyobject-removal-vol-3
AnyObject removal volume 3
2 parents 707cecb + a77f2f2 commit a7b8184

File tree

6 files changed

+67
-89
lines changed

6 files changed

+67
-89
lines changed

include/swift/Reflection/Records.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "swift/Basic/RelativePointer.h"
2121

22-
const uint16_t SWIFT_REFLECTION_METADATA_VERSION = 2; // use new mangling
22+
const uint16_t SWIFT_REFLECTION_METADATA_VERSION = 3; // superclass field
2323

2424
namespace swift {
2525
namespace reflection {
@@ -145,6 +145,7 @@ class FieldDescriptor {
145145
}
146146

147147
const RelativeDirectPointer<const char> MangledTypeName;
148+
const RelativeDirectPointer<const char> Superclass;
148149

149150
public:
150151
FieldDescriptor() = delete;
@@ -190,6 +191,14 @@ class FieldDescriptor {
190191
std::string getMangledTypeName() const {
191192
return MangledTypeName.get();
192193
}
194+
195+
bool hasSuperclass() const {
196+
return Superclass;
197+
}
198+
199+
std::string getSuperclass() const {
200+
return Superclass.get();
201+
}
193202
};
194203

195204
class FieldDescriptorIterator

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ class TypeRefBuilder {
134134
TypeRefBuilder &operator=(const TypeRefBuilder &other) = delete;
135135

136136
private:
137+
Demangle::Demangler Dem;
138+
137139
/// Makes sure dynamically allocated TypeRefs stick around for the life of
138140
/// this TypeRefBuilder and are automatically released.
139141
std::vector<std::unique_ptr<const TypeRef>> TypeRefPool;
@@ -314,8 +316,8 @@ class TypeRefBuilder {
314316
const FieldDescriptor *getFieldTypeInfo(const TypeRef *TR);
315317

316318
/// Get the parsed and substituted field types for a nominal type.
317-
std::vector<FieldTypeInfo>
318-
getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD);
319+
bool getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD,
320+
std::vector<FieldTypeInfo> &Fields);
319321

320322
/// Get the primitive type lowering for a builtin type.
321323
const BuiltinTypeDescriptor *getBuiltinTypeInfo(const TypeRef *TR);

lib/AST/DiagnosticEngine.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,18 @@ static bool isInterestingTypealias(Type type) {
323323
if (type->is<BuiltinType>())
324324
return false;
325325

326-
auto underlyingTy = aliasTy->getDecl()->getUnderlyingTypeLoc().getType();
326+
auto aliasDecl = aliasTy->getDecl();
327327

328-
// A typealias that directly points at Builtin.AnyObject is not
329-
// 'interesting', since it is in fact the AnyObject typealias in
330-
// the standard library.
331-
if (underlyingTy->isAnyObject() &&
332-
isa<ProtocolCompositionType>(underlyingTy.getPointer()))
328+
// The 'Swift.AnyObject' typealias is not 'interesting'.
329+
if (aliasDecl->getName() ==
330+
aliasDecl->getASTContext().getIdentifier("AnyObject") &&
331+
aliasDecl->getParentModule()->isStdlibModule()) {
333332
return false;
333+
}
334+
335+
auto underlyingTy = aliasDecl->getUnderlyingTypeLoc().getType();
334336

335-
if (aliasTy->getDecl()->isCompatibilityAlias())
337+
if (aliasDecl->isCompatibilityAlias())
336338
return isInterestingTypealias(underlyingTy);
337339

338340
return true;

lib/IRGen/GenReflection.cpp

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -321,43 +321,6 @@ class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
321321
}
322322
};
323323

324-
class SuperclassMetadataBuilder : public ReflectionMetadataBuilder {
325-
static const uint32_t AssociatedTypeRecordSize = 8;
326-
327-
const ClassDecl *Class;
328-
CanType Superclass;
329-
330-
void layout() override {
331-
PrettyStackTraceDecl DebugStack("emitting superclass metadata",
332-
Class);
333-
334-
auto *M = IGM.getSILModule().getSwiftModule();
335-
336-
addTypeRef(M, Class->getDeclaredType()->getCanonicalType());
337-
addTypeRef(M, IGM.Context.getAnyObjectType());
338-
339-
B.addInt32(1);
340-
B.addInt32(AssociatedTypeRecordSize);
341-
342-
auto NameGlobal = IGM.getAddrOfStringForTypeRef("super");
343-
B.addRelativeAddress(NameGlobal);
344-
addTypeRef(M, Superclass);
345-
}
346-
347-
public:
348-
SuperclassMetadataBuilder(IRGenModule &IGM,
349-
const ClassDecl *Class,
350-
CanType Superclass)
351-
: ReflectionMetadataBuilder(IGM), Class(Class),
352-
Superclass(Superclass) {}
353-
354-
llvm::GlobalVariable *emit() {
355-
auto entity = LinkEntity::forReflectionSuperclassDescriptor(Class);
356-
auto section = IGM.getAssociatedTypeMetadataSectionName();
357-
return ReflectionMetadataBuilder::emit(entity, section);
358-
}
359-
};
360-
361324
class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
362325
const uint32_t fieldRecordSize = 12;
363326
const NominalTypeDecl *NTD;
@@ -465,15 +428,23 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
465428
}
466429

467430
void layout() override {
468-
PrettyStackTraceDecl DebugStack("emitting field type metadata", NTD);
469-
auto type = NTD->getDeclaredType()->getCanonicalType();
470-
addTypeRef(NTD->getModuleContext(), type);
471-
472431
if (NTD->hasClangNode() &&
473432
!isa<ClassDecl>(NTD) &&
474433
!isa<ProtocolDecl>(NTD))
475434
return;
476435

436+
PrettyStackTraceDecl DebugStack("emitting field type metadata", NTD);
437+
auto type = NTD->getDeclaredType()->getCanonicalType();
438+
addTypeRef(NTD->getModuleContext(), type);
439+
440+
auto *CD = dyn_cast<ClassDecl>(NTD);
441+
if (CD && CD->getSuperclass()) {
442+
addTypeRef(NTD->getModuleContext(),
443+
CD->getSuperclass()->getCanonicalType());
444+
} else {
445+
B.addInt32(0);
446+
}
447+
477448
switch (NTD->getKind()) {
478449
case DeclKind::Class:
479450
case DeclKind::Struct:
@@ -992,21 +963,6 @@ void IRGenModule::emitFieldMetadataRecord(const NominalTypeDecl *Decl) {
992963

993964
FieldTypeMetadataBuilder builder(*this, Decl);
994965
builder.emit();
995-
996-
// So that -parse-stdlib tests don't need to define an AnyObject
997-
// protocol (which will go away one day anyway).
998-
if (!Context.getProtocol(KnownProtocolKind::AnyObject))
999-
return;
1000-
1001-
// If this is a class declaration with a superclass, record the
1002-
// superclass as a special associated type named 'super' on the
1003-
// 'AnyObject' protocol.
1004-
if (auto Superclass = Decl->getDeclaredInterfaceType()
1005-
->getSuperclass()) {
1006-
SuperclassMetadataBuilder builder(*this, cast<ClassDecl>(Decl),
1007-
Superclass->getCanonicalType());
1008-
builder.emit();
1009-
}
1010966
}
1011967

1012968
void IRGenModule::emitReflectionMetadataVersion() {

stdlib/public/Reflection/TypeLowering.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,13 @@ class EnumTypeInfoBuilder {
878878
unsigned NoPayloadCases = 0;
879879
std::vector<FieldTypeInfo> PayloadCases;
880880

881-
for (auto Case : TC.getBuilder().getFieldTypeRefs(TR, FD)) {
881+
std::vector<FieldTypeInfo> Fields;
882+
if (!TC.getBuilder().getFieldTypeRefs(TR, FD, Fields)) {
883+
Invalid = true;
884+
return nullptr;
885+
}
886+
887+
for (auto Case : Fields) {
882888
if (Case.TR == nullptr) {
883889
NoPayloadCases++;
884890
continue;
@@ -1024,7 +1030,12 @@ class LowerType
10241030
// Lower the struct's fields using substitutions from the
10251031
// TypeRef to make field types concrete.
10261032
RecordTypeInfoBuilder builder(TC, RecordKind::Struct);
1027-
for (auto Field : TC.getBuilder().getFieldTypeRefs(TR, FD))
1033+
1034+
std::vector<FieldTypeInfo> Fields;
1035+
if (!TC.getBuilder().getFieldTypeRefs(TR, FD, Fields))
1036+
return nullptr;
1037+
1038+
for (auto Field : Fields)
10281039
builder.addField(Field.Name, Field.TR);
10291040
return builder.build();
10301041
}
@@ -1266,11 +1277,15 @@ const TypeInfo *TypeConverter::getClassInstanceTypeInfo(const TypeRef *TR,
12661277
// TypeRef to make field types concrete.
12671278
RecordTypeInfoBuilder builder(*this, RecordKind::ClassInstance);
12681279

1280+
std::vector<FieldTypeInfo> Fields;
1281+
if (!getBuilder().getFieldTypeRefs(TR, FD, Fields))
1282+
return nullptr;
1283+
12691284
// Start layout from the given instance start offset. This should
12701285
// be the superclass instance size.
12711286
builder.addField(start, 1, /*numExtraInhabitants=*/0);
12721287

1273-
for (auto Field : getBuilder().getFieldTypeRefs(TR, FD))
1288+
for (auto Field : Fields)
12741289
builder.addField(Field.Name, Field.TR);
12751290
return builder.build();
12761291
}

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ lookupTypeWitness(const std::string &MangledTypeName,
4949
continue;
5050

5151
std::string ProtocolMangledName(AssocTyDescriptor.ProtocolTypeName);
52-
Demangle::Demangler Dem;
5352
auto DemangledProto = Dem.demangleType(ProtocolMangledName);
5453
auto TR = swift::remote::decodeMangledType(*this, DemangledProto);
5554

@@ -82,19 +81,16 @@ lookupSuperclass(const std::string &MangledTypeName) {
8281

8382
const TypeRef * TypeRefBuilder::
8483
lookupSuperclass(const TypeRef *TR) {
85-
const TypeRef *Superclass = nullptr;
86-
87-
if (auto *Nominal = dyn_cast<NominalTypeRef>(TR)) {
88-
Superclass = lookupSuperclass(Nominal->getMangledName());
89-
} else {
90-
auto BG = cast<BoundGenericTypeRef>(TR);
91-
Superclass = lookupSuperclass(BG->getMangledName());
92-
}
84+
auto *FD = getFieldTypeInfo(TR);
85+
if (FD == nullptr)
86+
return nullptr;
9387

94-
if (Superclass == nullptr)
88+
auto Demangled = Dem.demangleType(FD->getSuperclass());
89+
auto Unsubstituted = swift::remote::decodeMangledType(*this, Demangled);
90+
if (!Unsubstituted)
9591
return nullptr;
9692

97-
return Superclass->subst(*this, TR->getSubstMap());
93+
return Unsubstituted->subst(*this, TR->getSubstMap());
9894
}
9995

10096
const FieldDescriptor *
@@ -124,15 +120,15 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
124120
return nullptr;
125121
}
126122

127-
std::vector<FieldTypeInfo>
128-
TypeRefBuilder::getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD) {
123+
124+
bool TypeRefBuilder::getFieldTypeRefs(const TypeRef *TR,
125+
const FieldDescriptor *FD,
126+
std::vector<FieldTypeInfo> &Fields) {
129127
if (FD == nullptr)
130-
return {};
128+
return false;
131129

132130
auto Subs = TR->getSubstMap();
133131

134-
Demangle::Demangler Dem;
135-
std::vector<FieldTypeInfo> Fields;
136132
for (auto &Field : *FD) {
137133
auto FieldName = Field.getFieldName();
138134

@@ -145,7 +141,7 @@ TypeRefBuilder::getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD) {
145141
auto Demangled = Dem.demangleType(Field.getMangledTypeName());
146142
auto Unsubstituted = swift::remote::decodeMangledType(*this, Demangled);
147143
if (!Unsubstituted)
148-
return {};
144+
return false;
149145

150146
auto Substituted = Unsubstituted->subst(*this, Subs);
151147

@@ -156,7 +152,7 @@ TypeRefBuilder::getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD) {
156152

157153
Fields.push_back(FieldTypeInfo::forField(FieldName, Substituted));
158154
}
159-
return Fields;
155+
return true;
160156
}
161157

162158
const BuiltinTypeDescriptor *
@@ -208,7 +204,6 @@ ClosureContextInfo
208204
TypeRefBuilder::getClosureContextInfo(const CaptureDescriptor &CD) {
209205
ClosureContextInfo Info;
210206

211-
Demangle::Demangler Dem;
212207
for (auto i = CD.capture_begin(), e = CD.capture_end(); i != e; ++i) {
213208
const TypeRef *TR = nullptr;
214209
if (i->hasMangledTypeName()) {
@@ -251,7 +246,6 @@ TypeRefBuilder::dumpTypeRef(const std::string &MangledName,
251246
auto TypeName = Demangle::demangleTypeAsString(MangledName);
252247
OS << TypeName << '\n';
253248

254-
Demangle::Demangler Dem;
255249
auto DemangleTree = Dem.demangleType(MangledName);
256250
auto TR = swift::remote::decodeMangledType(*this, DemangleTree);
257251
if (!TR) {

0 commit comments

Comments
 (0)