|
| 1 | +//===--- SequenceUtilities.swift ------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Basic |
| 14 | + |
| 15 | +/// Types conforming to `HasName` will be displayed by their name (instead of the |
| 16 | +/// full object) in collection descriptions. |
| 17 | +/// |
| 18 | +/// This is useful to make collections, e.g. of BasicBlocks or Functions, readable. |
| 19 | +public protocol HasShortDescription { |
| 20 | + var shortDescription: String { get } |
| 21 | +} |
| 22 | + |
| 23 | +private struct CustomMirrorChild : CustomStringConvertible, NoReflectionChildren { |
| 24 | + public var description: String |
| 25 | + |
| 26 | + public init(description: String) { self.description = description } |
| 27 | +} |
| 28 | + |
| 29 | +/// Makes a Sequence's `description` and `customMirror` formatted like Array, e.g. [a, b, c]. |
| 30 | +public protocol FormattedLikeArray : Sequence, CustomStringConvertible, CustomReflectable { |
| 31 | +} |
| 32 | + |
| 33 | +extension FormattedLikeArray { |
| 34 | + /// Display a Sequence in an array like format, e.g. [a, b, c] |
| 35 | + public var description: String { |
| 36 | + "[" + map { |
| 37 | + if let named = $0 as? HasShortDescription { |
| 38 | + return named.shortDescription |
| 39 | + } |
| 40 | + return String(describing: $0) |
| 41 | + }.joined(separator: ", ") + "]" |
| 42 | + } |
| 43 | + |
| 44 | + /// The mirror which adds the children of a Sequence, similar to `Array`. |
| 45 | + public var customMirror: Mirror { |
| 46 | + // If the one-line description is not too large, print that instead of the |
| 47 | + // children in separate lines. |
| 48 | + if description.count <= 80 { |
| 49 | + return Mirror(self, children: []) |
| 50 | + } |
| 51 | + let c: [Mirror.Child] = map { |
| 52 | + let val: Any |
| 53 | + if let named = $0 as? HasShortDescription { |
| 54 | + val = CustomMirrorChild(description: named.shortDescription) |
| 55 | + } else { |
| 56 | + val = $0 |
| 57 | + } |
| 58 | + return (label: nil, value: val) |
| 59 | + } |
| 60 | + return Mirror(self, children: c, displayStyle: .collection) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// RandomAccessCollection which bridges to some C++ array. |
| 65 | +/// |
| 66 | +/// It fixes the default reflection for bridged random access collections, which usually have a |
| 67 | +/// `bridged` stored property. |
| 68 | +/// Conforming to this protocol displays the "real" children not just `bridged`. |
| 69 | +public protocol BridgedRandomAccessCollection : RandomAccessCollection, CustomReflectable { |
| 70 | +} |
| 71 | + |
| 72 | +extension BridgedRandomAccessCollection { |
| 73 | + public var customMirror: Mirror { |
| 74 | + Mirror(self, children: self.map { (label: nil, value: $0 as Any) }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// A Sequence which is not consuming and therefore behaves like a Collection. |
| 79 | +/// |
| 80 | +/// Many sequences in SIL and the optimizer should be collections but cannot |
| 81 | +/// because their Index cannot conform to Comparable. Those sequences conform |
| 82 | +/// to CollectionLikeSequence. |
| 83 | +/// |
| 84 | +/// For convenience it also inherits from FormattedLikeArray. |
| 85 | +public protocol CollectionLikeSequence : FormattedLikeArray { |
| 86 | +} |
| 87 | + |
| 88 | +public extension CollectionLikeSequence { |
| 89 | + var isEmpty: Bool { !contains(where: { _ in true }) } |
| 90 | +} |
| 91 | + |
| 92 | +// Also make the lazy sequences a CollectionLikeSequence if the underlying sequence is one. |
| 93 | + |
| 94 | +extension LazySequence : CollectionLikeSequence, |
| 95 | + FormattedLikeArray, CustomStringConvertible, CustomReflectable |
| 96 | + where Base: CollectionLikeSequence {} |
| 97 | + |
| 98 | +extension FlattenSequence : CollectionLikeSequence, |
| 99 | + FormattedLikeArray, CustomStringConvertible, CustomReflectable |
| 100 | + where Base: CollectionLikeSequence {} |
| 101 | + |
| 102 | +extension LazyMapSequence : CollectionLikeSequence, |
| 103 | + FormattedLikeArray, CustomStringConvertible, CustomReflectable |
| 104 | + where Base: CollectionLikeSequence {} |
| 105 | + |
| 106 | +extension LazyFilterSequence : CollectionLikeSequence, |
| 107 | + FormattedLikeArray, CustomStringConvertible, CustomReflectable |
| 108 | + where Base: CollectionLikeSequence {} |
0 commit comments