diff --git a/include/swift/AST/ASTBridging.h b/include/swift/AST/ASTBridging.h index 1795ce6e9a9d9..c7bd27f089f97 100644 --- a/include/swift/AST/ASTBridging.h +++ b/include/swift/AST/ASTBridging.h @@ -185,6 +185,9 @@ SWIFT_NAME("BridgedASTContext.langOptsHasFeature(self:_:)") bool BridgedASTContext_langOptsHasFeature(BridgedASTContext cContext, BridgedFeature feature); +SWIFT_NAME("getter:BridgedASTContext.majorLanguageVersion(self:)") +unsigned BridgedASTContext_majorLanguageVersion(BridgedASTContext cContext); + //===----------------------------------------------------------------------===// // MARK: AST nodes //===----------------------------------------------------------------------===// diff --git a/lib/AST/ASTBridging.cpp b/lib/AST/ASTBridging.cpp index 9d49bd0f0597e..b6a7840242e5f 100644 --- a/lib/AST/ASTBridging.cpp +++ b/lib/AST/ASTBridging.cpp @@ -116,6 +116,10 @@ bool BridgedASTContext_langOptsHasFeature(BridgedASTContext cContext, return cContext.unbridged().LangOpts.hasFeature((Feature)feature); } +unsigned BridgedASTContext_majorLanguageVersion(BridgedASTContext cContext) { + return cContext.unbridged().LangOpts.EffectiveLanguageVersion[0]; +} + //===----------------------------------------------------------------------===// // MARK: AST nodes //===----------------------------------------------------------------------===// diff --git a/lib/ASTGen/Sources/ASTGen/SourceFile.swift b/lib/ASTGen/Sources/ASTGen/SourceFile.swift index 3be44fc063f78..d75c2ada97870 100644 --- a/lib/ASTGen/Sources/ASTGen/SourceFile.swift +++ b/lib/ASTGen/Sources/ASTGen/SourceFile.swift @@ -64,6 +64,20 @@ extension Parser.ExperimentalFeatures { } } +extension Parser.SwiftVersion { + init?(from context: BridgedASTContext?) { + guard let context else { + return nil + } + switch context.majorLanguageVersion { + case 1, 2, 3, 4: self = .v4 + case 5: self = .v5 + case 6: self = .v6 + default: self = .v6 + } + } +} + /// Parses the given source file and produces a pointer to a new /// ExportedSourceFile instance. @_cdecl("swift_ASTGen_parseSourceFile") @@ -77,7 +91,11 @@ public func parseSourceFile( let buffer = UnsafeBufferPointer(start: buffer, count: bufferLength) let ctx = ctxPtr.map { BridgedASTContext(raw: $0) } - let sourceFile = Parser.parse(source: buffer, experimentalFeatures: .init(from: ctx)) + let sourceFile = Parser.parse( + source: buffer, + swiftVersion: Parser.SwiftVersion(from: ctx), + experimentalFeatures: .init(from: ctx) + ) let exportedPtr = UnsafeMutablePointer.allocate(capacity: 1) let moduleName = String(cString: moduleName)