1919
2020package deezer.kustomexport.compiler.js.pattern
2121
22- import com.google.devtools.ksp.getAllSuperTypes
2322import com.google.devtools.ksp.getConstructors
2423import com.google.devtools.ksp.getDeclaredFunctions
2524import com.google.devtools.ksp.getDeclaredProperties
2625import com.google.devtools.ksp.isPrivate
2726import com.google.devtools.ksp.isPublic
2827import com.google.devtools.ksp.symbol.ClassKind
2928import com.google.devtools.ksp.symbol.KSClassDeclaration
29+ import com.google.devtools.ksp.symbol.KSFunctionDeclaration
30+ import com.google.devtools.ksp.symbol.KSName
31+ import com.google.devtools.ksp.symbol.KSTypeParameter
3032import com.google.devtools.ksp.symbol.Modifier
3133import com.squareup.kotlinpoet.TypeName
3234import com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview
@@ -45,49 +47,37 @@ import deezer.kustomexport.compiler.js.PropertyDescriptor
4547import deezer.kustomexport.compiler.js.SealedClassDescriptor
4648import deezer.kustomexport.compiler.js.SealedSubClassDescriptor
4749import deezer.kustomexport.compiler.js.SuperDescriptor
50+ import deezer.kustomexport.compiler.js.TopLevelFunctionDescriptor
4851import deezer.kustomexport.compiler.js.TypeParameterDescriptor
4952import deezer.kustomexport.compiler.js.ValueClassDescriptor
5053import deezer.kustomexport.compiler.js.mapping.OriginTypeName
5154
55+ fun parseFunction (
56+ function : KSFunctionDeclaration ,
57+ forcedConcreteTypeParameters : List <Pair <String , TypeName >>? = null,
58+ ): TopLevelFunctionDescriptor = TopLevelFunctionDescriptor (
59+ packageName = function.packageName.asString(),
60+ function = function.toDescriptor(
61+ sequenceOf(),
62+ function.typeParameters.toTypeParameterResolver(),
63+ buildConcreteTypeParameters(forcedConcreteTypeParameters) { function.typeParameters[0 ] }
64+ )
65+ )
66+
5267@KotlinPoetKspPreview
5368fun parseClass (
5469 classDeclaration : KSClassDeclaration ,
5570 forcedConcreteTypeParameters : List <Pair <String , TypeName >>? = null,
5671 exportedClassSimpleName : String
57- ): Descriptor ? {
72+ ): Descriptor {
5873 val typeParamResolver = classDeclaration.typeParameters.toTypeParameterResolver()
5974
60- val concreteTypeParameters: MutableList <TypeParameterDescriptor > = mutableListOf ()
61- (forcedConcreteTypeParameters ? : emptyList())/* ?: typeParamResolver.parametersMap.values*/
62- .forEach { (name, type) ->
63- val className = try {
64- type.asClassName()
65- } catch (t: Throwable ) {
66- Logger .error(
67- " Cannot use @KustomException on a not concrete generics class." ,
68- classDeclaration.typeParameters[0 ]
69- )
70- return null
71- }
72- concreteTypeParameters.add(
73- TypeParameterDescriptor (
74- name = name,
75- origin = className.cached(concreteTypeParameters),
76- )
77- )
78- }
75+ val concreteTypeParameters: MutableList <TypeParameterDescriptor > =
76+ buildConcreteTypeParameters(forcedConcreteTypeParameters) { classDeclaration.typeParameters[0 ] }
7977
8078 val packageName = classDeclaration.packageName.asString()
8179 val classSimpleName = classDeclaration.simpleName.asString()
8280
83- // val superTypes = classDeclaration.getAllSuperTypes()
84-
85- Logger .warn(
86- " PARSING - ${classSimpleName} ${classDeclaration.superTypes.count()} ${
87- classDeclaration.getAllSuperTypes().count()
88- } "
89- )
90-
9181 val superTypes = classDeclaration.superTypes
9282 .map { superType ->
9383 val superTypeName = superType.toTypeNamePatch(typeParamResolver).cached(concreteTypeParameters)
@@ -187,6 +177,32 @@ fun parseClass(
187177 }
188178}
189179
180+ private fun buildConcreteTypeParameters (
181+ forcedConcreteTypeParameters : List <Pair <String , TypeName >>? ,
182+ firstTypeParameterProvider : () -> KSTypeParameter
183+ ): MutableList <TypeParameterDescriptor > {
184+ val concreteTypeParameters: MutableList <TypeParameterDescriptor > = mutableListOf ()
185+ (forcedConcreteTypeParameters ? : emptyList())/* ?: typeParamResolver.parametersMap.values*/
186+ .forEach { (name, type) ->
187+ val className = try {
188+ type.asClassName()
189+ } catch (t: Throwable ) {
190+ Logger .error(
191+ " Cannot use @KustomException on a not concrete generics class." ,
192+ firstTypeParameterProvider()
193+ )
194+ error(t)
195+ }
196+ concreteTypeParameters.add(
197+ TypeParameterDescriptor (
198+ name = name,
199+ origin = className.cached(concreteTypeParameters),
200+ )
201+ )
202+ }
203+ return concreteTypeParameters
204+ }
205+
190206private val nonExportableFunctions = listOf (
191207 " <init>" ,
192208 " equals" ,
@@ -216,21 +232,28 @@ fun KSClassDeclaration.parseFunctions(
216232 .filter { it.simpleName.asString() !in nonExportableFunctions }
217233 .filter { it.isPublic() }
218234 .map { func ->
219- FunctionDescriptor (
220- name = func.simpleName.asString(),
221- isOverride = func.findOverridee() != null || ! declaredNames.contains(func.simpleName),
222- isSuspend = func.modifiers.contains(Modifier .SUSPEND ),
223- returnType = func.returnType!! .toTypeNamePatch(typeParamResolver).cached(concreteTypeParameters),
224- parameters = func.parameters.map { p ->
225- ParameterDescriptor (
226- name = p.name?.asString() ? : TODO (" not sure what we want here" ),
227- type = p.type.toTypeNamePatch(typeParamResolver).cached(concreteTypeParameters),
228- )
229- }
230- )
235+ func.toDescriptor(declaredNames, typeParamResolver, concreteTypeParameters)
231236 }.toList()
232237}
233238
239+ private fun KSFunctionDeclaration.toDescriptor (
240+ declaredNames : Sequence <KSName >,
241+ typeParamResolver : TypeParameterResolver ,
242+ concreteTypeParameters : MutableList <TypeParameterDescriptor >
243+ ) =
244+ FunctionDescriptor (
245+ name = simpleName.asString(),
246+ isOverride = findOverridee() != null || ! declaredNames.contains(simpleName),
247+ isSuspend = modifiers.contains(Modifier .SUSPEND ),
248+ returnType = returnType!! .toTypeNamePatch(typeParamResolver).cached(concreteTypeParameters),
249+ parameters = parameters.map { p ->
250+ ParameterDescriptor (
251+ name = p.name?.asString() ? : TODO (" not sure what we want here" ),
252+ type = p.type.toTypeNamePatch(typeParamResolver).cached(concreteTypeParameters),
253+ )
254+ }
255+ )
256+
234257@OptIn(KotlinPoetKspPreview ::class )
235258fun KSClassDeclaration.parseProperties (
236259 typeParamResolver : TypeParameterResolver ,
0 commit comments