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.IrFile
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||||
import org.jetbrains.kotlin.ir.expressions.*
|
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.expressions.impl.IrBlockImpl
|
||||||
import org.jetbrains.kotlin.ir.util.DeepCopyIrTree
|
import org.jetbrains.kotlin.ir.util.DeepCopyIrTree
|
||||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||||
@@ -30,25 +29,25 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid(
|
|||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
|
|
||||||
override fun visitCall(expression: IrCall): IrExpression {
|
override fun visitCall(expression: IrCall): IrExpression {
|
||||||
val functionDescriptor = expression.descriptor as FunctionDescriptor //
|
val functionDescriptor = expression.descriptor as FunctionDescriptor
|
||||||
if (!functionDescriptor.isInline) return super.visitCall(expression) // function is not to be inlined - do nothing
|
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
|
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?
|
if (functionDeclaration == null) return super.visitCall(expression) // Function is declared in another module.
|
||||||
val copyFuncDeclaration = functionDeclaration.accept(DeepCopyIrTree(), null) as IrFunction
|
val copyFuncDeclaration = functionDeclaration.accept(DeepCopyIrTree(), null) as IrFunction // Create copy of the function.
|
||||||
|
|
||||||
val body = copyFuncDeclaration.body!! as IrBlockBody
|
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 startOffset = copyFuncDeclaration.startOffset
|
||||||
val endOffset = copyFuncDeclaration.endOffset
|
val endOffset = copyFuncDeclaration.endOffset
|
||||||
val returnType = copyFuncDeclaration.descriptor.returnType!!
|
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)
|
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 {
|
override fun visitGetValue(expression: IrGetValue): IrExpression {
|
||||||
val descriptor = expression.descriptor
|
val descriptor = expression.descriptor
|
||||||
if (descriptor !is ParameterDescriptor) {
|
if (descriptor !is ParameterDescriptor) { // TODO do we need this check?
|
||||||
return super.visitGetValue(expression)
|
return super.visitGetValue(expression)
|
||||||
}
|
}
|
||||||
val parExp = parameterToExpression.find { it.first == descriptor }
|
val parExp = parameterToExpression.find { it.first == descriptor } // Find expression to replace this parameter.
|
||||||
return parExp?.let { parExp.second } ?: super.visitGetValue(expression)
|
return parExp?.let { parExp.second } ?: super.visitGetValue(expression) // TODO should we proceed with IR iteration here?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user