Introduce NoLookupLocation and some new constants to classify LookupLocation.NO_LOCATION usages

This commit is contained in:
Zalim Bashorov
2015-08-17 19:17:23 +03:00
parent e692b7b9c4
commit 0ca9c6fd40
7 changed files with 51 additions and 20 deletions
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils.getLineAndColumnInPsiFile
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.incremental.components.ScopeKind
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.doNotAnalyze
@@ -30,7 +31,7 @@ import org.jetbrains.kotlin.resolve.scopes.JetScope
public fun LookupTracker.record(from: LookupLocation, inScope: JetScope, name: Name) {
if (this == LookupTracker.DO_NOTHING || from == LookupLocation.NO_LOCATION) return
if (this == LookupTracker.DO_NOTHING || from is NoLookupLocation) return
if (from !is KotlinLookupLocation) throw AssertionError("Unexpected location type: ${from.javaClass}")
@@ -20,23 +20,28 @@ import com.google.common.collect.HashMultimap
import com.google.common.collect.Multimap
import com.google.common.collect.Sets
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.Errors.REDECLARATION
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.JetElement
import org.jetbrains.kotlin.psi.JetPackageDirective
import org.jetbrains.kotlin.resolve.lazy.FileScopeProvider
import org.jetbrains.kotlin.resolve.lazy.TopLevelDescriptorProvider
import org.jetbrains.kotlin.utils.*
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.keysToMap
import java.util.HashSet
import org.jetbrains.kotlin.diagnostics.Errors.REDECLARATION
import kotlin.properties.Delegates
public class DeclarationResolver(
private val annotationResolver: AnnotationResolver,
private val trace: BindingTrace
) {
public class DeclarationResolver(private val annotationResolver: AnnotationResolver,
private val trace: BindingTrace) {
private val NO_LOCATION_WHEN_CHECK_REDECLARATIONS = NoLookupLocation.create("when check redeclarations")
public fun resolveAnnotationsOnFiles(c: TopDownAnalysisContext, scopeProvider: FileScopeProvider) {
val filesToScope = c.getFiles().keysToMap { scopeProvider.getFileScope(it) }
@@ -112,7 +117,7 @@ public class DeclarationResolver(private val annotationResolver: AnnotationResol
val parentFragment = topLevelDescriptorProvider.getPackageFragment(parentFqName)
if (parentFragment != null) {
// Filter out extension properties
descriptors.addAll(parentFragment.getMemberScope().getProperties(fqName.shortName()).filter {
descriptors.addAll(parentFragment.getMemberScope().getProperties(fqName.shortName(), NO_LOCATION_WHEN_CHECK_REDECLARATIONS).filter {
it.getExtensionReceiverParameter() == null
})
}
@@ -27,7 +27,9 @@ import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.kotlin.context.GlobalContext;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.incremental.components.LookupLocation;
import org.jetbrains.kotlin.incremental.components.LookupTracker;
import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
@@ -41,7 +43,6 @@ import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationsContextImpl;
import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyPackageDescriptor;
import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyScriptDescriptor;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.incremental.components.LookupLocation;
import org.jetbrains.kotlin.storage.*;
import javax.inject.Inject;
@@ -50,6 +51,8 @@ import java.util.Collections;
import java.util.List;
public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
private static final LookupLocation NO_LOCATION_FOR_SCRIPT = NoLookupLocation.create("for script");
private final LazyResolveStorageManager storageManager;
private final ExceptionTracker exceptionTracker;
@@ -289,7 +292,7 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
public ClassDescriptor getClassDescriptorForScript(@NotNull JetScript script) {
JetScope resolutionScope = lazyDeclarationResolver.resolutionScopeToResolveDeclaration(script);
FqName fqName = ScriptNameUtil.classNameForScript(script);
ClassifierDescriptor classifier = resolutionScope.getClassifier(fqName.shortName(), LookupLocation.NO_LOCATION);
ClassifierDescriptor classifier = resolutionScope.getClassifier(fqName.shortName(), NO_LOCATION_FOR_SCRIPT);
assert classifier != null : "No descriptor for " + fqName + " in file " + script.getContainingFile();
return (ClassDescriptor) classifier;
}
@@ -25,13 +25,14 @@ import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.*;
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl;
import org.jetbrains.kotlin.incremental.components.LookupLocation;
import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.FqNameUnsafe;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.constants.ConstantValue;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.incremental.components.LookupLocation;
import org.jetbrains.kotlin.storage.LockBasedStorageManager;
import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.types.checker.JetTypeChecker;
@@ -55,6 +56,8 @@ public class KotlinBuiltIns {
private static volatile boolean initializing;
private static Throwable initializationFailed;
private static final LookupLocation NO_LOCATION_FROM_BUILTINS = NoLookupLocation.create("from BuiltIns");
private static synchronized void initialize() {
if (instance == null) {
if (initializationFailed != null) {
@@ -239,7 +242,7 @@ public class KotlinBuiltIns {
@NotNull
private ClassDescriptor getAnnotationClassByName(@NotNull Name simpleName) {
ClassifierDescriptor classifier = annotationPackageFragment.getMemberScope().getClassifier(simpleName, LookupLocation.NO_LOCATION);
ClassifierDescriptor classifier = annotationPackageFragment.getMemberScope().getClassifier(simpleName, NO_LOCATION_FROM_BUILTINS);
assert classifier instanceof ClassDescriptor : "Must be a class descriptor " + simpleName + ", but was " +
(classifier == null ? "null" : classifier.toString());
return (ClassDescriptor) classifier;
@@ -254,7 +257,7 @@ public class KotlinBuiltIns {
@Nullable
public ClassDescriptor getBuiltInClassByNameNullable(@NotNull Name simpleName) {
ClassifierDescriptor classifier = getBuiltInsPackageFragment().getMemberScope().getClassifier(simpleName, LookupLocation.NO_LOCATION);
ClassifierDescriptor classifier = getBuiltInsPackageFragment().getMemberScope().getClassifier(simpleName, NO_LOCATION_FROM_BUILTINS);
assert classifier == null ||
classifier instanceof ClassDescriptor : "Must be a class descriptor " + simpleName + ", but was " + classifier;
return (ClassDescriptor) classifier;
@@ -399,7 +402,7 @@ public class KotlinBuiltIns {
@Nullable
public ClassDescriptor getAnnotationTargetEnumEntry(@NotNull KotlinTarget target) {
ClassifierDescriptor result = getAnnotationTargetEnum().getUnsubstitutedInnerClassesScope().getClassifier(
Name.identifier(target.name()), LookupLocation.NO_LOCATION
Name.identifier(target.name()), NO_LOCATION_FROM_BUILTINS
);
return result instanceof ClassDescriptor ? (ClassDescriptor) result : null;
}
@@ -412,7 +415,7 @@ public class KotlinBuiltIns {
@Nullable
public ClassDescriptor getAnnotationRetentionEnumEntry(@NotNull KotlinRetention retention) {
ClassifierDescriptor result = getAnnotationRetentionEnum().getUnsubstitutedInnerClassesScope().getClassifier(
Name.identifier(retention.name()), LookupLocation.NO_LOCATION
Name.identifier(retention.name()), NO_LOCATION_FROM_BUILTINS
);
return result instanceof ClassDescriptor ? (ClassDescriptor) result : null;
}
@@ -1042,6 +1045,6 @@ public class KotlinBuiltIns {
@NotNull
public FunctionDescriptor getIdentityEquals() {
return KotlinPackage.first(getBuiltInsPackageFragment().getMemberScope().getFunctions(Name.identifier("identityEquals"), LookupLocation.NO_LOCATION));
return KotlinPackage.first(getBuiltInsPackageFragment().getMemberScope().getFunctions(Name.identifier("identityEquals"), NO_LOCATION_FROM_BUILTINS));
}
}
@@ -16,8 +16,24 @@
package org.jetbrains.kotlin.incremental.components
import kotlin.platform.platformStatic
public interface LookupLocation {
companion object {
val NO_LOCATION = object : LookupLocation {}
@deprecated("Use more suitable constant if possible")
val NO_LOCATION = NoLookupLocation.create("(unsorted)")
val NO_LOCATION_FROM_IDE = NoLookupLocation.create("from IDE")
val NO_LOCATION_FROM_BACKEND = NoLookupLocation.create("from backend")
val NO_LOCATION_FROM_TEST = NoLookupLocation.create("from test")
}
}
public class NoLookupLocation private constructor(private val reason: String) : LookupLocation {
override fun toString() = "NO LOCATION $reason"
companion object {
platformStatic
public fun create(reason: String): LookupLocation = NoLookupLocation(reason)
}
}
@@ -105,7 +105,7 @@ abstract class KCallableContainerImpl : DeclarationContainerImpl {
fun findPropertyDescriptor(name: String, signature: String): PropertyDescriptor {
val properties = scope
.getProperties(Name.guess(name))
.getProperties(Name.guess(name), NO_LOCATION_FROM_REFLECTION)
.filter { descriptor ->
descriptor is PropertyDescriptor &&
RuntimeTypeMapper.mapPropertySignature(descriptor).asString() == signature
@@ -123,7 +123,7 @@ abstract class KCallableContainerImpl : DeclarationContainerImpl {
}
fun findFunctionDescriptor(name: String, signature: String): FunctionDescriptor {
val functions = (if (name == "<init>") constructorDescriptors.toList() else scope.getFunctions(Name.guess(name)))
val functions = (if (name == "<init>") constructorDescriptors.toList() else scope.getFunctions(Name.guess(name), NO_LOCATION_FROM_REFLECTION))
.filter { descriptor ->
RuntimeTypeMapper.mapSignature(descriptor).asString() == signature
}
@@ -16,11 +16,14 @@
package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.FqName
import kotlin.reflect.IllegalCallableAccessException
internal val PLATFORM_STATIC = FqName("kotlin.platform.platformStatic")
internal val NO_LOCATION_FROM_REFLECTION = NoLookupLocation.create("from reflection")
// TODO: wrap other exceptions
internal inline fun <R> reflectionCall(block: () -> R): R =
try {