Skip to content

Commit

Permalink
Fixed ir transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
IVIanuu committed Feb 23, 2024
1 parent cdfc929 commit bf54223
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Copyright 2022 Manuel Wrage. Use of this source code is governed by the Apache 2.0 license.
*/

@file:OptIn(UnsafeCastFunction::class, ObsoleteDescriptorBasedAPI::class,
FirIncompatiblePluginAPI::class, ObsoleteDescriptorBasedAPI::class
@file:OptIn(UnsafeCastFunction::class, FirIncompatiblePluginAPI::class, ObsoleteDescriptorBasedAPI::class,
ObsoleteDescriptorBasedAPI::class
)

package com.ivianuu.injekt.compiler.backend
Expand Down Expand Up @@ -35,7 +35,7 @@ import java.io.*
import kotlin.collections.*

class InjectCallTransformer(
private val localDeclarations: LocalDeclarations,
private val compilationDeclarations: CompilationDeclarations,
private val irCtx: IrPluginContext,
private val ctx: Context
) : IrElementTransformerVoidWithContext() {
Expand Down Expand Up @@ -382,52 +382,27 @@ class InjectCallTransformer(
)
else irBuilder.irGet(
injectable.type.toIrType().typeOrNull!!,
localDeclarations.variables.single { it.descriptor == descriptor }.symbol
compilationDeclarations.variables.single { it.descriptor == descriptor }.symbol
)

private fun CallableDescriptor.irCallable(): IrFunction {
when (this) {
is ClassConstructorDescriptor -> {
if (constructedClass.visibility == DescriptorVisibilities.LOCAL)
return localDeclarations.classes
.single { it.descriptor.uniqueKey(ctx) == constructedClass.uniqueKey(ctx) }
.constructors
.single { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }

return irCtx.referenceConstructors(constructedClass.fqNameSafe)
.single { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }
.owner
}
is FunctionDescriptor -> {
if (visibility == DescriptorVisibilities.LOCAL)
return localDeclarations.functions.single {
it.descriptor.uniqueKey(ctx) == uniqueKey(ctx)
}

if (containingDeclaration.safeAs<DeclarationDescriptorWithVisibility>()
?.visibility == DescriptorVisibilities.LOCAL)
return localDeclarations.classes.flatMap { it.declarations }
.single { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }
.cast()

return irCtx.referenceFunctions(fqNameSafe)
.single { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }
.owner
}
is PropertyDescriptor -> {
if (containingDeclaration.safeAs<DeclarationDescriptorWithVisibility>()
?.visibility == DescriptorVisibilities.LOCAL)
return localDeclarations.classes.flatMap { it.declarations }
.single { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }
.cast()

return irCtx.referenceProperties(fqNameSafe)
private fun CallableDescriptor.irCallable() = when (this) {
is ClassConstructorDescriptor -> compilationDeclarations.constructors
.singleOrNull { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }
?: irCtx.referenceConstructors(constructedClass.fqNameSafe)
.single { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }
.owner
.getter!!
}
else -> throw AssertionError("Unexpected callable $this")
}
is FunctionDescriptor -> compilationDeclarations.functions.singleOrNull {
it.descriptor.uniqueKey(ctx) == uniqueKey(ctx)
} ?: irCtx.referenceFunctions(fqNameSafe)
.single { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }
.owner
is PropertyDescriptor -> (compilationDeclarations.properties
.singleOrNull { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }
?: irCtx.referenceProperties(fqNameSafe)
.single { it.descriptor.uniqueKey(ctx) == uniqueKey(ctx) }
.owner)
.getter!!
else -> throw AssertionError("Unexpected callable $this")
}

private fun InjektType.toIrType(): IrTypeArgument {
Expand Down Expand Up @@ -472,7 +447,7 @@ class InjectCallTransformer(
else -> {
val key = classifier.descriptor!!.uniqueKey(ctx)
val fqName = FqName(key.split(":")[1])
val irClassifier = localDeclarations.classes.singleOrNull {
val irClassifier = compilationDeclarations.classes.singleOrNull {
it.descriptor.uniqueKey(ctx) == key
}
?.symbol
Expand Down Expand Up @@ -528,23 +503,33 @@ class InjectCallTransformer(
}
}

class LocalDeclarations : IrElementTransformerVoid() {
class CompilationDeclarations : IrElementTransformerVoid() {
val classes = mutableSetOf<IrClass>()
val constructors = mutableSetOf<IrConstructor>()
val functions = mutableSetOf<IrFunction>()
val properties = mutableSetOf<IrProperty>()
val variables = mutableSetOf<IrVariable>()

override fun visitClass(declaration: IrClass): IrStatement {
if (declaration.visibility == DescriptorVisibilities.LOCAL)
classes += declaration
classes += declaration
return super.visitClass(declaration)
}

override fun visitConstructor(declaration: IrConstructor): IrStatement {
constructors += declaration
return super.visitConstructor(declaration)
}

override fun visitFunction(declaration: IrFunction): IrStatement {
if (declaration.visibility == DescriptorVisibilities.LOCAL)
functions += declaration
functions += declaration
return super.visitFunction(declaration)
}

override fun visitProperty(declaration: IrProperty): IrStatement {
properties += declaration
return super.visitProperty(declaration)
}

override fun visitVariable(declaration: IrVariable): IrStatement {
variables += declaration
return super.visitVariable(declaration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class InjektIrGenerationExtension(private val dumpDir: File) : IrGenerationExten
DelegatingBindingTrace(pluginContext.bindingContext, "IR trace")
)

val localDeclarations = LocalDeclarations()
moduleFragment.transform(localDeclarations, null)
moduleFragment.transform(InjectCallTransformer(localDeclarations, pluginContext, ctx), null)
val compilationDeclarations = CompilationDeclarations()
moduleFragment.transform(compilationDeclarations, null)
moduleFragment.transform(InjectCallTransformer(compilationDeclarations, pluginContext, ctx), null)
moduleFragment.patchDeclarationParents()
moduleFragment.dumpToFiles(dumpDir, ctx)
}
Expand Down

0 comments on commit bf54223

Please sign in to comment.