WIP: Use IR for KotlinParser instead of PSI/FIR#6325
WIP: Use IR for KotlinParser instead of PSI/FIR#6325Crustack wants to merge 9 commits intoopenrewrite:mainfrom
Conversation
rewrite-kotlin/src/main/java/org/openrewrite/kotlin/KotlinParser.java
Outdated
Show resolved
Hide resolved
rewrite-kotlin/src/main/java/org/openrewrite/kotlin/KotlinParser.java
Outdated
Show resolved
Hide resolved
|
@PhilKes , thanks for the work here. However, this seems to be using the K1 compiler which would limit options toward Kotlin 2.x work and be going in reverse of where the new development is happening by the Kotlin team in the K2 compiler. |
Hi thanks for the feedback, I now updated the code to leverage the same logic as My next step would be to try to update the |
| import java.util.regex.Pattern; | ||
| import java.util.stream.Collectors; |
There was a problem hiding this comment.
| import java.util.regex.Pattern; | |
| import java.util.stream.Collectors; | |
| import java.util.regex.Pattern; |
rewrite-kotlin/src/main/java/org/openrewrite/kotlin/KotlinParser.java
Outdated
Show resolved
Hide resolved
rewrite-kotlin/src/main/kotlin/org/openrewrite/kotlin/KotlinIrTypeMapping.kt
Outdated
Show resolved
Hide resolved
Other than that I am checking what differences are in the generated |
| .filter(source -> { | ||
| return !source.getSourcePath().getFileName().toString().startsWith("dependsOn-"); | ||
| }); |
There was a problem hiding this comment.
| .filter(source -> { | |
| return !source.getSourcePath().getFileName().toString().startsWith("dependsOn-"); | |
| }); | |
| .filter(source -> !source.getSourcePath().getFileName().toString().startsWith("dependsOn-")); |
| configureBaseRoots(compilerConfiguration, arguments); | ||
| AtomicInteger idx = new AtomicInteger(0); | ||
| arguments.setFreeArgs(sources.stream() | ||
| .map(source -> tempFile(ctx, source, idx.getAndIncrement())).collect(Collectors.toList())); |
There was a problem hiding this comment.
| .map(source -> tempFile(ctx, source, idx.getAndIncrement())).collect(Collectors.toList())); | |
| .map(source -> tempFile(ctx, source, idx.getAndIncrement())).collect(toList())); |
rewrite-kotlin/src/main/kotlin/org/openrewrite/kotlin/KotlinIrTypeMapping.kt
Outdated
Show resolved
Hide resolved
rewrite-kotlin/src/main/kotlin/org/openrewrite/kotlin/KotlinIrTypeMapping.kt
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
| .filter(source -> { | ||
| return !source.getSourcePath().getFileName().toString().startsWith("dependsOn-"); | ||
| }); |
There was a problem hiding this comment.
| .filter(source -> { | |
| return !source.getSourcePath().getFileName().toString().startsWith("dependsOn-"); | |
| }); | |
| .filter(source -> !source.getSourcePath().getFileName().toString().startsWith("dependsOn-")); |
| configureBaseRoots(compilerConfiguration, arguments); | ||
| AtomicInteger idx = new AtomicInteger(0); | ||
| arguments.setFreeArgs(sources.stream() | ||
| .map(source -> tempFile(ctx, source, idx.getAndIncrement())).collect(Collectors.toList())); |
There was a problem hiding this comment.
| .map(source -> tempFile(ctx, source, idx.getAndIncrement())).collect(Collectors.toList())); | |
| .map(source -> tempFile(ctx, source, idx.getAndIncrement())).collect(toList())); |
| .filter(source -> { | ||
| return !source.getSourcePath().getFileName().toString().startsWith("dependsOn-"); | ||
| }); |
There was a problem hiding this comment.
| .filter(source -> { | |
| return !source.getSourcePath().getFileName().toString().startsWith("dependsOn-"); | |
| }); | |
| .filter(source -> !source.getSourcePath().getFileName().toString().startsWith("dependsOn-")); |
| addJvmClasspathRoot(compilerConfiguration, PathUtil.getResourcePathForClass(AnnotationTarget.class)); | ||
| AtomicInteger idx = new AtomicInteger(0); | ||
| arguments.setFreeArgs(sources.stream() | ||
| .map(source -> tempFile(ctx, source, idx.getAndIncrement())).collect(Collectors.toList())); |
There was a problem hiding this comment.
| .map(source -> tempFile(ctx, source, idx.getAndIncrement())).collect(Collectors.toList())); | |
| .map(source -> tempFile(ctx, source, idx.getAndIncrement())).collect(toList())); |
|
|
||
| import java.nio.charset.Charset; | ||
| import java.nio.file.Path; | ||
| import java.util.*; |
There was a problem hiding this comment.
| import java.util.*; | |
| import java.util.ArrayList; | |
| import java.util.List; |
| } | ||
| JContainer<Expression> init = JContainer.build(Space.EMPTY, elems, Markers.EMPTY); | ||
| JavaType arrType = typeMapping.type(irVararg); | ||
| return new J.NewArray(randomId(), Space.EMPTY, Markers.EMPTY, null, Collections.emptyList(), init, arrType); |
There was a problem hiding this comment.
| return new J.NewArray(randomId(), Space.EMPTY, Markers.EMPTY, null, Collections.emptyList(), init, arrType); | |
| return new J.NewArray(randomId(), Space.EMPTY, Markers.EMPTY, null, emptyList(), init, arrType); |
| @@ -0,0 +1,48 @@ | |||
| package org.openrewrite.kotlin.internal; | |||
There was a problem hiding this comment.
| package org.openrewrite.kotlin.internal; | |
| /* | |
| * Copyright 2025 the original author or authors. | |
| * <p> | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * <p> | |
| * https://www.apache.org/licenses/LICENSE-2.0 | |
| * <p> | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| package org.openrewrite.kotlin.internal; |
| "K.CompilationUnit of KotlinIrTreeParserVisitor does not match output of KotlinTreeParserVisitor" | ||
| ); | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
| } | |
| } |
|
Since its not prudent trying to update Kotlin-Compiler and move from PSI/FIR to IR in one PR, I am closing this in favor of: #6338 |
This is Work in Progress
What's changed?
Instead of relying on PSI/FIR for Kotlin Parsing, this proposes to leverage
KotlinToJVMBytecodeCompiler.INSTANCE.analyze+JvmIrCodegenFactory.convertToIrto generate theIrFiles.What's your motivation?
Improve Kotlin support
Anything in particular you'd like reviewers to focus on?
Since I am fairly new to the entire Kotlin Compiler eco-system and have only worked with PSI before, some input on the used methods to get the IrFiles is welcome.
Also since I haven't looked deeply into the
KotlinTreeParserVisitorimplementation yet, it would be good to know if we would still need PSI/FIR for some information or would IR be sufficient?Anyone you would like to review specifically?
@shanman190
Have you considered any alternatives or workarounds?
No
Any additional context
TODOs:
IrFiles for the given source kotlin files (workingKotlinTypeIrSignatureBuilderTest)firFile+nodesfromKotlinSource(or will they be necessary still?)KotlinTypeMappingwith generatedFirFilesfirSessionsfromCompiledSourceKotlinTreeParserVisitorvia customIrTreeVisitor?Checklist