Shrinking IR: introduce IrBuiltIns (as a container for special operator descriptors)

This commit is contained in:
Dmitry Petrov
2016-08-19 18:17:48 +03:00
committed by Dmitry Petrov
parent 70dfb75f82
commit c36a0d04ce
7 changed files with 228 additions and 26 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi2ir.generators
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.declarations.IrModuleImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.PsiSourceManager
import org.jetbrains.kotlin.resolve.BindingContext
@@ -30,5 +31,6 @@ class GeneratorContext(
val sourceManager = PsiSourceManager()
val builtIns: KotlinBuiltIns get() = moduleDescriptor.builtIns
val irBuiltIns = IrBuiltIns(builtIns)
}
@@ -24,7 +24,8 @@ class ModuleGenerator(override val context: GeneratorContext) : IrGenerator {
fun generateModule(ktFiles: List<KtFile>): IrModule {
val irDeclarationGenerator = DeclarationGenerator(context)
val irModule = IrModuleImpl(context.moduleDescriptor)
val irModule = IrModuleImpl(context.moduleDescriptor, context.irBuiltIns)
for (ktFile in ktFiles) {
val fileEntry = context.sourceManager.getOrCreateFileEntry(ktFile)
val packageFragmentDescriptor = getOrFail(BindingContext.FILE_TO_PACKAGE_FRAGMENT, ktFile)
@@ -1,23 +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.ir
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
class IrBuiltIns(val builtIns: KotlinBuiltIns) {
}
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.ir.declarations
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.*
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import java.util.*
@@ -32,14 +33,15 @@ interface IrModule : IrElement {
}
val descriptor: ModuleDescriptor
val irBuiltins: IrBuiltIns
val files: List<IrFile>
fun addFile(file: IrFile)
}
class IrModuleImpl(
override val descriptor: ModuleDescriptor
override val descriptor: ModuleDescriptor,
override val irBuiltins: IrBuiltIns
) : IrModule {
override val files: MutableList<IrFile> = ArrayList()
@@ -0,0 +1,54 @@
/*
* 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.ir.descriptors
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.KotlinType
class IrBuiltIns(val builtIns: KotlinBuiltIns) {
val packageFragment = IrBuiltinsPackageFragmentDescriptorImpl(builtIns.builtInsModule)
val eqeqeq: IrBuiltinOperatorDescriptor
val eqeq: IrBuiltinOperatorDescriptor
val lt0: IrBuiltinOperatorDescriptor
val lteq0: IrBuiltinOperatorDescriptor
val gt0: IrBuiltinOperatorDescriptor
val gteq0: IrBuiltinOperatorDescriptor
init {
val bool = builtIns.booleanType
val anyN = builtIns.nullableAnyType
val int = builtIns.intType
eqeqeq = defineOperator("EQEQEQ", bool, listOf(anyN, anyN))
eqeq = defineOperator("EQEQ", bool, listOf(anyN, anyN))
lt0 = defineOperator("LT0", bool, listOf(int))
lteq0 = defineOperator("LTEQ0", bool, listOf(int))
gt0 = defineOperator("GT0", bool, listOf(int))
gteq0 = defineOperator("GTEQ0", bool, listOf(int))
}
private fun defineOperator(name: String, returnType: KotlinType, valueParameterTypes: List<KotlinType>): IrBuiltinOperatorDescriptor {
val operatorDescriptor = IrSimpleBuiltinOperatorDescriptorImpl(packageFragment, Name.identifier(name), returnType)
for ((i, valueParameterType) in valueParameterTypes.withIndex()) {
operatorDescriptor.addValueParameter(
IrBuiltinValueParameterDescriptorImpl(operatorDescriptor, Name.identifier("arg$i"), i, valueParameterType))
}
return operatorDescriptor
}
}
@@ -0,0 +1,116 @@
/*
* 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.ir.descriptors
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorNonRootImpl
import org.jetbrains.kotlin.descriptors.impl.VariableDescriptorImpl
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.constants.ConstantValue
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSubstitutor
import java.util.*
interface IrBuiltinOperatorDescriptor : FunctionDescriptor
interface IrBuiltinValueParameterDescriptor : ValueParameterDescriptor
abstract class IrBuiltinOperatorDescriptorBase(containingDeclaration: DeclarationDescriptor, name: Name) :
DeclarationDescriptorNonRootImpl(containingDeclaration, Annotations.EMPTY, name, SourceElement.NO_SOURCE),
IrBuiltinOperatorDescriptor
{
override fun getDispatchReceiverParameter(): ReceiverParameterDescriptor? = null
override fun getExtensionReceiverParameter(): ReceiverParameterDescriptor? = null
override fun getOriginal(): FunctionDescriptor = this
override fun substitute(substitutor: TypeSubstitutor): FunctionDescriptor = throw UnsupportedOperationException()
override fun getOverriddenDescriptors(): Collection<FunctionDescriptor> = emptyList()
override fun setOverriddenDescriptors(overriddenDescriptors: Collection<CallableMemberDescriptor>) = throw UnsupportedOperationException()
override fun getTypeParameters(): List<TypeParameterDescriptor> = emptyList()
override fun getVisibility(): Visibility = Visibilities.PUBLIC
override fun getModality(): Modality = Modality.FINAL
override fun getKind(): CallableMemberDescriptor.Kind = CallableMemberDescriptor.Kind.SYNTHESIZED
override fun getInitialSignatureDescriptor(): FunctionDescriptor? = null
override fun isExternal(): Boolean = false
override fun <V : Any> getUserData(key: FunctionDescriptor.UserDataKey<V>?): V? = null
override fun isHiddenForResolutionEverywhereBesideSupercalls(): Boolean = false
override fun isHiddenToOvercomeSignatureClash(): Boolean = false
override fun isInfix(): Boolean = false
override fun isInline(): Boolean = false
override fun isOperator(): Boolean = false
override fun isSuspend(): Boolean = false
override fun isTailrec(): Boolean = false
override fun hasStableParameterNames(): Boolean = true
override fun hasSynthesizedParameterNames(): Boolean = false
override fun copy(newOwner: DeclarationDescriptor?, modality: Modality?, visibility: Visibility?, kind: CallableMemberDescriptor.Kind?, copyOverrides: Boolean): FunctionDescriptor =
throw UnsupportedOperationException()
override fun newCopyBuilder(): FunctionDescriptor.CopyBuilder<out FunctionDescriptor> =
throw UnsupportedOperationException()
override fun <R : Any?, D : Any?> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R {
return visitor.visitFunctionDescriptor(this, data)
}
}
class IrSimpleBuiltinOperatorDescriptorImpl(
containingDeclaration: DeclarationDescriptor,
name: Name,
private val returnType: KotlinType
) : IrBuiltinOperatorDescriptorBase(containingDeclaration, name), IrBuiltinOperatorDescriptor {
private val valueParameters: MutableList<IrBuiltinValueParameterDescriptor> = ArrayList()
fun addValueParameter(valueParameter: IrBuiltinValueParameterDescriptor) {
valueParameters.add(valueParameter)
}
override fun getReturnType(): KotlinType = returnType
override fun getValueParameters(): List<ValueParameterDescriptor> = valueParameters
}
class IrBuiltinValueParameterDescriptorImpl(
private val containingDeclaration: CallableDescriptor,
name: Name,
override val index: Int,
outType: KotlinType
) : VariableDescriptorImpl(containingDeclaration, Annotations.EMPTY, name, outType, SourceElement.NO_SOURCE),
IrBuiltinValueParameterDescriptor {
override fun getContainingDeclaration(): CallableDescriptor = containingDeclaration
override fun declaresDefaultValue(): Boolean = false
override fun getOriginal(): ValueParameterDescriptor = this
override fun getOverriddenDescriptors(): Collection<ValueParameterDescriptor> = emptyList()
override val isCoroutine: Boolean get() = false
override val isCrossinline: Boolean get() = false
override val isNoinline: Boolean get() = false
override val varargElementType: KotlinType? get() = null
override fun getCompileTimeInitializer(): ConstantValue<*>? = null
override fun isVar(): Boolean = false
override fun getVisibility(): Visibility = Visibilities.LOCAL
override fun copy(newOwner: CallableDescriptor, newName: Name, newIndex: Int): ValueParameterDescriptor =
throw UnsupportedOperationException()
override fun substitute(substitutor: TypeSubstitutor): ValueParameterDescriptor =
throw UnsupportedOperationException()
override fun <R : Any?, D : Any?> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R {
return visitor.visitValueParameterDescriptor(this, data)
}
}
@@ -0,0 +1,50 @@
/*
* 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.ir.descriptors
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.TypeSubstitutor
interface IrBuiltinsPackageFragmentDescriptor : PackageFragmentDescriptor
class IrBuiltinsPackageFragmentDescriptorImpl(val containingModule: ModuleDescriptor) : IrBuiltinsPackageFragmentDescriptor {
override val fqName: FqName get() = FqName("kotlin.internal.ir")
override fun getName(): Name = Name.identifier("ir")
override fun getContainingDeclaration(): ModuleDescriptor = containingModule
override fun getMemberScope(): MemberScope = MemberScope.Empty
override fun getOriginal(): DeclarationDescriptorWithSource = this
override fun getSource(): SourceElement = SourceElement.NO_SOURCE
override val annotations: Annotations = Annotations.EMPTY
override fun substitute(substitutor: TypeSubstitutor): DeclarationDescriptor = throw UnsupportedOperationException()
override fun <R : Any?, D : Any?> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R {
return visitor.visitPackageFragmentDescriptor(this, data)
}
override fun acceptVoid(visitor: DeclarationDescriptorVisitor<Void, Void>) {
visitor.visitPackageFragmentDescriptor(this, null)
}
}