Kotlin: Add support for string templates

This commit is contained in:
Ian Lynagh
2021-11-01 16:34:26 +00:00
parent 168786ae71
commit 976cc31c7a
3 changed files with 33 additions and 0 deletions

View File

@@ -1419,6 +1419,17 @@ class X {
val exprParent = parent.expr(e, callable) val exprParent = parent.expr(e, callable)
extractCall(e, callable, exprParent.parent, exprParent.idx) extractCall(e, callable, exprParent.parent, exprParent.idx)
} }
is IrStringConcatenation -> {
val exprParent = parent.expr(e, callable)
val id = tw.getFreshIdLabel<DbStringtemplateexpr>()
val type = useType(e.type)
val locId = tw.getLocation(e)
tw.writeExprs_stringtemplateexpr(id, type.javaResult.id, type.kotlinResult.id, exprParent.parent, exprParent.idx)
tw.writeHasLocation(id, locId)
e.arguments.forEachIndexed { i, a ->
extractExpressionExpr(a, callable, id, i)
}
}
is IrConst<*> -> { is IrConst<*> -> {
val exprParent = parent.expr(e, callable) val exprParent = parent.expr(e, callable)
val v = e.value val v = e.value

View File

@@ -670,6 +670,7 @@ case @expr.kind of
| 77 = @safecastexpr | 77 = @safecastexpr
| 78 = @notinstanceofexpr | 78 = @notinstanceofexpr
| 79 = @stmtexpr | 79 = @stmtexpr
| 80 = @stringtemplateexpr
; ;
/** Holds if this `when` expression was written as an `if` expression. */ /** Holds if this `when` expression was written as an `if` expression. */

View File

@@ -2246,3 +2246,24 @@ class StmtExpr extends Expr, @stmtexpr {
override string getAPrimaryQlClass() { result = "StmtExpr" } override string getAPrimaryQlClass() { result = "StmtExpr" }
} }
/**
* A Kotlin string template expression. For example, `"foo${bar}baz"`.
*/
class StringTemplateExpr extends Expr, @stringtemplateexpr {
/**
* Gets the `i`th component of this string template.
*
* For example, in the string template `"foo${bar}baz"`, the 0th
* component is the string literal `"foo"`, the 1st component is
* the variable access `bar`, and the 2nd component is the string
* literal `"bar"`.
*/
Expr getComponent(int i) { result.isNthChildOf(this, i) }
override string toString() { result = "\"...\"" }
override string getHalsteadID() { result = "StringTemplateExpr" }
override string getAPrimaryQlClass() { result = "StringTemplateExpr" }
}