Skip to content

Commit 356cf31

Browse files
committed
[ASTGen] Migrate onto some BasicBridging types
Migrate onto BridgedStringRef, and the BasicBridging equivalents of BridgedSourceLoc and BridgedArrayRef.
1 parent ce23252 commit 356cf31

15 files changed

+80
-83
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_C_AST_ASTBRIDGING_H
1414
#define SWIFT_C_AST_ASTBRIDGING_H
1515

16+
#include "swift/Basic/BasicBridging.h"
1617
#include "swift/Basic/CBasicBridging.h"
1718
#include "swift/Basic/Compiler.h"
1819
#include "swift/Basic/Nullability.h"
@@ -101,24 +102,10 @@ extern "C" {
101102

102103
SWIFT_BEGIN_ASSUME_NONNULL
103104

104-
typedef struct {
105-
const unsigned char *_Nullable data;
106-
size_t length;
107-
} BridgedString;
108-
109-
typedef struct {
110-
const void *_Nullable data;
111-
size_t numElements;
112-
} BridgedArrayRef;
113-
114105
typedef struct BridgedASTContext {
115106
void *raw;
116107
} BridgedASTContext;
117108

118-
typedef struct BridgedSourceLoc {
119-
const void *_Nullable raw;
120-
} BridgedSourceLoc;
121-
122109
typedef struct {
123110
BridgedSourceLoc startLoc;
124111
BridgedSourceLoc endLoc;
@@ -298,7 +285,8 @@ extern "C" {
298285
/// \returns a diagnostic instance that can be extended with additional
299286
/// information and then must be finished via \c SwiftDiagnostic_finish.
300287
SWIFT_NAME("BridgedDiagnostic.init(at:message:severity:engine:)")
301-
BridgedDiagnostic Diagnostic_create(BridgedSourceLoc cLoc, BridgedString cText,
288+
BridgedDiagnostic Diagnostic_create(BridgedSourceLoc cLoc,
289+
BridgedStringRef cText,
302290
BridgedDiagnosticSeverity severity,
303291
BridgedDiagnosticEngine cDiags);
304292

@@ -312,15 +300,15 @@ SWIFT_NAME("BridgedDiagnostic.fixItReplace(self:start:end:replacement:)")
312300
void Diagnostic_fixItReplace(BridgedDiagnostic cDiag,
313301
BridgedSourceLoc cStartLoc,
314302
BridgedSourceLoc cEndLoc,
315-
BridgedString cReplaceText);
303+
BridgedStringRef cReplaceText);
316304

317305
/// Finish the given diagnostic and emit it.
318306
SWIFT_NAME("BridgedDiagnostic.finish(self:)")
319307
void Diagnostic_finish(BridgedDiagnostic cDiag);
320308

321309
SWIFT_NAME("BridgedASTContext.getIdentifier(self:_:)")
322310
BridgedIdentifier ASTContext_getIdentifier(BridgedASTContext cContext,
323-
BridgedString cStr);
311+
BridgedStringRef cStr);
324312

325313
SWIFT_NAME("BridgedASTContext.langOptsHasFeature(self:_:)")
326314
_Bool ASTContext_langOptsHasFeature(BridgedASTContext cContext,
@@ -371,12 +359,12 @@ BridgedUnresolvedDeclRefExpr UnresolvedDeclRefExpr_createParsed(
371359

372360
SWIFT_NAME("BridgedStringLiteralExpr.createParsed(_:value:loc:)")
373361
BridgedStringLiteralExpr
374-
StringLiteralExpr_createParsed(BridgedASTContext cContext, BridgedString cStr,
362+
StringLiteralExpr_createParsed(BridgedASTContext cContext, BridgedStringRef cStr,
375363
BridgedSourceLoc cTokenLoc);
376364

377365
SWIFT_NAME("BridgedIntegerLiteralExpr.createParsed(_:value:loc:)")
378366
BridgedIntegerLiteralExpr
379-
IntegerLiteralExpr_createParsed(BridgedASTContext cContext, BridgedString cStr,
367+
IntegerLiteralExpr_createParsed(BridgedASTContext cContext, BridgedStringRef cStr,
380368
BridgedSourceLoc cTokenLoc);
381369

382370
SWIFT_NAME("BridgedBooleanLiteralExpr.createParsed(_:value:loc:)")
@@ -680,7 +668,7 @@ BridgedParameterList ParameterList_createParsed(
680668
BridgedASTContext cContext, BridgedSourceLoc cLeftParenLoc,
681669
BridgedArrayRef cParameters, BridgedSourceLoc cRightParenLoc);
682670

683-
BridgedTypeAttrKind TypeAttrKind_fromString(BridgedString cStr);
671+
BridgedTypeAttrKind TypeAttrKind_fromString(BridgedStringRef cStr);
684672
BridgedTypeAttributes TypeAttributes_create(void);
685673
void TypeAttributes_addSimpleAttr(BridgedTypeAttributes cAttributes,
686674
BridgedTypeAttrKind kind,

include/swift/Bridging/ASTGen.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void swift_ASTGen_addQueuedDiagnostic(
2929
ptrdiff_t numHighlightRanges);
3030
void swift_ASTGen_renderQueuedDiagnostics(
3131
void *_Nonnull queued, ssize_t contextSize, ssize_t colorize,
32-
BridgedString *_Nonnull renderedString);
32+
BridgedStringRef *_Nonnull renderedString);
3333

3434
// FIXME: Hack because we cannot easily get to the already-parsed source
3535
// file from here. Fix this egregious oversight!
@@ -43,7 +43,7 @@ void swift_ASTGen_destroySourceFile(void *_Nonnull sourceFile);
4343
void *_Nullable swift_ASTGen_resolveMacroType(const void *_Nonnull macroType);
4444
void swift_ASTGen_destroyMacro(void *_Nonnull macro);
4545

46-
void swift_ASTGen_freeBridgedString(BridgedString);
46+
void swift_ASTGen_freeBridgedString(BridgedStringRef);
4747

4848
void *_Nonnull swift_ASTGen_resolveExecutableMacro(
4949
const char *_Nonnull moduleName, const char *_Nonnull typeName,
@@ -53,7 +53,7 @@ void swift_ASTGen_destroyExecutableMacro(void *_Nonnull macro);
5353
ptrdiff_t swift_ASTGen_checkMacroDefinition(
5454
void *_Nonnull diagEngine, void *_Nonnull sourceFile,
5555
const void *_Nonnull macroSourceLocation,
56-
BridgedString *_Nonnull expansionSourceOutPtr,
56+
BridgedStringRef *_Nonnull expansionSourceOutPtr,
5757
ptrdiff_t *_Nullable *_Nonnull replacementsPtr,
5858
ptrdiff_t *_Nonnull numReplacements);
5959
void swift_ASTGen_freeExpansionReplacements(
@@ -63,7 +63,7 @@ ptrdiff_t swift_ASTGen_expandFreestandingMacro(
6363
void *_Nonnull diagEngine, const void *_Nonnull macro, uint8_t externalKind,
6464
const char *_Nonnull discriminator, uint8_t rawMacroRole,
6565
void *_Nonnull sourceFile, const void *_Nullable sourceLocation,
66-
BridgedString *_Nonnull evaluatedSourceOut);
66+
BridgedStringRef *_Nonnull evaluatedSourceOut);
6767

6868
ptrdiff_t swift_ASTGen_expandAttachedMacro(
6969
void *_Nonnull diagEngine, const void *_Nonnull macro, uint8_t externalKind,
@@ -75,14 +75,14 @@ ptrdiff_t swift_ASTGen_expandAttachedMacro(
7575
const void *_Nullable declarationSourceLocation,
7676
void *_Nullable parentDeclSourceFile,
7777
const void *_Nullable parentDeclSourceLocation,
78-
BridgedString *_Nonnull evaluatedSourceOut);
78+
BridgedStringRef *_Nonnull evaluatedSourceOut);
7979

8080
bool swift_ASTGen_initializePlugin(void *_Nonnull handle,
8181
void *_Nullable diagEngine);
8282
void swift_ASTGen_deinitializePlugin(void *_Nonnull handle);
8383
bool swift_ASTGen_pluginServerLoadLibraryPlugin(
8484
void *_Nonnull handle, const char *_Nonnull libraryPath,
85-
const char *_Nonnull moduleName, BridgedString *_Nullable errorOut);
85+
const char *_Nonnull moduleName, BridgedStringRef *_Nullable errorOut);
8686

8787
#ifdef __cplusplus
8888
}

lib/AST/ASTBridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void DiagnosticEngine_diagnose(
6060
auto inflight = D->diagnose(loc.get(), diagID, arguments);
6161

6262
// Add highlight.
63-
if (highlightStart.isValid()) {
63+
if (highlightStart.get().isValid()) {
6464
CharSourceRange highlight(highlightStart.get(), (unsigned)hightlightLength);
6565
inflight.highlightChars(highlight.getStart(), highlight.getEnd());
6666
}

lib/AST/CASTBridging.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "swift/AST/CASTBridging.h"
22

3+
#include "swift/Basic/BasicBridging.h"
34
#include "swift/AST/ASTContext.h"
45
#include "swift/AST/ASTNode.h"
56
#include "swift/AST/Decl.h"
@@ -117,18 +118,16 @@ unbridgedArrayRef(const BridgedArrayRef bridged) {
117118
return {static_cast<const T *>(bridged.data), size_t(bridged.numElements)};
118119
}
119120

120-
static inline StringRef unbridged(BridgedString cStr) {
121-
return StringRef{reinterpret_cast<const char *>(cStr.data),
122-
size_t(cStr.length)};
121+
static inline StringRef unbridged(BridgedStringRef cStr) {
122+
return cStr.get();
123123
}
124124

125125
static inline ASTContext &unbridged(BridgedASTContext cContext) {
126126
return *static_cast<ASTContext *>(cContext.raw);
127127
}
128128

129129
static inline SourceLoc unbridged(BridgedSourceLoc cLoc) {
130-
auto smLoc = llvm::SMLoc::getFromPointer(static_cast<const char *>(cLoc.raw));
131-
return SourceLoc(smLoc);
130+
return cLoc.get();
132131
}
133132

134133
static inline SourceRange unbridged(BridgedSourceRange cRange) {
@@ -162,7 +161,7 @@ static TypeAttrKind unbridged(BridgedTypeAttrKind kind) {
162161
}
163162
}
164163

165-
BridgedDiagnostic Diagnostic_create(BridgedSourceLoc cLoc, BridgedString cText,
164+
BridgedDiagnostic Diagnostic_create(BridgedSourceLoc cLoc, BridgedStringRef cText,
166165
BridgedDiagnosticSeverity severity,
167166
BridgedDiagnosticEngine cDiags) {
168167
StringRef origText = unbridged(cText);
@@ -208,7 +207,7 @@ void Diagnostic_highlight(BridgedDiagnostic cDiag, BridgedSourceLoc cStartLoc,
208207
void Diagnostic_fixItReplace(BridgedDiagnostic cDiag,
209208
BridgedSourceLoc cStartLoc,
210209
BridgedSourceLoc cEndLoc,
211-
BridgedString cReplaceText) {
210+
BridgedStringRef cReplaceText) {
212211

213212
SourceLoc startLoc = unbridged(cStartLoc);
214213
SourceLoc endLoc = unbridged(cEndLoc);
@@ -229,7 +228,7 @@ void Diagnostic_finish(BridgedDiagnostic cDiag) {
229228
}
230229

231230
BridgedIdentifier ASTContext_getIdentifier(BridgedASTContext cContext,
232-
BridgedString cStr) {
231+
BridgedStringRef cStr) {
233232
StringRef str = unbridged(cStr);
234233
if (str.size() == 1 && str.front() == '_')
235234
return BridgedIdentifier();
@@ -329,15 +328,15 @@ BridgedUnresolvedDeclRefExpr UnresolvedDeclRefExpr_createParsed(
329328
}
330329

331330
BridgedStringLiteralExpr
332-
StringLiteralExpr_createParsed(BridgedASTContext cContext, BridgedString cStr,
331+
StringLiteralExpr_createParsed(BridgedASTContext cContext, BridgedStringRef cStr,
333332
BridgedSourceLoc cTokenLoc) {
334333
ASTContext &context = unbridged(cContext);
335334
auto str = context.AllocateCopy(unbridged(cStr));
336335
return bridged(new (context) StringLiteralExpr(str, unbridged(cTokenLoc)));
337336
}
338337

339338
BridgedIntegerLiteralExpr
340-
IntegerLiteralExpr_createParsed(BridgedASTContext cContext, BridgedString cStr,
339+
IntegerLiteralExpr_createParsed(BridgedASTContext cContext, BridgedStringRef cStr,
341340
BridgedSourceLoc cTokenLoc) {
342341
ASTContext &context = unbridged(cContext);
343342
auto str = context.AllocateCopy(unbridged(cStr));
@@ -462,7 +461,7 @@ BridgedParamDecl ParamDecl_createParsed(
462461
BridgedSourceLoc cFirstNameLoc, BridgedIdentifier cSecondName,
463462
BridgedSourceLoc cSecondNameLoc, BridgedNullableTypeRepr opaqueType,
464463
BridgedNullableExpr opaqueDefaultValue) {
465-
assert((bool)cSecondNameLoc.raw == (bool)cSecondName.raw);
464+
assert(cSecondNameLoc.get().isValid() == (bool)cSecondName.raw);
466465
if (!cSecondName.raw) {
467466
cSecondName = cFirstName;
468467
cSecondNameLoc = cFirstNameLoc;
@@ -540,7 +539,7 @@ BridgedConstructorDecl ConstructorDecl_createParsed(
540539
BridgedParameterList bridgedParameterList, BridgedSourceLoc cAsyncLoc,
541540
BridgedSourceLoc cThrowsLoc, BridgedNullableTypeRepr thrownType,
542541
BridgedNullableTrailingWhereClause genericWhereClause) {
543-
assert((bool)cFailabilityMarkLoc.raw || !isIUO);
542+
assert(cFailabilityMarkLoc.get().isValid() || !isIUO);
544543

545544
ASTContext &context = unbridged(cContext);
546545

@@ -850,8 +849,9 @@ BridgedOperatorDecl OperatorDecl_createParsed(
850849
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
851850
BridgedSourceLoc cColonLoc, BridgedIdentifier cPrecedenceGroupName,
852851
BridgedSourceLoc cPrecedenceGroupLoc) {
853-
assert(bool(cColonLoc.raw) == (bool)cPrecedenceGroupName.raw);
854-
assert(bool(cColonLoc.raw) == (bool)cPrecedenceGroupLoc.raw);
852+
auto colonLoc = cColonLoc.get();
853+
assert(colonLoc.isValid() == (bool)cPrecedenceGroupName.raw);
854+
assert(colonLoc.isValid() == cPrecedenceGroupLoc.get().isValid());
855855

856856
ASTContext &context = unbridged(cContext);
857857
auto operatorKeywordLoc = unbridged(cOperatorKeywordLoc);
@@ -867,12 +867,12 @@ BridgedOperatorDecl OperatorDecl_createParsed(
867867
unbridged(cPrecedenceGroupName), unbridged(cPrecedenceGroupLoc));
868868
break;
869869
case BridgedOperatorFixityPrefix:
870-
assert(!cColonLoc.raw);
870+
assert(colonLoc.isInvalid());
871871
decl = new (context)
872872
PrefixOperatorDecl(declContext, operatorKeywordLoc, name, nameLoc);
873873
break;
874874
case BridgedOperatorFixityPostfix:
875-
assert(!cColonLoc.raw);
875+
assert(colonLoc.isInvalid());
876876
decl = new (context)
877877
PostfixOperatorDecl(declContext, operatorKeywordLoc, name, nameLoc);
878878
break;
@@ -1012,7 +1012,7 @@ PackExpansionTypeRepr_createParsed(BridgedASTContext cContext,
10121012
return bridged(PE);
10131013
}
10141014

1015-
BridgedTypeAttrKind TypeAttrKind_fromString(BridgedString cStr) {
1015+
BridgedTypeAttrKind TypeAttrKind_fromString(BridgedStringRef cStr) {
10161016
TypeAttrKind kind = TypeAttributes::getAttrKindFromString(unbridged(cStr));
10171017
switch (kind) {
10181018
#define TYPE_ATTR(X) case TAK_##X: return BridgedTypeAttrKind_##X;

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import BasicBridging
12
import CASTBridging
23
import CBasicBridging
34
// Needed to use BumpPtrAllocator

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import BasicBridging
12
import CASTBridging
23
import CBasicBridging
34
import SwiftSyntax
@@ -69,17 +70,17 @@ extension BridgedSourceRange {
6970
}
7071

7172
extension String {
72-
mutating func withBridgedString<R>(_ body: (BridgedString) throws -> R) rethrows -> R {
73+
mutating func withBridgedString<R>(_ body: (BridgedStringRef) throws -> R) rethrows -> R {
7374
try withUTF8 { buffer in
74-
try body(BridgedString(data: buffer.baseAddress, length: buffer.count))
75+
try body(BridgedStringRef(data: buffer.baseAddress, count: buffer.count))
7576
}
7677
}
7778
}
7879

7980
/// Allocate a copy of the given string as a null-terminated UTF-8 string.
8081
func allocateBridgedString(
8182
_ string: String
82-
) -> BridgedString {
83+
) -> BridgedStringRef {
8384
var string = string
8485
return string.withUTF8 { utf8 in
8586
let ptr = UnsafeMutablePointer<UInt8>.allocate(
@@ -92,18 +93,18 @@ func allocateBridgedString(
9293
// null terminate, for client's convenience.
9394
ptr[utf8.count] = 0
9495

95-
return BridgedString(data: ptr, length: utf8.count)
96+
return BridgedStringRef(data: ptr, count: utf8.count)
9697
}
9798
}
9899

99100
@_cdecl("swift_ASTGen_freeBridgedString")
100-
public func freeBridgedString(bridged: BridgedString) {
101+
public func freeBridgedString(bridged: BridgedStringRef) {
101102
bridged.data?.deallocate()
102103
}
103104

104-
extension BridgedString {
105+
extension BridgedStringRef {
105106
var isEmptyInitialized: Bool {
106-
return self.data == nil && self.length == 0
107+
return self.data == nil && self.count == 0
107108
}
108109
}
109110

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import BasicBridging
12
import CASTBridging
23
import SwiftDiagnostics
34
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax

lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import BasicBridging
12
import CASTBridging
23
import SwiftDiagnostics
34
import SwiftSyntax
@@ -364,7 +365,7 @@ public func addQueuedDiagnostic(
364365
to: QueuedDiagnostics.self
365366
)
366367

367-
guard let rawPosition = position.raw else {
368+
guard let rawPosition = position.getOpaquePointerValue() else {
368369
return
369370
}
370371

@@ -397,8 +398,8 @@ public func addQueuedDiagnostic(
397398
)
398399
for index in 0..<numHighlightRanges {
399400
// Make sure both the start and the end land within this source file.
400-
guard let start = highlightRanges[index * 2].raw,
401-
let end = highlightRanges[index * 2 + 1].raw
401+
guard let start = highlightRanges[index * 2].getOpaquePointerValue(),
402+
let end = highlightRanges[index * 2 + 1].getOpaquePointerValue()
402403
else {
403404
continue
404405
}
@@ -457,7 +458,7 @@ public func renderQueuedDiagnostics(
457458
queuedDiagnosticsPtr: UnsafeMutableRawPointer,
458459
contextSize: Int,
459460
colorize: Int,
460-
renderedStringOutPtr: UnsafeMutablePointer<BridgedString>
461+
renderedStringOutPtr: UnsafeMutablePointer<BridgedStringRef>
461462
) {
462463
let queuedDiagnostics = queuedDiagnosticsPtr.assumingMemoryBound(to: QueuedDiagnostics.self)
463464
let formatter = DiagnosticsFormatter(contextSize: contextSize, colorize: colorize != 0)

0 commit comments

Comments
 (0)