Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Crash Course on the Kotlin Compiler - KotlinCon...

mvndy_hd
April 14, 2023

Crash Course on the Kotlin Compiler - KotlinConf 2023

Whether you’re learning about writing compiler plugins, learning about the data structures/algorithms in real-life scenarios, or maybe just wondering why that little red squiggly shows up in your IntelliJ IDE, learning about the Kotlin compiler is your answer to all the above.

Let’s face it - learning about the Kotlin compiler is hard. Luckily, being able to look through various plugins can give insight to how the Kotlin compiler processes data at every phase, from human-readable Kotlin code to machine-readable bytecode. This session intends to expose Kotlin compiler functionality through reverse engineering compiler plugins and all the phases a plugin may intercept, giving us a conceptual, high-level overview what the the Kotlin compiler looks like.

The audience will hitch a ride with the source code fed through the phases of the Kotlin compiler to observe its transformations and optimizations frontend-to-backend: no better way to learn than going in deep!

mvndy_hd

April 14, 2023
Tweet

More Decks by mvndy_hd

Other Decks in Technology

Transcript

  1. Reverse engineering popular plugins Gear kit Kotlin compiler map Exchanging

    research notes Reverse engineering popular plugins @mvndy_hd @[email protected]
  2. Backend PSI BindingContext Frontend .kt *.class *.js *.so K1 Psi2Ir

    Program Structure Interface (PSI) - layer in IntelliJ to help parse files and create syntactic/semantic code models FE10- @mvndy_hd @[email protected]
  3. Backend PSI BindingContext Frontend .kt *.class *.js *.so K1 Psi2Ir

    Program Structure Interface (PSI) - layer in IntelliJ to help parsing files and creating syntactic/semantic code models Binding Context - Big map which contains information of program in pair with PSI FE10- Descriptors hold context, scope, containing info, overrides, companion object etc.
  4. .kt Optimized target code Tokens *.class *.js *.so src fun

    two() = 2 K1 Frontend: Lexical Analysis
  5. .kt Optimized target code Tokens *.class *.js *.so src fun

    K1 Frontend: Lexical Analysis KtModifierKeywordToken.FUN_KEYWORD KtToken.Identifier KtSingleValueToken.LPAR KtSingleValueToken.RPAR KtToken.Identifier two ( ) = 2 KtSingleValueToken.EQ
  6. .kt AST/PSI Optimized target code Tokens *.class *.js *.so src

    K1 Frontend: Parsing Abstract Syntax Tree (AST) Made of nodes that have direct mapping to the text ranges in src fun two() = 2
  7. .kt AST/PSI Optimized target code Tokens *.class *.js *.so src

    K1 Frontend: Syntax Analysis Element.FUN Token.fun Element.VALUE_ARGUMENT_LIST Element.IDENTIFER Token.LPAR Token.RPAR Token.EQ Element.IDENTIFER_CONSTANT Token.INTEGER_LITERAL fun two() = 2
  8. .kt AST/PSI Optimized target code Tokens *.class *.js *.so src

    K1 Frontend: Syntax Analysis FUN PsiElement(fun) PsiWhiteSpace PsiElement(IDENTIFIER) VALUE_PARAMETER_LIST PsiElement(LPAR) PsiElement(RPAR) PsiWhiteSpace PsiElement(EQ) PsiWhiteSpace INTEGER_CONSTANT PsiElement(INTEGER_LITERAL) fun two() = 2 PSIViewer
  9. .kt AST/PSI Optimized target code Tokens *.class *.js *.so src

    K1 Frontend: Resolution fun two() = 2 FUN PsiElement(fun) PsiWhiteSpace PsiElement(IDENTIFIER) VALUE_PARAMETER_LIST PsiElement(LPAR) PsiElement(RPAR) PsiWhiteSpace PsiElement(EQ) PsiWhiteSpace INTEGER_CONSTANT PsiElement(INTEGER_LITERAL) BindingContext Descriptors
  10. Backend FIR Frontend .kt *.class *.js *.so K2 Fir2Ir Frontend

    Intermediate Representation (FIR) Fir- @mvndy_hd @[email protected]
  11. .kt AST/PSI Optimized target code Tokens *.class *.js *.so src

    K2 Frontend: Psi2Fir private val kotlinFiles = mutableListOf(1, 2, 3)
  12. .kt AST/PSI Optimized target code Tokens *.class *.js *.so src

    K2 Frontend: Parsing PROPERTY MODIFIER_LIST PsiElement(private) PsiElement(val) PsiElement(IDENTIFIER) PsiElement(EQ) CALL_EXPRESSION REFERNCE_EXPRESSION PsiElement(IDENTIFIER) VALUE_ARGUMENT INTEGER_CONSTANT PsiElement(INTEGER) PsiElement(COMMA) VALUE_ARGUMENT INTEGER_CONSTANT PsiElement(INTEGER) PsiElement(COMMA) VALUE_ARGUMENT INTEGER_CONSTANT PsiElement(INTEGER) private val kotlinFiles = mutableListOf(1, 2, 3)
  13. .kt AST/PSI Raw FIR Optimized target code Tokens FILE: funAssignment.kt

    public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } *.class *.js *.so src K2 Frontend: Psi2Fir FILE: mutableList.kt private? final? Val kotlinFiles: <implicit> = ^kotlinFiles MutableList( vararg(Int(1), Int(2), Int(3) ) private val kotlinFiles = mutableListOf(1, 2, 3)
  14. .kt AST/PSI Raw FIR FIR Optimized target code Tokens FILE:

    funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit *.class *.js *.so src K2 Frontend: Resolution FILE: mutableList.kt private? final? Val kotlinFiles: <implicit> = ^kotlinFiles MutableList( vararg(Int(1), Int(2), Int(3) ) private val kotlinFiles = mutableListOf(1, 2, 3) Resolve Imports Super types Sealed class inheritors Types Status Contracts Implicit Types Body Resolve
  15. .kt AST/PSI Raw FIR FIR Optimized target code Tokens FILE:

    funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit *.class *.js *.so src K2 Frontend: Resolution private val kotlinFiles = mutableListOf(1, 2, 3) FILE: mutableList.kt lvar listVar: R|kotlin/collections/MutableList<kotlin/Int> | = R|kotlin/collections/mutableListOf |<R|kotlin/Int|>(vararg(Int(1), Int(2), Int(3)))
  16. .kt AST/PSI Raw FIR FIR Optimized target code Tokens FILE:

    funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit *.class *.js *.so src K2 Frontend: Resolution CHECKERS private val kotlinFiles = mutableListOf(1, 2, 3) FILE: mutableList.kt lvar listVar: R|kotlin/collections/MutableList<kotlin/Int> | = R|kotlin/collections/mutableListOf |<R|kotlin/Int|>(vararg(Int(1), Int(2), Int(3)))
  17. .kt AST/PSI Raw FIR FIR Optimized target code Tokens FILE:

    funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit *.class *.js *.so src K2 Frontend: Resolution VALIDATED private val kotlinFiles = mutableListOf(1, 2, 3) FILE: mutableList.kt lvar listVar: R|kotlin/collections/MutableList<kotlin/Int> | = R|kotlin/collections/mutableListOf |<R|kotlin/Int|>(vararg(Int(1), Int(2), Int(3)))
  18. .kt *.class *.js *.so Frontend JVM IR backend Isakova, Sletvana.

    “What Everyone Must Know about the New Kotlin K2 Compiler.” YouTube, JetBrains, 7 Oct. 2021, https://www.youtube.com/watch?v=iTdJJq_LyoY&t=1785s&ab_channel=KotlinbyJetBrains. JS IR backend Native IR backend IR Generator + optimizer JVM bytecode generator + optimizer JavaScript generator + optimizer LLVM bitcode generator + optimizer IR IR IR Syntax tree + semantic info
  19. .kt AST/PSI Raw FIR FIR IR Optimized target code Tokens

    FILE: funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing *.class *.js *.so src JVM Backend: IR Codegen private val kotlinFiles = mutableListOf(1, 2, 3) PROPERTY list visibility:public modality:FINAL [val] FIELD PROPERTY_BACKING_FIELD name:mutableListFun type:kotlin.Function1<kotlin.collections.MutableList<kotlin.Double>, kotlin.collections.MutableList<kotlin.Int>> visibility:private [final,static] EXPRESSION_BODY FUN_EXPR type=kotlin.Function1<kotlin.collections.MutableList<kotlin.Double>, kotlin.collections.MutableList<kotlin.Int>> origin=ANONYMOUS_FUNCTION FUN LOCAL_FUNCTION name:<no name provided> visibility:local modality:FINAL <> (l:kotlin.collections.MutableList<kotlin.Double>) returnType:kotlin.collections.MutableList<kotlin.Int> VALUE_PARAMETER name:l index:0 type:kotlin.collections.MutableList<kotlin.Double> BLOCK_BODY RETURN type=kotlin.Nothing from='local final fun <no name provided> (l: kotlin.collections.MutableList<kotlin.Double>): kotlin.collections.MutableList<kotlin.Int> declared in <root>.mutableListFun' CALL 'public final fun CHECK_NOT_NULL <T0> (arg0: T0 of kotlin.internal.ir.CHECK_NOT_NULL?): {T0 of kotlin.internal.ir.CHECK_NOT_NULL & Any} declared in kotlin.internal.ir' type=kotlin.Nothing origin=EXCLEXCL <T0>: kotlin.Nothing arg0: CONST Null type=kotlin.Nothing? value=null
  20. .kt AST/PSI Raw FIR FIR IR Optimized target code Tokens

    FILE: funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing *.class *.js *.so src JVM Backend: IR Codegen Intermediate Representation (IR) - abstracted representation for CPU- level architecture - IR Analysis produces control flow, call stacks PROPERTY list visibility:public modality:FINAL [val] FIELD PROPERTY_BACKING_FIELD name:mutableListFun type:kotlin.Function1<kotlin.collections.MutableList<kotlin.Double>, kotlin.collections.MutableList<kotlin.Int>> visibility:private [final,static] EXPRESSION_BODY FUN_EXPR type=kotlin.Function1<kotlin.collections.MutableList<kotlin.Double>, kotlin.collections.MutableList<kotlin.Int>> origin=ANONYMOUS_FUNCTION FUN LOCAL_FUNCTION name:<no name provided> visibility:local modality:FINAL <> (l:kotlin.collections.MutableList<kotlin.Double>) returnType:kotlin.collections.MutableList<kotlin.Int> VALUE_PARAMETER name:l index:0 type:kotlin.collections.MutableList<kotlin.Double> BLOCK_BODY RETURN type=kotlin.Nothing from='local final fun <no name provided> (l: kotlin.collections.MutableList<kotlin.Double>): kotlin.collections.MutableList<kotlin.Int> declared in <root>.mutableListFun' CALL 'public final fun CHECK_NOT_NULL <T0> (arg0: T0 of kotlin.internal.ir.CHECK_NOT_NULL?): {T0 of kotlin.internal.ir.CHECK_NOT_NULL & Any} declared in kotlin.internal.ir' type=kotlin.Nothing origin=EXCLEXCL <T0>: kotlin.Nothing arg0: CONST Null type=kotlin.Nothing? value=null
  21. .kt AST/PSI Raw FIR FIR IR Lowered IR Optimized target

    code Tokens FILE: funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing *.class *.js *.so FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing from='public fi nal fun two (): kotlin.Int d eclared in <root>' CONST Int type=kotlin.Int src JVM Backend: IR Lowerings Lowered IR - Improve operations around performance, concurrency - Resource + storage decisions 3^2 3*3
  22. .kt AST/PSI Raw FIR FIR IR Lowered IR Target code

    Optimized target code Tokens FILE: funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing *.class *.js *.so FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing from='public fi nal fun two (): kotlin.Int d eclared in <root>' CONST Int type=kotlin.Int *.class *.js *.so src JVM Backend: Bytecode Codegen private val kotlinFiles = mutableListOf(1, 2, 3) // access flags 0x12 // signature Ljava/util/ArrayList<Ljava/io/File;> // declaration: kotlinFiles extends // java.util.ArrayList<java.io.File> private final Ljava/util/ArrayList; kotlinFiles
  23. @Retention(RetentionPolicy.RUNTIME) public @interface SamWithReceiver1 { } @SamWithReceiver1 public interface Job

    { void execute(String context, int other); } public class Manager { public void doJob(Job job) {} } sam-with-receiver example @mvndy_hd @[email protected]
  24. import lib.Manager fun main(args: Array<String>) { println("Hello World!") val manager

    = Manager() manager.doJob { other -> println("Context: $this | Other: $other") } manager.doJob { context, other -> println("Context: $context | Other: $other") } } } sam-with-receiver not configured SAM interface @mvndy_hd @[email protected]
  25. import lib.Manager fun main(args: Array<String>) { println("Hello World!") val manager

    = Manager() manager.doJob { other -> println("Context: $this | Other: $other") } manager.doJob { context, other -> println("Context: $context | Other: $other") } } } sam-with-receiver not configured Anonymous class @mvndy_hd @[email protected]
  26. import lib.Manager fun main(args: Array<String>) { println("Hello World!") val manager

    = Manager() manager.doJob { other -> println("Context: $this | Other: $other") } manager.doJob { context, other -> println("Context: $context | Other: $other") } } } Required: (String!, Int) -> Unit Found: (String!) -> (Int) sam-with-receiver not configured
  27. import lib.Manager fun main(args: Array<String>) { println("Hello World!") val manager

    = Manager() manager.doJob { other -> println("Context: $this | Other: $other") } manager.doJob { context, other -> println("Context: $context | Other: $other") } } } Required: (String!.(Int) -> Unit)! Found: String!.(Int, Any) -> Unit sam-with-receiver configured
  28. CommandLineProcessor Extension Extension Extension Plugin Subplugin ComponentRegistrar Most, Kevin. “Kotlinconf

    2018 - Writing Your First Kotlin Compiler Plugin by Kevin Most.” YouTube, JetBrains, 12 Oct. 2018, https://www.youtube.com/watch?v=w-GMlaziIyo&ab_channel=JetBrains.
  29. class FirSamWithReceiverExtensionRegistrar( private val annotations: List<String> ) : FirExtensionRegistrar() {

    override fun ExtensionRegistrarContext.configurePlugin() { +::FirSamWithReceiverConventionTransformer.bind(annotations) } }
  30. .kt AST/PSI Raw FIR FIR IR Lowered IR Target code

    Optimized target code Tokens FILE: funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing *.class *.js *.so FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing from='public fi nal fun two (): kotlin.Int d eclared in <root>' CONST Int type=kotlin.Int *.class *.js *.so src class FirSamWithReceiverExtensionRegistrar( private val annotations: List<String> ) : FirExtensionRegistrar() { override fun ExtensionRegistrarContext.configurePlugin() { +::FirSamWithReceiverConventionTransformer.bind(annotations) } } FirExtensionRegistrar() +::FirSamWithReceiverConventionTransformer.bind(annotations)
  31. class FirSamWithReceiverConventionTransformer( private val annotations: List<String>, session: FirSession ) :

    FirSamConversionTransformerExtension(session) { override fun getCustomFunctionalTypeForSamConversion( function: FirSimpleFunction ): ConeLookupTagBasedType? { val containingClassSymbol = function.containingClassLookupTag()? .toFirRegularClassSymbol(session) ?: return null return runIf( containingClassSymbol.resolvedAnnotationClassIds .any { it.asSingleFqName().asString() in annotations } ) { val parameterTypes = function.valueParameters.map { it.returnTypeRef.coneType } if (parameterTypes.isEmpty()) return null createFunctionalType( parameters = parameterTypes.subList(1, parameterTypes.size), receiverType = parameterTypes[0], rawReturnType = function.returnTypeRef.coneType, isSuspend = function.isSuspend ) } } } class FirSamWithReceiverConventionTransformer( ) : FirSamConversionTransformerExtension(session) { override fun getCustomFunctionalTypeForSamConversion( ): ConeLookupTagBasedType? { val containingClassSymbol = function.containingClassLookupTag()? .toFirRegularClassSymbol(session) ?: return null return runIf( containingClassSymbol.resolvedAnnotationClassIds .any { it.asSingleFqName().asString() in annotations } } } Looks for registered SAM interface
  32. class FirSamWithReceiverConventionTransformer( private val annotations: List<String>, session: FirSession ) :

    FirSamConversionTransformerExtension(session) { override fun getCustomFunctionalTypeForSamConversion( function: FirSimpleFunction ): ConeLookupTagBasedType? { val containingClassSymbol = function.containingClassLookupTag()? .toFirRegularClassSymbol(session) ?: return null return runIf( containingClassSymbol.resolvedAnnotationClassIds .any { it.asSingleFqName().asString() in annotations } ) { val parameterTypes = function.valueParameters.map { it.returnTypeRef.coneType } if (parameterTypes.isEmpty()) return null createFunctionalType( parameters = parameterTypes.subList(1, parameterTypes.size), receiverType = parameterTypes[0], rawReturnType = function.returnTypeRef.coneType, isSuspend = function.isSuspend ) } } } class FirSamWithReceiverConventionTransformer( ) : FirSamConversionTransformerExtension(session) { override fun getCustomFunctionalTypeForSamConversion( return runIf( ) { val parameterTypes = function.valueParameters.map { it.returnTypeRef.coneType } if (parameterTypes.isEmpty()) return null createFunctionalType( parameters = parameterTypes.subList(1, parameterTypes.size), receiverType = parameterTypes[0], rawReturnType = function.returnTypeRef.coneType, isSuspend = function.isSuspend ) } } } Changes args so first param is receiver
  33. import lib.Manager fun main(args: Array<String>) { println("Hello World!") val manager

    = Manager() manager.doJob { other -> println("Context: $this | Other: $other") } manager.doJob { context, other -> println("Context: $context | Other: $other") } } } sam-with-receiver configured
  34. Group(5) EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY @Composable fun HelloWorld(greeting:

    String) { Text("Hello, $greeting!") } HelloWorld(“KotlinConf”) @mvndy_hd @[email protected] @Composable fun HelloWorld(greeting: String) { }
  35. Group(5) Group(6) Group(7) Group(8) “Hello KotlinConf!” EMPTY EMPTY @Composable fun

    HelloWorld(greeting: String) { Text("Hello, $greeting!") } HelloWorld(“KotlinConf”) @mvndy_hd @[email protected] Text("Hello, $greeting!")
  36. @Composable fun HelloWorld(greeting: String, %composer: Composer?, %changed: Int) { %composer

    = %composer.startRestartGroup(<>) val %dirty = %changed if (%changed and 0b1110 === 0) { %dirty = %dirty or if (%composer.changed(greeting)) 0b0100 else 0b0010 } if (%dirty and 0b1011 xor 0b0010 !== 0 || !%composer.skipping) { Text("Hello, $greeting!") } else { %composer.skipToGroupEnd() } %composer.endRestartGroup() ?.updateScope { %composer: Composer?, %force: Int -> HelloWorld(greeting, %composer, %changed or 0b0001) } }
  37. @Composable fun HelloWorld(greeting: String, %composer: Composer?, %changed: Int) { %composer

    = %composer.startRestartGroup(<>) val %dirty = %changed if (%changed and 0b1110 === 0) { %dirty = %dirty or if (%composer.changed(greeting)) 0b0100 else 0b0010 } if (%dirty and 0b1011 xor 0b0010 !== 0 || !%composer.skipping) { Text("Hello, $greeting!") } else { %composer.skipToGroupEnd() } %composer.endRestartGroup() ?.updateScope { %composer: Composer?, %force: Int -> HelloWorld(greeting, %composer, %changed or 0b0001) } }
  38. @Composable fun HelloWorld(greeting: String, %composer: Composer?, %changed: Int) { %composer

    = %composer.startRestartGroup(<>) val %dirty = %changed if (%changed and 0b1110 === 0) { %dirty = %dirty or if (%composer.changed(greeting)) 0b0100 else 0b0010 } if (%dirty and 0b1011 xor 0b0010 !== 0 || !%composer.skipping) { Text("Hello, $greeting!") } else { %composer.skipToGroupEnd() } %composer.endRestartGroup() ?.updateScope { %composer: Composer?, %force: Int -> HelloWorld(greeting, %composer, %changed or 0b0001) } }
  39. @Composable fun HelloWorld(greeting: String, %composer: Composer?, %changed: Int) { %composer

    = %composer.startRestartGroup(<>) val %dirty = %changed if (%changed and 0b1110 === 0) { %dirty = %dirty or if (%composer.changed(greeting)) 0b0100 else 0b0010 } if (%dirty and 0b1011 xor 0b0010 !== 0 || !%composer.skipping) { Text("Hello, $greeting!") } else { %composer.skipToGroupEnd() } %composer.endRestartGroup() ?.updateScope { %composer: Composer?, %force: Int -> HelloWorld(greeting, %composer, %changed or 0b0001) } }
  40. @Composable fun HelloWorld(greeting: String, %composer: Composer?, %changed: Int) { %composer

    = %composer.startRestartGroup(<>) val %dirty = %changed if (%changed and 0b1110 === 0) { %dirty = %dirty or if (%composer.changed(greeting)) 0b0100 else 0b0010 } if (%dirty and 0b1011 xor 0b0010 !== 0 || !%composer.skipping) { Text("Hello, $greeting!") } else { %composer.skipToGroupEnd() } %composer.endRestartGroup() ?.updateScope { %composer: Composer?, %force: Int -> HelloWorld(greeting, %composer, %changed or 0b0001) } }
  41. .kt AST/PSI Raw FIR FIR IR Lowered IR Target code

    Optimized target code Tokens FILE: funAssignment.kt public? fi nal? fun two(): <implicit> { ^two IntegerLiteral(2) } package public fun two(): kotlin.Unit FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing *.class *.js *.so FILE fqName:<root> fi leName :/funAssignment.kt FUN name:two visibility: public modality:FINAL <> () returnType:kotlin.Int BLOCK_BODY RETURN type=kotlin.Nothing from='public fi nal fun two (): kotlin.Int d eclared in <root>' CONST Int type=kotlin.Int *.class *.js *.so src class ComposeComponentRegistrar : @Suppress(“DEPRECATION”) ComponentRegistrar { override fun registerProjectComponents( project: MockProject, configuration: CompilerConfiguration ) { if (checkCompilerVersion(configuration)) { registerCommonExtensions(project) IrGenerationExtension.registerExtension(…) } }
  42. class ComposeIrGenerationExtension(…) : IrGenerationExtension { … override fun generate(…) {

    … // Verify that our transformations didn't break something if (validateIr) validateIr(moduleFragment, pluginContext.irBuiltIns) } }
  43. class ComposeIrGenerationExtension(…) : IrGenerationExtension { … override fun generate(…) {

    … // Input check. This should always pass… if (validateIr) validateIr(moduleFragment, pluginContext.irBuiltIns) // create a symbol remapper to be used across all transforms val symbolRemapper = ComposableSymbolRemapper() … ClassStabilityTransformer(…).lower(moduleFragment) fun A(@Composable f() -> Unit) fun A(Composer<*>, Int -> Unit)
  44. // transform all composable functions to have an extra synthetic

    composer // parameter. ComposerParamTransformer( pluginContext, symbolRemapper, decoysEnabled, metrics, ).lower(moduleFragment) … // transform calls to the currentComposer to just use the local parameter from // previous transform ComposerIntrinsicTransformer(pluginContext, decoysEnabled).lower(moduleFragment) ComposeIrGenerationExtension.kt
  45. // $changed[n] for (i in 0 until changedParamCount(realValueParams, ownerFn.thisParamCount)) {

    if (argIndex < ownerFn.valueParameters.size) { it.putValueArgument( argIndex++, irConst(0) ) } else { error("1. expected value parameter count to be higher: ${this.dumpSrc()}") } } ComposerParamTransformer.kt @mvndy_hd @[email protected]
  46. // $changed[n] for (i in 0 until changedParamCount(realValueParams, ownerFn.thisParamCount)) {

    if (argIndex < ownerFn.valueParameters.size) { it.putValueArgument( argIndex++, irConst(0) ) } else { error("1. expected value parameter count to be higher: ${this.dumpSrc()}") } } ComposerParamTransformer.kt @mvndy_hd @[email protected]
  47. val IrFunction.thisParamCount get() = contextReceiverParametersCount + (if (dispatchReceiverParameter != null)

    1 else 0) + (if (extensionReceiverParameter != null) 1 else 0) fun changedParamCount(realValueParams: Int, thisParams: Int): Int { val totalParams = realValueParams + thisParams if (totalParams == 0) return 1 // There is always at least 1 changed param return ceil( totalParams.toDouble() / SLOTS_PER_INT.toDouble() ).toInt() } ComposableFunctionBodyTransformer.kt Calculates the number of 'changed' params needed
  48. 1 MODULE_FRAGMENT name: <test-module> 2 FILE fqName: <root> fileName:/Test.kt 3

    FUN name:HellowWorld visibility:public modality:FINAL <> (greeting:kotlin.String) returnType: Kotlin.unit 4 annotations: 5 Composable 6 VALUE_PARAMETER name: greeting index:0 type:kotlin.String 7 BLOCK_BODY 1 MODULE_FRAGMENT name: <test-module> 2 FILE fqName: <root> fileName:/Test.kt 3 FUN name:HelloWorld visibility:public modality:FINAL <> (greeting:kotlin.String, $composer:androidx.compose.runtime.Composer?, $changed:kotlin.Int) returnType: Kotlin.unit 4 annotations: 5 Composable 6 VALUE_PARAMETER name:greeting index:0 type:kotlin.String 7 VALUE_PARAMETER name:$composer index:1 type:androidx.compose.runtime.Composer?, [assignable] 8 VALUE_PARAMETER name:$changed index:2 type:kotlin.Int 9 BLOCK_BODY 10 BLOCK type=kotlin.Unit origin=null 11 SET_VAR ‘$composer: android.compose.runtime.Composer? [assignable] declared in <root>.HelloWorld’ type=kotlin.Unit origin=null 12 CALL `public abstract fun startRestartGroup (key: Kotlin:Int): androids.compose.runtime.Composer declared in androidx.compose.runtime.Composer’ type=androidx.compose.runtime.Composer? origin=null 13 $this: GET_VAR ‘$composer: androidx.compose.runtime.Composer? [assignment] declared in <root>.Hello-World’ type:androidx.compose.runtime.Composer? origin=null 14 key: CONST Int type=kotlin.Int value=73430592 15 CALL ‘public final fun sourceInformation(composer: androidx.compose.runtime.ComposerKt’ type=kotlin.Unit origin=null 16 composer: GET_VAR ‘$composer: androidx.compose.runtime.Composer? [assignable] declared in <root>.HelloWorld’ type=kotlin.Unit origin=null 17 sourceInformation: CONST String type=kotlin.String value=“((HelloWorld):Test.kt” 18 WHEN type=kotlin.Boolean origin=IF 19 BRANCH 20 if: WHEN type=kotlin.Boolean origin=OROR 21 BRANCH 22 if: CALL ‘public final fun not (): kotlin.Boolean [operator] declared in kotlin.Boolean’ type=kotlin.Boolean origin=null 23 $this: CALL ‘public final fun EQEQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlinBoolean declared in kotlin.internal.ir' type=kotlinBoolean \. origin=null 24 arg0: CALL ‘public final fun and (other: kotlin.Int): kotlin.Int [infix] declared in kotlin.Int type=kotlin.Int origin=null 25 $this: GET_VAR ‘$changed: kotlin.Int declared in <root>.HelloWorld’ type=kotlin.Int origin=null 26 other: CONST Int type=kotlin.Int value=1 27 arg1: CONST Int type=kotlin.Int value=0 28 then: CONST Boolean type=kotlin.Boolean value=true 29 BRANCH 30 if: CONST Boolean type=kotlin.Boolean value=true @mvndy_hd @[email protected]