Skip to content
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

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

Open
wants to merge 1 commit into
base: ke2
Choose a base branch
from
Open
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
187 changes: 108 additions & 79 deletions java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<DbLocation>,
parent: Label<out DbExprparent>,
idx: Int,
callable: Label<out DbCallable>?,
enclosingStmt: Label<out DbStmt>?,
/*
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with extractBinaryExpressionWith and extractUnaryExpressionWith, how about renaming this extractConstantWith and putting the write parameter last?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or alternatively, having them put it first, so that optional parameters don't interfere with our ordering

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 as Label<out DbNamedexprorstmt>)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have private fun <T : DbNamedexprorstmt> and drop the cast?

extractExprContext(it, locId, callable, enclosingStmt)
}

/*
OLD: KE1
private fun extractAssignExpr(
Expand Down Expand Up @@ -436,8 +477,8 @@ private fun KotlinFileExtractor.extractBinaryExpression(
)

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

Expand All @@ -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,
Expand All @@ -1952,15 +2041,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 @@ -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<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