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
}
override fun loadCallableAnnotations(
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<T> {
override fun loadCallableAnnotations(container: ProtoContainer, proto: ProtoBuf.Callable, kind: AnnotatedCallableKind): List<T> {
val nameResolver = container.nameResolver
if (kind == AnnotatedCallableKind.PROPERTY) {
val syntheticFunctionSignature = getPropertySignature(proto, nameResolver, synthetic = true)
val fieldSignature = getPropertySignature(proto, nameResolver, field = true)
val propertyAnnotations = syntheticFunctionSignature?.let { sig ->
findClassAndLoadMemberAnnotations(container, proto, nameResolver, sig)
findClassAndLoadMemberAnnotations(container, proto, sig)
} ?: listOf()
val fieldAnnotations = fieldSignature?.let { sig ->
findClassAndLoadMemberAnnotations(container, proto, nameResolver, sig, isStaticFieldInOuter(proto))
findClassAndLoadMemberAnnotations(container, proto, sig, isStaticFieldInOuter(proto))
} ?: listOf()
return loadPropertyAnnotations(propertyAnnotations, fieldAnnotations)
}
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>
@@ -116,15 +112,14 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
private fun findClassAndLoadMemberAnnotations(
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
signature: MemberSignature,
isStaticFieldInOuter: Boolean = false
): List<A> {
val kotlinClass = findClassWithAnnotationsAndInitializers(
container, nameResolver, getImplClassName(proto, nameResolver), isStaticFieldInOuter
container, getImplClassName(proto, container.nameResolver), isStaticFieldInOuter
)
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()
}
@@ -134,16 +129,15 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
override fun loadValueParameterAnnotations(
container: ProtoContainer,
callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind,
parameterIndex: Int,
proto: ProtoBuf.ValueParameter
): List<A> {
val methodSignature = getCallableSignature(callable, nameResolver, kind)
val methodSignature = getCallableSignature(callable, container.nameResolver, kind)
if (methodSignature != null) {
val index = if (proto.hasExtension(index)) proto.getExtension(index) else parameterIndex
val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, index)
return findClassAndLoadMemberAnnotations(container, callable, nameResolver, paramSignature)
return findClassAndLoadMemberAnnotations(container, callable, paramSignature)
}
return listOf()
@@ -152,14 +146,13 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
override fun loadExtensionReceiverParameterAnnotations(
container: ProtoContainer,
callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<A> {
if (!callable.hasReceiverType()) return emptyList()
val methodSignature = getCallableSignature(callable, nameResolver, kind)
val methodSignature = getCallableSignature(callable, container.nameResolver, kind)
if (methodSignature != null) {
val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, 0)
return findClassAndLoadMemberAnnotations(container, callable, nameResolver, paramSignature)
return findClassAndLoadMemberAnnotations(container, callable, paramSignature)
}
return emptyList()
@@ -169,19 +162,15 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
return type.getExtension(JvmProtoBuf.typeAnnotation).map { loadTypeAnnotation(it, nameResolver) }
}
override fun loadPropertyConstant(
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
expectedType: JetType
): C? {
override fun loadPropertyConstant(container: ProtoContainer, proto: ProtoBuf.Callable, expectedType: JetType): C? {
val nameResolver = container.nameResolver
val signature = getCallableSignature(proto, nameResolver, AnnotatedCallableKind.PROPERTY) ?: return null
val kotlinClass = findClassWithAnnotationsAndInitializers(
container, nameResolver, getImplClassName(proto, nameResolver), isStaticFieldInOuter(proto)
container, getImplClassName(proto, nameResolver), isStaticFieldInOuter(proto)
)
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
}
@@ -189,10 +178,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
}
private fun findClassWithAnnotationsAndInitializers(
container: ProtoContainer,
nameResolver: NameResolver,
implClassName: Name?,
isStaticFieldInOuter: Boolean
container: ProtoContainer, implClassName: Name?, isStaticFieldInOuter: Boolean
): KotlinJvmBinaryClass? {
val (classProto, packageFqName) = container
return when {
@@ -200,7 +186,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
implClassName?.let { kotlinClassFinder.findKotlinClass(ClassId(packageFqName, it)) }
}
classProto != null -> {
val classId = nameResolver.getClassId(classProto.fqName)
val classId = container.nameResolver.getClassId(classProto.fqName)
if (implClassName != null) {
// TODO: store accurate name for nested traits
@@ -41,29 +41,26 @@ class BuiltInsAnnotationAndConstantLoader(
override fun loadCallableAnnotations(
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<AnnotationWithTarget> {
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(
container: ProtoContainer,
callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind,
parameterIndex: Int,
proto: ProtoBuf.ValueParameter
): List<AnnotationDescriptor> {
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(
container: ProtoContainer,
callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<AnnotationDescriptor> = emptyList()
@@ -75,10 +72,9 @@ class BuiltInsAnnotationAndConstantLoader(
override fun loadPropertyConstant(
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
expectedType: JetType
): ConstantValue<*>? {
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(
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
);
@@ -42,7 +41,6 @@ public interface AnnotationAndConstantLoader<A, C, T> {
List<A> loadValueParameterAnnotations(
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable callable,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind,
int parameterIndex,
@NotNull ProtoBuf.ValueParameter proto
@@ -52,7 +50,6 @@ public interface AnnotationAndConstantLoader<A, C, T> {
List<A> loadExtensionReceiverParameterAnnotations(
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable callable,
@NotNull NameResolver nameResolver,
@NotNull AnnotatedCallableKind kind
);
@@ -66,7 +63,6 @@ public interface AnnotationAndConstantLoader<A, C, T> {
C loadPropertyConstant(
@NotNull ProtoContainer container,
@NotNull ProtoBuf.Callable proto,
@NotNull NameResolver nameResolver,
@NotNull JetType expectedType
);
}
@@ -129,7 +129,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
property.setCompileTimeInitializer(
c.storageManager.createNullableLazyValue {
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) {
c.containingDeclaration.asProtoContainer()?.let {
c.components.annotationAndConstantLoader.loadCallableAnnotations(
it, proto, c.nameResolver, kind
)
c.components.annotationAndConstantLoader.loadCallableAnnotations(it, proto, kind)
}.orEmpty()
}
}
@@ -200,7 +198,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
if (proto.hasReceiverType()) {
c.containingDeclaration.asProtoContainer()?.let {
c.components.annotationAndConstantLoader
.loadExtensionReceiverParameterAnnotations(it, proto, c.nameResolver, receiverTargetedKind)
.loadExtensionReceiverParameterAnnotations(it, proto, receiverTargetedKind)
.map { AnnotationWithTarget(it, AnnotationUseSiteTarget.RECEIVER) }
}.orEmpty()
}
@@ -234,9 +232,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
valueParameter: ProtoBuf.ValueParameter
): Annotations {
return DeserializedAnnotations(c.storageManager) {
c.components.annotationAndConstantLoader.loadValueParameterAnnotations(
container, callable, c.nameResolver, kind, index, valueParameter
)
c.components.annotationAndConstantLoader.loadValueParameterAnnotations(container, callable, kind, index, valueParameter)
}
}
@@ -28,9 +28,6 @@ public data class ProtoContainer(
assert((classProto != null) xor (packageFqName != null))
}
fun getFqName(nameResolver: NameResolver): FqName {
if (packageFqName != null) return packageFqName
return nameResolver.getClassId(classProto!!.getFqName()).asSingleFqName()
}
fun getFqName(): FqName =
packageFqName ?: nameResolver.getClassId(classProto!!.getFqName()).asSingleFqName()
}
@@ -36,26 +36,26 @@ public class AnnotationLoaderForKotlinJavaScriptStubBuilder() : AnnotationAndCon
override fun loadCallableAnnotations(
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<ClassIdWithTarget> {
return proto.getExtension(JsProtoBuf.callableAnnotation).orEmpty().map { ClassIdWithTarget(nameResolver.getClassId(it.id), null) }
}
): List<ClassIdWithTarget> =
proto.getExtension(JsProtoBuf.callableAnnotation).orEmpty().map {
ClassIdWithTarget(container.nameResolver.getClassId(it.id), null)
}
override fun loadValueParameterAnnotations(
container: ProtoContainer,
callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind,
parameterIndex: Int,
proto: ProtoBuf.ValueParameter
): 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(
container: ProtoContainer,
callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<ClassId> = emptyList()
@@ -68,7 +68,6 @@ public class AnnotationLoaderForKotlinJavaScriptStubBuilder() : AnnotationAndCon
override fun loadPropertyConstant(
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
expectedType: JetType
): Unit {}
) {}
}
@@ -110,7 +110,7 @@ private class CallableClsStubBuilder(
)
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)
}
@@ -157,7 +157,7 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
val isVararg = valueParameterProto.hasVarargElementType()
val modifierList = if (isVararg) createModifierListStub(parameterStub, listOf(JetTokens.VARARG_KEYWORD)) else null
val parameterAnnotations = c.components.annotationLoader.loadValueParameterAnnotations(
container, callableProto, c.nameResolver, callableProto.annotatedCallableKind, index, valueParameterProto
container, callableProto, callableProto.annotatedCallableKind, index, valueParameterProto
)
if (parameterAnnotations.isNotEmpty()) {
createAnnotationStubs(parameterAnnotations, modifierList ?: createEmptyModifierList(parameterStub))
@@ -40,29 +40,26 @@ class KotlinJavascriptAnnotationAndConstantLoader(
override fun loadCallableAnnotations(
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<AnnotationWithTarget> {
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(
container: ProtoContainer,
callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind,
parameterIndex: Int,
proto: ProtoBuf.ValueParameter
): List<AnnotationDescriptor> {
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(
container: ProtoContainer,
callable: ProtoBuf.Callable,
nameResolver: NameResolver,
kind: AnnotatedCallableKind
): List<AnnotationDescriptor> = emptyList()
@@ -74,10 +71,9 @@ class KotlinJavascriptAnnotationAndConstantLoader(
override fun loadPropertyConstant(
container: ProtoContainer,
proto: ProtoBuf.Callable,
nameResolver: NameResolver,
expectedType: JetType
): ConstantValue<*>? {
val value = proto.getExtension(JsProtoBuf.compileTimeValue)
return deserializer.resolveValue(expectedType, value, nameResolver)
return deserializer.resolveValue(expectedType, value, container.nameResolver)
}
}