Skip to content

[runtime] Clean up symbol exports in casting and class introspection. #13005

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 1 commit into from
Dec 2, 2017
Merged
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
4 changes: 3 additions & 1 deletion include/swift/Runtime/HeapObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,9 @@ static inline bool swift_unknownUnownedIsEqual(UnownedReference *ref,
#endif /* SWIFT_OBJC_INTEROP */

/// Return the name of a Swift type represented by a metadata object.
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT
/// func _getTypeName(_ type: Any.Type, qualified: Bool)
/// -> (UnsafePointer<UInt8>, Int)
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
TwoWordPair<const char *, uintptr_t>::Return
swift_getTypeName(const Metadata *type, bool qualified);

Expand Down
44 changes: 15 additions & 29 deletions stdlib/public/core/BridgeObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ public func _bridgeAnythingToObjectiveC<T>(_ x: T) -> AnyObject {
return _bridgeAnythingNonVerbatimToObjectiveC(x)
}

/// COMPILER_INTRINSIC
@_inlineable // FIXME(sil-serialize-all)
@_silgen_name("_swift_bridgeAnythingNonVerbatimToObjectiveC")
@_silgen_name("")
public func _bridgeAnythingNonVerbatimToObjectiveC<T>(_ x: T) -> AnyObject

/// Convert a purportedly-nonnull `id` value from Objective-C into an Any.
Expand Down Expand Up @@ -214,8 +212,8 @@ public func _forceBridgeFromObjectiveC<T>(_ x: AnyObject, _: T.Type) -> T {

/// Convert `x` from its Objective-C representation to its Swift
/// representation.
/// COMPILER_INTRINSIC
@_inlineable // FIXME(sil-serialize-all)
@_silgen_name("_forceBridgeFromObjectiveC_bridgeable")
public func _forceBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeable> (
_ x: T._ObjectiveCType,
_: T.Type
Expand Down Expand Up @@ -253,8 +251,8 @@ public func _conditionallyBridgeFromObjectiveC<T>(

/// Attempt to convert `x` from its Objective-C representation to its Swift
/// representation.
/// COMPILER_INTRINSIC
@_inlineable // FIXME(sil-serialize-all)
@_silgen_name("_conditionallyBridgeFromObjectiveC_bridgeable")
public func _conditionallyBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeable>(
_ x: T._ObjectiveCType,
_: T.Type
Expand All @@ -264,32 +262,26 @@ public func _conditionallyBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeabl
return result
}

@_inlineable // FIXME(sil-serialize-all)
@_versioned // FIXME(sil-serialize-all)
@_silgen_name("_swift_bridgeNonVerbatimFromObjectiveC")
internal func _bridgeNonVerbatimFromObjectiveC<T>(
@_silgen_name("")
public func _bridgeNonVerbatimFromObjectiveC<T>(
_ x: AnyObject,
_ nativeType: T.Type,
_ result: inout T?
)

/// Helper stub to upcast to Any and store the result to an inout Any?
/// on the C++ runtime's behalf.
// COMPILER_INTRINSIC
@_inlineable // FIXME(sil-serialize-all)
@_silgen_name("_swift_bridgeNonVerbatimFromObjectiveCToAny")
public func _bridgeNonVerbatimFromObjectiveCToAny(
@_silgen_name("_bridgeNonVerbatimFromObjectiveCToAny")
internal func _bridgeNonVerbatimFromObjectiveCToAny(
_ x: AnyObject,
_ result: inout Any?
) {
result = x as Any
}

/// Helper stub to upcast to Optional on the C++ runtime's behalf.
// COMPILER_INTRINSIC
@_inlineable // FIXME(sil-serialize-all)
@_silgen_name("_swift_bridgeNonVerbatimBoxedValue")
public func _bridgeNonVerbatimBoxedValue<NativeType>(
@_silgen_name("_bridgeNonVerbatimBoxedValue")
internal func _bridgeNonVerbatimBoxedValue<NativeType>(
_ x: UnsafePointer<NativeType>,
_ result: inout NativeType?
) {
Expand All @@ -303,10 +295,8 @@ public func _bridgeNonVerbatimBoxedValue<NativeType>(
/// unchanged otherwise.
///
/// - Returns: `true` to indicate success, `false` to indicate failure.
@_inlineable // FIXME(sil-serialize-all)
@_versioned // FIXME(sil-serialize-all)
@_silgen_name("_swift_bridgeNonVerbatimFromObjectiveCConditional")
internal func _bridgeNonVerbatimFromObjectiveCConditional<T>(
@_silgen_name("")
public func _bridgeNonVerbatimFromObjectiveCConditional<T>(
_ x: AnyObject,
_ nativeType: T.Type,
_ result: inout T?
Expand All @@ -325,10 +315,8 @@ public func _isBridgedToObjectiveC<T>(_: T.Type) -> Bool {
return _isBridgedNonVerbatimToObjectiveC(T.self)
}

@_inlineable // FIXME(sil-serialize-all)
@_versioned // FIXME(sil-serialize-all)
@_silgen_name("_swift_isBridgedNonVerbatimToObjectiveC")
internal func _isBridgedNonVerbatimToObjectiveC<T>(_: T.Type) -> Bool
@_silgen_name("")
public func _isBridgedNonVerbatimToObjectiveC<T>(_: T.Type) -> Bool

/// A type that's bridged "verbatim" does not conform to
/// `_ObjectiveCBridgeable`, and can have its bits reinterpreted as an
Expand All @@ -348,10 +336,8 @@ public func _getBridgedObjectiveCType<T>(_: T.Type) -> Any.Type? {
return _getBridgedNonVerbatimObjectiveCType(T.self)
}

@_inlineable // FIXME(sil-serialize-all)
@_versioned // FIXME(sil-serialize-all)
@_silgen_name("_swift_getBridgedNonVerbatimObjectiveCType")
internal func _getBridgedNonVerbatimObjectiveCType<T>(_: T.Type) -> Any.Type?
@_silgen_name("")
public func _getBridgedNonVerbatimObjectiveCType<T>(_: T.Type) -> Any.Type?

// -- Pointer argument bridging

Expand Down
20 changes: 8 additions & 12 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public func _onFastPath() {
// when using RuntimeShims.h
@_inlineable // FIXME(sil-serialize-all)
@_versioned
@_silgen_name("swift_objc_class_usesNativeSwiftReferenceCounting")
@_silgen_name("_objcClassUsesNativeSwiftReferenceCounting")
internal func _usesNativeSwiftReferenceCounting(_ theClass: AnyClass) -> Bool
#else
@_inlineable // FIXME(sil-serialize-all)
Expand All @@ -335,24 +335,24 @@ internal func _usesNativeSwiftReferenceCounting(_ theClass: AnyClass) -> Bool {

@_inlineable // FIXME(sil-serialize-all)
@_versioned // FIXME(sil-serialize-all)
@_silgen_name("swift_class_getInstanceExtents")
internal func swift_class_getInstanceExtents(_ theClass: AnyClass)
@_silgen_name("_getSwiftClassInstanceExtents")
internal func getSwiftClassInstanceExtents(_ theClass: AnyClass)
-> (negative: UInt, positive: UInt)

@_inlineable // FIXME(sil-serialize-all)
@_versioned // FIXME(sil-serialize-all)
@_silgen_name("swift_objc_class_unknownGetInstanceExtents")
internal func swift_objc_class_unknownGetInstanceExtents(_ theClass: AnyClass)
@_silgen_name("_getObjCClassInstanceExtents")
internal func getObjCClassInstanceExtents(_ theClass: AnyClass)
-> (negative: UInt, positive: UInt)

@_inlineable // FIXME(sil-serialize-all)
@_versioned // FIXME(sil-serialize-all)
@inline(__always)
internal func _class_getInstancePositiveExtentSize(_ theClass: AnyClass) -> Int {
#if _runtime(_ObjC)
return Int(swift_objc_class_unknownGetInstanceExtents(theClass).positive)
return Int(getObjCClassInstanceExtents(theClass).positive)
#else
return Int(swift_class_getInstanceExtents(theClass).positive)
return Int(getSwiftClassInstanceExtents(theClass).positive)
#endif
}

Expand Down Expand Up @@ -488,16 +488,12 @@ internal func _makeBridgeObject(
)
}

@_inlineable // FIXME(sil-serialize-all)
@_versioned
@_silgen_name("_swift_class_getSuperclass")
internal func _swift_class_getSuperclass(_ t: AnyClass) -> AnyClass?

/// Returns the superclass of `t`, if any. The result is `nil` if `t` is
/// a root class or class protocol.
@_inlineable // FIXME(sil-serialize-all)
@inline(__always)
public // @testable
public
func _getSuperclass(_ t: AnyClass) -> AnyClass? {
return _swift_class_getSuperclass(t)
}
Expand Down
5 changes: 1 addition & 4 deletions stdlib/public/core/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,14 @@ func _typeName(_ type: Any.Type, qualified: Bool = true) -> String {
input: UnsafeBufferPointer(start: stringPtr, count: count))
}

@_inlineable // FIXME(sil-serialize-all)
@_versioned // FIXME(sil-serialize-all)
@_silgen_name("swift_getTypeByName")
@_silgen_name("")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it mean to have an empty string literal here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means the function gets the same symbol visibility treatment as ordinary @_silgen_name (i.e. internal functions get C-visible hidden symbols), but the mangled name of the function is unchanged. This syntax is a temporary hack and ought to be replaced someday.

The goal is that Swift functions that merely happen to be implemented in C today should have Swift-mangled symbol names.

internal func _getTypeByName(
_ name: UnsafePointer<UInt8>,
_ nameLength: UInt)
-> Any.Type?

/// Lookup a class given a name. Until the demangled encoding of type
/// names is stabilized, this is limited to top-level class names (Foo.bar).
@_inlineable // FIXME(sil-serialize-all)
public // SPI(Foundation)
func _typeByName(_ name: String) -> Any.Type? {
let nameUTF8 = Array(name.utf8)
Expand Down
85 changes: 47 additions & 38 deletions stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ std::string swift::nameForMetadata(const Metadata *type,
return result;
}

SWIFT_CC(swift)
TwoWordPair<const char *, uintptr_t>::Return
swift::swift_getTypeName(const Metadata *type, bool qualified) {
using Pair = TwoWordPair<const char *, uintptr_t>;
Expand Down Expand Up @@ -2972,9 +2971,13 @@ static id bridgeAnythingNonVerbatimToObjectiveC(OpaqueValue *src,
return (id)bridgeAnythingToSwiftValueObject(src, srcType, consume);
}

SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
id _swift_bridgeAnythingNonVerbatimToObjectiveC(OpaqueValue *src,
const Metadata *srcType) {
/// public func _bridgeAnythingNonVerbatimToObjectiveC<T>(_ x: T) -> AnyObject
/// Called by inlined stdlib code.
#define _bridgeAnythingNonVerbatimToObjectiveC \
MANGLE_SYM(s38_bridgeAnythingNonVerbatimToObjectiveCyXlxlF)
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
id _bridgeAnythingNonVerbatimToObjectiveC(OpaqueValue *src,
const Metadata *srcType) {
return bridgeAnythingNonVerbatimToObjectiveC(src, srcType, /*consume*/ true);
}

Expand Down Expand Up @@ -3031,8 +3034,12 @@ findBridgeWitness(const Metadata *T) {
return nullptr;
}

SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
const Metadata *_swift_getBridgedNonVerbatimObjectiveCType(
// public func _getBridgedNonVerbatimObjectiveCType<T>(_: T.Type) -> Any.Type?
// Called by inlined stdlib code.
#define _getBridgedNonVerbatimObjectiveCType \
MANGLE_SYM(s36_getBridgedNonVerbatimObjectiveCTypeypXpSgxmlF)
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
const Metadata *_getBridgedNonVerbatimObjectiveCType(
const Metadata *value, const Metadata *T
) {
// Classes and Objective-C existentials bridge verbatim.
Expand All @@ -3047,26 +3054,26 @@ const Metadata *_swift_getBridgedNonVerbatimObjectiveCType(
return nullptr;
}

// @_silgen_name("_swift_bridgeNonVerbatimFromObjectiveCToAny")
// @_silgen_name("_bridgeNonVerbatimFromObjectiveCToAny")
// func _bridgeNonVerbatimFromObjectiveCToAny(
// x: AnyObject,
// inout result: Any?
// )
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
void
_swift_bridgeNonVerbatimFromObjectiveCToAny(HeapObject *sourceValue,
OpaqueValue *destValue);
_bridgeNonVerbatimFromObjectiveCToAny(HeapObject *sourceValue,
OpaqueValue *destValue);

// @_silgen_name("_swift_bridgeNonVerbatimBoxedValue")
// @_silgen_name("_bridgeNonVerbatimBoxedValue")
// func _bridgeNonVerbatimBoxedValue<NativeType>(
// x: UnsafePointer<NativeType>,
// inout result: NativeType?
// )
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
void
_swift_bridgeNonVerbatimBoxedValue(const OpaqueValue *sourceValue,
OpaqueValue *destValue,
const Metadata *nativeType);
_bridgeNonVerbatimBoxedValue(const OpaqueValue *sourceValue,
OpaqueValue *destValue,
const Metadata *nativeType);

// Try bridging by conversion to Any or boxing if applicable.
static bool tryBridgeNonVerbatimFromObjectiveCUniversal(
Expand All @@ -3079,8 +3086,7 @@ static bool tryBridgeNonVerbatimFromObjectiveCUniversal(
if (auto nativeExistential = dyn_cast<ExistentialTypeMetadata>(nativeType)) {
if (nativeExistential->Protocols.NumProtocols == 0 &&
!nativeExistential->isClassBounded()) {
_swift_bridgeNonVerbatimFromObjectiveCToAny(sourceValue,
destValue);
_bridgeNonVerbatimFromObjectiveCToAny(sourceValue, destValue);
return true;
}
}
Expand All @@ -3092,25 +3098,25 @@ static bool tryBridgeNonVerbatimFromObjectiveCUniversal(

std::tie(sourceType, sourceBoxedValue) = getValueFromSwiftValue(srcBox);
if (sourceType == nativeType) {
_swift_bridgeNonVerbatimBoxedValue(sourceBoxedValue,
destValue,
nativeType);
_bridgeNonVerbatimBoxedValue(sourceBoxedValue, destValue, nativeType);
return true;
}
}

return false;
}

// @_silgen_name("_swift_bridgeNonVerbatimFromObjectiveC")
// func _bridgeNonVerbatimFromObjectiveC<NativeType>(
// x: AnyObject,
// nativeType: NativeType.Type
// inout result: T?
// )
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
// Called by inlined stdlib code.
#define _bridgeNonVerbatimFromObjectiveC \
MANGLE_SYM(s32_bridgeNonVerbatimFromObjectiveCyyXl_xmxSgztlF)
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
void
_swift_bridgeNonVerbatimFromObjectiveC(
_bridgeNonVerbatimFromObjectiveC(
HeapObject *sourceValue,
const Metadata *nativeType,
OpaqueValue *destValue,
Expand Down Expand Up @@ -3145,15 +3151,14 @@ _swift_bridgeNonVerbatimFromObjectiveC(
swift::crash("value type is not bridged to Objective-C");
}

// @_silgen_name("_swift_bridgeNonVerbatimFromObjectiveCConditional")
// func _bridgeNonVerbatimFromObjectiveCConditional<NativeType>(
// x: AnyObject,
// nativeType: T.Type,
// inout result: T?
// ) -> Bool
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
/// func _bridgeNonVerbatimFromObjectiveCConditional<NativeType>(
/// x: AnyObject, nativeType: T.Type, inout result: T?) -> Bool
/// Called by inlined stdlib code.
#define _bridgeNonVerbatimFromObjectiveCConditional \
MANGLE_SYM(s43_bridgeNonVerbatimFromObjectiveCConditionalSbyXl_xmxSgztlF)
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
bool
_swift_bridgeNonVerbatimFromObjectiveCConditional(
_bridgeNonVerbatimFromObjectiveCConditional(
HeapObject *sourceValue,
const Metadata *nativeType,
OpaqueValue *destValue,
Expand Down Expand Up @@ -3195,10 +3200,12 @@ _swift_bridgeNonVerbatimFromObjectiveCConditional(
}

// func _isBridgedNonVerbatimToObjectiveC<T>(x: T.Type) -> Bool
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
bool _swift_isBridgedNonVerbatimToObjectiveC(
const Metadata *value, const Metadata *T
) {
// Called by inlined stdlib code.
#define _isBridgedNonVerbatimToObjectiveC \
MANGLE_SYM(s33_isBridgedNonVerbatimToObjectiveCSbxmlF)
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
bool _isBridgedNonVerbatimToObjectiveC(const Metadata *value,
const Metadata *T) {
assert(!swift_isClassOrObjCExistentialTypeImpl(T));

auto bridgeWitness = findBridgeWitness(T);
Expand All @@ -3213,20 +3220,22 @@ bool _swift_isClassOrObjCExistentialType(const Metadata *value,
return swift_isClassOrObjCExistentialTypeImpl(T);
}

SWIFT_CC(swift)
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
const Metadata *swift::_swift_class_getSuperclass(const Metadata *theClass) {
if (const ClassMetadata *classType = theClass->getClassObject())
if (classHasSuperclass(classType))
return getMetadataForClass(classType->SuperClass);
return nullptr;
}

SWIFT_CC(c) SWIFT_RUNTIME_EXPORT
// Called by compiler-generated cast code.
SWIFT_CC(c) SWIFT_RUNTIME_STDLIB_API
bool swift_isClassType(const Metadata *type) {
return Metadata::isAnyKindOfClass(type->getKind());
}

SWIFT_CC(c) SWIFT_RUNTIME_EXPORT
// Called by compiler-generated code.
SWIFT_CC(c) SWIFT_RUNTIME_STDLIB_API
bool swift_isOptionalType(const Metadata *type) {
return type->getKind() == MetadataKind::Optional;
}
10 changes: 7 additions & 3 deletions stdlib/public/runtime/MetadataLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,14 @@ _classByName(const llvm::StringRef typeName) {

/// \param typeName The name of a class in the form: <module>.<class>
/// \return Returns the metadata of the type, if found.
SWIFT_CC(swift)
SWIFT_RUNTIME_EXPORT

/// internal func _getTypeByName(_ name: UnsafePointer<UInt8>,
/// _ nameLength: UInt) -> Any.Type?
#define _getTypeByName \
MANGLE_SYM(s14_getTypeByNameypXpSgSPys5UInt8VG_SutF)
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
const Metadata *
swift_getTypeByName(const char *typeName, size_t typeNameLength) {
_getTypeByName(const char *typeName, size_t typeNameLength) {
llvm::StringRef name(typeName, typeNameLength);
return _classByName(name);
}
Loading