Bridging: Bridge swift::CharSourceRange directly

This commit is contained in:
Anthony Latsis
2025-06-30 08:27:13 +01:00
parent 9001ce4687
commit 5620abbad8
15 changed files with 57 additions and 97 deletions

View File

@@ -513,7 +513,7 @@ public:
class BridgedFixIt {
public:
BridgedCharSourceRange replacementRange;
swift::CharSourceRange replacementRange;
BridgedStringRef replacementText;
};
@@ -1219,7 +1219,7 @@ BridgedProjectedValuePropertyAttr_createParsed(BridgedASTContext cContext,
SWIFT_NAME("BridgedRawDocCommentAttr.createParsed(_:range:)")
BridgedRawDocCommentAttr
BridgedRawDocCommentAttr_createParsed(BridgedASTContext cContext,
BridgedCharSourceRange cRange);
swift::CharSourceRange range);
SWIFT_NAME("BridgedRawLayoutAttr.createParsed(_:atLoc:range:size:alignment:)")
BridgedRawLayoutAttr BridgedStorageRestrictionsAttr_createParsed(
@@ -2113,13 +2113,13 @@ public:
};
class BridgedRegexLiteralPatternFeature final {
BridgedCharSourceRange Range;
swift::CharSourceRange Range;
BridgedRegexLiteralPatternFeatureKind Kind;
public:
SWIFT_NAME("init(kind:at:)")
BridgedRegexLiteralPatternFeature(BridgedRegexLiteralPatternFeatureKind kind,
BridgedCharSourceRange range)
swift::CharSourceRange range)
: Range(range), Kind(kind) {}
using UnbridgedTy = swift::RegexLiteralPatternFeature;

View File

@@ -874,7 +874,7 @@ BridgedRegexLiteralPatternFeature::BridgedRegexLiteralPatternFeature(
BridgedRegexLiteralPatternFeature::UnbridgedTy
BridgedRegexLiteralPatternFeature::unbridged() const {
return UnbridgedTy(Kind.unbridged(), Range.unbridged());
return UnbridgedTy(Kind.unbridged(), Range);
}
BridgedRegexLiteralPatternFeatures::UnbridgedTy

View File

@@ -317,33 +317,6 @@ public:
BridgedOStream Bridged_dbgs();
//===----------------------------------------------------------------------===//
// MARK: BridgedCharSourceRange
//===----------------------------------------------------------------------===//
class BridgedCharSourceRange {
public:
SWIFT_UNAVAILABLE("Use '.start' instead")
swift::SourceLoc Start;
SWIFT_UNAVAILABLE("Use '.byteLength' instead")
unsigned ByteLength;
SWIFT_NAME("init(start:byteLength:)")
BridgedCharSourceRange(swift::SourceLoc start, unsigned byteLength)
: Start(start), ByteLength(byteLength) {}
BRIDGED_INLINE BridgedCharSourceRange(swift::CharSourceRange range);
BRIDGED_INLINE swift::CharSourceRange unbridged() const;
SWIFT_COMPUTED_PROPERTY
swift::SourceLoc getStart() const { return Start; }
SWIFT_COMPUTED_PROPERTY
SwiftInt getByteLength() const { return static_cast<SwiftInt>(ByteLength); }
};
//===----------------------------------------------------------------------===//
// MARK: std::vector<BridgedCharSourceRange>
//===----------------------------------------------------------------------===//
@@ -360,7 +333,7 @@ public:
BridgedCharSourceRangeVector();
SWIFT_NAME("append(_:)")
void push_back(BridgedCharSourceRange range);
void push_back(swift::CharSourceRange range);
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
/// Returns the `std::vector<swift::CharSourceRange>` that this

View File

@@ -14,7 +14,6 @@
#define SWIFT_BASIC_BASICBRIDGINGIMPL_H
#include "swift/Basic/Assertions.h"
#include "swift/Basic/SourceLoc.h"
#include "llvm/ADT/StringRef.h"
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
@@ -38,17 +37,6 @@ llvm::StringRef BridgedStringRef::unbridged() const {
llvm::StringRef BridgedOwnedString::unbridgedRef() const { return llvm::StringRef(Data, Length); }
//===----------------------------------------------------------------------===//
// MARK: BridgedCharSourceRange
//===----------------------------------------------------------------------===//
BridgedCharSourceRange::BridgedCharSourceRange(swift::CharSourceRange range)
: Start(range.getStart()), ByteLength(range.getByteLength()) {}
swift::CharSourceRange BridgedCharSourceRange::unbridged() const {
return swift::CharSourceRange(Start, ByteLength);
}
//===----------------------------------------------------------------------===//
// MARK: BridgedSwiftVersion
//===----------------------------------------------------------------------===//

View File

@@ -89,15 +89,16 @@ public:
return SourceLoc::getFromPointer(Pointer + ByteOffset);
}
// Not imported into Swift in pure bridging mode.
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
SWIFT_UNAVAILABLE("Unavailable in Swift")
SourceLoc getAdvancedLocOrInvalid(int ByteOffset) const {
if (isValid())
return getAdvancedLoc(ByteOffset);
return SourceLoc();
}
// Not imported into Swift in pure bridging mode.
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
/// An explicit bool operator so one can check if a SourceLoc is valid in an
/// if statement:
///
@@ -226,9 +227,6 @@ public:
#endif // #ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
};
// Not imported into Swift in pure bridging mode.
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
/// A half-open character-based source range.
class CharSourceRange {
SourceLoc Start;
@@ -238,9 +236,13 @@ public:
/// Constructs an invalid range.
CharSourceRange() = default;
SWIFT_NAME("init(start:byteLength:)")
CharSourceRange(SourceLoc Start, unsigned ByteLength)
: Start(Start), ByteLength(ByteLength) {}
// Not imported into Swift in pure bridging mode.
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
/// Constructs a character range which starts and ends at the
/// specified character locations.
CharSourceRange(const SourceManager &SM, SourceLoc Start, SourceLoc End);
@@ -248,18 +250,25 @@ public:
/// Use Lexer::getCharSourceRangeFromSourceRange() instead.
CharSourceRange(const SourceManager &SM, SourceRange Range) = delete;
#endif // #ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
bool isValid() const { return Start.isValid(); }
bool isInvalid() const { return !isValid(); }
bool operator==(const CharSourceRange &other) const {
return Start == other.Start && ByteLength == other.ByteLength;
}
bool operator!=(const CharSourceRange &other) const {
return !operator==(other);
SWIFT_COMPUTED_PROPERTY
SourceLoc getStart() const { return Start; }
SWIFT_COMPUTED_PROPERTY
SourceLoc getEnd() const { return Start.getAdvancedLocOrInvalid(ByteLength); }
/// Return the length of this valid range in bytes. Can be zero.
SWIFT_COMPUTED_PROPERTY
unsigned getByteLength() const {
assert(isValid() && "length does not make sense for an invalid range");
return ByteLength;
}
SourceLoc getStart() const { return Start; }
SourceLoc getEnd() const { return Start.getAdvancedLocOrInvalid(ByteLength); }
// Not imported into Swift in pure bridging mode.
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
/// Returns true if the given source location is contained in the range.
bool contains(SourceLoc loc) const {
@@ -296,12 +305,13 @@ public:
StringRef str() const { return StringRef(Start.Pointer, ByteLength); }
/// Return the length of this valid range in bytes. Can be zero.
unsigned getByteLength() const {
assert(isValid() && "length does not make sense for an invalid range");
return ByteLength;
bool operator==(const CharSourceRange &other) const {
return Start == other.Start && ByteLength == other.ByteLength;
}
bool operator!=(const CharSourceRange &other) const {
return !operator==(other);
}
/// Print out the CharSourceRange. If the locations are in the same buffer
/// as specified by LastBufferID, then we don't print the filename. If not,
/// we do print the filename, and then update LastBufferID with the BufferID
@@ -314,11 +324,11 @@ public:
unsigned Tmp = ~0U;
print(OS, SM, Tmp, PrintText);
}
SWIFT_DEBUG_DUMPER(dump(const SourceManager &SM));
};
#endif // #ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
};
} // end namespace swift

View File

@@ -32,7 +32,7 @@ void swift_ASTGen_addQueuedDiagnostic(
swift::SourceLoc sourceLoc,
BridgedStringRef categoryName,
BridgedStringRef documentationPath,
const BridgedCharSourceRange *_Nullable highlightRanges,
const swift::CharSourceRange *_Nullable highlightRanges,
ptrdiff_t numHighlightRanges,
BridgedArrayRef /*BridgedFixIt*/ fixIts);
void swift_ASTGen_renderSingleDiagnostic(

View File

@@ -117,7 +117,7 @@ struct BridgedResolvedLoc {
/// This consumes `labelRanges` by calling `takeUnbridged` on it.
SWIFT_NAME(
"init(range:labelRanges:firstTrailingLabel:labelType:isActive:context:)")
BridgedResolvedLoc(BridgedCharSourceRange range,
BridgedResolvedLoc(swift::CharSourceRange range,
BridgedCharSourceRangeVector labelRanges,
unsigned firstTrailingLabel, LabelRangeType labelType,
bool isActive, ResolvedLocContext context);

View File

@@ -596,8 +596,8 @@ BridgedProjectedValuePropertyAttr_createParsed(BridgedASTContext cContext,
BridgedRawDocCommentAttr
BridgedRawDocCommentAttr_createParsed(BridgedASTContext cContext,
BridgedCharSourceRange cRange) {
return new (cContext.unbridged()) RawDocCommentAttr(cRange.unbridged());
CharSourceRange range) {
return new (cContext.unbridged()) RawDocCommentAttr(range);
}
BridgedRawLayoutAttr

View File

@@ -38,13 +38,6 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
info.FormatArgs);
}
// Map the highlight ranges.
SmallVector<BridgedCharSourceRange, 2> highlightRanges;
for (const auto &range : info.Ranges) {
if (range.isValid())
highlightRanges.push_back(range);
}
StringRef documentationPath = info.CategoryDocumentationURL;
SmallVector<BridgedFixIt, 2> fixIts;
@@ -58,7 +51,7 @@ static void addQueueDiagnostic(void *queuedDiagnostics,
info.Kind, info.Loc,
info.Category,
documentationPath,
highlightRanges.data(), highlightRanges.size(),
info.Ranges.data(), info.Ranges.size(),
llvm::ArrayRef<BridgedFixIt>(fixIts));
// TODO: A better way to do this would be to pass the notes as an

View File

@@ -291,10 +291,10 @@ extension ASTGenVisitor {
}
}
/// Obtains bridged character source range.
/// Obtains a C++ character source range.
@inline(__always)
func generateCharSourceRange(start: AbsolutePosition, length: SourceLength) -> BridgedCharSourceRange {
BridgedCharSourceRange(
func generateCharSourceRange(start: AbsolutePosition, length: SourceLength) -> CharSourceRange {
.init(
start: SourceLoc(at: start, in: self.base),
byteLength: UInt32(length.utf8Length)
)

View File

@@ -19,6 +19,7 @@ public typealias Identifier = swift.Identifier
public typealias DeclBaseName = swift.DeclBaseName
public typealias SourceLoc = swift.SourceLoc
public typealias SourceRange = swift.SourceRange
public typealias CharSourceRange = swift.CharSourceRange
public protocol BridgedNullable: ExpressibleByNilLiteral {
associatedtype RawPtr

View File

@@ -253,7 +253,7 @@ public func addQueuedDiagnostic(
loc: SourceLoc,
categoryName: BridgedStringRef,
documentationPath: BridgedStringRef,
highlightRangesPtr: UnsafePointer<BridgedCharSourceRange>?,
highlightRangesPtr: UnsafePointer<CharSourceRange>?,
numHighlightRanges: Int,
fixItsUntyped: BridgedArrayRef
) {
@@ -307,7 +307,7 @@ public func addQueuedDiagnostic(
// Map the highlights.
var highlights: [Syntax] = []
let highlightRanges = UnsafeBufferPointer<BridgedCharSourceRange>(
let highlightRanges = UnsafeBufferPointer<CharSourceRange>(
start: highlightRangesPtr,
count: numHighlightRanges
)
@@ -315,9 +315,7 @@ public func addQueuedDiagnostic(
let range = highlightRanges[index]
// Make sure both the start and the end land within this source file.
guard let start = range.start.raw,
let end = range.start.advanced(by: CInt(range.byteLength)).raw
else {
guard let start = range.start.raw, let end = range.end.raw else {
continue
}
@@ -386,9 +384,7 @@ public func addQueuedDiagnostic(
let fixItChanges: [FixIt.Change] = fixItsUntyped.withElements(ofType: BridgedFixIt.self) { fixIts in
fixIts.compactMap { fixIt in
guard let startPos = sourceFile.position(of: fixIt.replacementRange.start),
let endPos = sourceFile.position(
of: fixIt.replacementRange.start.advanced(
by: CInt(fixIt.replacementRange.byteLength))) else {
let endPos = sourceFile.position(of: fixIt.replacementRange.end) else {
return nil
}

View File

@@ -17,7 +17,7 @@ import swiftASTGen
// MARK: - Bridged type initializers
fileprivate extension BridgedCharSourceRange {
fileprivate extension CharSourceRange {
init(from range: Range<AbsolutePosition>, in sourceFile: ExportedSourceFile) {
self.init(
start: SourceLoc(at: range.lowerBound, in: sourceFile.buffer),
@@ -30,7 +30,7 @@ fileprivate extension BridgedCharSourceRangeVector {
init(from ranges: some Sequence<Range<AbsolutePosition>>, in sourceFile: ExportedSourceFile) {
self = .init()
for range in ranges {
self.append(BridgedCharSourceRange(from: range, in: sourceFile))
self.append(.init(from: range, in: sourceFile))
}
}
}
@@ -75,7 +75,7 @@ extension BridgedResolvedLoc {
case .selector(let arguments2): arguments = arguments2
}
self.init(
range: BridgedCharSourceRange(from: resolvedLoc.baseNameRange, in: sourceFile),
range: .init(from: resolvedLoc.baseNameRange, in: sourceFile),
labelRanges: BridgedCharSourceRangeVector(from: arguments.map { $0.range }, in: sourceFile),
firstTrailingLabel: firstTrailingClosureIndex,
labelType: LabelRangeType(resolvedLoc.arguments),

View File

@@ -91,9 +91,8 @@ void BridgedData::free() const {
BridgedCharSourceRangeVector::BridgedCharSourceRangeVector()
: vector(new std::vector<CharSourceRange>()) {}
void BridgedCharSourceRangeVector::push_back(BridgedCharSourceRange range) {
static_cast<std::vector<CharSourceRange> *>(vector)->push_back(
range.unbridged());
void BridgedCharSourceRangeVector::push_back(swift::CharSourceRange range) {
static_cast<std::vector<CharSourceRange> *>(vector)->push_back(range);
}
//===----------------------------------------------------------------------===//

View File

@@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 - 2023 Apple Inc. and the Swift project authors
// Copyright (c) 2022 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
@@ -25,13 +25,13 @@ ResolvedLoc::ResolvedLoc(swift::CharSourceRange range,
ResolvedLoc::ResolvedLoc() {}
BridgedResolvedLoc::BridgedResolvedLoc(BridgedCharSourceRange range,
BridgedResolvedLoc::BridgedResolvedLoc(swift::CharSourceRange range,
BridgedCharSourceRangeVector labelRanges,
unsigned firstTrailingLabel,
LabelRangeType labelType, bool isActive,
ResolvedLocContext context)
: resolvedLoc(
new ResolvedLoc(range.unbridged(), labelRanges.takeUnbridged(),
new ResolvedLoc(range, labelRanges.takeUnbridged(),
firstTrailingLabel == UINT_MAX
? std::nullopt
: std::optional<unsigned>(firstTrailingLabel),