Skip to content

Commit

Permalink
Rename things to FIR
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Jun 20, 2023
1 parent 5eaf1df commit 45f2171
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.zipline.apicheck
package app.cash.zipline.api.fir

import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirClass
Expand Down
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>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.zipline.apicheck
package app.cash.zipline.api.fir

import app.cash.zipline.kotlin.BridgedInterface.Companion.NON_INTERFACE_FUNCTION_NAMES
import app.cash.zipline.kotlin.FqPackageName
Expand All @@ -40,13 +40,13 @@ import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.FirUserTypeRef
import org.jetbrains.kotlin.types.Variance

fun readZiplineServices(
fun readFirZiplineApi(
sources: Collection<File>,
dependencies: Collection<File>,
): List<DeclaredZiplineService> {
): FirZiplineApi {
return KotlinFirLoader(sources, dependencies).use { loader ->
val output = loader.load("zipline-api-dump")
ZiplineServicesReader(output).read()
FirZiplineApiReader(output).read()
}
}

Expand All @@ -57,7 +57,7 @@ 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 ZiplineServicesReader(
internal class FirZiplineApiReader(
output: FirResult,
) {
private val platformOutput = output.platformOutput
Expand All @@ -66,14 +66,16 @@ internal class ZiplineServicesReader(
private val ziplineServiceClass: FirClassLikeSymbol<*>? =
session.symbolProvider.getClassLikeSymbolByClassId(ziplineServiceClassId)

fun read(): List<DeclaredZiplineService> {
fun read(): FirZiplineApi {
val types = platformOutput.fir
.flatMap { it.declarations.findRegularClassesRecursive() }
.filter { it.isInterface && it.isZiplineService }

return types
val services = types
.map { it.asDeclaredZiplineService() }
.sortedBy { it.name }

return FirZiplineApi(services)
}

private val FirRegularClass.isZiplineService: Boolean
Expand All @@ -82,15 +84,15 @@ internal class ZiplineServicesReader(
return ziplineServiceClssSymbol.isSupertypeOf(symbol, session)
}

private fun FirRegularClass.asDeclaredZiplineService(): DeclaredZiplineService {
return DeclaredZiplineService(
private fun FirRegularClass.asDeclaredZiplineService(): FirZiplineService {
return FirZiplineService(
symbol.classId.asSingleFqName().asString(),
bridgedFunctions(this),
)
}

private fun bridgedFunctions(type: FirRegularClass): List<DeclaredZiplineFunction> {
val result = sortedSetOf<DeclaredZiplineFunction>(
private fun bridgedFunctions(type: FirRegularClass): List<FirZiplineFunction> {
val result = sortedSetOf<FirZiplineFunction>(
{ a, b -> a.signature.compareTo(b.signature) },
)

Expand Down Expand Up @@ -119,23 +121,23 @@ internal class ZiplineServicesReader(
private val FirFunction.isNonInterfaceFunction: Boolean
get() = symbol.name.identifier in NON_INTERFACE_FUNCTION_NAMES

private fun FirFunction.asDeclaredZiplineFunction(): DeclaredZiplineFunction {
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 DeclaredZiplineFunction(signature)
return FirZiplineFunction(signature)
}

private fun FirProperty.asDeclaredZiplineFunction(): DeclaredZiplineFunction {
private fun FirProperty.asDeclaredZiplineFunction(): FirZiplineFunction {
val signature = when {
isVar -> "var ${symbol.name.identifier}: ${returnTypeRef.asString()}"
else -> "val ${symbol.name.identifier}: ${returnTypeRef.asString()}"
}
return DeclaredZiplineFunction(signature)
return FirZiplineFunction(signature)
}

/** See [app.cash.zipline.kotlin.asString]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.zipline.apicheck
package app.cash.zipline.api.fir

import app.cash.zipline.kotlin.signatureHash

data class DeclaredZiplineFunction(
data class FirZiplineFunction(
val id: String,
val signature: String,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.zipline.apicheck
package app.cash.zipline.api.fir

/** An interface that extends from ZiplineService. */
data class DeclaredZiplineService(
data class FirZiplineService(
val name: String,
val functions: List<DeclaredZiplineFunction>,
val functions: List<FirZiplineFunction>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.zipline.apicheck
package app.cash.zipline.api.fir

import java.io.File
import org.jetbrains.kotlin.KtVirtualFileSourceFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.zipline.apicheck
package app.cash.zipline.api.fir

import app.cash.zipline.ZiplineService
import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.isEqualTo
import java.io.File
import org.junit.Test

internal class ZiplineServicesReaderTest {
internal class FirZiplineApiReaderTest {
private val sources = System.getProperty("zipline.internal.sources")
.split(File.pathSeparator)
.map(::File)
Expand All @@ -33,34 +33,38 @@ internal class ZiplineServicesReaderTest {

@Test
fun happyPath() {
val ziplineServices = readZiplineServices(sources, classpath)
assertThat(ziplineServices).containsExactly(
DeclaredZiplineService(
name = EchoService::class.qualifiedName!!,
functions = listOf(
DeclaredZiplineFunction("fun close(): kotlin.Unit"),
DeclaredZiplineFunction("fun echo(kotlin.String): kotlin.String"),
DeclaredZiplineFunction("val greeting: kotlin.String"),
DeclaredZiplineFunction("var terse: kotlin.Boolean"),
),
),
DeclaredZiplineService(
name = ExtendedEchoService::class.qualifiedName!!,
functions = listOf(
DeclaredZiplineFunction("fun close(): kotlin.Unit"),
DeclaredZiplineFunction("fun echo(kotlin.String): kotlin.String"),
DeclaredZiplineFunction("fun echoAll(kotlin.collections.List<kotlin.String>): kotlin.collections.List<kotlin.String>"),
DeclaredZiplineFunction("val greeting: kotlin.String"),
DeclaredZiplineFunction("var terse: kotlin.Boolean"),
),
),
DeclaredZiplineService(
name = UnnecessaryEchoService::class.qualifiedName!!,
functions = listOf(
DeclaredZiplineFunction("fun close(): kotlin.Unit"),
DeclaredZiplineFunction("fun echo(kotlin.String): kotlin.String"),
DeclaredZiplineFunction("val greeting: kotlin.String"),
DeclaredZiplineFunction("var terse: kotlin.Boolean"),
val ziplineApi = readFirZiplineApi(sources, classpath)
assertThat(ziplineApi).isEqualTo(
FirZiplineApi(
listOf(
FirZiplineService(
name = EchoService::class.qualifiedName!!,
functions = listOf(
FirZiplineFunction("fun close(): kotlin.Unit"),
FirZiplineFunction("fun echo(kotlin.String): kotlin.String"),
FirZiplineFunction("val greeting: kotlin.String"),
FirZiplineFunction("var terse: kotlin.Boolean"),
),
),
FirZiplineService(
name = ExtendedEchoService::class.qualifiedName!!,
functions = listOf(
FirZiplineFunction("fun close(): kotlin.Unit"),
FirZiplineFunction("fun echo(kotlin.String): kotlin.String"),
FirZiplineFunction("fun echoAll(kotlin.collections.List<kotlin.String>): kotlin.collections.List<kotlin.String>"),
FirZiplineFunction("val greeting: kotlin.String"),
FirZiplineFunction("var terse: kotlin.Boolean"),
),
),
FirZiplineService(
name = UnnecessaryEchoService::class.qualifiedName!!,
functions = listOf(
FirZiplineFunction("fun close(): kotlin.Unit"),
FirZiplineFunction("fun echo(kotlin.String): kotlin.String"),
FirZiplineFunction("val greeting: kotlin.String"),
FirZiplineFunction("var terse: kotlin.Boolean"),
),
),
),
),
)
Expand Down

0 comments on commit 45f2171

Please sign in to comment.