hey, i'm playing around with a k2 compiler plugin to auto-magically integrate annotated suspend func...
t
hey, i'm playing around with a k2 compiler plugin to auto-magically integrate annotated suspend functions with a framework that doesn't understand them and doesn't know how to wrap them. i've got through the "find the functions i want to re-write" part and the "replace it with a dummy version" part. right now i'm trying to generate a function that ends up being something like
Copy code
@Annotation
fun myFunction(...) {
  CoroutineScope(Dispatchers.Default).async {
    // insert original function statements here
  }
}
but the plugin context can't find either the
CoroutineScope(CoroutineContext)
factory function or the
async
extension function (though it's not particularly clear how to find extension functions since there's no docs). when i try to have the symbol provider look up top level functions for the coroutines package, it finds others, but not those (and not
launch
either). what's the correct way to go about finding those function symbols so i can have my plugin invoke them? this is what i'm currently doing:
Copy code
pluginContext.referenceFunctions(CallableId(
  packageName = StandardNames.COROUTINES_PACKAGE_FQ_NAME,
  callableName = Name.identifier("CoroutineScope")
))
e
I don't know about
async
, but you might as well use
GlobalScope
instead of
CoroutineScope(Dispatchers.Default)
, as that's what you're effectively doing already
d
referenceFunctions
should work, if the
CoroutineScope
is present in the compile classpath Can you reference it just from the code?
BTW there is a #C7L3JB43G channel, which is dedicated for compiler internals and compiler plugins
t
wasn't sure if this was better as a k2 question or a compiler question, but since it's a k2 plugin, i decided on here.
both the project being compiled (my test app) and the compiler plugin have access to the function - the interface
CoroutineScope
and other functions defined in the same file are available with
referenceFunctions
or
referenceClasses
- it's only having problems with specific functions it seems
d
Ah, wait a minute
StandardNames.COROUTINES_PACKAGE_FQ_NAME
is a
kotlin.coroutines
And the function you are looking for in
kotlinx.coroutines
t
really? why would the compiler have the wrong package as a "standard name"?
d
It's not wrong There are some basic coroutine stuff in the standard library under the
kotlin.coroutines
package, and
StandardNames.COROUTINES_PACKAGE_FQ_NAME
is used to refer to them
kotlinx.corountines
is just a library (with a different base package) which provides all its capabilities based on this minimal entities declared in the stdlib
And the compiler doesn't know anything about the
kotlinx.coroutines
library
t
it makes sense that the compiler doesn't know about the library, but it's confusing that there's a standard name for the package when the bulk of the functionality for that feature is in a library and has a different package. i'll try things out with the package set correctly and let you know
d
Everything from the
StandardNames
and
StandardClassIds
is about kotlin stdlib and java stdlib
t
good to know - is that by chance documented somewhere? i wasn't able to find any documentation on backend compiler plugin stuff, which made it a lot of trial and error
yep - good call. using
kotlinx.coroutines
instead of the standard name worked
i didn't even look at the value for that constant, i just trusted that the compiler team had the right value and was being nice to people developing compiler plugins
it also fixed the missing
async
and
launch
functions.
thank you for your help @dmitriy.novozhilov
👌 1
d
is that by chance documented somewhere?
This definitely isn't There are some docs, but not much Also there are some readmes in compiler modules
👍 1