1
1
package org.jetbrains.kotlinx.dataframe
2
2
3
- import java.io.File
4
3
import org.jetbrains.kotlin.backend.common.FileLoweringPass
5
4
import org.jetbrains.kotlin.backend.common.extensions.FirIncompatiblePluginAPI
6
5
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
@@ -44,10 +43,13 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
44
43
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
45
44
import org.jetbrains.kotlin.name.FqName
46
45
import org.jetbrains.kotlin.name.Name
46
+ import java.io.File
47
47
48
48
data class ContainingDeclarations (val clazz : IrClass ? , val function : IrFunction ? , val statementIndex : Int = 0 )
49
49
50
- class ExplainerIrTransformer (val pluginContext : IrPluginContext ) : FileLoweringPass, IrElementTransformer<ContainingDeclarations> {
50
+ class ExplainerIrTransformer (
51
+ val pluginContext : IrPluginContext
52
+ ) : FileLoweringPass, IrElementTransformer<ContainingDeclarations> {
51
53
lateinit var file: IrFile
52
54
lateinit var source: String
53
55
override fun lower (irFile : IrFile ) {
@@ -70,7 +72,7 @@ class ExplainerIrTransformer(val pluginContext: IrPluginContext) : FileLoweringP
70
72
override fun visitBlockBody (body : IrBlockBody , data : ContainingDeclarations ): IrBody {
71
73
for (i in 0 until body.statements.size) {
72
74
@Suppress(" UNCHECKED_CAST" )
73
- (body.statements.set(i, (body.statements.get(i) as IrElementBase ).transform(this , data.copy(statementIndex = i)) as IrStatement ))
75
+ (body.statements.set(i, (body.statements.get(i) as IrElementBase ).transform(this , data.copy(statementIndex = i)) as IrStatement )) // ktlint-disable
74
76
}
75
77
return body
76
78
}
@@ -168,14 +170,14 @@ class ExplainerIrTransformer(val pluginContext: IrPluginContext) : FileLoweringP
168
170
169
171
val symbol = IrSimpleFunctionSymbolImpl ()
170
172
val alsoLambda = IrFunctionImpl (
171
- - 1 ,
172
- - 1 ,
173
- IrDeclarationOrigin .LOCAL_FUNCTION_FOR_LAMBDA ,
174
- symbol,
175
- Name .special(" <anonymous>" ),
176
- DescriptorVisibilities .LOCAL ,
177
- Modality .FINAL ,
178
- pluginContext.irBuiltIns.unitType,
173
+ startOffset = - 1 ,
174
+ endOffset = - 1 ,
175
+ origin = IrDeclarationOrigin .LOCAL_FUNCTION_FOR_LAMBDA ,
176
+ symbol = symbol ,
177
+ name = Name .special(" <anonymous>" ),
178
+ visibility = DescriptorVisibilities .LOCAL ,
179
+ modality = Modality .FINAL ,
180
+ returnType = pluginContext.irBuiltIns.unitType,
179
181
isInline = false ,
180
182
isExternal = false ,
181
183
isTailrec = false ,
@@ -185,55 +187,58 @@ class ExplainerIrTransformer(val pluginContext: IrPluginContext) : FileLoweringP
185
187
isExpect = false
186
188
).apply {
187
189
valueParameters = buildList {
188
- add(IrValueParameterImpl (
189
- - 1 ,
190
- - 1 ,
191
- IrDeclarationOrigin .DEFINED ,
192
- IrValueParameterSymbolImpl (),
193
- Name .identifier(" it" ),
194
- 0 ,
195
- expression.type,
196
- null ,
197
- isCrossinline = false ,
198
- isNoinline = false ,
199
- isHidden = false ,
200
- isAssignable = false
201
- ))
190
+ add(
191
+ IrValueParameterImpl (
192
+ startOffset = - 1 ,
193
+ endOffset = - 1 ,
194
+ origin = IrDeclarationOrigin .DEFINED ,
195
+ symbol = IrValueParameterSymbolImpl (),
196
+ name = Name .identifier(" it" ),
197
+ index = 0 ,
198
+ type = expression.type,
199
+ varargElementType = null ,
200
+ isCrossinline = false ,
201
+ isNoinline = false ,
202
+ isHidden = false ,
203
+ isAssignable = false
204
+ )
205
+ )
202
206
}
203
- val it = expression
204
207
val itSymbol = valueParameters[0 ].symbol
208
+ val source = try {
209
+ source.substring(expression.startOffset, expression.endOffset)
210
+ } catch (e: Exception ) {
211
+ throw Exception (" $expression ${ownerName.asString()} $source " , e)
212
+ }
213
+ val expressionId = expressionId(expression)
214
+ val receiverId = receiver?.let { expressionId(it) }
215
+ val valueArguments = buildList<IrExpression ?> {
216
+ add(source.irConstImpl())
217
+ add(ownerName.asStringStripSpecialMarkers().irConstImpl())
218
+ add(IrGetValueImpl (- 1 , - 1 , itSymbol))
219
+ add(expressionId.irConstImpl())
220
+ add(receiverId.irConstImpl())
221
+ add(data.clazz?.fqNameWhenAvailable?.asString().irConstImpl())
222
+ add(data.function?.name?.asString().irConstImpl())
223
+ add(IrConstImpl .int(- 1 , - 1 , pluginContext.irBuiltIns.intType, data.statementIndex))
224
+ }
205
225
body = pluginContext.irFactory.createBlockBody(- 1 , - 1 ) {
206
- val doAction = pluginContext.referenceFunctions(FqName (" org.jetbrains.kotlinx.dataframe.explainer.PluginCallback.doAction" )).single()
207
- statements + = IrCallImpl (- 1 , - 1 , doAction.owner.returnType, doAction, 0 , 8 ).apply {
208
- val pluginAction =
209
- pluginContext.referenceClass(FqName (" org.jetbrains.kotlinx.dataframe.explainer.PluginCallback" ))!!
210
- dispatchReceiver = IrGetObjectValueImpl (- 1 , - 1 , pluginAction.defaultType, pluginAction)
211
- val source = try {
212
- source.substring(it.startOffset, it.endOffset)
213
- } catch (e: Exception ) {
214
- throw Exception (" $it ${ownerName.asString()} $source " , e)
226
+ val callback = FqName (" org.jetbrains.kotlinx.dataframe.explainer.PluginCallback.doAction" )
227
+ val doAction = pluginContext.referenceFunctions(callback).single()
228
+ statements + = IrCallImpl (
229
+ startOffset = - 1 ,
230
+ endOffset = - 1 ,
231
+ type = doAction.owner.returnType,
232
+ symbol = doAction,
233
+ typeArgumentsCount = 0 ,
234
+ valueArgumentsCount = valueArguments.size
235
+ ).apply {
236
+ val clazz = FqName (" org.jetbrains.kotlinx.dataframe.explainer.PluginCallback" )
237
+ val plugin = pluginContext.referenceClass(clazz)!!
238
+ dispatchReceiver = IrGetObjectValueImpl (- 1 , - 1 , plugin.defaultType, plugin)
239
+ valueArguments.forEachIndexed { i, argument ->
240
+ putValueArgument(i, argument)
215
241
}
216
- val expressionId = expressionId(expression)
217
- val receiverId = receiver?.let { expressionId(it) }
218
- putValueArgument(0 , IrConstImpl .string(- 1 , - 1 , pluginContext.irBuiltIns.stringType, source))
219
- putValueArgument(1 , IrConstImpl .string(- 1 , - 1 , pluginContext.irBuiltIns.stringType, ownerName.asStringStripSpecialMarkers()))
220
- putValueArgument(2 , IrGetValueImpl (- 1 , - 1 , itSymbol))
221
- putValueArgument(3 , IrConstImpl .string(- 1 , - 1 , pluginContext.irBuiltIns.stringType, expressionId))
222
- fun String?.irConstImpl (): IrConstImpl <out String ?> {
223
- val nullableString = pluginContext.irBuiltIns.stringType.makeNullable()
224
- val argument = if (this == null ) {
225
- IrConstImpl .constNull(- 1 , - 1 , nullableString)
226
- } else {
227
- IrConstImpl .string(- 1 , - 1 , nullableString, this )
228
- }
229
- return argument
230
- }
231
-
232
- val argument = receiverId.irConstImpl()
233
- putValueArgument(4 , argument)
234
- putValueArgument(5 , data.clazz?.fqNameWhenAvailable?.asString().irConstImpl())
235
- putValueArgument(6 , data.function?.name?.asString().irConstImpl())
236
- putValueArgument(7 , IrConstImpl .int(- 1 , - 1 , pluginContext.irBuiltIns.intType, data.statementIndex))
237
242
}
238
243
}
239
244
}
@@ -250,6 +255,16 @@ class ExplainerIrTransformer(val pluginContext: IrPluginContext) : FileLoweringP
250
255
return result
251
256
}
252
257
258
+ private fun String?.irConstImpl (): IrConstImpl <out String ?> {
259
+ val nullableString = pluginContext.irBuiltIns.stringType.makeNullable()
260
+ val argument = if (this == null ) {
261
+ IrConstImpl .constNull(- 1 , - 1 , nullableString)
262
+ } else {
263
+ IrConstImpl .string(- 1 , - 1 , nullableString, this )
264
+ }
265
+ return argument
266
+ }
267
+
253
268
private fun expressionId (expression : IrExpression ): String {
254
269
val line = file.fileEntry.getLineNumber(expression.startOffset)
255
270
val column = file.fileEntry.getColumnNumber(expression.startOffset)
0 commit comments