A few questions about IR generation: 1. How do I a...
# compiler
m
A few questions about IR generation: 1. How do I add properties to package-level? 2. How do I use generic types for types? 3. How can I use kotlin/native cinterop methods with FqName, because it seems to complain that the types/names do not exist. 4. How do I use type aliases as types?
s
To add properties on package -level you can add properties to the file For generic types, you'll have to create a type parameter for method and use that as a type. When you call said method, there are type arguments that you can pass to a call, similar to regular value parameters
m
Do you have a code snippet example for these?
For 1. I got this far:
Copy code
context(IrPluginContext, KPyPackage)
    private fun generatePyMethod(method: KPyMethod) {
        // TODO: val `${name}-kpy-method` = staticCFunction { ... }
        // TODO: val `${name}-kpy-def` = `${name}-kpy-method`.pydef(...)
        irFactory.buildProperty {
            this.name = Name.identifier(method.name + "-kpy-method")
            this.modality = Modality.FINAL
            this.visibility = DescriptorVisibilities.INTERNAL
        }.also { prop ->
            prop.backingField = irFactory.buildField {
                this.name = Name.identifier(method.name + "-kpy-method")
                this.type = classFuncType.owner.expandedType
            }.also { field ->
                TODO("Set initializer")
            }

            TODO("addDefaultGetter needs a parent class?")
            // prop.addDefaultGetter(???, irBuiltIns)
        }
    }
For 2/4:
Copy code
val classPyObjectT = context.referenceTypeAlias(FqName("kpy.wrappers.PyObjectT"))!!.owner.expandedType
    val classFuncType = context.referenceTypeAlias(FqName("kpy.wrappers.FuncType"))!!  // How do I add a generic type parameter to this somehow? owner.expandedType does not have a withType function or anything
    val classMethodT = context.referenceTypeAlias(FqName("kpy.wrappers.PyMethodT"))!!.owner.expandedType
    val classMethodKwargsT = context.referenceTypeAlias(FqName("kpy.wrappers.PyMethodKwargsT"))!!.owner.expandedType
For 3 I'm clueless sadly, might be due to the way I'm testing (a test in jvm source, since I have no clue how to make a sibling project test a gradle plugin)
s
Yeah , I don't have any experience with cinterop as well, unfortunately Most likely you'll have to link it in some descriptor form with SyntheticResolve extension so compiler knows about them when creating IR
Btw, would KSP (generating sources) work for your case instead? Looking at the snippets you attached, I am not sure you want a compiler plugin here
m
Do you have any links to documentation on KSP?
Specifically, on how to use it in a gradle plugin as annotation processor