Intermediate results commit
This commit is contained in:
committed by
KonstantinAnisimov
parent
8d62ed20ea
commit
3ebcf92f8b
+37
-36
@@ -55,7 +55,7 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid(
|
|||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
|
|
||||||
fun evaluateParameters(irCall: IrCall,
|
fun evaluateParameters(irCall: IrCall,
|
||||||
statements: MutableList<IrStatement>): MutableList<Pair<DeclarationDescriptor, IrExpression>> {
|
statements: MutableList<IrStatement>): MutableList<Pair<ValueDescriptor, IrExpression>> {
|
||||||
|
|
||||||
val parametersOld = irCall.getArguments() // Create map call_site_argument -> inline_function_parameter.
|
val parametersOld = irCall.getArguments() // Create map call_site_argument -> inline_function_parameter.
|
||||||
val parametersNew = parametersOld.map {
|
val parametersNew = parametersOld.map {
|
||||||
@@ -82,8 +82,7 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid(
|
|||||||
val functionDeclaration = context.ir.originalModuleIndex
|
val functionDeclaration = context.ir.originalModuleIndex
|
||||||
.functions[functionDescriptor.original] // Get FunctionDeclaration by FunctionDescriptor.
|
.functions[functionDescriptor.original] // Get FunctionDeclaration by FunctionDescriptor.
|
||||||
|
|
||||||
val result = super.visitCall(irCall)
|
if (functionDeclaration == null) return irCall // Function is declared in another module.
|
||||||
if (functionDeclaration == null) return result // Function is declared in another module.
|
|
||||||
print("file: ${currentFile!!.fileEntry.name} ") // TODO debug output
|
print("file: ${currentFile!!.fileEntry.name} ") // TODO debug output
|
||||||
print("function: ${currentFunction!!.descriptor.name} ") // TODO debug output
|
print("function: ${currentFunction!!.descriptor.name} ") // TODO debug output
|
||||||
println("call: ${functionDescriptor.name} ${irCall.startOffset}") // 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 endOffset = copyFuncDeclaration.endOffset
|
||||||
val returnType = copyFuncDeclaration.descriptor.returnType!!
|
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 blockBody = copyFuncDeclaration.body!! as IrBlockBody
|
||||||
val statements = blockBody.statements
|
val statements = blockBody.statements
|
||||||
val parameters = evaluateParameters(irCall, statements) // Evaluate parameters representing expression.
|
|
||||||
val inlineBody = IrInlineFunctionBody(startOffset, endOffset, returnType, null, statements)
|
val inlineBody = IrInlineFunctionBody(startOffset, endOffset, returnType, null, statements)
|
||||||
|
|
||||||
val lambdaInliner = LambdaInliner(parameters)
|
val newBody = super.visitBlock(inlineBody) as IrBlock
|
||||||
inlineBody.accept(lambdaInliner, null)
|
|
||||||
val transformer = ParametersTransformer(parameters, irCall)
|
|
||||||
inlineBody.accept(transformer, null) // Replace parameters with expression.
|
|
||||||
|
|
||||||
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 {
|
override fun visitCall(expression: IrCall): IrExpression {
|
||||||
|
|
||||||
val fqName = currentFile!!.packageFragmentDescriptor.fqName.asString() // TODO to be removed after stdlib compilation
|
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
|
val fileName = currentFile!!.fileEntry.name
|
||||||
if (fileName.contains("cinterop")) return super.visitCall(expression)
|
if (fileName.contains("cinterop")) return super.visitCall(expression)
|
||||||
if (currentFunction!!.descriptor.isInline) return super.visitCall(expression) // TODO workaround
|
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:
|
internal class LambdaInliner(val parameterToArgument:
|
||||||
MutableList<Pair<DeclarationDescriptor, IrExpression>>): IrElementTransformerVoid() {
|
MutableList<Pair<ValueDescriptor, IrExpression>>, val scope: Scope): IrElementTransformerVoid() {
|
||||||
|
|
||||||
override fun visitElement(element: IrElement) = element.accept(this, null)
|
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 parameters = lambdaFunction.descriptor.valueParameters // Lambda function parameters
|
||||||
val res = parameters.map {
|
val res = parameters.map {
|
||||||
val argument = irCall.getValueArgument(it.index)
|
val argument = irCall.getValueArgument(it.index)
|
||||||
val parameter = it as DeclarationDescriptor
|
val parameter = it as ValueDescriptor
|
||||||
parameter to argument!!
|
parameter to argument!!
|
||||||
}.toMutableList()
|
}.toMutableList()
|
||||||
|
|
||||||
@@ -218,8 +219,8 @@ internal class LambdaInliner(val parameterToArgument:
|
|||||||
val lambdaReturnType = getLambdaReturnType(lambdaArgument)
|
val lambdaReturnType = getLambdaReturnType(lambdaArgument)
|
||||||
val inlineBody = IrInlineFunctionBody(0, 0, lambdaReturnType, null, lambdaStatements)
|
val inlineBody = IrInlineFunctionBody(0, 0, lambdaReturnType, null, lambdaStatements)
|
||||||
|
|
||||||
val transformer = ParametersTransformer(res, irCall)
|
val transformer = ParametersTransformer(res, irCall, scope)
|
||||||
inlineBody.accept(transformer, null) // Replace parameters with expression.
|
val aa = inlineBody.accept(transformer, null) // TODO // Replace parameters with expression.
|
||||||
|
|
||||||
return inlineBody // Replace call site with InlineFunctionBody.
|
return inlineBody // Replace call site with InlineFunctionBody.
|
||||||
}
|
}
|
||||||
@@ -234,11 +235,10 @@ internal class LambdaInliner(val parameterToArgument:
|
|||||||
|
|
||||||
//-----------------------------------------------------------------------------//
|
//-----------------------------------------------------------------------------//
|
||||||
|
|
||||||
internal class ParametersTransformer(val parameterToArgument: MutableList<Pair<DeclarationDescriptor, IrExpression>>,
|
internal class ParametersTransformer(val parameterToArgument: MutableList<Pair<ValueDescriptor, IrExpression>>,
|
||||||
val callSite: IrCall): IrElementTransformerVoid() {
|
val callSite: IrCall, val scope: Scope): IrElementTransformerVoid() {
|
||||||
|
|
||||||
override fun visitElement(element: IrElement) = element.accept(this, null)
|
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<Pair<D
|
|||||||
val typeNew = typeArgsMap[operandTypeDescriptor]!!
|
val typeNew = typeArgsMap[operandTypeDescriptor]!!
|
||||||
val startOffset = expression.startOffset
|
val startOffset = expression.startOffset
|
||||||
val endOffset = expression.endOffset
|
val endOffset = expression.endOffset
|
||||||
val type = expression.type
|
val type = expression.type // TODO
|
||||||
val operator = expression.operator
|
val operator = expression.operator
|
||||||
val argument = expression.argument
|
val argument = expression.argument
|
||||||
|
|
||||||
@@ -295,32 +295,33 @@ internal class ParametersTransformer(val parameterToArgument: MutableList<Pair<D
|
|||||||
fun substituteType(variable: IrVariable): KotlinType? {
|
fun substituteType(variable: IrVariable): KotlinType? {
|
||||||
|
|
||||||
val typeArgsMap = (callSite as IrMemberAccessExpressionBase).typeArguments
|
val typeArgsMap = (callSite as IrMemberAccessExpressionBase).typeArguments
|
||||||
if (typeArgsMap == null) return null
|
if (typeArgsMap == null) return variable.descriptor.type
|
||||||
val oldType = variable.descriptor.type
|
val oldType = variable.descriptor.type
|
||||||
val typeDescriptor = oldType.constructor.declarationDescriptor
|
val typeDescriptor = oldType.constructor.declarationDescriptor
|
||||||
return typeArgsMap[typeDescriptor]
|
val newType = typeArgsMap[typeDescriptor]
|
||||||
|
if (newType == null) return variable.descriptor.type
|
||||||
|
return newType
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
|
|
||||||
override fun visitVariable(declaration: IrVariable): IrStatement {
|
override fun visitVariable(declaration: IrVariable): IrStatement {
|
||||||
|
|
||||||
val result = super.visitVariable(declaration)
|
// println("visitVariableAkm ${declaration.descriptor.name}")
|
||||||
val type = substituteType(declaration)
|
|
||||||
if (type == null) {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
val newVar = newVariable(declaration, type)
|
val replacement = parameterToArgument.find {
|
||||||
val rr = parameterToArgument.find { it.first == declaration.descriptor }
|
if (it.second !is IrGetValue) return super.visitVariable(declaration)
|
||||||
if (rr == null) {
|
val aa = it.second as IrGetValue
|
||||||
val getVal = IrGetValueImpl(0, 0, newVar.descriptor) // Create new IR element representing access the new variable.
|
aa.descriptor == declaration.descriptor
|
||||||
parameterToArgument.add(declaration.descriptor to getVal)
|
} // Try to find
|
||||||
} else {
|
if (replacement != null) return super.visitVariable(declaration)
|
||||||
return rr.second
|
|
||||||
}
|
|
||||||
|
|
||||||
return newVar
|
val nn = super.visitVariable(declaration) as IrVariable
|
||||||
|
val newType = substituteType(nn)!! // If variable type should be replaced with type arg.
|
||||||
|
val newVariable = newVariable(nn, newType) // Create new local variable.
|
||||||
|
val getVal = IrGetValueImpl(0, 0, newVariable.descriptor) // Create new IR element representing access the new variable.
|
||||||
|
parameterToArgument.add(nn.descriptor to getVal)
|
||||||
|
return newVariable
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user