IR tree gen: restore strict cast in IrProperty.transformChildren

This commit is contained in:
Alexander Udalov
2022-06-10 23:20:49 +02:00
committed by Alexander Udalov
parent 529c03ab08
commit bf5ab49342
6 changed files with 23 additions and 8 deletions
@@ -55,7 +55,7 @@ abstract class IrProperty : IrDeclarationBase(), IrPossiblyExternalDeclaration,
override fun <D> transformChildren(transformer: IrElementTransformer<D>, data: D) {
backingField = backingField?.transform(transformer, data) as? IrField
getter = getter?.transform(transformer, data) as? IrSimpleFunction
setter = setter?.transform(transformer, data) as? IrSimpleFunction
getter = getter?.transform(transformer, data) as IrSimpleFunction?
setter = setter?.transform(transformer, data) as IrSimpleFunction?
}
}
@@ -380,8 +380,12 @@ object IrTree : AbstractTreeBuilder() {
+field("isExpect", boolean)
+field("isFakeOverride", boolean)
+field("backingField", field, mutable = true, nullable = true, isChild = true)
+field("getter", simpleFunction, mutable = true, nullable = true, isChild = true)
+field("setter", simpleFunction, mutable = true, nullable = true, isChild = true)
+field("getter", simpleFunction, mutable = true, nullable = true, isChild = true) {
strictCastInTransformChildren = true
}
+field("setter", simpleFunction, mutable = true, nullable = true, isChild = true) {
strictCastInTransformChildren = true
}
}
//TODO: make IrScript as IrPackageFragment, because script is used as a file, not as a class
@@ -87,6 +87,7 @@ sealed class FieldConfig(
var baseDefaultValue: CodeBlock? = null
var baseGetter: CodeBlock? = null
var printProperty = true
var strictCastInTransformChildren = false
var generationCallback: (PropertySpec.Builder.() -> Unit)? = null
@@ -74,6 +74,7 @@ sealed class Field(
val nullable: Boolean,
val mutable: Boolean,
val isChild: Boolean,
val strictCastInTransformChildren: Boolean,
) {
abstract val type: TypeRef
abstract val baseDefaultValue: CodeBlock?
@@ -95,9 +96,10 @@ class SingleField(
nullable: Boolean,
mutable: Boolean,
isChild: Boolean,
strictCastInTransformChildren: Boolean,
override val baseDefaultValue: CodeBlock?,
override val baseGetter: CodeBlock?,
) : Field(config, name, nullable, mutable, isChild) {
) : Field(config, name, nullable, mutable, isChild, strictCastInTransformChildren) {
override val transformable: Boolean
get() = mutable
}
@@ -110,10 +112,11 @@ class ListField(
nullable: Boolean,
mutable: Boolean,
isChild: Boolean,
strictCastInTransformChildren: Boolean,
override val transformable: Boolean,
override val baseDefaultValue: CodeBlock?,
override val baseGetter: CodeBlock?,
) : Field(config, name, nullable, mutable, isChild) {
) : Field(config, name, nullable, mutable, isChild, strictCastInTransformChildren) {
override val type: TypeRef
get() = listType.withArgs(elementType)
}
@@ -28,6 +28,7 @@ fun config2model(config: Config): Model {
fc.nullable,
fc.mutable,
fc.isChild,
fc.strictCastInTransformChildren,
fc.baseDefaultValue,
fc.baseGetter
)
@@ -44,6 +45,7 @@ fun config2model(config: Config): Model {
fc.nullable,
fc.mutability == ListFieldConfig.Mutability.Var,
fc.isChild,
fc.strictCastInTransformChildren,
fc.mutability != ListFieldConfig.Mutability.Immutable,
fc.baseDefaultValue,
fc.baseGetter
@@ -158,8 +158,13 @@ fun printElements(generationPath: File, model: Model) = sequence {
val elRef = child.type as ElementRef
if (!elRef.element.transform) {
append(" as")
if (child.nullable) append("?")
append(" %T")
if (child.strictCastInTransformChildren) {
append(" %T")
if (child.nullable) append("?")
} else {
if (child.nullable) append("?")
append(" %T")
}
args.add(elRef.toPoet())
}
}