From cbc1458180a40f88da98137dcc9aca7b7791f28b Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Wed, 27 Nov 2024 11:24:39 +0100 Subject: [PATCH] KE2: Extract `bool`, `char`, `float`, `double` constants --- .../src/main/kotlin/entities/Expression.kt | 187 ++++++++++-------- 1 file changed, 108 insertions(+), 79 deletions(-) diff --git a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt index a14edc38c82b..c4302651f5be 100644 --- a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt +++ b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt @@ -217,6 +217,47 @@ private fun KotlinFileExtractor.extractNull( extractExprContext(it, locId, callable, enclosingStmt) } +private fun KotlinFileExtractor.extractConstantInteger( + t: KaType, + text: String, + v: Number, + locId: Label, + parent: Label, + idx: Int, + callable: Label?, + enclosingStmt: Label?, + /* + OLD: KE1 + overrideId: Label? = null + */ +) = + extractConstant(tw::writeExprs_integerliteral, t, text, v.toString(), locId, parent, idx, callable, enclosingStmt) + +@Suppress("UNCHECKED_CAST") +private fun KotlinFileExtractor.extractConstant( + write: (Label, Label, Label, Int) -> Unit, + t: KaType, + textName: String, + textValue: String, + locId: Label, + parent: Label, + idx: Int, + callable: Label?, + enclosingStmt: Label?, + /* + OLD: KE1 + overrideId: Label? = null + */ +) = + // OLD: KE1: Was: exprIdOrFresh(overrideId).also { + tw.getFreshIdLabel().also { + val type = useType(t) + write(it, type.javaResult.id, parent, idx) + tw.writeExprsKotlinType(it, type.kotlinResult.id) + tw.writeNamestrings(textName, textValue, it as Label) + extractExprContext(it, locId, callable, enclosingStmt) + } + /* OLD: KE1 private fun extractAssignExpr( @@ -436,8 +477,8 @@ private fun KotlinFileExtractor.extractBinaryExpression( ) extractConstantInteger( - "0", builtinTypes.int, + "0", 0, tw.getLocation(expression), id, @@ -1912,16 +1953,65 @@ private fun KotlinFileExtractor.extractConstant( if (t == null) { TODO() } - when (elementType) { - KtNodeTypes.NULL -> { - return extractNull( + return when (elementType) { + KtNodeTypes.NULL -> extractNull( + t, + tw.getLocation(e), + parent, + idx, + enclosingCallable, + enclosingStmt, + // OLD: KE1: overrideId = overrideId + ) + + KtNodeTypes.BOOLEAN_CONSTANT -> extractConstant( + tw::writeExprs_booleanliteral, + t, + text, + text, + tw.getLocation(e), + parent, + idx, + enclosingCallable, + enclosingStmt + ) + + KtNodeTypes.CHARACTER_CONSTANT -> extractConstant( + tw::writeExprs_characterliteral, + t, + text, + text, + tw.getLocation(e), + parent, + idx, + enclosingCallable, + enclosingStmt + ) + + + KtNodeTypes.FLOAT_CONSTANT -> { + val f = parseNumericLiteral(text, elementType) + + if (f == null) { + TODO() + } + + val trapWriterWriteExpr = when { + t.isFloatType -> tw::writeExprs_floatingpointliteral + t.isDoubleType -> tw::writeExprs_doubleliteral + else -> TODO() + } + + return extractConstant( + trapWriterWriteExpr, t, + f.toString(), + f.toString(), tw.getLocation(e), parent, idx, enclosingCallable, - enclosingStmt, - // OLD: KE1: overrideId = overrideId + enclosingStmt ) } @@ -1933,11 +2023,10 @@ private fun KotlinFileExtractor.extractConstant( TODO() } - t.isIntType || t.isShortType || t.isByteType -> { return extractConstantInteger( - text, t, + text, i, tw.getLocation(e), parent, @@ -1952,15 +2041,17 @@ private fun KotlinFileExtractor.extractConstant( } t.isLongType -> { - // OLD: KE1: Was: exprIdOrFresh(overrideId).also { id -> - return tw.getFreshIdLabel().also { id -> - val type = useType(t) - val locId = tw.getLocation(e) - tw.writeExprs_longliteral(id, type.javaResult.id, parent, idx) - tw.writeExprsKotlinType(id, type.kotlinResult.id) - extractExprContext(id, locId, enclosingCallable, enclosingStmt) - tw.writeNamestrings(text, i.toString(), id) - } + return extractConstant( + tw::writeExprs_longliteral, + t, + text, + i.toString(), + tw.getLocation(e), + parent, + idx, + enclosingCallable, + enclosingStmt + ) } else -> { @@ -1973,68 +2064,6 @@ private fun KotlinFileExtractor.extractConstant( TODO() } } - - // TODO: Wrong - return TODO() - /* - OLD: KE1 - val v = e.value - return when { - v is Float -> { - exprIdOrFresh(overrideId).also { id -> - val type = useType(e.type) - val locId = tw.getLocation(e) - tw.writeExprs_floatingpointliteral(id, type.javaResult.id, parent, idx) - tw.writeExprsKotlinType(id, type.kotlinResult.id) - extractExprContext(id, locId, enclosingCallable, enclosingStmt) - tw.writeNamestrings(v.toString(), v.toString(), id) - } - } - v is Double -> { - exprIdOrFresh(overrideId).also { id -> - val type = useType(e.type) - val locId = tw.getLocation(e) - tw.writeExprs_doubleliteral(id, type.javaResult.id, parent, idx) - tw.writeExprsKotlinType(id, type.kotlinResult.id) - extractExprContext(id, locId, enclosingCallable, enclosingStmt) - tw.writeNamestrings(v.toString(), v.toString(), id) - } - } - v is Boolean -> { - exprIdOrFresh(overrideId).also { id -> - val type = useType(e.type) - val locId = tw.getLocation(e) - tw.writeExprs_booleanliteral(id, type.javaResult.id, parent, idx) - tw.writeExprsKotlinType(id, type.kotlinResult.id) - extractExprContext(id, locId, enclosingCallable, enclosingStmt) - tw.writeNamestrings(v.toString(), v.toString(), id) - } - } - v is Char -> { - exprIdOrFresh(overrideId).also { id -> - val type = useType(e.type) - val locId = tw.getLocation(e) - tw.writeExprs_characterliteral(id, type.javaResult.id, parent, idx) - tw.writeExprsKotlinType(id, type.kotlinResult.id) - extractExprContext(id, locId, enclosingCallable, enclosingStmt) - tw.writeNamestrings(v.toString(), v.toString(), id) - } - } - v is String -> { - exprIdOrFresh(overrideId).also { id -> - val type = useType(e.type) - val locId = tw.getLocation(e) - tw.writeExprs_stringliteral(id, type.javaResult.id, parent, idx) - tw.writeExprsKotlinType(id, type.kotlinResult.id) - extractExprContext(id, locId, enclosingCallable, enclosingStmt) - tw.writeNamestrings(toQuotedLiteral(v.toString()), v.toString(), id) - } - } - else -> { - null.also { logger.errorElement("Unrecognised IrConst: " + v.javaClass, e) } - } - } - */ } /*