Skip to content

Commit 2e66b84

Browse files
committed
[cxx-interop] Ensure field offset vector matches the structs/fields metadata
The field offset vector should be consistent with the type metadata emitted for structs and fields. If we don't emit metadata for private C++ fields, then the field offset vector shouldn't include these fields. This is a follow-up to #81838 (cherry picked from c3077bf and 7686f05)
1 parent fbd2b8d commit 2e66b84

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/IRGen/GenMeta.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3157,7 +3157,7 @@ emitInitializeFieldOffsetVector(SILType T, llvm::Value *metadata,
31573157
}
31583158

31593159
// Collect the stored properties of the type.
3160-
unsigned numFields = getNumFields(target);
3160+
unsigned numFields = countExportableFields(IGM, target);
31613161

31623162
// Fill out an array with the field type metadata records.
31633163
Address fields = createAlloca(
@@ -3170,6 +3170,9 @@ emitInitializeFieldOffsetVector(SILType T, llvm::Value *metadata,
31703170
forEachField(IGM, target, [&](Field field) {
31713171
assert(field.isConcrete() &&
31723172
"initializing offset vector for type with missing member?");
3173+
if (!isExportableField(field))
3174+
return;
3175+
31733176
SILType propTy = field.getType(IGM, T);
31743177
llvm::Value *fieldLayout = emitTypeLayoutRef(*this, propTy, collector);
31753178
Address fieldLayoutAddr =
@@ -3277,7 +3280,7 @@ static void emitInitializeFieldOffsetVectorWithLayoutString(
32773280
emitAddressOfFieldOffsetVector(IGF, metadata, target).getAddress();
32783281

32793282
// Collect the stored properties of the type.
3280-
unsigned numFields = getNumFields(target);
3283+
unsigned numFields = countExportableFields(IGM, target);
32813284

32823285
// Ask the runtime to lay out the struct or class.
32833286
auto numFieldsV = IGM.getSize(Size(numFields));
@@ -3300,6 +3303,9 @@ static void emitInitializeFieldOffsetVectorWithLayoutString(
33003303
forEachField(IGM, target, [&](Field field) {
33013304
assert(field.isConcrete() &&
33023305
"initializing offset vector for type with missing member?");
3306+
if (!isExportableField(field))
3307+
return;
3308+
33033309
SILType propTy = field.getType(IGM, T);
33043310
llvm::Value *fieldMetatype;
33053311
llvm::Value *fieldTag;

lib/IRGen/StructMetadataVisitor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_IRGEN_STRUCTMETADATALAYOUT_H
1818
#define SWIFT_IRGEN_STRUCTMETADATALAYOUT_H
1919

20+
#include "Field.h"
2021
#include "NominalMetadataVisitor.h"
2122
#include "swift/AST/IRGenOptions.h"
2223

@@ -64,8 +65,7 @@ template <class Impl> class StructMetadataVisitor
6465
// Struct field offsets.
6566
asImpl().noteStartOfFieldOffsets();
6667
for (VarDecl *prop : Target->getStoredProperties()) {
67-
if (!(prop->getClangDecl() &&
68-
prop->getFormalAccess() == AccessLevel::Private))
68+
if (isExportableField(prop))
6969
asImpl().addFieldOffset(prop);
7070
}
7171

0 commit comments

Comments
 (0)