From 3ebcf92f8b540893ccd2509f52c2779d646e7384 Mon Sep 17 00:00:00 2001 From: Konstantin Anisimov Date: Mon, 13 Mar 2017 12:28:50 +0700 Subject: [PATCH] Intermediate results commit --- .../backend/konan/lower/FunctionInlining.kt | 73 ++++++++++--------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt index 69c96c54e11..31295e1b6e7 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt @@ -55,7 +55,7 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid( //-------------------------------------------------------------------------// fun evaluateParameters(irCall: IrCall, - statements: MutableList): MutableList> { + statements: MutableList): MutableList> { val parametersOld = irCall.getArguments() // Create map call_site_argument -> inline_function_parameter. val parametersNew = parametersOld.map { @@ -82,8 +82,7 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid( val functionDeclaration = context.ir.originalModuleIndex .functions[functionDescriptor.original] // Get FunctionDeclaration by FunctionDescriptor. - val result = super.visitCall(irCall) - if (functionDeclaration == null) return result // Function is declared in another module. + if (functionDeclaration == null) return irCall // Function is declared in another module. print("file: ${currentFile!!.fileEntry.name} ") // TODO debug output print("function: ${currentFunction!!.descriptor.name} ") // TODO debug output println("call: ${functionDescriptor.name} ${irCall.startOffset}") // TODO debug output @@ -95,18 +94,20 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid( val endOffset = copyFuncDeclaration.endOffset val returnType = copyFuncDeclaration.descriptor.returnType!! - if (copyFuncDeclaration.body == null) return result // TODO workaround + if (copyFuncDeclaration.body == null) return irCall // TODO workaround val blockBody = copyFuncDeclaration.body!! as IrBlockBody val statements = blockBody.statements - val parameters = evaluateParameters(irCall, statements) // Evaluate parameters representing expression. val inlineBody = IrInlineFunctionBody(startOffset, endOffset, returnType, null, statements) - val lambdaInliner = LambdaInliner(parameters) - inlineBody.accept(lambdaInliner, null) - val transformer = ParametersTransformer(parameters, irCall) - inlineBody.accept(transformer, null) // Replace parameters with expression. + val newBody = super.visitBlock(inlineBody) as IrBlock - return super.visitBlock(inlineBody) + val parameters = evaluateParameters(irCall, newBody.statements) // Evaluate parameters representing expression. + val lambdaInliner = LambdaInliner(parameters, functionScope!!) + newBody.accept(lambdaInliner, null) // TODO + val transformer = ParametersTransformer(parameters, irCall, functionScope!!) + newBody.accept(transformer, null) // TODO // Replace parameters with expression. + + return newBody } //-------------------------------------------------------------------------// @@ -114,7 +115,7 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid( override fun visitCall(expression: IrCall): IrExpression { val fqName = currentFile!!.packageFragmentDescriptor.fqName.asString() // TODO to be removed after stdlib compilation - // if(fqName.contains("kotlin")) return super.visitCall(expression) // TODO to be removed after stdlib compilation + //if(fqName.contains("kotlin")) return super.visitCall(expression) // TODO to be removed after stdlib compilation val fileName = currentFile!!.fileEntry.name if (fileName.contains("cinterop")) return super.visitCall(expression) if (currentFunction!!.descriptor.isInline) return super.visitCall(expression) // TODO workaround @@ -133,7 +134,7 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid( //-----------------------------------------------------------------------------// internal class LambdaInliner(val parameterToArgument: - MutableList>): IrElementTransformerVoid() { + MutableList>, val scope: Scope): IrElementTransformerVoid() { override fun visitElement(element: IrElement) = element.accept(this, null) @@ -210,7 +211,7 @@ internal class LambdaInliner(val parameterToArgument: val parameters = lambdaFunction.descriptor.valueParameters // Lambda function parameters val res = parameters.map { val argument = irCall.getValueArgument(it.index) - val parameter = it as DeclarationDescriptor + val parameter = it as ValueDescriptor parameter to argument!! }.toMutableList() @@ -218,8 +219,8 @@ internal class LambdaInliner(val parameterToArgument: val lambdaReturnType = getLambdaReturnType(lambdaArgument) val inlineBody = IrInlineFunctionBody(0, 0, lambdaReturnType, null, lambdaStatements) - val transformer = ParametersTransformer(res, irCall) - inlineBody.accept(transformer, null) // Replace parameters with expression. + val transformer = ParametersTransformer(res, irCall, scope) + val aa = inlineBody.accept(transformer, null) // TODO // Replace parameters with expression. return inlineBody // Replace call site with InlineFunctionBody. } @@ -234,11 +235,10 @@ internal class LambdaInliner(val parameterToArgument: //-----------------------------------------------------------------------------// -internal class ParametersTransformer(val parameterToArgument: MutableList>, - val callSite: IrCall): IrElementTransformerVoid() { +internal class ParametersTransformer(val parameterToArgument: MutableList>, + val callSite: IrCall, val scope: Scope): IrElementTransformerVoid() { override fun visitElement(element: IrElement) = element.accept(this, null) - val scope = Scope(callSite.descriptor as FunctionDescriptor) //-------------------------------------------------------------------------// @@ -260,7 +260,7 @@ internal class ParametersTransformer(val parameterToArgument: MutableList