-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First setup for multiple constructors
- Loading branch information
1 parent
12a5116
commit c3a4f24
Showing
16 changed files
with
163 additions
and
88 deletions.
There are no files selected for viewing
7 changes: 6 additions & 1 deletion
7
compiler-plugin/src/main/kotlin/io/github/mappie/BaseVisitor.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
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
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
64 changes: 33 additions & 31 deletions
64
compiler-plugin/src/main/kotlin/io/github/mappie/resolving/classes/ClassMappingResolver.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 |
---|---|---|
@@ -1,52 +1,54 @@ | ||
package io.github.mappie.resolving.classes | ||
|
||
import io.github.mappie.BaseVisitor | ||
import io.github.mappie.MappieIrRegistrar.Companion.context | ||
import io.github.mappie.resolving.* | ||
import io.github.mappie.util.irGet | ||
import org.jetbrains.kotlin.ir.declarations.IrFunction | ||
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol | ||
import org.jetbrains.kotlin.ir.types.getClass | ||
import org.jetbrains.kotlin.ir.util.getPropertyGetter | ||
import org.jetbrains.kotlin.ir.util.properties | ||
import org.jetbrains.kotlin.ir.util.callableId | ||
import org.jetbrains.kotlin.ir.util.fileEntry | ||
import org.jetbrains.kotlin.name.CallableId | ||
import org.jetbrains.kotlin.name.FqName | ||
import org.jetbrains.kotlin.name.Name | ||
|
||
class ClassMappingResolver : BaseVisitor<Mapping, Unit> { | ||
class ClassMappingResolver : BaseVisitor<List<Mapping>, Unit>() { | ||
|
||
override fun visitFunction(declaration: IrFunction, data: Unit): Mapping { | ||
override fun visitFunction(declaration: IrFunction, data: Unit): List<Mapping> { | ||
check(declaration.returnType.getClass()!!.isData) | ||
|
||
val sourceParameter = requireNotNull(declaration.valueParameters.firstOrNull()) | ||
val targets = declaration.accept(ValueParametersCollector(), Unit) | ||
val dispatchReceiverSymbol = declaration.valueParameters.first().symbol | ||
val concreteSources = declaration.body?.accept( | ||
ObjectSourcesCollector(dispatchReceiverSymbol), | ||
Unit | ||
) ?: emptyList() | ||
val possibilities = declaration.accept(ConstructorsCollector(), Unit) | ||
val sourceParameter = declaration.valueParameters.first() | ||
val getters = sourceParameter.accept(GettersCollector(), Unit) | ||
val dispatchReceiverSymbol = sourceParameter.symbol | ||
val concreteSources = declaration.body?.accept(ObjectSourcesCollector(declaration.fileEntry, dispatchReceiverSymbol), Unit) ?: emptyList() | ||
|
||
val mappings = targets.associateWith { target -> | ||
val concreteSource = concreteSources.firstOrNull { it.first == target.name } | ||
return possibilities.map { constructor -> | ||
val targets = constructor.valueParameters | ||
val mappings = targets.associateWith { target -> | ||
val concreteSource = concreteSources.firstOrNull { it.first == target.name } | ||
|
||
if (concreteSource != null) { | ||
listOf(concreteSource.second) | ||
} else { | ||
val sourceClass = requireNotNull(sourceParameter.type.getClass()) { | ||
"Expected type of source argument to be non-null." | ||
} | ||
val source = sourceClass.properties.firstOrNull { source -> source.name == target.name } | ||
if (source != null) { | ||
val getter = sourceClass.getPropertyGetter(source.name.asString()) | ||
if (concreteSource != null) { | ||
listOf(concreteSource.second) | ||
} else { | ||
val getter = getters.firstOrNull { getter -> | ||
getter.name == Name.special("<get-${target.name.asString()}>") | ||
} | ||
if (getter != null) { | ||
listOf(PropertySource(getter, target.type, sourceParameter.symbol)) | ||
listOf(PropertySource(getter.symbol, target.type, sourceParameter.symbol)) | ||
} else { | ||
emptyList() | ||
} | ||
} else { | ||
emptyList() | ||
} | ||
} | ||
} | ||
|
||
return ConstructorCallMapping( | ||
targetType = declaration.returnType, | ||
sourceType = sourceParameter.type, | ||
mappings = mappings | ||
) | ||
ConstructorCallMapping( | ||
targetType = declaration.returnType, | ||
sourceType = sourceParameter.type, | ||
symbol = constructor.symbol, | ||
mappings = mappings | ||
) | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
compiler-plugin/src/main/kotlin/io/github/mappie/resolving/classes/ConstructorsCollector.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,14 @@ | ||
package io.github.mappie.resolving.classes | ||
|
||
import io.github.mappie.BaseVisitor | ||
import org.jetbrains.kotlin.ir.declarations.IrConstructor | ||
import org.jetbrains.kotlin.ir.declarations.IrFunction | ||
import org.jetbrains.kotlin.ir.types.getClass | ||
import org.jetbrains.kotlin.ir.util.constructors | ||
|
||
class ConstructorsCollector : BaseVisitor<List<IrConstructor>, Unit>() { | ||
|
||
override fun visitFunction(declaration: IrFunction, data: Unit): List<IrConstructor> { | ||
return declaration.returnType.getClass()!!.constructors.toList() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
compiler-plugin/src/main/kotlin/io/github/mappie/resolving/classes/GettersCollector.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,19 @@ | ||
package io.github.mappie.resolving.classes | ||
|
||
import io.github.mappie.BaseVisitor | ||
import org.jetbrains.kotlin.ir.declarations.IrProperty | ||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction | ||
import org.jetbrains.kotlin.ir.declarations.IrValueParameter | ||
import org.jetbrains.kotlin.ir.types.getClass | ||
import org.jetbrains.kotlin.ir.util.properties | ||
|
||
class GettersCollector : BaseVisitor<List<IrSimpleFunction>, Unit>() { | ||
|
||
override fun visitValueParameter(declaration: IrValueParameter, data: Unit): List<IrSimpleFunction> { | ||
return declaration.type.getClass()!!.properties.flatMap { it.accept(Unit) }.toList() | ||
} | ||
|
||
override fun visitProperty(declaration: IrProperty, data: Unit): List<IrSimpleFunction> { | ||
return if (declaration.getter != null) declaration.getter?.let { listOf(it) } ?: emptyList() else emptyList() | ||
} | ||
} |
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
20 changes: 0 additions & 20 deletions
20
...ler-plugin/src/main/kotlin/io/github/mappie/resolving/classes/ValueParametersCollector.kt
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.