Skip to content

KaTypeBuilder

A base interface for all concrete type builders.

KaTypeBuilder is derived from KaTypeCreator to extend its type building API while keeping all base endpoints accessible.

Inheritors

Properties

token

abstract val token: KaLifetimeToken

The KaLifetimeToken which determines the lifetime of the lifetime owner.

Functions

arrayType

abstract fun arrayType(elementType: KaType, init: KaArrayTypeBuilder.() -> Unit = {}): KaType

Builds an array type from the given elementType. For primitive element types, the KaArrayTypeBuilder.shouldPreferPrimitiveTypes option determines whether the array type will be a primitive array type.

Array types are essentially class types and could be built manually via classType. This builder just provides a more convenient way to construct them.

If the type was constructed successfully, returns KaClassType. Otherwise, returns KaClassErrorType.

assertIsValidAndAccessible

capturedType

abstract fun capturedType(type: KaCapturedType, init: KaCapturedTypeBuilder.() -> Unit = {}): KaCapturedType

Builds a KaCapturedType based on the given type.

abstract fun capturedType(projection: KaTypeProjection, init: KaCapturedTypeBuilder.() -> Unit = {}): KaCapturedType

Builds a KaCapturedType with the given projection.

Note that if projection is KaTypeArgumentWithVariance, its KaTypeArgumentWithVariance.variance must not be Variance.INVARIANT. Captured types are only intended to capture non-invariant projections. Otherwise, an exception is thrown.

classType

abstract fun classType(classId: ClassId, init: KaClassTypeBuilder.() -> Unit = {}): KaType

Builds a class type with the given classId.

If there are no classes available by classId, returns KaClassErrorType.

A generic class type can be built by providing type arguments using the init block. The caller should provide the correct number of type arguments for the class. If the number of provided type arguments is lower, missing types arguments are filled with star projections. If there are more type arguments than required, extra arguments are ignored.

Example

classType(StandardClassIds.List) {
    invariantTypeArgument(builtinTypes.string)
}
abstract fun classType(symbol: KaClassLikeSymbol, init: KaClassTypeBuilder.() -> Unit = {}): KaType

Builds a class type from the given class symbol.

If it's impossible to construct a type from symbol, returns KaClassErrorType.

A generic class type can be built by providing type arguments using the init block. The caller is supposed to provide the correct number of type arguments for the class.

For Kotlin built-in types, consider using the overload that accepts a ClassId instead: classType(StandardClassIds.String).

Example

classType(ktClass.classSymbol as KaClassSymbol) {
    invariantTypeArgument(builtinTypes.string)
}

definitelyNotNullType

Builds a KaDefinitelyNotNullType wrapping the given type.

If type is not nullable, the original type is returned, as wrapping it in KaDefinitelyNotNullType is unnecessary.

dynamicType

abstract fun dynamicType(init: KaDynamicTypeBuilder.() -> Unit = {}): KaDynamicType

Builds a KaDynamicType.

flexibleType

abstract fun flexibleType(type: KaFlexibleType, init: KaFlexibleTypeBuilder.() -> Unit = {}): KaType?

Builds a KaFlexibleType with initial bounds taken from the given type.

If either of the bounds is KaFlexibleType itself, then the corresponding bound of this type is considered instead. I.e., if the upper bound is a KaFlexibleType, then it's upper bound is taken as the resulting upper bound.

If the lower bound is not a subtype of the upper bound, null is returned.

If both bounds are equal, the bound type is returned, as it's unnecessary to create a flexible type in this case.

abstract fun flexibleType(init: KaFlexibleTypeBuilder.() -> Unit = {}): KaType?

Builds a KaFlexibleType.

If either of the bounds is KaFlexibleType itself, then the corresponding bound of this type is considered instead. I.e., if the upper bound is a KaFlexibleType, then it's upper bound is taken as the resulting upper bound.

If the lower bound is not a subtype of the upper bound, null is returned.

If both bounds are equal, the bound type is returned, as it's unnecessary to create a flexible type in this case.

abstract fun flexibleType(lowerBound: KaType, upperBound: KaType): KaType?

Builds a KaFlexibleType with lowerBound and upperBound as bounds.

If either of the bounds is KaFlexibleType itself, then the corresponding bound of this type is considered instead. I.e., if the upper bound is a KaFlexibleType, then it's upper bound is taken as the resulting upper bound.

If the lower bound is not a subtype of the upper bound, null is returned.

If both bounds are equal, the bound type is returned, as it's unnecessary to create a flexible type in this case.

functionType

abstract fun functionType(init: KaFunctionTypeBuilder.() -> Unit = {}): KaFunctionType

Builds a KaFunctionType based on the given init block.

intersectionType

abstract fun intersectionType(init: KaIntersectionTypeBuilder.() -> Unit = {}): KaType

Builds an KaIntersectionType.

The builder returns a normalized version of the intersection, i.e., all duplicated types are removed, nested intersection types are unwrapped, etc.

This normalized version isn't always KaIntersectionType. For example, if there is a KaFlexibleType among conjuncts, the intersector might return another KaFlexibleType with KaIntersectionTypes as bounds. That's due to the distributive property of intersection as a mathematical operation: (A..B) & C = (A & C)..(B & C).

If there are no conjuncts, returns Any? as a neutral element of intersection operation. If a single conjunct is passed, returns that conjunct.

Note that currently it's impossible to provide annotations in KaIntersectionTypeBuilder due to KT-80749.

isValid

Whether the lifetime owner is still valid, i.e. we are still in the scope of the lifetime owner's regular lifetime.

starTypeProjection

typeParameterType

Builds a KaTypeParameterType with the given type parameter symbol.

typeProjection

abstract fun typeProjection(variance: Variance, type: KaType): KaTypeArgumentWithVariance

abstract fun typeProjection(variance: Variance, type: KaTypeCreator.() -> KaType): KaTypeArgumentWithVariance

varargArrayType

abstract fun varargArrayType(elementType: KaType): KaType

Builds the underlying array type of a vararg function parameter with the given elementType.

If the type was constructed successfully, returns KaClassType. Otherwise, returns KaClassErrorType.

withValidityAssertion

inline fun <R> KaLifetimeOwner.withValidityAssertion(action: () -> R): R

Executes action only if the KaLifetimeOwner is still valid and accessible. Otherwise, throws a validity exception based on the concrete violation.