Skip to content

Commit 404badb

Browse files
authored
Introduce SWIFT_ENABLE_REFLECTION to turn on/off the support for Mirrors and reflection (#33617)
1 parent b5b008f commit 404badb

32 files changed

+176
-3
lines changed

stdlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ set(SWIFT_STDLIB_ENABLE_LTO OFF CACHE STRING "Build Swift stdlib with LTO. One
102102
must specify the form of LTO by setting this to one of: 'full', 'thin'. This
103103
option only affects the standard library and runtime, not tools.")
104104

105+
option(SWIFT_ENABLE_REFLECTION
106+
"Build stdlib with support for runtime reflection and mirrors."
107+
TRUE)
108+
105109
#
106110
# End of user-configurable options.
107111
#

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ function(_add_target_variant_c_compile_flags)
315315
list(APPEND result "-DSWIFT_RUNTIME_NO_COMPATIBILITY_OVERRIDES")
316316
endif()
317317

318+
if(SWIFT_ENABLE_REFLECTION)
319+
list(APPEND result "-DSWIFT_ENABLE_REFLECTION")
320+
endif()
321+
318322
if(SWIFT_RUNTIME_MACHO_NO_DYLD)
319323
list(APPEND result "-DSWIFT_RUNTIME_MACHO_NO_DYLD")
320324
endif()

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ function(_compile_swift_files
432432
list(APPEND swift_flags "-Xfrontend" "-emit-sorted-sil")
433433
endif()
434434

435+
if(NOT SWIFT_ENABLE_REFLECTION)
436+
list(APPEND swift_flags "-Xfrontend" "-disable-reflection-metadata")
437+
else()
438+
list(APPEND swift_flags "-D" "SWIFT_ENABLE_REFLECTION")
439+
endif()
440+
435441
# FIXME: Cleaner way to do this?
436442
if(SWIFTFILE_IS_STDLIB_CORE)
437443
list(APPEND swift_flags

stdlib/public/core/AnyHashable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,15 @@ extension AnyHashable: CustomDebugStringConvertible {
252252
}
253253
}
254254

255+
#if SWIFT_ENABLE_REFLECTION
255256
extension AnyHashable: CustomReflectable {
256257
public var customMirror: Mirror {
257258
return Mirror(
258259
self,
259260
children: ["value": base])
260261
}
261262
}
263+
#endif
262264

263265
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
264266
extension AnyHashable: _HasCustomAnyHashableRepresentation {

stdlib/public/core/Array.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,7 @@ extension Array {
14141414
}
14151415
}
14161416

1417+
#if SWIFT_ENABLE_REFLECTION
14171418
extension Array: CustomReflectable {
14181419
/// A mirror that reflects the array.
14191420
public var customMirror: Mirror {
@@ -1423,6 +1424,7 @@ extension Array: CustomReflectable {
14231424
displayStyle: .collection)
14241425
}
14251426
}
1427+
#endif
14261428

14271429
extension Array: CustomStringConvertible, CustomDebugStringConvertible {
14281430
/// A textual representation of the array and its elements.

stdlib/public/core/ArraySlice.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ extension ArraySlice: RangeReplaceableCollection {
11181118
}
11191119
}
11201120

1121+
#if SWIFT_ENABLE_REFLECTION
11211122
extension ArraySlice: CustomReflectable {
11221123
/// A mirror that reflects the array.
11231124
public var customMirror: Mirror {
@@ -1127,6 +1128,7 @@ extension ArraySlice: CustomReflectable {
11271128
displayStyle: .collection)
11281129
}
11291130
}
1131+
#endif
11301132

11311133
extension ArraySlice: CustomStringConvertible, CustomDebugStringConvertible {
11321134
/// A textual representation of the array and its elements.

stdlib/public/core/ClosedRange.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,14 @@ extension ClosedRange: CustomDebugStringConvertible {
396396
}
397397
}
398398

399+
#if SWIFT_ENABLE_REFLECTION
399400
extension ClosedRange: CustomReflectable {
400401
public var customMirror: Mirror {
401402
return Mirror(
402403
self, children: ["lowerBound": lowerBound, "upperBound": upperBound])
403404
}
404405
}
406+
#endif
405407

406408
extension ClosedRange {
407409
/// Returns a copy of this range clamped to the given limiting range.

stdlib/public/core/CollectionOfOne.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,13 @@ extension CollectionOfOne: CustomDebugStringConvertible {
165165
}
166166
}
167167

168+
#if SWIFT_ENABLE_REFLECTION
168169
extension CollectionOfOne: CustomReflectable {
169170
public var customMirror: Mirror {
170171
return Mirror(self, children: ["element": _element])
171172
}
172173
}
174+
#endif
173175

174176
extension CollectionOfOne: Sendable where Element: Sendable { }
175177
extension CollectionOfOne.Iterator: Sendable where Element: Sendable { }

stdlib/public/core/ContiguousArray.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@ extension ContiguousArray: RangeReplaceableCollection {
10191019
}
10201020
}
10211021

1022+
#if SWIFT_ENABLE_REFLECTION
10221023
extension ContiguousArray: CustomReflectable {
10231024
/// A mirror that reflects the array.
10241025
public var customMirror: Mirror {
@@ -1028,6 +1029,7 @@ extension ContiguousArray: CustomReflectable {
10281029
displayStyle: .collection)
10291030
}
10301031
}
1032+
#endif
10311033

10321034
extension ContiguousArray: CustomStringConvertible, CustomDebugStringConvertible {
10331035
/// A textual representation of the array and its elements.

stdlib/public/core/DebuggerSupport.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import SwiftShims
1414

15+
#if SWIFT_ENABLE_REFLECTION
16+
1517
@frozen // namespace
1618
public enum _DebuggerSupport {
1719
private enum CollectionStatus {
@@ -262,6 +264,8 @@ public func _stringForPrintObject(_ value: Any) -> String {
262264
return _DebuggerSupport.stringForPrintObject(value)
263265
}
264266

267+
#endif // SWIFT_ENABLE_REFLECTION
268+
265269
public func _debuggerTestingCheckExpect(_: String, _: String) { }
266270

267271
// Utilities to get refcount(s) of class objects.

stdlib/public/core/Dictionary.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,7 @@ extension Dictionary.Iterator: IteratorProtocol {
20332033
}
20342034
}
20352035

2036+
#if SWIFT_ENABLE_REFLECTION
20362037
extension Dictionary.Iterator: CustomReflectable {
20372038
/// A mirror that reflects the iterator.
20382039
public var customMirror: Mirror {
@@ -2049,6 +2050,7 @@ extension Dictionary: CustomReflectable {
20492050
return Mirror(self, unlabeledChildren: self, displayStyle: style)
20502051
}
20512052
}
2053+
#endif
20522054

20532055
extension Dictionary {
20542056
/// Removes and returns the first key-value pair of the dictionary if the

stdlib/public/core/Dump.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if SWIFT_ENABLE_REFLECTION
14+
1315
/// Dumps the given object's contents using its mirror to the specified output
1416
/// stream.
1517
///
@@ -247,3 +249,4 @@ internal func _dumpSuperclass_unlocked<TargetStream: TextOutputStream>(
247249
}
248250
}
249251

252+
#endif // SWIFT_ENABLE_REFLECTION

stdlib/public/core/MigrationSupport.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,14 @@ extension Substring {
309309
}
310310
}
311311

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

319321
extension Collection {
320322
// FIXME: <rdar://problem/34142121>
@@ -630,6 +632,7 @@ public enum _PlaygroundQuickLook {
630632
case _raw([UInt8], String)
631633
}
632634

635+
#if SWIFT_ENABLE_REFLECTION
633636
extension _PlaygroundQuickLook {
634637
/// Creates a new Quick Look for the given instance.
635638
///
@@ -663,6 +666,7 @@ extension _PlaygroundQuickLook {
663666
}
664667
}
665668
}
669+
#endif
666670

667671
/// A type that explicitly supplies its own playground Quick Look.
668672
///

stdlib/public/core/Mirror.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// FIXME: ExistentialCollection needs to be supported before this will work
1313
// without the ObjC Runtime.
1414

15+
#if SWIFT_ENABLE_REFLECTION
16+
1517
/// A representation of the substructure and display style of an instance of
1618
/// any type.
1719
///
@@ -456,6 +458,70 @@ extension Mirror {
456458
}
457459
}
458460

461+
#else // SWIFT_ENABLE_REFLECTION
462+
463+
@available(*, unavailable)
464+
public struct Mirror {
465+
public enum AncestorRepresentation {
466+
case generated
467+
case customized(() -> Mirror)
468+
case suppressed
469+
}
470+
public init(reflecting subject: Any) { Builtin.unreachable() }
471+
public typealias Child = (label: String?, value: Any)
472+
public typealias Children = AnyCollection<Child>
473+
public enum DisplayStyle: Sendable {
474+
case `struct`, `class`, `enum`, tuple, optional, collection
475+
case dictionary, `set`
476+
}
477+
public init<Subject, C: Collection>(
478+
_ subject: Subject,
479+
children: C,
480+
displayStyle: DisplayStyle? = nil,
481+
ancestorRepresentation: AncestorRepresentation = .generated
482+
) where C.Element == Child { Builtin.unreachable() }
483+
public init<Subject, C: Collection>(
484+
_ subject: Subject,
485+
unlabeledChildren: C,
486+
displayStyle: DisplayStyle? = nil,
487+
ancestorRepresentation: AncestorRepresentation = .generated
488+
) { Builtin.unreachable() }
489+
public init<Subject>(
490+
_ subject: Subject,
491+
children: KeyValuePairs<String, Any>,
492+
displayStyle: DisplayStyle? = nil,
493+
ancestorRepresentation: AncestorRepresentation = .generated
494+
) { Builtin.unreachable() }
495+
public let subjectType: Any.Type
496+
public let children: Children
497+
public let displayStyle: DisplayStyle?
498+
public var superclassMirror: Mirror? { Builtin.unreachable() }
499+
}
500+
501+
@available(*, unavailable)
502+
public protocol CustomReflectable {
503+
var customMirror: Mirror { get }
504+
}
505+
506+
@available(*, unavailable)
507+
public protocol CustomLeafReflectable: CustomReflectable {}
508+
509+
@available(*, unavailable)
510+
public protocol MirrorPath {}
511+
@available(*, unavailable)
512+
extension Int: MirrorPath {}
513+
@available(*, unavailable)
514+
extension String: MirrorPath {}
515+
516+
@available(*, unavailable)
517+
extension Mirror {
518+
public func descendant(_ first: MirrorPath, _ rest: MirrorPath...) -> Any? {
519+
Builtin.unreachable()
520+
}
521+
}
522+
523+
#endif // SWIFT_ENABLE_REFLECTION
524+
459525
//===--- General Utilities ------------------------------------------------===//
460526

461527
extension String {
@@ -693,6 +759,8 @@ extension String {
693759
}
694760
}
695761

762+
#if SWIFT_ENABLE_REFLECTION
763+
696764
/// Reflection for `Mirror` itself.
697765
extension Mirror: CustomStringConvertible {
698766
public var description: String {
@@ -705,3 +773,5 @@ extension Mirror: CustomReflectable {
705773
return Mirror(self, children: [:])
706774
}
707775
}
776+
777+
#endif // SWIFT_ENABLE_REFLECTION

stdlib/public/core/Mirrors.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if SWIFT_ENABLE_REFLECTION
14+
1315
extension Float: CustomReflectable {
1416
/// A mirror that reflects the `Float` instance.
1517
public var customMirror: Mirror {
@@ -258,3 +260,5 @@ extension Float80: CustomReflectable {
258260
}
259261
}
260262
#endif
263+
264+
#endif

stdlib/public/core/Optional.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ extension Optional: CustomDebugStringConvertible {
279279
}
280280
}
281281

282+
#if SWIFT_ENABLE_REFLECTION
282283
extension Optional: CustomReflectable {
283284
public var customMirror: Mirror {
284285
switch self {
@@ -292,6 +293,7 @@ extension Optional: CustomReflectable {
292293
}
293294
}
294295
}
296+
#endif
295297

296298
@_transparent
297299
public // COMPILER_INTRINSIC

stdlib/public/core/OutputStream.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ internal func _fallbackEnumRawValue<T>(_ value: T) -> Int64? {
296296
}
297297
}
298298

299+
#if SWIFT_ENABLE_REFLECTION
299300
/// Do our best to print a value that cannot be printed directly.
300301
@_semantics("optimize.sil.specialize.generic.never")
301302
internal func _adHocPrint_unlocked<T, TargetStream: TextOutputStream>(
@@ -396,6 +397,7 @@ internal func _adHocPrint_unlocked<T, TargetStream: TextOutputStream>(
396397
}
397398
}
398399
}
400+
#endif
399401

400402
@usableFromInline
401403
@_semantics("optimize.sil.specialize.generic.never")
@@ -435,8 +437,12 @@ internal func _print_unlocked<T, TargetStream: TextOutputStream>(
435437
return
436438
}
437439

440+
#if SWIFT_ENABLE_REFLECTION
438441
let mirror = Mirror(reflecting: value)
439442
_adHocPrint_unlocked(value, mirror, &target, isDebugPrint: false)
443+
#else
444+
target.write("(value cannot be printed without reflection)")
445+
#endif
440446
}
441447

442448
//===----------------------------------------------------------------------===//
@@ -463,10 +469,15 @@ public func _debugPrint_unlocked<T, TargetStream: TextOutputStream>(
463469
return
464470
}
465471

472+
#if SWIFT_ENABLE_REFLECTION
466473
let mirror = Mirror(reflecting: value)
467474
_adHocPrint_unlocked(value, mirror, &target, isDebugPrint: true)
475+
#else
476+
target.write("(value cannot be printed without reflection)")
477+
#endif
468478
}
469479

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

534545
_adHocPrint_unlocked(value, mirror, &target, isDebugPrint: true)
535546
}
547+
#endif
536548

537549
//===----------------------------------------------------------------------===//
538550
// OutputStreams

0 commit comments

Comments
 (0)