Skip to content

Add FIR-to-IR conversion phase to KotlinParser#6793

Merged
timtebeek merged 2 commits intotimtebeek/gradle-kts-templatesfrom
timtebeek/add-fir2ir-phase
Feb 23, 2026
Merged

Add FIR-to-IR conversion phase to KotlinParser#6793
timtebeek merged 2 commits intotimtebeek/gradle-kts-templatesfrom
timtebeek/add-fir2ir-phase

Conversation

@timtebeek
Copy link
Copy Markdown
Member

After FIR analysis completes in KotlinParser.parse(), run JvmFir2IrPipelinePhase to convert FIR to IR
Populates the existing irFile field on KotlinSource, enabling future use of KotlinIrTypeMapping for improved Kotlin type resolution
FIR-to-IR conversion is best-effort: if it fails (e.g. for certain annotation expressions with KClass.java), parsing continues with just FIR data

The FIR-to-IR conversion populates the irFile field on KotlinSource, which enables switching from the FIR-based KotlinTypeMapping to the IR-based KotlinIrTypeMapping for type resolution. The benefits:

Better type information. IR (Intermediate Representation) is a lower-level, more fully resolved representation than FIR. After FIR-to-IR conversion, types are fully desugared — type aliases are expanded, implicit conversions are made explicit, and synthetic members (like copy() on data classes) are materialized. This means KotlinIrTypeMapping can produce more accurate JavaType results than the FIR-based mapping.

Simpler type mapping code. The existing KotlinTypeMapping.kt (FIR-based) is ~1400 lines and handles many FIR-specific complexities — Java interop types (FirJavaTypeRef, BinaryJavaClass), symbol resolution through FirSession, and various FIR expression forms. The IR-based KotlinIrTypeMapping.kt is ~790 lines because IR is a more uniform representation — there's one IrClass instead of multiple FIR class variants, one IrFunction hierarchy instead of scattered FIR function types.

Alignment with the Kotlin compiler pipeline. The Kotlin compiler itself uses FIR → IR → bytecode. By running the same FIR-to-IR phase, OpenRewrite's parser sees the same resolved types that the compiler would use for code generation. This reduces the chance of type mismatches between what OpenRewrite sees and what the compiler produces.

Enables future improvements. With IR available, features like resolving operator overloads, understanding delegation patterns, and mapping extension functions become more straightforward since these are all lowered to explicit IR constructs.

After FIR analysis, run JvmFir2IrPipelinePhase to populate the
irFile field on KotlinSource, enabling future use of IR-based
type mapping for improved Kotlin parsing.
@timtebeek timtebeek self-assigned this Feb 22, 2026
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Feb 22, 2026
@timtebeek timtebeek changed the base branch from main to timtebeek/gradle-kts-templates February 23, 2026 09:09
@timtebeek timtebeek marked this pull request as ready for review February 23, 2026 09:15
@timtebeek timtebeek merged commit 0f83c26 into timtebeek/gradle-kts-templates Feb 23, 2026
@timtebeek timtebeek deleted the timtebeek/add-fir2ir-phase branch February 23, 2026 09:15
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Feb 23, 2026
timtebeek added a commit that referenced this pull request Feb 23, 2026
* Add Kotlin script template support for Gradle KTS parsing

Leverage K2 FIR compiler extensions to configure implicit receivers and default
imports for build.gradle.kts and settings.gradle.kts files. This improves type
information for Gradle DSL methods, enabling better recipe matching and method
resolution. Implements FirScriptConfiguratorExtension and
FirScriptResolutionConfigurationExtension to provide Project/Settings as implicit
receivers and Gradle API packages as default imports.

* Unify Gradle recipe visitors for Groovy and Kotlin DSL

Now that Kotlin script templates provide type information for .gradle.kts
files, merge separate GroovyVisitor/KotlinVisitor pairs into single
JavaVisitor implementations where possible.

- UpdateJavaCompatibility: replace dual GroovyIsoVisitor + KotlinIsoVisitor
  with a single JavaVisitor that handles both DSLs
- UpgradeTransitiveDependencyVersion: replace inline GroovyIsoVisitor and
  KotlinIsoVisitor literal visitors with shared ChangeStringLiteral, and
  simplify the constraints precondition check

* Fix FindRepository matching for KTS with type information

Replace MethodMatcher-based pluginManagement/buildscript detection with
simple name matching, since the synthetic class names (RewriteSettings,
RewriteGradleProject) don't match the real types resolved by the K2
compiler (org.gradle.api.initialization.Settings, org.gradle.api.Project).

* Add FIR-to-IR conversion phase to KotlinParser (#6793)

After FIR analysis, run JvmFir2IrPipelinePhase to populate the
irFile field on KotlinSource, enabling future use of IR-based
type mapping for improved Kotlin parsing.

* Unify more Gradle recipe visitors for Groovy and Kotlin DSL

Convert ChangeDependencyGroupId and ChangeDependencyArtifactId from
GroovyIsoVisitor to JavaIsoVisitor so they work with both Groovy and
Kotlin DSL build files.

* Use real Gradle API types in MethodMatcher patterns for KTS support

Update MethodMatcher patterns from synthetic Groovy template types
(RewriteGradleProject, RewriteSettings, PluginSpec, Plugin) to real
Gradle API types (org.gradle.api.Project, Settings, PluginDependenciesSpec,
PluginDependencySpec) with matchOverrides=true, enabling matching for
both Groovy DSL and Kotlin DSL scripts.

* Increase test heap to 2g for rewrite-gradle

The K2 compiler used for KTS parsing loads many JDK modules and
consumes significant memory. The default test worker heap is
insufficient, causing OutOfMemoryError on CI.

* Use regular imports in Plugin.groovy

* Restore `isKotlin` conditions in RemovePluginVisitor due to missing types

* Verify `ChangeDependencyArtifactId` works with .kts

* Verify `ChangeDependencyGroupId` works with .kts

* Verify `DependencyInsight` works with .kts

* Verify `DependencyConstraintToRule` works with .kts

* Merge Groovy/Kotlin visitors into single JavaIsoVisitor in `UpdateJavaCompatibility`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant