Skip to content

Commit

Permalink
KE2: Extract bool, char, float, double constants
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasvajk committed Nov 27, 2024
1 parent d3daac8 commit 9d2f523
Showing 1 changed file with 89 additions and 84 deletions.
173 changes: 89 additions & 84 deletions java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.parsing.parseNumericLiteral
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.utils.mapToIndex

context(KaSession)
private fun KotlinFileExtractor.extractExpressionBody(e: KtExpression, callable: Label<out DbCallable>) {
Expand Down Expand Up @@ -194,8 +193,8 @@ private fun KotlinFileExtractor.extractNull(
}

private fun KotlinFileExtractor.extractConstantInteger(
text: String,
t: KaType,
text: String,
v: Number,
locId: Label<DbLocation>,
parent: Label<out DbExprparent>,
Expand All @@ -206,13 +205,31 @@ private fun KotlinFileExtractor.extractConstantInteger(
OLD: KE1
overrideId: Label<out DbExpr>? = null
*/
) =
extractConstant(tw::writeExprs_integerliteral, t, text, v.toString(), locId, parent, idx, callable, enclosingStmt)

@Suppress("UNCHECKED_CAST")
private fun <T : DbExpr> KotlinFileExtractor.extractConstant(
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<DbIntegerliteral>().also {
tw.getFreshIdLabel<T>().also {
val type = useType(t)
tw.writeExprs_integerliteral(it, type.javaResult.id, parent, idx)
write(it, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(it, type.kotlinResult.id)
tw.writeNamestrings(text, v.toString(), it)
tw.writeNamestrings(textName, textValue, it as Label<out DbNamedexprorstmt>)
extractExprContext(it, locId, callable, enclosingStmt)
}

Expand Down Expand Up @@ -435,8 +452,8 @@ private fun KotlinFileExtractor.extractBinaryExpression(
)

extractConstantInteger(
"0",
builtinTypes.int,
"0",
0,
tw.getLocation(expression),
id,
Expand Down Expand Up @@ -1985,16 +2002,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
)
}

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


t.isIntType || t.isShortType || t.isByteType -> {
return extractConstantInteger(
text,
t,
text,
i,
tw.getLocation(e),
parent,
Expand All @@ -2025,15 +2090,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 extractConstant(
tw::writeExprs_longliteral,
t,
text,
i.toString(),
tw.getLocation(e),
parent,
idx,
enclosingCallable,
enclosingStmt
)
}

else -> {
Expand All @@ -2046,68 +2113,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

0 comments on commit 9d2f523

Please sign in to comment.