All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.resolve.calls.results.FlatSignatureUtils.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.resolve.calls.results

import org.jetbrains.kotlin.builtins.getValueParameterTypesFromCallableReflectionType
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.MemberDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.UnwrappedType

fun  FlatSignature.Companion.createFromReflectionType(
    origin: T,
    descriptor: CallableDescriptor,
    numDefaults: Int,
    // Reflection type for callable references with bound receiver doesn't contain receiver type
    hasBoundExtensionReceiver: Boolean,
    reflectionType: UnwrappedType
): FlatSignature {
    // Note that receiver is taking over descriptor, not reflection type
    // This is correct as extension receiver can't have any defaults/varargs/coercions, so there is no need to use reflection type
    // Plus, currently, receiver for reflection type is taking from *candidate*, see buildReflectionType, this candidate can
    // have transient receiver which is not the same in its signature
    val receiver = descriptor.extensionReceiverParameter?.type
    val parameters = reflectionType.getValueParameterTypesFromCallableReflectionType(
        receiver != null && !hasBoundExtensionReceiver
    ).map { it.type }

    return FlatSignature(
        origin,
        descriptor.typeParameters,
        listOfNotNull(receiver) + parameters,
        hasExtensionReceiver = receiver != null,
        hasVarargs = descriptor.valueParameters.any { it.varargElementType != null },
        numDefaults = numDefaults,
        isExpect = descriptor is MemberDescriptor && descriptor.isExpect,
        isSyntheticMember = descriptor is SyntheticMemberDescriptor<*>
    )
}

fun  FlatSignature.Companion.create(
    origin: T,
    descriptor: CallableDescriptor,
    numDefaults: Int,
    parameterTypes: List
): FlatSignature {
    val extensionReceiverType = descriptor.extensionReceiverParameter?.type

    return FlatSignature(
        origin,
        descriptor.typeParameters,
        valueParameterTypes =
        listOfNotNull(extensionReceiverType) + parameterTypes,
        hasExtensionReceiver = extensionReceiverType != null,
        hasVarargs = descriptor.valueParameters.any { it.varargElementType != null },
        numDefaults = numDefaults,
        isExpect = descriptor is MemberDescriptor && descriptor.isExpect,
        isSyntheticMember = descriptor is SyntheticMemberDescriptor<*>
    )
}

fun  FlatSignature.Companion.createFromCallableDescriptor(
    descriptor: D
): FlatSignature =
    create(descriptor, descriptor, numDefaults = 0, parameterTypes = descriptor.valueParameters.map { it.argumentValueType })

fun  FlatSignature.Companion.createForPossiblyShadowedExtension(descriptor: D): FlatSignature =
    FlatSignature(
        descriptor,
        descriptor.typeParameters,
        valueParameterTypes = descriptor.valueParameters.map { it.argumentValueType },
        hasExtensionReceiver = false,
        hasVarargs = descriptor.valueParameters.any { it.varargElementType != null },
        numDefaults = descriptor.valueParameters.count { it.hasDefaultValue() },
        isExpect = descriptor is MemberDescriptor && descriptor.isExpect,
        isSyntheticMember = descriptor is SyntheticMemberDescriptor<*>
    )

val ValueParameterDescriptor.argumentValueType get() = varargElementType ?: type




© 2015 - 2024 Weber Informatics LLC | Privacy Policy