[IR-plugin] New symbol tables
This commit is contained in:
@@ -8,7 +8,7 @@ configureJvmProject(project)
|
|||||||
configurePublishing(project)
|
configurePublishing(project)
|
||||||
|
|
||||||
group = 'org.jetbrains.kotlinx'
|
group = 'org.jetbrains.kotlinx'
|
||||||
version = '0.6.2'
|
version = '0.7.1'
|
||||||
if (!project.hasProperty("deploy")) {
|
if (!project.hasProperty("deploy")) {
|
||||||
version = "$version-SNAPSHOT"
|
version = "$version-SNAPSHOT"
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ jar {
|
|||||||
manifestAttributes(manifest, project)
|
manifestAttributes(manifest, project)
|
||||||
}
|
}
|
||||||
|
|
||||||
ArtifactsKt.runtimeJar(project, EmbeddableKt.rewriteDepsToShadedCompiler(project, jar, {}), {})
|
//ArtifactsKt.runtimeJar(project, EmbeddableKt.rewriteDepsToShadedCompiler(project, jar, {}), {})
|
||||||
|
|
||||||
artifacts {
|
artifacts {
|
||||||
archives sourcesJar
|
archives sourcesJar
|
||||||
|
|||||||
+2
-2
@@ -45,14 +45,14 @@ class SerializationKotlinGradleSubplugin : KotlinGradleSubplugin<AbstractCompile
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val log = Logging.getLogger(this.javaClass)
|
private val log = Logging.getLogger(this.javaClass)
|
||||||
private val pluginVersion = "0.6.2-SNAPSHOT"
|
private val pluginVersion = "0.7.1-SNAPSHOT"
|
||||||
|
|
||||||
override fun isApplicable(project: Project, task: AbstractCompile) = SerializationGradleSubplugin.isEnabled(project)
|
override fun isApplicable(project: Project, task: AbstractCompile) = SerializationGradleSubplugin.isEnabled(project)
|
||||||
|
|
||||||
override fun apply(
|
override fun apply(
|
||||||
project: Project,
|
project: Project,
|
||||||
kotlinCompile: AbstractCompile,
|
kotlinCompile: AbstractCompile,
|
||||||
javaCompile: AbstractCompile,
|
javaCompile: AbstractCompile?,
|
||||||
variantData: Any?,
|
variantData: Any?,
|
||||||
androidProjectHandler: Any?,
|
androidProjectHandler: Any?,
|
||||||
javaSourceSet: SourceSet?
|
javaSourceSet: SourceSet?
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
<expressionCodegenExtension implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationCodegenExtension"/>
|
<expressionCodegenExtension implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationCodegenExtension"/>
|
||||||
<syntheticResolveExtension implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationResolveExtension"/>
|
<syntheticResolveExtension implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationResolveExtension"/>
|
||||||
<jsSyntheticTranslateExtension implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationJsExtension"/>
|
<jsSyntheticTranslateExtension implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationJsExtension"/>
|
||||||
|
<irGenerationExtension implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationLoweringExtension"/>
|
||||||
|
|
||||||
<gradleProjectImportHandler implementation="org.jetbrains.kotlinx.serialization.idea.KotlinSerializationGradleImportHandler"/>
|
<gradleProjectImportHandler implementation="org.jetbrains.kotlinx.serialization.idea.KotlinSerializationGradleImportHandler"/>
|
||||||
<mavenProjectImportHandler implementation="org.jetbrains.kotlinx.serialization.idea.KotlinSerializationMavenImportHandler"/>
|
<mavenProjectImportHandler implementation="org.jetbrains.kotlinx.serialization.idea.KotlinSerializationMavenImportHandler"/>
|
||||||
|
|||||||
+30
-16
@@ -22,24 +22,26 @@ import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol
|
|||||||
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
|
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
|
||||||
import org.jetbrains.kotlin.ir.types.IrType
|
import org.jetbrains.kotlin.ir.types.IrType
|
||||||
import org.jetbrains.kotlin.ir.types.toKotlinType
|
import org.jetbrains.kotlin.ir.types.toKotlinType
|
||||||
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
import org.jetbrains.kotlin.ir.util.*
|
||||||
import org.jetbrains.kotlin.ir.util.declareSimpleFunctionWithOverrides
|
|
||||||
import org.jetbrains.kotlin.ir.util.withScope
|
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerializableProperty
|
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerializableProperty
|
||||||
|
|
||||||
|
val BackendContext.externalSymbols: ReferenceSymbolTable get() = ir.symbols.externalSymbolTable
|
||||||
|
|
||||||
interface IrBuilderExtension {
|
interface IrBuilderExtension {
|
||||||
val compilerContext: BackendContext
|
val compilerContext: BackendContext
|
||||||
val translator: TypeTranslator
|
val translator: TypeTranslator
|
||||||
|
|
||||||
fun IrClass.contributeFunction(descriptor: FunctionDescriptor, bodyGen: IrBlockBodyBuilder.(IrFunction) -> Unit) {
|
val BackendContext.localSymbolTable: SymbolTable
|
||||||
val f = compilerContext.symbolTable.declareSimpleFunctionWithOverrides(
|
|
||||||
|
fun IrClass.contributeFunction(descriptor: FunctionDescriptor, fromStubs: Boolean = false, bodyGen: IrBlockBodyBuilder.(IrFunction) -> Unit) {
|
||||||
|
val f: IrSimpleFunction = if (!fromStubs) compilerContext.localSymbolTable.declareSimpleFunctionWithOverrides(
|
||||||
this.startOffset,
|
this.startOffset,
|
||||||
this.endOffset,
|
this.endOffset,
|
||||||
SERIALIZABLE_PLUGIN_ORIGIN,
|
SERIALIZABLE_PLUGIN_ORIGIN,
|
||||||
descriptor
|
descriptor
|
||||||
)
|
) else compilerContext.externalSymbols.referenceSimpleFunction(descriptor).owner
|
||||||
f.parent = this
|
f.parent = this
|
||||||
f.returnType = descriptor.returnType!!.toIrType()
|
f.returnType = descriptor.returnType!!.toIrType()
|
||||||
f.createParameterDeclarations()
|
f.createParameterDeclarations()
|
||||||
@@ -51,7 +53,7 @@ interface IrBuilderExtension {
|
|||||||
descriptor: ClassConstructorDescriptor,
|
descriptor: ClassConstructorDescriptor,
|
||||||
bodyGen: IrBlockBodyBuilder.(IrConstructor) -> Unit
|
bodyGen: IrBlockBodyBuilder.(IrConstructor) -> Unit
|
||||||
) {
|
) {
|
||||||
val c = compilerContext.symbolTable.declareConstructor(
|
val c = compilerContext.localSymbolTable.declareConstructor(
|
||||||
this.startOffset,
|
this.startOffset,
|
||||||
this.endOffset,
|
this.endOffset,
|
||||||
SERIALIZABLE_PLUGIN_ORIGIN,
|
SERIALIZABLE_PLUGIN_ORIGIN,
|
||||||
@@ -90,12 +92,20 @@ interface IrBuilderExtension {
|
|||||||
startOffset,
|
startOffset,
|
||||||
endOffset,
|
endOffset,
|
||||||
classDescriptor.defaultType.toIrType(),
|
classDescriptor.defaultType.toIrType(),
|
||||||
compilerContext.symbolTable.referenceClass(classDescriptor)
|
compilerContext.externalSymbols.referenceClass(classDescriptor)
|
||||||
|
)
|
||||||
|
|
||||||
|
fun IrBuilderWithScope.irGetObject(irObject: IrClass) =
|
||||||
|
IrGetObjectValueImpl(
|
||||||
|
startOffset,
|
||||||
|
endOffset,
|
||||||
|
irObject.defaultType,
|
||||||
|
irObject.symbol
|
||||||
)
|
)
|
||||||
|
|
||||||
fun <T : IrDeclaration> T.buildWithScope(builder: (T) -> Unit): T =
|
fun <T : IrDeclaration> T.buildWithScope(builder: (T) -> Unit): T =
|
||||||
also { irDeclaration ->
|
also { irDeclaration ->
|
||||||
compilerContext.symbolTable.withScope(irDeclaration.descriptor) {
|
compilerContext.localSymbolTable.withScope(irDeclaration.descriptor) {
|
||||||
builder(irDeclaration)
|
builder(irDeclaration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,8 +149,12 @@ interface IrBuilderExtension {
|
|||||||
fun KotlinType.toIrType() = translateType(this)
|
fun KotlinType.toIrType() = translateType(this)
|
||||||
|
|
||||||
|
|
||||||
val SerializableProperty.irField: IrField
|
val SerializableProperty.irField: IrField get() = compilerContext.externalSymbols.referenceField(this.descriptor).owner
|
||||||
get () = compilerContext.symbolTable.referenceField(this.descriptor).owner
|
// get () {
|
||||||
|
// val symb = compilerContext.localSymbolTable.referenceField(this.descriptor)
|
||||||
|
// return if (symb.isBound) symb.owner
|
||||||
|
// else compilerContext.localSymbolTable.declareField()
|
||||||
|
// }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The rest of the file is mainly copied from FunctionGenerator.
|
The rest of the file is mainly copied from FunctionGenerator.
|
||||||
@@ -155,7 +169,7 @@ interface IrBuilderExtension {
|
|||||||
+IrDelegatingConstructorCallImpl(
|
+IrDelegatingConstructorCallImpl(
|
||||||
startOffset, endOffset,
|
startOffset, endOffset,
|
||||||
compilerContext.irBuiltIns.unitType,
|
compilerContext.irBuiltIns.unitType,
|
||||||
compilerContext.symbolTable.referenceConstructor(anyConstructor),
|
compilerContext.externalSymbols.referenceConstructor(anyConstructor),
|
||||||
anyConstructor
|
anyConstructor
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -182,7 +196,7 @@ interface IrBuilderExtension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun generatePropertyBackingField(propertyDescriptor: PropertyDescriptor): IrField {
|
fun generatePropertyBackingField(propertyDescriptor: PropertyDescriptor): IrField {
|
||||||
return compilerContext.symbolTable.declareField(
|
return compilerContext.localSymbolTable.declareField(
|
||||||
UNDEFINED_OFFSET,
|
UNDEFINED_OFFSET,
|
||||||
UNDEFINED_OFFSET,
|
UNDEFINED_OFFSET,
|
||||||
SERIALIZABLE_PLUGIN_ORIGIN,
|
SERIALIZABLE_PLUGIN_ORIGIN,
|
||||||
@@ -196,7 +210,7 @@ interface IrBuilderExtension {
|
|||||||
fieldSymbol: IrFieldSymbol,
|
fieldSymbol: IrFieldSymbol,
|
||||||
ownerSymbol: IrValueSymbol
|
ownerSymbol: IrValueSymbol
|
||||||
): IrSimpleFunction {
|
): IrSimpleFunction {
|
||||||
return compilerContext.symbolTable.declareSimpleFunctionWithOverrides(
|
return compilerContext.localSymbolTable.declareSimpleFunctionWithOverrides(
|
||||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||||
SERIALIZABLE_PLUGIN_ORIGIN, descriptor
|
SERIALIZABLE_PLUGIN_ORIGIN, descriptor
|
||||||
).buildWithScope { irAccessor ->
|
).buildWithScope { irAccessor ->
|
||||||
@@ -228,7 +242,7 @@ interface IrBuilderExtension {
|
|||||||
irAccessor.symbol,
|
irAccessor.symbol,
|
||||||
IrGetFieldImpl(
|
IrGetFieldImpl(
|
||||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||||
compilerContext.symbolTable.referenceField(property),
|
compilerContext.localSymbolTable.referenceField(property),
|
||||||
property.type.toIrType(),
|
property.type.toIrType(),
|
||||||
receiver
|
receiver
|
||||||
)
|
)
|
||||||
@@ -254,7 +268,7 @@ interface IrBuilderExtension {
|
|||||||
irBody.statements.add(
|
irBody.statements.add(
|
||||||
IrSetFieldImpl(
|
IrSetFieldImpl(
|
||||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||||
compilerContext.symbolTable.referenceField(property),
|
compilerContext.localSymbolTable.referenceField(property),
|
||||||
receiver,
|
receiver,
|
||||||
IrGetValueImpl(startOffset, endOffset, irValueParameter.type, irValueParameter.symbol),
|
IrGetValueImpl(startOffset, endOffset, irValueParameter.type, irValueParameter.symbol),
|
||||||
compilerContext.irBuiltIns.unitType
|
compilerContext.irBuiltIns.unitType
|
||||||
|
|||||||
+8
-3
@@ -6,6 +6,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
|||||||
import org.jetbrains.kotlin.ir.builders.irReturn
|
import org.jetbrains.kotlin.ir.builders.irReturn
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||||
|
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||||
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializableCompanionCodegen
|
import org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializableCompanionCodegen
|
||||||
@@ -19,7 +20,10 @@ class SerializableCompanionIrGenerator(
|
|||||||
override val compilerContext: BackendContext,
|
override val compilerContext: BackendContext,
|
||||||
bindingContext: BindingContext
|
bindingContext: BindingContext
|
||||||
) : SerializableCompanionCodegen(irClass.descriptor), IrBuilderExtension {
|
) : SerializableCompanionCodegen(irClass.descriptor), IrBuilderExtension {
|
||||||
override val translator: TypeTranslator = TypeTranslator(compilerContext.symbolTable)
|
override val translator: TypeTranslator = TypeTranslator(compilerContext.externalSymbols, compilerContext.irBuiltIns.languageVersionSettings)
|
||||||
|
private val _table = SymbolTable()
|
||||||
|
override val BackendContext.localSymbolTable: SymbolTable
|
||||||
|
get() = _table
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun generate(irClass: IrClass,
|
fun generate(irClass: IrClass,
|
||||||
@@ -32,12 +36,13 @@ class SerializableCompanionIrGenerator(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun generateSerializerGetter(methodDescriptor: FunctionDescriptor) = irClass.contributeFunction(methodDescriptor) { getter ->
|
override fun generateSerializerGetter(methodDescriptor: FunctionDescriptor) =
|
||||||
|
irClass.contributeFunction(methodDescriptor, fromStubs = true) { getter ->
|
||||||
val serializer = serializableDescriptor.classSerializer?.toClassDescriptor!!
|
val serializer = serializableDescriptor.classSerializer?.toClassDescriptor!!
|
||||||
val expr = if (serializer.kind == ClassKind.OBJECT) {
|
val expr = if (serializer.kind == ClassKind.OBJECT) {
|
||||||
irGetObject(serializer)
|
irGetObject(serializer)
|
||||||
} else {
|
} else {
|
||||||
val ctor = compilerContext.symbolTable.referenceConstructor(serializer.unsubstitutedPrimaryConstructor!!)
|
val ctor = compilerContext.externalSymbols.referenceConstructor(serializer.unsubstitutedPrimaryConstructor!!)
|
||||||
val args: List<IrExpression> = emptyList() // todo
|
val args: List<IrExpression> = emptyList() // todo
|
||||||
irInvoke(null, ctor, *args.toTypedArray())
|
irInvoke(null, ctor, *args.toTypedArray())
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-4
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.ir.declarations.IrField
|
|||||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
|
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
|
||||||
|
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||||
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
||||||
import org.jetbrains.kotlin.ir.util.constructors
|
import org.jetbrains.kotlin.ir.util.constructors
|
||||||
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
|
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
|
||||||
@@ -29,8 +30,10 @@ class SerializableIrGenerator(
|
|||||||
override val compilerContext: BackendContext,
|
override val compilerContext: BackendContext,
|
||||||
bindingContext: BindingContext
|
bindingContext: BindingContext
|
||||||
) : SerializableCodegen(irClass.descriptor, bindingContext), IrBuilderExtension {
|
) : SerializableCodegen(irClass.descriptor, bindingContext), IrBuilderExtension {
|
||||||
override val translator: TypeTranslator = TypeTranslator(compilerContext.symbolTable)
|
override val translator: TypeTranslator = TypeTranslator(compilerContext.externalSymbols, compilerContext.irBuiltIns.languageVersionSettings)
|
||||||
|
private val _table = SymbolTable()
|
||||||
|
override val BackendContext.localSymbolTable: SymbolTable
|
||||||
|
get() = _table
|
||||||
|
|
||||||
override fun generateInternalConstructor(constructorDescriptor: ClassConstructorDescriptor) =
|
override fun generateInternalConstructor(constructorDescriptor: ClassConstructorDescriptor) =
|
||||||
irClass.contributeConstructor(constructorDescriptor) { ctor ->
|
irClass.contributeConstructor(constructorDescriptor) { ctor ->
|
||||||
@@ -52,8 +55,8 @@ class SerializableIrGenerator(
|
|||||||
val exceptionCtor =
|
val exceptionCtor =
|
||||||
serializableDescriptor.getClassFromSerializationPackage(MISSING_FIELD_EXC)
|
serializableDescriptor.getClassFromSerializationPackage(MISSING_FIELD_EXC)
|
||||||
.unsubstitutedPrimaryConstructor!!
|
.unsubstitutedPrimaryConstructor!!
|
||||||
val exceptionCtorRef = compilerContext.symbolTable.referenceConstructor(exceptionCtor)
|
val exceptionCtorRef = compilerContext.externalSymbols.referenceConstructor(exceptionCtor)
|
||||||
val exceptionType = exceptionCtor.returnType.toIrType()
|
val exceptionType = exceptionCtorRef.owner.returnType
|
||||||
|
|
||||||
val thiz = irClass.thisReceiver!!
|
val thiz = irClass.thisReceiver!!
|
||||||
if (KotlinBuiltIns.isAny(irClass.descriptor.getSuperClassOrAny()))
|
if (KotlinBuiltIns.isAny(irClass.descriptor.getSuperClassOrAny()))
|
||||||
|
|||||||
+22
-21
@@ -21,9 +21,7 @@ import org.jetbrains.kotlin.ir.expressions.mapValueParameters
|
|||||||
import org.jetbrains.kotlin.ir.expressions.mapValueParametersIndexed
|
import org.jetbrains.kotlin.ir.expressions.mapValueParametersIndexed
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
|
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
|
||||||
import org.jetbrains.kotlin.ir.types.*
|
import org.jetbrains.kotlin.ir.types.*
|
||||||
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
import org.jetbrains.kotlin.ir.util.*
|
||||||
import org.jetbrains.kotlin.ir.util.constructors
|
|
||||||
import org.jetbrains.kotlin.ir.util.withScope
|
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
|
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
@@ -46,7 +44,10 @@ object SERIALIZABLE_PLUGIN_ORIGIN : IrDeclarationOriginImpl("SERIALIZER")
|
|||||||
class SerializerIrGenerator(val irClass: IrClass, override val compilerContext: BackendContext, bindingContext: BindingContext) :
|
class SerializerIrGenerator(val irClass: IrClass, override val compilerContext: BackendContext, bindingContext: BindingContext) :
|
||||||
SerializerCodegen(irClass.descriptor, bindingContext), IrBuilderExtension {
|
SerializerCodegen(irClass.descriptor, bindingContext), IrBuilderExtension {
|
||||||
|
|
||||||
override val translator: TypeTranslator = TypeTranslator(compilerContext.symbolTable)
|
override val translator: TypeTranslator = TypeTranslator(compilerContext.externalSymbols, compilerContext.irBuiltIns.languageVersionSettings)
|
||||||
|
private val _table = SymbolTable()
|
||||||
|
override val BackendContext.localSymbolTable: SymbolTable
|
||||||
|
get() = _table
|
||||||
|
|
||||||
override fun generateSerialDesc() {
|
override fun generateSerialDesc() {
|
||||||
val desc: PropertyDescriptor = generatedSerialDescPropertyDescriptor ?: return
|
val desc: PropertyDescriptor = generatedSerialDescPropertyDescriptor ?: return
|
||||||
@@ -61,24 +62,24 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
lateinit var prop: IrProperty
|
lateinit var prop: IrProperty
|
||||||
|
|
||||||
// how to (auto)create backing field and getter/setter?
|
// how to (auto)create backing field and getter/setter?
|
||||||
compilerContext.symbolTable.withScope(irClass.descriptor) {
|
compilerContext.localSymbolTable.withScope(irClass.descriptor) {
|
||||||
|
|
||||||
introduceValueParameter(thisAsReceiverParameter)
|
introduceValueParameter(thisAsReceiverParameter)
|
||||||
prop = generateSimplePropertyWithBackingField(thisAsReceiverParameter.symbol, desc, irClass)
|
prop = generateSimplePropertyWithBackingField(thisAsReceiverParameter.symbol, desc, irClass)
|
||||||
irClass.addMember(prop)
|
irClass.addMember(prop)
|
||||||
}
|
}
|
||||||
|
|
||||||
compilerContext.symbolTable.declareAnonymousInitializer(
|
compilerContext.localSymbolTable.declareAnonymousInitializer(
|
||||||
irClass.startOffset, irClass.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, irClass.descriptor
|
irClass.startOffset, irClass.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, irClass.descriptor
|
||||||
).buildWithScope { initIrBody ->
|
).buildWithScope { initIrBody ->
|
||||||
val ctor = irClass.declarations.filterIsInstance<IrConstructor>().singleOrNull()
|
val ctor = irClass.declarations.filterIsInstance<IrConstructor>().singleOrNull()
|
||||||
val serialClassDescImplCtor = compilerContext.symbolTable.referenceConstructor(serialDescImplConstructor)
|
val serialClassDescImplCtor = compilerContext.externalSymbols.referenceConstructor(serialDescImplConstructor)
|
||||||
compilerContext.symbolTable.withScope(initIrBody.descriptor) {
|
compilerContext.localSymbolTable.withScope(initIrBody.descriptor) {
|
||||||
initIrBody.body = compilerContext.createIrBuilder(initIrBody.symbol).irBlockBody {
|
initIrBody.body = compilerContext.createIrBuilder(initIrBody.symbol).irBlockBody {
|
||||||
val localDesc = irTemporary(
|
val localDesc = irTemporary(
|
||||||
irCall(
|
irCall(
|
||||||
serialClassDescImplCtor,
|
serialClassDescImplCtor,
|
||||||
type = serialDescImplConstructor.returnType.toIrType()
|
type = serialClassDescImplCtor.owner.returnType
|
||||||
).mapValueParameters { irString(serialName) },
|
).mapValueParameters { irString(serialName) },
|
||||||
nameHint = "serialDesc"
|
nameHint = "serialDesc"
|
||||||
)
|
)
|
||||||
@@ -125,7 +126,7 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
genericIndex: Int? = null
|
genericIndex: Int? = null
|
||||||
): IrExpression? {
|
): IrExpression? {
|
||||||
val nullableSerClass =
|
val nullableSerClass =
|
||||||
compilerContext.symbolTable.referenceClass(module.getClassFromInternalSerializationPackage(SpecialBuiltins.nullableSerializer))
|
compilerContext.externalSymbols.referenceClass(module.getClassFromInternalSerializationPackage(SpecialBuiltins.nullableSerializer))
|
||||||
if (serializerClass == null) {
|
if (serializerClass == null) {
|
||||||
if (genericIndex == null) return null
|
if (genericIndex == null) return null
|
||||||
return TODO("Saved serializer for generic argument")
|
return TODO("Saved serializer for generic argument")
|
||||||
@@ -146,9 +147,9 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
val serializable = getSerializableClassDescriptorBySerializer(serializerClass)
|
val serializable = getSerializableClassDescriptorBySerializer(serializerClass)
|
||||||
val ctor = if (serializable?.declaredTypeParameters?.isNotEmpty() == true) {
|
val ctor = if (serializable?.declaredTypeParameters?.isNotEmpty() == true) {
|
||||||
KSerializerDescriptorResolver.createTypedSerializerConstructorDescriptor(serializerClass, serializableDescriptor)
|
KSerializerDescriptorResolver.createTypedSerializerConstructorDescriptor(serializerClass, serializableDescriptor)
|
||||||
.let { compilerContext.symbolTable.referenceConstructor(it) }
|
.let { compilerContext.externalSymbols.referenceConstructor(it) }
|
||||||
} else {
|
} else {
|
||||||
compilerContext.symbolTable.referenceConstructor(serializerClass.unsubstitutedPrimaryConstructor!!)
|
compilerContext.externalSymbols.referenceConstructor(serializerClass.unsubstitutedPrimaryConstructor!!)
|
||||||
}
|
}
|
||||||
return irInvoke(
|
return irInvoke(
|
||||||
null,
|
null,
|
||||||
@@ -159,7 +160,7 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun ClassDescriptor.referenceMethod(methodName: String) =
|
fun ClassDescriptor.referenceMethod(methodName: String) =
|
||||||
getFuncDesc(methodName).single().let { compilerContext.symbolTable.referenceFunction(it) }
|
getFuncDesc(methodName).single().let { compilerContext.externalSymbols.referenceFunction(it) }
|
||||||
|
|
||||||
override fun generateSave(function: FunctionDescriptor) = irClass.contributeFunction(function) { saveFunc ->
|
override fun generateSave(function: FunctionDescriptor) = irClass.contributeFunction(function) { saveFunc ->
|
||||||
|
|
||||||
@@ -168,7 +169,7 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
|
|
||||||
val kOutputClass = serializerDescriptor.getClassFromSerializationPackage(STRUCTURE_ENCODER_CLASS)
|
val kOutputClass = serializerDescriptor.getClassFromSerializationPackage(STRUCTURE_ENCODER_CLASS)
|
||||||
|
|
||||||
val descriptorGetterSymbol = compilerContext.symbolTable.referenceFunction(anySerialDescProperty?.getter!!)
|
val descriptorGetterSymbol = compilerContext.localSymbolTable.referenceFunction(anySerialDescProperty?.getter!!) //???
|
||||||
|
|
||||||
val localSerialDesc = irTemporary(irGet(descriptorGetterSymbol.owner.returnType, irThis(), descriptorGetterSymbol), "desc")
|
val localSerialDesc = irTemporary(irGet(descriptorGetterSymbol.owner.returnType, irThis(), descriptorGetterSymbol), "desc")
|
||||||
|
|
||||||
@@ -206,7 +207,7 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
irInt(index),
|
irInt(index),
|
||||||
// todo: direct field access?
|
// todo: direct field access?
|
||||||
// irInvoke(irGet(serialObjectSymbol), compilerContext.symbolTable.referenceFunction(property.descriptor.getter!!))
|
// irInvoke(irGet(serialObjectSymbol), compilerContext.symbolTable.referenceFunction(property.descriptor.getter!!))
|
||||||
irGetField(irGet(serialObjectSymbol), compilerContext.symbolTable.referenceField(property.descriptor).owner)
|
irGetField(irGet(serialObjectSymbol), property.irField)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
val writeFunc = kOutputClass.referenceMethod("${CallingConventions.encode}${sti.elementMethodPrefix}Serializable${CallingConventions.elementPostfix}")
|
val writeFunc = kOutputClass.referenceMethod("${CallingConventions.encode}${sti.elementMethodPrefix}Serializable${CallingConventions.elementPostfix}")
|
||||||
@@ -218,7 +219,7 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
innerSerial,
|
innerSerial,
|
||||||
// todo: direct field access?
|
// todo: direct field access?
|
||||||
// irInvoke(irGet(serialObjectSymbol), compilerContext.symbolTable.referenceFunction(property.descriptor.getter!!))
|
// irInvoke(irGet(serialObjectSymbol), compilerContext.symbolTable.referenceFunction(property.descriptor.getter!!))
|
||||||
irGetField(irGet(serialObjectSymbol), compilerContext.symbolTable.referenceField(property.descriptor).owner)
|
irGetField(irGet(serialObjectSymbol), property.irField)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,7 +257,7 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
fun IrVariable.get() = irGet(this)
|
fun IrVariable.get() = irGet(this)
|
||||||
|
|
||||||
val inputClass = serializerDescriptor.getClassFromSerializationPackage(STRUCTURE_DECODER_CLASS)
|
val inputClass = serializerDescriptor.getClassFromSerializationPackage(STRUCTURE_DECODER_CLASS)
|
||||||
val descriptorGetterSymbol = compilerContext.symbolTable.referenceFunction(anySerialDescProperty?.getter!!)
|
val descriptorGetterSymbol = compilerContext.localSymbolTable.referenceFunction(anySerialDescProperty?.getter!!) //???
|
||||||
val localSerialDesc = irTemporary(irGet(descriptorGetterSymbol.owner.returnType, irThis(), descriptorGetterSymbol), "desc")
|
val localSerialDesc = irTemporary(irGet(descriptorGetterSymbol.owner.returnType, irThis(), descriptorGetterSymbol), "desc")
|
||||||
|
|
||||||
// workaround due to unavailability of labels (KT-25386)
|
// workaround due to unavailability of labels (KT-25386)
|
||||||
@@ -332,14 +333,14 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
val exceptionCtor =
|
val exceptionCtor =
|
||||||
serializableDescriptor.getClassFromSerializationPackage(UNKNOWN_FIELD_EXC)
|
serializableDescriptor.getClassFromSerializationPackage(UNKNOWN_FIELD_EXC)
|
||||||
.unsubstitutedPrimaryConstructor!!
|
.unsubstitutedPrimaryConstructor!!
|
||||||
val excClassRef = compilerContext.symbolTable.referenceConstructor(exceptionCtor)
|
val excClassRef = compilerContext.externalSymbols.referenceConstructor(exceptionCtor)
|
||||||
+elseBranch(
|
+elseBranch(
|
||||||
irThrow(
|
irThrow(
|
||||||
irInvoke(
|
irInvoke(
|
||||||
null,
|
null,
|
||||||
excClassRef,
|
excClassRef,
|
||||||
indexVar.get(),
|
indexVar.get(),
|
||||||
typeHint = exceptionCtor.returnType.toIrType()
|
typeHint = excClassRef.owner.returnType
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -358,12 +359,12 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
|||||||
// todo: set properties in external deserialization
|
// todo: set properties in external deserialization
|
||||||
var args: List<IrExpression> = localProps.map { it.get() }
|
var args: List<IrExpression> = localProps.map { it.get() }
|
||||||
val ctor: IrConstructorSymbol = if (serializableDescriptor.isInternalSerializable) {
|
val ctor: IrConstructorSymbol = if (serializableDescriptor.isInternalSerializable) {
|
||||||
val ctorDesc = compilerContext.symbolTable.referenceClass(serializableDescriptor)
|
val ctorDesc = compilerContext.externalSymbols.referenceClass(serializableDescriptor)
|
||||||
.owner.constructors.single { it.origin == SERIALIZABLE_PLUGIN_ORIGIN }
|
.owner.constructors.single { it.origin == SERIALIZABLE_PLUGIN_ORIGIN }
|
||||||
args = listOf(irGet(bitMasks[0])) + args + irNull()
|
args = listOf(irGet(bitMasks[0])) + args + irNull()
|
||||||
ctorDesc.symbol
|
ctorDesc.symbol
|
||||||
} else {
|
} else {
|
||||||
compilerContext.symbolTable.referenceConstructor(serializableDescriptor.unsubstitutedPrimaryConstructor!!)
|
compilerContext.externalSymbols.referenceConstructor(serializableDescriptor.unsubstitutedPrimaryConstructor!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
+irReturn(irInvoke(null, ctor, *args.toTypedArray()))
|
+irReturn(irInvoke(null, ctor, *args.toTypedArray()))
|
||||||
|
|||||||
+3
-3
@@ -41,9 +41,9 @@ import org.jetbrains.kotlinx.serialization.compiler.resolve.*
|
|||||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerialEntityNames.SERIAL_DESCRIPTOR_CLASS_IMPL
|
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerialEntityNames.SERIAL_DESCRIPTOR_CLASS_IMPL
|
||||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerialEntityNames.typeArgPrefix
|
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerialEntityNames.typeArgPrefix
|
||||||
|
|
||||||
class SerializerJsTranslator(declaration: KtPureClassOrObject,
|
class SerializerJsTranslator(descriptor: ClassDescriptor,
|
||||||
val translator: DeclarationBodyVisitor,
|
val translator: DeclarationBodyVisitor,
|
||||||
val context: TranslationContext) : SerializerCodegen(declaration, context.bindingContext()) {
|
val context: TranslationContext) : SerializerCodegen(descriptor, context.bindingContext()) {
|
||||||
|
|
||||||
private fun generateFunction(descriptor: FunctionDescriptor, bodyGen: JsBlockBuilder.(JsFunction, TranslationContext) -> Unit) {
|
private fun generateFunction(descriptor: FunctionDescriptor, bodyGen: JsBlockBuilder.(JsFunction, TranslationContext) -> Unit) {
|
||||||
val f = context.buildFunction(descriptor, bodyGen)
|
val f = context.buildFunction(descriptor, bodyGen)
|
||||||
@@ -362,7 +362,7 @@ class SerializerJsTranslator(declaration: KtPureClassOrObject,
|
|||||||
companion object {
|
companion object {
|
||||||
fun translate(declaration: KtPureClassOrObject, descriptor: ClassDescriptor, translator: DeclarationBodyVisitor, context: TranslationContext) {
|
fun translate(declaration: KtPureClassOrObject, descriptor: ClassDescriptor, translator: DeclarationBodyVisitor, context: TranslationContext) {
|
||||||
if (getSerializableClassDescriptorBySerializer(descriptor) != null)
|
if (getSerializableClassDescriptorBySerializer(descriptor) != null)
|
||||||
SerializerJsTranslator(declaration, translator, context).generate()
|
SerializerJsTranslator(descriptor, translator, context).generate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -32,7 +32,7 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
|
|||||||
class SerializerCodegenImpl(
|
class SerializerCodegenImpl(
|
||||||
private val codegen: ImplementationBodyCodegen,
|
private val codegen: ImplementationBodyCodegen,
|
||||||
serializableClass: ClassDescriptor
|
serializableClass: ClassDescriptor
|
||||||
) : SerializerCodegen(codegen.myClass, codegen.bindingContext) {
|
) : SerializerCodegen(codegen.descriptor, codegen.bindingContext) {
|
||||||
|
|
||||||
|
|
||||||
private val serialDescField = "\$\$serialDesc"
|
private val serialDescField = "\$\$serialDesc"
|
||||||
|
|||||||
+2
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlinx.serialization.compiler.extensions
|
package org.jetbrains.kotlinx.serialization.compiler.extensions
|
||||||
|
|
||||||
import com.intellij.mock.MockProject
|
import com.intellij.mock.MockProject
|
||||||
|
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
|
||||||
import org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension
|
import org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension
|
||||||
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
|
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
|
||||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||||
@@ -28,5 +29,6 @@ class SerializationComponentRegistrar : ComponentRegistrar {
|
|||||||
ExpressionCodegenExtension.registerExtension(project, SerializationCodegenExtension())
|
ExpressionCodegenExtension.registerExtension(project, SerializationCodegenExtension())
|
||||||
SyntheticResolveExtension.registerExtension(project, SerializationResolveExtension())
|
SyntheticResolveExtension.registerExtension(project, SerializationResolveExtension())
|
||||||
JsSyntheticTranslateExtension.registerExtension(project, SerializationJsExtension())
|
JsSyntheticTranslateExtension.registerExtension(project, SerializationJsExtension())
|
||||||
|
IrGenerationExtension.registerExtension(project, SerializationLoweringExtension())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+4
-4
@@ -33,9 +33,9 @@ object SerialEntityNames {
|
|||||||
val IMPL_NAME = Name.identifier("Impl")
|
val IMPL_NAME = Name.identifier("Impl")
|
||||||
|
|
||||||
const val ENCODER_CLASS = "Encoder"
|
const val ENCODER_CLASS = "Encoder"
|
||||||
const val STRUCTURE_ENCODER_CLASS = "CompositeEncoder"
|
const val STRUCTURE_ENCODER_CLASS = "StructureEncoder"
|
||||||
const val DECODER_CLASS = "Decoder"
|
const val DECODER_CLASS = "Decoder"
|
||||||
const val STRUCTURE_DECODER_CLASS = "CompositeDecoder"
|
const val STRUCTURE_DECODER_CLASS = "StructureDecoder"
|
||||||
|
|
||||||
const val SERIAL_SAVER_CLASS = "SerializationStrategy"
|
const val SERIAL_SAVER_CLASS = "SerializationStrategy"
|
||||||
const val SERIAL_LOADER_CLASS = "DeserializationStrategy"
|
const val SERIAL_LOADER_CLASS = "DeserializationStrategy"
|
||||||
@@ -75,8 +75,8 @@ object CallingConventions {
|
|||||||
const val decode = "decode"
|
const val decode = "decode"
|
||||||
const val update = "update"
|
const val update = "update"
|
||||||
const val encode = "encode"
|
const val encode = "encode"
|
||||||
const val decodeElementIndex = "decodeElementIndex"
|
const val decodeElementIndex = "decodeElement"
|
||||||
const val elementPostfix = "Element"
|
const val elementPostfix = "ElementValue"
|
||||||
|
|
||||||
const val addElement = "addElement"
|
const val addElement = "addElement"
|
||||||
const val addAnnotation = "pushAnnotation"
|
const val addAnnotation = "pushAnnotation"
|
||||||
|
|||||||
Reference in New Issue
Block a user