[FIR generator] Avoid FIR-specific code in AbstractBuilderPrinter
This commit is contained in:
committed by
Space Team
parent
cae4a9930b
commit
f3016e1dcb
+3
-1
@@ -33,7 +33,9 @@ class Element(name: String, override val propertyName: String, kind: Kind) : Abs
|
||||
override var kDoc: String? = null
|
||||
|
||||
override val fields = mutableSetOf<Field>()
|
||||
override val typeName: String = "Fir$name"
|
||||
|
||||
override val namePrefix: String
|
||||
get() = "Fir"
|
||||
|
||||
override val packageName: String = BASE_PACKAGE + kind.packageName.let { if (it.isBlank()) it else "." + it }
|
||||
|
||||
|
||||
+2
-1
@@ -76,7 +76,8 @@ class Element(
|
||||
|
||||
override var kind: ImplementationKind? = null
|
||||
|
||||
override val typeName = "Ir$name"
|
||||
override val namePrefix: String
|
||||
get() = "Ir"
|
||||
|
||||
/**
|
||||
* Whether this element is semantically a leaf element in the hierarchy.
|
||||
|
||||
+14
-5
@@ -162,6 +162,18 @@ abstract class AbstractBuilderPrinter<Element, Implementation, BuilderField, Ele
|
||||
defaultValue = "{}".takeIf { !hasRequiredFields },
|
||||
)
|
||||
|
||||
context(ImportCollector)
|
||||
private fun SmartPrinter.contractCallsInPlaceExactlyOnce() {
|
||||
addStarImport("kotlin.contracts")
|
||||
print("contract")
|
||||
printBlock {
|
||||
println("callsInPlace(init, InvocationKind.EXACTLY_ONCE)")
|
||||
}
|
||||
}
|
||||
|
||||
private fun builderFunctionName(builder: LeafBuilder<BuilderField, Element, Implementation>) =
|
||||
"build" + builder.implementation.run { name?.removePrefix(namePrefix) ?: element.name }
|
||||
|
||||
context(ImportCollector)
|
||||
private fun SmartPrinter.printDslBuildFunction(
|
||||
builder: LeafBuilder<BuilderField, Element, Implementation>,
|
||||
@@ -173,10 +185,9 @@ abstract class AbstractBuilderPrinter<Element, Implementation, BuilderField, Ele
|
||||
} else if (builder.implementation.isPublic) {
|
||||
println("@OptIn(", implementationDetailAnnotation.render(), "::class)")
|
||||
}
|
||||
val name = builder.implementation.name?.replaceFirst("Fir", "") ?: builder.implementation.element.name
|
||||
val initParameter = if (isEmpty) null else lambdaParameterForBuilderFunction(builder, hasRequiredFields)
|
||||
printFunctionWithBlockBody(
|
||||
name = "build$name",
|
||||
name = builderFunctionName(builder),
|
||||
parameters = listOfNotNull(initParameter),
|
||||
returnType = builder.implementation.element,
|
||||
typeParameters = builder.implementation.element.params,
|
||||
@@ -325,12 +336,10 @@ abstract class AbstractBuilderPrinter<Element, Implementation, BuilderField, Ele
|
||||
.filter { !it.invisibleField }
|
||||
.mapNotNullTo(mutableSetOf(experimentalContractsAnnotation)) { it.optInAnnotation }
|
||||
println("@OptIn(", optIns.joinToString { "${it.render()}::class" }, ")")
|
||||
// FIXME: Avoid FIR-specific code here
|
||||
val name = builder.implementation.name?.replaceFirst("Fir", "") ?: builder.implementation.element.name
|
||||
val originalParameter = FunctionParameter(name = "original", type = builder.implementation.element)
|
||||
val initParameter = lambdaParameterForBuilderFunction(builder, hasRequiredFields)
|
||||
printFunctionWithBlockBody(
|
||||
name = "build${name}Copy",
|
||||
name = builderFunctionName(builder) + "Copy",
|
||||
parameters = listOf(originalParameter, initParameter),
|
||||
returnType = builder.implementation.element,
|
||||
typeParameters = builder.implementation.element.params,
|
||||
|
||||
+5
@@ -20,6 +20,8 @@ abstract class AbstractElement<Element, Field, Implementation>(
|
||||
*/
|
||||
abstract val propertyName: String
|
||||
|
||||
abstract val namePrefix: String
|
||||
|
||||
abstract val kDoc: String?
|
||||
|
||||
abstract val fields: Set<Field>
|
||||
@@ -70,6 +72,9 @@ abstract class AbstractElement<Element, Field, Implementation>(
|
||||
override val allParents: List<Element>
|
||||
get() = elementParents.map { it.element }
|
||||
|
||||
override val typeName: String
|
||||
get() = namePrefix + name
|
||||
|
||||
context(ImportCollector)
|
||||
final override fun renderTo(appendable: Appendable) {
|
||||
addImport(this)
|
||||
|
||||
+6
-2
@@ -23,7 +23,11 @@ abstract class AbstractImplementation<Implementation, Element, Field>(
|
||||
override val allParents: List<ImplementationKindOwner>
|
||||
get() = listOf(element)
|
||||
|
||||
override val typeName: String = name ?: (element.typeName + "Impl")
|
||||
val namePrefix: String
|
||||
get() = element.namePrefix
|
||||
|
||||
override val typeName: String
|
||||
get() = name ?: (element.typeName + "Impl")
|
||||
|
||||
context(ImportCollector)
|
||||
override fun renderTo(appendable: Appendable) {
|
||||
@@ -87,4 +91,4 @@ abstract class AbstractImplementation<Implementation, Element, Field>(
|
||||
}
|
||||
|
||||
var builder: LeafBuilder<Field, Element, Implementation>? = null
|
||||
}
|
||||
}
|
||||
|
||||
+2
-6
@@ -51,11 +51,7 @@ class LeafBuilder<BuilderField, Element, Implementation>(
|
||||
Element : AbstractElement<Element, *, Implementation>,
|
||||
Implementation : AbstractImplementation<Implementation, Element, BuilderField> {
|
||||
override val typeName: String
|
||||
get() = if (implementation.name != null) {
|
||||
"${implementation.name}Builder"
|
||||
} else {
|
||||
"${implementation.element.typeName}Builder"
|
||||
}
|
||||
get() = (implementation.name ?: implementation.element.typeName) + "Builder"
|
||||
|
||||
override val allFields: List<BuilderField> by lazy { implementation.fieldsWithoutDefault }
|
||||
|
||||
@@ -87,4 +83,4 @@ class IntermediateBuilder<BuilderField, Element>(
|
||||
}
|
||||
|
||||
override val uselessFields: List<BuilderField> = emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user