Skip to content

Commit ce23252

Browse files
committed
[Basic] Improve some bridging APIs
Improve APIs for BridgedStringRef, BridgedOwnedString, and BridgedSourceLoc.
1 parent 3e3fb58 commit ce23252

File tree

9 files changed

+169
-71
lines changed

9 files changed

+169
-71
lines changed

SwiftCompilerSources/Sources/Basic/SourceLoc.swift

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,19 @@ import ASTBridging
1414

1515
public struct SourceLoc {
1616
/// Points into a source file.
17-
let locationInFile: UnsafePointer<UInt8>
18-
19-
public init?(locationInFile: UnsafePointer<UInt8>?) {
20-
guard let locationInFile = locationInFile else {
21-
return nil
22-
}
23-
self.locationInFile = locationInFile
24-
}
17+
public let bridged: BridgedSourceLoc
2518

2619
public init?(bridged: BridgedSourceLoc) {
27-
guard bridged.isValid() else {
20+
guard bridged.isValid else {
2821
return nil
2922
}
30-
self.locationInFile = bridged.uint8Pointer()!
31-
}
32-
33-
public var bridged: BridgedSourceLoc {
34-
.init(locationInFile)
23+
self.bridged = bridged
3524
}
3625
}
3726

3827
extension SourceLoc {
3928
public func advanced(by n: Int) -> SourceLoc {
40-
SourceLoc(locationInFile: locationInFile.advanced(by: n))!
29+
SourceLoc(bridged: bridged.advanced(by: n))!
4130
}
4231
}
4332

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,23 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
7979
public var description: String { string }
8080

8181
public var count: Int {
82-
Int(_bridged.size())
82+
_bridged.count
8383
}
8484

8585
public subscript(index: Int) -> UInt8 {
86-
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.uintData(), count: count)
86+
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.data, count: count)
8787
return buffer[index]
8888
}
8989

9090
public static func ==(lhs: StringRef, rhs: StringRef) -> Bool {
91-
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.uintData(), count: lhs.count)
92-
let rhsBuffer = UnsafeBufferPointer<UInt8>(start: rhs._bridged.uintData(), count: rhs.count)
91+
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: lhs.count)
92+
let rhsBuffer = UnsafeBufferPointer<UInt8>(start: rhs._bridged.data, count: rhs.count)
9393
if lhsBuffer.count != rhsBuffer.count { return false }
9494
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)
9595
}
9696

9797
public static func ==(lhs: StringRef, rhs: StaticString) -> Bool {
98-
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.uintData(), count: lhs.count)
98+
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: lhs.count)
9999
return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in
100100
if lhsBuffer.count != rhsBuffer.count { return false }
101101
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)
@@ -116,17 +116,17 @@ extension String {
116116
public func _withBridgedStringRef<T>(_ c: (BridgedStringRef) -> T) -> T {
117117
var str = self
118118
return str.withUTF8 { buffer in
119-
return c(BridgedStringRef(buffer.baseAddress, buffer.count))
119+
return c(BridgedStringRef(data: buffer.baseAddress, count: buffer.count))
120120
}
121121
}
122122

123123
public init(_ s: BridgedStringRef) {
124-
let buffer = UnsafeBufferPointer<UInt8>(start: s.uintData(), count: Int(s.size()))
124+
let buffer = UnsafeBufferPointer<UInt8>(start: s.data, count: s.count)
125125
self.init(decoding: buffer, as: UTF8.self)
126126
}
127127

128128
public init(taking s: BridgedOwnedString) {
129-
let buffer = UnsafeBufferPointer<UInt8>(start: s.uintData(), count: s.size())
129+
let buffer = UnsafeBufferPointer<UInt8>(start: s.data, count: s.count)
130130
self.init(decoding: buffer, as: UTF8.self)
131131
s.destroy()
132132
}

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ struct FunctionPassContext : MutatingContext {
226226

227227
func loadFunction(name: StaticString, loadCalleesRecursively: Bool) -> Function? {
228228
return name.withUTF8Buffer { (nameBuffer: UnsafeBufferPointer<UInt8>) in
229-
let nameStr = BridgedStringRef(nameBuffer.baseAddress, nameBuffer.count)
229+
let nameStr = BridgedStringRef(data: nameBuffer.baseAddress, count: nameBuffer.count)
230230
return _bridged.loadFunction(nameStr, loadCalleesRecursively).function
231231
}
232232
}
@@ -244,7 +244,7 @@ struct FunctionPassContext : MutatingContext {
244244
/// Returns nil if no such function or multiple matching functions are found.
245245
func lookupStdlibFunction(name: StaticString) -> Function? {
246246
return name.withUTF8Buffer { (nameBuffer: UnsafeBufferPointer<UInt8>) in
247-
let nameStr = BridgedStringRef(nameBuffer.baseAddress, nameBuffer.count)
247+
let nameStr = BridgedStringRef(data: nameBuffer.baseAddress, count: nameBuffer.count)
248248
return _bridged.lookupStdlibFunction(nameStr).function
249249
}
250250
}

SwiftCompilerSources/Sources/Parse/Regex.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ private func _RegexLiteralLexingFn(
6363
if let error = error {
6464
// Emit diagnostic if diagnostics are enabled.
6565
if let diagEngine = DiagnosticEngine(bridged: bridgedDiagnosticEngine) {
66-
let startLoc = SourceLoc(
67-
locationInFile: error.location.assumingMemoryBound(to: UInt8.self))!
66+
let startLoc = SourceLoc(bridged: BridgedSourceLoc(raw: error.location))!
6867
diagEngine.diagnose(startLoc, .foreign_diagnostic, error.message)
6968
}
7069
return error.completelyErroneous

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
112112

113113
public func hasSemanticsAttribute(_ attr: StaticString) -> Bool {
114114
attr.withUTF8Buffer { (buffer: UnsafeBufferPointer<UInt8>) in
115-
bridged.hasSemanticsAttr(BridgedStringRef(buffer.baseAddress!, buffer.count))
115+
bridged.hasSemanticsAttr(BridgedStringRef(data: buffer.baseAddress!, count: buffer.count))
116116
}
117117
}
118118

include/swift/Basic/BasicBridging.h

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@
3131
#include <cassert>
3232
#include "llvm/CAS/CASReference.h"
3333

34+
#include "swift/Basic/SourceLoc.h"
3435
#include "llvm/ADT/StringRef.h"
3536
#include <string>
3637
#endif
3738

39+
// FIXME: We ought to be importing '<swift/briging>' instead.
40+
#if __has_attribute(swift_name)
41+
#define SWIFT_NAME(NAME) __attribute__((swift_name(NAME)))
42+
#else
43+
#define SWIFT_NAME(NAME)
44+
#endif
45+
3846
#ifdef PURE_BRIDGING_MODE
3947
// In PURE_BRIDGING_MODE, briding functions are not inlined
4048
#define BRIDGED_INLINE
@@ -52,62 +60,98 @@ struct BridgedOStream {
5260
};
5361

5462
class BridgedStringRef {
55-
const char * _Nonnull data;
56-
size_t length;
63+
const char *_Nullable Data;
64+
size_t Length;
5765

5866
public:
5967
#ifdef USED_IN_CPP_SOURCE
60-
BridgedStringRef(llvm::StringRef sref) : data(sref.data()), length(sref.size()) {}
68+
BridgedStringRef(llvm::StringRef sref)
69+
: Data(sref.data()), Length(sref.size()) {}
6170

62-
llvm::StringRef get() const { return llvm::StringRef(data, length); }
71+
llvm::StringRef get() const { return llvm::StringRef(Data, Length); }
6372
#endif
6473

65-
BridgedStringRef(const char * _Nullable data, size_t length)
66-
: data(data), length(length) {}
74+
SWIFT_NAME("init(data:count:)")
75+
BridgedStringRef(const char *_Nullable data, size_t length)
76+
: Data(data), Length(length) {}
6777

68-
SWIFT_IMPORT_UNSAFE const uint8_t * _Nonnull uintData() const {
69-
return (const uint8_t * _Nonnull)data;
70-
}
71-
SwiftInt size() const { return (SwiftInt)length; }
7278
void write(BridgedOStream os) const;
7379
};
7480

81+
SWIFT_NAME("getter:BridgedStringRef.data(self:)")
82+
BRIDGED_INLINE
83+
const uint8_t *_Nullable BridgedStringRef_data(BridgedStringRef str);
84+
85+
SWIFT_NAME("getter:BridgedStringRef.count(self:)")
86+
BRIDGED_INLINE SwiftInt BridgedStringRef_count(BridgedStringRef str);
87+
88+
SWIFT_NAME("getter:BridgedStringRef.isEmpty(self:)")
89+
BRIDGED_INLINE bool BridgedStringRef_empty(BridgedStringRef str);
90+
7591
class BridgedOwnedString {
76-
char * _Nonnull data;
77-
size_t length;
92+
char *_Nonnull Data;
93+
size_t Length;
7894

7995
public:
8096
#ifdef USED_IN_CPP_SOURCE
8197
BridgedOwnedString(const std::string &stringToCopy);
98+
99+
llvm::StringRef getRef() const { return llvm::StringRef(Data, Length); }
82100
#endif
83101

84-
SWIFT_IMPORT_UNSAFE const uint8_t * _Nonnull uintData() const {
85-
return (const uint8_t * _Nonnull)(data ? data : "");
86-
}
87-
SwiftInt size() const { return (SwiftInt)length; }
88102
void destroy() const;
89103
};
90104

105+
SWIFT_NAME("getter:BridgedOwnedString.data(self:)")
106+
BRIDGED_INLINE
107+
const uint8_t *_Nullable BridgedOwnedString_data(BridgedOwnedString str);
108+
109+
SWIFT_NAME("getter:BridgedOwnedString.count(self:)")
110+
BRIDGED_INLINE SwiftInt BridgedOwnedString_count(BridgedOwnedString str);
111+
112+
SWIFT_NAME("getter:BridgedOwnedString.isEmpty(self:)")
113+
BRIDGED_INLINE bool BridgedOwnedString_empty(BridgedOwnedString str);
114+
91115
class BridgedSourceLoc {
92-
const void * _Nullable opaquePointer;
116+
const void *_Nullable Raw;
117+
93118
public:
94-
BridgedSourceLoc() : opaquePointer(nullptr) {}
95-
BridgedSourceLoc(const void * _Nullable loc) : opaquePointer(loc) {}
119+
BridgedSourceLoc() : Raw(nullptr) {}
96120

97-
bool isValid() const { return opaquePointer != nullptr; }
98-
SWIFT_IMPORT_UNSAFE const uint8_t * _Nullable uint8Pointer() const {
99-
return (const uint8_t * _Nullable)opaquePointer;
100-
}
101-
const char * _Nullable getLoc() const {
102-
return (const char * _Nullable)opaquePointer;
121+
SWIFT_NAME("init(raw:)")
122+
BridgedSourceLoc(const void *_Nullable raw) : Raw(raw) {}
123+
124+
#ifdef USED_IN_CPP_SOURCE
125+
BridgedSourceLoc(swift::SourceLoc loc) : Raw(loc.getOpaquePointerValue()) {}
126+
127+
swift::SourceLoc get() const {
128+
return swift::SourceLoc(
129+
llvm::SMLoc::getFromPointer(static_cast<const char *>(Raw)));
103130
}
131+
#endif
132+
133+
SWIFT_IMPORT_UNSAFE
134+
const void *_Nullable getOpaquePointerValue() const { return Raw; }
135+
136+
SWIFT_NAME("advanced(by:)")
137+
BRIDGED_INLINE
138+
BridgedSourceLoc advancedBy(size_t n) const;
104139
};
105140

141+
SWIFT_NAME("getter:BridgedSourceLoc.isValid(self:)")
142+
BRIDGED_INLINE bool BridgedSourceLoc_isValid(BridgedSourceLoc str);
143+
106144
struct BridgedArrayRef {
107145
const void * _Nullable data;
108146
size_t numElements;
109147
};
110148

111149
SWIFT_END_NULLABILITY_ANNOTATIONS
112150

151+
#ifndef PURE_BRIDGING_MODE
152+
// In _not_ PURE_BRIDGING_MODE, bridging functions are inlined and therefore
153+
// included in the header file.
154+
#include "BasicBridgingImpl.h"
155+
#endif
156+
113157
#endif
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- BasicBridgingImpl.h - header for the swift BasicBridging module --===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 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+
#ifndef SWIFT_BASIC_BASICBRIDGINGIMPL_H
14+
#define SWIFT_BASIC_BASICBRIDGINGIMPL_H
15+
16+
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
17+
18+
//===----------------------------------------------------------------------===//
19+
// BridgedStringRef
20+
//===----------------------------------------------------------------------===//
21+
22+
const uint8_t *_Nullable BridgedStringRef_data(BridgedStringRef str) {
23+
return (const uint8_t *)str.get().data();
24+
}
25+
26+
SwiftInt BridgedStringRef_count(BridgedStringRef str) {
27+
return (SwiftInt)str.get().size();
28+
}
29+
30+
bool BridgedStringRef_empty(BridgedStringRef str) { return str.get().empty(); }
31+
32+
//===----------------------------------------------------------------------===//
33+
// BridgedOwnedString
34+
//===----------------------------------------------------------------------===//
35+
36+
const uint8_t *_Nullable BridgedOwnedString_data(BridgedOwnedString str) {
37+
auto *data = str.getRef().data();
38+
return (const uint8_t *)(data ? data : "");
39+
}
40+
41+
SwiftInt BridgedOwnedString_count(BridgedOwnedString str) {
42+
return (SwiftInt)str.getRef().size();
43+
}
44+
45+
bool BridgedOwnedString_empty(BridgedOwnedString str) {
46+
return str.getRef().empty();
47+
}
48+
49+
//===----------------------------------------------------------------------===//
50+
// BridgedSourceLoc
51+
//===----------------------------------------------------------------------===//
52+
53+
bool BridgedSourceLoc_isValid(BridgedSourceLoc str) {
54+
return str.getOpaquePointerValue() != nullptr;
55+
}
56+
57+
BridgedSourceLoc BridgedSourceLoc::advancedBy(size_t n) const {
58+
return BridgedSourceLoc(get().getAdvancedLoc(n));
59+
}
60+
61+
SWIFT_END_NULLABILITY_ANNOTATIONS
62+
63+
#endif // SWIFT_BASIC_BASICBRIDGINGIMPL_H

lib/AST/ASTBridging.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,12 @@ BridgedDiagnosticArgument::BridgedDiagnosticArgument(BridgedStringRef s)
3737
static_assert(sizeof(BridgedDiagnosticFixIt) >= sizeof(DiagnosticInfo::FixIt),
3838
"BridgedDiagnosticFixIt has wrong size");
3939

40-
static SourceLoc getSourceLoc(BridgedSourceLoc bridgedLoc) {
41-
return SourceLoc(llvm::SMLoc::getFromPointer(bridgedLoc.getLoc()));
42-
}
43-
44-
BridgedDiagnosticFixIt::BridgedDiagnosticFixIt(BridgedSourceLoc start, uint32_t length, BridgedStringRef text)
45-
: BridgedDiagnosticFixIt(DiagnosticInfo::FixIt(
46-
CharSourceRange(getSourceLoc(start), length),
47-
text.get(),
48-
llvm::ArrayRef<DiagnosticArgument>())) {}
40+
BridgedDiagnosticFixIt::BridgedDiagnosticFixIt(BridgedSourceLoc start,
41+
uint32_t length,
42+
BridgedStringRef text)
43+
: BridgedDiagnosticFixIt(DiagnosticInfo::FixIt(
44+
CharSourceRange(start.get(), length), text.get(),
45+
llvm::ArrayRef<DiagnosticArgument>())) {}
4946

5047
void DiagnosticEngine_diagnose(
5148
BridgedDiagEngine bridgedEngine, BridgedSourceLoc loc,
@@ -60,11 +57,11 @@ void DiagnosticEngine_diagnose(
6057
for (auto arg : getArrayRef<BridgedDiagnosticArgument>(bridgedArguments)) {
6158
arguments.push_back(arg.get());
6259
}
63-
auto inflight = D->diagnose(SourceLoc(llvm::SMLoc::getFromPointer(loc.getLoc())), diagID, arguments);
60+
auto inflight = D->diagnose(loc.get(), diagID, arguments);
6461

6562
// Add highlight.
6663
if (highlightStart.isValid()) {
67-
CharSourceRange highlight(getSourceLoc(highlightStart), (unsigned)hightlightLength);
64+
CharSourceRange highlight(highlightStart.get(), (unsigned)hightlightLength);
6865
inflight.highlightChars(highlight.getStart(), highlight.getEnd());
6966
}
7067

0 commit comments

Comments
 (0)