Skip to content

Commit 2f810b4

Browse files
authored
Merge pull request #14678 from mikeash/remove-reflectionlegacy
[Runtime] Remove old-style mirrors in ReflectionLegacy.swift.
2 parents 8414005 + 35e4d1e commit 2f810b4

File tree

12 files changed

+1002
-1968
lines changed

12 files changed

+1002
-1968
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <cassert>
2525
#include <cstdint>
2626
#include "llvm/ADT/StringRef.h"
27+
#include "swift/Runtime/Config.h"
2728

2829
namespace llvm {
2930
class raw_ostream;
@@ -553,4 +554,30 @@ llvm::StringRef makeSymbolicMangledNameStringRef(const char *base);
553554
} // end namespace Demangle
554555
} // end namespace swift
555556

557+
// NB: This function is not used directly in the Swift codebase, but is
558+
// exported for Xcode support and is used by the sanitizers. Please coordinate
559+
// before changing.
560+
//
561+
/// Demangles a Swift symbol name.
562+
///
563+
/// \param mangledName is the symbol name that needs to be demangled.
564+
/// \param mangledNameLength is the length of the string that should be
565+
/// demangled.
566+
/// \param outputBuffer is the user provided buffer where the demangled name
567+
/// will be placed. If nullptr, a new buffer will be malloced. In that case,
568+
/// the user of this API is responsible for freeing the returned buffer.
569+
/// \param outputBufferSize is the size of the output buffer. If the demangled
570+
/// name does not fit into the outputBuffer, the output will be truncated and
571+
/// the size will be updated, indicating how large the buffer should be.
572+
/// \param flags can be used to select the demangling style. TODO: We should
573+
//// define what these will be.
574+
/// \returns the demangled name. Returns nullptr if the input String is not a
575+
/// Swift mangled name.
576+
SWIFT_RUNTIME_EXPORT
577+
char *swift_demangle(const char *mangledName,
578+
size_t mangledNameLength,
579+
char *outputBuffer,
580+
size_t *outputBufferSize,
581+
uint32_t flags);
582+
556583
#endif // SWIFT_DEMANGLING_DEMANGLE_H

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ set(SWIFTLIB_ESSENTIAL
8989
MutableCollection.swift
9090
NewtypeWrapper.swift.gyb
9191
NormalizedCodeUnitIterator.swift
92-
ObjCMirrors.swift
9392
ObjectIdentifier.swift
9493
Optional.swift
9594
OptionSet.swift
@@ -101,7 +100,7 @@ set(SWIFTLIB_ESSENTIAL
101100
RandomAccessCollection.swift
102101
Range.swift
103102
RangeReplaceableCollection.swift
104-
ReflectionLegacy.swift
103+
ReflectionMirror.swift
105104
Repeat.swift
106105
REPL.swift
107106
Reverse.swift

stdlib/public/core/GroupInfo.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@
123123
"Dump.swift",
124124
"Mirror.swift",
125125
"Mirrors.swift",
126-
"ObjCMirrors.swift",
127-
"ObjectIdentifier.swift",
128-
"ReflectionLegacy.swift"
126+
"ReflectionMirror.swift",
127+
"ObjectIdentifier.swift"
129128
],
130129
"Math": [
131130
"SetAlgebra.swift",

stdlib/public/core/Mirror.swift

Lines changed: 12 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ public struct Mirror {
121121
if case let customized as CustomReflectable = subject {
122122
self = customized.customMirror
123123
} else {
124-
self = Mirror(
125-
legacy: _reflect(subject),
126-
subjectType: type(of: subject))
124+
self = Mirror(internalReflecting: subject)
127125
}
128126
}
129127

@@ -164,29 +162,6 @@ public struct Mirror {
164162
@_versioned // FIXME(sil-serialize-all)
165163
internal static func _noSuperclassMirror() -> Mirror? { return nil }
166164

167-
/// Returns the legacy mirror representing the part of `subject`
168-
/// corresponding to the superclass of `staticSubclass`.
169-
@_inlineable // FIXME(sil-serialize-all)
170-
@_versioned // FIXME(sil-serialize-all)
171-
internal static func _legacyMirror(
172-
_ subject: AnyObject, asClass targetSuperclass: AnyClass) -> _Mirror? {
173-
174-
// get a legacy mirror and the most-derived type
175-
var cls: AnyClass = type(of: subject)
176-
var clsMirror = _reflect(subject)
177-
178-
// Walk up the chain of mirrors/classes until we find staticSubclass
179-
while let superclass: AnyClass = _getSuperclass(cls) {
180-
guard let superclassMirror = clsMirror._superMirror() else { break }
181-
182-
if superclass == targetSuperclass { return superclassMirror }
183-
184-
clsMirror = superclassMirror
185-
cls = superclass
186-
}
187-
return nil
188-
}
189-
190165
@_semantics("optimize.sil.specialize.generic.never")
191166
@inline(never)
192167
@_inlineable // FIXME(sil-serialize-all)
@@ -201,14 +176,19 @@ public struct Mirror {
201176
switch ancestorRepresentation {
202177
case .generated:
203178
return {
204-
self._legacyMirror(_unsafeDowncastToAnyObject(fromAny: subject), asClass: superclass).map {
205-
Mirror(legacy: $0, subjectType: superclass)
206-
}
179+
Mirror(internalReflecting: subject, subjectType: superclass)
207180
}
208181
case .customized(let makeAncestor):
209182
return {
210-
Mirror(_unsafeDowncastToAnyObject(fromAny: subject), subjectClass: superclass,
211-
ancestor: makeAncestor())
183+
let ancestor = makeAncestor()
184+
if superclass == ancestor.subjectType
185+
|| ancestor._defaultDescendantRepresentation == .suppressed {
186+
return ancestor
187+
} else {
188+
return Mirror(internalReflecting: subject,
189+
subjectType: superclass,
190+
customAncestor: ancestor)
191+
}
212192
}
213193
case .suppressed:
214194
break
@@ -503,161 +483,6 @@ extension Mirror {
503483
}
504484
}
505485

506-
//===--- Legacy _Mirror Support -------------------------------------------===//
507-
extension Mirror.DisplayStyle {
508-
/// Construct from a legacy `_MirrorDisposition`
509-
@_inlineable // FIXME(sil-serialize-all)
510-
@_versioned // FIXME(sil-serialize-all)
511-
internal init?(legacy: _MirrorDisposition) {
512-
switch legacy {
513-
case .`struct`: self = .`struct`
514-
case .`class`: self = .`class`
515-
case .`enum`: self = .`enum`
516-
case .tuple: self = .tuple
517-
case .aggregate: return nil
518-
case .indexContainer: self = .collection
519-
case .keyContainer: self = .dictionary
520-
case .membershipContainer: self = .`set`
521-
case .container: preconditionFailure("unused!")
522-
case .optional: self = .optional
523-
case .objCObject: self = .`class`
524-
}
525-
}
526-
}
527-
528-
@_inlineable // FIXME(sil-serialize-all)
529-
@_versioned // FIXME(sil-serialize-all)
530-
internal func _isClassSuperMirror(_ t: Any.Type) -> Bool {
531-
#if _runtime(_ObjC)
532-
return t == _ClassSuperMirror.self || t == _ObjCSuperMirror.self
533-
#else
534-
return t == _ClassSuperMirror.self
535-
#endif
536-
}
537-
538-
extension _Mirror {
539-
@_inlineable // FIXME(sil-serialize-all)
540-
@_versioned // FIXME(sil-serialize-all)
541-
internal func _superMirror() -> _Mirror? {
542-
if self.count > 0 {
543-
let childMirror = self[0].1
544-
if _isClassSuperMirror(type(of: childMirror)) {
545-
return childMirror
546-
}
547-
}
548-
return nil
549-
}
550-
}
551-
552-
/// When constructed using the legacy reflection infrastructure, the
553-
/// resulting `Mirror`'s `children` collection will always be
554-
/// upgradable to `AnyRandomAccessCollection` even if it doesn't
555-
/// exhibit appropriate performance. To avoid this pitfall, convert
556-
/// mirrors to use the new style, which only present forward
557-
/// traversal in general.
558-
internal extension Mirror {
559-
/// An adapter that represents a legacy `_Mirror`'s children as
560-
/// a `Collection` with integer `Index`. Note that the performance
561-
/// characteristics of the underlying `_Mirror` may not be
562-
/// appropriate for random access! To avoid this pitfall, convert
563-
/// mirrors to use the new style, which only present forward
564-
/// traversal in general.
565-
@_fixed_layout // FIXME(sil-serialize-all)
566-
@_versioned // FIXME(sil-serialize-all)
567-
internal struct LegacyChildren : RandomAccessCollection {
568-
internal typealias Indices = Range<Int>
569-
570-
@_inlineable // FIXME(sil-serialize-all)
571-
@_versioned // FIXME(sil-serialize-all)
572-
internal init(_ oldMirror: _Mirror) {
573-
self._oldMirror = oldMirror
574-
}
575-
576-
@_inlineable // FIXME(sil-serialize-all)
577-
@_versioned // FIXME(sil-serialize-all)
578-
internal var startIndex: Int {
579-
return _oldMirror._superMirror() == nil ? 0 : 1
580-
}
581-
582-
@_inlineable // FIXME(sil-serialize-all)
583-
@_versioned // FIXME(sil-serialize-all)
584-
internal var endIndex: Int { return _oldMirror.count }
585-
586-
@_inlineable // FIXME(sil-serialize-all)
587-
@_versioned // FIXME(sil-serialize-all)
588-
internal subscript(position: Int) -> Child {
589-
let (label, childMirror) = _oldMirror[position]
590-
return (label: label, value: childMirror.value)
591-
}
592-
593-
@_versioned // FIXME(sil-serialize-all)
594-
internal let _oldMirror: _Mirror
595-
}
596-
597-
/// Initialize for a view of `subject` as `subjectClass`.
598-
///
599-
/// - parameter ancestor: A Mirror for a (non-strict) ancestor of
600-
/// `subjectClass`, to be injected into the resulting hierarchy.
601-
///
602-
/// - parameter legacy: Either `nil`, or a legacy mirror for `subject`
603-
/// as `subjectClass`.
604-
@_inlineable // FIXME(sil-serialize-all)
605-
@_versioned // FIXME(sil-serialize-all)
606-
internal init(
607-
_ subject: AnyObject,
608-
subjectClass: AnyClass,
609-
ancestor: Mirror,
610-
legacy legacyMirror: _Mirror? = nil
611-
) {
612-
if ancestor.subjectType == subjectClass
613-
|| ancestor._defaultDescendantRepresentation == .suppressed {
614-
self = ancestor
615-
}
616-
else {
617-
let legacyMirror = legacyMirror ?? Mirror._legacyMirror(
618-
subject, asClass: subjectClass)!
619-
620-
self = Mirror(
621-
legacy: legacyMirror,
622-
subjectType: subjectClass,
623-
makeSuperclassMirror: {
624-
_getSuperclass(subjectClass).map {
625-
Mirror(
626-
subject,
627-
subjectClass: $0,
628-
ancestor: ancestor,
629-
legacy: legacyMirror._superMirror())
630-
}
631-
})
632-
}
633-
}
634-
635-
@_inlineable // FIXME(sil-serialize-all)
636-
@_versioned // FIXME(sil-serialize-all)
637-
internal init(
638-
legacy legacyMirror: _Mirror,
639-
subjectType: Any.Type,
640-
makeSuperclassMirror: (() -> Mirror?)? = nil
641-
) {
642-
if let makeSuperclassMirror = makeSuperclassMirror {
643-
self._makeSuperclassMirror = makeSuperclassMirror
644-
}
645-
else if let subjectSuperclass = _getSuperclass(subjectType) {
646-
self._makeSuperclassMirror = {
647-
legacyMirror._superMirror().map {
648-
Mirror(legacy: $0, subjectType: subjectSuperclass) }
649-
}
650-
}
651-
else {
652-
self._makeSuperclassMirror = Mirror._noSuperclassMirror
653-
}
654-
self.subjectType = subjectType
655-
self.children = Children(LegacyChildren(legacyMirror))
656-
self.displayStyle = DisplayStyle(legacy: legacyMirror.disposition)
657-
self._defaultDescendantRepresentation = .generated
658-
}
659-
}
660-
661486
//===--- QuickLooks -------------------------------------------------------===//
662487

663488
/// The sum of types that can be used as a Quick Look representation.
@@ -769,7 +594,7 @@ extension PlaygroundQuickLook {
769594
self = customized._defaultCustomPlaygroundQuickLook
770595
}
771596
else {
772-
if let q = _reflect(subject).quickLookObject {
597+
if let q = Mirror.quickLookObject(subject) {
773598
self = q
774599
}
775600
else {

0 commit comments

Comments
 (0)