Introduce protocol-based structure for generating code#249
Introduce protocol-based structure for generating code#249ktoso merged 9 commits intoswiftlang:mainfrom
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.
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.
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.
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
| /// a checked truncation operation at the Java/Swift board. | ||
| var javaPrimitiveForSwiftInt: JavaType { .long } | ||
|
|
||
| var result: AnalysisResult { |
There was a problem hiding this comment.
Perhaps this should just be the return value of analyze()?
There was a problem hiding this comment.
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 |
|
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 :) |
|
|
||
| if let outputFile = try printer.writeContents( | ||
| outputDirectory: outputDirectory, | ||
| outputDirectory: javaOutputDirectory, |
There was a problem hiding this comment.
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 intoswiftModule` etc.
| @Option( | ||
| name: .long, | ||
| help: "The mode of generation to use for the output files.") | ||
| var mode: GenerationMode = .ffm |
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.