Repository: JetBrains/kotlin
File: compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/fragments/FragmentCompilerSymbolTableDecorator.kt (Kotlin)

Code before:
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.util.IdSignatureComposer
import org.jetbrains.kotlin.ir.util.NameProvider
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi2ir.generators.fragments.EvaluatorFragmentInfo
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitClassReceiver

class FragmentCompilerSymbolTableDecorator(
    signatureComposer: IdSignatureComposer,
    irFactory: IrFactory,
    nameProvider: NameProvider = NameProvider.DEFAULT,
    private val fragmentInfo: EvaluatorFragmentInfo
) : SymbolTable(signatureComposer, irFactory, nameProvider) {

    override fun referenceValueParameter(descriptor: ParameterDescriptor): IrValueParameterSymbol {
        if (descriptor !is ReceiverParameterDescriptor) return super.referenceValueParameter(descriptor)

        val finderPredicate = when (val receiverValue = descriptor.value) {
            is ExtensionReceiver -> { targetDescriptor: DeclarationDescriptor ->
                receiverValue == (targetDescriptor as? ReceiverParameterDescriptor)?.value
            }
            is ImplicitClassReceiver -> { targetDescriptor: DeclarationDescriptor ->
                receiverValue.classDescriptor == targetDescriptor.original
            }
            else -> TODO("Unimplemented/unhandled/...unreachable?")
        }

        val parameterPosition =
            fragmentInfo.parameters.indexOfFirst(finderPredicate)
        if (parameterPosition > -1) {
            return super.referenceValueParameter(fragmentInfo.methodDescriptor.valueParameters[parameterPosition])
        }
        return super.referenceValueParameter(descriptor)
    }

    override fun referenceValue(value: ValueDescriptor): IrValueSymbol {
        val parameterPosition =
            fragmentInfo.parameters.map { it }.indexOf(value)
        if (parameterPosition > -1) {
            return super.referenceValueParameter(fragmentInfo.methodDescriptor.valueParameters[parameterPosition])
        }

        return super.referenceValue(value)
    }
}

Review comment (suggestion):
Let's go with "Unimplemented" for the text? I'm not sure that this covers all the cases, so unimplemented seems good for now. We should probably explicitly take care off all possibilities here in a follow-up change.

Code after:
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.util.IdSignatureComposer
import org.jetbrains.kotlin.ir.util.NameProvider
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi2ir.generators.fragments.EvaluatorFragmentInfo
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitClassReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ThisClassReceiver

// Used from CodeFragmentCompiler for IDE Debugger Plug-In
@Suppress("unused")
class FragmentCompilerSymbolTableDecorator(
    signatureComposer: IdSignatureComposer,
    irFactory: IrFactory,
    private val fragmentInfo: EvaluatorFragmentInfo,
    nameProvider: NameProvider = NameProvider.DEFAULT,
) : SymbolTable(signatureComposer, irFactory, nameProvider) {

    override fun referenceValueParameter(descriptor: ParameterDescriptor): IrValueParameterSymbol {
        if (descriptor !is ReceiverParameterDescriptor) return super.referenceValueParameter(descriptor)

        val finderPredicate = when (val receiverValue = descriptor.value) {
            is ExtensionReceiver -> { targetDescriptor: DeclarationDescriptor ->
                receiverValue == (targetDescriptor as? ReceiverParameterDescriptor)?.value
            }
            is ThisClassReceiver -> { targetDescriptor: DeclarationDescriptor ->
                receiverValue.classDescriptor == targetDescriptor.original
            }
            else -> TODO("Unimplemented")
        }

        val parameterPosition =
            fragmentInfo.parameters.indexOfFirst(finderPredicate)
        if (parameterPosition > -1) {
            return super.referenceValueParameter(fragmentInfo.methodDescriptor.valueParameters[parameterPosition])
        }
        return super.referenceValueParameter(descriptor)
    }

    override fun referenceValue(value: ValueDescriptor): IrValueSymbol {
        val parameterPosition =
            fragmentInfo.parameters.indexOf(value)
        if (parameterPosition > -1) {
            return super.referenceValueParameter(fragmentInfo.methodDescriptor.valueParameters[parameterPosition])
        }

        return super.referenceValue(value)
    }
}