Skip to content

Commit bd70bbe

Browse files
committed
[wip][cxx-interop] Start fixing issue with layout of FRTs that contain base classes or un-importable memebers.
1 parent 992cae4 commit bd70bbe

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

lib/IRGen/GenClass.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
#include "GenClass.h"
1818

19+
#include "clang/AST/ASTContext.h"
20+
#include "clang/AST/Decl.h"
21+
#include "clang/AST/RecordLayout.h"
1922
#include "swift/ABI/Class.h"
2023
#include "swift/ABI/MetadataValues.h"
2124
#include "swift/AST/ASTContext.h"
@@ -281,6 +284,42 @@ namespace {
281284
superclass);
282285
}
283286

287+
void maybeAddCxxRecordBases(ClassDecl *cd) {
288+
auto cxxRecord = dyn_cast_or_null<clang::CXXRecordDecl>(cd->getClangDecl());
289+
if (!cxxRecord)
290+
return;
291+
292+
auto &layout = cxxRecord->getASTContext().getASTRecordLayout(cxxRecord);
293+
294+
for (auto base : cxxRecord->bases()) {
295+
if (base.isVirtual())
296+
continue;
297+
298+
auto baseType = base.getType().getCanonicalType();
299+
300+
auto baseRecord = cast<clang::RecordType>(baseType)->getDecl();
301+
auto baseCxxRecord = cast<clang::CXXRecordDecl>(baseRecord);
302+
303+
if (baseCxxRecord->isEmpty())
304+
continue;
305+
306+
auto offset = Size(layout.getBaseClassOffset(baseCxxRecord).getQuantity());
307+
auto size = Size(cxxRecord->getASTContext().getTypeSizeInChars(baseType).getQuantity());
308+
309+
if (offset != CurSize) {
310+
assert(offset > CurSize);
311+
auto paddingSize = offset - CurSize;
312+
auto &opaqueTI = IGM.getOpaqueStorageTypeInfo(paddingSize, Alignment(1));
313+
auto element = ElementLayout::getIncomplete(opaqueTI);
314+
addField(element, LayoutStrategy::Universal);
315+
}
316+
317+
auto &opaqueTI = IGM.getOpaqueStorageTypeInfo(size, Alignment(1));
318+
auto element = ElementLayout::getIncomplete(opaqueTI);
319+
addField(element, LayoutStrategy::Universal);
320+
}
321+
}
322+
284323
void addDirectFieldsFromClass(ClassDecl *rootClass, SILType rootClassType,
285324
ClassDecl *theClass, SILType classType,
286325
bool superclass) {
@@ -290,6 +329,8 @@ namespace {
290329
->getRecursiveProperties()
291330
.hasUnboundGeneric());
292331

332+
maybeAddCxxRecordBases(theClass);
333+
293334
forEachField(IGM, theClass, [&](Field field) {
294335
// Ignore missing properties here; we should have flagged these
295336
// with the classHasIncompleteLayout call above.

0 commit comments

Comments
 (0)