Skip to content

Commit

Permalink
Use SerializerFactory interface and generate function
Browse files Browse the repository at this point in the history
for sealed and abstract serializable classes.

Fixes Kotlin/kotlinx.serialization#1116
Fixes Kotlin/kotlinx.serialization#1078
  • Loading branch information
sandwwraith committed May 6, 2021
1 parent 9baa24e commit 11df5b5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class SerializableCodegen(
}

private inline fun ClassDescriptor.shouldHaveSpecificSyntheticMethods(functionPresenceChecker: () -> FunctionDescriptor?) =
!isInlineClass() && (isAbstractSerializableClass() || isSealedSerializableClass() || functionPresenceChecker() != null)
!isInlineClass() && (isAbstractOrSealedSerializableClass() || functionPresenceChecker() != null)

private fun generateSyntheticInternalConstructor() {
val serializerDescriptor = serializableDescriptor.classSerializer ?: return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package org.jetbrains.kotlinx.serialization.compiler.backend.common
Expand Down Expand Up @@ -44,7 +33,7 @@ abstract class SerializableCompanionCodegen(
"probably clash with user-defined function has occurred"
)

if (serializableDescriptor.isSerializableObject || serializableDescriptor.isSealedSerializableClass() || serializableDescriptor.isAbstractSerializableClass()) {
if (serializableDescriptor.isSerializableObject || serializableDescriptor.isAbstractOrSealedSerializableClass()) {
generateLazySerializerGetter(serializerGetterDescriptor)
} else {
generateSerializerGetter(serializerGetterDescriptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class SerializableIrGenerator(

if (useFieldMissingOptimization() &&
// for abstract classes fields MUST BE checked in child classes
!serializableDescriptor.isAbstractSerializableClass() && !serializableDescriptor.isSealedSerializableClass()
!serializableDescriptor.isAbstractOrSealedSerializableClass()
) {
val getDescriptorExpr = if (serializableDescriptor.isStaticSerializable) {
getStaticSerialDescriptorExpr()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2021 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package org.jetbrains.kotlinx.serialization.compiler.backend.jvm
Expand Down Expand Up @@ -285,7 +274,7 @@ class SerializableCodegenImpl(
}

private fun InstructionAdapter.generateOptimizedGoldenMaskCheck(maskVar: Int) {
if (serializableDescriptor.isAbstractSerializableClass() || serializableDescriptor.isSealedSerializableClass()) {
if (serializableDescriptor.isAbstractOrSealedSerializableClass()) {
// for abstract classes fields MUST BE checked in child classes
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package org.jetbrains.kotlinx.serialization.compiler.resolve
Expand Down Expand Up @@ -156,17 +145,14 @@ internal fun Annotated.findSerializableAnnotationDeclaration(): KtAnnotationEntr
// For abstract classes marked with @Serializable,
// methods are generated anyway although they shouldn't have
// generated $serializer and use Polymorphic one.
internal fun ClassDescriptor.isAbstractSerializableClass(): Boolean =
isInternalSerializable && modality == Modality.ABSTRACT

internal fun ClassDescriptor.isSealedSerializableClass(): Boolean =
isInternalSerializable && modality == Modality.SEALED
internal fun ClassDescriptor.isAbstractOrSealedSerializableClass(): Boolean =
isInternalSerializable && (modality == Modality.ABSTRACT || modality == Modality.SEALED)

internal fun ClassDescriptor.polymorphicSerializerIfApplicableAutomatically(): ClassDescriptor? {
val serializer = when {
this.isAbstractSerializableClass()
|| kind == ClassKind.INTERFACE -> SpecialBuiltins.polymorphicSerializer
this.isSealedSerializableClass() -> SpecialBuiltins.sealedSerializer
kind == ClassKind.INTERFACE -> SpecialBuiltins.polymorphicSerializer
isInternalSerializable && modality == Modality.ABSTRACT -> SpecialBuiltins.polymorphicSerializer
isInternalSerializable && modality == Modality.SEALED -> SpecialBuiltins.sealedSerializer
else -> null
}
return serializer?.let { module.getClassFromSerializationPackage(it) }
Expand Down Expand Up @@ -217,6 +203,7 @@ internal fun ClassDescriptor.needSerializerFactory(): Boolean {
if (!(this.platform?.isNative() == true || this.platform.isJs())) return false
val serializableClass = getSerializableClassDescriptorByCompanion(this) ?: return false
if (serializableClass.isSerializableObject) return true
if (serializableClass.isAbstractOrSealedSerializableClass()) return true
if (serializableClass.declaredTypeParameters.isEmpty()) return false
return true
}
Expand Down

0 comments on commit 11df5b5

Please sign in to comment.