Skip to content

Commit 404badb

Browse files
authored
Introduce SWIFT_ENABLE_REFLECTION to turn on/off the support for Mirrors and reflection (swiftlang#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.

0 commit comments

Comments
 (0)