Skip to content

Commit 035590f

Browse files
committed
[ASTGen] Parse Swift version to SwiftParser
1 parent 23519be commit 035590f

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
@@ -184,6 +184,9 @@ SWIFT_NAME("BridgedASTContext.langOptsHasFeature(self:_:)")
184184
bool BridgedASTContext_langOptsHasFeature(BridgedASTContext cContext,
185185
BridgedFeature feature);
186186

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

lib/AST/ASTBridging.cpp

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

130+
unsigned BridgedASTContext_majorLanguageVersion(BridgedASTContext cContext) {
131+
return cContext.unbridged().LangOpts.EffectiveLanguageVersion[0];
132+
}
133+
130134
//===----------------------------------------------------------------------===//
131135
// MARK: AST nodes
132136
//===----------------------------------------------------------------------===//

lib/ASTGen/Sources/ASTGen/SourceFile.swift

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

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

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

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

0 commit comments

Comments
 (0)