Skip to content

Commit ff0f1a4

Browse files
committed
[ASTGen] Parse Swift version to SwiftParser
1 parent a2a2083 commit ff0f1a4

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ SWIFT_NAME("BridgedASTContext.langOptsHasFeature(self:_:)")
185185
bool BridgedASTContext_langOptsHasFeature(BridgedASTContext cContext,
186186
BridgedFeature feature);
187187

188+
SWIFT_NAME("getter:BridgedASTContext.majorLanguageVersion(self:)")
189+
unsigned BridgedASTContext_majorLanguageVersion(BridgedASTContext cContext);
190+
188191
//===----------------------------------------------------------------------===//
189192
// MARK: AST nodes
190193
//===----------------------------------------------------------------------===//

lib/AST/ASTBridging.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ bool BridgedASTContext_langOptsHasFeature(BridgedASTContext cContext,
116116
return cContext.unbridged().LangOpts.hasFeature((Feature)feature);
117117
}
118118

119+
unsigned BridgedASTContext_majorLanguageVersion(BridgedASTContext cContext) {
120+
return cContext.unbridged().LangOpts.EffectiveLanguageVersion[0];
121+
}
122+
119123
//===----------------------------------------------------------------------===//
120124
// MARK: AST nodes
121125
//===----------------------------------------------------------------------===//

lib/ASTGen/Sources/ASTGen/SourceFile.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ extension Parser.ExperimentalFeatures {
6464
}
6565
}
6666

67+
extension Parser.SwiftVersion {
68+
init?(from context: BridgedASTContext?) {
69+
guard let context else {
70+
return nil
71+
}
72+
switch context.majorLanguageVersion {
73+
case 1, 2, 3, 4: self = .v4
74+
case 5: self = .v5
75+
case 6: self = .v6
76+
default: self = .v6
77+
}
78+
}
79+
}
80+
6781
/// Parses the given source file and produces a pointer to a new
6882
/// ExportedSourceFile instance.
6983
@_cdecl("swift_ASTGen_parseSourceFile")
@@ -77,7 +91,11 @@ public func parseSourceFile(
7791
let buffer = UnsafeBufferPointer(start: buffer, count: bufferLength)
7892

7993
let ctx = ctxPtr.map { BridgedASTContext(raw: $0) }
80-
let sourceFile = Parser.parse(source: buffer, experimentalFeatures: .init(from: ctx))
94+
let sourceFile = Parser.parse(
95+
source: buffer,
96+
swiftVersion: Parser.SwiftVersion(from: ctx),
97+
experimentalFeatures: .init(from: ctx)
98+
)
8199

82100
let exportedPtr = UnsafeMutablePointer<ExportedSourceFile>.allocate(capacity: 1)
83101
let moduleName = String(cString: moduleName)

0 commit comments

Comments
 (0)