Minor.Reformat

This commit is contained in:
Mikhael Bogdanov
2018-02-27 12:09:58 +01:00
parent 308283e52e
commit affe445955
@@ -60,21 +60,23 @@ interface SourceCompilerForInline {
val lazySourceMapper: DefaultSourceMapper val lazySourceMapper: DefaultSourceMapper
fun generateLambdaBody(adapter: MethodVisitor, fun generateLambdaBody(
jvmMethodSignature: JvmMethodSignature, adapter: MethodVisitor,
lambdaInfo: ExpressionLambda): SMAP jvmMethodSignature: JvmMethodSignature,
lambdaInfo: ExpressionLambda
): SMAP
fun doCreateMethodNodeFromSource( fun doCreateMethodNodeFromSource(
callableDescriptor: FunctionDescriptor, callableDescriptor: FunctionDescriptor,
jvmSignature: JvmMethodSignature, jvmSignature: JvmMethodSignature,
callDefault: Boolean, callDefault: Boolean,
asmMethod: Method asmMethod: Method
): SMAPAndMethodNode ): SMAPAndMethodNode
fun generateAndInsertFinallyBlocks( fun generateAndInsertFinallyBlocks(
intoNode: MethodNode, intoNode: MethodNode,
insertPoints: List<MethodInliner.PointForExternalFinallyBlocks>, insertPoints: List<MethodInliner.PointForExternalFinallyBlocks>,
offsetForFinallyLocalVar: Int offsetForFinallyLocalVar: Int
) )
fun isCallInsideSameModuleAsDeclared(functionDescriptor: FunctionDescriptor): Boolean fun isCallInsideSameModuleAsDeclared(functionDescriptor: FunctionDescriptor): Boolean
@@ -91,7 +93,7 @@ interface SourceCompilerForInline {
} }
class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, override val callElement: KtElement): SourceCompilerForInline { class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, override val callElement: KtElement) : SourceCompilerForInline {
override val state = codegen.state override val state = codegen.state
@@ -130,51 +132,53 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
val signature = codegen.state.typeMapper.mapSignatureSkipGeneric(context.functionDescriptor, context.contextKind) val signature = codegen.state.typeMapper.mapSignatureSkipGeneric(context.functionDescriptor, context.contextKind)
return InlineCallSiteInfo( return InlineCallSiteInfo(
parentCodegen.className, signature.asmMethod.name, signature.asmMethod.descriptor parentCodegen.className, signature.asmMethod.name, signature.asmMethod.descriptor
) )
} }
override val lazySourceMapper override val lazySourceMapper
get() = codegen.parentCodegen.orCreateSourceMapper get() = codegen.parentCodegen.orCreateSourceMapper
override fun generateLambdaBody(adapter: MethodVisitor, override fun generateLambdaBody(
jvmMethodSignature: JvmMethodSignature, adapter: MethodVisitor,
lambdaInfo: ExpressionLambda): SMAP { jvmMethodSignature: JvmMethodSignature,
lambdaInfo: ExpressionLambda
): SMAP {
lambdaInfo as? PsiExpressionLambda ?: error("TODO") lambdaInfo as? PsiExpressionLambda ?: error("TODO")
val invokeMethodDescriptor = lambdaInfo.invokeMethodDescriptor val invokeMethodDescriptor = lambdaInfo.invokeMethodDescriptor
val closureContext = val closureContext =
if (lambdaInfo.isPropertyReference) if (lambdaInfo.isPropertyReference)
codegen.getContext().intoAnonymousClass(lambdaInfo.classDescriptor, codegen, OwnerKind.IMPLEMENTATION) codegen.getContext().intoAnonymousClass(lambdaInfo.classDescriptor, codegen, OwnerKind.IMPLEMENTATION)
else else
codegen.getContext().intoClosure(invokeMethodDescriptor, codegen, state.typeMapper) codegen.getContext().intoClosure(invokeMethodDescriptor, codegen, state.typeMapper)
val context = closureContext.intoInlinedLambda(invokeMethodDescriptor, lambdaInfo.isCrossInline, lambdaInfo.isPropertyReference) val context = closureContext.intoInlinedLambda(invokeMethodDescriptor, lambdaInfo.isCrossInline, lambdaInfo.isPropertyReference)
return generateMethodBody( return generateMethodBody(
adapter, invokeMethodDescriptor, context, adapter, invokeMethodDescriptor, context,
lambdaInfo.functionWithBodyOrCallableReference, lambdaInfo.functionWithBodyOrCallableReference,
jvmMethodSignature, lambdaInfo jvmMethodSignature, lambdaInfo
) )
} }
private fun generateMethodBody( private fun generateMethodBody(
adapter: MethodVisitor, adapter: MethodVisitor,
descriptor: FunctionDescriptor, descriptor: FunctionDescriptor,
context: MethodContext, context: MethodContext,
expression: KtExpression, expression: KtExpression,
jvmMethodSignature: JvmMethodSignature, jvmMethodSignature: JvmMethodSignature,
lambdaInfo: PsiExpressionLambda? lambdaInfo: PsiExpressionLambda?
): SMAP { ): SMAP {
val isLambda = lambdaInfo != null val isLambda = lambdaInfo != null
// Wrapping for preventing marking actual parent codegen as containing reified markers // Wrapping for preventing marking actual parent codegen as containing reified markers
val parentCodegen = FakeMemberCodegen( val parentCodegen = FakeMemberCodegen(
codegen.parentCodegen, expression, context.parentContext as FieldOwnerContext<*>, codegen.parentCodegen, expression, context.parentContext as FieldOwnerContext<*>,
if (isLambda) if (isLambda)
codegen.parentCodegen.className codegen.parentCodegen.className
else else
state.typeMapper.mapImplementationOwner(descriptor).internalName, state.typeMapper.mapImplementationOwner(descriptor).internalName,
if (isLambda) emptyList() else additionalInnerClasses, if (isLambda) emptyList() else additionalInnerClasses,
isLambda isLambda
) )
val strategy = when (expression) { val strategy = when (expression) {
@@ -186,11 +190,10 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
val asmType = state.typeMapper.mapClass(lambdaInfo.classDescriptor) val asmType = state.typeMapper.mapClass(lambdaInfo.classDescriptor)
val info = lambdaInfo.propertyReferenceInfo val info = lambdaInfo.propertyReferenceInfo
PropertyReferenceCodegen.PropertyReferenceGenerationStrategy( PropertyReferenceCodegen.PropertyReferenceGenerationStrategy(
true, info!!.getFunction, info.target, asmType, receiverType, true, info!!.getFunction, info.target, asmType, receiverType,
lambdaInfo.functionWithBodyOrCallableReference, state, true lambdaInfo.functionWithBodyOrCallableReference, state, true
) )
} } else {
else {
FunctionReferenceGenerationStrategy(state, descriptor, resolvedCall, receiverType, null, true) FunctionReferenceGenerationStrategy(state, descriptor, resolvedCall, receiverType, null, true)
} }
} }
@@ -209,8 +212,8 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
private fun createSMAPWithDefaultMapping( private fun createSMAPWithDefaultMapping(
declaration: KtExpression, declaration: KtExpression,
mappings: List<FileMapping> mappings: List<FileMapping>
): SMAP { ): SMAP {
val containingFile = declaration.containingFile val containingFile = declaration.containingFile
CodegenUtil.getLineNumberForElement(containingFile, true) ?: error("Couldn't extract line count in " + containingFile) CodegenUtil.getLineNumberForElement(containingFile, true) ?: error("Couldn't extract line count in " + containingFile)
@@ -220,12 +223,12 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
private class FakeMemberCodegen( private class FakeMemberCodegen(
internal val delegate: MemberCodegen<*>, internal val delegate: MemberCodegen<*>,
declaration: KtElement, declaration: KtElement,
codegenContext: FieldOwnerContext<*>, codegenContext: FieldOwnerContext<*>,
private val className: String, private val className: String,
private val parentAsInnerClasses: List<ClassDescriptor>, private val parentAsInnerClasses: List<ClassDescriptor>,
private val isInlineLambdaCodegen: Boolean private val isInlineLambdaCodegen: Boolean
) : MemberCodegen<KtPureElement>(delegate as MemberCodegen<KtPureElement>, declaration, codegenContext) { ) : MemberCodegen<KtPureElement>(delegate as MemberCodegen<KtPureElement>, declaration, codegenContext) {
override fun generateDeclaration() { override fun generateDeclaration() {
@@ -252,18 +255,17 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
override fun addParentsToInnerClassesIfNeeded(innerClasses: MutableCollection<ClassDescriptor>) { override fun addParentsToInnerClassesIfNeeded(innerClasses: MutableCollection<ClassDescriptor>) {
if (isInlineLambdaCodegen) { if (isInlineLambdaCodegen) {
super.addParentsToInnerClassesIfNeeded(innerClasses) super.addParentsToInnerClassesIfNeeded(innerClasses)
} } else {
else {
innerClasses.addAll(parentAsInnerClasses) innerClasses.addAll(parentAsInnerClasses)
} }
} }
} }
override fun doCreateMethodNodeFromSource( override fun doCreateMethodNodeFromSource(
callableDescriptor: FunctionDescriptor, callableDescriptor: FunctionDescriptor,
jvmSignature: JvmMethodSignature, jvmSignature: JvmMethodSignature,
callDefault: Boolean, callDefault: Boolean,
asmMethod: Method asmMethod: Method
): SMAPAndMethodNode { ): SMAPAndMethodNode {
val element = DescriptorToSourceUtils.descriptorToDeclaration(callableDescriptor) val element = DescriptorToSourceUtils.descriptorToDeclaration(callableDescriptor)
@@ -273,10 +275,10 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
val inliningFunction = element as KtDeclarationWithBody? val inliningFunction = element as KtDeclarationWithBody?
val node = MethodNode( val node = MethodNode(
API, API,
AsmUtil.getMethodAsmFlags(callableDescriptor, context.contextKind, state) or if (callDefault) Opcodes.ACC_STATIC else 0, AsmUtil.getMethodAsmFlags(callableDescriptor, context.contextKind, state) or if (callDefault) Opcodes.ACC_STATIC else 0,
asmMethod.name, asmMethod.name,
asmMethod.descriptor, null, null asmMethod.descriptor, null, null
) )
//for maxLocals calculation //for maxLocals calculation
@@ -287,10 +289,10 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
val smap = if (callDefault) { val smap = if (callDefault) {
val implementationOwner = state.typeMapper.mapImplementationOwner(callableDescriptor) val implementationOwner = state.typeMapper.mapImplementationOwner(callableDescriptor)
val parentCodegen = FakeMemberCodegen( val parentCodegen = FakeMemberCodegen(
codegen.parentCodegen, inliningFunction!!, methodContext.parentContext as FieldOwnerContext<*>, codegen.parentCodegen, inliningFunction!!, methodContext.parentContext as FieldOwnerContext<*>,
implementationOwner.internalName, implementationOwner.internalName,
additionalInnerClasses, additionalInnerClasses,
false false
) )
if (element !is KtNamedFunction) { if (element !is KtNamedFunction) {
throw IllegalStateException("Property accessors with default parameters not supported " + callableDescriptor) throw IllegalStateException("Property accessors with default parameters not supported " + callableDescriptor)
@@ -300,8 +302,7 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
inliningFunction as KtNamedFunction?, parentCodegen, asmMethod inliningFunction as KtNamedFunction?, parentCodegen, asmMethod
) )
createSMAPWithDefaultMapping(inliningFunction, parentCodegen.orCreateSourceMapper.resultMappings) createSMAPWithDefaultMapping(inliningFunction, parentCodegen.orCreateSourceMapper.resultMappings)
} } else {
else {
generateMethodBody(maxCalcAdapter, callableDescriptor, methodContext, inliningFunction!!, jvmSignature, null) generateMethodBody(maxCalcAdapter, callableDescriptor, methodContext, inliningFunction!!, jvmSignature, null)
} }
maxCalcAdapter.visitMaxs(-1, -1) maxCalcAdapter.visitMaxs(-1, -1)
@@ -311,9 +312,9 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
} }
override fun generateAndInsertFinallyBlocks( override fun generateAndInsertFinallyBlocks(
intoNode: MethodNode, intoNode: MethodNode,
insertPoints: List<MethodInliner.PointForExternalFinallyBlocks>, insertPoints: List<MethodInliner.PointForExternalFinallyBlocks>,
offsetForFinallyLocalVar: Int offsetForFinallyLocalVar: Int
) { ) {
if (!codegen.hasFinallyBlocks()) return if (!codegen.hasFinallyBlocks()) return
@@ -340,8 +341,10 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
val finallyNode = createEmptyMethodNode() val finallyNode = createEmptyMethodNode()
finallyNode.visitLabel(start) finallyNode.visitLabel(start)
val finallyCodegen = ExpressionCodegen(finallyNode, codegen.frameMap, codegen.returnType, val finallyCodegen = ExpressionCodegen(
codegen.getContext(), codegen.state, codegen.parentCodegen) finallyNode, codegen.frameMap, codegen.returnType,
codegen.getContext(), codegen.state, codegen.parentCodegen
)
finallyCodegen.addBlockStackElementsForNonLocalReturns(codegen.blockStackElements, curFinallyDepth) finallyCodegen.addBlockStackElementsForNonLocalReturns(codegen.blockStackElements, curFinallyDepth)
val frameMap = finallyCodegen.frameMap val frameMap = finallyCodegen.frameMap
@@ -394,19 +397,28 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
val parentContext = context.parentContext val parentContext = context.parentContext
val descriptor = if (parentContext is ClosureContext && parentContext.originalSuspendLambdaDescriptor != null) { val descriptor = if (parentContext is ClosureContext && parentContext.originalSuspendLambdaDescriptor != null) {
parentContext.originalSuspendLambdaDescriptor!! parentContext.originalSuspendLambdaDescriptor!!
} } else context.contextDescriptor
else context.contextDescriptor
return InlineCodegen.getDeclarationLabels(DescriptorToSourceUtils.descriptorToDeclaration(descriptor), descriptor) return InlineCodegen.getDeclarationLabels(DescriptorToSourceUtils.descriptorToDeclaration(descriptor), descriptor)
} }
override fun initializeInlineFunctionContext(functionDescriptor: FunctionDescriptor) { override fun initializeInlineFunctionContext(functionDescriptor: FunctionDescriptor) {
context = getContext(functionDescriptor, functionDescriptor, state, DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor)?.containingFile as? KtFile, additionalInnerClasses) context = getContext(
functionDescriptor,
functionDescriptor,
state,
DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor)?.containingFile as? KtFile,
additionalInnerClasses
)
} }
companion object { companion object {
fun getContext( fun getContext(
descriptor: DeclarationDescriptor, innerDescriptor: DeclarationDescriptor, state: GenerationState, sourceFile: KtFile?, additionalInners: MutableList<ClassDescriptor> descriptor: DeclarationDescriptor,
innerDescriptor: DeclarationDescriptor,
state: GenerationState,
sourceFile: KtFile?,
additionalInners: MutableList<ClassDescriptor>
): CodegenContext<*> { ): CodegenContext<*> {
if (descriptor is PackageFragmentDescriptor) { if (descriptor is PackageFragmentDescriptor) {
//no inners //no inners
@@ -415,30 +427,30 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
val container = descriptor.containingDeclaration ?: error("No container for descriptor: " + descriptor) val container = descriptor.containingDeclaration ?: error("No container for descriptor: " + descriptor)
val parent = getContext( val parent = getContext(
container, container,
descriptor, descriptor,
state, state,
sourceFile, sourceFile,
additionalInners additionalInners
) )
return when (descriptor) { return when (descriptor) {
is ScriptDescriptor -> { is ScriptDescriptor -> {
val earlierScripts = state.replSpecific.earlierScriptsForReplInterpreter val earlierScripts = state.replSpecific.earlierScriptsForReplInterpreter
parent.intoScript( parent.intoScript(
descriptor, descriptor,
earlierScripts ?: emptyList(), earlierScripts ?: emptyList(),
descriptor as ClassDescriptor, state.typeMapper descriptor as ClassDescriptor, state.typeMapper
) )
} }
is ClassDescriptor -> { is ClassDescriptor -> {
val kind = val kind =
if (DescriptorUtils.isInterface(descriptor) && innerDescriptor !is ClassDescriptor) if (DescriptorUtils.isInterface(descriptor) && innerDescriptor !is ClassDescriptor)
OwnerKind.DEFAULT_IMPLS OwnerKind.DEFAULT_IMPLS
else OwnerKind.IMPLEMENTATION else OwnerKind.IMPLEMENTATION
additionalInners.addIfNotNull( additionalInners.addIfNotNull(
InnerClassConsumer.classForInnerClassRecord(descriptor, kind == OwnerKind.DEFAULT_IMPLS) InnerClassConsumer.classForInnerClassRecord(descriptor, kind == OwnerKind.DEFAULT_IMPLS)
) )
parent.intoClass(descriptor, kind, state) parent.intoClass(descriptor, kind, state)
} }