diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/GeneratorContext.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/GeneratorContext.kt index ad1a12417d7..afa8c68da0f 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/GeneratorContext.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/GeneratorContext.kt @@ -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) } diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleGenerator.kt index 88189311927..baa47510498 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleGenerator.kt @@ -24,7 +24,8 @@ class ModuleGenerator(override val context: GeneratorContext) : IrGenerator { fun generateModule(ktFiles: List): 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) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/IrBuiltIns.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/IrBuiltIns.kt deleted file mode 100644 index 06cda0a2c92..00000000000 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/IrBuiltIns.kt +++ /dev/null @@ -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) { - -} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrModule.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrModule.kt index e34ef4a93b3..f900b7d723e 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrModule.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrModule.kt @@ -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 fun addFile(file: IrFile) } class IrModuleImpl( - override val descriptor: ModuleDescriptor + override val descriptor: ModuleDescriptor, + override val irBuiltins: IrBuiltIns ) : IrModule { override val files: MutableList = ArrayList() diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltIns.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltIns.kt new file mode 100644 index 00000000000..12062b50888 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltIns.kt @@ -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): 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 + } +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltinFunctionDescriptor.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltinFunctionDescriptor.kt new file mode 100644 index 00000000000..cc2edde3fce --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltinFunctionDescriptor.kt @@ -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 = emptyList() + override fun setOverriddenDescriptors(overriddenDescriptors: Collection) = throw UnsupportedOperationException() + override fun getTypeParameters(): List = 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 getUserData(key: FunctionDescriptor.UserDataKey?): 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 = + throw UnsupportedOperationException() + + override fun accept(visitor: DeclarationDescriptorVisitor, 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 = ArrayList() + + fun addValueParameter(valueParameter: IrBuiltinValueParameterDescriptor) { + valueParameters.add(valueParameter) + } + + override fun getReturnType(): KotlinType = returnType + override fun getValueParameters(): List = 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 = 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 accept(visitor: DeclarationDescriptorVisitor, data: D): R { + return visitor.visitValueParameterDescriptor(this, data) + } +} \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltinsPackageFragmentDescriptor.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltinsPackageFragmentDescriptor.kt new file mode 100644 index 00000000000..34e55d30b28 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltinsPackageFragmentDescriptor.kt @@ -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 accept(visitor: DeclarationDescriptorVisitor, data: D): R { + return visitor.visitPackageFragmentDescriptor(this, data) + } + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor) { + visitor.visitPackageFragmentDescriptor(this, null) + } +} + +