From 3f7050635cab8af4750d3698954c1673b92cb009 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Wed, 23 Jul 2014 17:12:08 +0400 Subject: [PATCH] Builtins: check for function and extension function types by fq names As opposed to checking by descriptor --- .../jet/lang/types/lang/KotlinBuiltIns.java | 63 +++++++++++-------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/lang/KotlinBuiltIns.java b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/KotlinBuiltIns.java index 760e92ee563..892d60c30b3 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/lang/KotlinBuiltIns.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/KotlinBuiltIns.java @@ -97,9 +97,6 @@ public class KotlinBuiltIns { private volatile ImmutableSet nonPhysicalClasses; - private final ImmutableSet functionClassesSet; - private final ImmutableSet extensionFunctionClassesSet; - private final Map primitiveTypeToNullableJetType; private final Map primitiveTypeToArrayJetType; private final Map primitiveJetTypeToJetArrayType; @@ -114,9 +111,6 @@ public class KotlinBuiltIns { builtinsPackageFragment = new BuiltinsPackageFragment(new LockBasedStorageManager(), builtInsModule); builtInsModule.addFragmentProvider(DependencyKind.SOURCES, builtinsPackageFragment.getProvider()); - functionClassesSet = computeIndexedClasses("Function", FUNCTION_TRAIT_COUNT); - extensionFunctionClassesSet = computeIndexedClasses("ExtensionFunction", FUNCTION_TRAIT_COUNT); - primitiveTypeToNullableJetType = new EnumMap(PrimitiveType.class); primitiveTypeToArrayJetType = new EnumMap(PrimitiveType.class); primitiveJetTypeToJetArrayType = new HashMap(); @@ -146,10 +140,23 @@ public class KotlinBuiltIns { public final FqNameUnsafe nothing = fqName("Nothing"); public final FqNameUnsafe suppress = fqName("suppress"); + public final ImmutableSet functionClasses = computeIndexedFqNames("Function", FUNCTION_TRAIT_COUNT); + public final ImmutableSet extensionFunctionClasses = + computeIndexedFqNames("ExtensionFunction", FUNCTION_TRAIT_COUNT); + @NotNull private static FqNameUnsafe fqName(@NotNull String simpleName) { return BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier(simpleName)).toUnsafe(); } + + @NotNull + private static ImmutableSet computeIndexedFqNames(@NotNull String prefix, int count) { + ImmutableSet.Builder builder = ImmutableSet.builder(); + for (int i = 0; i < count; i++) { + builder.add(fqName(prefix + i)); + } + return builder.build(); + } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -708,22 +715,13 @@ public class KotlinBuiltIns { // Functions - @NotNull - private ImmutableSet computeIndexedClasses(@NotNull String prefix, int count) { - ImmutableSet.Builder builder = ImmutableSet.builder(); - for (int i = 0; i < count; i++) { - builder.add(getBuiltInClassByName(prefix + i)); - } - return builder.build(); - } - public boolean isFunctionOrExtensionFunctionType(@NotNull JetType type) { return isFunctionType(type) || isExtensionFunctionType(type); } public boolean isFunctionType(@NotNull JetType type) { if (type instanceof PackageType) return false; - if (setContainsClassOf(functionClassesSet, type)) return true; + if (isExactFunctionType(type)) return true; for (JetType superType : type.getConstructor().getSupertypes()) { if (isFunctionType(superType)) return true; @@ -732,14 +730,9 @@ public class KotlinBuiltIns { return false; } - public boolean isExactFunctionOrExtensionFunctionType(@NotNull JetType type) { - return !(type instanceof PackageType) - && (setContainsClassOf(extensionFunctionClassesSet, type) || setContainsClassOf(functionClassesSet, type)); - } - public boolean isExtensionFunctionType(@NotNull JetType type) { if (type instanceof PackageType) return false; - if (setContainsClassOf(extensionFunctionClassesSet, type)) return true; + if (isExactExtensionFunctionType(type)) return true; for (JetType superType : type.getConstructor().getSupertypes()) { if (isExtensionFunctionType(superType)) return true; @@ -748,6 +741,27 @@ public class KotlinBuiltIns { return false; } + public boolean isExactFunctionOrExtensionFunctionType(@NotNull JetType type) { + return isExactFunctionType(type) || isExactExtensionFunctionType(type); + } + + public boolean isExactFunctionType(@NotNull JetType type) { + return isTypeConstructorFqNameInSet(type, fqNames.functionClasses); + } + + public boolean isExactExtensionFunctionType(@NotNull JetType type) { + return isTypeConstructorFqNameInSet(type, fqNames.extensionFunctionClasses); + } + + private static boolean isTypeConstructorFqNameInSet(@NotNull JetType type, @NotNull ImmutableSet classes) { + ClassifierDescriptor declarationDescriptor = type.getConstructor().getDeclarationDescriptor(); + + if (declarationDescriptor == null) return false; + + FqNameUnsafe fqName = DescriptorUtils.getFqName(declarationDescriptor); + return classes.contains(fqName); + } + @Nullable public JetType getReceiverType(@NotNull JetType type) { assert isFunctionOrExtensionFunctionType(type) : type; @@ -855,9 +869,4 @@ public class KotlinBuiltIns { public JetType getDefaultBound() { return getNullableAnyType(); } - - private static boolean setContainsClassOf(ImmutableSet set, JetType type) { - //noinspection SuspiciousMethodCalls - return set.contains(type.getConstructor().getDeclarationDescriptor()); - } }