[FIR] Remove Candidate.oldToNewArgumentMapping and use
Candidate.argumentMapping instead of the Fir*Call's argumentList to remap vararg arguments to a FirVarargArgumentExpression. The argumentList was used to preserve the order, but we can rely on Candidate.argumentMapping to have its keys in order.
This commit is contained in:
committed by
teamcityserver
parent
a2a4d94834
commit
6b83f2d70e
@@ -104,7 +104,6 @@ class Candidate(
|
||||
|
||||
var argumentMapping: Map<FirExpression, FirValueParameter>? = null
|
||||
var numDefaults: Int = 0
|
||||
var oldToNewArgumentMapping: Map<FirExpression, FirExpression>? = null
|
||||
lateinit var typeArgumentMapping: TypeArgumentMapping
|
||||
val postponedAtoms = mutableListOf<PostponedResolvedAtom>()
|
||||
|
||||
|
||||
+2
-5
@@ -32,7 +32,6 @@ data class ArgumentMapping(
|
||||
// TODO: Consider changing this (and other similar declarations like Candidate.argumentMapping) to LinkedHashMap to signify that
|
||||
// order is important. Right now we're assuming that mutableMapOf() will always return a LinkedHashMap.
|
||||
val parameterToCallArgumentMap: Map<FirValueParameter, ResolvedCallArgument>,
|
||||
val oldToNewArgumentMap: Map<FirExpression, FirExpression>,
|
||||
val diagnostics: List<ResolutionDiagnostic>
|
||||
) {
|
||||
fun toArgumentToParameterMapping(): Map<FirExpression, FirValueParameter> {
|
||||
@@ -53,7 +52,7 @@ data class ArgumentMapping(
|
||||
}
|
||||
}
|
||||
|
||||
private val EmptyArgumentMapping = ArgumentMapping(emptyMap(), emptyMap(), emptyList())
|
||||
private val EmptyArgumentMapping = ArgumentMapping(emptyMap(), emptyList())
|
||||
|
||||
fun BodyResolveComponents.mapArguments(
|
||||
arguments: List<FirExpression>,
|
||||
@@ -72,7 +71,6 @@ fun BodyResolveComponents.mapArguments(
|
||||
|
||||
// If this is an overloading indexed access operator, it could have default values or a vararg parameter in the middle.
|
||||
// For proper argument mapping, wrap the last one, which is supposed to be the updated value, as a named argument.
|
||||
val oldToNewArgumentMap = mutableMapOf<FirExpression, FirExpression>()
|
||||
if ((function as? FirSimpleFunction)?.isOperator == true &&
|
||||
function.name == Name.identifier("set") &&
|
||||
function.valueParameters.any { it.defaultValue != null || it.isVararg }
|
||||
@@ -86,7 +84,6 @@ fun BodyResolveComponents.mapArguments(
|
||||
name = function.valueParameters.last().name
|
||||
}
|
||||
argumentsInParenthesis = argumentsInParenthesis.dropLast(1) + listOf(namedV)
|
||||
oldToNewArgumentMap[v] = namedV
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +94,7 @@ fun BodyResolveComponents.mapArguments(
|
||||
}
|
||||
processor.processDefaultsAndRunChecks()
|
||||
|
||||
return ArgumentMapping(processor.result, oldToNewArgumentMap, processor.diagnostics ?: emptyList())
|
||||
return ArgumentMapping(processor.result, processor.diagnostics ?: emptyList())
|
||||
}
|
||||
|
||||
private class FirCallArgumentsProcessor(
|
||||
|
||||
@@ -172,7 +172,6 @@ internal object MapArguments : ResolutionStage() {
|
||||
val mapping = context.bodyResolveComponents.mapArguments(callInfo.arguments, function, candidate.originScope)
|
||||
candidate.argumentMapping = mapping.toArgumentToParameterMapping()
|
||||
candidate.numDefaults = mapping.numDefaults()
|
||||
candidate.oldToNewArgumentMapping = mapping.oldToNewArgumentMap
|
||||
|
||||
mapping.diagnostics.forEach(sink::reportDiagnostic)
|
||||
sink.yieldIfNeed()
|
||||
|
||||
+5
-14
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.fir.*
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic
|
||||
import org.jetbrains.kotlin.fir.expressions.*
|
||||
import org.jetbrains.kotlin.fir.expressions.builder.buildArgumentList
|
||||
import org.jetbrains.kotlin.fir.references.builder.buildErrorNamedReference
|
||||
import org.jetbrains.kotlin.fir.references.builder.buildResolvedCallableReference
|
||||
import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference
|
||||
@@ -166,15 +165,7 @@ class FirCallCompletionResultsWriterTransformer(
|
||||
val expectedArgumentsTypeMapping = runIf(!calleeReference.isError) { subCandidate.createArgumentsMapping() }
|
||||
result.argumentList.transformArguments(this, expectedArgumentsTypeMapping)
|
||||
if (!calleeReference.isError) {
|
||||
subCandidate.oldToNewArgumentMapping?.let {
|
||||
result.replaceArgumentList(buildArgumentList {
|
||||
source = result.argumentList.source
|
||||
for (oldArgument in result.argumentList.arguments) {
|
||||
arguments += it[oldArgument] ?: oldArgument
|
||||
}
|
||||
})
|
||||
}
|
||||
subCandidate.handleVarargs(result.argumentList)
|
||||
subCandidate.handleVarargs()
|
||||
subCandidate.argumentMapping?.let {
|
||||
result.replaceArgumentList(buildResolvedArgumentList(it))
|
||||
}
|
||||
@@ -221,7 +212,7 @@ class FirCallCompletionResultsWriterTransformer(
|
||||
}
|
||||
}
|
||||
if (!calleeReference.isError) {
|
||||
subCandidate.handleVarargs(annotationCall.argumentList)
|
||||
subCandidate.handleVarargs()
|
||||
subCandidate.argumentMapping?.let {
|
||||
annotationCall.replaceArgumentList(buildResolvedArgumentList(it))
|
||||
}
|
||||
@@ -229,14 +220,14 @@ class FirCallCompletionResultsWriterTransformer(
|
||||
return annotationCall.compose()
|
||||
}
|
||||
|
||||
private fun Candidate.handleVarargs(argumentList: FirArgumentList) {
|
||||
private fun Candidate.handleVarargs() {
|
||||
val argumentMapping = this.argumentMapping
|
||||
val varargParameter = argumentMapping?.values?.firstOrNull { it.isVararg }
|
||||
if (varargParameter != null) {
|
||||
// Create a FirVarargArgumentExpression for the vararg arguments
|
||||
val varargParameterTypeRef = varargParameter.returnTypeRef
|
||||
val resolvedArrayType = varargParameterTypeRef.substitute(this)
|
||||
this.argumentMapping = remapArgumentsWithVararg(varargParameter, resolvedArrayType, argumentList, argumentMapping)
|
||||
this.argumentMapping = remapArgumentsWithVararg(varargParameter, resolvedArrayType, argumentMapping)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -367,7 +358,7 @@ class FirCallCompletionResultsWriterTransformer(
|
||||
val argumentsMapping = runIf(!calleeReference.isError) { calleeReference.candidate.createArgumentsMapping() }
|
||||
delegatedConstructorCall.argumentList.transformArguments(this, argumentsMapping)
|
||||
if (!calleeReference.isError) {
|
||||
subCandidate.handleVarargs(delegatedConstructorCall.argumentList)
|
||||
subCandidate.handleVarargs()
|
||||
subCandidate.argumentMapping?.let {
|
||||
delegatedConstructorCall.replaceArgumentList(buildResolvedArgumentList(it))
|
||||
}
|
||||
|
||||
+7
-8
@@ -37,7 +37,6 @@ internal inline var FirExpression.resultType: FirTypeRef
|
||||
internal fun remapArgumentsWithVararg(
|
||||
varargParameter: FirValueParameter,
|
||||
varargArrayType: ConeKotlinType,
|
||||
argumentList: FirArgumentList,
|
||||
argumentMapping: Map<FirExpression, FirValueParameter>
|
||||
): Map<FirExpression, FirValueParameter> {
|
||||
// Create a FirVarargArgumentExpression for the vararg arguments.
|
||||
@@ -45,13 +44,14 @@ internal fun remapArgumentsWithVararg(
|
||||
// FIR2IR uses the mapping order to determine if arguments need to be reordered.
|
||||
val varargParameterTypeRef = varargParameter.returnTypeRef
|
||||
val varargElementType = varargArrayType.arrayElementType()
|
||||
var indexAfterVarargs = argumentList.arguments.size
|
||||
val argumentList = argumentMapping.keys.toList()
|
||||
var indexAfterVarargs = argumentList.size
|
||||
val newArgumentMapping = mutableMapOf<FirExpression, FirValueParameter>()
|
||||
val varargArgument = buildVarargArgumentsExpression {
|
||||
this.varargElementType = varargParameterTypeRef.withReplacedConeType(varargElementType)
|
||||
this.typeRef = varargParameterTypeRef.withReplacedConeType(varargArrayType)
|
||||
for ((i, arg) in argumentList.arguments.withIndex()) {
|
||||
val valueParameter = argumentMapping[arg] ?: continue
|
||||
for ((i, arg) in argumentList.withIndex()) {
|
||||
val valueParameter = argumentMapping.getValue(arg)
|
||||
if (valueParameter.isVararg) {
|
||||
// `arg` is a vararg argument.
|
||||
arguments += arg
|
||||
@@ -68,10 +68,9 @@ internal fun remapArgumentsWithVararg(
|
||||
newArgumentMapping[varargArgument] = varargParameter
|
||||
|
||||
// Add mapping for arguments after the vararg arguments, if any.
|
||||
for (i in indexAfterVarargs until argumentList.arguments.size) {
|
||||
val arg = argumentList.arguments[i]
|
||||
val valueParameter = argumentMapping[arg] ?: continue
|
||||
newArgumentMapping[arg] = valueParameter
|
||||
for (i in indexAfterVarargs until argumentList.size) {
|
||||
val arg = argumentList[i]
|
||||
newArgumentMapping[arg] = argumentMapping.getValue(arg)
|
||||
}
|
||||
return newArgumentMapping
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user