1 /* 2 * Copyright 2020 Google LLC 3 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package com.google.devtools.ksp.symbol 18 19 /** 20 * Represents a type in Kotlin's type system. 21 * 22 * Generally, a type is comprised of a declaration (e.g., class), corresponding type arguments, and other details like nullability. 23 * KSType is useful when doing type checking, finding the declaration, and so on. Some of the information, 24 * such as type annotations and type arguments, are often available in the corresponding type reference without resolution. 25 */ 26 interface KSType { 27 /** 28 * The declaration that generates this type. 29 */ 30 val declaration: KSDeclaration 31 32 /** 33 * A type can be nullable, not nullable, or context-specific in the case of platform types. 34 */ 35 val nullability: Nullability 36 37 /** 38 * Type arguments to the type. 39 */ 40 val arguments: List<KSTypeArgument> 41 42 /** 43 * Type annotations to the type. 44 */ 45 val annotations: Sequence<KSAnnotation> 46 47 /** 48 * Check whether this type is assign-compatible from another type. 49 * 50 * @param: that the other type being checked. 51 */ isAssignableFromnull52 fun isAssignableFrom(that: KSType): Boolean 53 54 /** 55 * True if the type is a collection and can be both mutable and immutable, depending on the context. 56 */ 57 fun isMutabilityFlexible(): Boolean 58 59 /** 60 * True if the type can be both invariant and covariant, depending on the context. 61 */ 62 fun isCovarianceFlexible(): Boolean 63 64 /** 65 * Replace the type arguments 66 * 67 * @param arguemnts New type arguments 68 * @return A type with the arguments replaced. 69 */ 70 fun replace(arguments: List<KSTypeArgument>): KSType 71 72 /** 73 * Returns the star projection of the type. 74 */ 75 fun starProjection(): KSType 76 77 /** 78 * Make the type nullable 79 */ 80 fun makeNullable(): KSType 81 82 /** 83 * Make the type not nullable 84 */ 85 fun makeNotNullable(): KSType 86 87 /** 88 * True if the type is explicitly marked as nullable type, i.e. has question mark in type declaration. 89 */ 90 val isMarkedNullable: Boolean 91 92 /** 93 * True if the type is an error type, which means the type can't be resolved by compiler. 94 */ 95 val isError: Boolean 96 97 /** 98 * True if the type is a function type. Note that a suspend function will return false here. 99 */ 100 val isFunctionType: Boolean 101 102 /** 103 * True if the type is a suspend function 104 */ 105 val isSuspendFunctionType: Boolean 106 } 107