Simplify AnnotationAndConstantLoader, use NameResolver from ProtoContainer

This commit is contained in:
Alexander Udalov
2015-09-30 20:12:02 +03:00
parent 3a7a48a079
commit 4f21caecc4
9 changed files with 39 additions and 73 deletions
@@ -85,28 +85,24 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
return result return result
} }
override fun loadCallableAnnotations( override fun loadCallableAnnotations(container: ProtoContainer, proto: ProtoBuf.Callable, kind: AnnotatedCallableKind): List<T> {
container: ProtoContainer, val nameResolver = container.nameResolver
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<T> {
if (kind == AnnotatedCallableKind.PROPERTY) { if (kind == AnnotatedCallableKind.PROPERTY) {
val syntheticFunctionSignature = getPropertySignature(proto, nameResolver, synthetic = true) val syntheticFunctionSignature = getPropertySignature(proto, nameResolver, synthetic = true)
val fieldSignature = getPropertySignature(proto, nameResolver, field = true) val fieldSignature = getPropertySignature(proto, nameResolver, field = true)
val propertyAnnotations = syntheticFunctionSignature?.let { sig -> val propertyAnnotations = syntheticFunctionSignature?.let { sig ->
findClassAndLoadMemberAnnotations(container, proto, nameResolver, sig) findClassAndLoadMemberAnnotations(container, proto, sig)
} ?: listOf() } ?: listOf()
val fieldAnnotations = fieldSignature?.let { sig -> val fieldAnnotations = fieldSignature?.let { sig ->
findClassAndLoadMemberAnnotations(container, proto, nameResolver, sig, isStaticFieldInOuter(proto)) findClassAndLoadMemberAnnotations(container, proto, sig, isStaticFieldInOuter(proto))
} ?: listOf() } ?: listOf()
return loadPropertyAnnotations(propertyAnnotations, fieldAnnotations) return loadPropertyAnnotations(propertyAnnotations, fieldAnnotations)
} }
val signature = getCallableSignature(proto, nameResolver, kind) ?: return listOf() val signature = getCallableSignature(proto, nameResolver, kind) ?: return listOf()
return transformAnnotations(findClassAndLoadMemberAnnotations(container, proto, nameResolver, signature)) return transformAnnotations(findClassAndLoadMemberAnnotations(container, proto, signature))
} }
protected abstract fun loadPropertyAnnotations(propertyAnnotations: List<A>, fieldAnnotations: List<A>): List<T> protected abstract fun loadPropertyAnnotations(propertyAnnotations: List<A>, fieldAnnotations: List<A>): List<T>
@@ -116,15 +112,14 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
private fun findClassAndLoadMemberAnnotations( private fun findClassAndLoadMemberAnnotations(
container: ProtoContainer, container: ProtoContainer,
proto: ProtoBuf.Callable, proto: ProtoBuf.Callable,
nameResolver: NameResolver,
signature: MemberSignature, signature: MemberSignature,
isStaticFieldInOuter: Boolean = false isStaticFieldInOuter: Boolean = false
): List<A> { ): List<A> {
val kotlinClass = findClassWithAnnotationsAndInitializers( val kotlinClass = findClassWithAnnotationsAndInitializers(
container, nameResolver, getImplClassName(proto, nameResolver), isStaticFieldInOuter container, getImplClassName(proto, container.nameResolver), isStaticFieldInOuter
) )
if (kotlinClass == null) { if (kotlinClass == null) {
errorReporter.reportLoadingError("Kotlin class for loading member annotations is not found: ${container.getFqName(nameResolver)}", null) errorReporter.reportLoadingError("Kotlin class for loading member annotations is not found: ${container.getFqName()}", null)
return listOf() return listOf()
} }
@@ -134,16 +129,15 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
override fun loadValueParameterAnnotations( override fun loadValueParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
callable: ProtoBuf.Callable, callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind, kind: AnnotatedCallableKind,
parameterIndex: Int, parameterIndex: Int,
proto: ProtoBuf.ValueParameter proto: ProtoBuf.ValueParameter
): List<A> { ): List<A> {
val methodSignature = getCallableSignature(callable, nameResolver, kind) val methodSignature = getCallableSignature(callable, container.nameResolver, kind)
if (methodSignature != null) { if (methodSignature != null) {
val index = if (proto.hasExtension(index)) proto.getExtension(index) else parameterIndex val index = if (proto.hasExtension(index)) proto.getExtension(index) else parameterIndex
val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, index) val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, index)
return findClassAndLoadMemberAnnotations(container, callable, nameResolver, paramSignature) return findClassAndLoadMemberAnnotations(container, callable, paramSignature)
} }
return listOf() return listOf()
@@ -152,14 +146,13 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
override fun loadExtensionReceiverParameterAnnotations( override fun loadExtensionReceiverParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
callable: ProtoBuf.Callable, callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<A> { ): List<A> {
if (!callable.hasReceiverType()) return emptyList() if (!callable.hasReceiverType()) return emptyList()
val methodSignature = getCallableSignature(callable, nameResolver, kind) val methodSignature = getCallableSignature(callable, container.nameResolver, kind)
if (methodSignature != null) { if (methodSignature != null) {
val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, 0) val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, 0)
return findClassAndLoadMemberAnnotations(container, callable, nameResolver, paramSignature) return findClassAndLoadMemberAnnotations(container, callable, paramSignature)
} }
return emptyList() return emptyList()
@@ -169,19 +162,15 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
return type.getExtension(JvmProtoBuf.typeAnnotation).map { loadTypeAnnotation(it, nameResolver) } return type.getExtension(JvmProtoBuf.typeAnnotation).map { loadTypeAnnotation(it, nameResolver) }
} }
override fun loadPropertyConstant( override fun loadPropertyConstant(container: ProtoContainer, proto: ProtoBuf.Callable, expectedType: JetType): C? {
container: ProtoContainer, val nameResolver = container.nameResolver
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
expectedType: JetType
): C? {
val signature = getCallableSignature(proto, nameResolver, AnnotatedCallableKind.PROPERTY) ?: return null val signature = getCallableSignature(proto, nameResolver, AnnotatedCallableKind.PROPERTY) ?: return null
val kotlinClass = findClassWithAnnotationsAndInitializers( val kotlinClass = findClassWithAnnotationsAndInitializers(
container, nameResolver, getImplClassName(proto, nameResolver), isStaticFieldInOuter(proto) container, getImplClassName(proto, nameResolver), isStaticFieldInOuter(proto)
) )
if (kotlinClass == null) { if (kotlinClass == null) {
errorReporter.reportLoadingError("Kotlin class for loading property constant is not found: ${container.getFqName(nameResolver)}", null) errorReporter.reportLoadingError("Kotlin class for loading property constant is not found: ${container.getFqName()}", null)
return null return null
} }
@@ -189,10 +178,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
} }
private fun findClassWithAnnotationsAndInitializers( private fun findClassWithAnnotationsAndInitializers(
container: ProtoContainer, container: ProtoContainer, implClassName: Name?, isStaticFieldInOuter: Boolean
nameResolver: NameResolver,
implClassName: Name?,
isStaticFieldInOuter: Boolean
): KotlinJvmBinaryClass? { ): KotlinJvmBinaryClass? {
val (classProto, packageFqName) = container val (classProto, packageFqName) = container
return when { return when {
@@ -200,7 +186,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
implClassName?.let { kotlinClassFinder.findKotlinClass(ClassId(packageFqName, it)) } implClassName?.let { kotlinClassFinder.findKotlinClass(ClassId(packageFqName, it)) }
} }
classProto != null -> { classProto != null -> {
val classId = nameResolver.getClassId(classProto.fqName) val classId = container.nameResolver.getClassId(classProto.fqName)
if (implClassName != null) { if (implClassName != null) {
// TODO: store accurate name for nested traits // TODO: store accurate name for nested traits
@@ -41,29 +41,26 @@ class BuiltInsAnnotationAndConstantLoader(
override fun loadCallableAnnotations( override fun loadCallableAnnotations(
container: ProtoContainer, container: ProtoContainer,
proto: ProtoBuf.Callable, proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<AnnotationWithTarget> { ): List<AnnotationWithTarget> {
val annotations = proto.getExtension(BuiltInsProtoBuf.callableAnnotation).orEmpty() val annotations = proto.getExtension(BuiltInsProtoBuf.callableAnnotation).orEmpty()
return annotations.map { proto -> AnnotationWithTarget(deserializer.deserializeAnnotation(proto, nameResolver), null) } return annotations.map { proto -> AnnotationWithTarget(deserializer.deserializeAnnotation(proto, container.nameResolver), null) }
} }
override fun loadValueParameterAnnotations( override fun loadValueParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
callable: ProtoBuf.Callable, callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind, kind: AnnotatedCallableKind,
parameterIndex: Int, parameterIndex: Int,
proto: ProtoBuf.ValueParameter proto: ProtoBuf.ValueParameter
): List<AnnotationDescriptor> { ): List<AnnotationDescriptor> {
val annotations = proto.getExtension(BuiltInsProtoBuf.parameterAnnotation).orEmpty() val annotations = proto.getExtension(BuiltInsProtoBuf.parameterAnnotation).orEmpty()
return annotations.map { proto -> deserializer.deserializeAnnotation(proto, nameResolver) } return annotations.map { proto -> deserializer.deserializeAnnotation(proto, container.nameResolver) }
} }
override fun loadExtensionReceiverParameterAnnotations( override fun loadExtensionReceiverParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
callable: ProtoBuf.Callable, callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<AnnotationDescriptor> = emptyList() ): List<AnnotationDescriptor> = emptyList()
@@ -75,10 +72,9 @@ class BuiltInsAnnotationAndConstantLoader(
override fun loadPropertyConstant( override fun loadPropertyConstant(
container: ProtoContainer, container: ProtoContainer,
proto: ProtoBuf.Callable, proto: ProtoBuf.Callable,
nameResolver: NameResolver,
expectedType: JetType expectedType: JetType
): ConstantValue<*>? { ): ConstantValue<*>? {
val value = proto.getExtension(BuiltInsProtoBuf.compileTimeValue) val value = proto.getExtension(BuiltInsProtoBuf.compileTimeValue)
return deserializer.resolveValue(expectedType, value, nameResolver) return deserializer.resolveValue(expectedType, value, container.nameResolver)
} }
} }
@@ -34,7 +34,6 @@ public interface AnnotationAndConstantLoader<A, C, T> {
List<T> loadCallableAnnotations( List<T> loadCallableAnnotations(
@NotNull ProtoContainer container, @NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto, @NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind @NotNull AnnotatedCallableKind kind
); );
@@ -42,7 +41,6 @@ public interface AnnotationAndConstantLoader<A, C, T> {
List<A> loadValueParameterAnnotations( List<A> loadValueParameterAnnotations(
@NotNull ProtoContainer container, @NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable callable, @NotNull ProtoBuf.Callable callable,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind, @NotNull AnnotatedCallableKind kind,
int parameterIndex, int parameterIndex,
@NotNull ProtoBuf.ValueParameter proto @NotNull ProtoBuf.ValueParameter proto
@@ -52,7 +50,6 @@ public interface AnnotationAndConstantLoader<A, C, T> {
List<A> loadExtensionReceiverParameterAnnotations( List<A> loadExtensionReceiverParameterAnnotations(
@NotNull ProtoContainer container, @NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable callable, @NotNull ProtoBuf.Callable callable,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind @NotNull AnnotatedCallableKind kind
); );
@@ -66,7 +63,6 @@ public interface AnnotationAndConstantLoader<A, C, T> {
C loadPropertyConstant( C loadPropertyConstant(
@NotNull ProtoContainer container, @NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto, @NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull JetType expectedType @NotNull JetType expectedType
); );
} }
@@ -129,7 +129,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
property.setCompileTimeInitializer( property.setCompileTimeInitializer(
c.storageManager.createNullableLazyValue { c.storageManager.createNullableLazyValue {
val container = c.containingDeclaration.asProtoContainer()!! val container = c.containingDeclaration.asProtoContainer()!!
c.components.annotationAndConstantLoader.loadPropertyConstant(container, proto, c.nameResolver, property.getReturnType()) c.components.annotationAndConstantLoader.loadPropertyConstant(container, proto, property.returnType)
} }
) )
} }
@@ -184,9 +184,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
} }
return DeserializedAnnotationsWithPossibleTargets(c.storageManager) { return DeserializedAnnotationsWithPossibleTargets(c.storageManager) {
c.containingDeclaration.asProtoContainer()?.let { c.containingDeclaration.asProtoContainer()?.let {
c.components.annotationAndConstantLoader.loadCallableAnnotations( c.components.annotationAndConstantLoader.loadCallableAnnotations(it, proto, kind)
it, proto, c.nameResolver, kind
)
}.orEmpty() }.orEmpty()
} }
} }
@@ -200,7 +198,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
if (proto.hasReceiverType()) { if (proto.hasReceiverType()) {
c.containingDeclaration.asProtoContainer()?.let { c.containingDeclaration.asProtoContainer()?.let {
c.components.annotationAndConstantLoader c.components.annotationAndConstantLoader
.loadExtensionReceiverParameterAnnotations(it, proto, c.nameResolver, receiverTargetedKind) .loadExtensionReceiverParameterAnnotations(it, proto, receiverTargetedKind)
.map { AnnotationWithTarget(it, AnnotationUseSiteTarget.RECEIVER) } .map { AnnotationWithTarget(it, AnnotationUseSiteTarget.RECEIVER) }
}.orEmpty() }.orEmpty()
} }
@@ -234,9 +232,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
valueParameter: ProtoBuf.ValueParameter valueParameter: ProtoBuf.ValueParameter
): Annotations { ): Annotations {
return DeserializedAnnotations(c.storageManager) { return DeserializedAnnotations(c.storageManager) {
c.components.annotationAndConstantLoader.loadValueParameterAnnotations( c.components.annotationAndConstantLoader.loadValueParameterAnnotations(container, callable, kind, index, valueParameter)
container, callable, c.nameResolver, kind, index, valueParameter
)
} }
} }
@@ -28,9 +28,6 @@ public data class ProtoContainer(
assert((classProto != null) xor (packageFqName != null)) assert((classProto != null) xor (packageFqName != null))
} }
fun getFqName(nameResolver: NameResolver): FqName { fun getFqName(): FqName =
if (packageFqName != null) return packageFqName packageFqName ?: nameResolver.getClassId(classProto!!.getFqName()).asSingleFqName()
return nameResolver.getClassId(classProto!!.getFqName()).asSingleFqName()
}
} }
@@ -36,26 +36,26 @@ public class AnnotationLoaderForKotlinJavaScriptStubBuilder() : AnnotationAndCon
override fun loadCallableAnnotations( override fun loadCallableAnnotations(
container: ProtoContainer, container: ProtoContainer,
proto: ProtoBuf.Callable, proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<ClassIdWithTarget> { ): List<ClassIdWithTarget> =
return proto.getExtension(JsProtoBuf.callableAnnotation).orEmpty().map { ClassIdWithTarget(nameResolver.getClassId(it.id), null) } proto.getExtension(JsProtoBuf.callableAnnotation).orEmpty().map {
} ClassIdWithTarget(container.nameResolver.getClassId(it.id), null)
}
override fun loadValueParameterAnnotations( override fun loadValueParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
callable: ProtoBuf.Callable, callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind, kind: AnnotatedCallableKind,
parameterIndex: Int, parameterIndex: Int,
proto: ProtoBuf.ValueParameter proto: ProtoBuf.ValueParameter
): List<ClassId> = ): List<ClassId> =
proto.getExtension(JsProtoBuf.parameterAnnotation).orEmpty().map { nameResolver.getClassId(it.id) } proto.getExtension(JsProtoBuf.parameterAnnotation).orEmpty().map {
container.nameResolver.getClassId(it.id)
}
override fun loadExtensionReceiverParameterAnnotations( override fun loadExtensionReceiverParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
callable: ProtoBuf.Callable, callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<ClassId> = emptyList() ): List<ClassId> = emptyList()
@@ -68,7 +68,6 @@ public class AnnotationLoaderForKotlinJavaScriptStubBuilder() : AnnotationAndCon
override fun loadPropertyConstant( override fun loadPropertyConstant(
container: ProtoContainer, container: ProtoContainer,
proto: ProtoBuf.Callable, proto: ProtoBuf.Callable,
nameResolver: NameResolver,
expectedType: JetType expectedType: JetType
): Unit {} ) {}
} }
@@ -110,7 +110,7 @@ private class CallableClsStubBuilder(
) )
val kind = callableProto.annotatedCallableKind val kind = callableProto.annotatedCallableKind
val annotationIds = c.components.annotationLoader.loadCallableAnnotations(protoContainer, callableProto, c.nameResolver, kind) val annotationIds = c.components.annotationLoader.loadCallableAnnotations(protoContainer, callableProto, kind)
createTargetedAnnotationStubs(annotationIds, modifierListStubImpl) createTargetedAnnotationStubs(annotationIds, modifierListStubImpl)
} }
@@ -157,7 +157,7 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
val isVararg = valueParameterProto.hasVarargElementType() val isVararg = valueParameterProto.hasVarargElementType()
val modifierList = if (isVararg) createModifierListStub(parameterStub, listOf(JetTokens.VARARG_KEYWORD)) else null val modifierList = if (isVararg) createModifierListStub(parameterStub, listOf(JetTokens.VARARG_KEYWORD)) else null
val parameterAnnotations = c.components.annotationLoader.loadValueParameterAnnotations( val parameterAnnotations = c.components.annotationLoader.loadValueParameterAnnotations(
container, callableProto, c.nameResolver, callableProto.annotatedCallableKind, index, valueParameterProto container, callableProto, callableProto.annotatedCallableKind, index, valueParameterProto
) )
if (parameterAnnotations.isNotEmpty()) { if (parameterAnnotations.isNotEmpty()) {
createAnnotationStubs(parameterAnnotations, modifierList ?: createEmptyModifierList(parameterStub)) createAnnotationStubs(parameterAnnotations, modifierList ?: createEmptyModifierList(parameterStub))
@@ -40,29 +40,26 @@ class KotlinJavascriptAnnotationAndConstantLoader(
override fun loadCallableAnnotations( override fun loadCallableAnnotations(
container: ProtoContainer, container: ProtoContainer,
proto: ProtoBuf.Callable, proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<AnnotationWithTarget> { ): List<AnnotationWithTarget> {
val annotations = proto.getExtension(JsProtoBuf.callableAnnotation).orEmpty() val annotations = proto.getExtension(JsProtoBuf.callableAnnotation).orEmpty()
return annotations.map { proto -> AnnotationWithTarget(deserializer.deserializeAnnotation(proto, nameResolver), null) } return annotations.map { proto -> AnnotationWithTarget(deserializer.deserializeAnnotation(proto, container.nameResolver), null) }
} }
override fun loadValueParameterAnnotations( override fun loadValueParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
callable: ProtoBuf.Callable, callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind, kind: AnnotatedCallableKind,
parameterIndex: Int, parameterIndex: Int,
proto: ProtoBuf.ValueParameter proto: ProtoBuf.ValueParameter
): List<AnnotationDescriptor> { ): List<AnnotationDescriptor> {
val annotations = proto.getExtension(JsProtoBuf.parameterAnnotation).orEmpty() val annotations = proto.getExtension(JsProtoBuf.parameterAnnotation).orEmpty()
return annotations.map { proto -> deserializer.deserializeAnnotation(proto, nameResolver) } return annotations.map { proto -> deserializer.deserializeAnnotation(proto, container.nameResolver) }
} }
override fun loadExtensionReceiverParameterAnnotations( override fun loadExtensionReceiverParameterAnnotations(
container: ProtoContainer, container: ProtoContainer,
callable: ProtoBuf.Callable, callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<AnnotationDescriptor> = emptyList() ): List<AnnotationDescriptor> = emptyList()
@@ -74,10 +71,9 @@ class KotlinJavascriptAnnotationAndConstantLoader(
override fun loadPropertyConstant( override fun loadPropertyConstant(
container: ProtoContainer, container: ProtoContainer,
proto: ProtoBuf.Callable, proto: ProtoBuf.Callable,
nameResolver: NameResolver,
expectedType: JetType expectedType: JetType
): ConstantValue<*>? { ): ConstantValue<*>? {
val value = proto.getExtension(JsProtoBuf.compileTimeValue) val value = proto.getExtension(JsProtoBuf.compileTimeValue)
return deserializer.resolveValue(expectedType, value, nameResolver) return deserializer.resolveValue(expectedType, value, container.nameResolver)
} }
} }