Skip to content

Introduce SWIFT_ENABLE_REFLECTION to turn on/off the support for Mirrors and reflection #33617

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
Sep 8, 2021
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: 4 additions & 0 deletions stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ set(SWIFT_STDLIB_ENABLE_LTO OFF CACHE STRING "Build Swift stdlib with LTO. One
must specify the form of LTO by setting this to one of: 'full', 'thin'. This
option only affects the standard library and runtime, not tools.")

option(SWIFT_ENABLE_REFLECTION
"Build stdlib with support for runtime reflection and mirrors."
TRUE)

#
# End of user-configurable options.
#
Expand Down
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ function(_add_target_variant_c_compile_flags)
list(APPEND result "-DSWIFT_RUNTIME_NO_COMPATIBILITY_OVERRIDES")
endif()

if(SWIFT_ENABLE_REFLECTION)
list(APPEND result "-DSWIFT_ENABLE_REFLECTION")
endif()

if(SWIFT_RUNTIME_MACHO_NO_DYLD)
list(APPEND result "-DSWIFT_RUNTIME_MACHO_NO_DYLD")
endif()
Expand Down
6 changes: 6 additions & 0 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ function(_compile_swift_files
list(APPEND swift_flags "-Xfrontend" "-emit-sorted-sil")
endif()

if(NOT SWIFT_ENABLE_REFLECTION)
list(APPEND swift_flags "-Xfrontend" "-disable-reflection-metadata")
else()
list(APPEND swift_flags "-D" "SWIFT_ENABLE_REFLECTION")
endif()

# FIXME: Cleaner way to do this?
if(SWIFTFILE_IS_STDLIB_CORE)
list(APPEND swift_flags
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/core/AnyHashable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,15 @@ extension AnyHashable: CustomDebugStringConvertible {
}
}

#if SWIFT_ENABLE_REFLECTION
extension AnyHashable: CustomReflectable {
public var customMirror: Mirror {
return Mirror(
self,
children: ["value": base])
}
}
#endif

@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
extension AnyHashable: _HasCustomAnyHashableRepresentation {
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,7 @@ extension Array {
}
}

#if SWIFT_ENABLE_REFLECTION
extension Array: CustomReflectable {
/// A mirror that reflects the array.
public var customMirror: Mirror {
Expand All @@ -1423,6 +1424,7 @@ extension Array: CustomReflectable {
displayStyle: .collection)
}
}
#endif

extension Array: CustomStringConvertible, CustomDebugStringConvertible {
/// A textual representation of the array and its elements.
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/core/ArraySlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,7 @@ extension ArraySlice: RangeReplaceableCollection {
}
}

#if SWIFT_ENABLE_REFLECTION
extension ArraySlice: CustomReflectable {
/// A mirror that reflects the array.
public var customMirror: Mirror {
Expand All @@ -1127,6 +1128,7 @@ extension ArraySlice: CustomReflectable {
displayStyle: .collection)
}
}
#endif

extension ArraySlice: CustomStringConvertible, CustomDebugStringConvertible {
/// A textual representation of the array and its elements.
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/core/ClosedRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,14 @@ extension ClosedRange: CustomDebugStringConvertible {
}
}

#if SWIFT_ENABLE_REFLECTION
extension ClosedRange: CustomReflectable {
public var customMirror: Mirror {
return Mirror(
self, children: ["lowerBound": lowerBound, "upperBound": upperBound])
}
}
#endif

extension ClosedRange {
/// Returns a copy of this range clamped to the given limiting range.
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/core/CollectionOfOne.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,13 @@ extension CollectionOfOne: CustomDebugStringConvertible {
}
}

#if SWIFT_ENABLE_REFLECTION
extension CollectionOfOne: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, children: ["element": _element])
}
}
#endif

extension CollectionOfOne: Sendable where Element: Sendable { }
extension CollectionOfOne.Iterator: Sendable where Element: Sendable { }
2 changes: 2 additions & 0 deletions stdlib/public/core/ContiguousArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ extension ContiguousArray: RangeReplaceableCollection {
}
}

#if SWIFT_ENABLE_REFLECTION
extension ContiguousArray: CustomReflectable {
/// A mirror that reflects the array.
public var customMirror: Mirror {
Expand All @@ -1028,6 +1029,7 @@ extension ContiguousArray: CustomReflectable {
displayStyle: .collection)
}
}
#endif

extension ContiguousArray: CustomStringConvertible, CustomDebugStringConvertible {
/// A textual representation of the array and its elements.
Expand Down
4 changes: 4 additions & 0 deletions stdlib/public/core/DebuggerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import SwiftShims

#if SWIFT_ENABLE_REFLECTION

@frozen // namespace
public enum _DebuggerSupport {
private enum CollectionStatus {
Expand Down Expand Up @@ -262,6 +264,8 @@ public func _stringForPrintObject(_ value: Any) -> String {
return _DebuggerSupport.stringForPrintObject(value)
}

#endif // SWIFT_ENABLE_REFLECTION

public func _debuggerTestingCheckExpect(_: String, _: String) { }

// Utilities to get refcount(s) of class objects.
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/core/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,7 @@ extension Dictionary.Iterator: IteratorProtocol {
}
}

#if SWIFT_ENABLE_REFLECTION
extension Dictionary.Iterator: CustomReflectable {
/// A mirror that reflects the iterator.
public var customMirror: Mirror {
Expand All @@ -2049,6 +2050,7 @@ extension Dictionary: CustomReflectable {
return Mirror(self, unlabeledChildren: self, displayStyle: style)
}
}
#endif

extension Dictionary {
/// Removes and returns the first key-value pair of the dictionary if the
Expand Down
3 changes: 3 additions & 0 deletions stdlib/public/core/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//
//===----------------------------------------------------------------------===//

#if SWIFT_ENABLE_REFLECTION

/// Dumps the given object's contents using its mirror to the specified output
/// stream.
///
Expand Down Expand Up @@ -247,3 +249,4 @@ internal func _dumpSuperclass_unlocked<TargetStream: TextOutputStream>(
}
}

#endif // SWIFT_ENABLE_REFLECTION
4 changes: 4 additions & 0 deletions stdlib/public/core/MigrationSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,14 @@ extension Substring {
}
}

#if SWIFT_ENABLE_REFLECTION
extension Substring: _CustomPlaygroundQuickLookable {
@available(swift, deprecated: 4.2/*, obsoleted: 5.0*/, message: "Substring.customPlaygroundQuickLook will be removed in a future Swift version")
public var customPlaygroundQuickLook: _PlaygroundQuickLook {
return String(self).customPlaygroundQuickLook
}
}
#endif

extension Collection {
// FIXME: <rdar://problem/34142121>
Expand Down Expand Up @@ -630,6 +632,7 @@ public enum _PlaygroundQuickLook {
case _raw([UInt8], String)
}

#if SWIFT_ENABLE_REFLECTION
extension _PlaygroundQuickLook {
/// Creates a new Quick Look for the given instance.
///
Expand Down Expand Up @@ -663,6 +666,7 @@ extension _PlaygroundQuickLook {
}
}
}
#endif

/// A type that explicitly supplies its own playground Quick Look.
///
Expand Down
70 changes: 70 additions & 0 deletions stdlib/public/core/Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// FIXME: ExistentialCollection needs to be supported before this will work
// without the ObjC Runtime.

#if SWIFT_ENABLE_REFLECTION

/// A representation of the substructure and display style of an instance of
/// any type.
///
Expand Down Expand Up @@ -456,6 +458,70 @@ extension Mirror {
}
}

#else // SWIFT_ENABLE_REFLECTION

@available(*, unavailable)
public struct Mirror {
public enum AncestorRepresentation {
case generated
case customized(() -> Mirror)
case suppressed
}
public init(reflecting subject: Any) { Builtin.unreachable() }
public typealias Child = (label: String?, value: Any)
public typealias Children = AnyCollection<Child>
public enum DisplayStyle: Sendable {
case `struct`, `class`, `enum`, tuple, optional, collection
case dictionary, `set`
}
public init<Subject, C: Collection>(
_ subject: Subject,
children: C,
displayStyle: DisplayStyle? = nil,
ancestorRepresentation: AncestorRepresentation = .generated
) where C.Element == Child { Builtin.unreachable() }
public init<Subject, C: Collection>(
_ subject: Subject,
unlabeledChildren: C,
displayStyle: DisplayStyle? = nil,
ancestorRepresentation: AncestorRepresentation = .generated
) { Builtin.unreachable() }
public init<Subject>(
_ subject: Subject,
children: KeyValuePairs<String, Any>,
displayStyle: DisplayStyle? = nil,
ancestorRepresentation: AncestorRepresentation = .generated
) { Builtin.unreachable() }
public let subjectType: Any.Type
public let children: Children
public let displayStyle: DisplayStyle?
public var superclassMirror: Mirror? { Builtin.unreachable() }
}

@available(*, unavailable)
public protocol CustomReflectable {
var customMirror: Mirror { get }
}

@available(*, unavailable)
public protocol CustomLeafReflectable: CustomReflectable {}

@available(*, unavailable)
public protocol MirrorPath {}
@available(*, unavailable)
extension Int: MirrorPath {}
@available(*, unavailable)
extension String: MirrorPath {}

@available(*, unavailable)
extension Mirror {
public func descendant(_ first: MirrorPath, _ rest: MirrorPath...) -> Any? {
Builtin.unreachable()
}
}

#endif // SWIFT_ENABLE_REFLECTION

//===--- General Utilities ------------------------------------------------===//

extension String {
Expand Down Expand Up @@ -693,6 +759,8 @@ extension String {
}
}

#if SWIFT_ENABLE_REFLECTION

/// Reflection for `Mirror` itself.
extension Mirror: CustomStringConvertible {
public var description: String {
Expand All @@ -705,3 +773,5 @@ extension Mirror: CustomReflectable {
return Mirror(self, children: [:])
}
}

#endif // SWIFT_ENABLE_REFLECTION
4 changes: 4 additions & 0 deletions stdlib/public/core/Mirrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//
//===----------------------------------------------------------------------===//

#if SWIFT_ENABLE_REFLECTION

extension Float: CustomReflectable {
/// A mirror that reflects the `Float` instance.
public var customMirror: Mirror {
Expand Down Expand Up @@ -258,3 +260,5 @@ extension Float80: CustomReflectable {
}
}
#endif

#endif
2 changes: 2 additions & 0 deletions stdlib/public/core/Optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ extension Optional: CustomDebugStringConvertible {
}
}

#if SWIFT_ENABLE_REFLECTION
extension Optional: CustomReflectable {
public var customMirror: Mirror {
switch self {
Expand All @@ -292,6 +293,7 @@ extension Optional: CustomReflectable {
}
}
}
#endif

@_transparent
public // COMPILER_INTRINSIC
Expand Down
12 changes: 12 additions & 0 deletions stdlib/public/core/OutputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ internal func _fallbackEnumRawValue<T>(_ value: T) -> Int64? {
}
}

#if SWIFT_ENABLE_REFLECTION
/// Do our best to print a value that cannot be printed directly.
@_semantics("optimize.sil.specialize.generic.never")
internal func _adHocPrint_unlocked<T, TargetStream: TextOutputStream>(
Expand Down Expand Up @@ -396,6 +397,7 @@ internal func _adHocPrint_unlocked<T, TargetStream: TextOutputStream>(
}
}
}
#endif

@usableFromInline
@_semantics("optimize.sil.specialize.generic.never")
Expand Down Expand Up @@ -435,8 +437,12 @@ internal func _print_unlocked<T, TargetStream: TextOutputStream>(
return
}

#if SWIFT_ENABLE_REFLECTION
let mirror = Mirror(reflecting: value)
_adHocPrint_unlocked(value, mirror, &target, isDebugPrint: false)
#else
target.write("(value cannot be printed without reflection)")
#endif
}

//===----------------------------------------------------------------------===//
Expand All @@ -463,10 +469,15 @@ public func _debugPrint_unlocked<T, TargetStream: TextOutputStream>(
return
}

#if SWIFT_ENABLE_REFLECTION
let mirror = Mirror(reflecting: value)
_adHocPrint_unlocked(value, mirror, &target, isDebugPrint: true)
#else
target.write("(value cannot be printed without reflection)")
#endif
}

#if SWIFT_ENABLE_REFLECTION
@_semantics("optimize.sil.specialize.generic.never")
internal func _dumpPrint_unlocked<T, TargetStream: TextOutputStream>(
_ value: T, _ mirror: Mirror, _ target: inout TargetStream
Expand Down Expand Up @@ -533,6 +544,7 @@ internal func _dumpPrint_unlocked<T, TargetStream: TextOutputStream>(

_adHocPrint_unlocked(value, mirror, &target, isDebugPrint: true)
}
#endif

//===----------------------------------------------------------------------===//
// OutputStreams
Expand Down
Loading