FIR: optimize converting string expressions in raw FIR builder
Do get ASTNode for every string template entry as it is expensive operation
This commit is contained in:
@@ -1449,9 +1449,21 @@ class RawFirBuilder(
|
||||
generateConstantExpressionByLiteral(expression)
|
||||
|
||||
override fun visitStringTemplateExpression(expression: KtStringTemplateExpression, data: Unit): FirElement {
|
||||
return expression.entries.toInterpolatingCall(expression) {
|
||||
(this as KtStringTemplateEntryWithExpression).expression.toFirExpression(it)
|
||||
}
|
||||
return expression.entries.toInterpolatingCall(
|
||||
expression,
|
||||
getElementType = { element ->
|
||||
when (element) {
|
||||
is KtLiteralStringTemplateEntry -> KtNodeTypes.LITERAL_STRING_TEMPLATE_ENTRY
|
||||
is KtEscapeStringTemplateEntry -> KtNodeTypes.ESCAPE_STRING_TEMPLATE_ENTRY
|
||||
is KtSimpleNameStringTemplateEntry -> KtNodeTypes.SHORT_STRING_TEMPLATE_ENTRY
|
||||
is KtBlockStringTemplateEntry -> KtNodeTypes.LONG_STRING_TEMPLATE_ENTRY
|
||||
else -> error("invalid node type $element")
|
||||
}
|
||||
},
|
||||
convertTemplateEntry = {
|
||||
(this as KtStringTemplateEntryWithExpression).expression.toFirExpression(it)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
override fun visitReturnExpression(expression: KtReturnExpression, data: Unit): FirElement {
|
||||
|
||||
+2
-1
@@ -325,6 +325,7 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
|
||||
|
||||
fun Array<out T?>.toInterpolatingCall(
|
||||
base: T,
|
||||
getElementType: (T) -> IElementType = { it.elementType },
|
||||
convertTemplateEntry: T?.(String) -> FirExpression
|
||||
): FirExpression {
|
||||
return buildStringConcatenationCall {
|
||||
@@ -333,7 +334,7 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
|
||||
argumentList = buildArgumentList {
|
||||
L@ for (entry in this@toInterpolatingCall) {
|
||||
if (entry == null) continue
|
||||
arguments += when (entry.elementType) {
|
||||
arguments += when (getElementType(entry)) {
|
||||
OPEN_QUOTE, CLOSING_QUOTE -> continue@L
|
||||
LITERAL_STRING_TEMPLATE_ENTRY -> {
|
||||
sb.append(entry.asText)
|
||||
|
||||
Reference in New Issue
Block a user