-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use FIR to dump ZiplineService declarations (#1049)
* Use FIR to dump ZiplineService declarations I considered using KSP but it's awkard to generate artifacts from KSP during compile that the compile task doesn't itself need. Working towards #1047 * Spotless * Rename things to FIR
- Loading branch information
1 parent
b3f244d
commit fd33c34
Showing
12 changed files
with
568 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
zipline-kotlin-plugin/src/main/kotlin/app/cash/zipline/api/fir/Fir.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2023 Cash App | ||
* | ||
* 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 | ||
* | ||
* http://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 app.cash.zipline.api.fir | ||
|
||
import org.jetbrains.kotlin.fir.FirSession | ||
import org.jetbrains.kotlin.fir.declarations.FirClass | ||
import org.jetbrains.kotlin.fir.declarations.FirRegularClass | ||
import org.jetbrains.kotlin.fir.resolve.firClassLike | ||
|
||
/** Recursively collect all supertypes of this class. */ | ||
internal fun FirClass.getAllSupertypes(session: FirSession): Set<FirClass> { | ||
val result = mutableSetOf<FirClass>() | ||
collectSupertypes(session, this, result) | ||
return result | ||
} | ||
|
||
private fun collectSupertypes( | ||
session: FirSession, | ||
type: FirClass, | ||
sink: MutableSet<FirClass>, | ||
) { | ||
if (!sink.add(type)) return // Already added. | ||
if (type !is FirRegularClass) return // Cannot traverse supertypes. | ||
|
||
val supertypes = type.symbol.resolvedSuperTypeRefs.mapNotNull { | ||
it.firClassLike(session) as? FirClass | ||
} | ||
|
||
for (supertype in supertypes) { | ||
collectSupertypes(session, supertype, sink) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
zipline-kotlin-plugin/src/main/kotlin/app/cash/zipline/api/fir/FirZiplineApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (C) 2023 Cash App | ||
* | ||
* 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 | ||
* | ||
* http://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 app.cash.zipline.api.fir | ||
|
||
data class FirZiplineApi( | ||
val services: List<FirZiplineService>, | ||
) |
184 changes: 184 additions & 0 deletions
184
zipline-kotlin-plugin/src/main/kotlin/app/cash/zipline/api/fir/FirZiplineApiReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* | ||
* Copyright (C) 2023 Cash App | ||
* | ||
* 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 | ||
* | ||
* http://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 app.cash.zipline.api.fir | ||
|
||
import app.cash.zipline.kotlin.BridgedInterface.Companion.NON_INTERFACE_FUNCTION_NAMES | ||
import app.cash.zipline.kotlin.FqPackageName | ||
import app.cash.zipline.kotlin.classId | ||
import java.io.File | ||
import org.jetbrains.kotlin.fir.FirSession | ||
import org.jetbrains.kotlin.fir.analysis.checkers.isSupertypeOf | ||
import org.jetbrains.kotlin.fir.analysis.checkers.toRegularClassSymbol | ||
import org.jetbrains.kotlin.fir.declarations.FirDeclaration | ||
import org.jetbrains.kotlin.fir.declarations.FirFunction | ||
import org.jetbrains.kotlin.fir.declarations.FirProperty | ||
import org.jetbrains.kotlin.fir.declarations.FirRegularClass | ||
import org.jetbrains.kotlin.fir.declarations.utils.isInterface | ||
import org.jetbrains.kotlin.fir.declarations.utils.isSuspend | ||
import org.jetbrains.kotlin.fir.pipeline.FirResult | ||
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider | ||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol | ||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol | ||
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef | ||
import org.jetbrains.kotlin.fir.types.FirStarProjection | ||
import org.jetbrains.kotlin.fir.types.FirTypeProjection | ||
import org.jetbrains.kotlin.fir.types.FirTypeProjectionWithVariance | ||
import org.jetbrains.kotlin.fir.types.FirTypeRef | ||
import org.jetbrains.kotlin.fir.types.FirUserTypeRef | ||
import org.jetbrains.kotlin.types.Variance | ||
|
||
fun readFirZiplineApi( | ||
sources: Collection<File>, | ||
dependencies: Collection<File>, | ||
): FirZiplineApi { | ||
return KotlinFirLoader(sources, dependencies).use { loader -> | ||
val output = loader.load("zipline-api-dump") | ||
FirZiplineApiReader(output).read() | ||
} | ||
} | ||
|
||
private val ziplineFqPackage = FqPackageName("app.cash.zipline") | ||
private val ziplineServiceClassId = ziplineFqPackage.classId("ZiplineService") | ||
|
||
/** | ||
* Read the frontend intermediate representation of a program and emit its ZiplineService | ||
* interfaces. These are subject to strict API compatibility requirements. | ||
*/ | ||
internal class FirZiplineApiReader( | ||
output: FirResult, | ||
) { | ||
private val platformOutput = output.platformOutput | ||
private val session: FirSession = platformOutput.session | ||
|
||
private val ziplineServiceClass: FirClassLikeSymbol<*>? = | ||
session.symbolProvider.getClassLikeSymbolByClassId(ziplineServiceClassId) | ||
|
||
fun read(): FirZiplineApi { | ||
val types = platformOutput.fir | ||
.flatMap { it.declarations.findRegularClassesRecursive() } | ||
.filter { it.isInterface && it.isZiplineService } | ||
|
||
val services = types | ||
.map { it.asDeclaredZiplineService() } | ||
.sortedBy { it.name } | ||
|
||
return FirZiplineApi(services) | ||
} | ||
|
||
private val FirRegularClass.isZiplineService: Boolean | ||
get() { | ||
val ziplineServiceClssSymbol = ziplineServiceClass as? FirClassSymbol<*> ?: return false | ||
return ziplineServiceClssSymbol.isSupertypeOf(symbol, session) | ||
} | ||
|
||
private fun FirRegularClass.asDeclaredZiplineService(): FirZiplineService { | ||
return FirZiplineService( | ||
symbol.classId.asSingleFqName().asString(), | ||
bridgedFunctions(this), | ||
) | ||
} | ||
|
||
private fun bridgedFunctions(type: FirRegularClass): List<FirZiplineFunction> { | ||
val result = sortedSetOf<FirZiplineFunction>( | ||
{ a, b -> a.signature.compareTo(b.signature) }, | ||
) | ||
|
||
for (supertype in type.getAllSupertypes(session)) { | ||
if (!supertype.isInterface) continue // Skip kotlin.Any. | ||
|
||
for (declaration in supertype.declarations) { | ||
when (declaration) { | ||
is FirFunction -> { | ||
if (declaration.isNonInterfaceFunction) continue | ||
result += declaration.asDeclaredZiplineFunction() | ||
} | ||
|
||
is FirProperty -> { | ||
result += declaration.asDeclaredZiplineFunction() | ||
} | ||
|
||
else -> Unit | ||
} | ||
} | ||
} | ||
|
||
return result.toList() | ||
} | ||
|
||
private val FirFunction.isNonInterfaceFunction: Boolean | ||
get() = symbol.name.identifier in NON_INTERFACE_FUNCTION_NAMES | ||
|
||
private fun FirFunction.asDeclaredZiplineFunction(): FirZiplineFunction { | ||
val signature = buildString { | ||
if (isSuspend) append("suspend ") | ||
append("fun ${symbol.name.identifier}(") | ||
append(valueParameters.joinToString { it.returnTypeRef.asString() }) | ||
append("): ${returnTypeRef.asString()}") | ||
} | ||
|
||
return FirZiplineFunction(signature) | ||
} | ||
|
||
private fun FirProperty.asDeclaredZiplineFunction(): FirZiplineFunction { | ||
val signature = when { | ||
isVar -> "var ${symbol.name.identifier}: ${returnTypeRef.asString()}" | ||
else -> "val ${symbol.name.identifier}: ${returnTypeRef.asString()}" | ||
} | ||
return FirZiplineFunction(signature) | ||
} | ||
|
||
/** See [app.cash.zipline.kotlin.asString]. */ | ||
private fun FirTypeRef.asString(): String { | ||
val classSymbol = toRegularClassSymbol(session) ?: error("unexpected class: $this") | ||
|
||
val typeRef = when (this) { | ||
is FirResolvedTypeRef -> delegatedTypeRef ?: this | ||
else -> this | ||
} | ||
|
||
return buildString { | ||
append(classSymbol.classId.asSingleFqName().asString()) | ||
|
||
if (typeRef is FirUserTypeRef) { | ||
val typeArguments = typeRef.qualifier.lastOrNull()?.typeArgumentList?.typeArguments | ||
if (typeArguments?.isEmpty() == false) { | ||
typeArguments.joinTo(this, separator = ",", prefix = "<", postfix = ">") { | ||
it.asString() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun FirTypeProjection.asString(): String { | ||
return when (this) { | ||
is FirStarProjection -> { | ||
"*" | ||
} | ||
is FirTypeProjectionWithVariance -> { | ||
variance.label + (if (variance != Variance.INVARIANT) " " else "") + typeRef.asString() | ||
} | ||
else -> { | ||
error("Unexpected kind of FirTypeProjection: " + javaClass.simpleName) | ||
} | ||
} | ||
} | ||
|
||
private fun List<FirDeclaration>.findRegularClassesRecursive(): List<FirRegularClass> { | ||
val classes = filterIsInstance<FirRegularClass>() | ||
return classes + classes.flatMap { it.declarations.findRegularClassesRecursive() } | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
zipline-kotlin-plugin/src/main/kotlin/app/cash/zipline/api/fir/FirZiplineFunction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (C) 2023 Cash App | ||
* | ||
* 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 | ||
* | ||
* http://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 app.cash.zipline.api.fir | ||
|
||
import app.cash.zipline.kotlin.signatureHash | ||
|
||
data class FirZiplineFunction( | ||
val id: String, | ||
val signature: String, | ||
) { | ||
internal constructor(signature: String) : this(signature.signatureHash(), signature) | ||
} |
22 changes: 22 additions & 0 deletions
22
zipline-kotlin-plugin/src/main/kotlin/app/cash/zipline/api/fir/FirZiplineService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (C) 2023 Cash App | ||
* | ||
* 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 | ||
* | ||
* http://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 app.cash.zipline.api.fir | ||
|
||
/** An interface that extends from ZiplineService. */ | ||
data class FirZiplineService( | ||
val name: String, | ||
val functions: List<FirZiplineFunction>, | ||
) |
Oops, something went wrong.