Skip to content

Commit b6af4a7

Browse files
committed
[cxx-interop][SwiftCompilerSources] Use swift::SourceLoc instead of BridgedSourceLoc
C++ interop is now enabled in SwiftCompilerSources, so we can remove some of the C bridging layer and use C++ classes directly from Swift. rdar://83361087
1 parent c6a95c6 commit b6af4a7

File tree

9 files changed

+20
-59
lines changed

9 files changed

+20
-59
lines changed

SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public struct DiagnosticEngine {
8080
highlight: CharSourceRange? = nil,
8181
fixIts: [DiagnosticFixIt] = []) {
8282

83-
let bridgedSourceLoc: BridgedSourceLoc = position.bridged
83+
let bridgedSourceLoc: swift.SourceLoc = position.bridged
8484
let bridgedHighlightRange: BridgedCharSourceRange = highlight.bridged
8585
var bridgedArgs: [BridgedDiagnosticArgument] = []
8686
var bridgedFixIts: [BridgedDiagnosticFixIt] = []

SwiftCompilerSources/Sources/Basic/SourceLoc.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public struct SourceLoc {
2121
self.locationInFile = locationInFile
2222
}
2323

24-
public init?(bridged: BridgedSourceLoc) {
25-
guard let locationInFile = bridged.pointer else {
24+
public init?(bridged: swift.SourceLoc) {
25+
guard bridged.isValid() else {
2626
return nil
2727
}
28-
self.init(locationInFile: locationInFile)
28+
self.locationInFile = bridged.getOpaquePointerValue().assumingMemoryBound(to: UInt8.self)
2929
}
3030

31-
public var bridged: BridgedSourceLoc {
32-
.init(pointer: locationInFile)
31+
public var bridged: swift.SourceLoc {
32+
.init(llvm.SMLoc.getFromPointer(locationInFile))
3333
}
3434
}
3535

@@ -40,8 +40,8 @@ extension SourceLoc {
4040
}
4141

4242
extension Optional where Wrapped == SourceLoc {
43-
public var bridged: BridgedSourceLoc {
44-
self?.bridged ?? .init(pointer: nil)
43+
public var bridged: swift.SourceLoc {
44+
self?.bridged ?? .init()
4545
}
4646
}
4747

@@ -68,6 +68,6 @@ public struct CharSourceRange {
6868

6969
extension Optional where Wrapped == CharSourceRange {
7070
public var bridged: BridgedCharSourceRange {
71-
self?.bridged ?? .init(start: .init(pointer: nil), byteLength: 0)
71+
self?.bridged ?? .init(start: .init(), byteLength: 0)
7272
}
7373
}

SwiftCompilerSources/Sources/Parse/Regex.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public func _RegexLiteralParsingFn(
9292
_ versionOut: UnsafeMutablePointer<CUnsignedInt>,
9393
_ captureStructureOut: UnsafeMutableRawPointer,
9494
_ captureStructureSize: CUnsignedInt,
95-
_ bridgedDiagnosticBaseLoc: BridgedSourceLoc,
95+
_ bridgedDiagnosticBaseLoc: swift.SourceLoc,
9696
_ bridgedDiagnosticEngine: BridgedDiagnosticEngine
9797
) -> Bool {
9898
let str = String(cString: inputPtr)

include/swift/AST/ASTBridging.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
#include <stdbool.h>
1919
#include <stddef.h>
2020

21-
#ifdef __cplusplus
22-
extern "C" {
23-
#endif
24-
2521
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2622

2723
//===----------------------------------------------------------------------===//
@@ -56,7 +52,7 @@ typedef struct {
5652
} BridgedDiagnosticArgument;
5753

5854
typedef struct {
59-
BridgedSourceLoc start;
55+
swift::SourceLoc start;
6056
SwiftInt byteLength;
6157
BridgedStringRef text;
6258
} BridgedDiagnosticFixIt;
@@ -70,7 +66,7 @@ typedef struct {
7066
} BridgedOptionalDiagnosticEngine;
7167

7268
// FIXME: Can we bridge InFlightDiagnostic?
73-
void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, BridgedSourceLoc loc,
69+
void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, swift::SourceLoc loc,
7470
BridgedDiagID diagID, BridgedArrayRef arguments,
7571
BridgedCharSourceRange highlight,
7672
BridgedArrayRef fixIts);
@@ -79,8 +75,4 @@ bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine);
7975

8076
SWIFT_END_NULLABILITY_ANNOTATIONS
8177

82-
#ifdef __cplusplus
83-
} // extern "C"
84-
#endif
85-
8678
#endif // SWIFT_AST_ASTBRIDGING_H

include/swift/Basic/BasicBridging.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@
1414
#define SWIFT_BASIC_BASICBRIDGING_H
1515

1616
#include "swift/Basic/BridgedSwiftObject.h"
17+
#include "swift/Basic/SourceLoc.h"
1718
#include <stddef.h>
1819

19-
#ifdef __cplusplus
20-
extern "C" {
21-
#endif
22-
2320
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2421

2522
typedef intptr_t SwiftInt;
@@ -48,18 +45,10 @@ void freeBridgedStringRef(BridgedStringRef str);
4845
//===----------------------------------------------------------------------===//
4946

5047
typedef struct {
51-
const unsigned char * _Nullable pointer;
52-
} BridgedSourceLoc;
53-
54-
typedef struct {
55-
BridgedSourceLoc start;
48+
swift::SourceLoc start;
5649
SwiftInt byteLength;
5750
} BridgedCharSourceRange;
5851

5952
SWIFT_END_NULLABILITY_ANNOTATIONS
6053

61-
#ifdef __cplusplus
62-
} // extern "C"
63-
#endif
64-
6554
#endif

include/swift/Basic/BridgingUtils.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,14 @@ inline llvm::ArrayRef<T> getArrayRef(BridgedArrayRef bridged) {
3636
return {static_cast<const T *>(bridged.data), bridged.numElements};
3737
}
3838

39-
inline SourceLoc getSourceLoc(const BridgedSourceLoc &bridged) {
40-
return SourceLoc(
41-
llvm::SMLoc::getFromPointer((const char *)bridged.pointer));
42-
}
43-
44-
inline BridgedSourceLoc getBridgedSourceLoc(const SourceLoc &loc) {
45-
return {static_cast<const unsigned char *>(loc.getOpaquePointerValue())};
46-
}
47-
4839
inline CharSourceRange
4940
getCharSourceRange(const BridgedCharSourceRange &bridged) {
50-
auto start = getSourceLoc(bridged.start);
51-
return CharSourceRange(start, bridged.byteLength);
41+
return CharSourceRange(bridged.start, bridged.byteLength);
5242
}
5343

5444
inline BridgedCharSourceRange
5545
getBridgedCharSourceRange(const CharSourceRange &range) {
56-
auto start = getBridgedSourceLoc(range.getStart());
57-
return {start, range.getByteLength()};
46+
return {range.getStart(), range.getByteLength()};
5847
}
5948

6049
/// Copies the string in an malloc'ed memory and the caller is responsible for

include/swift/Parse/RegexParserBridging.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
#include "swift/AST/ASTBridging.h"
1818
#include <stdbool.h>
1919

20-
#ifdef __cplusplus
21-
extern "C" {
22-
#endif
23-
2420
/// Attempt to lex a regex literal string. Takes the following arguments:
2521
///
2622
/// - CurPtrPtr: A pointer to the current pointer of lexer, which should be the
@@ -60,12 +56,8 @@ typedef bool (*RegexLiteralParsingFn)(/*InputPtr*/ const char *_Nonnull,
6056
/*VersionOut*/ unsigned *_Nonnull,
6157
/*CaptureStructureOut*/ void *_Nonnull,
6258
/*CaptureStructureSize*/ unsigned,
63-
/*DiagnosticBaseLoc*/ BridgedSourceLoc,
59+
/*DiagnosticBaseLoc*/ swift::SourceLoc,
6460
BridgedDiagnosticEngine);
6561
void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn _Nullable fn);
6662

67-
#ifdef __cplusplus
68-
} // extern "C"
69-
#endif
70-
7163
#endif // REGEX_PARSER_BRIDGING

lib/AST/ASTBridging.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ getDiagnosticArgument(const BridgedDiagnosticArgument &bridged) {
3838
} // namespace
3939

4040
void DiagnosticEngine_diagnose(
41-
BridgedDiagnosticEngine bridgedEngine, BridgedSourceLoc bridgedLoc,
41+
BridgedDiagnosticEngine bridgedEngine, SourceLoc loc,
4242
BridgedDiagID bridgedDiagID,
4343
BridgedArrayRef /*BridgedDiagnosticArgument*/ bridgedArguments,
4444
BridgedCharSourceRange bridgedHighlight,
4545
BridgedArrayRef /*BridgedDiagnosticFixIt*/ bridgedFixIts) {
4646
auto *D = getDiagnosticEngine(bridgedEngine);
4747

48-
auto loc = getSourceLoc(bridgedLoc);
4948
auto diagID = static_cast<DiagID>(bridgedDiagID);
5049
SmallVector<DiagnosticArgument, 2> arguments;
5150
for (auto bridgedArg :
@@ -62,7 +61,7 @@ void DiagnosticEngine_diagnose(
6261

6362
// Add fix-its.
6463
for (auto bridgedFixIt : getArrayRef<BridgedDiagnosticFixIt>(bridgedFixIts)) {
65-
auto range = CharSourceRange(getSourceLoc(bridgedFixIt.start),
64+
auto range = CharSourceRange(bridgedFixIt.start,
6665
bridgedFixIt.byteLength);
6766
auto text = getStringRef(bridgedFixIt.text);
6867
inflight.fixItReplaceChars(range.getStart(), range.getEnd(), text);

lib/Parse/ParseRegex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ParserResult<Expr> Parser::parseExprRegexLiteral() {
5151
regexLiteralParsingFn(regexText.str().c_str(), &version,
5252
/*captureStructureOut*/ capturesBuf.data(),
5353
/*captureStructureSize*/ capturesBuf.size(),
54-
/*diagBaseLoc*/ getBridgedSourceLoc(Tok.getLoc()),
54+
/*diagBaseLoc*/ Tok.getLoc(),
5555
getBridgedDiagnosticEngine(&Diags));
5656
auto loc = consumeToken();
5757
SourceMgr.recordRegexLiteralStartLoc(loc);

0 commit comments

Comments
 (0)