Repository: JetBrains/kotlin
File: compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/StringTrimLowering.kt (Kotlin)

Code before:

    private fun maybeComputeTrimIndent(call: IrCall): IrExpression {
        val receiverString = call.extensionReceiver!!.toConstantString() ?: return call
        val newString = receiverString.trimIndent()
        return IrConstImpl.string(call.startOffset, call.endOffset, call.type, newString)
    }

    private fun maybeComputeTrimMargin(call: IrCall): IrExpression {
        val receiverString = call.extensionReceiver!!.toConstantString() ?: return call

        val prefixArgument = call.getValueArgument(0)
        val newString = if (prefixArgument != null) {
            val prefixString = prefixArgument.toConstantString() ?: return call
            receiverString.trimMargin(prefixString)
        } else {
            receiverString.trimMargin()
        }

        return IrConstImpl.string(call.startOffset, call.endOffset, call.type, newString)
    }

    companion object {
        private fun IrExpression.toConstantString(): String? {
            return when (this) {
                is IrConst<*> -> IrConstKind.String.valueOf(this)
                is IrStringConcatenation -> toConstantString()
                else -> null
            }
        }

        private fun IrStringConcatenation.toConstantString(): String? {
            return buildString {
                for (argument in arguments) {
                    when (argument) {
                        is IrConst<*> -> {
                            append(argument.kind.valueOf(argument).toString())
                        }
                        is IrStringConcatenation -> {
                            append(argument.toConstantString() ?: return null)
                        }
                        else -> return null // Concatenation with non-constant.
                    }
                }
            }
        }

        private val trimIndentMatcher = SimpleCalleeMatcher {
            extensionReceiver { it != null && it.type.isString() }
            fqName { it == TRIM_INDENT_FQ_NAME }
            parameterCount { it == 0 }
        }

Review comment (suggestion):
I feel like this should be its own lowering phase for all platforms. Currently this phase gets IR like:
```
$receiver: STRING_CONCATENATION type=kotlin.String
  CONST String type=kotlin.String value="\n        |Hello,\n        |"
  CONST String type=kotlin.String value="Hello,\n        |World"
  CONST String type=kotlin.String value="\n        |World\n    "
```
when it really should only have to care about / check for `IrConst<*>`.

Code after:
            else -> super.visitCall(expression)
        }
    }

    private fun maybeComputeTrimIndent(call: IrCall): IrExpression {
        val receiverString = call.extensionReceiver!!.getConstantString() ?: return call
        val newString = receiverString.trimIndent()
        return IrConstImpl.string(call.startOffset, call.endOffset, call.type, newString)
    }

    private fun maybeComputeTrimMargin(call: IrCall): IrExpression {
        val receiverString = call.extensionReceiver!!.getConstantString() ?: return call

        val prefixArgument = call.getValueArgument(0)
        val newString = if (prefixArgument != null) {
            val prefixString = prefixArgument.getConstantString() ?: return call
            receiverString.trimMargin(prefixString)
        } else {
            receiverString.trimMargin()
        }

        return IrConstImpl.string(call.startOffset, call.endOffset, call.type, newString)
    }

    companion object {
        private fun IrExpression.getConstantString(): String? {
            if (this is IrConst<*> && kind == IrConstKind.String) {
                return IrConstKind.String.valueOf(this)
            }
            return null
        }

        private val trimIndentMatcher = SimpleCalleeMatcher {
            extensionReceiver { it != null && it.type.isString() }
            fqName { it == TRIM_INDENT_FQ_NAME }
            parameterCount { it == 0 }
        }

        private val trimMarginMatcher = SimpleCalleeMatcher {
            extensionReceiver { it != null && it.type.isString() }
            fqName { it == TRIM_MARGIN_FQ_NAME }
            parameterCount { it == 1 }
            parameter(0) { it.type.isString() }
        }

        private val TRIM_MARGIN_FQ_NAME = FqName.fromSegments(listOf("kotlin", "text", "trimMargin"))
        private val TRIM_INDENT_FQ_NAME = FqName.fromSegments(listOf("kotlin", "text", "trimIndent"))
    }
}