Skip to content

Commit cbc1458

Browse files
committed
KE2: Extract bool, char, float, double constants
1 parent 0572e28 commit cbc1458

File tree

1 file changed

+108
-79
lines changed

1 file changed

+108
-79
lines changed

java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt

Lines changed: 108 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,47 @@ private fun KotlinFileExtractor.extractNull(
217217
extractExprContext(it, locId, callable, enclosingStmt)
218218
}
219219

220+
private fun KotlinFileExtractor.extractConstantInteger(
221+
t: KaType,
222+
text: String,
223+
v: Number,
224+
locId: Label<DbLocation>,
225+
parent: Label<out DbExprparent>,
226+
idx: Int,
227+
callable: Label<out DbCallable>?,
228+
enclosingStmt: Label<out DbStmt>?,
229+
/*
230+
OLD: KE1
231+
overrideId: Label<out DbExpr>? = null
232+
*/
233+
) =
234+
extractConstant(tw::writeExprs_integerliteral, t, text, v.toString(), locId, parent, idx, callable, enclosingStmt)
235+
236+
@Suppress("UNCHECKED_CAST")
237+
private fun <T : DbExpr> KotlinFileExtractor.extractConstant(
238+
write: (Label<out T>, Label<out DbType>, Label<out DbExprparent>, Int) -> Unit,
239+
t: KaType,
240+
textName: String,
241+
textValue: String,
242+
locId: Label<DbLocation>,
243+
parent: Label<out DbExprparent>,
244+
idx: Int,
245+
callable: Label<out DbCallable>?,
246+
enclosingStmt: Label<out DbStmt>?,
247+
/*
248+
OLD: KE1
249+
overrideId: Label<out DbExpr>? = null
250+
*/
251+
) =
252+
// OLD: KE1: Was: exprIdOrFresh<DbIntegerliteral>(overrideId).also {
253+
tw.getFreshIdLabel<T>().also {
254+
val type = useType(t)
255+
write(it, type.javaResult.id, parent, idx)
256+
tw.writeExprsKotlinType(it, type.kotlinResult.id)
257+
tw.writeNamestrings(textName, textValue, it as Label<out DbNamedexprorstmt>)
258+
extractExprContext(it, locId, callable, enclosingStmt)
259+
}
260+
220261
/*
221262
OLD: KE1
222263
private fun extractAssignExpr(
@@ -436,8 +477,8 @@ private fun KotlinFileExtractor.extractBinaryExpression(
436477
)
437478

438479
extractConstantInteger(
439-
"0",
440480
builtinTypes.int,
481+
"0",
441482
0,
442483
tw.getLocation(expression),
443484
id,
@@ -1912,16 +1953,65 @@ private fun KotlinFileExtractor.extractConstant(
19121953
if (t == null) {
19131954
TODO()
19141955
}
1915-
when (elementType) {
1916-
KtNodeTypes.NULL -> {
1917-
return extractNull(
1956+
return when (elementType) {
1957+
KtNodeTypes.NULL -> extractNull(
1958+
t,
1959+
tw.getLocation(e),
1960+
parent,
1961+
idx,
1962+
enclosingCallable,
1963+
enclosingStmt,
1964+
// OLD: KE1: overrideId = overrideId
1965+
)
1966+
1967+
KtNodeTypes.BOOLEAN_CONSTANT -> extractConstant(
1968+
tw::writeExprs_booleanliteral,
1969+
t,
1970+
text,
1971+
text,
1972+
tw.getLocation(e),
1973+
parent,
1974+
idx,
1975+
enclosingCallable,
1976+
enclosingStmt
1977+
)
1978+
1979+
KtNodeTypes.CHARACTER_CONSTANT -> extractConstant(
1980+
tw::writeExprs_characterliteral,
1981+
t,
1982+
text,
1983+
text,
1984+
tw.getLocation(e),
1985+
parent,
1986+
idx,
1987+
enclosingCallable,
1988+
enclosingStmt
1989+
)
1990+
1991+
1992+
KtNodeTypes.FLOAT_CONSTANT -> {
1993+
val f = parseNumericLiteral(text, elementType)
1994+
1995+
if (f == null) {
1996+
TODO()
1997+
}
1998+
1999+
val trapWriterWriteExpr = when {
2000+
t.isFloatType -> tw::writeExprs_floatingpointliteral
2001+
t.isDoubleType -> tw::writeExprs_doubleliteral
2002+
else -> TODO()
2003+
}
2004+
2005+
return extractConstant(
2006+
trapWriterWriteExpr,
19182007
t,
2008+
f.toString(),
2009+
f.toString(),
19192010
tw.getLocation(e),
19202011
parent,
19212012
idx,
19222013
enclosingCallable,
1923-
enclosingStmt,
1924-
// OLD: KE1: overrideId = overrideId
2014+
enclosingStmt
19252015
)
19262016
}
19272017

@@ -1933,11 +2023,10 @@ private fun KotlinFileExtractor.extractConstant(
19332023
TODO()
19342024
}
19352025

1936-
19372026
t.isIntType || t.isShortType || t.isByteType -> {
19382027
return extractConstantInteger(
1939-
text,
19402028
t,
2029+
text,
19412030
i,
19422031
tw.getLocation(e),
19432032
parent,
@@ -1952,15 +2041,17 @@ private fun KotlinFileExtractor.extractConstant(
19522041
}
19532042

19542043
t.isLongType -> {
1955-
// OLD: KE1: Was: exprIdOrFresh<DbLongliteral>(overrideId).also { id ->
1956-
return tw.getFreshIdLabel<DbLongliteral>().also { id ->
1957-
val type = useType(t)
1958-
val locId = tw.getLocation(e)
1959-
tw.writeExprs_longliteral(id, type.javaResult.id, parent, idx)
1960-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1961-
extractExprContext(id, locId, enclosingCallable, enclosingStmt)
1962-
tw.writeNamestrings(text, i.toString(), id)
1963-
}
2044+
return extractConstant(
2045+
tw::writeExprs_longliteral,
2046+
t,
2047+
text,
2048+
i.toString(),
2049+
tw.getLocation(e),
2050+
parent,
2051+
idx,
2052+
enclosingCallable,
2053+
enclosingStmt
2054+
)
19642055
}
19652056

19662057
else -> {
@@ -1973,68 +2064,6 @@ private fun KotlinFileExtractor.extractConstant(
19732064
TODO()
19742065
}
19752066
}
1976-
1977-
// TODO: Wrong
1978-
return TODO()
1979-
/*
1980-
OLD: KE1
1981-
val v = e.value
1982-
return when {
1983-
v is Float -> {
1984-
exprIdOrFresh<DbFloatingpointliteral>(overrideId).also { id ->
1985-
val type = useType(e.type)
1986-
val locId = tw.getLocation(e)
1987-
tw.writeExprs_floatingpointliteral(id, type.javaResult.id, parent, idx)
1988-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1989-
extractExprContext(id, locId, enclosingCallable, enclosingStmt)
1990-
tw.writeNamestrings(v.toString(), v.toString(), id)
1991-
}
1992-
}
1993-
v is Double -> {
1994-
exprIdOrFresh<DbDoubleliteral>(overrideId).also { id ->
1995-
val type = useType(e.type)
1996-
val locId = tw.getLocation(e)
1997-
tw.writeExprs_doubleliteral(id, type.javaResult.id, parent, idx)
1998-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1999-
extractExprContext(id, locId, enclosingCallable, enclosingStmt)
2000-
tw.writeNamestrings(v.toString(), v.toString(), id)
2001-
}
2002-
}
2003-
v is Boolean -> {
2004-
exprIdOrFresh<DbBooleanliteral>(overrideId).also { id ->
2005-
val type = useType(e.type)
2006-
val locId = tw.getLocation(e)
2007-
tw.writeExprs_booleanliteral(id, type.javaResult.id, parent, idx)
2008-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
2009-
extractExprContext(id, locId, enclosingCallable, enclosingStmt)
2010-
tw.writeNamestrings(v.toString(), v.toString(), id)
2011-
}
2012-
}
2013-
v is Char -> {
2014-
exprIdOrFresh<DbCharacterliteral>(overrideId).also { id ->
2015-
val type = useType(e.type)
2016-
val locId = tw.getLocation(e)
2017-
tw.writeExprs_characterliteral(id, type.javaResult.id, parent, idx)
2018-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
2019-
extractExprContext(id, locId, enclosingCallable, enclosingStmt)
2020-
tw.writeNamestrings(v.toString(), v.toString(), id)
2021-
}
2022-
}
2023-
v is String -> {
2024-
exprIdOrFresh<DbStringliteral>(overrideId).also { id ->
2025-
val type = useType(e.type)
2026-
val locId = tw.getLocation(e)
2027-
tw.writeExprs_stringliteral(id, type.javaResult.id, parent, idx)
2028-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
2029-
extractExprContext(id, locId, enclosingCallable, enclosingStmt)
2030-
tw.writeNamestrings(toQuotedLiteral(v.toString()), v.toString(), id)
2031-
}
2032-
}
2033-
else -> {
2034-
null.also { logger.errorElement("Unrecognised IrConst: " + v.javaClass, e) }
2035-
}
2036-
}
2037-
*/
20382067
}
20392068

20402069
/*

0 commit comments

Comments
 (0)