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

Experimenting with the Kotlin Compiler

Experimenting with the Kotlin Compiler

Slides for my talk about the Kotlin Compiler, given at Android Makers 2020.

Jossi Wolf

April 21, 2020
Tweet

More Decks by Jossi Wolf

Other Decks in Programming

Transcript

  1. @jossiwolf class ChatSdk: MessageFeature { private val messages = mutableListOf<Message>()

    override fun poll(): List<Message> = messages override fun push(message: Message) { messages.add(message) } }
  2. @jossiwolf class SimpleMessenger: MessageFeature { private val messages = mutableListOf<Message>()

    override fun poll(): List<Message> = messages override fun push(message: Message) { messages.add(message) } }
  3. @jossiwolf public final class ChatSdk implements MessageFeature { // $FF:

    synthetic field private final SimpleMessenger $$delegate_0 = new SimpleMessenger(); @NotNull public List poll() { return this.$$delegate_0.poll(); } public void push(@NotNull Message message) { Intrinsics.checkParameterIsNotNull(message, "message"); this.$$delegate_0.push(message); } }
  4. @jossiwolf public final class ChatSdk implements MessageFeature { // $FF:

    synthetic field private final SimpleMessenger $$delegate_0 = new SimpleMessenger(); @NotNull public List poll() { return this.$$delegate_0.poll(); } public void push(@NotNull Message message) { Intrinsics.checkParameterIsNotNull(message, "message"); this.$$delegate_0.push(message); } }
  5. @jossiwolf public final class ChatSdk implements MessageFeature { // $FF:

    synthetic field private final SimpleMessenger $$delegate_0 = new SimpleMessenger(); @NotNull public List poll() { return this.$$delegate_0.poll(); } public void push(@NotNull Message message) { Intrinsics.checkParameterIsNotNull(message, "message"); this.$$delegate_0.push(message); } }
  6. @jossiwolf public final class ChatSdk implements MessageFeature { // $FF:

    synthetic field private final SimpleMessenger $$delegate_0 = new SimpleMessenger(); @NotNull public List poll() { return this.$$delegate_0.poll(); } public void push(@NotNull Message message) { Intrinsics.checkParameterIsNotNull(message, "message"); this.$$delegate_0.push(message); } }
  7. @jossiwolf public final class ChatSdk implements MessageFeature { // $FF:

    synthetic field private final SimpleMessenger $$delegate_0 = new SimpleMessenger(); @NotNull public List poll() { return this.$$delegate_0.poll(); } public void push(@NotNull Message message) { Intrinsics.checkParameterIsNotNull(message, "message"); this.$$delegate_0.push(message); } }
  8. @jossiwolf public final class ChatSdk implements MessageFeature { // $FF:

    synthetic field private final SimpleMessenger $$delegate_0 = new SimpleMessenger(); @NotNull public List poll() { return this.$$delegate_0.poll(); } public void push(@NotNull Message message) { Intrinsics.checkParameterIsNotNull(message, "message"); this.$$delegate_0.push(message); } }
  9. @jossiwolf To build Kotlin, you need different JDK versions! -

    github.com/jetbrains/kotlin #build-environment-requirements
  10. @jossiwolf Abstract Syntax Tree @Annotation1("abc") // line comment between annotations

    @Annotation2("\${123}") fun parse() {} https://github.com/kotlinx/ast
  11. @jossiwolf Abstract Syntax Tree PackageHeader() importList KlassDeclaration(fun parse) KlassAnnotation(Annotation1) KlassArgument()

    KlassString "abc" KlassAnnotation(Annotation2) KlassArgument() KlassString Escape("\$") "{123}" https://github.com/kotlinx/ast
  12. @jossiwolf Compiler Parser Lexer Codegen Intermediate Code Generator Non-optimised IR

    https://github.com/ahinchman1/Kotlin-Compiler-Crash-Course
  13. @jossiwolf Compiler Parser Lexer Codegen Intermediate Code Generator Intermediate Code

    Optimizer Non-optimised IR https://github.com/ahinchman1/Kotlin-Compiler-Crash-Course
  14. @jossiwolf class ChatSdk: MessageFeature by messenger { classdelegate var messenger:

    MessageFeature fun swapMessenger() { messenger = when (messenger) { is SimpleMessenger -> ComplexMessenger() is ComplexMessenger -> SimpleMessenger() } } }
  15. @jossiwolf KtModifierKeywordToken LATEINIT_KEYWORD = KtModifierKeywordToken.softKeywordModifier("lateinit"); KtModifierKeywordToken DATA_KEYWORD = KtModifierKeywordToken.softKeywordModifier("data"); KtModifierKeywordToken

    INLINE_KEYWORD = KtModifierKeywordToken.softKeywordModifier("inline"); KtModifierKeywordToken NOINLINE_KEYWORD = KtModifierKeywordToken.softKeywordModifier("noinline"); KtTokens.java
  16. @jossiwolf KtModifierKeywordToken CLASSDELEGATE_KEYWORD = KtModifierKeywordToken.softKeywordModifier("classdelegate"); KtModifierKeywordToken LATEINIT_KEYWORD = KtModifierKeywordToken.softKeywordModifier("lateinit"); KtModifierKeywordToken

    DATA_KEYWORD = KtModifierKeywordToken.softKeywordModifier("data"); KtModifierKeywordToken INLINE_KEYWORD = KtModifierKeywordToken.softKeywordModifier("inline"); KtModifierKeywordToken NOINLINE_KEYWORD = KtModifierKeywordToken.softKeywordModifier("noinline"); KtTokens.java
  17. @jossiwolf public final class ChatSdk implements MessageFeature { // $FF:

    synthetic field private final SimpleMessenger $$delegate_0 = new SimpleMessenger();
  18. @jossiwolf for (specifier: myClass.getSuperTypeListEntries()) { if (specifier instanceof KtDelegatedSuperTypeEntry) {

    genCallToDelegatorByExpressionSpecifier( iv, codegen, (KtDelegatedSuperTypeEntry) specifier, fieldsInfo ); } }
  19. @jossiwolf for (specifier: myClass.getSuperTypeListEntries()) { if (specifier instanceof KtDelegatedSuperTypeEntry) {

    genCallToDelegatorByExpressionSpecifier( iv, codegen, (KtDelegatedSuperTypeEntry) specifier, fieldsInfo ); } }
  20. @jossiwolf for (specifier: myClass.getSuperTypeListEntries()) { if (specifier instanceof KtDelegatedSuperTypeEntry) {

    genCallToDelegatorByExpressionSpecifier( iv, codegen, (KtDelegatedSuperTypeEntry) specifier, fieldsInfo ); } }
  21. @jossiwolf for (specifier: myClass.getSuperTypeListEntries()) { if (specifier instanceof KtDelegatedSuperTypeEntry) {

    genCallToDelegatorByExpressionSpecifier( iv, codegen, (KtDelegatedSuperTypeEntry) specifier, fieldsInfo ); } }
  22. @jossiwolf List<ValueParameterDescriptor> parameters = constructorDescriptor.getValueParameters(); for (KtParameter parameter : classBodyCodegen.getPrimaryConstructorParameters())

    { if (parameter.hasValOrVar()) { VariableDescriptor descriptor = parameters.get(curParam); Type type = typeMapper.mapType(descriptor); iv.load(0, classAsmType); iv.load(codegen.myFrameMap.getIndex(descriptor), type); PropertyDescriptor propertyDescriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter); iv.putfield(classAsmType.getInternalName(), context.getFieldName(propertyDescriptor, false), type.getDescriptor()); } curParam++; }
  23. @jossiwolf public final class ChatSdk implements MessageFeature { public MessageFeature

    messenger; public final MessageFeature getMessenger() { return this.messenger; } public final void setMessenger(MessageFeature var1) { this.messenger = var1; } public List poll() { return this.messenger.poll(); } public void push(Message message) { this.messenger.push(message); } }
  24. @jossiwolf public final class ChatSdk implements MessageFeature { public MessageFeature

    messenger; public final MessageFeature getMessenger() { return this.messenger; } public final void setMessenger(MessageFeature var1) { this.messenger = var1; } public List poll() { return this.messenger.poll(); } public void push(Message message) { this.messenger.push(message); } }
  25. @jossiwolf public final class ChatSdk implements MessageFeature { public MessageFeature

    messenger; public final MessageFeature getMessenger() { return this.messenger; } public final void setMessenger(MessageFeature var1) { this.messenger = var1; } public List poll() { return this.messenger.poll(); } public void push(Message message) { this.messenger.push(message); } }
  26. @jossiwolf PSI - KotlinStubVersions.kt - Lexer - KtTokens.java Resolution -

    FakeCallableDescriptorForObject.kt - Scopes - Scopes.kt - MemberScope.kt - ClassResolutionScopesSupport.kt - DelegationValueArgument.java - TypeResolver.kt for modifying the scope - DescriptorResolver.java - BodyResolver.java Codegen - DelegationFieldsInfo.java - ImplementationBodyCodegen.java - ConstructorCodegen.java - CodegenUtil.kt
  27. @jossiwolf IR - IrProperty.kt Backend - MoveCompanionObjectFieldsLowering.kt - SharedVariablesManager.kt Descriptors

    - JavaPropertyDescriptor.java - ValueParameterDescriptor.java - VariableDescriptor.java - PropertyDescriptorImpl.java - ErrorUtils.java Serialization Adjustments Debugger Tinkering Scripting Adjustments