You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/topics/compiler-plugins.md
+17-14Lines changed: 17 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
[//]: #(title: Compiler plugins)
2
2
3
-
This page explains the Kotlin compiler plugins available to you and what you can do if none of them suits your use case.
4
-
5
3
Compiler plugins hook into the compilation process to analyze or change code while it's being compiled, without modifying the compiler itself. For example, they can annotate code or generate new code to make it compatible with other frameworks or APIs.
6
4
5
+
This page explains the Kotlin compiler plugins available to you and what you can do if none of them suits your use case.
6
+
7
7
The Kotlin team maintains the following compiler plugins:
If you need to adapt the compilation process in a way that these plugins don't cover, first check whether you can use the
24
25
[Kotlin Symbol Processing (KSP) API](https://kotlinlang.org/docs/ksp-overview.html) or an external linter such as [Android lint](https://developer.android.com/studio/write/lint).
@@ -27,15 +28,15 @@ and ask for advice about your use case.
27
28
28
29
If you _still_ can't find what you need, you can create a custom compiler plugin. Use this approach only as a last resort,
29
30
because the Kotlin compiler plugin API is **unstable**. Maintaining a custom compiler plugin requires significant ongoing
30
-
effort, since each new compiler release introduces breaking changes.
31
+
effort, because each new compiler release introduces breaking changes.
31
32
32
33
## Custom compiler plugins
33
34
34
-
> The Kotlin compiler plugin API is unstable and includes breaking changes in every release.
35
+
> The Kotlin compiler plugin API is unstable and introduces breaking changes in every release.
35
36
>
36
37
{style="warning"}
37
38
38
-
Compiler plugins can influence different stages of the compilation process depending on their use case.
39
+
Compiler plugins can influence different stages of the compilation process.
@@ -109,10 +110,17 @@ The backend plugin API, also known as IR, has a single extension point: [`IrGene
109
110
Use this extension point and override the `generate()` function to add bodies to declarations already generated by the
110
111
frontend or change existing declaration bodies.
111
112
112
-
Changes made through this extension point are **not checked**. You must ensure that your changes don't break the compiler's
113
+
Changes made through this extension point are **not checked** by the compiler. You must ensure that your changes don't break the compiler's
113
114
expectations at this stage. For example, you might accidentally introduce an invalid type, an incorrect function reference,
114
115
or a reference outside the correct scope.
115
116
117
+
### Explore backend plugin code
118
+
119
+
You can explore the Kotlin serialization plugin code to see what backend plugin compiler code looks like in practice.
120
+
For example, [`SerializableCompanionIrGenerator.kt`](https://github.com/JetBrains/kotlin/blob/master/plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializerIrGenerator.kt)
121
+
fills in missing bodies for key serializer members. One example is the [`generateChildSerializersGetter()`](https://github.com/JetBrains/kotlin/blob/9cfa558902abc13d245c825717026af63ef82dd2/plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializerIrGenerator.kt#L242)
122
+
function, which collects a list of `KSerializer` expressions and returns them in an array.
123
+
116
124
#### Check your backend plugin for problems
117
125
118
126
You can check for problems in your backend plugin code in three main ways:
@@ -129,11 +137,6 @@ You can check for problems in your backend plugin code in three main ways:
129
137
130
138
In the `convertToIr.kt` file, add breakpoints in the `convertToIrAndActualize()` function and run the compiler in debug mode to get more detailed information during compilation.
131
139
132
-
You can also explore the Kotlin serialization plugin code to see what backend plugin compiler code looks like in practice.
133
-
For example, [`SerializableCompanionIrGenerator.kt`](https://github.com/JetBrains/kotlin/blob/master/plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializerIrGenerator.kt)
134
-
fills in missing bodies for key serializer members. One example is the [`generateChildSerializersGetter()`](https://github.com/JetBrains/kotlin/blob/9cfa558902abc13d245c825717026af63ef82dd2/plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializerIrGenerator.kt#L242)
135
-
function, which collects a list of `KSerializer` expressions and returns them in an array.
136
-
137
140
### Test your plugin
138
141
139
142
Once you implement your plugin, test it thoroughly. The [Kotlin compiler plugin template](https://github.com/Kotlin/compiler-plugin-template)
@@ -157,5 +160,5 @@ new dump files become your latest _golden_ files: an approved and trusted source
157
160
### Get help
158
161
159
162
If you run into issues developing a custom compiler plugin, reach out in [Kotlin Slack](https://surveys.jetbrains.com/s3/kotlin-slack-sign-up)
160
-
in the [#compiler](https://kotlinlang.slack.com/archives/C7L3JB43G) channel. We can't promise a solution but we will try
163
+
in the [#compiler](https://slack-chats.kotlinlang.org/c/compiler) channel. We can't promise a solution but we will try
0 commit comments