|
| 1 | +package com.sschr15.aoc.compiler.internal |
| 2 | + |
| 3 | +import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext |
| 4 | +import org.jetbrains.kotlin.backend.common.ir.inline |
| 5 | +import org.jetbrains.kotlin.config.CompilerConfiguration |
| 6 | +import org.jetbrains.kotlin.ir.IrStatement |
| 7 | +import org.jetbrains.kotlin.ir.builders.declarations.buildVariable |
| 8 | +import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin |
| 9 | +import org.jetbrains.kotlin.ir.declarations.IrFunction |
| 10 | +import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyFunction |
| 11 | +import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyFunctionBase |
| 12 | +import org.jetbrains.kotlin.ir.expressions.IrCall |
| 13 | +import org.jetbrains.kotlin.ir.expressions.IrExpression |
| 14 | +import org.jetbrains.kotlin.ir.expressions.IrSyntheticBody |
| 15 | +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI |
| 16 | +import org.jetbrains.kotlin.ir.types.isArray |
| 17 | +import org.jetbrains.kotlin.ir.types.isSubtypeOfClass |
| 18 | +import org.jetbrains.kotlin.ir.types.typeWith |
| 19 | +import org.jetbrains.kotlin.ir.util.getPackageFragment |
| 20 | +import org.jetbrains.kotlin.ir.util.isAnnotationWithEqualFqName |
| 21 | +import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid |
| 22 | +import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid |
| 23 | +import org.jetbrains.kotlin.name.FqName |
| 24 | + |
| 25 | +@OptIn(UnsafeDuringIrConstructionAPI::class) |
| 26 | +class SumInlining(val context: IrPluginContext, val config: CompilerConfiguration) : IrElementTransformerVoid() { |
| 27 | + val skipCheckAnnotation = FqName("com.sschr15.aoc.annotations.SkipOverflowChecks") |
| 28 | + |
| 29 | + override fun visitFunction(declaration: IrFunction): IrStatement { |
| 30 | + if (declaration.annotations.any { it.isAnnotationWithEqualFqName(skipCheckAnnotation) }) { |
| 31 | + return declaration // Skip this function |
| 32 | + } |
| 33 | + |
| 34 | + val body = declaration.body ?: return super.visitFunction(declaration) // No body to transform |
| 35 | + if (body is IrSyntheticBody) return super.visitFunction(declaration) // Synthetic body has no statements |
| 36 | + |
| 37 | +// val statements = body.statements.toMutableList() |
| 38 | +// statements.transformFlat { statement -> |
| 39 | +// if (statement !is IrCall) return@transformFlat (statement.transform(this, null) as? IrStatement)?.let(::listOf) |
| 40 | +// val function = statement.symbol.owner |
| 41 | +// config.report(CompilerMessageSeverity.WARNING, "Attempting function call: ${function.name}") |
| 42 | +// if (function.getPackageFragment().packageFqName != FqName("kotlin.collections")) |
| 43 | +// return@transformFlat listOf(statement.transform(this, null)) |
| 44 | +// if (function.name.asString() !in listOf("sum", "sumBy", "sumOf")) return@transformFlat listOf(statement.transform(this, null)) |
| 45 | +//// config.report(CompilerMessageSeverity.WARNING, "Attempting sum function call: ${function.name}") |
| 46 | +// if (statement.extensionReceiver == null) return@transformFlat listOf(statement.transform(this, null)) |
| 47 | +// val receiver = statement.extensionReceiver!! |
| 48 | +// val receiverType = receiver.type |
| 49 | +// if (!receiverType.isArray() && !receiverType.isSubtypeOfClass(context.irBuiltIns.iterableClass)) return@transformFlat listOf(statement.transform(this, null)) |
| 50 | +// |
| 51 | +// // Narrowed it to a summation call on a collection, time to forcefully inline it! |
| 52 | +// if (function.valueParameters.isEmpty()) { |
| 53 | +// // sum() |
| 54 | +// listOf(function.inline(declaration)) |
| 55 | +// } else { |
| 56 | +// // sumBy { ... } or sumOf { ... } |
| 57 | +// val lambda = buildVariable(declaration, function.startOffset, function.endOffset, IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA, function.name, context.irBuiltIns.functionN(1).typeWith(receiverType)) |
| 58 | +// lambda.initializer = statement.getValueArgument(0) |
| 59 | +// listOf( |
| 60 | +// lambda, |
| 61 | +// function.inline(declaration, listOf(lambda)) |
| 62 | +// ) |
| 63 | +// } |
| 64 | +// } |
| 65 | + |
| 66 | + declaration.transformChildrenVoid(object : IrElementTransformerVoid() { |
| 67 | + override fun visitCall(expression: IrCall): IrExpression { |
| 68 | + val function = expression.symbol.owner |
| 69 | + if (function.getPackageFragment().packageFqName != FqName("kotlin.collections")) |
| 70 | + return super.visitCall(expression) |
| 71 | + if (function.name.asString() !in listOf("sum", "sumBy", "sumOf")) return super.visitCall(expression) |
| 72 | + if (expression.extensionReceiver == null) return super.visitCall(expression) |
| 73 | + val receiver = expression.extensionReceiver!! |
| 74 | + val receiverType = receiver.type |
| 75 | + if (!receiverType.isArray() && !receiverType.isSubtypeOfClass(context.irBuiltIns.iterableClass)) return super.visitCall(expression) |
| 76 | + |
| 77 | + if (function.valueParameters.isEmpty()) { |
| 78 | + // sum() |
| 79 | + return function.inline(declaration) |
| 80 | + } else { |
| 81 | + // sumBy { ... } or sumOf { ... } |
| 82 | + val lambda = buildVariable(declaration, function.startOffset, function.endOffset, IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA, function.name, context.irBuiltIns.functionN(1).typeWith(receiverType)) |
| 83 | + lambda.initializer = expression.getValueArgument(0) |
| 84 | + |
| 85 | + return function.inline(declaration, listOf(lambda)) |
| 86 | + } |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + return declaration |
| 91 | + } |
| 92 | +} |
0 commit comments