-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Runtime support for foreign reference types. #59439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f972f66
[cxx-interop] Runtime support for foreign reference types.
zoecarver 81181a0
[nfc][cxx-interop] Add a few comments and asserts
zoecarver 96086a0
[nfc][cxx-interop] Disable FRT test on Windows (not yet supported).
zoecarver bdc37c9
[nfc][cxx-interop] Create and use custom metadata builder, visitor fo…
zoecarver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1688,7 +1688,7 @@ namespace { | |
VTable(IGM.getSILModule().lookUpVTable(getType())), | ||
Resilient(IGM.hasResilientMetadata(Type, ResilienceExpansion::Minimal)) { | ||
|
||
if (getType()->isForeign() || Type->isForeignReferenceType()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (This was just a redundant check.) |
||
if (getType()->isForeign()) | ||
return; | ||
|
||
MetadataLayout = &IGM.getClassMetadataLayout(Type); | ||
|
@@ -1719,7 +1719,6 @@ namespace { | |
} | ||
|
||
void layout() { | ||
assert(!getType()->isForeignReferenceType()); | ||
super::layout(); | ||
addVTable(); | ||
addOverrideTable(); | ||
|
@@ -2556,15 +2555,6 @@ void irgen::emitLazyTypeContextDescriptor(IRGenModule &IGM, | |
void irgen::emitLazyTypeMetadata(IRGenModule &IGM, NominalTypeDecl *type) { | ||
eraseExistingTypeContextDescriptor(IGM, type); | ||
|
||
// Special case, UFOs are opaque pointers for now. | ||
if (auto cd = dyn_cast<ClassDecl>(type)) { | ||
if (cd->isForeignReferenceType()) { | ||
auto sd = cast<StructDecl>(type->getASTContext().getOpaquePointerDecl()); | ||
emitStructMetadata(IGM, sd); | ||
return; | ||
} | ||
} | ||
|
||
if (requiresForeignTypeMetadata(type)) { | ||
emitForeignTypeMetadata(IGM, type); | ||
} else if (auto sd = dyn_cast<StructDecl>(type)) { | ||
|
@@ -3245,12 +3235,7 @@ static void createNonGenericMetadataAccessFunction(IRGenModule &IGM, | |
/// Emit the base-offset variable for the class. | ||
static void emitClassMetadataBaseOffset(IRGenModule &IGM, | ||
ClassDecl *classDecl) { | ||
if (classDecl->isForeignReferenceType()) { | ||
classDecl->getASTContext().Diags.diagnose( | ||
classDecl->getLoc(), diag::foreign_reference_types_unsupported.ID, | ||
{}); | ||
exit(1); | ||
} | ||
assert(!classDecl->isForeignReferenceType()); | ||
|
||
// Otherwise, we know the offset at compile time, even if our | ||
// clients do not, so just emit a constant. | ||
|
@@ -5444,6 +5429,9 @@ namespace { | |
ForeignClassMetadataBuilder(IRGenModule &IGM, ClassDecl *target, | ||
ConstantStructBuilder &B) | ||
: ForeignMetadataBuilderBase(IGM, target, B) { | ||
assert(!getTargetType()->isForeignReferenceType() && | ||
"foreign reference type metadata must be built with the ForeignReferenceTypeMetadataBuilder"); | ||
|
||
if (IGM.getOptions().LazyInitializeClassMetadata) | ||
CanBeConstant = false; | ||
} | ||
|
@@ -5532,6 +5520,61 @@ namespace { | |
B.addNullPointer(IGM.Int8PtrTy); | ||
} | ||
}; | ||
|
||
class ForeignReferenceTypeMetadataBuilder; | ||
class ForeignReferenceTypeMetadataBuilderBase : | ||
public ForeignReferenceTypeMetadataVisitor<ForeignReferenceTypeMetadataBuilder> { | ||
protected: | ||
ConstantStructBuilder &B; | ||
|
||
ForeignReferenceTypeMetadataBuilderBase(IRGenModule &IGM, ClassDecl *target, | ||
ConstantStructBuilder &B) | ||
: ForeignReferenceTypeMetadataVisitor(IGM, target), B(B) {} | ||
}; | ||
|
||
/// A builder for ForeignReferenceTypeMetadata. | ||
class ForeignReferenceTypeMetadataBuilder : | ||
public ForeignMetadataBuilderBase<ForeignReferenceTypeMetadataBuilder, | ||
ForeignReferenceTypeMetadataBuilderBase> { | ||
public: | ||
ForeignReferenceTypeMetadataBuilder(IRGenModule &IGM, ClassDecl *target, | ||
ConstantStructBuilder &B) | ||
: ForeignMetadataBuilderBase(IGM, target, B) { | ||
assert(getTargetType()->isForeignReferenceType() && | ||
"foreign reference type metadata build must be used on foreign reference types."); | ||
zoecarver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (IGM.getOptions().LazyInitializeClassMetadata) | ||
CanBeConstant = false; | ||
} | ||
|
||
void emitInitializeMetadata(IRGenFunction &IGF, llvm::Value *metadata, | ||
MetadataDependencyCollector *collector) { | ||
llvm_unreachable("Not implemented for foreign reference types."); | ||
} | ||
|
||
// Visitor methods. | ||
|
||
void addValueWitnessTable() { | ||
auto type = getTargetType()->getCanonicalType(); | ||
B.add(irgen::emitValueWitnessTable(IGM, type, false, false).getValue()); | ||
} | ||
|
||
void addMetadataFlags() { | ||
B.addInt(IGM.MetadataKindTy, (unsigned) MetadataKind::ForeignReferenceType); | ||
} | ||
|
||
void addNominalTypeDescriptor() { | ||
auto descriptor = | ||
ClassContextDescriptorBuilder(this->IGM, Target, RequireMetadata).emit(); | ||
B.addSignedPointer(descriptor, | ||
IGM.getOptions().PointerAuth.TypeDescriptors, | ||
PointerAuthEntity::Special::TypeDescriptor); | ||
} | ||
|
||
void addReservedWord() { | ||
B.addNullPointer(IGM.Int8PtrTy); | ||
} | ||
}; | ||
|
||
/// A builder for ForeignStructMetadata. | ||
class ForeignStructMetadataBuilder : | ||
|
@@ -5598,8 +5641,9 @@ bool irgen::requiresForeignTypeMetadata(CanType type) { | |
|
||
bool irgen::requiresForeignTypeMetadata(NominalTypeDecl *decl) { | ||
if (auto *clazz = dyn_cast<ClassDecl>(decl)) { | ||
assert(!clazz->isForeignReferenceType()); | ||
|
||
if (clazz->isForeignReferenceType()) | ||
return true; | ||
|
||
switch (clazz->getForeignClassKind()) { | ||
case ClassDecl::ForeignKind::Normal: | ||
case ClassDecl::ForeignKind::RuntimeOnly: | ||
|
@@ -5623,16 +5667,25 @@ void irgen::emitForeignTypeMetadata(IRGenModule &IGM, NominalTypeDecl *decl) { | |
init.setPacked(true); | ||
|
||
if (auto classDecl = dyn_cast<ClassDecl>(decl)) { | ||
assert(classDecl->getForeignClassKind() == ClassDecl::ForeignKind::CFType || | ||
classDecl->isForeignReferenceType()); | ||
if (classDecl->isForeignReferenceType()) { | ||
ForeignReferenceTypeMetadataBuilder builder(IGM, classDecl, init); | ||
builder.layout(); | ||
|
||
ForeignClassMetadataBuilder builder(IGM, classDecl, init); | ||
builder.layout(); | ||
IGM.defineTypeMetadata(type, /*isPattern=*/false, | ||
builder.canBeConstant(), | ||
init.finishAndCreateFuture()); | ||
builder.createMetadataAccessFunction(); | ||
} else { | ||
assert(classDecl->getForeignClassKind() == ClassDecl::ForeignKind::CFType); | ||
|
||
IGM.defineTypeMetadata(type, /*isPattern=*/false, | ||
builder.canBeConstant(), | ||
init.finishAndCreateFuture()); | ||
builder.createMetadataAccessFunction(); | ||
ForeignClassMetadataBuilder builder(IGM, classDecl, init); | ||
builder.layout(); | ||
|
||
IGM.defineTypeMetadata(type, /*isPattern=*/false, | ||
builder.canBeConstant(), | ||
init.finishAndCreateFuture()); | ||
builder.createMetadataAccessFunction(); | ||
} | ||
} else if (auto structDecl = dyn_cast<StructDecl>(decl)) { | ||
assert(isa<ClangModuleUnit>(structDecl->getModuleScopeContext())); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rjmccall I have no idea what this value should be.