Skip to content

[no review][no merge] Testing foreign reference types + extensions to replace libSwift bridging. #40293

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ function(add_libswift name)
set(libswift_compile_options
"-Xfrontend" "-validate-tbd-against-ir=none"
"-Xfrontend" "-enable-cxx-interop"
"-Xfrontend" "-disable-llvm-verify"
"-Xcc" "-UIBOutlet" "-Xcc" "-UIBAction" "-Xcc" "-UIBInspectable")

if(CMAKE_BUILD_TYPE STREQUAL Debug)
Expand Down Expand Up @@ -778,7 +779,18 @@ function(add_libswift name)
"-emit-module-path" "${build_dir}/${module}.swiftmodule"
"-parse-as-library" ${sources}
"-wmo" ${libswift_compile_options}
"-I" "${SWIFT_SOURCE_DIR}/include/swift"
"-I" "${CMAKE_SOURCE_DIR}/include/swift"
# The Swift source header includes:
"-I" "${CMAKE_SOURCE_DIR}/include"
# LLVM and Clang source header includes:
"-I" "${LLVM_MAIN_SRC_DIR}/../clang/include"
"-I" "${LLVM_MAIN_INCLUDE_DIR}"
# Swift build includes:
"-I" "${CMAKE_BINARY_DIR}/include"
# LLVM and Clang build includes:
"-I" "${LLVM_BINARY_DIR}/tools/clang/include"
"-I" "${LLVM_BINARY_DIR}/include"
# libSwift build includes:
"-I" "${build_dir}"
COMMENT "Building libswift module ${module}")

Expand Down
5 changes: 5 additions & 0 deletions include/swift/ABI/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,11 @@ struct TargetStructMetadata : public TargetValueMetadata<Runtime> {
}

bool isCanonicalStaticallySpecializedGenericMetadata() const {
// Struct metadata is used for foreign reference types, in which case the
// descriptor is not a struct descriptor.
if (!llvm::isa<TargetStructDescriptor<Runtime>>(this->Description))
return false;

auto *description = getDescription();
if (!description->isGeneric())
return false;
Expand Down
30 changes: 25 additions & 5 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "swift/AST/GenericParamKey.h"
#include "swift/AST/IfConfigClause.h"
#include "swift/AST/LayoutConstraint.h"
#include "swift/AST/ReferenceCounting.h"
#include "swift/AST/StorageImpl.h"
#include "swift/AST/TypeAlignments.h"
#include "swift/AST/TypeWalker.h"
Expand All @@ -37,10 +38,10 @@
#include "swift/Basic/Compiler.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/InlineBitfield.h"
#include "swift/Basic/Located.h"
#include "swift/Basic/NullablePtr.h"
#include "swift/Basic/OptionalEnum.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/Located.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/Support/TrailingObjects.h"
#include <type_traits>
Expand Down Expand Up @@ -3900,7 +3901,8 @@ class ClassDecl final : public NominalTypeDecl {
///
/// \see getForeignClassKind
bool isForeign() const {
return getForeignClassKind() != ForeignKind::Normal;
return getForeignClassKind() != ForeignKind::Normal ||
const_cast<ClassDecl *>(this)->isForeignReferenceType();
}

/// Whether the class is (known to be) a default actor.
Expand Down Expand Up @@ -3935,9 +3937,22 @@ class ClassDecl final : public NominalTypeDecl {
bool isNativeNSObjectSubclass() const;

/// Whether the class uses the ObjC object model (reference counting,
/// allocation, etc.) instead of the Swift model.
bool usesObjCObjectModel() const {
return checkAncestry(AncestryFlags::ObjCObjectModel);
/// allocation, etc.), the Swift model, or has no reference counting at all.
ReferenceCounting getObjectModel() {
if (isForeignReferenceType())
return ReferenceCounting::None;

if (checkAncestry(AncestryFlags::ObjCObjectModel))
return ReferenceCounting::ObjC;

return ReferenceCounting::Native;
}

LayoutConstraintKind getLayoutConstraintKind() {
if (getObjectModel() == ReferenceCounting::ObjC)
return LayoutConstraintKind::Class;

return LayoutConstraintKind::NativeClass;
}

/// Returns true if the class has designated initializers that are not listed
Expand Down Expand Up @@ -4059,6 +4074,11 @@ class ClassDecl final : public NominalTypeDecl {
bool hasKnownSwiftImplementation() const {
return !hasClangNode();
}

/// Used to determine if this class decl is a foriegn reference type. I.e., a
/// non-reference-counted swift reference type that was imported from a C++
/// record.
bool isForeignReferenceType();
};

/// A convenience wrapper around the \c SelfReferencePosition::Kind enum.
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/ReferenceCounting.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ enum class ReferenceCounting : uint8_t {
/// Blocks are always ObjC reference counting compatible.
ObjC,

/// The object has no reference counting. This is used by foreign reference
/// types.
None,

/// The object uses _Block_copy/_Block_release reference counting.
///
/// This is a strict subset of ObjC; all blocks are also ObjC reference
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ class CanType : public Type {
NominalTypeDecl *getAnyNominal() const;
GenericTypeDecl *getAnyGeneric() const;

bool isForeignReferenceType(); // in Types.h

CanType getOptionalObjectType() const {
return getOptionalObjectTypeImpl(*this);
}
Expand Down
13 changes: 7 additions & 6 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -811,16 +811,19 @@ class alignas(1 << TypeAlignInBits) TypeBase

/// If this is a class type or a bound generic class type, returns the
/// (possibly generic) class.
ClassDecl *getClassOrBoundGenericClass();
ClassDecl *getClassOrBoundGenericClass() const;

/// If this is a struct type or a bound generic struct type, returns
/// the (possibly generic) class.
StructDecl *getStructOrBoundGenericStruct();

/// If this is an enum or a bound generic enum type, returns the
/// (possibly generic) enum.
EnumDecl *getEnumOrBoundGenericEnum();


/// If this is a class, check if this class is a foreign reference type.
bool isForeignReferenceType();

/// Determine whether this type may have a superclass, which holds for
/// classes, bound generic classes, and archetypes that are only instantiable
/// with a class type.
Expand Down Expand Up @@ -6175,7 +6178,7 @@ inline bool TypeBase::canDynamicallyBeOptionalType(bool includeExistential) {
return isArchetypeOrExistential && !T.isAnyClassReferenceType();
}

inline ClassDecl *TypeBase::getClassOrBoundGenericClass() {
inline ClassDecl *TypeBase::getClassOrBoundGenericClass() const {
return getCanonicalType().getClassOrBoundGenericClass();
}

Expand Down Expand Up @@ -6239,8 +6242,6 @@ inline GenericTypeDecl *TypeBase::getAnyGeneric() {
return getCanonicalType().getAnyGeneric();
}



inline bool TypeBase::isBuiltinIntegerType(unsigned n) {
if (auto intTy = dyn_cast<BuiltinIntegerType>(getCanonicalType()))
return intTy->getWidth().isFixedWidth()
Expand Down
4 changes: 2 additions & 2 deletions include/swift/ClangImporter/ClangImporterRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ class CXXNamespaceMemberLookup

/// The input type for a record member lookup request.
struct ClangRecordMemberLookupDescriptor final {
StructDecl *recordDecl;
NominalTypeDecl *recordDecl;
DeclName name;

ClangRecordMemberLookupDescriptor(StructDecl *recordDecl, DeclName name)
ClangRecordMemberLookupDescriptor(NominalTypeDecl *recordDecl, DeclName name)
: recordDecl(recordDecl), name(name) {
assert(isa<clang::RecordDecl>(recordDecl->getClangDecl()));
}
Expand Down
5 changes: 2 additions & 3 deletions include/swift/SIL/BridgedSwiftObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
// Provide macros to temporarily suppress warning about the use of
// _Nullable and _Nonnull.
# define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wnullability-extension\"")
_Pragma("clang assume_nonnull begin")
# define SWIFT_END_NULLABILITY_ANNOTATIONS \
_Pragma("clang diagnostic pop")
_Pragma("clang assume_nonnull end")

#else
// #define _Nullable and _Nonnull to nothing if we're not being built
Expand Down
9 changes: 6 additions & 3 deletions include/swift/SIL/SILArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ struct SILArgumentKind {
}
};

class SILArgument : public ValueBase {
class __attribute__((swift_attr("import_as_ref"))) SILArgument
: public ValueBase {
friend class SILBasicBlock;

SILBasicBlock *parentBlock;
Expand Down Expand Up @@ -202,7 +203,8 @@ inline SILArgument *castToArgument(SwiftObject argument) {
return static_cast<SILArgument *>(argument);
}

class SILPhiArgument : public SILArgument {
class __attribute__((swift_attr("import_as_ref"))) SILPhiArgument
: public SILArgument {
friend class SILBasicBlock;

SILPhiArgument(SILBasicBlock *parentBlock, SILType type,
Expand Down Expand Up @@ -310,7 +312,8 @@ class SILPhiArgument : public SILArgument {
}
};

class SILFunctionArgument : public SILArgument {
class __attribute__((swift_attr("import_as_ref"))) SILFunctionArgument
: public SILArgument {
friend class SILBasicBlock;

bool noImplicitCopy = false;
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILBasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SILFunction;
class SILArgument;
class SILPrintContext;

class SILBasicBlock :
class __attribute__((swift_attr("import_as_ref"))) SILBasicBlock :
public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock>,
public SwiftObjectHeader {
friend class SILSuccessor;
Expand Down
Loading