diff --git a/SwiftCompilerSources/CMakeLists.txt b/SwiftCompilerSources/CMakeLists.txt index 8504320ebda79..9b6fe3b36d88d 100644 --- a/SwiftCompilerSources/CMakeLists.txt +++ b/SwiftCompilerSources/CMakeLists.txt @@ -165,6 +165,10 @@ function(add_swift_compiler_modules_library name) "-emit-module-path" "${build_dir}/${module}.swiftmodule" "-parse-as-library" ${sources} "-wmo" ${swift_compile_options} + # LLVM modules and headers. + "-Xcc" "-I" "-Xcc" "${LLVM_MAIN_INCLUDE_DIR}" + # Generated LLVM headers. + "-Xcc" "-I" "-Xcc" "${LLVM_INCLUDE_DIR}" # Bridging modules and headers. "-Xcc" "-I" "-Xcc" "${SWIFT_SOURCE_DIR}/include" # Generated C headers. diff --git a/SwiftCompilerSources/Package.swift b/SwiftCompilerSources/Package.swift index 3c65e816d8442..196da0de98d36 100644 --- a/SwiftCompilerSources/Package.swift +++ b/SwiftCompilerSources/Package.swift @@ -20,6 +20,8 @@ private extension Target { "-Xfrontend", "-enable-cxx-interop", // Bridging modules and headers "-Xcc", "-I", "-Xcc", "../include", + // LLVM modules and headers + "-Xcc", "-I", "-Xcc", "../../llvm-project/llvm/include", "-cross-module-optimization" ]), ] diff --git a/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift b/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift index 133ba17979066..37683c57f6599 100644 --- a/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift +++ b/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift @@ -80,7 +80,7 @@ public struct DiagnosticEngine { highlight: CharSourceRange? = nil, fixIts: [DiagnosticFixIt] = []) { - let bridgedSourceLoc: BridgedSourceLoc = position.bridged + let bridgedSourceLoc: swift.SourceLoc = position.bridged let bridgedHighlightRange: BridgedCharSourceRange = highlight.bridged var bridgedArgs: [BridgedDiagnosticArgument] = [] var bridgedFixIts: [BridgedDiagnosticFixIt] = [] diff --git a/SwiftCompilerSources/Sources/Basic/SourceLoc.swift b/SwiftCompilerSources/Sources/Basic/SourceLoc.swift index 6d79f80e1ae1f..08426f512ec8b 100644 --- a/SwiftCompilerSources/Sources/Basic/SourceLoc.swift +++ b/SwiftCompilerSources/Sources/Basic/SourceLoc.swift @@ -21,15 +21,15 @@ public struct SourceLoc { self.locationInFile = locationInFile } - public init?(bridged: BridgedSourceLoc) { - guard let locationInFile = bridged.pointer else { + public init?(bridged: swift.SourceLoc) { + guard bridged.isValid() else { return nil } - self.init(locationInFile: locationInFile) + self.locationInFile = bridged.getOpaquePointerValue().assumingMemoryBound(to: UInt8.self) } - public var bridged: BridgedSourceLoc { - .init(pointer: locationInFile) + public var bridged: swift.SourceLoc { + .init(llvm.SMLoc.getFromPointer(locationInFile)) } } @@ -40,8 +40,8 @@ extension SourceLoc { } extension Optional where Wrapped == SourceLoc { - public var bridged: BridgedSourceLoc { - self?.bridged ?? .init(pointer: nil) + public var bridged: swift.SourceLoc { + self?.bridged ?? .init() } } @@ -68,6 +68,6 @@ public struct CharSourceRange { extension Optional where Wrapped == CharSourceRange { public var bridged: BridgedCharSourceRange { - self?.bridged ?? .init(start: .init(pointer: nil), byteLength: 0) + self?.bridged ?? .init(start: .init(), byteLength: 0) } } diff --git a/SwiftCompilerSources/Sources/Parse/Regex.swift b/SwiftCompilerSources/Sources/Parse/Regex.swift index f8ad6b0656a83..192b6440e44ba 100644 --- a/SwiftCompilerSources/Sources/Parse/Regex.swift +++ b/SwiftCompilerSources/Sources/Parse/Regex.swift @@ -92,7 +92,7 @@ public func _RegexLiteralParsingFn( _ versionOut: UnsafeMutablePointer, _ captureStructureOut: UnsafeMutableRawPointer, _ captureStructureSize: CUnsignedInt, - _ bridgedDiagnosticBaseLoc: BridgedSourceLoc, + _ bridgedDiagnosticBaseLoc: swift.SourceLoc, _ bridgedDiagnosticEngine: BridgedDiagnosticEngine ) -> Bool { let str = String(cString: inputPtr) diff --git a/include/swift/AST/ASTBridging.h b/include/swift/AST/ASTBridging.h index daeb249a76497..7a2a8716f6af3 100644 --- a/include/swift/AST/ASTBridging.h +++ b/include/swift/AST/ASTBridging.h @@ -18,10 +18,6 @@ #include #include -#ifdef __cplusplus -extern "C" { -#endif - SWIFT_BEGIN_NULLABILITY_ANNOTATIONS //===----------------------------------------------------------------------===// @@ -56,7 +52,7 @@ typedef struct { } BridgedDiagnosticArgument; typedef struct { - BridgedSourceLoc start; + swift::SourceLoc start; SwiftInt byteLength; BridgedStringRef text; } BridgedDiagnosticFixIt; @@ -70,7 +66,7 @@ typedef struct { } BridgedOptionalDiagnosticEngine; // FIXME: Can we bridge InFlightDiagnostic? -void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, BridgedSourceLoc loc, +void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, swift::SourceLoc loc, BridgedDiagID diagID, BridgedArrayRef arguments, BridgedCharSourceRange highlight, BridgedArrayRef fixIts); @@ -79,8 +75,4 @@ bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine); SWIFT_END_NULLABILITY_ANNOTATIONS -#ifdef __cplusplus -} // extern "C" -#endif - #endif // SWIFT_AST_ASTBRIDGING_H diff --git a/include/swift/Basic/BasicBridging.h b/include/swift/Basic/BasicBridging.h index 7586370b117fa..111dc08a96c80 100644 --- a/include/swift/Basic/BasicBridging.h +++ b/include/swift/Basic/BasicBridging.h @@ -14,12 +14,9 @@ #define SWIFT_BASIC_BASICBRIDGING_H #include "swift/Basic/BridgedSwiftObject.h" +#include "swift/Basic/SourceLoc.h" #include -#ifdef __cplusplus -extern "C" { -#endif - SWIFT_BEGIN_NULLABILITY_ANNOTATIONS typedef intptr_t SwiftInt; @@ -48,18 +45,10 @@ void freeBridgedStringRef(BridgedStringRef str); //===----------------------------------------------------------------------===// typedef struct { - const unsigned char * _Nullable pointer; -} BridgedSourceLoc; - -typedef struct { - BridgedSourceLoc start; + swift::SourceLoc start; SwiftInt byteLength; } BridgedCharSourceRange; SWIFT_END_NULLABILITY_ANNOTATIONS -#ifdef __cplusplus -} // extern "C" -#endif - #endif diff --git a/include/swift/Basic/BridgingUtils.h b/include/swift/Basic/BridgingUtils.h index bd8fe2e6a06d2..c256857e19a7c 100644 --- a/include/swift/Basic/BridgingUtils.h +++ b/include/swift/Basic/BridgingUtils.h @@ -36,25 +36,14 @@ inline llvm::ArrayRef getArrayRef(BridgedArrayRef bridged) { return {static_cast(bridged.data), bridged.numElements}; } -inline SourceLoc getSourceLoc(const BridgedSourceLoc &bridged) { - return SourceLoc( - llvm::SMLoc::getFromPointer((const char *)bridged.pointer)); -} - -inline BridgedSourceLoc getBridgedSourceLoc(const SourceLoc &loc) { - return {static_cast(loc.getOpaquePointerValue())}; -} - inline CharSourceRange getCharSourceRange(const BridgedCharSourceRange &bridged) { - auto start = getSourceLoc(bridged.start); - return CharSourceRange(start, bridged.byteLength); + return CharSourceRange(bridged.start, bridged.byteLength); } inline BridgedCharSourceRange getBridgedCharSourceRange(const CharSourceRange &range) { - auto start = getBridgedSourceLoc(range.getStart()); - return {start, range.getByteLength()}; + return {range.getStart(), range.getByteLength()}; } /// Copies the string in an malloc'ed memory and the caller is responsible for diff --git a/include/swift/Parse/RegexParserBridging.h b/include/swift/Parse/RegexParserBridging.h index bc2f97ed2d9cf..22d0e7024aa49 100644 --- a/include/swift/Parse/RegexParserBridging.h +++ b/include/swift/Parse/RegexParserBridging.h @@ -17,10 +17,6 @@ #include "swift/AST/ASTBridging.h" #include -#ifdef __cplusplus -extern "C" { -#endif - /// Attempt to lex a regex literal string. Takes the following arguments: /// /// - CurPtrPtr: A pointer to the current pointer of lexer, which should be the @@ -60,12 +56,8 @@ typedef bool (*RegexLiteralParsingFn)(/*InputPtr*/ const char *_Nonnull, /*VersionOut*/ unsigned *_Nonnull, /*CaptureStructureOut*/ void *_Nonnull, /*CaptureStructureSize*/ unsigned, - /*DiagnosticBaseLoc*/ BridgedSourceLoc, + /*DiagnosticBaseLoc*/ swift::SourceLoc, BridgedDiagnosticEngine); void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn _Nullable fn); -#ifdef __cplusplus -} // extern "C" -#endif - #endif // REGEX_PARSER_BRIDGING diff --git a/lib/AST/ASTBridging.cpp b/lib/AST/ASTBridging.cpp index a14c4f322c9ce..cfd0e6e7ed5c6 100644 --- a/lib/AST/ASTBridging.cpp +++ b/lib/AST/ASTBridging.cpp @@ -38,14 +38,13 @@ getDiagnosticArgument(const BridgedDiagnosticArgument &bridged) { } // namespace void DiagnosticEngine_diagnose( - BridgedDiagnosticEngine bridgedEngine, BridgedSourceLoc bridgedLoc, + BridgedDiagnosticEngine bridgedEngine, SourceLoc loc, BridgedDiagID bridgedDiagID, BridgedArrayRef /*BridgedDiagnosticArgument*/ bridgedArguments, BridgedCharSourceRange bridgedHighlight, BridgedArrayRef /*BridgedDiagnosticFixIt*/ bridgedFixIts) { auto *D = getDiagnosticEngine(bridgedEngine); - auto loc = getSourceLoc(bridgedLoc); auto diagID = static_cast(bridgedDiagID); SmallVector arguments; for (auto bridgedArg : @@ -62,7 +61,7 @@ void DiagnosticEngine_diagnose( // Add fix-its. for (auto bridgedFixIt : getArrayRef(bridgedFixIts)) { - auto range = CharSourceRange(getSourceLoc(bridgedFixIt.start), + auto range = CharSourceRange(bridgedFixIt.start, bridgedFixIt.byteLength); auto text = getStringRef(bridgedFixIt.text); inflight.fixItReplaceChars(range.getStart(), range.getEnd(), text); diff --git a/lib/Parse/ParseRegex.cpp b/lib/Parse/ParseRegex.cpp index 23abebb5c61b5..52eee160f8b7f 100644 --- a/lib/Parse/ParseRegex.cpp +++ b/lib/Parse/ParseRegex.cpp @@ -51,7 +51,7 @@ ParserResult Parser::parseExprRegexLiteral() { regexLiteralParsingFn(regexText.str().c_str(), &version, /*captureStructureOut*/ capturesBuf.data(), /*captureStructureSize*/ capturesBuf.size(), - /*diagBaseLoc*/ getBridgedSourceLoc(Tok.getLoc()), + /*diagBaseLoc*/ Tok.getLoc(), getBridgedDiagnosticEngine(&Diags)); auto loc = consumeToken(); SourceMgr.recordRegexLiteralStartLoc(loc);