/
ksTypes.kt
190 lines (176 loc) · 7.02 KB
/
ksTypes.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (C) 2021 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.kotlinpoet.ksp
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeAlias
import com.google.devtools.ksp.symbol.KSTypeArgument
import com.google.devtools.ksp.symbol.KSTypeParameter
import com.google.devtools.ksp.symbol.KSTypeReference
import com.google.devtools.ksp.symbol.Variance
import com.google.devtools.ksp.symbol.Variance.CONTRAVARIANT
import com.google.devtools.ksp.symbol.Variance.COVARIANT
import com.google.devtools.ksp.symbol.Variance.INVARIANT
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.STAR
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.WildcardTypeName
import com.squareup.kotlinpoet.tags.TypeAliasTag
/** Returns the [ClassName] representation of this [KSType] IFF it's a [KSClassDeclaration]. */
@KotlinPoetKspPreview
public fun KSType.toClassName(): ClassName {
val decl = declaration
check(decl is KSClassDeclaration) {
"Declaration was not a KSClassDeclaration: $this"
}
return decl.toClassName()
}
/**
* Returns the [TypeName] representation of this [KSType].
*
* @see toTypeParameterResolver
* @param typeParamResolver an optional resolver for enclosing declarations' type parameters. Parent
* declarations can be anything with generics that child nodes declare as
* defined by [KSType.arguments].
*/
@KotlinPoetKspPreview
public fun KSType.toTypeName(
typeParamResolver: TypeParameterResolver = TypeParameterResolver.EMPTY
): TypeName = toTypeName(typeParamResolver, emptyList())
@KotlinPoetKspPreview
internal fun KSType.toTypeName(
typeParamResolver: TypeParameterResolver,
typeArguments: List<KSTypeArgument>,
): TypeName {
require(!isError) {
"Error type '$this' is not resolvable in the current round of processing."
}
val type = when (val decl = declaration) {
is KSClassDeclaration -> {
decl.toClassName().withTypeArguments(arguments.map { it.toTypeName(typeParamResolver) })
}
is KSTypeParameter -> typeParamResolver[decl.name.getShortName()]
is KSTypeAlias -> {
var typeAlias: KSTypeAlias = decl
var arguments = arguments
var resolvedType: KSType
var mappedArgs: List<KSTypeArgument>
var extraResolver: TypeParameterResolver
while (true) {
resolvedType = typeAlias.type.resolve()
mappedArgs = mapTypeArgumentsFromTypeAliasToAbbreviatedType(
typeAlias = typeAlias,
typeAliasTypeArguments = arguments,
abbreviatedType = resolvedType,
)
extraResolver = if (typeAlias.typeParameters.isEmpty()) {
typeParamResolver
} else {
typeAlias.typeParameters.toTypeParameterResolver(typeParamResolver)
}
typeAlias = resolvedType.declaration as? KSTypeAlias ?: break
arguments = mappedArgs
}
val abbreviatedType = resolvedType
.toTypeName(extraResolver)
.copy(nullable = isMarkedNullable)
.rawType()
.withTypeArguments(mappedArgs.map { it.toTypeName(typeParamResolver) })
val aliasArgs = typeArguments.map { it.toTypeName(typeParamResolver) }
decl.toClassNameInternal()
.withTypeArguments(aliasArgs)
.copy(tags = mapOf(TypeAliasTag::class to TypeAliasTag(abbreviatedType)))
}
else -> error("Unsupported type: $declaration")
}
return type.copy(nullable = isMarkedNullable)
}
@KotlinPoetKspPreview
private fun mapTypeArgumentsFromTypeAliasToAbbreviatedType(
typeAlias: KSTypeAlias,
typeAliasTypeArguments: List<KSTypeArgument>,
abbreviatedType: KSType,
): List<KSTypeArgument> {
return abbreviatedType.arguments
.map { typeArgument ->
// Check if type argument is a reference to a typealias type parameter, and not an actual type.
val typeAliasTypeParameterIndex = typeAlias.typeParameters.indexOfFirst { typeAliasTypeParameter ->
typeAliasTypeParameter.name.asString() == typeArgument.type.toString()
}
if (typeAliasTypeParameterIndex >= 0) {
typeAliasTypeArguments[typeAliasTypeParameterIndex]
} else {
typeArgument
}
}
}
/**
* Returns a [TypeVariableName] representation of this [KSTypeParameter].
*
* @see toTypeParameterResolver
* @param typeParamResolver an optional resolver for enclosing declarations' type parameters. Parent
* declarations can be anything with generics that child nodes declare as
* defined by [KSType.arguments].
*/
@KotlinPoetKspPreview
public fun KSTypeParameter.toTypeVariableName(
typeParamResolver: TypeParameterResolver = TypeParameterResolver.EMPTY
): TypeVariableName {
val typeVarName = name.getShortName()
val typeVarBounds = bounds.map { it.toTypeName(typeParamResolver) }.toList()
val typeVarVariance = when (variance) {
COVARIANT -> KModifier.OUT
CONTRAVARIANT -> KModifier.IN
else -> null
}
return TypeVariableName(typeVarName, bounds = typeVarBounds, variance = typeVarVariance)
}
/**
* Returns a [TypeName] representation of this [KSTypeArgument].
*
* @see toTypeParameterResolver
* @param typeParamResolver an optional resolver for enclosing declarations' type parameters. Parent
* declarations can be anything with generics that child nodes declare as
* defined by [KSType.arguments].
*/
@KotlinPoetKspPreview
public fun KSTypeArgument.toTypeName(
typeParamResolver: TypeParameterResolver = TypeParameterResolver.EMPTY
): TypeName {
val typeName = type?.toTypeName(typeParamResolver) ?: return STAR
return when (variance) {
COVARIANT -> WildcardTypeName.producerOf(typeName)
CONTRAVARIANT -> WildcardTypeName.consumerOf(typeName)
Variance.STAR -> STAR
INVARIANT -> typeName
}
}
/**
* Returns a [TypeName] representation of this [KSTypeReference].
*
* @see toTypeParameterResolver
* @param typeParamResolver an optional resolver for enclosing declarations' type parameters. Parent
* declarations can be anything with generics that child nodes declare as
* defined by [KSType.arguments].
*/
@KotlinPoetKspPreview
public fun KSTypeReference.toTypeName(
typeParamResolver: TypeParameterResolver = TypeParameterResolver.EMPTY
): TypeName {
return resolve().toTypeName(typeParamResolver, element?.typeArguments.orEmpty())
}