[ANDROID] Simplify parcelize ir plugin code

- avoid direct usages of CommonBackendContext & Backend Symbols
This commit is contained in:
Roman Artemev
2020-05-08 11:25:41 +03:00
committed by romanart
parent c20aa297f1
commit 19e352a1b5
4 changed files with 12 additions and 11 deletions
@@ -13,6 +13,9 @@ import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
@Suppress("DEPRECATION_ERROR") @Suppress("DEPRECATION_ERROR")
class ParcelableIrGeneratorExtension : org.jetbrains.kotlin.backend.common.extensions.PureIrGenerationExtension { class ParcelableIrGeneratorExtension : org.jetbrains.kotlin.backend.common.extensions.PureIrGenerationExtension {
override fun generate(moduleFragment: IrModuleFragment, context: CommonBackendContext) { override fun generate(moduleFragment: IrModuleFragment, context: CommonBackendContext) {
ParcelableIrTransformer(context, AndroidSymbols(context, moduleFragment)).transform(moduleFragment) val arrayOfNulls = context.ir.symbols.arrayOfNulls
val charSequence = context.ir.symbols.charSequence
val androidSymbols = AndroidSymbols(context.irBuiltIns, arrayOfNulls, charSequence, moduleFragment)
ParcelableIrTransformer(context, androidSymbols).transform(moduleFragment)
} }
} }
@@ -30,13 +30,11 @@ import org.jetbrains.kotlin.name.Name
// hence contain just enough information to produce correct JVM bytecode for *calls*. In particular, we omit generic types and // hence contain just enough information to produce correct JVM bytecode for *calls*. In particular, we omit generic types and
// supertypes, which are not needed to produce correct bytecode. // supertypes, which are not needed to produce correct bytecode.
class AndroidSymbols( class AndroidSymbols(
context: CommonBackendContext, val irBuiltIns: IrBuiltIns,
val arrayOfNulls: IrSimpleFunctionSymbol,
private val charSequence: IrClassSymbol,
private val moduleFragment: IrModuleFragment private val moduleFragment: IrModuleFragment
) { ) {
val irBuiltIns: IrBuiltIns = context.irBuiltIns
val irSymbols: Symbols<CommonBackendContext> = context.ir.symbols
private val androidOs: IrPackageFragment = createPackage("android.os") private val androidOs: IrPackageFragment = createPackage("android.os")
private val androidText: IrPackageFragment = createPackage("android.text") private val androidText: IrPackageFragment = createPackage("android.text")
@@ -117,7 +115,7 @@ class AndroidSymbols(
modality = Modality.ABSTRACT modality = Modality.ABSTRACT
}.apply { }.apply {
createImplicitParameterDeclarationWithWrappedDescriptor() createImplicitParameterDeclarationWithWrappedDescriptor()
val t = addTypeParameter("T", context.irBuiltIns.anyNType) val t = addTypeParameter("T", irBuiltIns.anyNType)
parent = androidOsParcelable.owner parent = androidOsParcelable.owner
addFunction("createFromParcel", t.defaultType, Modality.ABSTRACT).apply { addFunction("createFromParcel", t.defaultType, Modality.ABSTRACT).apply {
@@ -128,7 +126,7 @@ class AndroidSymbols(
"newArray", irBuiltIns.arrayClass.typeWith(t.defaultType.makeNullable()), "newArray", irBuiltIns.arrayClass.typeWith(t.defaultType.makeNullable()),
Modality.ABSTRACT Modality.ABSTRACT
).apply { ).apply {
addValueParameter("size", context.irBuiltIns.intType) addValueParameter("size", irBuiltIns.intType)
} }
}.symbol }.symbol
@@ -386,7 +384,7 @@ class AndroidSymbols(
val textUtilsWriteToParcel: IrSimpleFunctionSymbol = val textUtilsWriteToParcel: IrSimpleFunctionSymbol =
androidTextTextUtils.owner.addFunction("writeToParcel", irBuiltIns.unitType, isStatic = true).apply { androidTextTextUtils.owner.addFunction("writeToParcel", irBuiltIns.unitType, isStatic = true).apply {
addValueParameter("cs", irSymbols.charSequence.defaultType) addValueParameter("cs", charSequence.defaultType)
addValueParameter("p", androidOsParcel.defaultType) addValueParameter("p", androidOsParcel.defaultType)
addValueParameter("parcelableFlags", irBuiltIns.intType) addValueParameter("parcelableFlags", irBuiltIns.intType)
}.symbol }.symbol
@@ -216,7 +216,7 @@ class IrArrayParcelSerializer(
) : IrParcelSerializer { ) : IrParcelSerializer {
private fun AndroidIrBuilder.newArray(size: IrExpression): IrExpression { private fun AndroidIrBuilder.newArray(size: IrExpression): IrExpression {
val arrayConstructor: IrFunctionSymbol = if (arrayType.isBoxedArray) val arrayConstructor: IrFunctionSymbol = if (arrayType.isBoxedArray)
androidSymbols.irSymbols.arrayOfNulls androidSymbols.arrayOfNulls
else else
arrayType.classOrNull!!.constructors.single { it.owner.valueParameters.size == 1 } arrayType.classOrNull!!.constructors.single { it.owner.valueParameters.size == 1 }
@@ -190,7 +190,7 @@ class ParcelableIrTransformer(private val context: CommonBackendContext, private
body = context.createIrBuilder(symbol).run { body = context.createIrBuilder(symbol).run {
irExprBody( irExprBody(
parcelerNewArray(parcelerObject, sizeParameter) parcelerNewArray(parcelerObject, sizeParameter)
?: irCall(androidSymbols.irSymbols.arrayOfNulls, arrayType).apply { ?: irCall(androidSymbols.arrayOfNulls, arrayType).apply {
putTypeArgument(0, arrayType) putTypeArgument(0, arrayType)
putValueArgument(0, irGet(sizeParameter)) putValueArgument(0, irGet(sizeParameter))
} }