Clean code after convertion
This commit is contained in:
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.codegen.inline.InlineCodegenUtil.*
|
|||||||
import org.jetbrains.kotlin.codegen.intrinsics.bytecode
|
import org.jetbrains.kotlin.codegen.intrinsics.bytecode
|
||||||
import org.jetbrains.kotlin.codegen.intrinsics.classId
|
import org.jetbrains.kotlin.codegen.intrinsics.classId
|
||||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||||
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper
|
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.isInlineOnly
|
import org.jetbrains.kotlin.descriptors.annotations.isInlineOnly
|
||||||
import org.jetbrains.kotlin.incremental.KotlinLookupLocation
|
import org.jetbrains.kotlin.incremental.KotlinLookupLocation
|
||||||
@@ -71,24 +70,44 @@ class InlineCodegen(
|
|||||||
private val callElement: KtElement,
|
private val callElement: KtElement,
|
||||||
private val typeParameterMappings: TypeParameterMappings
|
private val typeParameterMappings: TypeParameterMappings
|
||||||
) : CallGenerator() {
|
) : CallGenerator() {
|
||||||
private val typeMapper: KotlinTypeMapper
|
init {
|
||||||
|
assert(InlineUtil.isInline(function) || InlineUtil.isArrayConstructorWithLambda(function)) {
|
||||||
|
"InlineCodegen can inline only inline functions and array constructors: " + function
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val functionDescriptor: FunctionDescriptor
|
// TODO: implement AS_FUNCTION inline strategy
|
||||||
private val jvmSignature: JvmMethodSignature
|
private val asFunctionInline = false
|
||||||
private val context: MethodContext
|
|
||||||
|
|
||||||
private val asFunctionInline: Boolean
|
private val typeMapper = state.typeMapper
|
||||||
private val initialFrameSize: Int
|
|
||||||
private val isSameModule: Boolean
|
private val initialFrameSize = codegen.frameMap.currentSize
|
||||||
|
|
||||||
|
private val reifiedTypeInliner = ReifiedTypeInliner(typeParameterMappings)
|
||||||
|
|
||||||
|
private val functionDescriptor: FunctionDescriptor =
|
||||||
|
if (InlineUtil.isArrayConstructorWithLambda(function))
|
||||||
|
FictitiousArrayConstructor.create(function as ConstructorDescriptor)
|
||||||
|
else
|
||||||
|
function.original
|
||||||
|
|
||||||
|
private val context =
|
||||||
|
getContext(
|
||||||
|
functionDescriptor, state,
|
||||||
|
DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor)?.containingFile as? KtFile
|
||||||
|
) as MethodContext
|
||||||
|
|
||||||
|
private val jvmSignature = typeMapper.mapSignatureWithGeneric(functionDescriptor, context.contextKind)
|
||||||
|
|
||||||
|
private val isSameModule = JvmCodegenUtil.isCallInsideSameModuleAsDeclared(functionDescriptor, codegen.getContext(), state.outDirectory)
|
||||||
|
|
||||||
private val invocationParamBuilder = ParametersBuilder.newBuilder()
|
private val invocationParamBuilder = ParametersBuilder.newBuilder()
|
||||||
private val expressionMap = LinkedHashMap<Int, LambdaInfo>()
|
|
||||||
|
|
||||||
private val reifiedTypeInliner: ReifiedTypeInliner
|
private val expressionMap = linkedMapOf<Int, LambdaInfo>()
|
||||||
|
|
||||||
private var activeLambda: LambdaInfo? = null
|
private var activeLambda: LambdaInfo? = null
|
||||||
|
|
||||||
private val sourceMapper: SourceMapper
|
private val sourceMapper = codegen.parentCodegen.orCreateSourceMapper
|
||||||
|
|
||||||
private var delayedHiddenWriting: Function0<Unit>? = null
|
private var delayedHiddenWriting: Function0<Unit>? = null
|
||||||
|
|
||||||
@@ -97,28 +116,6 @@ class InlineCodegen(
|
|||||||
private var methodHandleInDefaultMethodIndex = -1
|
private var methodHandleInDefaultMethodIndex = -1
|
||||||
|
|
||||||
init {
|
init {
|
||||||
assert(InlineUtil.isInline(function) || InlineUtil.isArrayConstructorWithLambda(function)) { "InlineCodegen can inline only inline functions and array constructors: " + function }
|
|
||||||
this.typeMapper = state.typeMapper
|
|
||||||
this.functionDescriptor = if (InlineUtil.isArrayConstructorWithLambda(function))
|
|
||||||
FictitiousArrayConstructor.create(function as ConstructorDescriptor)
|
|
||||||
else
|
|
||||||
function.original
|
|
||||||
|
|
||||||
reifiedTypeInliner = ReifiedTypeInliner(typeParameterMappings)
|
|
||||||
|
|
||||||
initialFrameSize = codegen.frameMap.currentSize
|
|
||||||
|
|
||||||
val element = DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor)
|
|
||||||
context = getContext(functionDescriptor, state, element?.containingFile as? KtFile) as MethodContext
|
|
||||||
jvmSignature = typeMapper.mapSignatureWithGeneric(functionDescriptor, context.contextKind)
|
|
||||||
|
|
||||||
// TODO: implement AS_FUNCTION inline strategy
|
|
||||||
this.asFunctionInline = false
|
|
||||||
|
|
||||||
isSameModule = JvmCodegenUtil.isCallInsideSameModuleAsDeclared(functionDescriptor, codegen.getContext(), state.outDirectory)
|
|
||||||
|
|
||||||
sourceMapper = codegen.parentCodegen.orCreateSourceMapper
|
|
||||||
|
|
||||||
if (functionDescriptor !is FictitiousArrayConstructor) {
|
if (functionDescriptor !is FictitiousArrayConstructor) {
|
||||||
reportIncrementalInfo(functionDescriptor, codegen.getContext().functionDescriptor.original, jvmSignature, state)
|
reportIncrementalInfo(functionDescriptor, codegen.getContext().functionDescriptor.original, jvmSignature, state)
|
||||||
val functionOrAccessorName = typeMapper.mapAsmMethod(function).name
|
val functionOrAccessorName = typeMapper.mapAsmMethod(function).name
|
||||||
@@ -206,7 +203,7 @@ class InlineCodegen(
|
|||||||
for (lambda in defaultLambdas) {
|
for (lambda in defaultLambdas) {
|
||||||
invocationParamBuilder.buildParameters().getParameterByDeclarationSlot(lambda.offset).lambda = lambda
|
invocationParamBuilder.buildParameters().getParameterByDeclarationSlot(lambda.offset).lambda = lambda
|
||||||
val prev = expressionMap.put(lambda.offset, lambda)
|
val prev = expressionMap.put(lambda.offset, lambda)
|
||||||
assert(prev == null) { "Lambda with offset " + lambda.offset + " already exists: " + prev }
|
assert(prev == null) { "Lambda with offset ${lambda.offset} already exists: $prev" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val reificationResult = reifiedTypeInliner.reifyInstructions(node)
|
val reificationResult = reifiedTypeInliner.reifyInstructions(node)
|
||||||
@@ -351,18 +348,14 @@ class InlineCodegen(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun recordParameterValueInLocalVal(delayedWritingToLocals: Boolean, skipStore: Boolean, vararg infos: ParameterInfo): Function0<Unit>? {
|
private fun recordParameterValueInLocalVal(delayedWritingToLocals: Boolean, skipStore: Boolean, vararg infos: ParameterInfo): Function0<Unit>? {
|
||||||
val index = IntArray(infos.size)
|
val index = IntArray(infos.size) { i ->
|
||||||
for (i in infos.indices) {
|
if (!infos[i].isSkippedOrRemapped) {
|
||||||
val info = infos[i]
|
codegen.frameMap.enterTemp(infos[i].getType())
|
||||||
if (!info.isSkippedOrRemapped) {
|
|
||||||
index[i] = codegen.frameMap.enterTemp(info.getType())
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
index[i] = -1
|
|
||||||
}
|
}
|
||||||
|
else -1
|
||||||
}
|
}
|
||||||
|
|
||||||
val runnable = {
|
val possibleLazyTask = {
|
||||||
for (i in infos.indices.reversed()) {
|
for (i in infos.indices.reversed()) {
|
||||||
val info = infos[i]
|
val info = infos[i]
|
||||||
if (!info.isSkippedOrRemapped) {
|
if (!info.isSkippedOrRemapped) {
|
||||||
@@ -379,8 +372,8 @@ class InlineCodegen(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (delayedWritingToLocals) return runnable
|
if (delayedWritingToLocals) return possibleLazyTask
|
||||||
runnable()
|
possibleLazyTask()
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,10 +396,8 @@ class InlineCodegen(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun leaveTemps() {
|
private fun leaveTemps() {
|
||||||
val infos = invocationParamBuilder.listAllParams()
|
invocationParamBuilder.listAllParams().asReversed().forEach {
|
||||||
val iterator = infos.listIterator(infos.size)
|
param ->
|
||||||
while (iterator.hasPrevious()) {
|
|
||||||
val param = iterator.previous()
|
|
||||||
if (!param.isSkippedOrRemapped || CapturedParamInfo.isSynthetic(param)) {
|
if (!param.isSkippedOrRemapped || CapturedParamInfo.isSynthetic(param)) {
|
||||||
codegen.frameMap.leaveTemp(param.type)
|
codegen.frameMap.leaveTemp(param.type)
|
||||||
}
|
}
|
||||||
@@ -414,15 +405,16 @@ class InlineCodegen(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun rememberClosure(expression: KtExpression, type: Type, parameter: ValueParameterDescriptor): LambdaInfo {
|
private fun rememberClosure(expression: KtExpression, type: Type, parameter: ValueParameterDescriptor): LambdaInfo {
|
||||||
val lambda = KtPsiUtil.deparenthesize(expression)
|
val ktLambda = KtPsiUtil.deparenthesize(expression)
|
||||||
assert(isInlinableParameterExpression(lambda)) { "Couldn't find inline expression in " + expression.text }
|
assert(isInlinableParameterExpression(ktLambda)) { "Couldn't find inline expression in ${expression.text}" }
|
||||||
|
|
||||||
val info = ExpressionLambda(lambda!!, typeMapper, parameter.isCrossinline, getBoundCallableReferenceReceiver(expression) != null)
|
return ExpressionLambda(
|
||||||
|
ktLambda!!, typeMapper, parameter.isCrossinline, getBoundCallableReferenceReceiver(expression) != null
|
||||||
val closureInfo = invocationParamBuilder.addNextValueParameter(type, true, null, parameter.index)
|
).also { lambda ->
|
||||||
closureInfo.lambda = info
|
val closureInfo = invocationParamBuilder.addNextValueParameter(type, true, null, parameter.index)
|
||||||
expressionMap.put(closureInfo.index, info)
|
closureInfo.lambda = lambda
|
||||||
return info
|
expressionMap.put(closureInfo.index, lambda)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun putClosureParametersOnStack() {
|
private fun putClosureParametersOnStack() {
|
||||||
@@ -442,15 +434,13 @@ class InlineCodegen(
|
|||||||
rememberCapturedForDefaultLambda(next)
|
rememberCapturedForDefaultLambda(next)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw RuntimeException("Unknown lambda: " + next)
|
throw RuntimeException("Unknown lambda: $next")
|
||||||
}
|
}
|
||||||
activeLambda = null
|
activeLambda = null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun rememberCapturedForDefaultLambda(defaultLambda: DefaultLambda) {
|
private fun rememberCapturedForDefaultLambda(defaultLambda: DefaultLambda) {
|
||||||
val vars = defaultLambda.capturedVars
|
for ((paramIndex, captured) in defaultLambda.capturedVars.withIndex()) {
|
||||||
var paramIndex = 0
|
|
||||||
for (captured in vars) {
|
|
||||||
putArgumentOrCapturedToLocalVal(
|
putArgumentOrCapturedToLocalVal(
|
||||||
captured.type,
|
captured.type,
|
||||||
//HACK: actually parameter would be placed on stack in default function
|
//HACK: actually parameter would be placed on stack in default function
|
||||||
@@ -461,7 +451,6 @@ class InlineCodegen(
|
|||||||
ValueKind.DEFAULT_LAMBDA_CAPTURED_PARAMETER
|
ValueKind.DEFAULT_LAMBDA_CAPTURED_PARAMETER
|
||||||
)
|
)
|
||||||
|
|
||||||
paramIndex++
|
|
||||||
defaultLambda.parameterOffsetsInDefault.add(invocationParamBuilder.nextParameterOffset)
|
defaultLambda.parameterOffsetsInDefault.add(invocationParamBuilder.nextParameterOffset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user