Skip to content

Commit 5620abb

Browse files
committed
Bridging: Bridge swift::CharSourceRange directly
1 parent 9001ce4 commit 5620abb

File tree

15 files changed

+57
-97
lines changed

15 files changed

+57
-97
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ class BridgedDiagnosticArgument {
513513

514514
class BridgedFixIt {
515515
public:
516-
BridgedCharSourceRange replacementRange;
516+
swift::CharSourceRange replacementRange;
517517
BridgedStringRef replacementText;
518518
};
519519

@@ -1219,7 +1219,7 @@ BridgedProjectedValuePropertyAttr_createParsed(BridgedASTContext cContext,
12191219
SWIFT_NAME("BridgedRawDocCommentAttr.createParsed(_:range:)")
12201220
BridgedRawDocCommentAttr
12211221
BridgedRawDocCommentAttr_createParsed(BridgedASTContext cContext,
1222-
BridgedCharSourceRange cRange);
1222+
swift::CharSourceRange range);
12231223

12241224
SWIFT_NAME("BridgedRawLayoutAttr.createParsed(_:atLoc:range:size:alignment:)")
12251225
BridgedRawLayoutAttr BridgedStorageRestrictionsAttr_createParsed(
@@ -2113,13 +2113,13 @@ class BridgedRegexLiteralPatternFeatureKind final {
21132113
};
21142114

21152115
class BridgedRegexLiteralPatternFeature final {
2116-
BridgedCharSourceRange Range;
2116+
swift::CharSourceRange Range;
21172117
BridgedRegexLiteralPatternFeatureKind Kind;
21182118

21192119
public:
21202120
SWIFT_NAME("init(kind:at:)")
21212121
BridgedRegexLiteralPatternFeature(BridgedRegexLiteralPatternFeatureKind kind,
2122-
BridgedCharSourceRange range)
2122+
swift::CharSourceRange range)
21232123
: Range(range), Kind(kind) {}
21242124

21252125
using UnbridgedTy = swift::RegexLiteralPatternFeature;

include/swift/AST/ASTBridgingImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ BridgedRegexLiteralPatternFeature::BridgedRegexLiteralPatternFeature(
874874

875875
BridgedRegexLiteralPatternFeature::UnbridgedTy
876876
BridgedRegexLiteralPatternFeature::unbridged() const {
877-
return UnbridgedTy(Kind.unbridged(), Range.unbridged());
877+
return UnbridgedTy(Kind.unbridged(), Range);
878878
}
879879

880880
BridgedRegexLiteralPatternFeatures::UnbridgedTy

include/swift/Basic/BasicBridging.h

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -317,33 +317,6 @@ class BridgedOStream {
317317

318318
BridgedOStream Bridged_dbgs();
319319

320-
//===----------------------------------------------------------------------===//
321-
// MARK: BridgedCharSourceRange
322-
//===----------------------------------------------------------------------===//
323-
324-
class BridgedCharSourceRange {
325-
public:
326-
SWIFT_UNAVAILABLE("Use '.start' instead")
327-
swift::SourceLoc Start;
328-
329-
SWIFT_UNAVAILABLE("Use '.byteLength' instead")
330-
unsigned ByteLength;
331-
332-
SWIFT_NAME("init(start:byteLength:)")
333-
BridgedCharSourceRange(swift::SourceLoc start, unsigned byteLength)
334-
: Start(start), ByteLength(byteLength) {}
335-
336-
BRIDGED_INLINE BridgedCharSourceRange(swift::CharSourceRange range);
337-
338-
BRIDGED_INLINE swift::CharSourceRange unbridged() const;
339-
340-
SWIFT_COMPUTED_PROPERTY
341-
swift::SourceLoc getStart() const { return Start; }
342-
343-
SWIFT_COMPUTED_PROPERTY
344-
SwiftInt getByteLength() const { return static_cast<SwiftInt>(ByteLength); }
345-
};
346-
347320
//===----------------------------------------------------------------------===//
348321
// MARK: std::vector<BridgedCharSourceRange>
349322
//===----------------------------------------------------------------------===//
@@ -360,7 +333,7 @@ class BridgedCharSourceRangeVector {
360333
BridgedCharSourceRangeVector();
361334

362335
SWIFT_NAME("append(_:)")
363-
void push_back(BridgedCharSourceRange range);
336+
void push_back(swift::CharSourceRange range);
364337

365338
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
366339
/// Returns the `std::vector<swift::CharSourceRange>` that this

include/swift/Basic/BasicBridgingImpl.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#define SWIFT_BASIC_BASICBRIDGINGIMPL_H
1515

1616
#include "swift/Basic/Assertions.h"
17-
#include "swift/Basic/SourceLoc.h"
1817
#include "llvm/ADT/StringRef.h"
1918

2019
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
@@ -38,17 +37,6 @@ llvm::StringRef BridgedStringRef::unbridged() const {
3837

3938
llvm::StringRef BridgedOwnedString::unbridgedRef() const { return llvm::StringRef(Data, Length); }
4039

41-
//===----------------------------------------------------------------------===//
42-
// MARK: BridgedCharSourceRange
43-
//===----------------------------------------------------------------------===//
44-
45-
BridgedCharSourceRange::BridgedCharSourceRange(swift::CharSourceRange range)
46-
: Start(range.getStart()), ByteLength(range.getByteLength()) {}
47-
48-
swift::CharSourceRange BridgedCharSourceRange::unbridged() const {
49-
return swift::CharSourceRange(Start, ByteLength);
50-
}
51-
5240
//===----------------------------------------------------------------------===//
5341
// MARK: BridgedSwiftVersion
5442
//===----------------------------------------------------------------------===//

include/swift/Basic/SourceLoc.h

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,16 @@ class SourceLoc {
8989
return SourceLoc::getFromPointer(Pointer + ByteOffset);
9090
}
9191

92-
// Not imported into Swift in pure bridging mode.
93-
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
94-
92+
SWIFT_UNAVAILABLE("Unavailable in Swift")
9593
SourceLoc getAdvancedLocOrInvalid(int ByteOffset) const {
9694
if (isValid())
9795
return getAdvancedLoc(ByteOffset);
9896
return SourceLoc();
9997
}
10098

99+
// Not imported into Swift in pure bridging mode.
100+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
101+
101102
/// An explicit bool operator so one can check if a SourceLoc is valid in an
102103
/// if statement:
103104
///
@@ -226,9 +227,6 @@ class SourceRange {
226227
#endif // #ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
227228
};
228229

229-
// Not imported into Swift in pure bridging mode.
230-
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
231-
232230
/// A half-open character-based source range.
233231
class CharSourceRange {
234232
SourceLoc Start;
@@ -238,29 +236,40 @@ class CharSourceRange {
238236
/// Constructs an invalid range.
239237
CharSourceRange() = default;
240238

239+
SWIFT_NAME("init(start:byteLength:)")
241240
CharSourceRange(SourceLoc Start, unsigned ByteLength)
242241
: Start(Start), ByteLength(ByteLength) {}
243242

243+
// Not imported into Swift in pure bridging mode.
244+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
245+
244246
/// Constructs a character range which starts and ends at the
245247
/// specified character locations.
246248
CharSourceRange(const SourceManager &SM, SourceLoc Start, SourceLoc End);
247249

248250
/// Use Lexer::getCharSourceRangeFromSourceRange() instead.
249251
CharSourceRange(const SourceManager &SM, SourceRange Range) = delete;
250252

253+
#endif // #ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
254+
251255
bool isValid() const { return Start.isValid(); }
252256
bool isInvalid() const { return !isValid(); }
253257

254-
bool operator==(const CharSourceRange &other) const {
255-
return Start == other.Start && ByteLength == other.ByteLength;
256-
}
257-
bool operator!=(const CharSourceRange &other) const {
258-
return !operator==(other);
259-
}
260-
258+
SWIFT_COMPUTED_PROPERTY
261259
SourceLoc getStart() const { return Start; }
260+
SWIFT_COMPUTED_PROPERTY
262261
SourceLoc getEnd() const { return Start.getAdvancedLocOrInvalid(ByteLength); }
263262

263+
/// Return the length of this valid range in bytes. Can be zero.
264+
SWIFT_COMPUTED_PROPERTY
265+
unsigned getByteLength() const {
266+
assert(isValid() && "length does not make sense for an invalid range");
267+
return ByteLength;
268+
}
269+
270+
// Not imported into Swift in pure bridging mode.
271+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
272+
264273
/// Returns true if the given source location is contained in the range.
265274
bool contains(SourceLoc loc) const {
266275
auto less = std::less<const char *>();
@@ -296,12 +305,13 @@ class CharSourceRange {
296305

297306
StringRef str() const { return StringRef(Start.Pointer, ByteLength); }
298307

299-
/// Return the length of this valid range in bytes. Can be zero.
300-
unsigned getByteLength() const {
301-
assert(isValid() && "length does not make sense for an invalid range");
302-
return ByteLength;
308+
bool operator==(const CharSourceRange &other) const {
309+
return Start == other.Start && ByteLength == other.ByteLength;
303310
}
304-
311+
bool operator!=(const CharSourceRange &other) const {
312+
return !operator==(other);
313+
}
314+
305315
/// Print out the CharSourceRange. If the locations are in the same buffer
306316
/// as specified by LastBufferID, then we don't print the filename. If not,
307317
/// we do print the filename, and then update LastBufferID with the BufferID
@@ -314,11 +324,11 @@ class CharSourceRange {
314324
unsigned Tmp = ~0U;
315325
print(OS, SM, Tmp, PrintText);
316326
}
317-
327+
318328
SWIFT_DEBUG_DUMPER(dump(const SourceManager &SM));
319-
};
320329

321330
#endif // #ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
331+
};
322332

323333
} // end namespace swift
324334

include/swift/Bridging/ASTGen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void swift_ASTGen_addQueuedDiagnostic(
3232
swift::SourceLoc sourceLoc,
3333
BridgedStringRef categoryName,
3434
BridgedStringRef documentationPath,
35-
const BridgedCharSourceRange *_Nullable highlightRanges,
35+
const swift::CharSourceRange *_Nullable highlightRanges,
3636
ptrdiff_t numHighlightRanges,
3737
BridgedArrayRef /*BridgedFixIt*/ fixIts);
3838
void swift_ASTGen_renderSingleDiagnostic(

include/swift/IDE/IDEBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ struct BridgedResolvedLoc {
117117
/// This consumes `labelRanges` by calling `takeUnbridged` on it.
118118
SWIFT_NAME(
119119
"init(range:labelRanges:firstTrailingLabel:labelType:isActive:context:)")
120-
BridgedResolvedLoc(BridgedCharSourceRange range,
120+
BridgedResolvedLoc(swift::CharSourceRange range,
121121
BridgedCharSourceRangeVector labelRanges,
122122
unsigned firstTrailingLabel, LabelRangeType labelType,
123123
bool isActive, ResolvedLocContext context);

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,8 @@ BridgedProjectedValuePropertyAttr_createParsed(BridgedASTContext cContext,
596596

597597
BridgedRawDocCommentAttr
598598
BridgedRawDocCommentAttr_createParsed(BridgedASTContext cContext,
599-
BridgedCharSourceRange cRange) {
600-
return new (cContext.unbridged()) RawDocCommentAttr(cRange.unbridged());
599+
CharSourceRange range) {
600+
return new (cContext.unbridged()) RawDocCommentAttr(range);
601601
}
602602

603603
BridgedRawLayoutAttr

lib/AST/DiagnosticBridge.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
3838
info.FormatArgs);
3939
}
4040

41-
// Map the highlight ranges.
42-
SmallVector<BridgedCharSourceRange, 2> highlightRanges;
43-
for (const auto &range : info.Ranges) {
44-
if (range.isValid())
45-
highlightRanges.push_back(range);
46-
}
47-
4841
StringRef documentationPath = info.CategoryDocumentationURL;
4942

5043
SmallVector<BridgedFixIt, 2> fixIts;
@@ -58,7 +51,7 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
5851
info.Kind, info.Loc,
5952
info.Category,
6053
documentationPath,
61-
highlightRanges.data(), highlightRanges.size(),
54+
info.Ranges.data(), info.Ranges.size(),
6255
llvm::ArrayRef<BridgedFixIt>(fixIts));
6356

6457
// TODO: A better way to do this would be to pass the notes as an

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ extension ASTGenVisitor {
291291
}
292292
}
293293

294-
/// Obtains bridged character source range.
294+
/// Obtains a C++ character source range.
295295
@inline(__always)
296-
func generateCharSourceRange(start: AbsolutePosition, length: SourceLength) -> BridgedCharSourceRange {
297-
BridgedCharSourceRange(
296+
func generateCharSourceRange(start: AbsolutePosition, length: SourceLength) -> CharSourceRange {
297+
.init(
298298
start: SourceLoc(at: start, in: self.base),
299299
byteLength: UInt32(length.utf8Length)
300300
)

0 commit comments

Comments
 (0)