Skip to content

Commit

Permalink
Fix Kotlin Multiplatform support
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierSegoviaCordoba committed Aug 31, 2024
1 parent 8f71e01 commit 7a850b8
Show file tree
Hide file tree
Showing 30 changed files with 227 additions and 201 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Fixed

- Kotlin Multiplatform support

### Removed

### Updated
Expand All @@ -18,8 +20,8 @@

### Fixed

- crash when atomicfu plugin is applied
- type mismatch errors inside the `copy` function are not shown
- Kotlin Multiplatform support

### Updated

Expand Down
37 changes: 2 additions & 35 deletions kopy-args/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,7 @@ hubdle {
}
}
}
kotlin {
multiplatform {
common()
androidNative()
apple {
ios {
iosArm64()
iosSimulatorArm64()
iosX64()
}
macos {
macosArm64()
macosX64()
}
tvos {
tvosArm64()
tvosSimulatorArm64()
tvosX64()
}
}
jvm()
js {
browser()
nodejs()
}
linux {
linuxArm64()
linuxX64()
}
mingw {
mingwX64()
}
native()
wasmJs()
}
kotlin { //
jvm()
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.javiersc.kotlin.kopy.args

public enum class KopyFunctions(public val value: String) : KopyArgument {
import java.io.Serializable

public enum class KopyFunctions(public val value: String) : Serializable {
All(value = "all"),
Copy(value = "copy"),
Invoke(value = "invoke"),
Expand All @@ -9,6 +11,7 @@ public enum class KopyFunctions(public val value: String) : KopyArgument {
public companion object {

public const val NAME: String = "KopyFunctions"
public const val VALUE_DESCRIPTION: String = "<all|copy|invoke>"
public const val DESCRIPTION: String = "Kopy functions to be generated"

public fun from(value: String): KopyFunctions =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.javiersc.kotlin.kopy.args

import java.io.Serializable

public enum class KopyVisibility(
public val value: String,
public val restrictive: Int,
) : KopyArgument {
) : Serializable {
Auto(value = "auto", restrictive = 0),
Public(value = "public", restrictive = 1),
Internal(value = "internal", restrictive = 2),
Expand All @@ -14,6 +16,7 @@ public enum class KopyVisibility(
public companion object {

public const val NAME: String = "KopyVisibility"
public const val VALUE_DESCRIPTION: String = "<auto|public|internal|protected|private>"
public const val DESCRIPTION: String = "Visibility of the generated copy function"

public fun from(value: String): KopyVisibility =
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public class KopyCommandLineProcessor : CommandLineProcessor {
listOf(
CliOption(
optionName = KopyFunctions.NAME,
valueDescription = KopyFunctions.DESCRIPTION,
valueDescription = KopyFunctions.VALUE_DESCRIPTION,
description = KopyFunctions.DESCRIPTION,
required = true,
required = false,
),
CliOption(
optionName = KopyVisibility.NAME,
valueDescription = KopyVisibility.DESCRIPTION,
valueDescription = KopyVisibility.VALUE_DESCRIPTION,
description = KopyVisibility.DESCRIPTION,
required = true,
required = false,
),
)

Expand All @@ -39,8 +39,8 @@ public class KopyCommandLineProcessor : CommandLineProcessor {
configuration.put(key, value)

when (option.optionName) {
KopyFunctions.NAME -> put(KopyKey.Functions, KopyFunctions.from(value))
KopyVisibility.NAME -> put(KopyKey.Visibility, KopyVisibility.from(value))
KopyFunctions.NAME -> put(KopyKey.Functions, value)
KopyVisibility.NAME -> put(KopyKey.Visibility, value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import com.javiersc.kotlin.kopy.args.KopyVisibility
import org.jetbrains.kotlin.config.CompilerConfigurationKey

internal object KopyKey {
val Visibility = CompilerConfigurationKey<KopyVisibility>(KopyVisibility.NAME)
val Functions = CompilerConfigurationKey<KopyFunctions>(KopyFunctions.NAME)
val Functions = CompilerConfigurationKey<String>(KopyFunctions.NAME)
val Visibility = CompilerConfigurationKey<String>(KopyVisibility.NAME)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.javiersc.kotlin.kopy.compiler.fir.checker.checkers

import com.javiersc.kotlin.compiler.extensions.common.classId
import com.javiersc.kotlin.compiler.extensions.common.fqName
import com.javiersc.kotlin.compiler.extensions.fir.name
import com.javiersc.kotlin.kopy.Kopy
import com.javiersc.kotlin.kopy.compiler.fir.checker.FirKopyError
import com.javiersc.kotlin.kopy.compiler.kopyClassId
import com.javiersc.kotlin.kopy.compiler.kopyFqName
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.SourceElementPositioningStrategies
import org.jetbrains.kotlin.diagnostics.reportOn
Expand Down Expand Up @@ -35,11 +34,11 @@ private object DataClassKopyAnnotationChecker :
context: CheckerContext,
reporter: DiagnosticReporter
) {
val isKopy: Boolean = declaration.hasAnnotation(classId<Kopy>(), context.session)
val isKopy: Boolean = declaration.hasAnnotation(kopyClassId, context.session)
val isDataClass: Boolean = declaration.symbol.isData
if (isKopy && !isDataClass) {
val kopyAnnotation: FirAnnotation =
declaration.annotations.first { it.fqName(context.session) == fqName<Kopy>() }
declaration.annotations.first { it.fqName(context.session) == kopyFqName }
reporter.reportOn(
source = kopyAnnotation.source,
factory = FirKopyError.NON_DATA_CLASS_KOPY_ANNOTATED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

package com.javiersc.kotlin.kopy.compiler.fir.generation

import com.javiersc.kotlin.compiler.extensions.common.classId
import com.javiersc.kotlin.compiler.extensions.common.fqName
import com.javiersc.kotlin.compiler.extensions.common.toName
import com.javiersc.kotlin.compiler.extensions.fir.asFirOrNull
import com.javiersc.kotlin.kopy.Kopy
import com.javiersc.kotlin.kopy.compiler.kopyClassId
import com.javiersc.kotlin.kopy.compiler.kopyFqName
import com.javiersc.kotlin.kopy.compiler.utils.toName
import org.jetbrains.kotlin.KtFakeSourceElementKind.AssignmentPluginAltered
import org.jetbrains.kotlin.fakeElement
import org.jetbrains.kotlin.fir.FirSession
Expand Down Expand Up @@ -53,7 +52,7 @@ internal class FirKopyAssignExpressionAltererExtension(
val kopyClass: FirRegularClassSymbol =
session.symbolProvider.getRegularClassSymbolByClassId(kopyClassClassId) ?: return null

if (!kopyClass.hasAnnotation(classId = classId<Kopy>(), session = session)) return null
if (!kopyClass.hasAnnotation(classId = kopyClassId, session = session)) return null

val leftArgument: FirReference = variableAssignment.calleeReference!!
val leftSymbol: FirVariableSymbol<*> = leftArgument.toResolvedVariableSymbol()!!
Expand Down Expand Up @@ -87,6 +86,6 @@ internal class FirKopyAssignExpressionAltererExtension(
}

override fun FirDeclarationPredicateRegistrar.registerPredicates() {
register(DeclarationPredicate.create { annotated(fqName<Kopy>()) })
register(DeclarationPredicate.create { annotated(kopyFqName) })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

package com.javiersc.kotlin.kopy.compiler.fir.generation

import com.javiersc.kotlin.compiler.extensions.common.classId
import com.javiersc.kotlin.compiler.extensions.common.fqName
import com.javiersc.kotlin.compiler.extensions.common.toClassId
import com.javiersc.kotlin.compiler.extensions.common.toName
import com.javiersc.kotlin.compiler.extensions.fir.createFirAnnotation
import com.javiersc.kotlin.compiler.extensions.fir.toFirTypeRef
import com.javiersc.kotlin.kopy.Kopy
import com.javiersc.kotlin.kopy.KopyFunctionCopy
import com.javiersc.kotlin.kopy.KopyFunctionInvoke
import com.javiersc.kotlin.kopy.KopyFunctionSet
import com.javiersc.kotlin.kopy.KopyFunctionUpdate
import com.javiersc.kotlin.kopy.KopyFunctionUpdateEach
import com.javiersc.kotlin.kopy.args.KopyFunctions
import com.javiersc.kotlin.kopy.args.KopyVisibility
import com.javiersc.kotlin.kopy.compiler.KopyKey
import com.javiersc.kotlin.kopy.compiler.underscoreAtomicName
import com.javiersc.kotlin.kopy.compiler.atomicRefClassId
import com.javiersc.kotlin.kopy.compiler.copyName
import com.javiersc.kotlin.kopy.compiler.fir.Key
import com.javiersc.kotlin.kopy.compiler.invokeName
import com.javiersc.kotlin.kopy.compiler.iterableClassId
import com.javiersc.kotlin.kopy.compiler.kopyClassId
import com.javiersc.kotlin.kopy.compiler.kopyFqName
import com.javiersc.kotlin.kopy.compiler.kopyFunctionCopyClassId
import com.javiersc.kotlin.kopy.compiler.kopyFunctionInvokeClassId
import com.javiersc.kotlin.kopy.compiler.kopyFunctionSetClassId
import com.javiersc.kotlin.kopy.compiler.kopyFunctionUpdateClassId
import com.javiersc.kotlin.kopy.compiler.kopyFunctionUpdateEachClassId
import com.javiersc.kotlin.kopy.compiler.kopyOptInClassId
import com.javiersc.kotlin.kopy.compiler.setName
import com.javiersc.kotlin.kopy.compiler.updateEachName
import com.javiersc.kotlin.kopy.compiler.updateName
import com.javiersc.kotlin.kopy.compiler.utils.toName
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
Expand Down Expand Up @@ -67,25 +74,11 @@ internal class FirKopyDeclarationGenerationExtension(
private val configuration: CompilerConfiguration,
) : FirDeclarationGenerationExtension(session) {

private val kopyVisibility: KopyVisibility
get() = configuration[KopyKey.Visibility, KopyVisibility.Auto]

private val kopyFunctions: KopyFunctions
get() = configuration[KopyKey.Functions, KopyFunctions.All]

private val kopyOptInClassId: ClassId = "com.javiersc.kotlin.kopy.KopyOptIn".toClassId()
private val kopyFunctionCopyClassId: ClassId = classId<KopyFunctionCopy>()
private val kopyFunctionInvokeClassId: ClassId = classId<KopyFunctionInvoke>()
private val kopyFunctionSetClassId: ClassId = classId<KopyFunctionSet>()
private val kopyFunctionUpdateClassId: ClassId = classId<KopyFunctionUpdate>()
private val kopyFunctionUpdateEachClassId: ClassId = classId<KopyFunctionUpdateEach>()
private val atomicRefClassId: ClassId = "kotlinx.atomicfu.AtomicRef".toClassId()
private val atomicName: Name = "_atomic".toName()
private val copyName: Name = "copy".toName()
private val invokeName: Name = "invoke".toName()
private val setName: Name = "set".toName()
private val updateName: Name = "update".toName()
private val updateEachName: Name = "updateEach".toName()
get() = KopyFunctions.from(configuration[KopyKey.Functions, KopyFunctions.All.value])

private val kopyVisibility: KopyVisibility
get() = KopyVisibility.from(configuration[KopyKey.Visibility, KopyVisibility.Auto.value])

private val function1Class: FirClassLikeSymbol<*>
get() = session
Expand All @@ -97,7 +90,7 @@ internal class FirKopyDeclarationGenerationExtension(
.withArguments { it.type!! }

override fun FirDeclarationPredicateRegistrar.registerPredicates() {
register(DeclarationPredicate.create { annotated(fqName<Kopy>()) })
register(DeclarationPredicate.create { annotated(kopyFqName) })
}

override fun getCallableNamesForClass(
Expand All @@ -106,7 +99,7 @@ internal class FirKopyDeclarationGenerationExtension(
): Set<Name> {
val names: Set<Name> =
setOf(
atomicName,
underscoreAtomicName,
copyName,
invokeName,
setName,
Expand All @@ -120,9 +113,9 @@ internal class FirKopyDeclarationGenerationExtension(
callableId: CallableId,
context: MemberGenerationContext?
): List<FirPropertySymbol> {
if (callableId.callableName != atomicName) return emptyList()
if (callableId.callableName != underscoreAtomicName) return emptyList()
val owner: FirClassSymbol<*> = context?.owner ?: return emptyList()
if (!owner.hasAnnotation(classId<Kopy>(), session)) return emptyList()
if (!owner.hasAnnotation(kopyClassId, session)) return emptyList()
if (!owner.isData) return emptyList()

val atomicRefType: ConeKotlinType = createAtomicRefType(owner)
Expand Down Expand Up @@ -151,7 +144,7 @@ internal class FirKopyDeclarationGenerationExtension(
context: MemberGenerationContext?
): List<FirNamedFunctionSymbol> = buildList {
val owner: FirClassSymbol<*> = context?.owner ?: return@buildList
if (!owner.hasAnnotation(classId<Kopy>(), session)) return@buildList
if (!owner.hasAnnotation(kopyClassId, session)) return@buildList
if (!owner.isData) return@buildList

if (kopyFunctions == KopyFunctions.All || kopyFunctions == KopyFunctions.Copy) {
Expand Down Expand Up @@ -306,7 +299,7 @@ internal class FirKopyDeclarationGenerationExtension(
val iterableClass: FirClassLikeSymbol<*> =
session
.symbolProvider
.getClassLikeSymbolByClassId(classId<Iterable<*>>()) ?: return null
.getClassLikeSymbolByClassId(iterableClassId) ?: return null

val iterableType: ConeClassLikeType =
(iterableClass.toFirTypeRef().coneType as ConeClassLikeType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.javiersc.kotlin.kopy.compiler.fir.utils

import com.javiersc.kotlin.compiler.extensions.common.classId

import com.javiersc.kotlin.compiler.extensions.fir.asFirOrNull
import com.javiersc.kotlin.kopy.KopyFunctionSet
import com.javiersc.kotlin.kopy.KopyFunctionUpdate
import com.javiersc.kotlin.kopy.KopyFunctionUpdateEach
import com.javiersc.kotlin.kopy.compiler.kopyFunctionSetClassId
import com.javiersc.kotlin.kopy.compiler.kopyFunctionUpdateClassId
import com.javiersc.kotlin.kopy.compiler.kopyFunctionUpdateEachClassId
import org.jetbrains.kotlin.fir.expressions.FirCall
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
import org.jetbrains.kotlin.fir.references.symbol
Expand Down Expand Up @@ -41,13 +41,13 @@ internal val FirCall.isKopyFunctionSetOrUpdateOrUpdateEachCall: Boolean
}

internal val ClassId?.isKopyFunctionSet: Boolean
get() = this == classId<KopyFunctionSet>()
get() = this == kopyFunctionSetClassId

internal val ClassId?.isKopyFunctionUpdate: Boolean
get() = this == kopyFunctionUpdateClassId

internal val ClassId?.isKopyFunctionUpdateEach: Boolean
get() = this == classId<KopyFunctionUpdateEach>()
get() = this == kopyFunctionUpdateEachClassId

internal val ClassId?.isKopyFunctionSetOrUpdateOrUpdateEach: Boolean
get() =
this == classId<KopyFunctionSet>() ||
this == classId<KopyFunctionUpdate>() ||
this == classId<KopyFunctionUpdateEach>()
get() = isKopyFunctionSet || isKopyFunctionUpdate || isKopyFunctionUpdateEach
Loading

0 comments on commit 7a850b8

Please sign in to comment.