PHondogo
09/25/2020, 6:48 AMinline fun<reified T> test() {throw AssertionError("Implementing by compiler plugin")}
In IR generator I want to replace all calls to this function by some expression (using T type literal).
For calls with direct literals (for example test<Int>()
) it works as expected.
But when calling from templated function with template parameter it appears to substitute just type parameter.
For example inline fun<reified T> test2(){test<T>()}
Is it possible in IR generation phase to transform all actual inlined types?Manuel Wrage
09/25/2020, 8:47 AMPublic api stub
inline fun <@CustomReified T> test(): KClass<T> = error("Stub")
// user code
fun test2() {
val stringClass = test<String>()
}
// transformed code
fun test2() {
// type is known -> transform in place
val stringClass = String::class
}
// user code
inline fun <@CustomReified T> test2() {
val tClass = test<T>()
}
// transformed code
inline fun <@CustomReified T> test2(provideTypeT: () -> KClass<T>) {
// type is unknown -> let the caller pass the result
val tClass = provideTypeT()
}
// user code
fun test3() {
test2<String>()
}
// transformed code
fun test3() {
// pass the known type
test2<String> { String::class }
}
Instead of reified
I added a custom annotation for these kind of type parameters.
Then everytime the special function get's called I check if the type is fully known
If yes -> do the transformation
If not -> add a lambda parameter to the function which propagates the responsibility to the caller.
Feel free to ask more if you have any questions.shikasd
09/25/2020, 11:20 AMKClass?
instead of lambda, I think, but general approach that Manuel suggested is probably the best.
You can then substitute type parameters with the known type at the outer call and propagate it to the places where it is unknown otherwise.Manuel Wrage
09/25/2020, 1:56 PMPHondogo
09/25/2020, 2:16 PMHopefully this will change in the future.
where did you get hope?Manuel Wrage
09/25/2020, 2:38 PMManuel Wrage
09/25/2020, 2:44 PMIrInlineFunctionCall
expression which contains information about the inline function call + the inlined function body.shikasd
09/25/2020, 4:03 PM