diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/IntrinsicsMap.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/IntrinsicsMap.java deleted file mode 100644 index d949e9ffe73..00000000000 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/IntrinsicsMap.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2010-2015 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.codegen.intrinsics; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.descriptors.*; -import org.jetbrains.kotlin.name.FqName; -import org.jetbrains.kotlin.name.FqNameUnsafe; -import org.jetbrains.kotlin.resolve.DescriptorUtils; - -import java.util.HashMap; -import java.util.Map; - -class IntrinsicsMap { - private static final class Key { - private final FqNameUnsafe owner; - private final FqNameUnsafe receiverParameter; - private final String name; - private final int valueParameterCount; - - private Key(@NotNull FqNameUnsafe owner, @Nullable FqNameUnsafe receiverParameter, @NotNull String name, int valueParameterCount) { - this.owner = owner; - this.receiverParameter = receiverParameter; - this.name = name; - this.valueParameterCount = valueParameterCount; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Key key = (Key) o; - - if (valueParameterCount != key.valueParameterCount) return false; - if (!name.equals(key.name)) return false; - if (!owner.equals(key.owner)) return false; - if (receiverParameter == null ? key.receiverParameter != null : !receiverParameter.equals(key.receiverParameter)) return false; - - return true; - } - - @Override - public int hashCode() { - int result = owner.hashCode(); - result = 31 * result + name.hashCode(); - result = 31 * result + valueParameterCount; - result = 31 * result + (receiverParameter != null ? receiverParameter.hashCode() : 0); - return result; - } - } - - private static int valueParameterCountForKey(@NotNull CallableMemberDescriptor member) { - if (member instanceof PropertyDescriptor) { - return -1; - } - else { - return member.getValueParameters().size(); - } - } - - private final Map intrinsicsMap = new HashMap<>(); - - - /** - * @param valueParameterCount -1 for property - */ - public void registerIntrinsic( - @NotNull FqName owner, - @Nullable FqNameUnsafe receiverParameter, - @NotNull String name, - int valueParameterCount, - @NotNull IntrinsicMethod impl - ) { - intrinsicsMap.put(new Key(owner.toUnsafe(), receiverParameter, name, valueParameterCount), impl); - } - - @Nullable - public IntrinsicMethod getIntrinsic(@NotNull CallableMemberDescriptor descriptor) { - Key key = new Key( - DescriptorUtils.getFqName(descriptor.getContainingDeclaration()), - getReceiverParameterFqName(descriptor), - descriptor.getName().asString(), - valueParameterCountForKey(descriptor) - ); - return intrinsicsMap.get(key); - } - - @Nullable - private static FqNameUnsafe getReceiverParameterFqName(@NotNull CallableMemberDescriptor descriptor) { - ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter(); - if (receiverParameter == null) return null; - - ClassifierDescriptor classifier = receiverParameter.getType().getConstructor().getDeclarationDescriptor(); - if (classifier == null) return null; - - if (classifier instanceof TypeParameterDescriptor) { - return IntrinsicMethods.RECEIVER_PARAMETER_FQ_NAME; - } - - return DescriptorUtils.getFqName(classifier); - } -} diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/IntrinsicsMap.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/IntrinsicsMap.kt new file mode 100644 index 00000000000..271df50e1a9 --- /dev/null +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/IntrinsicsMap.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2010-2015 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.codegen.intrinsics + +import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.FqNameUnsafe +import org.jetbrains.kotlin.resolve.DescriptorUtils +import java.util.* + +internal class IntrinsicsMap { + + private val intrinsicsMap = HashMap() + + private data class Key( + private val owner: FqNameUnsafe, + private val receiverParameter: FqNameUnsafe?, + private val name: String, + private val valueParameterCount: Int + ) + + private fun valueParameterCountForKey(member: CallableMemberDescriptor): Int = + if (member is PropertyDescriptor) + -1 + else + member.valueParameters.size + + + /** + * @param valueParameterCount -1 for property + */ + fun registerIntrinsic( + owner: FqName, + receiverParameter: FqNameUnsafe?, + name: String, + valueParameterCount: Int, + impl: IntrinsicMethod + ) { + intrinsicsMap[Key(owner.toUnsafe(), receiverParameter, name, valueParameterCount)] = impl + } + + fun getIntrinsic(descriptor: CallableMemberDescriptor): IntrinsicMethod? = + intrinsicsMap[Key( + DescriptorUtils.getFqName(descriptor.containingDeclaration), + getReceiverParameterFqName(descriptor), + descriptor.name.asString(), + valueParameterCountForKey(descriptor) + )] + + private fun getReceiverParameterFqName(descriptor: CallableMemberDescriptor): FqNameUnsafe? { + val receiverParameter = descriptor.extensionReceiverParameter ?: return null + val classifier = receiverParameter.type.constructor.declarationDescriptor ?: return null + + return if (classifier is TypeParameterDescriptor) + IntrinsicMethods.RECEIVER_PARAMETER_FQ_NAME + else + DescriptorUtils.getFqName(classifier) + } +}