Hello everyone, I have a question about the priori...
# compiler
a
Hello everyone, I have a question about the priority when executing passes such as
visitClassNew
, is it possible to get control of the latter? I understand that the code logic itself should be written so that it is not important, but what if I want to change the name of a function that was generated by another plugin?
t
I think
visitClassNew
is probably quite deep in the plugin code, what you have to consider is the execution order of the plugin extensions. Which I don't know to be honest, but I guess it is the order the plugins are passed to the compiler.
👍 1
a
I encountered another problem when changing the function name using this code
Copy code
override fun visitFunctionNew(declaration: IrFunction): IrStatement {

  if (declaration.name.asString() == "test") {
    declaration.name = Name.identifier("test_fix")
  }
  return super.visitFunctionNew(declaration)
}
It works great for the JVM implementation and passes the tests I get something like
Copy code
@NotNull
    public final String test_fix() {
        return "test";
    }
When using already in Kotlin native, I get an error that says that my Descriptor and Ir contradict each other
Copy code
Compilation failed: FULL: FUN name:fix_test visibility:public modality:FINAL <> () returnType:kotlin.Unit
 Ir: kotlin.native#fix_test(){}
 Descriptor: kotlin.native#test(){}
I'm still looking at the open source code on GitHub, but mostly they're changing the
var
variables described in the
abstract class IrFunction
While the descriptor is marked as
val
, I couldn't find any builders to help change this
t
Changing the function name is something I've never tried, as it might have long-reaching effects. The problem is not just the compilation but also the metadata which won't contain this change. If you are building a library the code that imports that library will expect the original function, not the renamed one. Would it be a bad solution to create another function with the different name and simply call the original one?
a
I plan to use this for personal use without publishing it. Most likely it is easier to delete the function and create a new one with a different name, in any case, the number of abstractions that need to be used is amazing.I don’t really understand whether I will need to change calls to this function in all other places. It’s just that if this is so, then it’s easier to write an editor directly for llvm
t
The calling from other places is why I suggested not to delete it. The abstractions become easier as you gain experience with the compiler. Why would like to change the name? Maybe there is an easier solution.
a
I'm trying to create a small obfuscator
t
Hm, I know no easier solution then. Technically you can create a visitor that modifies all calls, don't know if it worth the trouble tho.
a
If I use a debugger and try to check the operating logic for the jv,, I see that when the name is changed, the descriptor also changes. Any ideas why this doesn't happen in the native? or is it only for jvm?
t
Honestly, I don't know, so I'm just guessing. The compiler backend is different for the targets. So, it might be possible that the native target works quite different than the JVM one. The compiler version may also matter, iirc native had a different plugin structure, I don't know how that evolved.
a
Most likely my next step will be to stick to the compiler in debug mode.
I checked just in case on a real jvm project, it works
I solved the problem, as usual I lost a lot of time due to my carelessness. • It's bad to use old imports that are not supported by K2 • Use only the latest version of the compiler (everything works fine on 2.0.0-RC2) To change the name of a function, just write the following code; the compiler will change all other calls on its own.
Copy code
override fun visitSimpleFunction(declaration: IrSimpleFunction): IrStatement {

    if (declaration.name.asString() == "test") {
      declaration.name = Name.identifier("test_fix")
    }
    return super.visitSimpleFunction(declaration)
 }
👍 1