Skip to content

[ASTGen] Pass Swift version to SwiftParser #71367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//===----------------------------------------------------------------------===//
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//===----------------------------------------------------------------------===//
Expand Down
20 changes: 19 additions & 1 deletion lib/ASTGen/Sources/ASTGen/SourceFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<ExportedSourceFile>.allocate(capacity: 1)
let moduleName = String(cString: moduleName)
Expand Down