[FIR generator] Specify Field.isChild in base tree configuration

instead of inside implementation configuration.

Whether a field is a child element is a core concept of the tree,
not a matter of particular implementation class.

It is especially visible in IR tree, where the base classes implement the
acceptChildren method. It will also be more relevant in the future.

This also aligns the config style between IR and FIR tree generators.

^KT-65773 In Progress
This commit is contained in:
Wojciech Litewka
2024-02-14 16:11:24 +01:00
committed by Space Team
parent 6d5b07ebe9
commit e737320d01
10 changed files with 41 additions and 53 deletions
@@ -66,10 +66,6 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
delegateCall = "shortName()" delegateCall = "shortName()"
withGetter = true withGetter = true
} }
default("delegate") {
isChild = false
}
} }
fun ImplementationContext.commonAnnotationConfig() { fun ImplementationContext.commonAnnotationConfig() {
@@ -87,16 +83,10 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
impl(annotationCall) { impl(annotationCall) {
commonAnnotationConfig() commonAnnotationConfig()
default("argumentMapping") {
isChild = false
}
} }
impl(errorAnnotationCall) { impl(errorAnnotationCall) {
commonAnnotationConfig() commonAnnotationConfig()
default("argumentMapping") {
isChild = false
}
default("annotationResolvePhase") { default("annotationResolvePhase") {
value = "FirAnnotationResolvePhase.Types" value = "FirAnnotationResolvePhase.Types"
} }
@@ -512,9 +502,6 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
impl(resolvedTypeRef) { impl(resolvedTypeRef) {
publicImplementation() publicImplementation()
default("delegatedTypeRef") {
isChild = false
}
} }
impl(errorExpression) { impl(errorExpression) {
@@ -528,7 +528,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
} }
resolvedImport.configure { resolvedImport.configure {
+field("delegate", import) +field("delegate", import, isChild = false)
+field("packageFqName", fqNameType) +field("packageFqName", fqNameType)
+field("relativeParentClassName", fqNameType, nullable = true) +field("relativeParentClassName", fqNameType, nullable = true)
+field("resolvedParentClassId", classIdType, nullable = true) +field("resolvedParentClassId", classIdType, nullable = true)
@@ -547,7 +547,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
} }
annotationCall.configure { annotationCall.configure {
+field("argumentMapping", annotationArgumentMapping, withReplace = true) +field("argumentMapping", annotationArgumentMapping, withReplace = true, isChild = false)
+field("annotationResolvePhase", annotationResolvePhaseType, withReplace = true) +field("annotationResolvePhase", annotationResolvePhaseType, withReplace = true)
+field("containingDeclarationSymbol", firBasedSymbolType.withArgs(TypeRef.Star)).apply { +field("containingDeclarationSymbol", firBasedSymbolType.withArgs(TypeRef.Star)).apply {
withBindThis = false withBindThis = false
@@ -555,7 +555,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
} }
errorAnnotationCall.configure { errorAnnotationCall.configure {
+field("argumentMapping", annotationArgumentMapping, withReplace = true) +field("argumentMapping", annotationArgumentMapping, withReplace = true, isChild = false)
} }
annotationArgumentMapping.configure { annotationArgumentMapping.configure {
@@ -721,7 +721,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
resolvedTypeRef.configure { resolvedTypeRef.configure {
+field("type", coneKotlinTypeType) +field("type", coneKotlinTypeType)
+field("delegatedTypeRef", typeRef, nullable = true) +field("delegatedTypeRef", typeRef, nullable = true, isChild = false)
element.otherParents.add(typeRefMarkerType) element.otherParents.add(typeRefMarkerType)
} }
@@ -10,11 +10,11 @@ import org.jetbrains.kotlin.generators.tree.*
// ----------- Simple field ----------- // ----------- Simple field -----------
fun field(name: String, type: TypeRefWithNullability, nullable: Boolean = false, withReplace: Boolean = false): Field { fun field(name: String, type: TypeRefWithNullability, nullable: Boolean = false, withReplace: Boolean = false): Field {
return SimpleField(name, type.copy(nullable), withReplace) return SimpleField(name, type.copy(nullable), withReplace = withReplace)
} }
fun field(type: ClassRef<*>, nullable: Boolean = false, withReplace: Boolean = false): Field { fun field(type: ClassRef<*>, nullable: Boolean = false, withReplace: Boolean = false): Field {
return SimpleField(type.simpleName.replaceFirstChar(Char::lowercaseChar), type.copy(nullable), withReplace) return SimpleField(type.simpleName.replaceFirstChar(Char::lowercaseChar), type.copy(nullable), withReplace = withReplace)
} }
fun booleanField(name: String, withReplace: Boolean = false): Field { fun booleanField(name: String, withReplace: Boolean = false): Field {
@@ -31,22 +31,23 @@ fun intField(name: String, withReplace: Boolean = false): Field {
// ----------- Fir field ----------- // ----------- Fir field -----------
fun field(name: String, element: ElementOrRef, nullable: Boolean = false, withReplace: Boolean = false): Field { fun field(name: String, element: ElementOrRef, nullable: Boolean = false, withReplace: Boolean = false, isChild: Boolean = true): Field {
return FirField(name, element.copy(nullable), withReplace) return FirField(name, element.copy(nullable), withReplace = withReplace, isChild = isChild)
} }
fun field(element: Element, nullable: Boolean = false, withReplace: Boolean = false): Field { fun field(element: Element, nullable: Boolean = false, withReplace: Boolean = false, isChild: Boolean = true): Field {
return FirField(element.name.replaceFirstChar(Char::lowercaseChar), element.copy(nullable), withReplace) return FirField(element.name.replaceFirstChar(Char::lowercaseChar), element.copy(nullable), withReplace = withReplace, isChild = isChild)
} }
// ----------- Field list ----------- // ----------- Field list -----------
fun fieldList(name: String, type: TypeRef, withReplace: Boolean = false, useMutableOrEmpty: Boolean = false): Field { fun fieldList(name: String, type: TypeRef, withReplace: Boolean = false, useMutableOrEmpty: Boolean = false, isChild: Boolean = true): Field {
return FieldList(name, type, withReplace, useMutableOrEmpty) return FieldList(name, type, withReplace = withReplace, isChild = isChild, useMutableOrEmpty = useMutableOrEmpty)
} }
fun fieldList(elementOrRef: ElementOrRef, withReplace: Boolean = false, useMutableOrEmpty: Boolean = false): Field { fun fieldList(elementOrRef: ElementOrRef, withReplace: Boolean = false, useMutableOrEmpty: Boolean = false, isChild: Boolean = true): Field {
return FieldList(elementOrRef.element.name.replaceFirstChar(Char::lowercaseChar) + "s", elementOrRef, withReplace, useMutableOrEmpty) val name = elementOrRef.element.name.replaceFirstChar(Char::lowercaseChar) + "s"
return FieldList(name, elementOrRef, withReplace = withReplace, isChild = isChild, useMutableOrEmpty = useMutableOrEmpty)
} }
// ----------- Field set ----------- // ----------- Field set -----------
@@ -71,6 +71,8 @@ class FieldWithDefault(override val origin: Field) : Field(), AbstractFieldWithD
override var withReplace: Boolean override var withReplace: Boolean
get() = origin.withReplace get() = origin.withReplace
set(_) {} set(_) {}
override val isChild: Boolean
get() = origin.isChild
override val containsElement: Boolean override val containsElement: Boolean
get() = origin.containsElement get() = origin.containsElement
override var needsSeparateTransform: Boolean override var needsSeparateTransform: Boolean
@@ -127,7 +129,6 @@ class FieldWithDefault(override val origin: Field) : Field(), AbstractFieldWithD
it.isMutable = isMutable it.isMutable = isMutable
it.withGetter = withGetter it.withGetter = withGetter
it.fromDelegate = fromDelegate it.fromDelegate = fromDelegate
it.isChild = isChild
} }
} }
} }
@@ -143,6 +144,9 @@ class SimpleField(
) : Field() { ) : Field() {
override var isMutable: Boolean = withReplace override var isMutable: Boolean = withReplace
override val isChild: Boolean
get() = false
override fun internalCopy(): Field { override fun internalCopy(): Field {
return SimpleField( return SimpleField(
name = name, name = name,
@@ -175,6 +179,7 @@ class FirField(
override val name: String, override val name: String,
val element: ElementRef, val element: ElementRef,
override var withReplace: Boolean, override var withReplace: Boolean,
override val isChild: Boolean,
) : Field() { ) : Field() {
override val typeRef: ElementRef override val typeRef: ElementRef
@@ -190,7 +195,8 @@ class FirField(
return FirField( return FirField(
name, name,
element, element,
withReplace withReplace,
isChild,
).apply { ).apply {
withBindThis = this@FirField.withBindThis withBindThis = this@FirField.withBindThis
} }
@@ -203,7 +209,8 @@ class FieldList(
override val name: String, override val name: String,
override val baseType: TypeRef, override val baseType: TypeRef,
override var withReplace: Boolean, override var withReplace: Boolean,
useMutableOrEmpty: Boolean = false override val isChild: Boolean,
useMutableOrEmpty: Boolean = false,
) : Field(), ListField { ) : Field(), ListField {
override var defaultValueInImplementation: String? = null override var defaultValueInImplementation: String? = null
@@ -225,6 +232,7 @@ class FieldList(
name, name,
baseType, baseType,
withReplace, withReplace,
isChild,
isMutableOrEmptyList isMutableOrEmptyList
) )
} }
@@ -78,8 +78,7 @@ abstract class AbstractTreeBuilder {
isChild: Boolean = true, isChild: Boolean = true,
initializer: SingleField.() -> Unit = {} initializer: SingleField.() -> Unit = {}
): SingleField { ): SingleField {
return SingleField(name, type.copy(nullable), mutable).apply { return SingleField(name, type.copy(nullable), mutable, isChild).apply {
this.isChild = isChild
initializer() initializer()
} }
} }
@@ -103,8 +102,8 @@ abstract class AbstractTreeBuilder {
listType = listType, listType = listType,
isNullable = nullable, isNullable = nullable,
mutable = mutability == ListField.Mutability.Var, mutable = mutability == ListField.Mutability.Var,
isChild = isChild,
).apply(initializer).apply { ).apply(initializer).apply {
this.isChild = isChild
initializer() initializer()
} }
} }
@@ -73,12 +73,13 @@ class SingleField(
name: String, name: String,
override var typeRef: TypeRefWithNullability, override var typeRef: TypeRefWithNullability,
mutable: Boolean, mutable: Boolean,
override val isChild: Boolean,
) : Field(name, mutable) { ) : Field(name, mutable) {
override fun replaceType(newType: TypeRefWithNullability) = override fun replaceType(newType: TypeRefWithNullability) =
SingleField(name, newType, isMutable).also(::updateFieldsInCopy) SingleField(name, newType, isMutable, isChild).also(::updateFieldsInCopy)
override fun internalCopy() = SingleField(name, typeRef, isMutable) override fun internalCopy() = SingleField(name, typeRef, isMutable, isChild)
} }
class ListField( class ListField(
@@ -87,6 +88,7 @@ class ListField(
private val isNullable: Boolean, private val isNullable: Boolean,
override val listType: ClassRef<PositionTypeParameterRef>, override val listType: ClassRef<PositionTypeParameterRef>,
mutable: Boolean, mutable: Boolean,
override val isChild: Boolean,
) : Field(name, mutable), AbstractListField { ) : Field(name, mutable), AbstractListField {
override val typeRef: ClassRef<PositionTypeParameterRef> override val typeRef: ClassRef<PositionTypeParameterRef>
@@ -94,7 +96,7 @@ class ListField(
override fun replaceType(newType: TypeRefWithNullability) = copy() override fun replaceType(newType: TypeRefWithNullability) = copy()
override fun internalCopy() = ListField(name, baseType, isNullable, listType, isMutable) override fun internalCopy() = ListField(name, baseType, isNullable, listType, isMutable, isChild)
enum class Mutability { enum class Mutability {
Var, Var,
@@ -64,7 +64,7 @@ abstract class AbstractField<Field : AbstractField<Field>> {
* *
* Only has effect if [containsElement] is `true`. * Only has effect if [containsElement] is `true`.
*/ */
var isChild: Boolean = true abstract val isChild: Boolean
open val overriddenTypes: MutableSet<TypeRefWithNullability> = mutableSetOf() open val overriddenTypes: MutableSet<TypeRefWithNullability> = mutableSetOf()
@@ -106,7 +106,6 @@ abstract class AbstractField<Field : AbstractField<Field>> {
copy.visibility = visibility copy.visibility = visibility
copy.fromParent = fromParent copy.fromParent = fromParent
copy.useInBaseTransformerDetection = useInBaseTransformerDetection copy.useInBaseTransformerDetection = useInBaseTransformerDetection
copy.isChild = isChild
copy.overriddenTypes += overriddenTypes copy.overriddenTypes += overriddenTypes
} }
} }
@@ -368,19 +368,10 @@ abstract class AbstractImplementationConfigurator<Implementation, Element, Imple
withGetter = true withGetter = true
} }
/**
* Whether this field semantically represents a reference to a child node of the tree.
*
* This has the effect of including or excluding this field from visiting it by visitors in the generated
* `acceptChildren` and `transformChildren` methods (child fields are always visited in those methods)
*/
var isChild: Boolean = true
fun applyConfiguration() { fun applyConfiguration() {
field.withGetter = withGetter field.withGetter = withGetter
field.customSetter = customSetter field.customSetter = customSetter
isMutable?.let { field.isMutable = it } isMutable?.let { field.isMutable = it }
field.isChild = isChild
when { when {
value != null -> field.defaultValueInImplementation = value value != null -> field.defaultValueInImplementation = value
@@ -61,8 +61,7 @@ abstract class AbstractSwiftIrTreeBuilder {
isChild: Boolean = true, isChild: Boolean = true,
initializer: SimpleField.() -> Unit = {} initializer: SimpleField.() -> Unit = {}
): SimpleField { ): SimpleField {
return SimpleField(name, type.copy(nullable), mutable).apply { return SimpleField(name, type.copy(nullable), mutable, isChild).apply {
this.isChild = isChild
initializer() initializer()
} }
} }
@@ -77,8 +76,8 @@ abstract class AbstractSwiftIrTreeBuilder {
name = name, name = name,
baseType = baseType, baseType = baseType,
isMutable = false, isMutable = false,
isChild = isChild,
).apply { ).apply {
this.isChild = isChild
initializer() initializer()
} }
} }
@@ -49,17 +49,19 @@ class SimpleField(
name: String, name: String,
override val typeRef: TypeRefWithNullability, override val typeRef: TypeRefWithNullability,
isMutable: Boolean, isMutable: Boolean,
override val isChild: Boolean,
) : Field(name, isMutable) { ) : Field(name, isMutable) {
override fun internalCopy() = SimpleField(name, typeRef, isMutable) override fun internalCopy() = SimpleField(name, typeRef, isMutable, isChild)
override fun replaceType(newType: TypeRefWithNullability) = SimpleField(name, newType, isMutable).also(::updateFieldsInCopy) override fun replaceType(newType: TypeRefWithNullability) = SimpleField(name, newType, isMutable, isChild).also(::updateFieldsInCopy)
} }
class ListField( class ListField(
name: String, name: String,
override val baseType: TypeRef, override val baseType: TypeRef,
isMutable: Boolean, isMutable: Boolean,
override val isChild: Boolean,
) : Field(name, isMutable), ListField { ) : Field(name, isMutable), ListField {
override val typeRef: ClassRef<PositionTypeParameterRef> override val typeRef: ClassRef<PositionTypeParameterRef>
@@ -68,7 +70,7 @@ class ListField(
override val listType: ClassRef<PositionTypeParameterRef> override val listType: ClassRef<PositionTypeParameterRef>
get() = StandardTypes.list get() = StandardTypes.list
override fun internalCopy() = ListField(name, baseType, isMutable) override fun internalCopy() = ListField(name, baseType, isMutable, isChild)
override fun replaceType(newType: TypeRefWithNullability) = internalCopy().also(::updateFieldsInCopy) override fun replaceType(newType: TypeRefWithNullability) = internalCopy().also(::updateFieldsInCopy)
} }