Skip to content

KE2: Extract bool, char, float, double constants #18128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 132 additions & 91 deletions java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,59 @@ private fun KotlinFileExtractor.extractNull(
extractExprContext(it, locId, callable, enclosingStmt)
}

context(KaSession)
private fun KotlinFileExtractor.extractConstantInteger(
t: KaType,
text: String,
v: Number,
locId: Label<DbLocation>,
parent: Label<out DbExprparent>,
idx: Int,
callable: Label<out DbCallable>?,
enclosingStmt: Label<out DbStmt>?,
/*
OLD: KE1
overrideId: Label<out DbExpr>? = null
*/
) =
extractConstantWith(
tw::writeExprs_integerliteral,
t,
text,
v.toString(),
locId,
parent,
idx,
callable,
enclosingStmt
)

context(KaSession)
@Suppress("UNCHECKED_CAST")
private fun <T : DbLiteral> KotlinFileExtractor.extractConstantWith(
write: (Label<out T>, Label<out DbType>, Label<out DbExprparent>, Int) -> Unit,
t: KaType,
textName: String,
textValue: String,
locId: Label<DbLocation>,
parent: Label<out DbExprparent>,
idx: Int,
callable: Label<out DbCallable>?,
enclosingStmt: Label<out DbStmt>?,
/*
OLD: KE1
overrideId: Label<out DbExpr>? = null
*/
) =
// OLD: KE1: Was: exprIdOrFresh<DbIntegerliteral>(overrideId).also {
tw.getFreshIdLabel<T>().also {
val type = useType(t)
write(it, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(it, type.kotlinResult.id)
tw.writeNamestrings(textName, textValue, it)
extractExprContext(it, locId, callable, enclosingStmt)
}

/*
OLD: KE1
private fun extractAssignExpr(
Expand Down Expand Up @@ -300,7 +353,7 @@ private fun KotlinFileExtractor.extractPrefixUnaryExpression(
}

if (trapWriterWriteExpr != null) {
return extractUnaryExpressionWith(expression, callable, parent, trapWriterWriteExpr)
return extractUnaryExpressionWith(trapWriterWriteExpr, expression, callable, parent)
}

TODO("Extract as method call")
Expand All @@ -327,7 +380,7 @@ private fun KotlinFileExtractor.extractPostfixUnaryExpression(
}

if (trapWriterWriteExpr != null) {
return extractUnaryExpressionWith(expression, callable, parent, trapWriterWriteExpr)
return extractUnaryExpressionWith(trapWriterWriteExpr, expression, callable, parent)
}

TODO("Extract as method call")
Expand Down Expand Up @@ -372,7 +425,7 @@ private fun KotlinFileExtractor.extractBinaryExpression(
else -> TODO("Extract error expression")
}

return extractBinaryExpressionWith(expression, callable, parent, trapWriterWriteExpr)
return extractBinaryExpressionWith(trapWriterWriteExpr, expression, callable, parent)
}

val trapWriterWriteExpr = when {
Expand All @@ -395,7 +448,7 @@ private fun KotlinFileExtractor.extractBinaryExpression(
}

if (trapWriterWriteExpr != null) {
return extractBinaryExpressionWith(expression, callable, parent, trapWriterWriteExpr)
return extractBinaryExpressionWith(trapWriterWriteExpr, expression, callable, parent)
}

val trapWriterWriteExprComparison = when (op) {
Expand Down Expand Up @@ -437,8 +490,8 @@ private fun KotlinFileExtractor.extractBinaryExpression(
)

extractConstantInteger(
"0",
builtinTypes.int,
"0",
0,
tw.getLocation(expression),
id,
Expand Down Expand Up @@ -490,15 +543,15 @@ private fun <T : DbBinaryexpr> KotlinFileExtractor.extractRawBinaryExpression(

context(KaSession)
private fun <T : DbBinaryexpr> KotlinFileExtractor.extractBinaryExpressionWith(
expression: KtBinaryExpression,
callable: Label<out DbCallable>,
parent: StmtExprParent,
extractExpression: (
id: Label<out T>,
typeid: Label<out DbType>,
parent: Label<out DbExprparent>,
idx: Int
) -> Unit
) -> Unit,
expression: KtBinaryExpression,
callable: Label<out DbCallable>,
parent: StmtExprParent
): Label<out DbExpr> {
val exprParent = parent.expr(expression, callable)
val id = extractRawBinaryExpression(expression.expressionType!!, exprParent, extractExpression)
Expand All @@ -512,15 +565,15 @@ private fun <T : DbBinaryexpr> KotlinFileExtractor.extractBinaryExpressionWith(

context(KaSession)
private fun <T : DbUnaryexpr> KotlinFileExtractor.extractUnaryExpressionWith(
expression: KtUnaryExpression,
callable: Label<out DbCallable>,
parent: StmtExprParent,
extractExpression: (
id: Label<out T>,
typeid: Label<out DbType>,
parent: Label<out DbExprparent>,
idx: Int
) -> Unit
) -> Unit,
expression: KtUnaryExpression,
callable: Label<out DbCallable>,
parent: StmtExprParent
): Label<out DbExpr> {
val id = tw.getFreshIdLabel<T>()
val type = useType(expression.expressionType)
Expand Down Expand Up @@ -1913,16 +1966,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 -> extractConstantWith(
tw::writeExprs_booleanliteral,
t,
text,
text,
tw.getLocation(e),
parent,
idx,
enclosingCallable,
enclosingStmt
)

KtNodeTypes.CHARACTER_CONSTANT -> extractConstantWith(
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 extractConstantWith(
trapWriterWriteExpr,
t,
f.toString(),
f.toString(),
tw.getLocation(e),
parent,
idx,
enclosingCallable,
enclosingStmt,
// OLD: KE1: overrideId = overrideId
enclosingStmt
)
}

Expand All @@ -1934,11 +2036,10 @@ private fun KotlinFileExtractor.extractConstant(
TODO()
}


t.isIntType || t.isShortType || t.isByteType -> {
return extractConstantInteger(
text,
t,
text,
i,
tw.getLocation(e),
parent,
Expand All @@ -1953,15 +2054,17 @@ private fun KotlinFileExtractor.extractConstant(
}

t.isLongType -> {
// OLD: KE1: Was: exprIdOrFresh<DbLongliteral>(overrideId).also { id ->
return tw.getFreshIdLabel<DbLongliteral>().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 extractConstantWith(
tw::writeExprs_longliteral,
t,
text,
i.toString(),
tw.getLocation(e),
parent,
idx,
enclosingCallable,
enclosingStmt
)
}

else -> {
Expand All @@ -1974,68 +2077,6 @@ private fun KotlinFileExtractor.extractConstant(
TODO()
}
}

// TODO: Wrong
return TODO()
/*
OLD: KE1
val v = e.value
return when {
v is Float -> {
exprIdOrFresh<DbFloatingpointliteral>(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<DbDoubleliteral>(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<DbBooleanliteral>(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<DbCharacterliteral>(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<DbStringliteral>(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) }
}
}
*/
}

/*
Expand Down