Character literals and string templates.
This commit is contained in:
committed by
Dmitry Petrov
parent
f83237498f
commit
86a52e6426
+15
-12
@@ -172,6 +172,8 @@ class StatementGenerator(
|
||||
IrConstImpl.double(expression.startOffset, expression.endOffset, constantType, constantValue.value)
|
||||
is FloatValue ->
|
||||
IrConstImpl.float(expression.startOffset, expression.endOffset, constantType, constantValue.value)
|
||||
is CharValue ->
|
||||
IrConstImpl.char(expression.startOffset, expression.endOffset, constantType, constantValue.value)
|
||||
else ->
|
||||
TODO("handle other literal types: ${constantValue.type}")
|
||||
}
|
||||
@@ -179,28 +181,29 @@ class StatementGenerator(
|
||||
|
||||
override fun visitStringTemplateExpression(expression: KtStringTemplateExpression, data: Nothing?): IrStatement {
|
||||
val entries = expression.entries
|
||||
when {
|
||||
entries.size == 1 -> {
|
||||
val entry0 = entries[0]
|
||||
if (entry0 is KtLiteralStringTemplateEntry) {
|
||||
return entry0.genExpr()
|
||||
}
|
||||
}
|
||||
entries.size == 0 -> {
|
||||
return IrConstImpl.string(expression.startOffset, expression.endOffset,
|
||||
getInferredTypeWithImplicitCastsOrFail(expression), "")
|
||||
}
|
||||
when (entries.size) {
|
||||
1 -> return entries[0].genExpr()
|
||||
0 -> return IrConstImpl.string(expression.startOffset, expression.endOffset,
|
||||
getInferredTypeWithImplicitCastsOrFail(expression), "")
|
||||
}
|
||||
|
||||
val irStringTemplate = IrStringConcatenationImpl(expression.startOffset, expression.endOffset,
|
||||
getInferredTypeWithImplicitCastsOrFail(expression))
|
||||
entries.forEach { it.expression!!.let { irStringTemplate.addArgument(it.genExpr()) } }
|
||||
entries.forEach {
|
||||
irStringTemplate.addArgument(it.genExpr())
|
||||
}
|
||||
return irStringTemplate
|
||||
}
|
||||
|
||||
override fun visitLiteralStringTemplateEntry(entry: KtLiteralStringTemplateEntry, data: Nothing?): IrStatement =
|
||||
IrConstImpl.string(entry.startOffset, entry.endOffset, context.builtIns.stringType, entry.text)
|
||||
|
||||
override fun visitEscapeStringTemplateEntry(entry: KtEscapeStringTemplateEntry, data: Nothing?): IrStatement =
|
||||
IrConstImpl.string(entry.startOffset, entry.endOffset, context.builtIns.stringType, entry.unescapedValue)
|
||||
|
||||
override fun visitStringTemplateEntryWithExpression(entry: KtStringTemplateEntryWithExpression, data: Nothing?): IrStatement =
|
||||
entry.expression!!.genExpr()
|
||||
|
||||
override fun visitSimpleNameExpression(expression: KtSimpleNameExpression, data: Nothing?): IrExpression {
|
||||
val resolvedCall = getResolvedCall(expression) ?: throw AssertionError("No resolved call for ${expression.text}")
|
||||
|
||||
|
||||
@@ -19,22 +19,21 @@ package org.jetbrains.kotlin.ir.expressions
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
interface IrConst<out T> : IrExpression, IrExpressionWithCopy {
|
||||
interface IrConst<T> : IrExpression, IrExpressionWithCopy {
|
||||
val kind: IrConstKind<T>
|
||||
val value: T
|
||||
|
||||
override fun copy(): IrConst<T>
|
||||
}
|
||||
|
||||
sealed class IrConstKind<out T>(val asString: kotlin.String) {
|
||||
sealed class IrConstKind<T>(val asString: kotlin.String) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun valueOf(aConst: IrConst<*>) =
|
||||
(aConst as IrConst<T>).value
|
||||
|
||||
object Null : IrConstKind<Nothing?>("Null")
|
||||
object Boolean : IrConstKind<kotlin.Boolean>("Boolean")
|
||||
object Byte : IrConstKind<kotlin.Byte>("Byte")
|
||||
object Short : IrConstKind<kotlin.Short>("Short")
|
||||
object Char : IrConstKind<kotlin.Char>("Char")
|
||||
object Int : IrConstKind<kotlin.Int>("Int")
|
||||
object Long : IrConstKind<kotlin.Long>("Long")
|
||||
object String : IrConstKind<kotlin.String>("String")
|
||||
@@ -44,7 +43,7 @@ sealed class IrConstKind<out T>(val asString: kotlin.String) {
|
||||
override fun toString() = asString
|
||||
}
|
||||
|
||||
class IrConstImpl<out T> (
|
||||
class IrConstImpl<T> (
|
||||
startOffset: Int,
|
||||
endOffset: Int,
|
||||
type: KotlinType,
|
||||
@@ -84,6 +83,9 @@ class IrConstImpl<out T> (
|
||||
|
||||
fun double(startOffset: Int, endOffset: Int, type: KotlinType, value: Double): IrExpression =
|
||||
IrConstImpl(startOffset, endOffset, type, IrConstKind.Double, value)
|
||||
|
||||
fun char(startOffset: Int, endOffset: Int, type: KotlinType, value: Char): IrExpression =
|
||||
IrConstImpl(startOffset, endOffset, type, IrConstKind.Char, value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -9,4 +9,5 @@ val test8 = -1L
|
||||
val test9 = 1.0
|
||||
val test10 = -1.0
|
||||
val test11 = 1.0f
|
||||
val test12 = -1.0f
|
||||
val test12 = -1.0f
|
||||
val test13 = 'a'
|
||||
@@ -35,3 +35,6 @@ FILE /literals.kt
|
||||
PROPERTY public val test12: kotlin.Float = -1.0.toFloat() getter=null setter=null
|
||||
EXPRESSION_BODY
|
||||
CONST Float type=kotlin.Float value='-1.0'
|
||||
PROPERTY public val test13: kotlin.Char = \u0061 ('a') getter=null setter=null
|
||||
EXPRESSION_BODY
|
||||
CONST Char type=kotlin.Char value='a'
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
fun foo(): String = ""
|
||||
|
||||
val test1 = ""
|
||||
val test2 = "abc"
|
||||
val test3 = """"""
|
||||
val test4 = """abc"""
|
||||
val test5 = """
|
||||
abc
|
||||
"""
|
||||
val test6 = "$test1 ${foo()}"
|
||||
@@ -0,0 +1,33 @@
|
||||
FILE /stringTemplates.kt
|
||||
FUN public fun foo(): kotlin.String
|
||||
BLOCK_BODY
|
||||
RETURN type=kotlin.Nothing from=foo
|
||||
CONST String type=kotlin.String value=''
|
||||
PROPERTY public val test1: kotlin.String = "" getter=null setter=null
|
||||
EXPRESSION_BODY
|
||||
CONST String type=kotlin.String value=''
|
||||
PROPERTY public val test2: kotlin.String = "abc" getter=null setter=null
|
||||
EXPRESSION_BODY
|
||||
CONST String type=kotlin.String value='abc'
|
||||
PROPERTY public val test3: kotlin.String = "" getter=null setter=null
|
||||
EXPRESSION_BODY
|
||||
CONST String type=kotlin.String value=''
|
||||
PROPERTY public val test4: kotlin.String = "abc" getter=null setter=null
|
||||
EXPRESSION_BODY
|
||||
CONST String type=kotlin.String value='abc'
|
||||
PROPERTY public val test5: kotlin.String = "
|
||||
abc
|
||||
" getter=null setter=null
|
||||
EXPRESSION_BODY
|
||||
STRING_CONCATENATION type=kotlin.String
|
||||
CONST String type=kotlin.String value='
|
||||
'
|
||||
CONST String type=kotlin.String value='abc'
|
||||
CONST String type=kotlin.String value='
|
||||
'
|
||||
PROPERTY public val test6: kotlin.String getter=null setter=null
|
||||
EXPRESSION_BODY
|
||||
STRING_CONCATENATION type=kotlin.String
|
||||
CALL .<get-test1> type=kotlin.String operator=GET_PROPERTY
|
||||
CONST String type=kotlin.String value=' '
|
||||
CALL .foo type=kotlin.String operator=null
|
||||
@@ -382,6 +382,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("stringTemplates.kt")
|
||||
public void testStringTemplates() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/stringTemplates.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("throw.kt")
|
||||
public void testThrow() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/throw.kt");
|
||||
|
||||
Reference in New Issue
Block a user