Skip to content

Commit cd456fa

Browse files
committed
Swift SIL: make Function.name and GlobalVariable.name return a StringRef and not a String
and introduce the StringRef struct. It's more efficient. Also, rename the `HasName` protocol to `HasShortDescription`, which introduces the new requirement `shortDescription`. This is need because `name` now has `StringRef` type and not `String` anymore
1 parent e819415 commit cd456fa

File tree

6 files changed

+43
-13
lines changed

6 files changed

+43
-13
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

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

1313
@_exported import BasicBridging
1414

15+
//===----------------------------------------------------------------------===//
16+
// StringRef
17+
//===----------------------------------------------------------------------===//
18+
19+
public struct StringRef : CustomStringConvertible, CustomReflectable {
20+
let _bridged : BridgedStringRef
21+
22+
public init(bridged: BridgedStringRef) { self._bridged = bridged }
23+
24+
public var string: String { _bridged.string }
25+
public var description: String { string }
26+
public var customMirror: Mirror { Mirror(self, children: []) }
27+
28+
public static func ==(lhs: StringRef, rhs: StaticString) -> Bool {
29+
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: Int(lhs._bridged.length))
30+
return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in
31+
if lhsBuffer.count != rhsBuffer.count { return false }
32+
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)
33+
}
34+
}
35+
36+
public static func !=(lhs: StringRef, rhs: StaticString) -> Bool { !(lhs == rhs) }
37+
}
38+
1539
//===----------------------------------------------------------------------===//
1640
// Bridging Utilities
1741
//===----------------------------------------------------------------------===//

SwiftCompilerSources/Sources/Optimizer/InstructionPasses/SimplifyBeginCOWMutation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Basic
1314
import SIL
1415

1516
/// Simplify begin_cow_mutation instructions.

SwiftCompilerSources/Sources/SIL/BasicBlock.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import Basic
1414
import SILBridging
1515

16-
final public class BasicBlock : ListNode, CustomStringConvertible, HasName {
16+
final public class BasicBlock : ListNode, CustomStringConvertible, HasShortDescription {
1717
public var next: BasicBlock? { SILBasicBlock_next(bridged).block }
1818
public var previous: BasicBlock? { SILBasicBlock_previous(bridged).block }
1919

@@ -28,6 +28,7 @@ final public class BasicBlock : ListNode, CustomStringConvertible, HasName {
2828
var s = SILBasicBlock_debugDescription(bridged)
2929
return String(cString: s.c_str())
3030
}
31+
public var shortDescription: String { name }
3132

3233
public var arguments: ArgumentArray { ArgumentArray(block: self) }
3334

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@
1313
import Basic
1414
import SILBridging
1515

16-
final public class Function : CustomStringConvertible, HasName {
16+
final public class Function : CustomStringConvertible, HasShortDescription {
1717
public private(set) var effects = FunctionEffects()
1818

19-
public var name: String {
20-
return SILFunction_getName(bridged).string
19+
public var name: StringRef {
20+
return StringRef(bridged: SILFunction_getName(bridged))
2121
}
2222

2323
final public var description: String {
2424
var s = SILFunction_debugDescription(bridged)
2525
return String(cString: s.c_str())
2626
}
2727

28+
public var shortDescription: String { name.string }
29+
2830
public var entryBlock: BasicBlock {
2931
SILFunction_firstBlock(bridged).block!
3032
}

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
import Basic
1414
import SILBridging
1515

16-
final public class GlobalVariable : CustomStringConvertible, HasName {
17-
public var name: String {
18-
return SILGlobalVariable_getName(bridged).string
16+
final public class GlobalVariable : CustomStringConvertible, HasShortDescription {
17+
public var name: StringRef {
18+
return StringRef(bridged: SILGlobalVariable_getName(bridged))
1919
}
2020

2121
public var description: String {
2222
var s = SILGlobalVariable_debugDescription(bridged)
2323
return String(cString: s.c_str())
2424
}
2525

26+
public var shortDescription: String { name.string }
27+
2628
// TODO: initializer instructions
2729

2830
var bridged: BridgedGlobalVar { BridgedGlobalVar(obj: SwiftObject(self)) }

SwiftCompilerSources/Sources/SIL/Utils.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public struct ReverseList<NodeType: ListNode> :
8585
/// full object) in collection descriptions.
8686
///
8787
/// This is useful to make collections, e.g. of BasicBlocks or Functions, readable.
88-
public protocol HasName {
89-
var name: String { get }
88+
public protocol HasShortDescription {
89+
var shortDescription: String { get }
9090
}
9191

9292
private struct CustomMirrorChild : CustomStringConvertible, CustomReflectable {
@@ -104,8 +104,8 @@ extension FormattedLikeArray {
104104
/// Display a Sequence in an array like format, e.g. [a, b, c]
105105
public var description: String {
106106
"[" + map {
107-
if let named = $0 as? HasName {
108-
return named.name
107+
if let named = $0 as? HasShortDescription {
108+
return named.shortDescription
109109
}
110110
return String(describing: $0)
111111
}.joined(separator: ", ") + "]"
@@ -120,8 +120,8 @@ extension FormattedLikeArray {
120120
}
121121
let c: [Mirror.Child] = map {
122122
let val: Any
123-
if let named = $0 as? HasName {
124-
val = CustomMirrorChild(description: named.name)
123+
if let named = $0 as? HasShortDescription {
124+
val = CustomMirrorChild(description: named.shortDescription)
125125
} else {
126126
val = $0
127127
}

0 commit comments

Comments
 (0)