Refactor LazyClassDescriptor and minor changes

Minor: get script definition once
Refactor: get rid of 'LazyClassDescriptor#getInjectedSupertypes'
Minor: rename scope
This commit is contained in:
Pavel V. Talanov
2016-05-31 18:28:08 +02:00
committed by Ilya Chernikov
parent 4d95e873a9
commit d5b486eb80
4 changed files with 64 additions and 74 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -61,38 +61,37 @@ public class JavaClassFinderImpl implements JavaClassFinder {
// Only activate post create // Only activate post create
} }
public class DelegatingGlobalSearchScopeWithBaseAccess extends DelegatingGlobalSearchScope { public class FilterOutKotlinSourceFilesScope extends DelegatingGlobalSearchScope {
public FilterOutKotlinSourceFilesScope(@NotNull GlobalSearchScope baseScope) {
public DelegatingGlobalSearchScopeWithBaseAccess(@NotNull GlobalSearchScope baseScope) {
super(baseScope); super(baseScope);
} }
@Override
public boolean contains(@NotNull VirtualFile file) {
return myBaseScope.contains(file) && (file.isDirectory() || file.getFileType() != KotlinFileType.INSTANCE);
}
@NotNull
public GlobalSearchScope getBase() { public GlobalSearchScope getBase() {
return myBaseScope; return myBaseScope;
} }
//NOTE: expected by class finder to be not null
@NotNull
@Override
public Project getProject() {
return project;
}
@Override
public String toString() {
return "JCFI: " + myBaseScope;
}
} }
@PostConstruct @PostConstruct
public void initialize() { public void initialize() {
javaSearchScope = new DelegatingGlobalSearchScopeWithBaseAccess(baseScope) { javaSearchScope = new FilterOutKotlinSourceFilesScope(baseScope);
@Override
public boolean contains(@NotNull VirtualFile file) {
return myBaseScope.contains(file) && (file.isDirectory() || file.getFileType() != KotlinFileType.INSTANCE);
}
//NOTE: expected by class finder to be not null
@NotNull
@Override
public Project getProject() {
return project;
}
@Override
public String toString() {
return "JCFI: " + baseScope;
}
};
javaFacade = KotlinJavaPsiFacade.getInstance(project); javaFacade = KotlinJavaPsiFacade.getInstance(project);
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -567,9 +567,6 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
return parameters.invoke(); return parameters.invoke();
} }
@Nullable
public List<? extends KotlinType> getInjectedSupertypes() { return null; }
private class LazyClassTypeConstructor extends AbstractClassTypeConstructor implements LazyEntity { private class LazyClassTypeConstructor extends AbstractClassTypeConstructor implements LazyEntity {
private final NotNullLazyValue<List<TypeParameterDescriptor>> parameters = c.getStorageManager().createLazyValue(new Function0<List<TypeParameterDescriptor>>() { private final NotNullLazyValue<List<TypeParameterDescriptor>> parameters = c.getStorageManager().createLazyValue(new Function0<List<TypeParameterDescriptor>>() {
@Override @Override
@@ -594,26 +591,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
@NotNull @NotNull
@Override @Override
protected Collection<KotlinType> computeSupertypes() { protected Collection<KotlinType> computeSupertypes() {
if (KotlinBuiltIns.isSpecialClassWithNoSupertypes(LazyClassDescriptor.this)) { return LazyClassDescriptor.this.computeSupertypes();
return Collections.emptyList();
}
List<? extends KotlinType> injectedSupertypes = getInjectedSupertypes();
if (injectedSupertypes != null) {
return Lists.newArrayList(Collections2.filter(injectedSupertypes, VALID_SUPERTYPE));
}
KtClassOrObject classOrObject = declarationProvider.getOwnerInfo().getCorrespondingClassOrObject();
if (classOrObject == null) {
return Collections.singleton(c.getModuleDescriptor().getBuiltIns().getAnyType());
}
List<KotlinType> allSupertypes =
c.getDescriptorResolver()
.resolveSupertypes(getScopeForClassHeaderResolution(), LazyClassDescriptor.this, classOrObject,
c.getTrace());
return Lists.newArrayList(Collections2.filter(allSupertypes, VALID_SUPERTYPE));
} }
@Override @Override
@@ -706,4 +684,23 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
ForceResolveUtil.forceResolveAllContents(getParameters()); ForceResolveUtil.forceResolveAllContents(getParameters());
} }
} }
@NotNull
protected Collection<KotlinType> computeSupertypes() {
if (KotlinBuiltIns.isSpecialClassWithNoSupertypes(this)) {
return Collections.emptyList();
}
KtClassOrObject classOrObject = declarationProvider.getOwnerInfo().getCorrespondingClassOrObject();
if (classOrObject == null) {
return Collections.singleton(c.getModuleDescriptor().getBuiltIns().getAnyType());
}
List<KotlinType> allSupertypes =
c.getDescriptorResolver()
.resolveSupertypes(getScopeForClassHeaderResolution(), this, classOrObject,
c.getTrace());
return Lists.newArrayList(Collections2.filter(allSupertypes, VALID_SUPERTYPE));
}
} }
@@ -1,18 +1,18 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.resolve.lazy.descriptors package org.jetbrains.kotlin.resolve.lazy.descriptors
@@ -28,8 +28,6 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.BindingTrace import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.lazy.ResolveSession import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProvider import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProvider
import org.jetbrains.kotlin.script.KotlinScriptDefinitionProvider
import org.jetbrains.kotlin.script.getScriptDefinition
import org.jetbrains.kotlin.utils.toReadOnlyList import org.jetbrains.kotlin.utils.toReadOnlyList
class LazyScriptClassMemberScope( class LazyScriptClassMemberScope(
@@ -55,10 +53,7 @@ class LazyScriptClassMemberScope(
} }
private fun createScriptParameters(constructor: ConstructorDescriptorImpl): List<ValueParameterDescriptor> { private fun createScriptParameters(constructor: ConstructorDescriptorImpl): List<ValueParameterDescriptor> {
val file = scriptDescriptor.scriptInfo.script.getContainingKtFile() return scriptDescriptor.scriptDefinition.getScriptParameters(scriptDescriptor).mapIndexed { index, scriptParameter ->
val scriptDefinition = getScriptDefinition(file) ?:
throw RuntimeException("file ${file.name} is not a script")
return scriptDefinition.getScriptParameters(scriptDescriptor).mapIndexed { index, scriptParameter ->
ValueParameterDescriptorImpl( ValueParameterDescriptorImpl(
constructor, null, index, Annotations.EMPTY, scriptParameter.name, scriptParameter.type, constructor, null, index, Annotations.EMPTY, scriptParameter.name, scriptParameter.type,
/* declaresDefaultValue = */ false, /* declaresDefaultValue = */ false,
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,17 +21,18 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptorVisitor
import org.jetbrains.kotlin.descriptors.ScriptDescriptor import org.jetbrains.kotlin.descriptors.ScriptDescriptor
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.lazy.LazyClassContext import org.jetbrains.kotlin.resolve.lazy.LazyClassContext
import org.jetbrains.kotlin.resolve.lazy.ResolveSession import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.resolve.lazy.data.KtScriptInfo import org.jetbrains.kotlin.resolve.lazy.data.KtScriptInfo
import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProvider import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProvider
import org.jetbrains.kotlin.resolve.source.toSourceElement import org.jetbrains.kotlin.resolve.source.toSourceElement
import org.jetbrains.kotlin.script.KotlinScriptDefinition import org.jetbrains.kotlin.script.KotlinScriptDefinition
import org.jetbrains.kotlin.script.KotlinScriptDefinitionProvider
import org.jetbrains.kotlin.script.ScriptPriorities import org.jetbrains.kotlin.script.ScriptPriorities
import org.jetbrains.kotlin.script.getScriptDefinition import org.jetbrains.kotlin.script.getScriptDefinition
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSubstitutor import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.utils.ifEmpty
class LazyScriptDescriptor( class LazyScriptDescriptor(
resolveSession: ResolveSession, resolveSession: ResolveSession,
@@ -56,11 +57,10 @@ class LazyScriptDescriptor(
override fun getPriority() = priority override fun getPriority() = priority
private val scriptDefinition: KotlinScriptDefinition val scriptDefinition: KotlinScriptDefinition
by lazy { by lazy {
val file = scriptInfo.script.getContainingKtFile() val file = scriptInfo.script.getContainingKtFile()
getScriptDefinition(file) ?: getScriptDefinition(file) ?: throw RuntimeException("file ${file.name} is not a script")
throw RuntimeException("file ${file.name} is not a script")
} }
override fun substitute(substitutor: TypeSubstitutor) = this override fun substitute(substitutor: TypeSubstitutor) = this
@@ -79,8 +79,7 @@ class LazyScriptDescriptor(
override fun getUnsubstitutedPrimaryConstructor() = super.getUnsubstitutedPrimaryConstructor()!! override fun getUnsubstitutedPrimaryConstructor() = super.getUnsubstitutedPrimaryConstructor()!!
override fun getInjectedSupertypes(): List<KotlinType>? = override fun computeSupertypes() = scriptDefinition.getScriptSupertypes(this).ifEmpty { listOf(builtIns.anyType) }
scriptDefinition.getScriptSupertypes(this).let { if (it.isEmpty()) null else it }
override fun getScriptParametersToPassToSuperclass(): List<Pair<Name, KotlinType>> { override fun getScriptParametersToPassToSuperclass(): List<Pair<Name, KotlinType>> {
val scriptParams = scriptDefinition.getScriptParameters(this) val scriptParams = scriptDefinition.getScriptParameters(this)