Skip to content

Commit 286fdd9

Browse files
committed
SwiftCompilerSources: split SIL/Utils.swift into multiple files
1 parent 510649f commit 286fdd9

File tree

9 files changed

+245
-234
lines changed

9 files changed

+245
-234
lines changed

SwiftCompilerSources/Sources/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
add_swift_compiler_module(Basic
1010
SOURCES
1111
SourceLoc.swift
12+
StringParser.swift
1213
Utils.swift)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===--- StringParser.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+
/// A simple utility to parse a string.
14+
public struct StringParser {
15+
private var s: Substring
16+
private let originalLength: Int
17+
18+
private mutating func consumeWhitespace() {
19+
s = s.drop { $0.isWhitespace }
20+
}
21+
22+
public init(_ string: String) {
23+
s = Substring(string)
24+
originalLength = string.count
25+
}
26+
27+
public mutating func isEmpty() -> Bool {
28+
consumeWhitespace()
29+
return s.isEmpty
30+
}
31+
32+
public mutating func consume(_ str: String) -> Bool {
33+
consumeWhitespace()
34+
if !s.starts(with: str) { return false }
35+
s = s.dropFirst(str.count)
36+
return true
37+
}
38+
39+
public mutating func consumeInt(withWhiteSpace: Bool = true) -> Int? {
40+
if withWhiteSpace {
41+
consumeWhitespace()
42+
}
43+
var intStr = ""
44+
s = s.drop {
45+
if $0.isNumber {
46+
intStr.append($0)
47+
return true
48+
}
49+
return false
50+
}
51+
return Int(intStr)
52+
}
53+
54+
public mutating func consumeIdentifier() -> String? {
55+
consumeWhitespace()
56+
var name = ""
57+
s = s.drop {
58+
if $0.isLetter {
59+
name.append($0)
60+
return true
61+
}
62+
return false
63+
}
64+
return name.isEmpty ? nil : name
65+
}
66+
67+
public func throwError(_ message: StaticString) throws -> Never {
68+
throw ParsingError(message: message, position: originalLength - s.count)
69+
}
70+
}
71+
72+
public struct ParsingError : Error {
73+
public let message: StaticString
74+
public let position: Int
75+
}

SwiftCompilerSources/Sources/SIL/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ add_swift_compiler_module(SIL
2222
Location.swift
2323
Operand.swift
2424
Registration.swift
25+
SILStage.swift
2526
SubstitutionMap.swift
2627
Type.swift
27-
Utils.swift
2828
Value.swift
2929
VTable.swift
3030
WitnessTable.swift)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- SILStage.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+
public enum SILStage: Int {
14+
/// "Raw" SIL, emitted by SILGen, but not yet run through guaranteed
15+
/// optimization and diagnostic passes.
16+
///
17+
/// Raw SIL does not have fully-constructed SSA and may contain undiagnosed
18+
/// dataflow errors.
19+
case raw
20+
21+
/// Canonical SIL, which has been run through at least the guaranteed
22+
/// optimization and diagnostic passes.
23+
///
24+
/// Canonical SIL has stricter invariants than raw SIL. It must not contain
25+
/// dataflow errors, and some instructions must be canonicalized to simpler
26+
/// forms.
27+
case canonical
28+
29+
/// Lowered SIL, which has been prepared for IRGen and will no longer
30+
/// be passed to canonical SIL transform passes.
31+
///
32+
/// In lowered SIL, the SILType of all SILValues is its SIL storage
33+
/// type. Explicit storage is required for all address-only and resilient
34+
/// types.
35+
///
36+
/// Generating the initial Raw SIL is typically referred to as lowering (from
37+
/// the AST). To disambiguate, refer to the process of generating the lowered
38+
/// stage of SIL as "address lowering".
39+
case lowered
40+
}

SwiftCompilerSources/Sources/SIL/Utilities/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
swift_compiler_sources(SIL
1010
AccessUtils.swift
11+
SequenceUtilities.swift
1112
SmallProjectionPath.swift
1213
WalkUtils.swift
1314
)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 {}

SwiftCompilerSources/Sources/SIL/Utilities/SmallProjectionPath.swift

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

1313
import Basic
1414

15+
// Needed to make some important utility functions in `Basic` available to importers of `SIL`.
16+
// For example, let `Basic.assert` be used in Optimizer module.
17+
@_exported import Basic
18+
1519
/// A small and very efficient representation of a projection path.
1620
///
1721
/// A `SmallProjectionPath` can be parsed and printed in SIL syntax and parsed from Swift

0 commit comments

Comments
 (0)