JVM IR: do not use GenerationState.typeMapper
The only remaining usages are in IrTypeMapper, MethodSignatureMapper and in InterfaceDelegationLowering.generateDelegationToDefaultImpl
This commit is contained in:
@@ -41,12 +41,12 @@ class JvmBackendContext(
|
||||
private val firMode: Boolean
|
||||
) : CommonBackendContext {
|
||||
override val builtIns = state.module.builtIns
|
||||
override val declarationFactory: JvmDeclarationFactory = JvmDeclarationFactory(state)
|
||||
override val sharedVariablesManager = JvmSharedVariablesManager(state.module, builtIns, irBuiltIns)
|
||||
|
||||
val typeMapper = IrTypeMapper(this)
|
||||
val methodSignatureMapper = MethodSignatureMapper(this)
|
||||
|
||||
override val declarationFactory: JvmDeclarationFactory = JvmDeclarationFactory(methodSignatureMapper)
|
||||
override val sharedVariablesManager = JvmSharedVariablesManager(state.module, builtIns, irBuiltIns)
|
||||
|
||||
private val symbolTable = symbolTable.lazyWrapper
|
||||
override val ir = JvmIr(irModuleFragment, this.symbolTable)
|
||||
|
||||
|
||||
+18
-24
@@ -19,16 +19,19 @@ package org.jetbrains.kotlin.backend.jvm.codegen
|
||||
import org.jetbrains.annotations.NotNull
|
||||
import org.jetbrains.annotations.Nullable
|
||||
import org.jetbrains.kotlin.backend.common.ir.ir2string
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention
|
||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.types.*
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.classifierOrNull
|
||||
import org.jetbrains.kotlin.ir.types.isMarkedNullable
|
||||
import org.jetbrains.kotlin.ir.types.isNullable
|
||||
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
|
||||
import org.jetbrains.kotlin.ir.util.getAnnotation
|
||||
import org.jetbrains.kotlin.ir.util.isAnnotationClass
|
||||
@@ -42,11 +45,11 @@ import java.lang.annotation.RetentionPolicy
|
||||
|
||||
class AnnotationCodegen(
|
||||
private val innerClassConsumer: InnerClassConsumer,
|
||||
state: GenerationState,
|
||||
context: JvmBackendContext,
|
||||
private val visitAnnotation: (descriptor: String, visible: Boolean) -> AnnotationVisitor
|
||||
) {
|
||||
|
||||
private val typeMapper = state.typeMapper
|
||||
private val typeMapper = context.typeMapper
|
||||
private val methodSignatureMapper = context.methodSignatureMapper
|
||||
|
||||
/**
|
||||
* @param returnType can be null if not applicable (e.g. [annotated] is a class)
|
||||
@@ -153,15 +156,13 @@ class AnnotationCodegen(
|
||||
|
||||
private fun genAnnotation(annotation: IrConstructorCall): String? {
|
||||
val annotationClass = annotation.annotationClass
|
||||
val rp = getRetentionPolicy(annotationClass)
|
||||
if (rp == RetentionPolicy.SOURCE && !typeMapper.classBuilderMode.generateSourceRetentionAnnotations) {
|
||||
return null
|
||||
}
|
||||
val retentionPolicy = getRetentionPolicy(annotationClass)
|
||||
if (retentionPolicy == RetentionPolicy.SOURCE) return null
|
||||
|
||||
innerClassConsumer.addInnerClassInfoFromAnnotation(annotationClass)
|
||||
|
||||
val asmTypeDescriptor = typeMapper.mapType(annotation.type.toKotlinType()).descriptor
|
||||
val annotationVisitor = visitAnnotation(asmTypeDescriptor, rp == RetentionPolicy.RUNTIME)
|
||||
val asmTypeDescriptor = typeMapper.mapType(annotation.type).descriptor
|
||||
val annotationVisitor = visitAnnotation(asmTypeDescriptor, retentionPolicy == RetentionPolicy.RUNTIME)
|
||||
|
||||
genAnnotationArguments(annotation, annotationVisitor)
|
||||
annotationVisitor.visitEnd()
|
||||
@@ -188,7 +189,7 @@ class AnnotationCodegen(
|
||||
annotationClass.declarations.filterIsInstance<IrField>().singleOrNull { it.name == parameterName }
|
||||
?: return parameterName.asString()
|
||||
|
||||
return typeMapper.mapAnnotationParameterName(field.descriptor)
|
||||
return methodSignatureMapper.mapAnnotationParameterName(field)
|
||||
}
|
||||
|
||||
private fun genCompileTimeValue(
|
||||
@@ -196,20 +197,13 @@ class AnnotationCodegen(
|
||||
value: IrExpression,
|
||||
annotationVisitor: AnnotationVisitor
|
||||
) {
|
||||
fun visitUnsupportedValue(value: IrExpression) {
|
||||
val mode = typeMapper.classBuilderMode
|
||||
if (mode.generateBodies) {
|
||||
throw IllegalStateException("Don't know how to compile annotation value ${ir2string(value)}")
|
||||
}
|
||||
}
|
||||
|
||||
when (value) {
|
||||
is IrConst<*> -> annotationVisitor.visit(name, value.value)
|
||||
is IrConstructorCall -> {
|
||||
val callee = value.symbol.owner
|
||||
when {
|
||||
callee.parentAsClass.isAnnotationClass -> {
|
||||
val internalAnnName = typeMapper.mapType(callee.returnType.toKotlinType()).descriptor
|
||||
val internalAnnName = typeMapper.mapType(callee.returnType).descriptor
|
||||
val visitor = annotationVisitor.visitAnnotation(name, internalAnnName)
|
||||
genAnnotationArguments(value, visitor)
|
||||
visitor.visitEnd()
|
||||
@@ -218,7 +212,7 @@ class AnnotationCodegen(
|
||||
}
|
||||
}
|
||||
is IrGetEnumValue -> {
|
||||
val enumClassInternalName = typeMapper.mapClass(value.symbol.owner.parentAsClass.descriptor).descriptor
|
||||
val enumClassInternalName = typeMapper.mapClass(value.symbol.owner.parentAsClass).descriptor
|
||||
val enumEntryName = value.symbol.owner.name
|
||||
annotationVisitor.visitEnum(name, enumClassInternalName, enumEntryName.asString())
|
||||
}
|
||||
@@ -230,10 +224,10 @@ class AnnotationCodegen(
|
||||
visitor.visitEnd()
|
||||
}
|
||||
is IrClassReference -> {
|
||||
annotationVisitor.visit(name, typeMapper.mapType(value.classType.toKotlinType()))
|
||||
annotationVisitor.visit(name, typeMapper.mapType(value.classType))
|
||||
}
|
||||
is IrErrorExpression -> visitUnsupportedValue(value)
|
||||
else -> error("Unsupported compiletime value ${ir2string(value)}")
|
||||
is IrErrorExpression -> error("Don't know how to compile annotation value ${ir2string(value)}")
|
||||
else -> error("Unsupported compile-time value ${ir2string(value)}")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -91,7 +91,7 @@ open class ClassCodegen protected constructor(
|
||||
signature.superclassName,
|
||||
signature.interfaces.toTypedArray()
|
||||
)
|
||||
AnnotationCodegen(this, context.state, visitor.visitor::visitAnnotation).genAnnotations(irClass, null)
|
||||
AnnotationCodegen(this, context, visitor.visitor::visitAnnotation).genAnnotations(irClass, null)
|
||||
|
||||
val nestedClasses = irClass.declarations.mapNotNull { declaration ->
|
||||
if (declaration is IrClass) {
|
||||
@@ -258,7 +258,7 @@ open class ClassCodegen protected constructor(
|
||||
fieldSignature, field.constantValue(implicitConst)?.value
|
||||
)
|
||||
|
||||
AnnotationCodegen(this, state, fv::visitAnnotation).genAnnotations(field, fieldType)
|
||||
AnnotationCodegen(this, context, fv::visitAnnotation).genAnnotations(field, fieldType)
|
||||
|
||||
val descriptor = field.metadata?.descriptor
|
||||
if (descriptor != null) {
|
||||
|
||||
+2
-2
@@ -321,7 +321,7 @@ class ExpressionCodegen(
|
||||
?.invoke(expression, this, data)?.let { return it.coerce(expression.type) }
|
||||
|
||||
val isSuperCall = (expression as? IrCall)?.superQualifier != null
|
||||
val callable = resolveToCallable(expression, isSuperCall)
|
||||
val callable = methodSignatureMapper.mapToCallableMethod(expression, isSuperCall)
|
||||
val callee = expression.symbol.owner
|
||||
val callGenerator = getOrCreateCallGenerator(expression, data)
|
||||
val asmType = if (expression is IrConstructorCall) typeMapper.mapTypeAsDeclaration(expression.type) else expression.asmType
|
||||
@@ -985,7 +985,7 @@ class ExpressionCodegen(
|
||||
}
|
||||
|
||||
private fun resolveToCallable(irCall: IrFunctionAccessExpression, isSuper: Boolean): IrCallableMethod =
|
||||
methodSignatureMapper.mapToCallableMethod(irCall.symbol.owner, isSuper)
|
||||
methodSignatureMapper.mapToCallableMethod(irCall, isSuper)
|
||||
|
||||
private fun getOrCreateCallGenerator(element: IrFunctionAccessExpression, data: BlockInfo): IrCallGenerator {
|
||||
if (!element.symbol.owner.isInlineFunctionCall(context) ||
|
||||
|
||||
+13
-18
@@ -5,12 +5,12 @@
|
||||
|
||||
package org.jetbrains.kotlin.backend.jvm.codegen
|
||||
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns.FQ_NAMES
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil
|
||||
import org.jetbrains.kotlin.codegen.ClassBuilderMode
|
||||
import org.jetbrains.kotlin.codegen.OwnerKind
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.codegen.visitAnnotableParameterCount
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
@@ -34,7 +34,7 @@ open class FunctionCodegen(
|
||||
private val classCodegen: ClassCodegen,
|
||||
private val isInlineLambda: Boolean = false
|
||||
) {
|
||||
|
||||
val context = classCodegen.context
|
||||
val state = classCodegen.state
|
||||
|
||||
fun generate(): JvmMethodGenericSignature =
|
||||
@@ -58,26 +58,26 @@ open class FunctionCodegen(
|
||||
//TODO: investigate this case: annotation here is generated twice in lowered function and in interface method overload
|
||||
irFunction.origin == JvmLoweredDeclarationOrigin.GENERATED_SAM_IMPLEMENTATION
|
||||
) {
|
||||
AnnotationCodegen(classCodegen, state, methodVisitor::visitAnnotation).genAnnotations(
|
||||
AnnotationCodegen(classCodegen, context, methodVisitor::visitAnnotation).genAnnotations(
|
||||
irFunction,
|
||||
signature.asmMethod.returnType
|
||||
)
|
||||
generateParameterAnnotations(irFunction, methodVisitor, signature, classCodegen, state)
|
||||
generateParameterAnnotations(irFunction, methodVisitor, signature, classCodegen, context)
|
||||
}
|
||||
|
||||
if (!state.classBuilderMode.generateBodies || flags.and(Opcodes.ACC_ABSTRACT) != 0 || irFunction.isExternal) {
|
||||
generateAnnotationDefaultValueIfNeeded(methodVisitor)
|
||||
} else {
|
||||
val frameMap = createFrameMapWithReceivers(signature)
|
||||
val irClass = classCodegen.context.suspendFunctionContinuations[irFunction]
|
||||
val irClass = context.suspendFunctionContinuations[irFunction]
|
||||
val element = (irFunction.symbol.descriptor.psiElement
|
||||
?: classCodegen.context.suspendLambdaToOriginalFunctionMap[irFunction.parent]?.symbol?.descriptor?.psiElement) as? KtElement
|
||||
val continuationClassBuilder = classCodegen.context.continuationClassBuilders[irClass]
|
||||
?: context.suspendLambdaToOriginalFunctionMap[irFunction.parent]?.symbol?.descriptor?.psiElement) as? KtElement
|
||||
val continuationClassBuilder = context.continuationClassBuilders[irClass]
|
||||
methodVisitor = when {
|
||||
irFunction.isSuspend -> generateStateMachineForNamedFunction(
|
||||
irFunction, classCodegen, methodVisitor, flags, signature, continuationClassBuilder, element!!
|
||||
)
|
||||
irFunction.isInvokeSuspendOfLambda(classCodegen.context) -> generateStateMachineForLambda(
|
||||
irFunction.isInvokeSuspendOfLambda(context) -> generateStateMachineForLambda(
|
||||
classCodegen, methodVisitor, flags, signature, element!!
|
||||
)
|
||||
else -> methodVisitor
|
||||
@@ -149,7 +149,7 @@ open class FunctionCodegen(
|
||||
|
||||
private fun generateAnnotationDefaultValueIfNeeded(methodVisitor: MethodVisitor) {
|
||||
getAnnotationDefaultValueExpression()?.let { defaultValueExpression ->
|
||||
val annotationCodegen = AnnotationCodegen(classCodegen, state) { _, _ -> methodVisitor.visitAnnotationDefault() }
|
||||
val annotationCodegen = AnnotationCodegen(classCodegen, context) { _, _ -> methodVisitor.visitAnnotationDefault() }
|
||||
annotationCodegen.generateAnnotationDefaultValue(defaultValueExpression)
|
||||
}
|
||||
}
|
||||
@@ -208,7 +208,7 @@ fun generateParameterAnnotations(
|
||||
mv: MethodVisitor,
|
||||
jvmSignature: JvmMethodSignature,
|
||||
innerClassConsumer: InnerClassConsumer,
|
||||
state: GenerationState
|
||||
context: JvmBackendContext
|
||||
) {
|
||||
val iterator = irFunction.valueParameters.iterator()
|
||||
val kotlinParameterTypes = jvmSignature.valueParameters
|
||||
@@ -217,7 +217,7 @@ fun generateParameterAnnotations(
|
||||
val kind = parameterSignature.kind
|
||||
if (kind.isSkippedInGenericSignature) {
|
||||
if (AsmUtil.IS_BUILT_WITH_ASM6) {
|
||||
markEnumOrInnerConstructorParameterAsSynthetic(mv, i, state.classBuilderMode)
|
||||
markEnumOrInnerConstructorParameterAsSynthetic(mv, i, ClassBuilderMode.FULL)
|
||||
} else {
|
||||
syntheticParameterCount++
|
||||
}
|
||||
@@ -238,18 +238,13 @@ fun generateParameterAnnotations(
|
||||
}
|
||||
|
||||
if (annotated != null) {
|
||||
|
||||
AnnotationCodegen(
|
||||
innerClassConsumer,
|
||||
state
|
||||
) { descriptor, visible ->
|
||||
AnnotationCodegen(innerClassConsumer, context) { descriptor, visible ->
|
||||
mv.visitParameterAnnotation(
|
||||
if (AsmUtil.IS_BUILT_WITH_ASM6) i else i - syntheticParameterCount,
|
||||
descriptor,
|
||||
visible
|
||||
)
|
||||
}
|
||||
.genAnnotations(annotated, parameterSignature.asmType)
|
||||
}.genAnnotations(annotated, parameterSignature.asmType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -107,7 +107,7 @@ class IrSourceCompilerForInline(
|
||||
|
||||
irClass.declarations.filterIsInstance<IrFunction>().single {
|
||||
it.descriptor.name.asString() == jvmSignature.asmMethod.name + JvmAbi.DEFAULT_PARAMS_IMPL_SUFFIX &&
|
||||
state.typeMapper.mapSignatureSkipGeneric(callableDescriptor).asmMethod.descriptor.startsWith(
|
||||
codegen.context.methodSignatureMapper.mapSignatureSkipGeneric(callElement.symbol.owner).asmMethod.descriptor.startsWith(
|
||||
jvmSignature.asmMethod.descriptor.substringBeforeLast(')')
|
||||
)
|
||||
}
|
||||
|
||||
+10
-3
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrField
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
|
||||
import org.jetbrains.kotlin.ir.types.toKotlinType
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
|
||||
@@ -26,12 +27,18 @@ class MethodSignatureMapper(context: JvmBackendContext) {
|
||||
fun mapFieldSignature(irField: IrField): String? =
|
||||
kotlinTypeMapper.mapFieldSignature(irField.type.toKotlinType(), irField.descriptor)
|
||||
|
||||
fun mapFunctionName(irFunction: IrFunction, ownerKind: OwnerKind): String =
|
||||
fun mapFunctionName(irFunction: IrFunction, ownerKind: OwnerKind?): String =
|
||||
kotlinTypeMapper.mapFunctionName(irFunction.descriptor, ownerKind)
|
||||
|
||||
fun mapAnnotationParameterName(field: IrField): String =
|
||||
kotlinTypeMapper.mapAnnotationParameterName(field.descriptor)
|
||||
|
||||
fun mapImplementationOwner(irDeclaration: IrDeclaration): Type =
|
||||
kotlinTypeMapper.mapImplementationOwner(irDeclaration.descriptor)
|
||||
|
||||
fun mapReturnType(irField: IrField): Type =
|
||||
kotlinTypeMapper.mapReturnType(irField.descriptor)
|
||||
|
||||
fun mapReturnType(irFunction: IrFunction): Type =
|
||||
kotlinTypeMapper.mapReturnType(irFunction.descriptor)
|
||||
|
||||
@@ -41,8 +48,8 @@ class MethodSignatureMapper(context: JvmBackendContext) {
|
||||
fun mapSignatureWithGeneric(f: IrFunction, kind: OwnerKind): JvmMethodGenericSignature =
|
||||
kotlinTypeMapper.mapSignatureWithGeneric(f.descriptor, kind)
|
||||
|
||||
fun mapToCallableMethod(f: IrFunction, superCall: Boolean): IrCallableMethod =
|
||||
with(kotlinTypeMapper.mapToCallableMethod(f.descriptor, superCall)) {
|
||||
fun mapToCallableMethod(expression: IrFunctionAccessExpression, superCall: Boolean): IrCallableMethod =
|
||||
with(kotlinTypeMapper.mapToCallableMethod(expression.descriptor, superCall)) {
|
||||
IrCallableMethod(
|
||||
owner, valueParameterTypes, invokeOpcode, getAsmMethod(), dispatchReceiverType, extensionReceiverType, isInterfaceMethod
|
||||
)
|
||||
|
||||
+5
-2
@@ -19,7 +19,10 @@ import org.jetbrains.kotlin.codegen.signature.BothSignatureWriter
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
@@ -94,7 +97,7 @@ fun JvmBackendContext.getSourceMapper(declaration: IrClass): DefaultSourceMapper
|
||||
return DefaultSourceMapper(
|
||||
SourceInfo.createInfoForIr(
|
||||
endLineNumber + 1,
|
||||
this.state.typeMapper.mapType(declaration.descriptor).internalName,
|
||||
typeMapper.mapClass(declaration).internalName,
|
||||
declaration.fileParent.name
|
||||
)
|
||||
)
|
||||
|
||||
+3
-4
@@ -5,15 +5,14 @@
|
||||
|
||||
package org.jetbrains.kotlin.backend.jvm.descriptors
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.deepCopyWithWrappedDescriptors
|
||||
import org.jetbrains.kotlin.backend.common.descriptors.WrappedClassConstructorDescriptor
|
||||
import org.jetbrains.kotlin.backend.common.descriptors.WrappedClassDescriptor
|
||||
import org.jetbrains.kotlin.backend.common.descriptors.WrappedValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.backend.common.ir.*
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
|
||||
import org.jetbrains.kotlin.backend.jvm.codegen.MethodSignatureMapper
|
||||
import org.jetbrains.kotlin.builtins.CompanionObjectMapping.isMappedIntrinsicCompanionObject
|
||||
import org.jetbrains.kotlin.codegen.OwnerKind
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
@@ -35,7 +34,7 @@ import org.jetbrains.kotlin.name.Name
|
||||
import java.util.*
|
||||
|
||||
class JvmDeclarationFactory(
|
||||
private val state: GenerationState
|
||||
private val methodSignatureMapper: MethodSignatureMapper
|
||||
) : DeclarationFactory {
|
||||
private val singletonFieldDeclarations = HashMap<IrSymbolOwner, IrField>()
|
||||
private val outerThisDeclarations = HashMap<IrClass, IrField>()
|
||||
@@ -136,7 +135,7 @@ class JvmDeclarationFactory(
|
||||
return defaultImplsMethods.getOrPut(interfaceFun) {
|
||||
val defaultImpls = getDefaultImplsClass(interfaceFun.parentAsClass)
|
||||
|
||||
val name = Name.identifier(state.typeMapper.mapFunctionName(interfaceFun.descriptor.original, OwnerKind.IMPLEMENTATION))
|
||||
val name = Name.identifier(methodSignatureMapper.mapFunctionName(interfaceFun, OwnerKind.IMPLEMENTATION))
|
||||
createStaticFunctionWithReceivers(
|
||||
defaultImpls, name, interfaceFun,
|
||||
dispatchReceiverType = parent.defaultType,
|
||||
|
||||
+2
-2
@@ -27,9 +27,9 @@ object ArrayIterator : IntrinsicMethod() {
|
||||
signature: JvmMethodSignature,
|
||||
context: JvmBackendContext
|
||||
): IrIntrinsicFunction {
|
||||
val method = context.state.typeMapper.mapToCallableMethod(expression.descriptor, false)
|
||||
val method = context.methodSignatureMapper.mapToCallableMethod(expression, false)
|
||||
return IrIntrinsicFunction.create(expression, signature, context, method.owner) {
|
||||
val methodSignature = "(${method.owner.descriptor})${method.returnType.descriptor}"
|
||||
val methodSignature = "(${method.owner.descriptor})${method.asmMethod.returnType.descriptor}"
|
||||
val intrinsicOwner =
|
||||
if (AsmUtil.isPrimitive(method.owner.elementType))
|
||||
"kotlin/jvm/internal/ArrayIteratorsKt"
|
||||
|
||||
@@ -20,23 +20,25 @@ import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil.numberFunctionOperandType
|
||||
import org.jetbrains.kotlin.codegen.StackValue
|
||||
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
|
||||
import org.jetbrains.org.objectweb.asm.Opcodes.*
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
|
||||
class BinaryOp(private val opcode: Int) : IntrinsicMethod() {
|
||||
private fun shift(): Boolean =
|
||||
opcode == ISHL || opcode == ISHR || opcode == IUSHR
|
||||
opcode == ISHL || opcode == ISHR || opcode == IUSHR
|
||||
|
||||
override fun toCallable(expression: IrFunctionAccessExpression, signature: JvmMethodSignature, context: JvmBackendContext): IrIntrinsicFunction {
|
||||
val owner = context.state.typeMapper.mapOwner(expression.descriptor)
|
||||
override fun toCallable(
|
||||
expression: IrFunctionAccessExpression,
|
||||
signature: JvmMethodSignature,
|
||||
context: JvmBackendContext
|
||||
): IrIntrinsicFunction {
|
||||
val owner = context.methodSignatureMapper.mapImplementationOwner(expression.symbol.owner)
|
||||
val returnType = signature.returnType
|
||||
val intermediateResultType: Type = numberFunctionOperandType(returnType)
|
||||
val intermediateResultType = numberFunctionOperandType(returnType)
|
||||
val argTypes = if (owner != Type.CHAR_TYPE) {
|
||||
listOf(intermediateResultType, if (shift()) Type.INT_TYPE else intermediateResultType)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
listOf(Type.CHAR_TYPE, signature.valueParameters[0].asmType)
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.backend.jvm.codegen.PromisedValue
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
|
||||
import org.jetbrains.kotlin.ir.types.toKotlinType
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
import org.jetbrains.org.objectweb.asm.commons.Method
|
||||
@@ -35,11 +34,11 @@ abstract class IntrinsicMethod {
|
||||
|
||||
companion object {
|
||||
fun calcReceiverType(call: IrMemberAccessExpression, context: JvmBackendContext): Type {
|
||||
return context.state.typeMapper.mapType((call.dispatchReceiver?.type ?: call.extensionReceiver!!.type).toKotlinType())
|
||||
return context.typeMapper.mapType(call.dispatchReceiver?.type ?: call.extensionReceiver!!.type)
|
||||
}
|
||||
|
||||
fun expressionType(expression: IrExpression, context: JvmBackendContext): Type {
|
||||
return context.state.typeMapper.mapType(expression.type.toKotlinType())
|
||||
return context.typeMapper.mapType(expression.type)
|
||||
}
|
||||
|
||||
fun JvmMethodSignature.newReturnType(type: Type): JvmMethodSignature {
|
||||
|
||||
+3
-3
@@ -32,8 +32,8 @@ object IrEnumValueOf : IntrinsicMethod() {
|
||||
signature: JvmMethodSignature,
|
||||
context: JvmBackendContext
|
||||
): IrIntrinsicFunction {
|
||||
val enumType = context.state.typeMapper.mapType(expression.descriptor.returnType!!)
|
||||
val newSignature = context.state.typeMapper.mapSignatureSkipGeneric(expression.descriptor, OwnerKind.IMPLEMENTATION)
|
||||
val enumType = context.typeMapper.mapType(expression.type)
|
||||
val newSignature = context.methodSignatureMapper.mapSignatureSkipGeneric(expression.symbol.owner, OwnerKind.IMPLEMENTATION)
|
||||
val stringType = AsmTypes.JAVA_STRING_TYPE
|
||||
|
||||
return object : IrIntrinsicFunction(expression, newSignature, context, listOf(stringType)) {
|
||||
@@ -46,4 +46,4 @@ object IrEnumValueOf : IntrinsicMethod() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -138,10 +138,10 @@ open class IrIntrinsicFunction(
|
||||
}
|
||||
|
||||
fun IrFunctionAccessExpression.argTypes(context: JvmBackendContext): ArrayList<Type> {
|
||||
val callableMethod = context.state.typeMapper.mapToCallableMethod(descriptor, false)
|
||||
val callableMethod = context.methodSignatureMapper.mapToCallableMethod(this, false)
|
||||
return arrayListOf<Type>().apply {
|
||||
callableMethod.dispatchReceiverType?.let { add(it) }
|
||||
addAll(callableMethod.getAsmMethod().argumentTypes)
|
||||
addAll(callableMethod.asmMethod.argumentTypes)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+9
-21
@@ -7,28 +7,16 @@ package org.jetbrains.kotlin.backend.jvm.intrinsics
|
||||
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
|
||||
import org.jetbrains.kotlin.ir.types.toKotlinType
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
import org.jetbrains.kotlin.ir.types.typeWith
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
|
||||
object IsArrayOf : IntrinsicMethod() {
|
||||
override fun toCallable(expression: IrFunctionAccessExpression, signature: JvmMethodSignature, context: JvmBackendContext): IrIntrinsicFunction {
|
||||
val typeMapper = context.state.typeMapper
|
||||
|
||||
val descriptor = expression.descriptor
|
||||
val builtIns = descriptor.module.builtIns
|
||||
assert(descriptor.typeParameters.size == 1) {
|
||||
"Expected only one type parameter for Any?.isArrayOf(), got: ${descriptor.typeParameters}"
|
||||
}
|
||||
/*TODO original?*/
|
||||
val elementType = expression.getTypeArgument(descriptor.original.typeParameters.first().index)!!
|
||||
val arrayKtType = builtIns.getArrayType(Variance.INVARIANT, elementType.toKotlinType())
|
||||
val arrayType = typeMapper.mapType(arrayKtType)
|
||||
|
||||
return IrIntrinsicFunction.create(expression, signature, context) {
|
||||
it.instanceOf(arrayType)
|
||||
}
|
||||
override fun toCallable(
|
||||
expression: IrFunctionAccessExpression,
|
||||
signature: JvmMethodSignature,
|
||||
context: JvmBackendContext
|
||||
): IrIntrinsicFunction = IrIntrinsicFunction.create(expression, signature, context) { v ->
|
||||
val arrayType = context.irBuiltIns.arrayClass.typeWith(expression.getTypeArgument(0)!!)
|
||||
v.instanceOf(context.typeMapper.mapType(arrayType))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-4
@@ -50,8 +50,7 @@ internal val additionalClassAnnotationPhase = makeIrFilePhase(
|
||||
)
|
||||
|
||||
private class AdditionalClassAnnotationLowering(private val context: JvmBackendContext) : ClassLoweringPass {
|
||||
|
||||
val typeMapper = context.state.typeMapper
|
||||
private val jvmTarget = context.state.target
|
||||
|
||||
// TODO: import IR structures from the library?
|
||||
|
||||
@@ -210,8 +209,8 @@ private class AdditionalClassAnnotationLowering(private val context: JvmBackendC
|
||||
|
||||
private fun generateTargetAnnotation(irClass: IrClass) {
|
||||
if (irClass.hasAnnotation(FqName("java.lang.annotation.Target"))) return
|
||||
val annotationTargetMap = annotationTargetMaps[typeMapper.jvmTarget]
|
||||
?: throw AssertionError("No annotation target map for JVM target ${typeMapper.jvmTarget}")
|
||||
val annotationTargetMap = annotationTargetMaps[jvmTarget]
|
||||
?: throw AssertionError("No annotation target map for JVM target $jvmTarget")
|
||||
|
||||
val targets = irClass.applicableTargetSet() ?: return
|
||||
val javaTargets = targets.mapNotNull { annotationTargetMap[it] }
|
||||
|
||||
+3
-6
@@ -51,10 +51,7 @@ internal val bridgePhase = makeIrFilePhase(
|
||||
)
|
||||
|
||||
private class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
||||
|
||||
private val state = context.state
|
||||
|
||||
private val typeMapper = state.typeMapper
|
||||
private val methodSignatureMapper = context.methodSignatureMapper
|
||||
|
||||
private val specialBridgeMethods = SpecialBridgeMethods(context)
|
||||
|
||||
@@ -383,8 +380,8 @@ private class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass
|
||||
return findConcreteSuperDeclaration(FunctionHandleForIrFunction(this))?.irFunction
|
||||
}
|
||||
|
||||
private fun IrFunction.getJvmSignature() = typeMapper.mapAsmMethod(descriptor)
|
||||
private fun IrFunction.getJvmName() = getJvmSignature().name
|
||||
private fun IrFunction.getJvmSignature(): Method = methodSignatureMapper.mapAsmMethod(this)
|
||||
private fun IrFunction.getJvmName(): String = getJvmSignature().name
|
||||
}
|
||||
|
||||
private data class SignatureWithSource(val signature: Method, val source: IrSimpleFunction) {
|
||||
|
||||
+9
-8
@@ -17,7 +17,10 @@ import org.jetbrains.kotlin.ir.builders.declarations.buildFun
|
||||
import org.jetbrains.kotlin.ir.builders.irBlockBody
|
||||
import org.jetbrains.kotlin.ir.builders.irCall
|
||||
import org.jetbrains.kotlin.ir.builders.irString
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
|
||||
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
|
||||
@@ -33,10 +36,7 @@ internal val collectionStubMethodLowering = makeIrFilePhase(
|
||||
)
|
||||
|
||||
private class CollectionStubMethodLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
||||
|
||||
private val state = context.state
|
||||
|
||||
private val typeMapper = state.typeMapper
|
||||
private val methodSignatureMapper = context.methodSignatureMapper
|
||||
|
||||
override fun lower(irClass: IrClass) {
|
||||
if (irClass.isInterface) {
|
||||
@@ -65,10 +65,11 @@ private class CollectionStubMethodLowering(val context: JvmBackendContext) : Cla
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: replace with new typeMapper using no descriptor
|
||||
private fun IrSimpleFunction.toSignature() = typeMapper.mapAsmMethod(this.descriptor).toString()
|
||||
private fun IrSimpleFunction.toSignature(): String = methodSignatureMapper.mapAsmMethod(this).toString()
|
||||
|
||||
private fun createStubMethod(function: IrSimpleFunction, irClass: IrClass, substitutionMap: Map<IrTypeParameterSymbol, IrType>): IrSimpleFunction {
|
||||
private fun createStubMethod(
|
||||
function: IrSimpleFunction, irClass: IrClass, substitutionMap: Map<IrTypeParameterSymbol, IrType>
|
||||
): IrSimpleFunction {
|
||||
return buildFun {
|
||||
name = function.name
|
||||
returnType = function.returnType.substitute(substitutionMap)
|
||||
|
||||
+1
-1
@@ -64,7 +64,7 @@ private class CompanionObjectJvmStaticLowering(val context: JvmBackendContext) :
|
||||
|
||||
companion?.declarations?.filter(::isJvmStaticFunction)?.forEach {
|
||||
val jvmStaticFunction = it as IrSimpleFunction
|
||||
val newName = Name.identifier(context.state.typeMapper.mapFunctionName(jvmStaticFunction.symbol.descriptor, null))
|
||||
val newName = Name.identifier(context.methodSignatureMapper.mapFunctionName(jvmStaticFunction, null))
|
||||
if (!jvmStaticFunction.visibility.isPublicAPI) {
|
||||
// TODO: Synthetic accessor creation logic should be supported in SyntheticAccessorLowering in the future.
|
||||
val accessorName = Name.identifier("access\$$newName")
|
||||
|
||||
+2
-2
@@ -55,12 +55,12 @@ internal class PropertyReferenceLowering(val context: JvmBackendContext) : Class
|
||||
get() = (this as? IrPropertyReference)?.field
|
||||
|
||||
private val IrSimpleFunction.signature: String
|
||||
get() = context.state.typeMapper.mapSignatureSkipGeneric(collectRealOverrides().first().descriptor).toString()
|
||||
get() = context.methodSignatureMapper.mapSignatureSkipGeneric(collectRealOverrides().first()).toString()
|
||||
|
||||
// Plain Java fields do not have a getter, but can be referenced nonetheless. The signature should be the one
|
||||
// that a getter would have, if it existed.
|
||||
private val IrField.signature: String
|
||||
get() = "${JvmAbi.getterName(name.asString())}()${context.state.typeMapper.mapReturnType(descriptor)}"
|
||||
get() = "${JvmAbi.getterName(name.asString())}()${context.methodSignatureMapper.mapReturnType(this)}"
|
||||
|
||||
private val IrMemberAccessExpression.signature: String
|
||||
get() = getter?.let { getter ->
|
||||
|
||||
+1
-4
@@ -416,10 +416,7 @@ private class SyntheticAccessorLowering(val context: JvmBackendContext) : IrElem
|
||||
private var nameCounter = 0
|
||||
|
||||
private fun IrFunction.accessorName(): Name {
|
||||
val jvmName = context.state.typeMapper.mapFunctionName(
|
||||
descriptor,
|
||||
OwnerKind.getMemberOwnerKind(parentAsClass.descriptor)
|
||||
)
|
||||
val jvmName = context.methodSignatureMapper.mapFunctionName(this, OwnerKind.IMPLEMENTATION)
|
||||
return Name.identifier("access\$$jvmName\$${nameCounter++}")
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -190,7 +190,7 @@ private class ToArrayLowering(private val context: JvmBackendContext) : ClassLow
|
||||
|
||||
private fun IrDeclaration.isGenericToArray(): Boolean {
|
||||
if (this !is IrSimpleFunction) return false
|
||||
val signature = context.state.typeMapper.mapAsmMethod(descriptor)
|
||||
val signature = context.methodSignatureMapper.mapAsmMethod(this)
|
||||
return signature.toString() == "toArray([Ljava/lang/Object;)[Ljava/lang/Object;"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user