Skip to content

Commit 4b3c51f

Browse files
authored
Merge pull request #65081 from hyp/eng/5.8-frt
[interop] add availability for foreign reference types
2 parents 5fa6d08 + 380f9a7 commit 4b3c51f

26 files changed

+132
-50
lines changed

SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ public struct DiagnosticFixIt {
5353
}
5454

5555
public struct DiagnosticEngine {
56-
private let bridged: swift.DiagnosticEngine
56+
private let bridged: BridgedDiagnosticEngine
5757

58-
public init(bridged: swift.DiagnosticEngine) {
58+
public init(bridged: BridgedDiagnosticEngine) {
5959
self.bridged = bridged
6060
}
61+
public init?(bridged: BridgedOptionalDiagnosticEngine) {
62+
guard let object = bridged.object else {
63+
return nil
64+
}
65+
self.bridged = BridgedDiagnosticEngine(object: object)
66+
}
6167

6268
public func diagnose(_ position: SourceLoc?,
6369
_ id: DiagID,

SwiftCompilerSources/Sources/Parse/Regex.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private func _RegexLiteralLexingFn(
4646
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>>,
4747
_ bufferEndPtr: UnsafePointer<CChar>,
4848
_ mustBeRegex: CBool,
49-
_ bridgedDiagnosticEngine: swift.DiagnosticEngine?
49+
_ bridgedDiagnosticEngine: BridgedOptionalDiagnosticEngine
5050
) -> /*CompletelyErroneous*/ CBool {
5151
let inputPtr = curPtrPtr.pointee
5252

@@ -62,8 +62,7 @@ private func _RegexLiteralLexingFn(
6262

6363
if let error = error {
6464
// Emit diagnostic if diagnostics are enabled.
65-
if let bridged = bridgedDiagnosticEngine {
66-
let diagEngine = DiagnosticEngine(bridged: bridged)
65+
if let diagEngine = DiagnosticEngine(bridged: bridgedDiagnosticEngine) {
6766
let startLoc = SourceLoc(
6867
locationInFile: error.location.assumingMemoryBound(to: UInt8.self))!
6968
diagEngine.diagnose(startLoc, .foreign_diagnostic, error.message)
@@ -94,7 +93,7 @@ public func _RegexLiteralParsingFn(
9493
_ captureStructureOut: UnsafeMutableRawPointer,
9594
_ captureStructureSize: CUnsignedInt,
9695
_ bridgedDiagnosticBaseLoc: swift.SourceLoc,
97-
_ bridgedDiagnosticEngine: swift.DiagnosticEngine
96+
_ bridgedDiagnosticEngine: BridgedDiagnosticEngine
9897
) -> Bool {
9998
let str = String(cString: inputPtr)
10099
let captureBuffer = UnsafeMutableRawBufferPointer(

include/swift/AST/ASTBridging.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,22 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagID : uint32_t {
3232
#include "swift/AST/DiagnosticsAll.def"
3333
} BridgedDiagID;
3434

35+
typedef struct {
36+
void * _Nonnull object;
37+
} BridgedDiagnosticEngine;
38+
39+
typedef struct {
40+
void *_Nullable object;
41+
} BridgedOptionalDiagnosticEngine;
42+
3543
// FIXME: Can we bridge InFlightDiagnostic?
36-
void DiagnosticEngine_diagnose(swift::DiagnosticEngine &, swift::SourceLoc loc,
44+
void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, swift::SourceLoc loc,
3745
BridgedDiagID diagID, BridgedArrayRef arguments,
3846
swift::CharSourceRange highlight,
3947
BridgedArrayRef fixIts);
4048

49+
bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine);
50+
4151
using ArrayRefOfDiagnosticArgument = llvm::ArrayRef<swift::DiagnosticArgument>;
4252

4353
SWIFT_END_NULLABILITY_ANNOTATIONS

include/swift/AST/BridgingUtils.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===--- BridgingUtils.h - utilities for swift bridging -------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_AST_BRIDGINGUTILS_H
14+
#define SWIFT_AST_BRIDGINGUTILS_H
15+
16+
#include "swift/AST/ASTBridging.h"
17+
#include "swift/AST/DiagnosticEngine.h"
18+
19+
namespace swift {
20+
21+
inline BridgedDiagnosticEngine getBridgedDiagnosticEngine(DiagnosticEngine *D) {
22+
return {(void *)D};
23+
}
24+
inline BridgedOptionalDiagnosticEngine
25+
getBridgedOptionalDiagnosticEngine(DiagnosticEngine *D) {
26+
return {(void *)D};
27+
}
28+
29+
} // namespace swift
30+
31+
#endif
32+

include/swift/AST/DiagnosticEngine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ namespace swift {
804804

805805
/// Class responsible for formatting diagnostics and presenting them
806806
/// to the user.
807-
class SWIFT_IMPORT_REFERENCE DiagnosticEngine {
807+
class DiagnosticEngine {
808808
public:
809809
/// The source manager used to interpret source locations and
810810
/// display diagnostics.

include/swift/Basic/Compiler.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,6 @@
184184
#define SWIFT_VFORMAT(fmt)
185185
#endif
186186

187-
// Tells Swift's ClangImporter to import a C++ type as a foreign reference type.
188-
#if __has_attribute(swift_attr)
189-
#define SWIFT_IMPORT_REFERENCE __attribute__((swift_attr("import_reference"))) \
190-
__attribute__((swift_attr("retain:immortal"))) \
191-
__attribute__((swift_attr("release:immortal")))
192-
#else
193-
#define SWIFT_IMPORT_REFERENCE
194-
#endif
195-
196187
#if __has_attribute(enum_extensibility)
197188
#define ENUM_EXTENSIBILITY_ATTR(arg) __attribute__((enum_extensibility(arg)))
198189
#else

include/swift/Parse/RegexParserBridging.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
/// past.
2828
/// - MustBeRegex: whether an error during lexing should be considered a regex
2929
/// literal, or some thing else.
30-
/// - OptionalDiagnosticEngine: RegexLiteralLexingFn should diagnose the
31-
/// token using this engine.
30+
/// - BridgedOptionalDiagnosticEngine: RegexLiteralLexingFn should diagnose the
31+
/// token using this engine.
3232
///
3333
/// Returns: A bool indicating whether lexing was completely erroneous, and
3434
/// cannot be recovered from, or false if there either was no error,
3535
/// or there was a recoverable error.
3636
typedef bool (*RegexLiteralLexingFn)(
3737
/*CurPtrPtr*/ const char *_Nonnull *_Nonnull,
3838
/*BufferEnd*/ const char *_Nonnull,
39-
/*MustBeRegex*/ bool, swift::DiagnosticEngine *_Nullable);
39+
/*MustBeRegex*/ bool, BridgedOptionalDiagnosticEngine);
4040
void Parser_registerRegexLiteralLexingFn(RegexLiteralLexingFn _Nullable fn);
4141

4242
/// Parse a regex literal string. Takes the following arguments:
@@ -48,16 +48,16 @@ void Parser_registerRegexLiteralLexingFn(RegexLiteralLexingFn _Nullable fn);
4848
/// - CaptureStructureSize: The size of the capture structure buffer. Must be
4949
/// greater than or equal to `strlen(InputPtr) + 3`.
5050
/// - DiagnosticBaseLoc: Start location of the regex literal.
51-
/// - DiagnosticEngine: RegexLiteralParsingFn should diagnose the
52-
/// parsing errors using this engine.
51+
/// - BridgedDiagnosticEngine: RegexLiteralParsingFn should diagnose the
52+
/// parsing errors using this engine.
5353
///
5454
/// Returns: A bool value indicating if there was an error while parsing.
5555
typedef bool (*RegexLiteralParsingFn)(/*InputPtr*/ const char *_Nonnull,
5656
/*VersionOut*/ unsigned *_Nonnull,
5757
/*CaptureStructureOut*/ void *_Nonnull,
5858
/*CaptureStructureSize*/ unsigned,
5959
/*DiagnosticBaseLoc*/ swift::SourceLoc,
60-
swift::DiagnosticEngine &);
60+
BridgedDiagnosticEngine);
6161
void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn _Nullable fn);
6262

6363
#endif // REGEX_PARSER_BRIDGING

lib/AST/ASTBridging.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,28 @@
1717

1818
using namespace swift;
1919

20+
namespace {
21+
/// BridgedDiagnosticEngine -> DiagnosticEngine *.
22+
DiagnosticEngine *getDiagnosticEngine(const BridgedDiagnosticEngine &bridged) {
23+
return static_cast<DiagnosticEngine *>(bridged.object);
24+
}
25+
26+
} // namespace
27+
2028
void DiagnosticEngine_diagnose(
21-
DiagnosticEngine &engine, SourceLoc loc, BridgedDiagID bridgedDiagID,
29+
BridgedDiagnosticEngine bridgedEngine, SourceLoc loc,
30+
BridgedDiagID bridgedDiagID,
2231
BridgedArrayRef /*DiagnosticArgument*/ bridgedArguments,
2332
CharSourceRange highlight,
2433
BridgedArrayRef /*DiagnosticInfo::FixIt*/ bridgedFixIts) {
34+
auto *D = getDiagnosticEngine(bridgedEngine);
2535

2636
auto diagID = static_cast<DiagID>(bridgedDiagID);
2737
SmallVector<DiagnosticArgument, 2> arguments;
2838
for (auto arg : getArrayRef<DiagnosticArgument>(bridgedArguments)) {
2939
arguments.push_back(arg);
3040
}
31-
auto inflight = engine.diagnose(loc, diagID, arguments);
41+
auto inflight = D->diagnose(loc, diagID, arguments);
3242

3343
// Add highlight.
3444
if (highlight.isValid()) {
@@ -42,3 +52,8 @@ void DiagnosticEngine_diagnose(
4252
inflight.fixItReplaceChars(range.getStart(), range.getEnd(), text);
4353
}
4454
}
55+
56+
bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine bridgedEngine) {
57+
auto *D = getDiagnosticEngine(bridgedEngine);
58+
return D->hadAnyError();
59+
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2681,9 +2681,25 @@ namespace {
26812681
if (!result)
26822682
return nullptr;
26832683

2684-
if (auto classDecl = dyn_cast<ClassDecl>(result))
2684+
if (auto classDecl = dyn_cast<ClassDecl>(result)) {
26852685
validateForeignReferenceType(decl, classDecl);
26862686

2687+
auto ctx = Impl.SwiftContext.getSwift58Availability();
2688+
if (!ctx.isAlwaysAvailable()) {
2689+
assert(ctx.getOSVersion().hasLowerEndpoint());
2690+
auto AvAttr = new (Impl.SwiftContext) AvailableAttr(
2691+
SourceLoc(), SourceRange(),
2692+
targetPlatform(Impl.SwiftContext.LangOpts), "", "",
2693+
/*RenameDecl=*/nullptr, ctx.getOSVersion().getLowerEndpoint(),
2694+
/*IntroducedRange=*/SourceRange(), {},
2695+
/*DeprecatedRange=*/SourceRange(), {},
2696+
/*ObsoletedRange=*/SourceRange(),
2697+
PlatformAgnosticAvailabilityKind::None, /*Implicit=*/false,
2698+
false);
2699+
classDecl->getAttrs().add(AvAttr);
2700+
}
2701+
}
2702+
26872703
// If this module is declared as a C++ module, try to synthesize
26882704
// conformances to Swift protocols from the Cxx module.
26892705
auto clangModule = decl->getOwningModule();

lib/Parse/Lexer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Parse/Lexer.h"
18+
#include "swift/AST/BridgingUtils.h"
1819
#include "swift/AST/DiagnosticsParse.h"
1920
#include "swift/AST/Identifier.h"
2021
#include "swift/Basic/LangOptions.h"
@@ -2084,7 +2085,7 @@ const char *Lexer::tryScanRegexLiteral(const char *TokStart, bool MustBeRegex,
20842085
// recovered from.
20852086
auto *Ptr = TokStart;
20862087
CompletelyErroneous = regexLiteralLexingFn(
2087-
&Ptr, BufferEnd, MustBeRegex, Diags);
2088+
&Ptr, BufferEnd, MustBeRegex, getBridgedOptionalDiagnosticEngine(Diags));
20882089

20892090
// If we didn't make any lexing progress, this isn't a regex literal and we
20902091
// should fallback to lexing as something else.

0 commit comments

Comments
 (0)