From 4c7eb815fcfd10200bf4bda13fd845fb3ff0dfc3 Mon Sep 17 00:00:00 2001 From: pyos Date: Tue, 25 May 2021 19:59:45 +0200 Subject: [PATCH] JVM: remove some FunctionDescriptors from SourceCompilerForInline --- .../kotlin/codegen/ExpressionCodegen.java | 3 +- .../kotlin/codegen/inline/InlineCodegen.kt | 8 +- .../inline/PsiSourceCompilerForInline.kt | 74 ++++++++----------- .../codegen/inline/SourceCompilerForInline.kt | 11 +-- .../backend/jvm/codegen/ExpressionCodegen.kt | 10 +-- .../jvm/codegen/IrSourceCompilerForInline.kt | 22 ++---- 6 files changed, 47 insertions(+), 81 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 223bdb26be6..f835d1921c5 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -2950,12 +2950,11 @@ public class ExpressionCodegen extends KtVisitor impleme bindingContext, state ); - PsiSourceCompilerForInline sourceCompiler = new PsiSourceCompilerForInline(this, callElement); FunctionDescriptor functionDescriptor = InlineUtil.isArrayConstructorWithLambda(original) ? FictitiousArrayConstructor.create((ConstructorDescriptor) original) : original.getOriginal(); + PsiSourceCompilerForInline sourceCompiler = new PsiSourceCompilerForInline(this, callElement, functionDescriptor); - sourceCompiler.initializeInlineFunctionContext(functionDescriptor); JvmMethodSignature signature = typeMapper.mapSignatureWithGeneric(functionDescriptor, sourceCompiler.getContextKind()); if (signature.getAsmMethod().getName().contains("-") && !state.getConfiguration().getBoolean(JVMConfigurationKeys.USE_OLD_INLINE_CLASSES_MANGLING_SCHEME) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt index 6b85a63e023..d6e2d46fe6c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt @@ -58,8 +58,6 @@ abstract class InlineCodegen( private val initialFrameSize = codegen.frameMap.currentSize - private val isSameModule: Boolean = sourceCompiler.isCallInsideSameModuleAsDeclared(functionDescriptor) - protected val invocationParamBuilder = ParametersBuilder.newBuilder() protected val expressionMap = linkedMapOf() @@ -214,7 +212,7 @@ abstract class InlineCodegen( val sourceInfo = sourceMapper.sourceInfo!! val callSite = SourcePosition(codegen.lastLineNumber, sourceInfo.sourceFileName!!, sourceInfo.pathOrCleanFQN) val inliner = MethodInliner( - node, parameters, info, FieldRemapper(null, null, parameters), isSameModule, + node, parameters, info, FieldRemapper(null, null, parameters), sourceCompiler.isCallInsideSameModuleAsCallee, "Method inlining " + sourceCompiler.callElementText, SourceMapCopier(sourceMapper, nodeAndSmap.classSMAP, callSite), info.callSiteInfo, if (functionDescriptor.isInlineOnly()) InlineOnlySmapSkipper(codegen) else null, @@ -234,7 +232,7 @@ abstract class InlineCodegen( generateAndInsertFinallyBlocks( adapter, infos, (remapper.remap(parameters.argsSizeOnStack + 1).value as StackValue.Local).index ) - if (!sourceCompiler.isFinallyMarkerRequired()) { + if (!sourceCompiler.isFinallyMarkerRequired) { removeFinallyMarkers(adapter) } @@ -456,7 +454,7 @@ abstract class InlineCodegen( val directMember = getDirectMemberAndCallableFromObject(functionDescriptor) if (!isBuiltInArrayIntrinsic(functionDescriptor) && !descriptorIsDeserialized(directMember)) { - val node = sourceCompiler.doCreateMethodNodeFromSource(functionDescriptor, jvmSignature, callDefault, asmMethod) + val node = sourceCompiler.compileInlineFunction(jvmSignature, callDefault, asmMethod) node.node.preprocessSuspendMarkers(forInline = true, keepFakeContinuation = false) return node } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiSourceCompilerForInline.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiSourceCompilerForInline.kt index e6cd279bedd..22182390d3e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiSourceCompilerForInline.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiSourceCompilerForInline.kt @@ -30,27 +30,30 @@ import org.jetbrains.org.objectweb.asm.Opcodes import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.commons.Method import org.jetbrains.org.objectweb.asm.tree.MethodNode -import kotlin.properties.Delegates -class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, override val callElement: KtElement) : - SourceCompilerForInline { +class PsiSourceCompilerForInline( + private val codegen: ExpressionCodegen, + override val callElement: KtElement, + private val functionDescriptor: FunctionDescriptor +) : SourceCompilerForInline { + override val state + get() = codegen.state - override val state = codegen.state + private val additionalInnerClasses = mutableListOf() - private var context by Delegates.notNull>() - - private var additionalInnerClasses = mutableListOf() + private val context = getContext( + functionDescriptor, + functionDescriptor, + codegen.state, + DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor)?.containingFile as? KtFile, + additionalInnerClasses + ) override val lookupLocation = KotlinLookupLocation(callElement) + override val callElementText: String by lazy { callElement.text } - override val callElementText: String by lazy { - callElement.text - } - - override val callsiteFile by lazy { - callElement.containingFile - } + override val callsiteFile by lazy { callElement.containingFile } override val contextKind get () = context.contextKind @@ -211,22 +214,18 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid } } - override fun doCreateMethodNodeFromSource( - callableDescriptor: FunctionDescriptor, - jvmSignature: JvmMethodSignature, - callDefault: Boolean, - asmMethod: Method - ): SMAPAndMethodNode { - val element = DescriptorToSourceUtils.descriptorToDeclaration(callableDescriptor) + override fun compileInlineFunction(jvmSignature: JvmMethodSignature, callDefault: Boolean, asmMethod: Method): SMAPAndMethodNode { + val element = DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor) if (!(element is KtNamedFunction || element is KtPropertyAccessor)) { - throw IllegalStateException("Couldn't find declaration for function $callableDescriptor") + throw IllegalStateException("Couldn't find declaration for function $functionDescriptor") } val inliningFunction = element as KtDeclarationWithBody? val node = MethodNode( Opcodes.API_VERSION, - DescriptorAsmUtil.getMethodAsmFlags(callableDescriptor, context.contextKind, state) or if (callDefault) Opcodes.ACC_STATIC else 0, + DescriptorAsmUtil.getMethodAsmFlags(functionDescriptor, context.contextKind, state) or + if (callDefault) Opcodes.ACC_STATIC else 0, asmMethod.name, asmMethod.descriptor, null, null ) @@ -234,10 +233,10 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid //for maxLocals calculation val maxCalcAdapter = wrapWithMaxLocalCalc(node) val parentContext = context.parentContext ?: error("Context has no parent: $context") - val methodContext = parentContext.intoFunction(callableDescriptor) + val methodContext = parentContext.intoFunction(functionDescriptor) val smap = if (callDefault) { - val implementationOwner = state.typeMapper.mapImplementationOwner(callableDescriptor) + val implementationOwner = state.typeMapper.mapImplementationOwner(functionDescriptor) val parentCodegen = FakeMemberCodegen( codegen.parentCodegen, inliningFunction!!, methodContext.parentContext as FieldOwnerContext<*>, implementationOwner.internalName, @@ -245,15 +244,15 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid false ) if (element !is KtNamedFunction) { - throw IllegalStateException("Property accessors with default parameters not supported $callableDescriptor") + throw IllegalStateException("Property accessors with default parameters not supported $functionDescriptor") } FunctionCodegen.generateDefaultImplBody( - methodContext, callableDescriptor, maxCalcAdapter, DefaultParameterValueLoader.DEFAULT, + methodContext, functionDescriptor, maxCalcAdapter, DefaultParameterValueLoader.DEFAULT, inliningFunction as KtNamedFunction?, parentCodegen, asmMethod ) SMAP(parentCodegen.orCreateSourceMapper.resultMappings) } else { - generateMethodBody(maxCalcAdapter, callableDescriptor, methodContext, inliningFunction!!, jvmSignature, null) + generateMethodBody(maxCalcAdapter, functionDescriptor, methodContext, inliningFunction!!, jvmSignature, null) } maxCalcAdapter.visitMaxs(-1, -1) maxCalcAdapter.visitEnd() @@ -277,12 +276,11 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid it.addBlockStackElementsForNonLocalReturns(codegen.blockStackElements, curFinallyDepth) } - override fun isCallInsideSameModuleAsDeclared(functionDescriptor: FunctionDescriptor): Boolean { - return JvmCodegenUtil.isCallInsideSameModuleAsDeclared(functionDescriptor, codegen.getContext(), codegen.state.outDirectory) - } - - override fun isFinallyMarkerRequired(): Boolean = isFinallyMarkerRequired(codegen.getContext()) + override val isCallInsideSameModuleAsCallee: Boolean + get() = JvmCodegenUtil.isCallInsideSameModuleAsDeclared(functionDescriptor, codegen.getContext(), codegen.state.outDirectory) + override val isFinallyMarkerRequired: Boolean + get() = isFinallyMarkerRequired(codegen.getContext()) override val compilationContextDescriptor get() = codegen.getContext().contextDescriptor @@ -301,16 +299,6 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid return labels.associateWith { null } // TODO add break/continue labels } - fun initializeInlineFunctionContext(functionDescriptor: FunctionDescriptor) { - context = getContext( - functionDescriptor, - functionDescriptor, - state, - DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor)?.containingFile as? KtFile, - additionalInnerClasses - ) - } - override fun reportSuspensionPointInsideMonitor(stackTraceElement: String) { org.jetbrains.kotlin.codegen.coroutines.reportSuspensionPointInsideMonitor(callElement, state, stackTraceElement) } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt index f54910ee6a1..14b37a743af 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt @@ -37,12 +37,7 @@ interface SourceCompilerForInline { fun generateLambdaBody(lambdaInfo: ExpressionLambda, reifiedTypeParameters: ReifiedTypeParametersUsages): SMAPAndMethodNode - fun doCreateMethodNodeFromSource( - callableDescriptor: FunctionDescriptor, - jvmSignature: JvmMethodSignature, - callDefault: Boolean, - asmMethod: Method - ): SMAPAndMethodNode + fun compileInlineFunction(jvmSignature: JvmMethodSignature, callDefault: Boolean, asmMethod: Method): SMAPAndMethodNode fun hasFinallyBlocks(): Boolean @@ -53,9 +48,9 @@ interface SourceCompilerForInline { fun generateFinallyBlocksIfNeeded(codegen: BaseExpressionCodegen, returnType: Type, afterReturnLabel: Label, target: Label?) - fun isCallInsideSameModuleAsDeclared(functionDescriptor: FunctionDescriptor): Boolean + val isCallInsideSameModuleAsCallee: Boolean - fun isFinallyMarkerRequired(): Boolean + val isFinallyMarkerRequired: Boolean val compilationContextDescriptor: DeclarationDescriptor diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt index d331ada4e3e..e6da942d7f8 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt @@ -35,7 +35,6 @@ import org.jetbrains.kotlin.codegen.signature.BothSignatureWriter import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.config.isReleaseCoroutines import org.jetbrains.kotlin.config.languageVersionSettings -import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.ir.IrElement @@ -1241,11 +1240,11 @@ class ExpressionCodegen( val gapStart = markNewLinkedLabel() data.localGapScope(tryWithFinallyInfo) { finallyDepth++ - if (isFinallyMarkerRequired()) { + if (isFinallyMarkerRequired) { generateFinallyMarker(mv, finallyDepth, true) } tryWithFinallyInfo.onExit.accept(this, data).discard() - if (isFinallyMarkerRequired()) { + if (isFinallyMarkerRequired) { generateFinallyMarker(mv, finallyDepth, false) } finallyDepth-- @@ -1445,9 +1444,8 @@ class ExpressionCodegen( } } - fun isFinallyMarkerRequired(): Boolean { - return irFunction.isInline || inlinedInto != null - } + val isFinallyMarkerRequired: Boolean + get() = irFunction.isInline || inlinedInto != null val IrType.isReifiedTypeParameter: Boolean get() = this.classifierOrNull?.safeAs()?.owner?.isReified == true diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt index e287c6d3507..7ae1ed248f8 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.incremental.components.Position import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI import org.jetbrains.kotlin.ir.declarations.* -import org.jetbrains.kotlin.ir.descriptors.IrBasedSimpleFunctionDescriptor import org.jetbrains.kotlin.ir.descriptors.toIrBasedDescriptor import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression import org.jetbrains.kotlin.ir.expressions.IrLoop @@ -105,12 +104,7 @@ class IrSourceCompilerForInline( return FunctionCodegen(lambdaInfo.function, codegen.classCodegen).generate(codegen, reifiedTypeParameters) } - override fun doCreateMethodNodeFromSource( - callableDescriptor: FunctionDescriptor, - jvmSignature: JvmMethodSignature, - callDefault: Boolean, - asmMethod: Method - ): SMAPAndMethodNode = + override fun compileInlineFunction(jvmSignature: JvmMethodSignature, callDefault: Boolean, asmMethod: Method): SMAPAndMethodNode = ClassCodegen.getOrCreate(callee.parentAsClass, codegen.context).generateMethodNode(callee) override fun hasFinallyBlocks() = data.hasFinallyBlocks() @@ -129,17 +123,11 @@ class IrSourceCompilerForInline( } @OptIn(ObsoleteDescriptorBasedAPI::class) - override fun isCallInsideSameModuleAsDeclared(functionDescriptor: FunctionDescriptor): Boolean { - require(functionDescriptor is IrBasedSimpleFunctionDescriptor) { - "expected an IrBasedSimpleFunctionDescriptor, got $functionDescriptor" - } - val function = functionDescriptor.owner - return function.module == codegen.irFunction.module - } + override val isCallInsideSameModuleAsCallee: Boolean + get() = callee.module == codegen.irFunction.module - override fun isFinallyMarkerRequired(): Boolean { - return codegen.isFinallyMarkerRequired() - } + override val isFinallyMarkerRequired: Boolean + get() = codegen.isFinallyMarkerRequired override val compilationContextDescriptor: DeclarationDescriptor get() = compilationContextFunctionDescriptor