-
Notifications
You must be signed in to change notification settings - Fork 46
Introduce protocol-based structure for generating code #249
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
Conversation
@rintaro I could use a bit of your thoughts here. Currently, it seems like the visitor depends on translated types, specifically this function
used in places like
This stuff seems FFM specific, so should really live in the FFM implementation of the protocol and the visitor should not depend on it. It seems like the property |
@@ -0,0 +1,3 @@ | |||
protocol Swift2JavaGenerator { | |||
func generate() throws |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems kind of weird that this protocol just has an "empty" function with no arguments. But, I did that to prevent having to pass AnalysisResult
to a bunch of sub-functions in print...
So instead the implementations can receive them as initializer arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear for me how this protocol will be used. The main flow can be just like:
try translator.analyze()
if shouldGenerateFFM {
FFMSwift2JavaGenerator(analysis, ...).generate()
}
if shouldGenrateJNI {
JNISwift2JavaGenerator(analysis, ...).generate()
}
Could you explain why we'd want this protocol?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that’s pretty equivalent. The protocol being just the generate method seemed nicer to me, and we’d swap out what we generate with but it’s equivalent to that if statement. Since there’s not much to share either approach is fine i think, modeling wise i like the protocol but we could do the if
@@ -88,6 +88,13 @@ extension Swift2JavaTranslator { | |||
/// a checked truncation operation at the Java/Swift board. | |||
var javaPrimitiveForSwiftInt: JavaType { .long } | |||
|
|||
var result: AnalysisResult { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should just be the return value of analyze()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that'll be nicer.
The translator still is stateful since it may keep names for resolution etc but it'll be nicer to return the result like that 👍
I'm totally fine with delaying FFM There are a couple of reasons I didn't do that:
That being said, I now feel sharing var translatedSignatures: [ImportedFunc: TranslatedFunctionSignature] |
Cool, that’s the outcome I was hoping for here — when we delay the translated signature the analysis can remain detached of the output, and FFM and JNI can do their own translation/lowering based on that result 👍 |
Okay, I am moving the FFM translation part to after @madsodgaard Sorry for the conflicts, but I believe this makes JNI additions easier. |
Thank you Rintaro! :) |
c668a45
to
08aa68d
Compare
@rintaro @ktoso I think this is ready for a first pass. For now, I have just kept the protocol, but as @rintaro pointed out, we don't actually do any dynamic swapping it in that way, but I'll just keep it around for now. Otherwise, most of the stuff is just moving the previous translator stuff into |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The license header failure is about Sources/JExtractSwift/GenerationMode.swift and other files missing the header we have in other files, can you add these please? :) |
I merged the tools rename, I can do the conflict resolution or leave it you -- let's catch up on chat :) |
let filename = "\(ty.swiftNominal.name).java" | ||
log.info("Printing contents: \(filename)") | ||
printImportedNominal(&printer, ty) | ||
|
||
if let outputFile = try printer.writeContents( | ||
outputDirectory: outputDirectory, | ||
outputDirectory: javaOutputDirectory, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good! Btw I think we should start consistently prefixing things with java or swift; We used to have a bit of a mix, we'll now start converging on everything being prefixed with swift/java depending on which bit it applies to (like moduleNameI think we need to make into
swiftModule` etc.
@Option( | ||
name: .long, | ||
help: "The mode of generation to use for the output files.") | ||
var mode: GenerationMode = .ffm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--mode
sounds good to me 👍
567a1da
to
30b0ce0
Compare
This goal of this PR is to introduce a protocol
Swift2JavaGenerator
, which does all the code generation. This will allow us to provide multiple generation modes, such as FFM or JNI.Most of the stuff from
Swift2JavaTranslator+...
has been moved intoFFMSwift2JavaGenerator
.