KT-65897: add exporting of kotlin constructors as Swift constructors #KT-65897 fixed
Merge-request: KT-MR-14734 Merged-by: Artem Olkov <artem.olkov@jetbrains.com>
This commit is contained in:
@@ -34,6 +34,8 @@ private abstract class PsiToSirTranslation<T>(
|
|||||||
) : KtVisitor<T, Unit?>() {
|
) : KtVisitor<T, Unit?>() {
|
||||||
abstract override fun visitClassOrObject(classOrObject: KtClassOrObject, data: Unit?): T
|
abstract override fun visitClassOrObject(classOrObject: KtClassOrObject, data: Unit?): T
|
||||||
abstract override fun visitNamedFunction(function: KtNamedFunction, data: Unit?): T
|
abstract override fun visitNamedFunction(function: KtNamedFunction, data: Unit?): T
|
||||||
|
abstract override fun visitPrimaryConstructor(constructor: KtPrimaryConstructor, data: Unit?): T
|
||||||
|
abstract override fun visitSecondaryConstructor(constructor: KtSecondaryConstructor, data: Unit?): T
|
||||||
abstract override fun visitProperty(property: KtProperty, data: Unit?): T
|
abstract override fun visitProperty(property: KtProperty, data: Unit?): T
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +53,14 @@ private class PsiToSirTranslationCollector(
|
|||||||
function.checkAndTranslate(null)
|
function.checkAndTranslate(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun visitPrimaryConstructor(constructor: KtPrimaryConstructor) {
|
||||||
|
constructor.checkAndTranslate(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun visitSecondaryConstructor(constructor: KtSecondaryConstructor) {
|
||||||
|
constructor.checkAndTranslate(null)
|
||||||
|
}
|
||||||
|
|
||||||
override fun visitProperty(property: KtProperty) {
|
override fun visitProperty(property: KtProperty) {
|
||||||
property.checkAndTranslate(null)
|
property.checkAndTranslate(null)
|
||||||
}
|
}
|
||||||
@@ -82,9 +92,21 @@ private class PsiToSirTranslatableChecker(
|
|||||||
return functionIsPublicAndTopLevel && functionSymbolIsTranslatable
|
return functionIsPublicAndTopLevel && functionSymbolIsTranslatable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun visitPrimaryConstructor(constructor: KtPrimaryConstructor, data: Unit?): Boolean {
|
||||||
|
return constructor.isConsumable()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun visitSecondaryConstructor(constructor: KtSecondaryConstructor, data: Unit?): Boolean {
|
||||||
|
return constructor.isConsumable()
|
||||||
|
}
|
||||||
|
|
||||||
override fun visitProperty(property: KtProperty, data: Unit?): Boolean {
|
override fun visitProperty(property: KtProperty, data: Unit?): Boolean {
|
||||||
return property.isPublic
|
return property.isPublic
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun KtConstructor<*>.isConsumable(): Boolean {
|
||||||
|
return isPublic
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class PsiToSirElementTranslation(
|
private class PsiToSirElementTranslation(
|
||||||
@@ -99,6 +121,14 @@ private class PsiToSirElementTranslation(
|
|||||||
buildSirFunctionFromPsi(function)
|
buildSirFunctionFromPsi(function)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun visitPrimaryConstructor(constructor: KtPrimaryConstructor, data: Unit?): SirDeclaration = with(analysisSession) {
|
||||||
|
buildSirConstructorFromPsi(constructor)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun visitSecondaryConstructor(constructor: KtSecondaryConstructor, data: Unit?): SirDeclaration = with(analysisSession) {
|
||||||
|
buildSirConstructorFromPsi(constructor)
|
||||||
|
}
|
||||||
|
|
||||||
override fun visitProperty(property: KtProperty, data: Unit?): SirDeclaration = with(analysisSession) {
|
override fun visitProperty(property: KtProperty, data: Unit?): SirDeclaration = with(analysisSession) {
|
||||||
buildSirVariableFromPsi(property)
|
buildSirVariableFromPsi(property)
|
||||||
}
|
}
|
||||||
@@ -118,6 +148,32 @@ internal fun buildSirClassFromPsi(classOrObject: KtClassOrObject): SirNamedDecla
|
|||||||
PsiToSirElementTranslation(analysisSession),
|
PsiToSirElementTranslation(analysisSession),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// HACK to support default constructors.
|
||||||
|
// todo: We should rework builder from PSI to AnalysisApi during KT-66310
|
||||||
|
val constructors = symbol.getMemberScope().getConstructors()
|
||||||
|
if (constructors.count() == 1 && constructors.first().psi == classOrObject) {
|
||||||
|
declarations.add(
|
||||||
|
0,
|
||||||
|
buildInit {
|
||||||
|
val constructorSymbol = constructors.first()
|
||||||
|
origin = KotlinSource(constructorSymbol)
|
||||||
|
|
||||||
|
kind = constructorSymbol.sirCallableKind
|
||||||
|
isFailable = false
|
||||||
|
initKind = SirInitializerKind.ORDINARY
|
||||||
|
|
||||||
|
constructorSymbol.valueParameters.mapTo(parameters) {
|
||||||
|
SirParameter(
|
||||||
|
argumentName = it.name.asString(),
|
||||||
|
type = buildSirNominalType(it.returnType)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
documentation = null
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}.also { resultedClass ->
|
}.also { resultedClass ->
|
||||||
resultedClass.declarations.forEach { decl -> decl.parent = resultedClass }
|
resultedClass.declarations.forEach { decl -> decl.parent = resultedClass }
|
||||||
}
|
}
|
||||||
@@ -143,6 +199,25 @@ internal fun buildSirFunctionFromPsi(function: KtNamedFunction): SirFunction = b
|
|||||||
documentation = function.docComment?.text
|
documentation = function.docComment?.text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context(KtAnalysisSession)
|
||||||
|
internal fun buildSirConstructorFromPsi(function: KtConstructor<*>): SirInit = buildInit {
|
||||||
|
val symbol = function.getConstructorSymbol()
|
||||||
|
origin = KotlinSource(symbol)
|
||||||
|
|
||||||
|
kind = symbol.sirCallableKind
|
||||||
|
isFailable = false
|
||||||
|
initKind = SirInitializerKind.ORDINARY
|
||||||
|
|
||||||
|
symbol.valueParameters.mapTo(parameters) {
|
||||||
|
SirParameter(
|
||||||
|
argumentName = it.name.asString(),
|
||||||
|
type = buildSirNominalType(it.returnType)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
documentation = function.docComment?.text
|
||||||
|
}
|
||||||
|
|
||||||
context(KtAnalysisSession)
|
context(KtAnalysisSession)
|
||||||
internal fun buildSirVariableFromPsi(variable: KtProperty): SirVariable = buildVariable {
|
internal fun buildSirVariableFromPsi(variable: KtProperty): SirVariable = buildVariable {
|
||||||
val symbol = variable.getVariableSymbol()
|
val symbol = variable.getVariableSymbol()
|
||||||
@@ -213,8 +288,9 @@ private val KtCallableSymbol.sirCallableKind: SirCallableKind
|
|||||||
SirCallableKind.STATIC_METHOD
|
SirCallableKind.STATIC_METHOD
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KtSymbolKind.CLASS_MEMBER, KtSymbolKind.ACCESSOR -> SirCallableKind.INSTANCE_METHOD
|
KtSymbolKind.CLASS_MEMBER, KtSymbolKind.ACCESSOR
|
||||||
|
-> SirCallableKind.INSTANCE_METHOD
|
||||||
KtSymbolKind.LOCAL,
|
KtSymbolKind.LOCAL,
|
||||||
KtSymbolKind.SAM_CONSTRUCTOR ->
|
KtSymbolKind.SAM_CONSTRUCTOR
|
||||||
TODO("encountered callable kind($symbolKind) that is not translatable currently. Fix this crash during KT-65980.")
|
-> TODO("encountered callable kind($symbolKind) that is not translatable currently. Fix this crash during KT-65980.")
|
||||||
}
|
}
|
||||||
|
|||||||
+48
-13
@@ -102,19 +102,7 @@ public class SirAsSwiftSourcesPrinter(private val printer: SmartPrinter) : SirVi
|
|||||||
function.name.swiftIdentifier,
|
function.name.swiftIdentifier,
|
||||||
"("
|
"("
|
||||||
)
|
)
|
||||||
if (function.parameters.isNotEmpty()) {
|
printParameters(function.parameters)
|
||||||
println()
|
|
||||||
withIndent {
|
|
||||||
function.parameters.forEachIndexed { index, sirParameter ->
|
|
||||||
print(sirParameter.swift)
|
|
||||||
if (index != function.parameters.lastIndex) {
|
|
||||||
println(",")
|
|
||||||
} else {
|
|
||||||
println()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
print(
|
print(
|
||||||
")",
|
")",
|
||||||
" -> ",
|
" -> ",
|
||||||
@@ -129,6 +117,26 @@ public class SirAsSwiftSourcesPrinter(private val printer: SmartPrinter) : SirVi
|
|||||||
println("}")
|
println("}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun visitInit(init: SirInit): Unit = with(printer) {
|
||||||
|
init.documentation?.let { println(it) }
|
||||||
|
printVisibility(init)
|
||||||
|
printInitKind(init.initKind)
|
||||||
|
print("init")
|
||||||
|
"?".takeIf { init.isFailable }?.let { print(it) }
|
||||||
|
print("(")
|
||||||
|
printParameters(init.parameters)
|
||||||
|
print(
|
||||||
|
")"
|
||||||
|
)
|
||||||
|
println(" {")
|
||||||
|
withIndent {
|
||||||
|
printFunctionBody(init.body).forEach {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println("}")
|
||||||
|
}
|
||||||
|
|
||||||
override fun visitEnum(enum: SirEnum): Unit = with(printer) {
|
override fun visitEnum(enum: SirEnum): Unit = with(printer) {
|
||||||
printVisibility(enum)
|
printVisibility(enum)
|
||||||
println(
|
println(
|
||||||
@@ -184,6 +192,16 @@ internal fun SmartPrinter.printVisibility(decl: SirDeclaration) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun SmartPrinter.printInitKind(decl: SirInitializerKind) {
|
||||||
|
print(
|
||||||
|
when (decl) {
|
||||||
|
SirInitializerKind.ORDINARY -> ""
|
||||||
|
SirInitializerKind.REQUIRED -> "required "
|
||||||
|
SirInitializerKind.CONVENIENCE -> "convenience "
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
internal fun SmartPrinter.printCallableKind(callableKind: SirCallableKind) {
|
internal fun SmartPrinter.printCallableKind(callableKind: SirCallableKind) {
|
||||||
print(
|
print(
|
||||||
when (callableKind) {
|
when (callableKind) {
|
||||||
@@ -194,3 +212,20 @@ internal fun SmartPrinter.printCallableKind(callableKind: SirCallableKind) {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun SmartPrinter.printParameters(params: List<SirParameter>): Unit = params
|
||||||
|
.takeIf { it.isNotEmpty() }
|
||||||
|
?.let {
|
||||||
|
println()
|
||||||
|
withIndent {
|
||||||
|
params.forEachIndexed { index, sirParameter ->
|
||||||
|
print(sirParameter.swift)
|
||||||
|
if (index != params.lastIndex) {
|
||||||
|
println(",")
|
||||||
|
} else {
|
||||||
|
println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?: Unit
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
public class Foo {
|
||||||
|
public init?(
|
||||||
|
arg1: Swift.Bool,
|
||||||
|
arg2: Swift.Int8,
|
||||||
|
arg3: Swift.Int16,
|
||||||
|
arg4: Swift.Int32,
|
||||||
|
arg5: Swift.Int64,
|
||||||
|
arg6: Swift.Double,
|
||||||
|
arg7: Swift.Float
|
||||||
|
) {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
|
public init(
|
||||||
|
arg1: Swift.UInt8,
|
||||||
|
arg2: Swift.UInt16,
|
||||||
|
arg3: Swift.UInt32,
|
||||||
|
arg4: Swift.UInt64
|
||||||
|
) {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
|
public required init(
|
||||||
|
arg1: Swift.UInt8,
|
||||||
|
arg2: Swift.UInt16,
|
||||||
|
arg3: Swift.UInt32,
|
||||||
|
arg4: Swift.UInt64
|
||||||
|
) {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
|
public convenience init(
|
||||||
|
arg1: Swift.UInt8,
|
||||||
|
arg2: Swift.UInt16,
|
||||||
|
arg3: Swift.UInt32,
|
||||||
|
arg4: Swift.UInt64
|
||||||
|
) {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
|
}
|
||||||
+151
@@ -460,6 +460,157 @@ class SirAsSwiftSourcesPrinterTests {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should print class with constructor`() {
|
||||||
|
|
||||||
|
val module = buildModule {
|
||||||
|
name = "Test"
|
||||||
|
declarations.add(
|
||||||
|
buildClass {
|
||||||
|
origin = SirOrigin.Unknown
|
||||||
|
name = "Foo"
|
||||||
|
|
||||||
|
declarations.add(
|
||||||
|
buildInit {
|
||||||
|
origin = SirOrigin.Unknown
|
||||||
|
kind = SirCallableKind.INSTANCE_METHOD
|
||||||
|
initKind = SirInitializerKind.ORDINARY
|
||||||
|
visibility = SirVisibility.PUBLIC
|
||||||
|
isFailable = true
|
||||||
|
parameters.addAll(
|
||||||
|
listOf(
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg1",
|
||||||
|
type = SirNominalType(SirSwiftModule.bool)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg2",
|
||||||
|
type = SirNominalType(SirSwiftModule.int8)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg3",
|
||||||
|
type = SirNominalType(SirSwiftModule.int16)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg4",
|
||||||
|
type = SirNominalType(SirSwiftModule.int32)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg5",
|
||||||
|
type = SirNominalType(SirSwiftModule.int64)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg6",
|
||||||
|
type = SirNominalType(SirSwiftModule.double)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg7",
|
||||||
|
type = SirNominalType(SirSwiftModule.float)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
declarations.add(
|
||||||
|
buildInit {
|
||||||
|
origin = SirOrigin.Unknown
|
||||||
|
kind = SirCallableKind.INSTANCE_METHOD
|
||||||
|
initKind = SirInitializerKind.ORDINARY
|
||||||
|
visibility = SirVisibility.PUBLIC
|
||||||
|
isFailable = false
|
||||||
|
parameters.addAll(
|
||||||
|
listOf(
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg1",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint8)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg2",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint16)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg3",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint32)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg4",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint64)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
declarations.add(
|
||||||
|
buildInit {
|
||||||
|
origin = SirOrigin.Unknown
|
||||||
|
kind = SirCallableKind.INSTANCE_METHOD
|
||||||
|
initKind = SirInitializerKind.REQUIRED
|
||||||
|
visibility = SirVisibility.PUBLIC
|
||||||
|
isFailable = false
|
||||||
|
parameters.addAll(
|
||||||
|
listOf(
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg1",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint8)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg2",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint16)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg3",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint32)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg4",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint64)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
declarations.add(
|
||||||
|
buildInit {
|
||||||
|
origin = SirOrigin.Unknown
|
||||||
|
kind = SirCallableKind.INSTANCE_METHOD
|
||||||
|
initKind = SirInitializerKind.CONVENIENCE
|
||||||
|
visibility = SirVisibility.PUBLIC
|
||||||
|
isFailable = false
|
||||||
|
parameters.addAll(
|
||||||
|
listOf(
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg1",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint8)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg2",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint16)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg3",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint32)
|
||||||
|
),
|
||||||
|
SirParameter(
|
||||||
|
argumentName = "arg4",
|
||||||
|
type = SirNominalType(SirSwiftModule.uint64)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
runTest(
|
||||||
|
module,
|
||||||
|
"testData/class_with_init"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should print class with variable`() {
|
fun `should print class with variable`() {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This file was generated automatically. See native/swift/sir/tree-generator/Readme.md.
|
||||||
|
// DO NOT MODIFY IT MANUALLY.
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.sir
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.sir.visitors.SirTransformer
|
||||||
|
import org.jetbrains.kotlin.sir.visitors.SirVisitor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generated from: [org.jetbrains.kotlin.sir.tree.generator.SwiftIrTree.init]
|
||||||
|
*/
|
||||||
|
abstract class SirInit : SirCallable() {
|
||||||
|
abstract override val origin: SirOrigin
|
||||||
|
abstract override val visibility: SirVisibility
|
||||||
|
abstract override var parent: SirDeclarationParent
|
||||||
|
abstract override val kind: SirCallableKind
|
||||||
|
abstract override var body: SirFunctionBody?
|
||||||
|
abstract val isFailable: Boolean
|
||||||
|
abstract val parameters: List<SirParameter>
|
||||||
|
abstract val initKind: SirInitializerKind
|
||||||
|
abstract var documentation: String?
|
||||||
|
|
||||||
|
override fun <R, D> accept(visitor: SirVisitor<R, D>, data: D): R =
|
||||||
|
visitor.visitInit(this, data)
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
override fun <E : SirElement, D> transform(transformer: SirTransformer<D>, data: D): E =
|
||||||
|
transformer.transformInit(this, data) as E
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This file was generated automatically. See native/swift/sir/tree-generator/Readme.md.
|
||||||
|
// DO NOT MODIFY IT MANUALLY.
|
||||||
|
|
||||||
|
@file:Suppress("DuplicatedCode", "unused")
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.sir.builder
|
||||||
|
|
||||||
|
import kotlin.contracts.*
|
||||||
|
import org.jetbrains.kotlin.sir.*
|
||||||
|
import org.jetbrains.kotlin.sir.impl.SirInitImpl
|
||||||
|
|
||||||
|
@SirBuilderDsl
|
||||||
|
class SirInitBuilder {
|
||||||
|
var origin: SirOrigin = SirOrigin.Unknown
|
||||||
|
var visibility: SirVisibility = SirVisibility.PUBLIC
|
||||||
|
lateinit var kind: SirCallableKind
|
||||||
|
var body: SirFunctionBody? = null
|
||||||
|
var isFailable: Boolean by kotlin.properties.Delegates.notNull<Boolean>()
|
||||||
|
val parameters: MutableList<SirParameter> = mutableListOf()
|
||||||
|
lateinit var initKind: SirInitializerKind
|
||||||
|
var documentation: String? = null
|
||||||
|
|
||||||
|
fun build(): SirInit {
|
||||||
|
return SirInitImpl(
|
||||||
|
origin,
|
||||||
|
visibility,
|
||||||
|
kind,
|
||||||
|
body,
|
||||||
|
isFailable,
|
||||||
|
parameters,
|
||||||
|
initKind,
|
||||||
|
documentation,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
inline fun buildInit(init: SirInitBuilder.() -> Unit): SirInit {
|
||||||
|
contract {
|
||||||
|
callsInPlace(init, InvocationKind.EXACTLY_ONCE)
|
||||||
|
}
|
||||||
|
return SirInitBuilder().apply(init).build()
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
inline fun buildInitCopy(original: SirInit, init: SirInitBuilder.() -> Unit): SirInit {
|
||||||
|
contract {
|
||||||
|
callsInPlace(init, InvocationKind.EXACTLY_ONCE)
|
||||||
|
}
|
||||||
|
val copyBuilder = SirInitBuilder()
|
||||||
|
copyBuilder.origin = original.origin
|
||||||
|
copyBuilder.visibility = original.visibility
|
||||||
|
copyBuilder.kind = original.kind
|
||||||
|
copyBuilder.body = original.body
|
||||||
|
copyBuilder.isFailable = original.isFailable
|
||||||
|
copyBuilder.parameters.addAll(original.parameters)
|
||||||
|
copyBuilder.initKind = original.initKind
|
||||||
|
copyBuilder.documentation = original.documentation
|
||||||
|
return copyBuilder.apply(init).build()
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This file was generated automatically. See native/swift/sir/tree-generator/Readme.md.
|
||||||
|
// DO NOT MODIFY IT MANUALLY.
|
||||||
|
|
||||||
|
@file:Suppress("DuplicatedCode")
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.sir.impl
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.sir.*
|
||||||
|
import org.jetbrains.kotlin.sir.visitors.SirTransformer
|
||||||
|
import org.jetbrains.kotlin.sir.visitors.SirVisitor
|
||||||
|
|
||||||
|
internal class SirInitImpl(
|
||||||
|
override val origin: SirOrigin,
|
||||||
|
override val visibility: SirVisibility,
|
||||||
|
override val kind: SirCallableKind,
|
||||||
|
override var body: SirFunctionBody?,
|
||||||
|
override val isFailable: Boolean,
|
||||||
|
override val parameters: MutableList<SirParameter>,
|
||||||
|
override val initKind: SirInitializerKind,
|
||||||
|
override var documentation: String?,
|
||||||
|
) : SirInit() {
|
||||||
|
override lateinit var parent: SirDeclarationParent
|
||||||
|
|
||||||
|
override fun <R, D> acceptChildren(visitor: SirVisitor<R, D>, data: D) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun <D> transformChildren(transformer: SirTransformer<D>, data: D) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -87,6 +87,14 @@ abstract class SirTransformer<in D> : SirVisitor<SirElement, D>() {
|
|||||||
return transformCallable(callable, data)
|
return transformCallable(callable, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open fun transformInit(init: SirInit, data: D): SirDeclaration {
|
||||||
|
return transformCallable(init, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
final override fun visitInit(init: SirInit, data: D): SirDeclaration {
|
||||||
|
return transformInit(init, data)
|
||||||
|
}
|
||||||
|
|
||||||
open fun transformFunction(function: SirFunction, data: D): SirDeclaration {
|
open fun transformFunction(function: SirFunction, data: D): SirDeclaration {
|
||||||
return transformCallable(function, data)
|
return transformCallable(function, data)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,12 @@ abstract class SirTransformerVoid : SirTransformer<Nothing?>() {
|
|||||||
final override fun transformCallable(callable: SirCallable, data: Nothing?): SirDeclaration =
|
final override fun transformCallable(callable: SirCallable, data: Nothing?): SirDeclaration =
|
||||||
transformCallable(callable)
|
transformCallable(callable)
|
||||||
|
|
||||||
|
open fun transformInit(init: SirInit): SirDeclaration =
|
||||||
|
transformCallable(init)
|
||||||
|
|
||||||
|
final override fun transformInit(init: SirInit, data: Nothing?): SirDeclaration =
|
||||||
|
transformInit(init)
|
||||||
|
|
||||||
open fun transformFunction(function: SirFunction): SirDeclaration =
|
open fun transformFunction(function: SirFunction): SirDeclaration =
|
||||||
transformCallable(function)
|
transformCallable(function)
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,9 @@ abstract class SirVisitor<out R, in D> {
|
|||||||
open fun visitCallable(callable: SirCallable, data: D): R =
|
open fun visitCallable(callable: SirCallable, data: D): R =
|
||||||
visitDeclaration(callable, data)
|
visitDeclaration(callable, data)
|
||||||
|
|
||||||
|
open fun visitInit(init: SirInit, data: D): R =
|
||||||
|
visitCallable(init, data)
|
||||||
|
|
||||||
open fun visitFunction(function: SirFunction, data: D): R =
|
open fun visitFunction(function: SirFunction, data: D): R =
|
||||||
visitCallable(function, data)
|
visitCallable(function, data)
|
||||||
|
|
||||||
|
|||||||
@@ -86,6 +86,14 @@ abstract class SirVisitorVoid : SirVisitor<Unit, Nothing?>() {
|
|||||||
visitDeclaration(callable)
|
visitDeclaration(callable)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final override fun visitInit(init: SirInit, data: Nothing?) {
|
||||||
|
visitInit(init)
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun visitInit(init: SirInit) {
|
||||||
|
visitCallable(init)
|
||||||
|
}
|
||||||
|
|
||||||
final override fun visitFunction(function: SirFunction, data: Nothing?) {
|
final override fun visitFunction(function: SirFunction, data: Nothing?) {
|
||||||
visitFunction(function)
|
visitFunction(function)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.sir
|
||||||
|
|
||||||
|
enum class SirInitializerKind {
|
||||||
|
ORDINARY,
|
||||||
|
REQUIRED,
|
||||||
|
CONVENIENCE
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.sir.*
|
|||||||
val SirCallable.allParameters: List<SirParameter>
|
val SirCallable.allParameters: List<SirParameter>
|
||||||
get() = when (this) {
|
get() = when (this) {
|
||||||
is SirFunction -> this.parameters
|
is SirFunction -> this.parameters
|
||||||
|
is SirInit -> this.parameters
|
||||||
is SirSetter -> listOf(SirParameter(parameterName = parameterName, type = this.valueType))
|
is SirSetter -> listOf(SirParameter(parameterName = parameterName, type = this.valueType))
|
||||||
is SirGetter -> listOf()
|
is SirGetter -> listOf()
|
||||||
}
|
}
|
||||||
@@ -18,7 +19,7 @@ val SirCallable.returnType: SirType
|
|||||||
get() = when (this) {
|
get() = when (this) {
|
||||||
is SirFunction -> this.returnType
|
is SirFunction -> this.returnType
|
||||||
is SirGetter -> this.valueType
|
is SirGetter -> this.valueType
|
||||||
is SirSetter -> SirNominalType(SirSwiftModule.void)
|
is SirSetter, is SirInit -> SirNominalType(SirSwiftModule.void)
|
||||||
}
|
}
|
||||||
|
|
||||||
val SirAccessor.valueType: SirType
|
val SirAccessor.valueType: SirType
|
||||||
|
|||||||
+12
@@ -81,6 +81,18 @@ object SwiftIrTree : AbstractSwiftIrTreeBuilder() {
|
|||||||
+field("body", functionBodyType, nullable = true, mutable = true)
|
+field("body", functionBodyType, nullable = true, mutable = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val init by element {
|
||||||
|
customParentInVisitor = callable
|
||||||
|
parent(callable)
|
||||||
|
|
||||||
|
+field("isFailable", boolean)
|
||||||
|
+listField("parameters", parameterType)
|
||||||
|
|
||||||
|
+field("initKind", initKind)
|
||||||
|
|
||||||
|
+field(name = "documentation", string, nullable = true, mutable = true)
|
||||||
|
}
|
||||||
|
|
||||||
val function by element {
|
val function by element {
|
||||||
customParentInVisitor = callable
|
customParentInVisitor = callable
|
||||||
parent(callable)
|
parent(callable)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ val typeType = type(BASE_PACKAGE, "SirType", TypeKind.Class)
|
|||||||
val enumCaseType = type(BASE_PACKAGE, "SirEnumCase", TypeKind.Class)
|
val enumCaseType = type(BASE_PACKAGE, "SirEnumCase", TypeKind.Class)
|
||||||
val functionBodyType = type(BASE_PACKAGE, "SirFunctionBody", TypeKind.Class)
|
val functionBodyType = type(BASE_PACKAGE, "SirFunctionBody", TypeKind.Class)
|
||||||
val callableKind = type(BASE_PACKAGE, "SirCallableKind", TypeKind.Class)
|
val callableKind = type(BASE_PACKAGE, "SirCallableKind", TypeKind.Class)
|
||||||
|
val initKind = type(BASE_PACKAGE, "SirInitializerKind", TypeKind.Class)
|
||||||
|
|
||||||
private const val VISITORS_PACKAGE = "$BASE_PACKAGE.visitors"
|
private const val VISITORS_PACKAGE = "$BASE_PACKAGE.visitors"
|
||||||
|
|
||||||
|
|||||||
+37
@@ -4,10 +4,22 @@ import KotlinRuntime
|
|||||||
public enum namespace {
|
public enum namespace {
|
||||||
public enum deeper {
|
public enum deeper {
|
||||||
public class NAMESPACED_CLASS {
|
public class NAMESPACED_CLASS {
|
||||||
|
public init() {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public class Foo {
|
public class Foo {
|
||||||
|
public init() {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
public class INSIDE_CLASS {
|
public class INSIDE_CLASS {
|
||||||
|
public init() {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
public class DEEPER_INSIDE_CLASS {
|
public class DEEPER_INSIDE_CLASS {
|
||||||
|
public init() {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
public func foo() -> Swift.Bool {
|
public func foo() -> Swift.Bool {
|
||||||
fatalError()
|
fatalError()
|
||||||
}
|
}
|
||||||
@@ -61,9 +73,18 @@ public enum namespace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public class NAMESPACED_CLASS {
|
public class NAMESPACED_CLASS {
|
||||||
|
public init() {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public class Foo {
|
public class Foo {
|
||||||
|
public init() {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
public class INSIDE_CLASS {
|
public class INSIDE_CLASS {
|
||||||
|
public init() {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public func foo() -> Swift.Bool {
|
public func foo() -> Swift.Bool {
|
||||||
fatalError()
|
fatalError()
|
||||||
@@ -84,8 +105,24 @@ public enum namespace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ClassWithNonPublicConstructor {
|
||||||
|
}
|
||||||
|
|
||||||
public class Foo {
|
public class Foo {
|
||||||
|
public init(
|
||||||
|
a: Swift.Int32
|
||||||
|
) {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
|
public init(
|
||||||
|
f: Swift.Float
|
||||||
|
) {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
public class INSIDE_CLASS {
|
public class INSIDE_CLASS {
|
||||||
|
public init() {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
public func my_func() -> Swift.Bool {
|
public func my_func() -> Swift.Bool {
|
||||||
fatalError()
|
fatalError()
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-1
@@ -1,5 +1,9 @@
|
|||||||
|
public class ClassWithNonPublicConstructor internal constructor(public val a: Int)
|
||||||
|
|
||||||
class Foo {
|
class Foo (a: Int) {
|
||||||
|
constructor(f: Float) : this(f.toInt())
|
||||||
|
|
||||||
|
private constructor(d: Double) : this(d.toInt())
|
||||||
class INSIDE_CLASS {
|
class INSIDE_CLASS {
|
||||||
fun my_func(): Boolean = TODO()
|
fun my_func(): Boolean = TODO()
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -29,4 +29,4 @@ abstract class ABSTRACT_CLASS
|
|||||||
|
|
||||||
sealed class SEALED {
|
sealed class SEALED {
|
||||||
object O : SEALED()
|
object O : SEALED()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user