[Analysis API] add KDocs to the substitution-related functionality

This commit is contained in:
Ilya Kirillov
2022-06-09 18:05:58 +02:00
parent 8ba7d41e00
commit f09459d172
10 changed files with 81 additions and 28 deletions
@@ -45,39 +45,39 @@ public interface KtSignatureSubstitutorMixIn : KtAnalysisSessionMixIn {
/**
* Applies a [substitutor] to the given signature and return a new signature with substituted types.
*
* @see KtSubstitutor.substituteOrSelf
* @see KtSubstitutor.substitute
*/
public fun <S : KtCallableSymbol> KtCallableSignature<S>.substitute(substitutor: KtSubstitutor): KtCallableSignature<S> =
withValidityAssertion { analysisSession.signatureSubstitutor.substitute(this, substitutor) }
/**
* Applies a [substitutor] to the given signature and return a new signature with substituted types.
* Applies a [substitutor] to the given signature and return a new signature with substituted types.
*
* @see KtSubstitutor.substituteOrSelf
* @see KtSubstitutor.substitute
*/
public fun <S : KtFunctionLikeSymbol> KtFunctionLikeSignature<S>.substitute(substitutor: KtSubstitutor): KtFunctionLikeSignature<S> =
withValidityAssertion { analysisSession.signatureSubstitutor.substitute(this, substitutor) }
/**
* Applies a [substitutor] to the given signature and return a new signature with substituted types.
* Applies a [substitutor] to the given signature and return a new signature with substituted types.
*
* @see KtSubstitutor.substituteOrSelf
* @see KtSubstitutor.substitute
*/
public fun <S : KtVariableLikeSymbol> KtVariableLikeSignature<S>.substitute(substitutor: KtSubstitutor): KtVariableLikeSignature<S> =
withValidityAssertion { analysisSession.signatureSubstitutor.substitute(this, substitutor) }
/**
* Applies a [substitutor] to the given symbols and return a signature with substituted types.
* Applies a [substitutor] to the given symbol and return a signature with substituted types.
*
* @see KtSubstitutor.substituteOrSelf
* @see KtSubstitutor.substitute
*/
public fun <S : KtCallableSymbol> S.substitute(substitutor: KtSubstitutor): KtCallableSignature<S> =
withValidityAssertion { analysisSession.signatureSubstitutor.substitute(this, substitutor) }
/**
* Applies a [substitutor] to the given symbols and return a signature with substituted types.
* Applies a [substitutor] to the given symbol and return a signature with substituted types.
*
* @see KtSubstitutor.substituteOrSelf
* @see KtSubstitutor.substitute
*/
public fun <S : KtFunctionLikeSymbol> S.substitute(substitutor: KtSubstitutor): KtFunctionLikeSignature<S> =
withValidityAssertion { analysisSession.signatureSubstitutor.substitute(this, substitutor) }
@@ -85,7 +85,7 @@ public interface KtSignatureSubstitutorMixIn : KtAnalysisSessionMixIn {
/**
* Applies a [substitutor] to the given symbols and return a signature with substituted types.
*
* @see KtSubstitutor.substituteOrSelf
* @see KtSubstitutor.substitute
*/
public fun <S : KtVariableLikeSymbol> S.substitute(substitutor: KtSubstitutor): KtVariableLikeSignature<S> =
withValidityAssertion { analysisSession.signatureSubstitutor.substitute(this, substitutor) }
@@ -22,6 +22,9 @@ public abstract class KtSubstitutorFactory : KtAnalysisSessionComponent() {
public abstract fun buildSubstitutor(builder: KtSubstitutorBuilder): KtSubstitutor
}
/**
* Creates new [KtSubstitutor] using substitutions specified inside [build] lambda
*/
@OptIn(ExperimentalContracts::class, KtAnalysisApiInternals::class)
public inline fun KtAnalysisSession.buildSubstitutor(
build: KtSubstitutorBuilder.() -> Unit
@@ -39,11 +42,20 @@ public class KtSubstitutorBuilder
public val mappings: Map<KtTypeParameterSymbol, KtType> get() = withValidityAssertion { _mapping }
/**
* Adds a new [typeParameter] -> [type] substitution to the substitutor which is being built.
* If there already was a substitution with a [typeParameter], replaces corresponding substitution with a new one.
*/
public fun substitution(typeParameter: KtTypeParameterSymbol, type: KtType) {
assertIsValidAndAccessible()
_mapping[typeParameter] = type
}
/**
* Adds a new substitutions to the substitutor which is being built.
* If there already was a substitution with a [KtTypeParameterSymbol] which is present in a [substitutions],
* replaces corresponding substitution with a new one.
*/
public fun substitutions(substitutions: Map<KtTypeParameterSymbol, KtType>) {
assertIsValidAndAccessible()
_mapping += substitutions
@@ -38,6 +38,9 @@ public sealed class KtCallableSignature<out S : KtCallableSymbol> : KtLifetimeOw
*/
public abstract val receiverType: KtType?
/**
* A [CallableId] of a substituted symbol
*/
public open val callableIdIfNonLocal: CallableId? get() = withValidityAssertion { symbol.callableIdIfNonLocal }
abstract override fun equals(other: Any?): Boolean
@@ -13,7 +13,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtValueParameterSymbol
import org.jetbrains.kotlin.analysis.api.types.KtType
/**
* A signature of a function-like symbol.
* A signature of a function-like symbol. This includes functions, getters, setters, lambdas, etc.
*/
public class KtFunctionLikeSignature<out S : KtFunctionLikeSymbol>(
private val _symbol: S,
@@ -19,7 +19,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.runIf
/**
* A signature of a variable-like symbol.
* A signature of a variable-like symbol. This includes properties, enum entries local variables, etc.
*/
public class KtVariableLikeSignature<out S : KtVariableLikeSymbol>(
private val _symbol: S,
@@ -9,12 +9,39 @@ import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeOwner
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
/**
* Type substitutor. Performs substitution of a type parameters inside a type to some fixed type.
* Usually can be represented as a map from type parameter to it corresponding substitution.
*
* Example of a substitution:
* ```
* substitutor = { T -> Int, S -> Long }
* substitute(Map<T, S>, substitutor) = Map<Int, Long>
* ```
*
* Can be built by [org.jetbrains.kotlin.analysis.api.components.buildSubstitutor] or retrieved via a [org.jetbrains.kotlin.analysis.api.calls.KtCall]
*/
public interface KtSubstitutor : KtLifetimeOwner {
public fun substituteOrSelf(type: KtType): KtType = withValidityAssertion { substituteOrNull(type) ?: type }
/**
* substitutes type parameters in a given type corresponding to internal mapping rules.
*
* @return substituted type if there was at least one substitution, [type] itself if there was no type parameter to substitute
*/
public fun substitute(type: KtType): KtType = withValidityAssertion { substituteOrNull(type) ?: type }
/**
* substitutes type parameters in a given type corresponding to internal mapping rules.
*
* @return substituted type if there was at least one substitution, `null` if there was no type parameter to substitute
*/
public fun substituteOrNull(type: KtType): KtType?
/**
* [KtSubstitutor] which does nothing on a type and always returns the type intact
*/
public class Empty(override val token: KtLifetimeToken) : KtSubstitutor {
override fun substituteOrNull(type: KtType): KtType = withValidityAssertion { type }
override fun substituteOrNull(type: KtType): KtType? = withValidityAssertion { null }
override fun substituteOrSelf(type: KtType): KtType = withValidityAssertion { type }
}
}