Make AnnotationLoader and ConstantLoader accept descriptor-independent data

Rewrite BaseDescriptorLoader to operate on data derived from proto
Intdoduce ProtoContainer to replace containing descriptor as input data
This commit is contained in:
Pavel V. Talanov
2014-11-26 18:36:34 +03:00
parent c73ac97ecf
commit e4d3a65124
8 changed files with 108 additions and 93 deletions
@@ -24,6 +24,7 @@ import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.descriptors.serialization.SerializationPackage;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotatedCallableKind;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationLoader;
import org.jetbrains.jet.descriptors.serialization.descriptors.ProtoContainer;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptorImpl;
@@ -57,15 +58,15 @@ public class AnnotationDescriptorLoader extends BaseDescriptorLoader implements
@NotNull
@Override
public List<AnnotationDescriptor> loadClassAnnotations(
@NotNull ClassDescriptor descriptor,
@NotNull ProtoBuf.Class classProto,
@NotNull NameResolver nameResolver
) {
KotlinJvmBinaryClass kotlinClass = findKotlinClassByDescriptor(descriptor);
ClassId classId = nameResolver.getClassId(classProto.getFqName());
KotlinJvmBinaryClass kotlinClass = findKotlinClassById(classId);
if (kotlinClass == null) {
// This means that the resource we're constructing the descriptor from is no longer present: KotlinClassFinder had found the
// class earlier, but it can't now
getErrorReporter().reportLoadingError("Kotlin class for loading class annotations is not found: " + descriptor, null);
getErrorReporter().reportLoadingError("Kotlin class for loading class annotations is not found: " + classId.asSingleFqName(), null);
return Collections.emptyList();
}
@@ -183,7 +184,7 @@ public class AnnotationDescriptorLoader extends BaseDescriptorLoader implements
@NotNull
@Override
public List<AnnotationDescriptor> loadCallableAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
@@ -196,7 +197,7 @@ public class AnnotationDescriptorLoader extends BaseDescriptorLoader implements
@NotNull
private List<AnnotationDescriptor> findClassAndLoadMemberAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind,
@@ -215,7 +216,7 @@ public class AnnotationDescriptorLoader extends BaseDescriptorLoader implements
@NotNull
@Override
public List<AnnotationDescriptor> loadValueParameterAnnotations(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable callable,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind,
@@ -20,21 +20,14 @@ import org.jetbrains.jet.descriptors.serialization.JavaProtoBuf.*;
import org.jetbrains.jet.descriptors.serialization.NameResolver;
import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotatedCallableKind;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassOrPackageFragmentDescriptor;
import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter;
import org.jetbrains.jet.lang.resolve.name.ClassId;
import org.jetbrains.jet.lang.resolve.DescriptorUtils.isClassObject
import org.jetbrains.jet.lang.resolve.DescriptorUtils.isTrait
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils.getPackageClassId
import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage.MemberSignature
import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.getClassId
import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.kotlinClassIdToJavaClassId
import kotlin.platform.platformStatic
import org.jetbrains.kotlin.util.sure
import org.jetbrains.jet.descriptors.serialization.descriptors.ProtoContainer
import org.jetbrains.jet.descriptors.serialization.Flags
import org.jetbrains.jet.lang.resolve.name.FqName
public abstract class BaseDescriptorLoader protected(
private val kotlinClassFinder: KotlinClassFinder,
@@ -43,88 +36,86 @@ public abstract class BaseDescriptorLoader protected(
) {
protected fun findClassWithAnnotationsAndInitializers(
container: ClassOrPackageFragmentDescriptor,
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
annotatedCallableKind: AnnotatedCallableKind
): KotlinJvmBinaryClass? {
if (container is PackageFragmentDescriptor) {
return findPackagePartClass(container, proto, nameResolver)
val packageFqName = container.packageFqName
if (packageFqName != null) {
return findPackagePartClass(packageFqName, proto, nameResolver)
}
else if (isClassObject(container) && isStaticFieldInOuter(proto)) {
val classProto = container.classProto!!
val classKind = Flags.CLASS_KIND[classProto.getFlags()]
val classId = nameResolver.getClassId(classProto.getFqName())
if (classKind == ProtoBuf.Class.Kind.CLASS_OBJECT && isStaticFieldInOuter(proto)) {
// Backing fields of properties of a class object are generated in the outer class
return findKotlinClassByDescriptor(container.getContainingDeclaration() as ClassOrPackageFragmentDescriptor)
return findKotlinClassById(classId.getOuterClassId())
}
else if (isTrait(container) && kind == AnnotatedCallableKind.PROPERTY) {
else if (classKind == ProtoBuf.Class.Kind.TRAIT && annotatedCallableKind == AnnotatedCallableKind.PROPERTY) {
if (proto.hasExtension(implClassName)) {
val packageFqName = getClassId(container as ClassDescriptor).getPackageFqName()
val parentPackageFqName = classId.getPackageFqName()
val tImplName = nameResolver.getName(proto.getExtension(implClassName))
// TODO: store accurate name for nested traits
return kotlinClassFinder.findKotlinClass(ClassId(packageFqName, tImplName))
return findKotlinClassById(ClassId(parentPackageFqName, tImplName))
}
return null
}
return findKotlinClassByDescriptor(container)
return findKotlinClassById(classId)
}
private fun findPackagePartClass(
container: PackageFragmentDescriptor,
packageFqName: FqName,
proto: ProtoBuf.Callable,
nameResolver: NameResolver
): KotlinJvmBinaryClass? {
if (proto.hasExtension(implClassName)) {
return kotlinClassFinder.findKotlinClass(ClassId(container.fqName, nameResolver.getName(proto.getExtension(implClassName))))
return findKotlinClassById(ClassId(packageFqName, nameResolver.getName(proto.getExtension(implClassName))))
}
return null
}
protected fun findKotlinClassByDescriptor(descriptor: ClassOrPackageFragmentDescriptor): KotlinJvmBinaryClass? {
return when (descriptor) {
is ClassDescriptor -> kotlinClassFinder.findKotlinClass(kotlinClassIdToJavaClassId(getClassId(descriptor)))
is PackageFragmentDescriptor -> kotlinClassFinder.findKotlinClass(getPackageClassId((descriptor).fqName))
else -> throw IllegalStateException("Unrecognized descriptor: " + descriptor)
}
protected fun findKotlinClassById(classId: ClassId): KotlinJvmBinaryClass? {
return kotlinClassFinder.findKotlinClass(kotlinClassIdToJavaClassId(classId))
}
class object {
platformStatic fun getCallableSignature(
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): MemberSignature? {
val deserializer = SignatureDeserializer(nameResolver)
when (kind) {
AnnotatedCallableKind.FUNCTION -> if (proto.hasExtension(methodSignature)) {
return deserializer.methodSignature(proto.getExtension(methodSignature))
}
AnnotatedCallableKind.PROPERTY_GETTER -> if (proto.hasExtension(propertySignature)) {
return deserializer.methodSignature(proto.getExtension(propertySignature).getGetter())
}
AnnotatedCallableKind.PROPERTY_SETTER -> if (proto.hasExtension(propertySignature)) {
return deserializer.methodSignature(proto.getExtension(propertySignature).getSetter())
}
AnnotatedCallableKind.PROPERTY -> if (proto.hasExtension(propertySignature)) {
val propertySignature = proto.getExtension(propertySignature)
private fun isStaticFieldInOuter(proto: ProtoBuf.Callable): Boolean {
if (!proto.hasExtension(propertySignature)) return false
val propertySignature = proto.getExtension(propertySignature)
return propertySignature.hasField() && propertySignature.getField().getIsStaticInOuter()
}
if (propertySignature.hasField()) {
val field = propertySignature.getField()
val type = deserializer.typeDescriptor(field.getType())
val name = nameResolver.getName(field.getName())
return MemberSignature.fromFieldNameAndDesc(name, type)
}
else if (propertySignature.hasSyntheticMethod()) {
return deserializer.methodSignature(propertySignature.getSyntheticMethod())
}
fun getCallableSignature(
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): MemberSignature? {
val deserializer = SignatureDeserializer(nameResolver)
when (kind) {
AnnotatedCallableKind.FUNCTION -> if (proto.hasExtension(methodSignature)) {
return deserializer.methodSignature(proto.getExtension(methodSignature))
}
AnnotatedCallableKind.PROPERTY_GETTER -> if (proto.hasExtension(propertySignature)) {
return deserializer.methodSignature(proto.getExtension(propertySignature).getGetter())
}
AnnotatedCallableKind.PROPERTY_SETTER -> if (proto.hasExtension(propertySignature)) {
return deserializer.methodSignature(proto.getExtension(propertySignature).getSetter())
}
AnnotatedCallableKind.PROPERTY -> if (proto.hasExtension(propertySignature)) {
val propertySignature = proto.getExtension(propertySignature)
if (propertySignature.hasField()) {
val field = propertySignature.getField()
val type = deserializer.typeDescriptor(field.getType())
val name = nameResolver.getName(field.getName())
return MemberSignature.fromFieldNameAndDesc(name, type)
}
else if (propertySignature.hasSyntheticMethod()) {
return deserializer.methodSignature(propertySignature.getSyntheticMethod())
}
}
return null
}
private fun isStaticFieldInOuter(proto: ProtoBuf.Callable): Boolean {
if (!proto.hasExtension(propertySignature)) return false
val propertySignature = proto.getExtension(propertySignature)
return propertySignature.hasField() && propertySignature.getField().getIsStaticInOuter()
}
return null
}
}
@@ -22,7 +22,7 @@ import org.jetbrains.jet.descriptors.serialization.NameResolver;
import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotatedCallableKind;
import org.jetbrains.jet.descriptors.serialization.descriptors.ConstantLoader;
import org.jetbrains.jet.lang.descriptors.ClassOrPackageFragmentDescriptor;
import org.jetbrains.jet.descriptors.serialization.descriptors.ProtoContainer;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter;
@@ -40,12 +40,12 @@ public class ConstantDescriptorLoader extends BaseDescriptorLoader implements Co
@Nullable
@Override
public CompileTimeConstant<?> loadPropertyConstant(
@NotNull ClassOrPackageFragmentDescriptor container,
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
) {
MemberSignature signature = BaseDescriptorLoader.getCallableSignature(proto, nameResolver, kind);
MemberSignature signature = getCallableSignature(proto, nameResolver, kind);
if (signature == null) return null;
KotlinJvmBinaryClass kotlinClass = findClassWithAnnotationsAndInitializers(container, proto, nameResolver, kind);