Comments added
This commit is contained in:
+13
-14
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockImpl
|
||||
import org.jetbrains.kotlin.ir.util.DeepCopyIrTree
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||
@@ -30,25 +29,25 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid(
|
||||
//-------------------------------------------------------------------------//
|
||||
|
||||
override fun visitCall(expression: IrCall): IrExpression {
|
||||
val functionDescriptor = expression.descriptor as FunctionDescriptor //
|
||||
if (!functionDescriptor.isInline) return super.visitCall(expression) // function is not to be inlined - do nothing
|
||||
val functionDescriptor = expression.descriptor as FunctionDescriptor
|
||||
if (!functionDescriptor.isInline) return super.visitCall(expression) // Function is not to be inlined - do nothing.
|
||||
|
||||
val functionDeclaration = context.ir.moduleIndex.functions[functionDescriptor] // get FunctionDeclaration by FunctionDescriptor
|
||||
if (functionDeclaration == null) return super.visitCall(expression) // TODO what if we do not have declaration?
|
||||
val copyFuncDeclaration = functionDeclaration.accept(DeepCopyIrTree(), null) as IrFunction
|
||||
val functionDeclaration = context.ir.moduleIndex.functions[functionDescriptor] // Get FunctionDeclaration by FunctionDescriptor.
|
||||
if (functionDeclaration == null) return super.visitCall(expression) // Function is declared in another module.
|
||||
val copyFuncDeclaration = functionDeclaration.accept(DeepCopyIrTree(), null) as IrFunction // Create copy of the function.
|
||||
|
||||
val body = copyFuncDeclaration.body!! as IrBlockBody
|
||||
val statements = removeReturn(body.statements)
|
||||
val statements = removeReturn(body.statements) // Replace "return" with its value.
|
||||
val startOffset = copyFuncDeclaration.startOffset
|
||||
val endOffset = copyFuncDeclaration.endOffset
|
||||
val returnType = copyFuncDeclaration.descriptor.returnType!!
|
||||
val irBlock = IrBlockImpl(startOffset, endOffset, returnType, null, statements) // create
|
||||
val irBlock = IrBlockImpl(startOffset, endOffset, returnType, null, statements) // Create IrBlock containing function statements.
|
||||
|
||||
val parameterToExpression = expression.getArguments()
|
||||
val parameterToExpression = expression.getArguments() // Build map parameter -> expression.
|
||||
val parametersTransformer = ParametersTransformer(parameterToExpression)
|
||||
irBlock.accept(parametersTransformer, null)
|
||||
irBlock.accept(parametersTransformer, null) // Replace parameters with expression.
|
||||
|
||||
return irBlock
|
||||
return irBlock // Return newly created IrBlock instead of IrCall.
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------//
|
||||
@@ -66,10 +65,10 @@ internal class ParametersTransformer(val parameterToExpression: List <Pair<Param
|
||||
|
||||
override fun visitGetValue(expression: IrGetValue): IrExpression {
|
||||
val descriptor = expression.descriptor
|
||||
if (descriptor !is ParameterDescriptor) {
|
||||
if (descriptor !is ParameterDescriptor) { // TODO do we need this check?
|
||||
return super.visitGetValue(expression)
|
||||
}
|
||||
val parExp = parameterToExpression.find { it.first == descriptor }
|
||||
return parExp?.let { parExp.second } ?: super.visitGetValue(expression)
|
||||
val parExp = parameterToExpression.find { it.first == descriptor } // Find expression to replace this parameter.
|
||||
return parExp?.let { parExp.second } ?: super.visitGetValue(expression) // TODO should we proceed with IR iteration here?
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user