Add ModuleDescriptor to AnnotationCodegen

This commit is contained in:
Alexander Udalov
2018-09-05 13:18:10 +03:00
parent ffdfec4815
commit 9183d7ca27
10 changed files with 35 additions and 38 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen;
import kotlin.collections.CollectionsKt; import kotlin.collections.CollectionsKt;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper; import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
import org.jetbrains.kotlin.config.JvmTarget; import org.jetbrains.kotlin.config.JvmTarget;
import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.*;
@@ -85,10 +86,12 @@ public abstract class AnnotationCodegen {
private final InnerClassConsumer innerClassConsumer; private final InnerClassConsumer innerClassConsumer;
private final KotlinTypeMapper typeMapper; private final KotlinTypeMapper typeMapper;
private final ModuleDescriptor module;
private AnnotationCodegen(@NotNull InnerClassConsumer innerClassConsumer, @NotNull KotlinTypeMapper typeMapper) { private AnnotationCodegen(@NotNull InnerClassConsumer innerClassConsumer, @NotNull GenerationState state) {
this.innerClassConsumer = innerClassConsumer; this.innerClassConsumer = innerClassConsumer;
this.typeMapper = typeMapper; this.typeMapper = state.getTypeMapper();
this.module = state.getModule();
} }
/** /**
@@ -547,9 +550,9 @@ public abstract class AnnotationCodegen {
public static AnnotationCodegen forClass( public static AnnotationCodegen forClass(
@NotNull ClassVisitor cv, @NotNull ClassVisitor cv,
@NotNull InnerClassConsumer innerClassConsumer, @NotNull InnerClassConsumer innerClassConsumer,
@NotNull KotlinTypeMapper mapper @NotNull GenerationState state
) { ) {
return new AnnotationCodegen(innerClassConsumer, mapper) { return new AnnotationCodegen(innerClassConsumer, state) {
@NotNull @NotNull
@Override @Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) { AnnotationVisitor visitAnnotation(String descr, boolean visible) {
@@ -561,9 +564,9 @@ public abstract class AnnotationCodegen {
public static AnnotationCodegen forMethod( public static AnnotationCodegen forMethod(
@NotNull MethodVisitor mv, @NotNull MethodVisitor mv,
@NotNull InnerClassConsumer innerClassConsumer, @NotNull InnerClassConsumer innerClassConsumer,
@NotNull KotlinTypeMapper mapper @NotNull GenerationState state
) { ) {
return new AnnotationCodegen(innerClassConsumer, mapper) { return new AnnotationCodegen(innerClassConsumer, state) {
@NotNull @NotNull
@Override @Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) { AnnotationVisitor visitAnnotation(String descr, boolean visible) {
@@ -575,9 +578,9 @@ public abstract class AnnotationCodegen {
public static AnnotationCodegen forField( public static AnnotationCodegen forField(
@NotNull FieldVisitor fv, @NotNull FieldVisitor fv,
@NotNull InnerClassConsumer innerClassConsumer, @NotNull InnerClassConsumer innerClassConsumer,
@NotNull KotlinTypeMapper mapper @NotNull GenerationState state
) { ) {
return new AnnotationCodegen(innerClassConsumer, mapper) { return new AnnotationCodegen(innerClassConsumer, state) {
@NotNull @NotNull
@Override @Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) { AnnotationVisitor visitAnnotation(String descr, boolean visible) {
@@ -590,9 +593,9 @@ public abstract class AnnotationCodegen {
int parameter, int parameter,
@NotNull MethodVisitor mv, @NotNull MethodVisitor mv,
@NotNull InnerClassConsumer innerClassConsumer, @NotNull InnerClassConsumer innerClassConsumer,
@NotNull KotlinTypeMapper mapper @NotNull GenerationState state
) { ) {
return new AnnotationCodegen(innerClassConsumer, mapper) { return new AnnotationCodegen(innerClassConsumer, state) {
@NotNull @NotNull
@Override @Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) { AnnotationVisitor visitAnnotation(String descr, boolean visible) {
@@ -604,9 +607,9 @@ public abstract class AnnotationCodegen {
public static AnnotationCodegen forAnnotationDefaultValue( public static AnnotationCodegen forAnnotationDefaultValue(
@NotNull MethodVisitor mv, @NotNull MethodVisitor mv,
@NotNull InnerClassConsumer innerClassConsumer, @NotNull InnerClassConsumer innerClassConsumer,
@NotNull KotlinTypeMapper mapper @NotNull GenerationState state
) { ) {
return new AnnotationCodegen(innerClassConsumer, mapper) { return new AnnotationCodegen(innerClassConsumer, state) {
@NotNull @NotNull
@Override @Override
AnnotationVisitor visitAnnotation(String descr, boolean visible) { AnnotationVisitor visitAnnotation(String descr, boolean visible) {
@@ -140,7 +140,7 @@ class DefaultParameterValueSubstitutor(val state: GenerationState) {
FunctionCodegen.getThrownExceptions(functionDescriptor, typeMapper) FunctionCodegen.getThrownExceptions(functionDescriptor, typeMapper)
) )
AnnotationCodegen.forMethod(mv, memberCodegen, typeMapper).genAnnotations(functionDescriptor, signature.returnType) AnnotationCodegen.forMethod(mv, memberCodegen, state).genAnnotations(functionDescriptor, signature.returnType)
if (state.classBuilderMode == ClassBuilderMode.KAPT3) { if (state.classBuilderMode == ClassBuilderMode.KAPT3) {
mv.visitAnnotation(ANNOTATION_TYPE_DESCRIPTOR_FOR_JVM_OVERLOADS_GENERATED_METHODS, false) mv.visitAnnotation(ANNOTATION_TYPE_DESCRIPTOR_FOR_JVM_OVERLOADS_GENERATED_METHODS, false)
@@ -307,7 +307,7 @@ public class FunctionCodegen {
annotationsOwner = functionDescriptor; annotationsOwner = functionDescriptor;
} }
AnnotationCodegen.forMethod(mv, memberCodegen, typeMapper).genAnnotations(annotationsOwner, asmMethod.getReturnType()); AnnotationCodegen.forMethod(mv, memberCodegen, state).genAnnotations(annotationsOwner, asmMethod.getReturnType());
generateParameterAnnotations(annotationsOwner, mv, jvmSignature, memberCodegen, state); generateParameterAnnotations(annotationsOwner, mv, jvmSignature, memberCodegen, state);
} }
@@ -514,7 +514,7 @@ public class FunctionCodegen {
@NotNull GenerationState state @NotNull GenerationState state
) { ) {
if (isAccessor(functionDescriptor)) return; if (isAccessor(functionDescriptor)) return;
KotlinTypeMapper typeMapper = state.getTypeMapper();
Iterator<ValueParameterDescriptor> iterator = valueParameters.iterator(); Iterator<ValueParameterDescriptor> iterator = valueParameters.iterator();
List<JvmMethodParameterSignature> kotlinParameterTypes = jvmSignature.getValueParameters(); List<JvmMethodParameterSignature> kotlinParameterTypes = jvmSignature.getValueParameters();
@@ -526,7 +526,6 @@ public class FunctionCodegen {
continue; continue;
} }
AnnotationCodegen annotationCodegen = AnnotationCodegen.forParameter(i, mv, innerClassConsumer, typeMapper);
Annotated annotated = Annotated annotated =
kind == JvmMethodParameterKind.VALUE kind == JvmMethodParameterKind.VALUE
? iterator.next() ? iterator.next()
@@ -535,7 +534,7 @@ public class FunctionCodegen {
: null; : null;
if (annotated != null) { if (annotated != null) {
annotationCodegen.genAnnotations(annotated, parameterSignature.getAsmType()); AnnotationCodegen.forParameter(i, mv, innerClassConsumer, state).genAnnotations(annotated, parameterSignature.getAsmType());
} }
} }
} }
@@ -1187,7 +1186,7 @@ public class FunctionCodegen {
// Only method annotations are copied to the $default method. Parameter annotations are not copied until there are valid use cases; // Only method annotations are copied to the $default method. Parameter annotations are not copied until there are valid use cases;
// enum constructors have two additional synthetic parameters which somewhat complicate this task // enum constructors have two additional synthetic parameters which somewhat complicate this task
AnnotationCodegen.forMethod(mv, memberCodegen, typeMapper).genAnnotations(functionDescriptor, defaultMethod.getReturnType()); AnnotationCodegen.forMethod(mv, memberCodegen, state).genAnnotations(functionDescriptor, defaultMethod.getReturnType());
if (!state.getClassBuilderMode().generateBodies) { if (!state.getClassBuilderMode().generateBodies) {
if (this.owner instanceof MultifileClassFacadeContext) if (this.owner instanceof MultifileClassFacadeContext)
@@ -228,7 +228,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
writeEnclosingMethod(); writeEnclosingMethod();
AnnotationCodegen.forClass(v.getVisitor(), this, typeMapper).genAnnotations(descriptor, null); AnnotationCodegen.forClass(v.getVisitor(), this, state).genAnnotations(descriptor, null);
generateEnumEntries(); generateEnumEntries();
} }
@@ -817,7 +817,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
FieldVisitor fv = v.newField(JvmDeclarationOriginKt.OtherOrigin(companionObject == null ? myClass.getPsiOrParent() : companionObject), FieldVisitor fv = v.newField(JvmDeclarationOriginKt.OtherOrigin(companionObject == null ? myClass.getPsiOrParent() : companionObject),
fieldAccessFlags, field.name, field.type.getDescriptor(), null, null); fieldAccessFlags, field.name, field.type.getDescriptor(), null, null);
if (fieldShouldBeDeprecated) { if (fieldShouldBeDeprecated) {
AnnotationCodegen.forField(fv, this, typeMapper).visitAnnotation("Ljava/lang/Deprecated;", true).visitEnd(); AnnotationCodegen.forField(fv, this, state).visitAnnotation("Ljava/lang/Deprecated;", true).visitEnd();
} }
} }
@@ -902,7 +902,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
type.getDescriptor(), typeMapper.mapFieldSignature(property.getType(), property), type.getDescriptor(), typeMapper.mapFieldSignature(property.getType(), property),
info.defaultValue); info.defaultValue);
AnnotationCodegen.forField(fv, this, typeMapper).genAnnotations(property, type); AnnotationCodegen.forField(fv, this, state).genAnnotations(property, type);
//This field are always static and final so if it has constant initializer don't do anything in clinit, //This field are always static and final so if it has constant initializer don't do anything in clinit,
//field would be initialized via default value in v.newField(...) - see JVM SPEC Ch.4 //field would be initialized via default value in v.newField(...) - see JVM SPEC Ch.4
@@ -1063,7 +1063,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
int isDeprecated = KotlinBuiltIns.isDeprecated(descriptor) ? ACC_DEPRECATED : 0; int isDeprecated = KotlinBuiltIns.isDeprecated(descriptor) ? ACC_DEPRECATED : 0;
FieldVisitor fv = v.newField(JvmDeclarationOriginKt.OtherOrigin(enumEntry, descriptor), ACC_PUBLIC | ACC_ENUM | ACC_STATIC | ACC_FINAL | isDeprecated, FieldVisitor fv = v.newField(JvmDeclarationOriginKt.OtherOrigin(enumEntry, descriptor), ACC_PUBLIC | ACC_ENUM | ACC_STATIC | ACC_FINAL | isDeprecated,
descriptor.getName().asString(), classAsmType.getDescriptor(), null, null); descriptor.getName().asString(), classAsmType.getDescriptor(), null, null);
AnnotationCodegen.forField(fv, this, typeMapper).genAnnotations(descriptor, null); AnnotationCodegen.forField(fv, this, state).genAnnotations(descriptor, null);
} }
initializeEnumConstants(enumEntries); initializeEnumConstants(enumEntries);
@@ -260,7 +260,7 @@ public abstract class MemberCodegen<T extends KtPureElement/* TODO: & KtDeclarat
int flags = ACC_DEPRECATED | ACC_STATIC | ACC_SYNTHETIC | AsmUtil.getVisibilityAccessFlag(descriptor); int flags = ACC_DEPRECATED | ACC_STATIC | ACC_SYNTHETIC | AsmUtil.getVisibilityAccessFlag(descriptor);
MethodVisitor mv = v.newMethod(JvmDeclarationOriginKt.OtherOrigin(descriptor), flags, syntheticMethod.getName(), MethodVisitor mv = v.newMethod(JvmDeclarationOriginKt.OtherOrigin(descriptor), flags, syntheticMethod.getName(),
syntheticMethod.getDescriptor(), null, null); syntheticMethod.getDescriptor(), null, null);
AnnotationCodegen.forMethod(mv, this, typeMapper).genAnnotations(new AnnotatedImpl(annotations), Type.VOID_TYPE); AnnotationCodegen.forMethod(mv, this, state).genAnnotations(new AnnotatedImpl(annotations), Type.VOID_TYPE);
mv.visitCode(); mv.visitCode();
mv.visitInsn(Opcodes.RETURN); mv.visitInsn(Opcodes.RETURN);
mv.visitEnd(); mv.visitEnd();
@@ -85,7 +85,7 @@ public class PackagePartCodegen extends MemberCodegen<KtFile> {
} }
} }
Annotated annotatedFile = new AnnotatedImpl(Annotations.Companion.create(fileAnnotationDescriptors)); Annotated annotatedFile = new AnnotatedImpl(Annotations.Companion.create(fileAnnotationDescriptors));
AnnotationCodegen.forClass(v.getVisitor(), this, state.getTypeMapper()).genAnnotations(annotatedFile, null); AnnotationCodegen.forClass(v.getVisitor(), this, state).genAnnotations(annotatedFile, null);
} }
@Override @Override
@@ -272,7 +272,7 @@ public class PropertyCodegen {
PropertyGetterDescriptor getter = descriptor.getGetter(); PropertyGetterDescriptor getter = descriptor.getGetter();
assert getter != null : "Annotation property should have a getter: " + descriptor; assert getter != null : "Annotation property should have a getter: " + descriptor;
v.getSerializationBindings().put(METHOD_FOR_FUNCTION, getter, asmMethod); v.getSerializationBindings().put(METHOD_FOR_FUNCTION, getter, asmMethod);
AnnotationCodegen.forMethod(mv, memberCodegen, typeMapper).genAnnotations(getter, asmMethod.getReturnType()); AnnotationCodegen.forMethod(mv, memberCodegen, state).genAnnotations(getter, asmMethod.getReturnType());
KtExpression defaultValue = loadAnnotationArgumentDefaultValue(parameter, descriptor, expectedAnnotationConstructor); KtExpression defaultValue = loadAnnotationArgumentDefaultValue(parameter, descriptor, expectedAnnotationConstructor);
if (defaultValue != null) { if (defaultValue != null) {
@@ -281,7 +281,7 @@ public class PropertyCodegen {
assert !state.getClassBuilderMode().generateBodies || constant != null assert !state.getClassBuilderMode().generateBodies || constant != null
: "Default value for annotation parameter should be compile time value: " + defaultValue.getText(); : "Default value for annotation parameter should be compile time value: " + defaultValue.getText();
if (constant != null) { if (constant != null) {
AnnotationCodegen annotationCodegen = AnnotationCodegen.forAnnotationDefaultValue(mv, memberCodegen, typeMapper); AnnotationCodegen annotationCodegen = AnnotationCodegen.forAnnotationDefaultValue(mv, memberCodegen, state);
annotationCodegen.generateAnnotationDefaultValue(constant, descriptor.getType()); annotationCodegen.generateAnnotationDefaultValue(constant, descriptor.getType());
} }
} }
@@ -432,7 +432,7 @@ public class PropertyCodegen {
); );
if (annotatedField != null) { if (annotatedField != null) {
AnnotationCodegen.forField(fv, memberCodegen, typeMapper).genAnnotations(annotatedField, type); AnnotationCodegen.forField(fv, memberCodegen, state).genAnnotations(annotatedField, type);
} }
} }
} }
@@ -49,7 +49,7 @@ class ScriptCodegen private constructor(
typeMapper.mapSupertype(scriptDescriptor.getSuperClassOrAny().defaultType, null).internalName, typeMapper.mapSupertype(scriptDescriptor.getSuperClassOrAny().defaultType, null).internalName,
mapSupertypesNames(typeMapper, scriptDescriptor.getSuperInterfaces(), null) mapSupertypesNames(typeMapper, scriptDescriptor.getSuperInterfaces(), null)
) )
AnnotationCodegen.forClass(v.visitor, this, typeMapper).genAnnotations(scriptDescriptor, null) AnnotationCodegen.forClass(v.visitor, this, state).genAnnotations(scriptDescriptor, null)
} }
override fun generateBody() { override fun generateBody() {
@@ -99,9 +99,7 @@ class ScriptCodegen private constructor(
OtherOrigin(scriptDeclaration, scriptDescriptor.unsubstitutedPrimaryConstructor), OtherOrigin(scriptDeclaration, scriptDescriptor.unsubstitutedPrimaryConstructor),
ACC_PUBLIC, jvmSignature.asmMethod.name, jvmSignature.asmMethod.descriptor, null, null) ACC_PUBLIC, jvmSignature.asmMethod.name, jvmSignature.asmMethod.descriptor, null, null)
AnnotationCodegen.forMethod(mv, this, typeMapper).genAnnotations( AnnotationCodegen.forMethod(mv, this, state).genAnnotations(scriptDescriptor.unsubstitutedPrimaryConstructor, asmMethod.returnType)
scriptDescriptor.unsubstitutedPrimaryConstructor, asmMethod.returnType
)
if (state.classBuilderMode.generateBodies) { if (state.classBuilderMode.generateBodies) {
mv.visitCode() mv.visitCode()
@@ -36,7 +36,6 @@ import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.types.ErrorUtils import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.org.objectweb.asm.Opcodes import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.Type
import java.lang.RuntimeException
open class ClassCodegen protected constructor( open class ClassCodegen protected constructor(
internal val irClass: IrClass, internal val irClass: IrClass,
@@ -84,7 +83,7 @@ open class ClassCodegen protected constructor(
signature.superclassName, signature.superclassName,
signature.interfaces.toTypedArray() signature.interfaces.toTypedArray()
) )
AnnotationCodegen.forClass(visitor.visitor, this, typeMapper).genAnnotations(descriptor, null) AnnotationCodegen.forClass(visitor.visitor, this, state).genAnnotations(descriptor, null)
visitor.visitSource(irClass.symbol.descriptor.source.containingFile.name!!, null) visitor.visitSource(irClass.symbol.descriptor.source.containingFile.name!!, null)
irClass.declarations.forEach { irClass.declarations.forEach {
@@ -159,7 +158,7 @@ open class ClassCodegen protected constructor(
) )
if (field.origin == IrDeclarationOrigin.FIELD_FOR_ENUM_ENTRY) { if (field.origin == IrDeclarationOrigin.FIELD_FOR_ENUM_ENTRY) {
AnnotationCodegen.forField(fv, this, typeMapper).genAnnotations(field.descriptor, null) AnnotationCodegen.forField(fv, this, state).genAnnotations(field.descriptor, null)
} else { } else {
} }
@@ -48,9 +48,7 @@ open class FunctionCodegen(private val irFunction: IrFunction, private val class
val flags = calculateMethodFlags(irFunction.isStatic) val flags = calculateMethodFlags(irFunction.isStatic)
val methodVisitor = createMethod(flags, signature) val methodVisitor = createMethod(flags, signature)
AnnotationCodegen.forMethod(methodVisitor, classCodegen, state.typeMapper).genAnnotations( AnnotationCodegen.forMethod(methodVisitor, classCodegen, state).genAnnotations(descriptor, signature.asmMethod.returnType)
descriptor, signature.asmMethod.returnType
)
FunctionCodegen.generateParameterAnnotations(descriptor, methodVisitor, signature, classCodegen, state) FunctionCodegen.generateParameterAnnotations(descriptor, methodVisitor, signature, classCodegen, state)
if (!state.classBuilderMode.generateBodies || flags.and(Opcodes.ACC_ABSTRACT) != 0 || irFunction.isExternal) { if (!state.classBuilderMode.generateBodies || flags.and(Opcodes.ACC_ABSTRACT) != 0 || irFunction.isExternal) {
@@ -113,7 +111,7 @@ open class FunctionCodegen(private val irFunction: IrFunction, private val class
) )
assert(!state.classBuilderMode.generateBodies || constant != null) { "Default value for annotation parameter should be compile time value: " + defaultValue.text } assert(!state.classBuilderMode.generateBodies || constant != null) { "Default value for annotation parameter should be compile time value: " + defaultValue.text }
if (constant != null) { if (constant != null) {
val annotationCodegen = AnnotationCodegen.forAnnotationDefaultValue(methodVisitor, classCodegen, state.typeMapper) val annotationCodegen = AnnotationCodegen.forAnnotationDefaultValue(methodVisitor, classCodegen, state)
annotationCodegen.generateAnnotationDefaultValue(constant, descriptor.returnType!!) annotationCodegen.generateAnnotationDefaultValue(constant, descriptor.returnType!!)
} }
} }