From 16daeb9544c6ac89a0b777569c4babf1f3beb859 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 29 Apr 2014 20:15:10 +0400 Subject: [PATCH] Misc refactorings in KotlinBuiltIns - delete fields which were supposed to be used for caching, but mistakenly were never used - delete/inline unused methods and constants - delete useless logic related to root package fragment: package fragment providers are only supposed to find packages they're aware of --- .../jetbrains/jet/asJava/LightClassUtil.java | 4 +- .../jet/lang/types/CommonSupertypes.java | 2 +- .../types/lang/BuiltinsPackageFragment.java | 24 ++++-- .../jet/lang/types/lang/KotlinBuiltIns.java | 77 ++++--------------- .../references/BuiltInsReferenceResolver.java | 3 +- 5 files changed, 36 insertions(+), 74 deletions(-) diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassUtil.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassUtil.java index 8331e219edf..e379db6e8f5 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassUtil.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassUtil.java @@ -52,7 +52,7 @@ import java.util.*; public class LightClassUtil { private static final Logger LOG = Logger.getInstance(LightClassUtil.class); - public static final File BUILT_INS_SRC_DIR = new File("core/builtins/native", KotlinBuiltIns.BUILT_INS_PACKAGE_NAME_STRING); + public static final File BUILT_INS_SRC_DIR = new File("core/builtins/native", KotlinBuiltIns.BUILT_INS_PACKAGE_NAME.asString()); /** * Checks whether the given file is loaded from the location where Kotlin's built-in classes are defined. @@ -85,7 +85,7 @@ public class LightClassUtil { @NotNull public static URL getBuiltInsDirUrl() { - String builtInFilePath = "/" + KotlinBuiltIns.BUILT_INS_PACKAGE_NAME_STRING + "/Library.kt"; + String builtInFilePath = "/" + KotlinBuiltIns.BUILT_INS_PACKAGE_NAME + "/Library.kt"; URL url = KotlinBuiltIns.class.getResource(builtInFilePath); diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java b/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java index 3e55e4b2adf..4c2eb28cd2c 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java @@ -164,7 +164,7 @@ public class CommonSupertypes { } // TODO : attributes? - JetScope newScope = KotlinBuiltIns.getInstance().STUB; + JetScope newScope = JetScope.EMPTY; DeclarationDescriptor declarationDescriptor = constructor.getDeclarationDescriptor(); if (declarationDescriptor instanceof ClassDescriptor) { newScope = ((ClassDescriptor) declarationDescriptor).getMemberScope(newProjections); diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/lang/BuiltinsPackageFragment.java b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/BuiltinsPackageFragment.java index ebebbcd650b..99851bd0752 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/lang/BuiltinsPackageFragment.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/BuiltinsPackageFragment.java @@ -1,3 +1,19 @@ +/* + * Copyright 2010-2014 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.jet.lang.types.lang; import kotlin.Function0; @@ -11,7 +27,6 @@ import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor; import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptorImpl; import org.jetbrains.jet.lang.descriptors.PackageFragmentProvider; -import org.jetbrains.jet.lang.descriptors.impl.MutablePackageFragmentDescriptor; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.resolve.scopes.JetScope; @@ -80,15 +95,10 @@ class BuiltinsPackageFragment extends PackageFragmentDescriptorImpl { } private class BuiltinsPackageFragmentProvider implements PackageFragmentProvider { - private final PackageFragmentDescriptor rootPackage = new MutablePackageFragmentDescriptor(getContainingDeclaration(), FqName.ROOT); - @NotNull @Override public List getPackageFragments(@NotNull FqName fqName) { - if (fqName.isRoot()) { - return Collections.singletonList(rootPackage); - } - else if (KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME.equals(fqName)) { + if (KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME.equals(fqName)) { return Collections.singletonList(BuiltinsPackageFragment.this); } return Collections.emptyList(); 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 3cb75893ca9..2239c21fc9a 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 @@ -38,10 +38,7 @@ import java.util.*; import static org.jetbrains.jet.lang.types.lang.PrimitiveType.*; public class KotlinBuiltIns { - public static final JetScope STUB = JetScope.EMPTY; - - public static final String BUILT_INS_PACKAGE_NAME_STRING = "kotlin"; - public static final Name BUILT_INS_PACKAGE_NAME = Name.identifier(BUILT_INS_PACKAGE_NAME_STRING); + public static final Name BUILT_INS_PACKAGE_NAME = Name.identifier("kotlin"); public static final FqName BUILT_INS_PACKAGE_FQ_NAME = FqName.topLevel(BUILT_INS_PACKAGE_NAME); public static final int FUNCTION_TRAIT_COUNT = 23; @@ -99,68 +96,30 @@ public class KotlinBuiltIns { private volatile ImmutableSet nonPhysicalClasses; private final ImmutableSet functionClassesSet; - private final ImmutableSet extensionFunctionClassesSet; - private final EnumMap primitiveTypeToClass; - private final EnumMap primitiveTypeToArrayClass; - private final EnumMap primitiveTypeToJetType; - private final EnumMap primitiveTypeToNullableJetType; - private final EnumMap primitiveTypeToArrayJetType; + private final Map primitiveTypeToNullableJetType; + private final Map primitiveTypeToArrayJetType; private final Map primitiveJetTypeToJetArrayType; private final Map jetArrayTypeToPrimitiveJetType; - private final ClassDescriptor nothingClass; - private final ClassDescriptor arrayClass; - private final ClassDescriptor deprecatedAnnotationClass; - private final ClassDescriptor dataAnnotationClass; - private final ClassDescriptor[] functionClasses; - - private volatile JetType anyType; - private volatile JetType nullableAnyType; - private volatile JetType nothingType; - private volatile JetType nullableNothingType; - private volatile JetType unitType; - private volatile JetType stringType; - private volatile JetType annotationType; - private KotlinBuiltIns() { - this.builtInsModule = new ModuleDescriptorImpl(Name.special(""), - Collections.emptyList(), - PlatformToKotlinClassMap.EMPTY); + builtInsModule = new ModuleDescriptorImpl(Name.special(""), + Collections.emptyList(), + PlatformToKotlinClassMap.EMPTY); builtinsPackageFragment = new BuiltinsPackageFragment(new LockBasedStorageManager(), builtInsModule); builtInsModule.addFragmentProvider(DependencyKind.SOURCES, builtinsPackageFragment.getProvider()); - this.functionClassesSet = computeIndexedClasses("Function", FUNCTION_TRAIT_COUNT); - this.extensionFunctionClassesSet = computeIndexedClasses("ExtensionFunction", FUNCTION_TRAIT_COUNT); + functionClassesSet = computeIndexedClasses("Function", FUNCTION_TRAIT_COUNT); + extensionFunctionClassesSet = computeIndexedClasses("ExtensionFunction", FUNCTION_TRAIT_COUNT); - this.primitiveTypeToClass = new EnumMap(PrimitiveType.class); - this.primitiveTypeToJetType = new EnumMap(PrimitiveType.class); - this.primitiveTypeToNullableJetType = new EnumMap(PrimitiveType.class); - this.primitiveTypeToArrayClass = new EnumMap(PrimitiveType.class); - this.primitiveTypeToArrayJetType = new EnumMap(PrimitiveType.class); - this.primitiveJetTypeToJetArrayType = new HashMap(); - this.jetArrayTypeToPrimitiveJetType = new HashMap(); - - this.nothingClass = getBuiltInClassByName("Nothing"); - this.arrayClass = getBuiltInClassByName("Array"); - this.deprecatedAnnotationClass = getBuiltInClassByName("deprecated"); - this.dataAnnotationClass = getBuiltInClassByName("data"); - this.functionClasses = new ClassDescriptor[FUNCTION_TRAIT_COUNT]; - for (int i = 0; i < functionClasses.length; i++) { - functionClasses[i] = getBuiltInClassByName("Function" + i); - } + primitiveTypeToNullableJetType = new EnumMap(PrimitiveType.class); + primitiveTypeToArrayJetType = new EnumMap(PrimitiveType.class); + primitiveJetTypeToJetArrayType = new HashMap(); + jetArrayTypeToPrimitiveJetType = new HashMap(); } private void doInitialize() { - anyType = getBuiltInTypeByClassName("Any"); - nullableAnyType = TypeUtils.makeNullable(anyType); - nothingType = getBuiltInTypeByClassName("Nothing"); - nullableNothingType = TypeUtils.makeNullable(nothingType); - unitType = getBuiltInTypeByClassName("Unit"); - stringType = getBuiltInTypeByClassName("String"); - annotationType = getBuiltInTypeByClassName("Annotation"); - for (PrimitiveType primitive : PrimitiveType.values()) { makePrimitive(primitive); } @@ -174,10 +133,7 @@ public class KotlinBuiltIns { ClassDescriptor arrayClass = getBuiltInClassByName(primitiveType.getArrayTypeName().asString()); JetType arrayType = new JetTypeImpl(arrayClass); - primitiveTypeToClass.put(primitiveType, theClass); - primitiveTypeToJetType.put(primitiveType, type); primitiveTypeToNullableJetType.put(primitiveType, TypeUtils.makeNullable(type)); - primitiveTypeToArrayClass.put(primitiveType, arrayClass); primitiveTypeToArrayJetType.put(primitiveType, arrayType); primitiveJetTypeToJetArrayType.put(type, arrayType); jetArrayTypeToPrimitiveJetType.put(arrayType, type); @@ -455,14 +411,14 @@ public class KotlinBuiltIns { @NotNull public ClassDescriptor getMapEntry() { - ClassDescriptor classDescriptor = DescriptorUtils.getInnerClassByName(getBuiltInClassByName("Map"), "Entry"); + ClassDescriptor classDescriptor = DescriptorUtils.getInnerClassByName(getMap(), "Entry"); assert classDescriptor != null : "Can't find Map.Entry"; return classDescriptor; } @NotNull public ClassDescriptor getMutableMapEntry() { - ClassDescriptor classDescriptor = DescriptorUtils.getInnerClassByName(getBuiltInClassByName("MutableMap"), "MutableEntry"); + ClassDescriptor classDescriptor = DescriptorUtils.getInnerClassByName(getMutableMap(), "MutableEntry"); assert classDescriptor != null : "Can't find MutableMap.MutableEntry"; return classDescriptor; } @@ -696,11 +652,6 @@ public class KotlinBuiltIns { return getBuiltInTypeByClassName("Annotation"); } - @NotNull - public ClassDescriptor getPropertyMetadata() { - return getBuiltInClassByName("PropertyMetadata"); - } - @NotNull public ClassDescriptor getPropertyMetadataImpl() { return getBuiltInClassByName("PropertyMetadataImpl"); diff --git a/idea/src/org/jetbrains/jet/plugin/references/BuiltInsReferenceResolver.java b/idea/src/org/jetbrains/jet/plugin/references/BuiltInsReferenceResolver.java index 1b183cb7a29..f8908057e4d 100644 --- a/idea/src/org/jetbrains/jet/plugin/references/BuiltInsReferenceResolver.java +++ b/idea/src/org/jetbrains/jet/plugin/references/BuiltInsReferenceResolver.java @@ -59,7 +59,8 @@ import java.util.Set; import static org.jetbrains.jet.lang.resolve.DescriptorUtils.unwrapFakeOverride; public class BuiltInsReferenceResolver extends AbstractProjectComponent { - private static final File BUILT_INS_COMPILABLE_SRC_DIR = new File("core/builtins/src", KotlinBuiltIns.BUILT_INS_PACKAGE_NAME_STRING); + private static final File BUILT_INS_COMPILABLE_SRC_DIR = + new File("core/builtins/src", KotlinBuiltIns.BUILT_INS_PACKAGE_NAME.asString()); private volatile BindingContext bindingContext; private volatile Set builtInsSources;