Skip to content

Commit 6eb5d7d

Browse files
committed
Bridging: Bridge swift::SourceLoc directly
1 parent 06a5670 commit 6eb5d7d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1429
-1644
lines changed

SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ public struct DiagnosticEngine {
7878
at position: SourceLoc?,
7979
highlight: CharSourceRange? = nil,
8080
fixIts: [DiagnosticFixIt] = []) {
81-
82-
let bridgedSourceLoc: BridgedSourceLoc = position.bridged
83-
let highlightStart: BridgedSourceLoc
81+
let bridgedSourceLoc = position.bridgedLocation
82+
let highlightStart: swift.SourceLoc
8483
let highlightLength: UInt32
8584
if let highlight = highlight {
8685
highlightStart = highlight.start.bridged
8786
highlightLength = highlight.byteLength
8887
} else {
89-
highlightStart = BridgedSourceLoc()
88+
highlightStart = .init()
9089
highlightLength = 0
9190
}
9291
var bridgedArgs: [BridgedDiagnosticArgument] = []

SwiftCompilerSources/Sources/Basic/SourceLoc.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2022 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -17,24 +17,20 @@ import BasicBridging
1717
/// In contrast to just having a filename+line+column, this allows displaying the context around
1818
/// the location when printing diagnostics.
1919
public struct SourceLoc {
20-
public let bridged: BridgedSourceLoc
20+
public let bridged: swift.SourceLoc
2121

22-
public init?(bridged: BridgedSourceLoc) {
23-
guard bridged.isValid else {
22+
public init?(bridged: swift.SourceLoc) {
23+
guard bridged.isValid() else {
2424
return nil
2525
}
2626
self.bridged = bridged
2727
}
2828
}
2929

30-
extension SourceLoc {
31-
public func advanced(by n: Int) -> SourceLoc {
32-
SourceLoc(bridged: bridged.advanced(by: n))!
33-
}
34-
}
35-
36-
extension Optional where Wrapped == SourceLoc {
37-
public var bridged: BridgedSourceLoc {
30+
extension Optional<SourceLoc> {
31+
// TODO: This can go back to being 'bridged' once we upgrade to a toolchain
32+
// where https://github.com/swiftlang/swift/issues/82609 is fixed.
33+
public var bridgedLocation: swift.SourceLoc {
3834
self?.bridged ?? .init()
3935
}
4036
}
@@ -48,7 +44,7 @@ public struct CharSourceRange {
4844
self.byteLength = byteLength
4945
}
5046

51-
public init?(bridgedStart: BridgedSourceLoc, byteLength: UInt32) {
47+
public init?(bridgedStart: swift.SourceLoc, byteLength: UInt32) {
5248
guard let start = SourceLoc(bridged: bridgedStart) else {
5349
return nil
5450
}

include/swift/AST/ASTBridging.h

Lines changed: 374 additions & 379 deletions
Large diffs are not rendered by default.

include/swift/AST/ASTBridgingImpl.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
3838

3939
BridgedConsumedLookupResult::BridgedConsumedLookupResult(
4040
swift::Identifier name, swift::SourceLoc sourceLoc, SwiftInt flag)
41-
: Name(name), NameLoc(BridgedSourceLoc(sourceLoc)), Flag(flag) {}
41+
: Name(name), NameLoc(swift::SourceLoc(sourceLoc)), Flag(flag) {}
4242

4343
//===----------------------------------------------------------------------===//
4444
// MARK: BridgedDeclNameRef
@@ -141,9 +141,8 @@ bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf) {
141141
// MARK: BridgedDeclObj
142142
//===----------------------------------------------------------------------===//
143143

144-
BridgedSourceLoc BridgedDeclObj::getLoc() const {
145-
swift::SourceLoc sourceLoc = unbridged()->getLoc();
146-
return BridgedSourceLoc(sourceLoc.getOpaquePointerValue());
144+
swift::SourceLoc BridgedDeclObj::getLoc() const {
145+
return unbridged()->getLoc();
147146
}
148147

149148
BridgedDeclObj BridgedDeclObj::getModuleContext() const {
@@ -162,8 +161,8 @@ BridgedStringRef BridgedDeclObj::Value_getUserFacingName() const {
162161
return getAs<swift::ValueDecl>()->getBaseName().userFacingName();
163162
}
164163

165-
BridgedSourceLoc BridgedDeclObj::Value_getNameLoc() const {
166-
return BridgedSourceLoc(getAs<swift::ValueDecl>()->getNameLoc().getOpaquePointerValue());
164+
swift::SourceLoc BridgedDeclObj::Value_getNameLoc() const {
165+
return getAs<swift::ValueDecl>()->getNameLoc();
167166
}
168167

169168
bool BridgedDeclObj::hasClangNode() const {
@@ -360,15 +359,15 @@ BridgedVarDecl_asAbstractStorageDecl(BridgedVarDecl decl) {
360359
//===----------------------------------------------------------------------===//
361360

362361
swift::Argument BridgedCallArgument::unbridged() const {
363-
return swift::Argument(labelLoc.unbridged(), label, argExpr.unbridged());
362+
return swift::Argument(labelLoc, label, argExpr.unbridged());
364363
}
365364

366365
//===----------------------------------------------------------------------===//
367366
// MARK: BridgedLabeledStmtInfo
368367
//===----------------------------------------------------------------------===//
369368

370369
swift::LabeledStmtInfo BridgedLabeledStmtInfo::unbridged() const {
371-
return {Name, Loc.unbridged()};
370+
return {Name, Loc};
372371
}
373372

374373
//===----------------------------------------------------------------------===//
@@ -845,9 +844,7 @@ swift::IfConfigClauseRangeInfo BridgedIfConfigClauseRangeInfo::unbridged() const
845844
break;
846845
}
847846

848-
return swift::IfConfigClauseRangeInfo(directiveLoc.unbridged(),
849-
bodyLoc.unbridged(),
850-
endLoc.unbridged(),
847+
return swift::IfConfigClauseRangeInfo(directiveLoc, bodyLoc, endLoc,
851848
clauseKind);
852849
}
853850

@@ -916,13 +913,13 @@ BridgedVarDecl BridgedCaptureListEntry::getVarDecl() const {
916913
//===----------------------------------------------------------------------===//
917914

918915
void BridgedFloatLiteralExpr_setNegative(BridgedFloatLiteralExpr cExpr,
919-
BridgedSourceLoc cLoc) {
920-
cExpr.unbridged()->setNegative(cLoc.unbridged());
916+
swift::SourceLoc loc) {
917+
cExpr.unbridged()->setNegative(loc);
921918
}
922919

923920
void BridgedIntegerLiteralExpr_setNegative(BridgedIntegerLiteralExpr cExpr,
924-
BridgedSourceLoc cLoc) {
925-
cExpr.unbridged()->setNegative(cLoc.unbridged());
921+
swift::SourceLoc loc) {
922+
cExpr.unbridged()->setNegative(loc);
926923
}
927924

928925
//===----------------------------------------------------------------------===//

include/swift/Basic/BasicBridging.h

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "swift/Basic/SwiftBridging.h"
3232

3333
#include "swift/Basic/BridgedSwiftObject.h"
34+
#include "swift/Basic/SourceLoc.h"
3435
#include <stddef.h>
3536
#include <stdint.h>
3637

@@ -40,7 +41,6 @@
4041
#include <cassert>
4142
#include "llvm/CAS/CASReference.h"
4243

43-
#include "swift/Basic/SourceLoc.h"
4444
#include "llvm/ADT/StringRef.h"
4545
#include "llvm/ADT/APInt.h"
4646
#include <string>
@@ -317,50 +317,22 @@ class BridgedOStream {
317317

318318
BridgedOStream Bridged_dbgs();
319319

320-
//===----------------------------------------------------------------------===//
321-
// MARK: SourceLoc
322-
//===----------------------------------------------------------------------===//
323-
324-
class BridgedSourceLoc {
325-
const void *_Nullable Raw;
326-
327-
public:
328-
BridgedSourceLoc() : Raw(nullptr) {}
329-
330-
SWIFT_NAME("init(raw:)")
331-
BridgedSourceLoc(const void *_Nullable raw) : Raw(raw) {}
332-
333-
BRIDGED_INLINE BridgedSourceLoc(swift::SourceLoc loc);
334-
335-
BRIDGED_INLINE swift::SourceLoc unbridged() const;
336-
337-
SWIFT_IMPORT_UNSAFE
338-
const void *_Nullable getOpaquePointerValue() const { return Raw; }
339-
340-
SWIFT_COMPUTED_PROPERTY
341-
bool getIsValid() const { return Raw != nullptr; }
342-
343-
SWIFT_NAME("advanced(by:)")
344-
BRIDGED_INLINE
345-
BridgedSourceLoc advancedBy(size_t n) const;
346-
};
347-
348320
//===----------------------------------------------------------------------===//
349321
// MARK: SourceRange
350322
//===----------------------------------------------------------------------===//
351323

352324
class BridgedSourceRange {
353325
public:
354326
SWIFT_NAME("start")
355-
BridgedSourceLoc Start;
327+
swift::SourceLoc Start;
356328

357329
SWIFT_NAME("end")
358-
BridgedSourceLoc End;
330+
swift::SourceLoc End;
359331

360332
BridgedSourceRange() : Start(), End() {}
361333

362334
SWIFT_NAME("init(start:end:)")
363-
BridgedSourceRange(BridgedSourceLoc start, BridgedSourceLoc end)
335+
BridgedSourceRange(swift::SourceLoc start, swift::SourceLoc end)
364336
: Start(start), End(end) {}
365337

366338
BRIDGED_INLINE BridgedSourceRange(swift::SourceRange range);
@@ -375,21 +347,21 @@ class BridgedSourceRange {
375347
class BridgedCharSourceRange {
376348
public:
377349
SWIFT_UNAVAILABLE("Use '.start' instead")
378-
BridgedSourceLoc Start;
350+
swift::SourceLoc Start;
379351

380352
SWIFT_UNAVAILABLE("Use '.byteLength' instead")
381353
unsigned ByteLength;
382354

383355
SWIFT_NAME("init(start:byteLength:)")
384-
BridgedCharSourceRange(BridgedSourceLoc start, unsigned byteLength)
356+
BridgedCharSourceRange(swift::SourceLoc start, unsigned byteLength)
385357
: Start(start), ByteLength(byteLength) {}
386358

387359
BRIDGED_INLINE BridgedCharSourceRange(swift::CharSourceRange range);
388360

389361
BRIDGED_INLINE swift::CharSourceRange unbridged() const;
390362

391363
SWIFT_COMPUTED_PROPERTY
392-
BridgedSourceLoc getStart() const { return Start; }
364+
swift::SourceLoc getStart() const { return Start; }
393365

394366
SWIFT_COMPUTED_PROPERTY
395367
SwiftInt getByteLength() const { return static_cast<SwiftInt>(ByteLength); }

include/swift/Basic/BasicBridgingImpl.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,6 @@ llvm::StringRef BridgedStringRef::unbridged() const {
3838

3939
llvm::StringRef BridgedOwnedString::unbridgedRef() const { return llvm::StringRef(Data, Length); }
4040

41-
//===----------------------------------------------------------------------===//
42-
// MARK: BridgedSourceLoc
43-
//===----------------------------------------------------------------------===//
44-
45-
BridgedSourceLoc::BridgedSourceLoc(swift::SourceLoc loc)
46-
: Raw(loc.getOpaquePointerValue()) {}
47-
48-
swift::SourceLoc BridgedSourceLoc::unbridged() const {
49-
return swift::SourceLoc::getFromPointer(static_cast<const char *>(Raw));
50-
}
51-
52-
BridgedSourceLoc BridgedSourceLoc::advancedBy(size_t n) const {
53-
return BridgedSourceLoc(unbridged().getAdvancedLoc(n));
54-
}
55-
5641
//===----------------------------------------------------------------------===//
5742
// MARK: BridgedSourceRange
5843
//===----------------------------------------------------------------------===//
@@ -61,7 +46,7 @@ BridgedSourceRange::BridgedSourceRange(swift::SourceRange range)
6146
: Start(range.Start), End(range.End) {}
6247

6348
swift::SourceRange BridgedSourceRange::unbridged() const {
64-
return swift::SourceRange(Start.unbridged(), End.unbridged());
49+
return swift::SourceRange(Start, End);
6550
}
6651

6752
//===----------------------------------------------------------------------===//
@@ -72,7 +57,7 @@ BridgedCharSourceRange::BridgedCharSourceRange(swift::CharSourceRange range)
7257
: Start(range.getStart()), ByteLength(range.getByteLength()) {}
7358

7459
swift::CharSourceRange BridgedCharSourceRange::unbridged() const {
75-
return swift::CharSourceRange(Start.unbridged(), ByteLength);
60+
return swift::CharSourceRange(Start, ByteLength);
7661
}
7762

7863
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)