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

org.jetbrains.kotlin.fir.references.FirReferenceUtils.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2022 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.fir.references

import org.jetbrains.kotlin.fir.diagnostics.FirDiagnosticHolder
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.*
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

val FirReference.symbol: FirBasedSymbol<*>?
    get() = when (this) {
        is FirThisReference -> boundSymbol
        is FirResolvedNamedReference -> resolvedSymbol
        is FirNamedReferenceWithCandidateBase -> candidateSymbol
        else -> null
    }

val FirReference.resolved: FirResolvedNamedReference? get() = this as? FirResolvedNamedReference

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE",)
inline fun > FirReference.toResolvedSymbol(
    discardErrorReference: Boolean = false
): @kotlin.internal.NoInfer T? {
    if (discardErrorReference && this is FirResolvedErrorReference) {
        return null
    }
    return resolved?.resolvedSymbol as? T
}

fun FirReference.toResolvedBaseSymbol(discardErrorReference: Boolean = false): FirBasedSymbol<*>? {
    return this.toResolvedSymbol>(discardErrorReference)
}

fun FirReference.toResolvedCallableSymbol(discardErrorReference: Boolean = false): FirCallableSymbol<*>? {
    return this.toResolvedSymbol>(discardErrorReference)
}

fun FirReference.toResolvedTypeParameterSymbol(discardErrorReference: Boolean = false): FirTypeParameterSymbol? {
    return this.toResolvedSymbol(discardErrorReference)
}

fun FirReference.toResolvedVariableSymbol(discardErrorReference: Boolean = false): FirVariableSymbol<*>? {
    return this.toResolvedSymbol>(discardErrorReference)
}

fun FirReference.toResolvedPropertySymbol(discardErrorReference: Boolean = false): FirPropertySymbol? {
    return this.toResolvedSymbol(discardErrorReference)
}

fun FirReference.toResolvedValueParameterSymbol(discardErrorReference: Boolean = false): FirValueParameterSymbol? {
    return this.toResolvedSymbol(discardErrorReference)
}

fun FirReference.toResolvedFunctionSymbol(discardErrorReference: Boolean = false): FirFunctionSymbol<*>? {
    return this.toResolvedSymbol>(discardErrorReference)
}

fun FirReference.toResolvedNamedFunctionSymbol(discardErrorReference: Boolean = false): FirNamedFunctionSymbol? {
    return this.toResolvedSymbol(discardErrorReference)
}

fun FirReference.toResolvedConstructorSymbol(discardErrorReference: Boolean = false): FirConstructorSymbol? {
    return this.toResolvedSymbol(discardErrorReference)
}

fun FirReference.toResolvedEnumEntrySymbol(discardErrorReference: Boolean = false): FirEnumEntrySymbol? {
    return this.toResolvedSymbol(discardErrorReference)
}

@OptIn(ExperimentalContracts::class)
fun FirReference.isError(): Boolean {
    contract {
        returns(true) implies (this@isError is FirDiagnosticHolder)
    }
    return when (this) {
        is FirResolvedErrorReference, is FirErrorNamedReference -> true
        else -> false
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy