Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14a46b0

Browse files
committed
Kotlin: Variable accesses
1 parent 4c8ff16 commit 14a46b0

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ import kotlin.system.exitProcess
88
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
99
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
1010
import org.jetbrains.kotlin.ir.IrElement
11-
import org.jetbrains.kotlin.ir.declarations.path
12-
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
13-
import org.jetbrains.kotlin.ir.declarations.IrClass
14-
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
15-
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
16-
import org.jetbrains.kotlin.ir.declarations.IrFile
17-
import org.jetbrains.kotlin.ir.declarations.IrFunction
18-
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
19-
import org.jetbrains.kotlin.ir.declarations.IrProperty
20-
import org.jetbrains.kotlin.ir.declarations.IrVariable
11+
import org.jetbrains.kotlin.ir.declarations.*
2112
import org.jetbrains.kotlin.ir.util.dump
2213
import org.jetbrains.kotlin.ir.util.IdSignature
2314
import org.jetbrains.kotlin.ir.util.packageFqName
@@ -237,10 +228,11 @@ class KotlinFileExtractor(val tw: TrapWriter) {
237228
}
238229
}
239230

240-
fun useDeclarationParent(dp: IrDeclarationParent): Label<out DbPackage_or_reftype> {
231+
fun useDeclarationParent(dp: IrDeclarationParent): Label<out DbElement> {
241232
when(dp) {
242233
is IrFile -> return usePackage(dp.fqName.asString())
243234
is IrClass -> return useClass(dp)
235+
is IrFunction -> return useFunction(dp)
244236
else -> {
245237
extractorBug("Unrecognised IrDeclarationParent: " + dp.javaClass)
246238
return Label(0)
@@ -257,9 +249,17 @@ class KotlinFileExtractor(val tw: TrapWriter) {
257249
return id
258250
}
259251

260-
fun extractValueParameter(vp: IrValueParameter, parent: Label<out DbMethod>, idx: Int) {
261-
val label = "@\"params;{$parent};$idx\""
252+
fun useValueParameter(vp: IrValueParameter): Label<out DbParam> {
253+
@Suppress("UNCHECKED_CAST")
254+
val parentId: Label<out DbMethod> = useDeclarationParent(vp.parent) as Label<out DbMethod>
255+
val idx = vp.index
256+
val label = "@\"params;{$parentId};$idx\""
262257
val id = tw.getLabelFor<DbParam>(label)
258+
return id
259+
}
260+
261+
fun extractValueParameter(vp: IrValueParameter, parent: Label<out DbMethod>, idx: Int) {
262+
val id = useValueParameter(vp)
263263
val typeId = useType(vp.type)
264264
val locId = tw.getLocation(vp.startOffset, vp.endOffset)
265265
tw.writeParams(id, typeId, idx, parent, id)
@@ -349,6 +349,18 @@ class KotlinFileExtractor(val tw: TrapWriter) {
349349
}
350350
}
351351

352+
fun useValueDeclaration(d: IrValueDeclaration): Label<out DbVariable> {
353+
when(d) {
354+
is IrValueParameter -> {
355+
return useValueParameter(d)
356+
}
357+
else -> {
358+
extractorBug("Unrecognised IrValueDeclaration: " + d.javaClass)
359+
return Label(0)
360+
}
361+
}
362+
}
363+
352364
fun extractExpression(e: IrExpression, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int) {
353365
when(e) {
354366
is IrCall -> {
@@ -389,6 +401,16 @@ class KotlinFileExtractor(val tw: TrapWriter) {
389401
tw.writeHasLocation(id, locId)
390402
tw.writeNamestrings(v.toString(), v.toString(), id)
391403
}
404+
is IrGetValue -> {
405+
val id = tw.getFreshIdLabel<DbVaraccess>()
406+
val typeId = useType(e.type)
407+
val locId = tw.getLocation(e.startOffset, e.endOffset)
408+
tw.writeExprs_varaccess(id, typeId, parent, idx)
409+
tw.writeHasLocation(id, locId)
410+
411+
val vId = useValueDeclaration(e.symbol.owner)
412+
tw.writeVariableBinding(id, vId)
413+
}
392414
is IrReturn -> {
393415
val id = tw.getFreshIdLabel<DbReturnstmt>()
394416
val locId = tw.getLocation(e.startOffset, e.endOffset)

java/ql/test/kotlin/library-tests/exprs/exprs.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
| exprs.kt:3:13:3:13 | x |
12
| exprs.kt:3:13:3:17 | ... + ... |
3+
| exprs.kt:3:17:3:17 | y |
24
| exprs.kt:4:12:4:14 | 123 |
35
| exprs.kt:4:12:4:20 | ... + ... |
46
| exprs.kt:4:18:4:20 | 456 |

0 commit comments

Comments
 (0)