From 8d7c20133966bef9345e959c0348aa1620b73e6c Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 7 Oct 2016 17:32:26 +0300 Subject: [PATCH] Refactor special descriptors. --- .../backend/jvm/codegen/ClassCodegen.kt | 4 +- .../DefaultImplsClassDescriptor.kt | 40 ++++++++++ ...ptor.kt => JvmDescriptorWithExtraFlags.kt} | 12 ++- .../jvm/lower/DefaultImplsClassDescriptor.kt | 80 ------------------- .../jvm/lower/InterfaceDelegationLowering.kt | 2 + .../backend/jvm/lower/InterfaceLowering.kt | 1 + 6 files changed, 54 insertions(+), 85 deletions(-) create mode 100644 compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/DefaultImplsClassDescriptor.kt rename compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/{JvmSpecialDescriptor.kt => JvmDescriptorWithExtraFlags.kt} (87%) delete mode 100644 compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/DefaultImplsClassDescriptor.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt index 298e4bd2da0..89b2364fcca 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.backend.jvm.codegen import com.intellij.psi.PsiElement import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin -import org.jetbrains.kotlin.backend.jvm.descriptors.JvmSpecialDescriptor +import org.jetbrains.kotlin.backend.jvm.descriptors.JvmDescriptorWithExtraFlags import org.jetbrains.kotlin.backend.jvm.descriptors.FileClassDescriptor import org.jetbrains.kotlin.codegen.* import org.jetbrains.kotlin.codegen.MemberCodegen.badDescriptor @@ -207,7 +207,7 @@ fun MemberDescriptor.calculateCommonFlags(): Int { flags = flags.or(calcModalityFlag()) - if (this is JvmSpecialDescriptor) { + if (this is JvmDescriptorWithExtraFlags) { flags = flags or extraFlags } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/DefaultImplsClassDescriptor.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/DefaultImplsClassDescriptor.kt new file mode 100644 index 00000000000..dbd81694b37 --- /dev/null +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/DefaultImplsClassDescriptor.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2010-2016 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. + */ + +package org.jetbrains.kotlin.backend.jvm.descriptors + +import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe +import org.jetbrains.kotlin.resolve.descriptorUtil.module +import org.jetbrains.kotlin.resolve.scopes.MemberScope +import org.jetbrains.kotlin.types.* + +interface DefaultImplsClassDescriptor : ClassDescriptor { + val correspondingInterface: ClassDescriptor +} + +class DefaultImplsClassDescriptorImpl( + name: Name, + override val correspondingInterface: ClassDescriptor, + sourceElement: SourceElement +) : DefaultImplsClassDescriptor, KnownClassDescriptor(name, correspondingInterface, sourceElement, ClassKind.CLASS, Modality.FINAL, Visibilities.PUBLIC, Annotations.EMPTY) { + init { + initialize(emptyList(), listOf(correspondingInterface.module.builtIns.anyType)) + } +} \ No newline at end of file diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmSpecialDescriptor.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDescriptorWithExtraFlags.kt similarity index 87% rename from compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmSpecialDescriptor.kt rename to compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDescriptorWithExtraFlags.kt index 510d79dc196..fdb4c3ca229 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmSpecialDescriptor.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDescriptorWithExtraFlags.kt @@ -22,7 +22,7 @@ import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.KotlinType -interface JvmSpecialDescriptor { +interface JvmDescriptorWithExtraFlags { val extraFlags: Int } @@ -39,11 +39,17 @@ class JvmPropertyDescriptorImpl( source: SourceElement, isLateInit: Boolean, isConst: Boolean -) : JvmSpecialDescriptor, PropertyDescriptorImpl( +) : JvmDescriptorWithExtraFlags, PropertyDescriptorImpl( containingDeclaration, original, annotations, modality, visibility, isVar, name, kind, source, isLateInit, isConst ) { - override fun createSubstitutedCopy(newOwner: DeclarationDescriptor, newModality: Modality, newVisibility: Visibility, original: PropertyDescriptor?, kind: CallableMemberDescriptor.Kind): PropertyDescriptorImpl = + override fun createSubstitutedCopy( + newOwner: DeclarationDescriptor, + newModality: Modality, + newVisibility: Visibility, + original: PropertyDescriptor?, + kind: CallableMemberDescriptor.Kind + ): PropertyDescriptorImpl = JvmPropertyDescriptorImpl( newOwner, original, annotations, newModality, newVisibility, extraFlags, isVar, name, kind, SourceElement.NO_SOURCE, isLateInit, isConst diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/DefaultImplsClassDescriptor.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/DefaultImplsClassDescriptor.kt deleted file mode 100644 index 4b47b5766f6..00000000000 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/DefaultImplsClassDescriptor.kt +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2010-2016 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. - */ - -package org.jetbrains.kotlin.backend.jvm.lower - -import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns -import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe -import org.jetbrains.kotlin.resolve.scopes.MemberScope -import org.jetbrains.kotlin.types.* - -interface DefaultImplsClassDescriptor : ClassDescriptor { - val correspondingInterface: ClassDescriptor -} - -class DefaultImplsClassDescriptorImpl( - private val nameImpl: Name, - override val correspondingInterface: ClassDescriptor, - private val sourceElement: SourceElement -) : DefaultImplsClassDescriptor { - override fun getCompanionObjectDescriptor(): ClassDescriptor? = null - override fun getConstructors(): Collection = emptyList() - override fun getContainingDeclaration(): DeclarationDescriptor = correspondingInterface - override fun getDeclaredTypeParameters(): List = emptyList() - override fun getKind(): ClassKind = ClassKind.CLASS - - override fun getMemberScope(typeSubstitution: TypeSubstitution): MemberScope = error("File class has no member scope") - override fun getModality(): Modality = Modality.FINAL - override fun getOriginal(): ClassDescriptor = this - override fun getName(): Name = nameImpl - override fun getStaticScope(): MemberScope = error("File class has no static scope") - override fun getThisAsReceiverParameter(): ReceiverParameterDescriptor = error("File class has no instances") - override fun getUnsubstitutedInnerClassesScope(): MemberScope = error("File class has no inner classes scope") - override fun getUnsubstitutedMemberScope(): MemberScope = error("File class has no member scope") - override fun getUnsubstitutedPrimaryConstructor(): ClassConstructorDescriptor? = null - override fun getVisibility(): Visibility = Visibilities.PUBLIC - override fun isCompanionObject(): Boolean = false - override fun isData(): Boolean = false - override fun substitute(substitutor: TypeSubstitutor): ClassDescriptor = error("File class can't be substituted") - override fun getSource(): SourceElement = sourceElement - override fun isInner(): Boolean = false - - override val annotations = Annotations.EMPTY - - private val typeConstructor = ClassTypeConstructorImpl(this, annotations, true, emptyList(), listOf(builtIns.anyType)) - private val defaultType = KotlinTypeFactory.simpleNotNullType(annotations, this, emptyList()) - - override fun getTypeConstructor(): TypeConstructor = typeConstructor - - override fun getDefaultType(): SimpleType = defaultType - - override fun getMemberScope(typeArguments: MutableList): MemberScope = - MemberScope.Empty // TODO do we need a more useful MemberScope here? - - override fun accept(visitor: DeclarationDescriptorVisitor, data: D): R { - return visitor.visitClassDescriptor(this, data) - } - - override fun acceptVoid(visitor: DeclarationDescriptorVisitor) { - visitor.visitClassDescriptor(this, null) - } - - override fun toString(): String = - "IrFileClassDescriptor($fqNameUnsafe)" -} \ No newline at end of file diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceDelegationLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceDelegationLowering.kt index 6f7d74837a8..2faff6a2e17 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceDelegationLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceDelegationLowering.kt @@ -20,6 +20,8 @@ import org.jetbrains.kotlin.backend.common.CodegenUtil import org.jetbrains.kotlin.backend.jvm.ClassLoweringPass import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.JvmLoweredStatementOrigin +import org.jetbrains.kotlin.backend.jvm.descriptors.DefaultImplsClassDescriptor +import org.jetbrains.kotlin.backend.jvm.descriptors.DefaultImplsClassDescriptorImpl import org.jetbrains.kotlin.codegen.JvmCodegenUtil import org.jetbrains.kotlin.codegen.isDefinitelyNotDefaultImplsMethod import org.jetbrains.kotlin.codegen.state.GenerationState diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt index 71990aa4e70..8753f6338b2 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.backend.jvm.lower import org.jetbrains.kotlin.backend.jvm.ClassLoweringPass import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin +import org.jetbrains.kotlin.backend.jvm.descriptors.DefaultImplsClassDescriptorImpl import org.jetbrains.kotlin.backend.jvm.lower.InitializersLowering.Companion.clinitName import org.jetbrains.kotlin.codegen.AsmUtil import org.jetbrains.kotlin.codegen.state.GenerationState