Kotlin: Add support for varargs

This commit is contained in:
Ian Lynagh
2021-11-09 13:56:50 +00:00
parent 497263e92d
commit be75d30ee0
8 changed files with 85 additions and 0 deletions

View File

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

View File

@@ -2267,3 +2267,26 @@ class StringTemplateExpr extends Expr, @stringtemplateexpr {
override string getAPrimaryQlClass() { result = "StringTemplateExpr" }
}
/**
* A Kotlin(TODO: Should Java make these too?) vararg expression.
* This is the argument to a function that corresponds to a `vararg`
* parameter.
*/
class VarArgExpr extends Expr, @varargexpr {
/**
* Gets the `i`th component of this vararg. TODO: Is this always Expr?
*
* 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 = "VarArgExpr" }
override string getAPrimaryQlClass() { result = "VarArgExpr" }
}

View File

@@ -0,0 +1,7 @@
| test.kt:13:5:13:34 | funWithOnlyVarArgs(...) | 0 | test.kt:13:24:13:33 | ... |
| test.kt:14:5:14:50 | funWithArgsAndVarArgs(...) | 0 | test.kt:14:28:14:30 | foo |
| test.kt:14:5:14:50 | funWithArgsAndVarArgs(...) | 1 | test.kt:14:34:14:37 | true |
| test.kt:14:5:14:50 | funWithArgsAndVarArgs(...) | 2 | test.kt:14:40:14:49 | ... |
| test.kt:15:5:15:53 | funWithMiddleVarArgs(...) | 0 | test.kt:15:27:15:29 | foo |
| test.kt:15:5:15:53 | funWithMiddleVarArgs(...) | 1 | test.kt:15:33:15:42 | ... |
| test.kt:15:5:15:53 | funWithMiddleVarArgs(...) | 2 | test.kt:15:49:15:52 | true |

View File

@@ -0,0 +1,4 @@
import java
from Call c, int i
select c, i, c.getArgument(i)

View File

@@ -0,0 +1,17 @@
fun funWithOnlyVarArgs(vararg xs: Int) {
}
fun funWithArgsAndVarArgs(x: String, y: Boolean, vararg xs: Int) {
}
fun funWithMiddleVarArgs(x: String, vararg xs: Int, y: Boolean) {
}
fun myFun() {
// TODO val xs = listOf(10, 11, 12)
funWithOnlyVarArgs(20, 21, 22)
funWithArgsAndVarArgs("foo", true, 30, 31, 32)
funWithMiddleVarArgs("foo", 41, 42, 43, y = true)
}

View File

@@ -0,0 +1,9 @@
| test.kt:13:24:13:33 | ... | 0 | test.kt:13:24:13:25 | 20 |
| test.kt:13:24:13:33 | ... | 1 | test.kt:13:28:13:29 | 21 |
| test.kt:13:24:13:33 | ... | 2 | test.kt:13:32:13:33 | 22 |
| test.kt:14:40:14:49 | ... | 0 | test.kt:14:40:14:41 | 30 |
| test.kt:14:40:14:49 | ... | 1 | test.kt:14:44:14:45 | 31 |
| test.kt:14:40:14:49 | ... | 2 | test.kt:14:48:14:49 | 32 |
| test.kt:15:33:15:42 | ... | 0 | test.kt:15:33:15:34 | 41 |
| test.kt:15:33:15:42 | ... | 1 | test.kt:15:37:15:38 | 42 |
| test.kt:15:33:15:42 | ... | 2 | test.kt:15:41:15:42 | 43 |

View File

@@ -0,0 +1,4 @@
import java
from VarArgExpr vae, int i
select vae, i, vae.getComponent(i)