-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Implement foreign reference types. #39605
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,5 +59,10 @@ ERROR(temporary_allocation_alignment_not_positive,none, | |
ERROR(temporary_allocation_alignment_not_power_of_2,none, | ||
"alignment value must be a power of two", ()) | ||
|
||
ERROR(foreign_reference_types_unsupported,none, | ||
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. |
||
"attempt to use a foreign reference type in a generic context. " | ||
"Foreign reference types are currently not supported. Using foreign " | ||
"reference types in a generic context is not yet implemented.", ()) | ||
|
||
#define UNDEFINE_DIAGNOSTIC_MACROS | ||
#include "DefineDiagnosticMacros.h" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1957,7 +1957,11 @@ static bool isPolymorphic(const AbstractStorageDecl *storage) { | |
return true; | ||
|
||
if (auto *classDecl = dyn_cast<ClassDecl>(storage->getDeclContext())) { | ||
if (storage->isFinal() || classDecl->isFinal()) | ||
// Accesses to members of foreign reference types should be made directly | ||
// to storage as these are references to clang records which are not allowed | ||
// to have dynamic dispatch. | ||
if (storage->isFinal() || classDecl->isFinal() || | ||
classDecl->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. Why is this here? Imported classes can have polymorphic operations. At the very least, this needs a comment. 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. Added a comment. |
||
return false; | ||
|
||
return true; | ||
|
@@ -4737,6 +4741,10 @@ bool ClassDecl::walkSuperclasses( | |
return false; | ||
} | ||
|
||
bool ClassDecl::isForeignReferenceType() { | ||
return getClangDecl() && isa<clang::RecordDecl>(getClangDecl()); | ||
} | ||
|
||
EnumCaseDecl *EnumCaseDecl::create(SourceLoc CaseLoc, | ||
ArrayRef<EnumElementDecl *> Elements, | ||
DeclContext *DC) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -846,7 +846,7 @@ Type TypeBase::stripConcurrency(bool recurse, bool dropGlobalActor) { | |
bool TypeBase::isAnyObject() { | ||
auto canTy = getCanonicalType(); | ||
|
||
if (!canTy.isExistentialType()) | ||
if (!canTy.isExistentialType() || canTy.isForeignReferenceType()) | ||
return false; | ||
|
||
return canTy.getExistentialLayout().isAnyObject(); | ||
|
@@ -5362,19 +5362,16 @@ bool UnownedStorageType::isLoadable(ResilienceExpansion resilience) const { | |
return ty->getReferenceCounting() == ReferenceCounting::Native; | ||
} | ||
|
||
static ReferenceCounting getClassReferenceCounting(ClassDecl *theClass) { | ||
return (theClass->usesObjCObjectModel() | ||
? ReferenceCounting::ObjC | ||
: ReferenceCounting::Native); | ||
} | ||
|
||
ReferenceCounting TypeBase::getReferenceCounting() { | ||
CanType type = getCanonicalType(); | ||
ASTContext &ctx = type->getASTContext(); | ||
|
||
// In the absence of Objective-C interoperability, everything uses native | ||
// reference counting or is the builtin BridgeObject. | ||
if (!ctx.LangOpts.EnableObjCInterop) { | ||
if (isForeignReferenceType()) | ||
return ReferenceCounting::None; | ||
|
||
return type->getKind() == TypeKind::BuiltinBridgeObject | ||
? ReferenceCounting::Bridge | ||
: ReferenceCounting::Native; | ||
|
@@ -5394,13 +5391,12 @@ ReferenceCounting TypeBase::getReferenceCounting() { | |
return ReferenceCounting::Bridge; | ||
|
||
case TypeKind::Class: | ||
return getClassReferenceCounting(cast<ClassType>(type)->getDecl()); | ||
return cast<ClassType>(type)->getDecl()->getObjectModel(); | ||
case TypeKind::BoundGenericClass: | ||
return getClassReferenceCounting( | ||
cast<BoundGenericClassType>(type)->getDecl()); | ||
return cast<BoundGenericClassType>(type)->getDecl()->getObjectModel(); | ||
case TypeKind::UnboundGeneric: | ||
return getClassReferenceCounting( | ||
cast<ClassDecl>(cast<UnboundGenericType>(type)->getDecl())); | ||
return cast<ClassDecl>(cast<UnboundGenericType>(type)->getDecl()) | ||
->getObjectModel(); | ||
|
||
case TypeKind::DynamicSelf: | ||
return cast<DynamicSelfType>(type).getSelfType() | ||
|
@@ -5612,6 +5608,18 @@ TypeBase::getAutoDiffTangentSpace(LookupConformanceFn lookupConformance) { | |
return cache(None); | ||
} | ||
|
||
bool TypeBase::isForeignReferenceType() { | ||
if (auto *classDecl = lookThroughAllOptionalTypes()->getClassOrBoundGenericClass()) | ||
return classDecl->isForeignReferenceType(); | ||
return false; | ||
} | ||
|
||
bool CanType::isForeignReferenceType() { | ||
if (auto *classDecl = getPointer()->lookThroughAllOptionalTypes()->getClassOrBoundGenericClass()) | ||
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. Looks like you duplicate the logic here. Can you call |
||
return classDecl->isForeignReferenceType(); | ||
return false; | ||
} | ||
|
||
// Creates an `AnyFunctionType` from the given parameters, result type, | ||
// generic signature, and `ExtInfo`. | ||
static AnyFunctionType * | ||
|
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.
This is not ideal, but I think it's OK because we never construct a const decl, right?