Support static class scope in frontend, JVM codegen and IDE

This commit is contained in:
Alexander Udalov
2014-09-03 15:14:59 +04:00
parent cb81023469
commit 8ccca1781d
12 changed files with 84 additions and 54 deletions
@@ -68,6 +68,7 @@ public class LazyJavaClassMemberScope(
emptyOrSingletonList(createDefaultConstructor())
}
}
override fun computeNonDeclaredFunctions(result: MutableCollection<SimpleFunctionDescriptor>, name: Name) {
val functionsFromSupertypes = getFunctionsFromSupertypes(name, getContainingDeclaration())
result.addAll(DescriptorResolverUtils.resolveOverrides(name, functionsFromSupertypes, result, getContainingDeclaration(), c.errorReporter))
@@ -237,6 +238,9 @@ public class LazyJavaClassMemberScope(
}
}
override fun getExpectedThisObject(): ReceiverParameterDescriptor? =
DescriptorUtils.getExpectedThisObjectIfNeeded(getContainingDeclaration())
override fun getClassifier(name: Name): ClassifierDescriptor? = nestedClasses(name)
override fun getAllClassNames(): Collection<Name> = nestedClassIndex().keySet() + enumEntryIndex().keySet()
@@ -43,7 +43,6 @@ import java.util.LinkedHashSet
import org.jetbrains.jet.lang.types.JetType
import org.jetbrains.jet.lang.resolve.java.descriptor.JavaPropertyDescriptor
import org.jetbrains.jet.lang.descriptors.impl.PropertyDescriptorImpl
import java.util.Collections
import org.jetbrains.jet.lang.resolve.java.resolver.ExternalSignatureResolver
import org.jetbrains.jet.lang.resolve.java.sam.SingleAbstractMethodUtils
import org.jetbrains.jet.utils.*
@@ -58,7 +57,7 @@ public abstract class LazyJavaMemberScope(
// when computing getAllPackageNames() we ask the JavaPsiFacade for all subpackages of foo
// it, in turn, asks JavaElementFinder for subpackages of Kotlin package foo, which calls getAllPackageNames() recursively
// when on recursive call we return an empty collection, recursion collapses gracefully
Collections.emptyList()
listOf()
)
override fun getContainingDeclaration() = _containingDeclaration
@@ -71,6 +70,8 @@ public abstract class LazyJavaMemberScope(
protected abstract fun computeNonDeclaredFunctions(result: MutableCollection<SimpleFunctionDescriptor>, name: Name)
protected abstract fun getExpectedThisObject(): ReceiverParameterDescriptor?
private val _functions = c.storageManager.createMemoizedFunction {
(name: Name): Collection<FunctionDescriptor>
->
@@ -138,7 +139,7 @@ public abstract class LazyJavaMemberScope(
functionDescriptorImpl.initialize(
effectiveSignature.getReceiverType(),
DescriptorUtils.getExpectedThisObjectIfNeeded(_containingDeclaration),
getExpectedThisObject(),
effectiveSignature.getTypeParameters(),
effectiveSignature.getValueParameters(),
effectiveSignature.getReturnType(),
@@ -256,7 +257,7 @@ public abstract class LazyJavaMemberScope(
c.externalSignatureResolver.reportSignatureErrors(propertyDescriptor, signatureErrors)
}
propertyDescriptor.setType(effectiveSignature.getReturnType(), Collections.emptyList(), DescriptorUtils.getExpectedThisObjectIfNeeded(getContainingDeclaration()), null : JetType?)
propertyDescriptor.setType(effectiveSignature.getReturnType(), listOf(), getExpectedThisObject(), null : JetType?)
if (DescriptorUtils.shouldRecordInitializerForProperty(propertyDescriptor, propertyDescriptor.getType())) {
propertyDescriptor.setCompileTimeInitializer(
@@ -44,6 +44,8 @@ public abstract class LazyJavaStaticScope(
protected val fqName: FqName = DescriptorUtils.getFqName(descriptor).toSafe()
override fun getExpectedThisObject() = null
protected fun computeMemberIndexForSamConstructors(delegate: MemberIndex): MemberIndex = object : MemberIndex by delegate {
override fun getAllMethodNames(): Collection<Name> {
val jClass = c.findClassInJava(fqName).jClass
@@ -107,10 +107,6 @@ public class DescriptorUtils {
return getFqName(containingDeclaration).child(descriptor.getName());
}
public static boolean isTopLevelDeclaration(@NotNull DeclarationDescriptor descriptor) {
return descriptor.getContainingDeclaration() instanceof PackageFragmentDescriptor;
}
@Nullable
private static DeclarationDescriptor getContainingDeclarationSkippingClassObjects(@NotNull DeclarationDescriptor descriptor) {
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
@@ -127,6 +123,22 @@ public class DescriptorUtils {
return getFqNameFromTopLevelClass(containingDeclaration).child(name);
}
public static boolean isTopLevelDeclaration(@NotNull DeclarationDescriptor descriptor) {
return descriptor.getContainingDeclaration() instanceof PackageFragmentDescriptor;
}
/**
* @return true iff this is a top-level declaration or a class member with no expected "this" object (e.g. static members in Java,
* values() and valueOf() methods of enum classes, etc.)
*/
public static boolean isStaticDeclaration(@NotNull CallableDescriptor descriptor) {
if (descriptor instanceof ConstructorDescriptor) return false;
DeclarationDescriptor container = descriptor.getContainingDeclaration();
return container instanceof PackageFragmentDescriptor ||
(container instanceof ClassDescriptor && descriptor.getExpectedThisObject() == null);
}
// WARNING! Don't use this method in JVM backend, use JvmCodegenUtil.isCallInsideSameModuleAsDeclared() instead.
// The latter handles compilation against compiled part of our module correctly.
public static boolean areInSameModule(@NotNull DeclarationDescriptor first, @NotNull DeclarationDescriptor second) {