Skip to content

[swift-3.0-branch] runtime: rename SwiftValue to _SwiftValue (it is a private class) #4223

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
Aug 11, 2016
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: 2 additions & 2 deletions stdlib/public/runtime/AnyHashableSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ extern "C" void _swift_stdlib_makeAnyHashableUpcastingToHashableBaseType(
#if SWIFT_OBJC_INTEROP
id srcObject;
memcpy(&srcObject, value, sizeof(id));
// Do we have a SwiftValue?
if (SwiftValue *srcSwiftValue = getAsSwiftValue(srcObject)) {
// Do we have a _SwiftValue?
if (_SwiftValue *srcSwiftValue = getAsSwiftValue(srcObject)) {
// If so, extract the boxed value and try to cast it.
const Metadata *unboxedType;
const OpaqueValue *unboxedValue;
Expand Down
10 changes: 5 additions & 5 deletions stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2261,11 +2261,11 @@ checkDynamicCastFromOptional(OpaqueValue *dest,
}

/******************************************************************************/
/***************************** Bridging SwiftValue ****************************/
/**************************** Bridging _SwiftValue ****************************/
/******************************************************************************/

#if SWIFT_OBJC_INTEROP
/// Try to unbox a SwiftValue box to perform a dynamic cast.
/// Try to unbox a _SwiftValue box to perform a dynamic cast.
static bool tryDynamicCastBoxedSwiftValue(OpaqueValue *dest,
OpaqueValue *src,
const Metadata *srcType,
Expand All @@ -2283,8 +2283,8 @@ static bool tryDynamicCastBoxedSwiftValue(OpaqueValue *dest,
id srcObject;
memcpy(&srcObject, src, sizeof(id));

// Do we have a SwiftValue?
SwiftValue *srcSwiftValue = getAsSwiftValue(srcObject);
// Do we have a _SwiftValue?
_SwiftValue *srcSwiftValue = getAsSwiftValue(srcObject);
if (!srcSwiftValue)
return false;

Expand Down Expand Up @@ -2483,7 +2483,7 @@ bool swift::swift_dynamicCast(OpaqueValue *dest,
return unwrapResult.success;

#if SWIFT_OBJC_INTEROP
// A class or AnyObject reference may point at a boxed SwiftValue.
// A class or AnyObject reference may point at a boxed _SwiftValue.
if (tryDynamicCastBoxedSwiftValue(dest, src, srcType,
targetType, flags)) {
return true;
Expand Down
22 changes: 11 additions & 11 deletions stdlib/public/runtime/SwiftValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@
#if SWIFT_OBJC_INTEROP
#include <objc/runtime.h>

// SwiftValue is an Objective-C class, but we shouldn't interface with it
// _SwiftValue is an Objective-C class, but we shouldn't interface with it
// directly as such. Keep the type opaque.
#if __OBJC__
@class SwiftValue;
@class _SwiftValue;
#else
typedef struct SwiftValue SwiftValue;
typedef struct _SwiftValue _SwiftValue;
#endif

namespace swift {

/// Bridge a Swift value to an Objective-C object by boxing it as a SwiftValue.
SwiftValue *bridgeAnythingToSwiftValueObject(OpaqueValue *src,
const Metadata *srcType,
bool consume);
/// Bridge a Swift value to an Objective-C object by boxing it as a _SwiftValue.
_SwiftValue *bridgeAnythingToSwiftValueObject(OpaqueValue *src,
const Metadata *srcType,
bool consume);

/// Get the type metadata for a value in a Swift box.
const Metadata *getSwiftValueTypeMetadata(SwiftValue *v);
const Metadata *getSwiftValueTypeMetadata(_SwiftValue *v);

/// Get the value out of a Swift box along with its type metadata. The value
/// inside the box is immutable and must not be modified or taken from the box.
std::pair<const Metadata *, const OpaqueValue *>
getValueFromSwiftValue(SwiftValue *v);
getValueFromSwiftValue(_SwiftValue *v);

/// Return the object reference as a SwiftValue* if it is a SwiftValue instance,
/// Return the object reference as a _SwiftValue* if it is a _SwiftValue instance,
/// or nil if it is not.
SwiftValue *getAsSwiftValue(id object);
_SwiftValue *getAsSwiftValue(id object);

} // namespace swift
#endif
Expand Down
40 changes: 20 additions & 20 deletions stdlib/public/runtime/SwiftValue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@
using namespace swift::hashable_support;

// TODO: Making this a SwiftObject subclass would let us use Swift refcounting,
// but we would need to be able to emit SwiftValue's Objective-C class object
// but we would need to be able to emit _SwiftValue's Objective-C class object
// with the Swift destructor pointer prefixed before it.
//
// The layout of `SwiftValue` is:
// The layout of `_SwiftValue` is:
// - object header,
// - `SwiftValueHeader` instance,
// - the payload, tail-allocated (the Swift value contained in this box).
@interface SwiftValue : NSObject <NSCopying>
@interface _SwiftValue : NSObject <NSCopying>

- (id)copyWithZone:(NSZone *)zone;

@end

/// The fixed-size ivars of `SwiftValue`. The actual boxed value is
/// The fixed-size ivars of `_SwiftValue`. The actual boxed value is
/// tail-allocated.
struct SwiftValueHeader {
/// The type of the value contained in the `SwiftValue` box.
/// The type of the value contained in the `_SwiftValue` box.
const Metadata *type;

/// The base type that introduces the `Hashable` conformance.
Expand Down Expand Up @@ -131,10 +131,10 @@ - (id)copyWithZone:(NSZone *)zone;
*/

static Class _getSwiftValueClass() {
auto theClass = [SwiftValue class];
// Fixed instance size of SwiftValue should be same as object header.
auto theClass = [_SwiftValue class];
// Fixed instance size of _SwiftValue should be same as object header.
assert(class_getInstanceSize(theClass) == SwiftValueHeaderOffset
&& "unexpected size of SwiftValue?!");
&& "unexpected size of _SwiftValue?!");
return theClass;
}

Expand All @@ -147,13 +147,13 @@ static constexpr size_t getSwiftValuePayloadOffset(size_t alignMask) {
~alignMask;
}

static SwiftValueHeader *getSwiftValueHeader(SwiftValue *v) {
static SwiftValueHeader *getSwiftValueHeader(_SwiftValue *v) {
auto instanceBytes = reinterpret_cast<char *>(v);
return reinterpret_cast<SwiftValueHeader *>(instanceBytes +
SwiftValueHeaderOffset);
}

static OpaqueValue *getSwiftValuePayload(SwiftValue *v, size_t alignMask) {
static OpaqueValue *getSwiftValuePayload(_SwiftValue *v, size_t alignMask) {
auto instanceBytes = reinterpret_cast<char *>(v);
return reinterpret_cast<OpaqueValue *>(instanceBytes +
getSwiftValuePayloadOffset(alignMask));
Expand All @@ -163,18 +163,18 @@ static size_t getSwiftValuePayloadAlignMask(const Metadata *type) {
return type->getValueWitnesses()->getAlignmentMask() | SwiftValueMinAlignMask;
}

const Metadata *swift::getSwiftValueTypeMetadata(SwiftValue *v) {
const Metadata *swift::getSwiftValueTypeMetadata(_SwiftValue *v) {
return getSwiftValueHeader(v)->type;
}

std::pair<const Metadata *, const OpaqueValue *>
swift::getValueFromSwiftValue(SwiftValue *v) {
swift::getValueFromSwiftValue(_SwiftValue *v) {
auto instanceType = getSwiftValueTypeMetadata(v);
size_t alignMask = getSwiftValuePayloadAlignMask(instanceType);
return {instanceType, getSwiftValuePayload(v, alignMask)};
}

SwiftValue *swift::bridgeAnythingToSwiftValueObject(OpaqueValue *src,
_SwiftValue *swift::bridgeAnythingToSwiftValueObject(OpaqueValue *src,
const Metadata *srcType,
bool consume) {
size_t alignMask = getSwiftValuePayloadAlignMask(srcType);
Expand All @@ -183,7 +183,7 @@ static size_t getSwiftValuePayloadAlignMask(const Metadata *type) {
getSwiftValuePayloadOffset(alignMask) + srcType->getValueWitnesses()->size;

void *instanceMemory = swift_slowAlloc(totalSize, alignMask);
SwiftValue *instance
_SwiftValue *instance
= objc_constructInstance(getSwiftValueClass(), instanceMemory);
/* TODO: If we're able to become a SwiftObject subclass in the future,
* change to this:
Expand All @@ -204,18 +204,18 @@ static size_t getSwiftValuePayloadAlignMask(const Metadata *type) {
return instance;
}

SwiftValue *swift::getAsSwiftValue(id object) {
// SwiftValue should have no subclasses or proxies. We can do an exact
_SwiftValue *swift::getAsSwiftValue(id object) {
// _SwiftValue should have no subclasses or proxies. We can do an exact
// class check.
if (object_getClass(object) == getSwiftValueClass())
return object;
return nil;
}

@implementation SwiftValue
@implementation _SwiftValue

+ (instancetype)allocWithZone:(NSZone *)zone {
swift::crash("SwiftValue cannot be instantiated");
swift::crash("_SwiftValue cannot be instantiated");
}

- (id)copyWithZone:(NSZone *)zone {
Expand Down Expand Up @@ -299,7 +299,7 @@ - (NSUInteger)hash {
selfHeader->type, hashableConformance);
}

static NSString *getValueDescription(SwiftValue *self) {
static NSString *getValueDescription(_SwiftValue *self) {
String tmp;
const Metadata *type;
const OpaqueValue *value;
Expand Down Expand Up @@ -332,6 +332,6 @@ - (const OpaqueValue *)_swiftValue {

@end

// TODO: We could pick specialized SwiftValue subclasses for trivial types
// TODO: We could pick specialized _SwiftValue subclasses for trivial types
// or for types with known size and alignment characteristics. Probably
// not enough of a real perf bottleneck to be worth it...