Skip to content

Commit aae9212

Browse files
committed
[NFC] Refactor objcImpl matadata layout logic
Rather than having individual methods return without doing anything, arrange for ClassMetadataVisitor to never call them in the first place. This makes it so ClassMetadataScanner also knows which fields are omitted and it can compute offsets correctly. The old logic is still present for field offsets to make this change NFC.
1 parent d3540b4 commit aae9212

File tree

2 files changed

+35
-48
lines changed

2 files changed

+35
-48
lines changed

lib/IRGen/ClassMetadataVisitor.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SWIFT_IRGEN_CLASSMETADATAVISITOR_H
2020

2121
#include "swift/AST/ASTContext.h"
22+
#include "swift/AST/Decl.h"
2223
#include "swift/AST/SubstitutionMap.h"
2324
#include "swift/SIL/SILDeclRef.h"
2425
#include "swift/SIL/SILModule.h"
@@ -67,6 +68,9 @@ template <class Impl> class ClassMetadataVisitor
6768
: super(IGM), Target(target), VTable(vtable) {}
6869

6970
public:
71+
bool isPureObjC() const {
72+
return Target->getObjCImplementationDecl();
73+
}
7074

7175
// Layout in embedded mode while considering the class type.
7276
// This is important for adding the right superclass pointer.
@@ -93,6 +97,17 @@ template <class Impl> class ClassMetadataVisitor
9397
return;
9498
}
9599

100+
if (isPureObjC()) {
101+
assert(IGM.ObjCInterop);
102+
asImpl().noteAddressPoint();
103+
asImpl().addMetadataFlags();
104+
asImpl().addSuperclass();
105+
asImpl().addClassCacheData();
106+
asImpl().addClassDataPointer();
107+
addClassMembers(Target, Target);
108+
return;
109+
}
110+
96111
// Pointer to layout string
97112
asImpl().addLayoutStringPointer();
98113

@@ -206,8 +221,10 @@ template <class Impl> class ClassMetadataVisitor
206221
rootClass))
207222
return;
208223

209-
// Add vtable entries.
210-
asImpl().addVTableEntries(theClass);
224+
if (!isPureObjC()) {
225+
// Add vtable entries.
226+
asImpl().addVTableEntries(theClass);
227+
}
211228
}
212229

213230
/// Add fields associated with the given class and its bases.

lib/IRGen/GenMeta.cpp

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3954,6 +3954,7 @@ namespace {
39543954

39553955
public:
39563956
const ClassLayout &getFieldLayout() const { return FieldLayout; }
3957+
using super::isPureObjC;
39573958

39583959
SILType getLoweredType() {
39593960
return IGM.getLoweredType(Target->getDeclaredTypeInContext());
@@ -3967,9 +3968,7 @@ namespace {
39673968
ClassFlags getClassFlags() { return ::getClassFlags(Target); }
39683969

39693970
void addClassFlags() {
3970-
if (asImpl().getFieldLayout().hasObjCImplementation())
3971-
return;
3972-
3971+
assert(!isPureObjC());
39733972
B.addInt32((uint32_t)asImpl().getClassFlags());
39743973
}
39753974

@@ -4005,9 +4004,7 @@ namespace {
40054004
}
40064005

40074006
void addValueWitnessTable() {
4008-
if (asImpl().getFieldLayout().hasObjCImplementation())
4009-
return;
4010-
4007+
assert(!isPureObjC());
40114008
B.add(asImpl().getValueWitnessTable(false).getValue());
40124009
}
40134010

@@ -4125,9 +4122,7 @@ namespace {
41254122
}
41264123

41274124
void addLayoutStringPointer() {
4128-
if (asImpl().getFieldLayout().hasObjCImplementation())
4129-
return;
4130-
4125+
assert(!isPureObjC());
41314126
if (auto *layoutString = getLayoutString()) {
41324127
B.addSignedPointer(layoutString,
41334128
IGM.getOptions().PointerAuth.TypeLayoutString,
@@ -4152,8 +4147,7 @@ namespace {
41524147
return;
41534148
}
41544149

4155-
if (asImpl().getFieldLayout().hasObjCImplementation())
4156-
return;
4150+
assert(!isPureObjC());
41574151

41584152
if (auto ptr = getAddrOfDestructorFunction(IGM, Target)) {
41594153
B.addSignedPointer(*ptr,
@@ -4184,8 +4178,7 @@ namespace {
41844178
return;
41854179
}
41864180

4187-
if (asImpl().getFieldLayout().hasObjCImplementation())
4188-
return;
4181+
assert(!isPureObjC());
41894182

41904183
auto dtorFunc = IGM.getAddrOfIVarInitDestroy(Target,
41914184
/*isDestroyer=*/ true,
@@ -4209,9 +4202,7 @@ namespace {
42094202
}
42104203

42114204
void addNominalTypeDescriptor() {
4212-
if (asImpl().getFieldLayout().hasObjCImplementation())
4213-
return;
4214-
4205+
assert(!isPureObjC());
42154206
B.addSignedPointer(asImpl().getNominalTypeDescriptor(),
42164207
IGM.getOptions().PointerAuth.TypeDescriptors,
42174208
PointerAuthEntity::Special::TypeDescriptor);
@@ -4226,9 +4217,7 @@ namespace {
42264217
}
42274218

42284219
void addInstanceAddressPoint() {
4229-
if (asImpl().getFieldLayout().hasObjCImplementation())
4230-
return;
4231-
4220+
assert(!isPureObjC());
42324221
// Right now, we never allocate fields before the address point.
42334222
B.addInt32(0);
42344223
}
@@ -4238,9 +4227,7 @@ namespace {
42384227
const ClassLayout &getFieldLayout() { return FieldLayout; }
42394228

42404229
void addInstanceSize() {
4241-
if (asImpl().getFieldLayout().hasObjCImplementation())
4242-
return;
4243-
4230+
assert(!isPureObjC());
42444231
if (asImpl().hasFixedLayout()) {
42454232
B.addInt32(asImpl().getFieldLayout().getSize().getValue());
42464233
} else {
@@ -4250,9 +4237,7 @@ namespace {
42504237
}
42514238

42524239
void addInstanceAlignMask() {
4253-
if (asImpl().getFieldLayout().hasObjCImplementation())
4254-
return;
4255-
4240+
assert(!isPureObjC());
42564241
if (asImpl().hasFixedLayout()) {
42574242
B.addInt16(asImpl().getFieldLayout().getAlignMask().getValue());
42584243
} else {
@@ -4262,24 +4247,18 @@ namespace {
42624247
}
42634248

42644249
void addRuntimeReservedBits() {
4265-
if (asImpl().getFieldLayout().hasObjCImplementation())
4266-
return;
4267-
4250+
assert(!isPureObjC());
42684251
B.addInt16(0);
42694252
}
42704253

42714254
void addClassSize() {
4272-
if (asImpl().getFieldLayout().hasObjCImplementation())
4273-
return;
4274-
4255+
assert(!isPureObjC());
42754256
auto size = MetadataLayout.getSize();
42764257
B.addInt32(size.FullSize.getValue());
42774258
}
42784259

42794260
void addClassAddressPoint() {
4280-
if (asImpl().getFieldLayout().hasObjCImplementation())
4281-
return;
4282-
4261+
assert(!isPureObjC());
42834262
// FIXME: Wrong
42844263
auto size = MetadataLayout.getSize();
42854264
B.addInt32(size.AddressPoint.getValue());
@@ -4516,12 +4495,12 @@ namespace {
45164495
: IGM(IGM), Target(theClass), B(builder), FieldLayout(fieldLayout) {}
45174496

45184497
llvm::Constant *emitNominalTypeDescriptor() {
4519-
if (FieldLayout.hasObjCImplementation())
4520-
return nullptr;
45214498
return ClassContextDescriptorBuilder(IGM, Target, RequireMetadata).emit();
45224499
}
45234500

45244501
void layout() {
4502+
assert(!FieldLayout.hasObjCImplementation()
4503+
&& "Resilient class metadata not supported for @objcImpl");
45254504
emitNominalTypeDescriptor();
45264505

45274506
addRelocationFunction();
@@ -4544,17 +4523,11 @@ namespace {
45444523
}
45454524

45464525
void addDestructorFunction() {
4547-
if (FieldLayout.hasObjCImplementation())
4548-
return;
4549-
45504526
auto function = getAddrOfDestructorFunction(IGM, Target);
45514527
B.addCompactFunctionReferenceOrNull(function ? *function : nullptr);
45524528
}
45534529

45544530
void addIVarDestroyer() {
4555-
if (FieldLayout.hasObjCImplementation())
4556-
return;
4557-
45584531
auto function = IGM.getAddrOfIVarInitDestroy(Target,
45594532
/*isDestroyer=*/ true,
45604533
/*isForeign=*/ false,
@@ -4563,9 +4536,6 @@ namespace {
45634536
}
45644537

45654538
void addClassFlags() {
4566-
if (FieldLayout.hasObjCImplementation())
4567-
return;
4568-
45694539
B.addInt32((uint32_t) getClassFlags(Target));
45704540
}
45714541

@@ -4628,7 +4598,7 @@ namespace {
46284598
// @_objcImplementation on true (non-ObjC) generic classes doesn't make
46294599
// much sense, and we haven't updated this builder to handle it.
46304600
assert(!FieldLayout.hasObjCImplementation()
4631-
&& "@_objcImplementation class with generic metadata?");
4601+
&& "Generic metadata not supported for @objcImpl");
46324602

46334603
super::layoutHeader();
46344604

0 commit comments

Comments
 (0)