Apply interop lowering to new declarations it produces

#KT-36182 Fixed
This commit is contained in:
Svyatoslav Scherbina
2020-01-27 19:47:30 +03:00
committed by SvyatoslavScherbina
parent b46200bc74
commit 2cf079abed
3 changed files with 33 additions and 2 deletions
@@ -815,7 +815,17 @@ private class InteropLoweringPart2(val context: Context) : FileLoweringPass {
val transformer = InteropTransformer(context, irFile)
irFile.transformChildrenVoid(transformer)
irFile.addChildren(transformer.newTopLevelDeclarations)
while (transformer.newTopLevelDeclarations.isNotEmpty()) {
val newTopLevelDeclarations = transformer.newTopLevelDeclarations.toList()
transformer.newTopLevelDeclarations.clear()
// Assuming these declarations contain only new IR (i.e. existing lowered IR has not been moved there).
// TODO: make this more reliable.
val loweredNewTopLevelDeclarations =
newTopLevelDeclarations.map { it.transform(transformer, null) as IrDeclaration }
irFile.addChildren(loweredNewTopLevelDeclarations)
}
}
}
@@ -7,13 +7,22 @@ import kotlin.test.*
import kotlinx.cinterop.*
import ccallbacksAndVarargs.*
fun main(args: Array<String>) {
fun main() {
testStructCallbacks()
testVarargs()
testCallableReferences()
}
fun testStructCallbacks() {
assertEquals(42, getX(staticCFunction { -> cValue<S> { x = 42 } }))
applyCallback(cValue { x = 17 }, staticCFunction { it: CValue<S> ->
assertEquals(17, it.useContents { x })
})
assertEquals(66, makeS(66, 111).useContents { x })
}
fun testVarargs() {
assertEquals(E.ONE, makeE(1))
getVarargs(
@@ -51,3 +60,11 @@ fun main(args: Array<String>) {
assertEquals(null, a15)
}
}
fun testCallableReferences() {
val sumRef = ::sum
assertEquals(3, sumRef(1, 2))
val sumPtr = staticCFunction(::sum)
assertEquals(7, sumPtr(3, 4))
}
@@ -72,3 +72,7 @@ static struct Args getVarargs(int ignore, ...) {
return result;
}
static int sum(int first, int second) {
return first + second;
}