org.jetbrains.kotlin.fir.references.FirReferenceUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* 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
}
}