[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()"
withGetter = true
}
default("delegate") {
isChild = false
}
}
fun ImplementationContext.commonAnnotationConfig() {
@@ -87,16 +83,10 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
impl(annotationCall) {
commonAnnotationConfig()
default("argumentMapping") {
isChild = false
}
}
impl(errorAnnotationCall) {
commonAnnotationConfig()
default("argumentMapping") {
isChild = false
}
default("annotationResolvePhase") {
value = "FirAnnotationResolvePhase.Types"
}
@@ -512,9 +502,6 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
impl(resolvedTypeRef) {
publicImplementation()
default("delegatedTypeRef") {
isChild = false
}
}
impl(errorExpression) {
@@ -528,7 +528,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
}
resolvedImport.configure {
+field("delegate", import)
+field("delegate", import, isChild = false)
+field("packageFqName", fqNameType)
+field("relativeParentClassName", fqNameType, nullable = true)
+field("resolvedParentClassId", classIdType, nullable = true)
@@ -547,7 +547,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
}
annotationCall.configure {
+field("argumentMapping", annotationArgumentMapping, withReplace = true)
+field("argumentMapping", annotationArgumentMapping, withReplace = true, isChild = false)
+field("annotationResolvePhase", annotationResolvePhaseType, withReplace = true)
+field("containingDeclarationSymbol", firBasedSymbolType.withArgs(TypeRef.Star)).apply {
withBindThis = false
@@ -555,7 +555,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
}
errorAnnotationCall.configure {
+field("argumentMapping", annotationArgumentMapping, withReplace = true)
+field("argumentMapping", annotationArgumentMapping, withReplace = true, isChild = false)
}
annotationArgumentMapping.configure {
@@ -721,7 +721,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
resolvedTypeRef.configure {
+field("type", coneKotlinTypeType)
+field("delegatedTypeRef", typeRef, nullable = true)
+field("delegatedTypeRef", typeRef, nullable = true, isChild = false)
element.otherParents.add(typeRefMarkerType)
}
@@ -10,11 +10,11 @@ import org.jetbrains.kotlin.generators.tree.*
// ----------- Simple 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 {
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 {
@@ -31,22 +31,23 @@ fun intField(name: String, withReplace: Boolean = false): Field {
// ----------- Fir field -----------
fun field(name: String, element: ElementOrRef, nullable: Boolean = false, withReplace: Boolean = false): Field {
return FirField(name, element.copy(nullable), withReplace)
fun field(name: String, element: ElementOrRef, nullable: Boolean = false, withReplace: Boolean = false, isChild: Boolean = true): Field {
return FirField(name, element.copy(nullable), withReplace = withReplace, isChild = isChild)
}
fun field(element: Element, nullable: Boolean = false, withReplace: Boolean = false): Field {
return FirField(element.name.replaceFirstChar(Char::lowercaseChar), element.copy(nullable), withReplace)
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 = withReplace, isChild = isChild)
}
// ----------- Field list -----------
fun fieldList(name: String, type: TypeRef, withReplace: Boolean = false, useMutableOrEmpty: Boolean = false): Field {
return FieldList(name, type, withReplace, useMutableOrEmpty)
fun fieldList(name: String, type: TypeRef, withReplace: Boolean = false, useMutableOrEmpty: Boolean = false, isChild: Boolean = true): Field {
return FieldList(name, type, withReplace = withReplace, isChild = isChild, useMutableOrEmpty = useMutableOrEmpty)
}
fun fieldList(elementOrRef: ElementOrRef, withReplace: Boolean = false, useMutableOrEmpty: Boolean = false): Field {
return FieldList(elementOrRef.element.name.replaceFirstChar(Char::lowercaseChar) + "s", elementOrRef, withReplace, useMutableOrEmpty)
fun fieldList(elementOrRef: ElementOrRef, withReplace: Boolean = false, useMutableOrEmpty: Boolean = false, isChild: Boolean = true): Field {
val name = elementOrRef.element.name.replaceFirstChar(Char::lowercaseChar) + "s"
return FieldList(name, elementOrRef, withReplace = withReplace, isChild = isChild, useMutableOrEmpty = useMutableOrEmpty)
}
// ----------- Field set -----------
@@ -71,6 +71,8 @@ class FieldWithDefault(override val origin: Field) : Field(), AbstractFieldWithD
override var withReplace: Boolean
get() = origin.withReplace
set(_) {}
override val isChild: Boolean
get() = origin.isChild
override val containsElement: Boolean
get() = origin.containsElement
override var needsSeparateTransform: Boolean
@@ -127,7 +129,6 @@ class FieldWithDefault(override val origin: Field) : Field(), AbstractFieldWithD
it.isMutable = isMutable
it.withGetter = withGetter
it.fromDelegate = fromDelegate
it.isChild = isChild
}
}
}
@@ -143,6 +144,9 @@ class SimpleField(
) : Field() {
override var isMutable: Boolean = withReplace
override val isChild: Boolean
get() = false
override fun internalCopy(): Field {
return SimpleField(
name = name,
@@ -175,6 +179,7 @@ class FirField(
override val name: String,
val element: ElementRef,
override var withReplace: Boolean,
override val isChild: Boolean,
) : Field() {
override val typeRef: ElementRef
@@ -190,7 +195,8 @@ class FirField(
return FirField(
name,
element,
withReplace
withReplace,
isChild,
).apply {
withBindThis = this@FirField.withBindThis
}
@@ -203,7 +209,8 @@ class FieldList(
override val name: String,
override val baseType: TypeRef,
override var withReplace: Boolean,
useMutableOrEmpty: Boolean = false
override val isChild: Boolean,
useMutableOrEmpty: Boolean = false,
) : Field(), ListField {
override var defaultValueInImplementation: String? = null
@@ -225,6 +232,7 @@ class FieldList(
name,
baseType,
withReplace,
isChild,
isMutableOrEmptyList
)
}
@@ -78,8 +78,7 @@ abstract class AbstractTreeBuilder {
isChild: Boolean = true,
initializer: SingleField.() -> Unit = {}
): SingleField {
return SingleField(name, type.copy(nullable), mutable).apply {
this.isChild = isChild
return SingleField(name, type.copy(nullable), mutable, isChild).apply {
initializer()
}
}
@@ -103,8 +102,8 @@ abstract class AbstractTreeBuilder {
listType = listType,
isNullable = nullable,
mutable = mutability == ListField.Mutability.Var,
isChild = isChild,
).apply(initializer).apply {
this.isChild = isChild
initializer()
}
}
@@ -73,12 +73,13 @@ class SingleField(
name: String,
override var typeRef: TypeRefWithNullability,
mutable: Boolean,
override val isChild: Boolean,
) : Field(name, mutable) {
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(
@@ -87,6 +88,7 @@ class ListField(
private val isNullable: Boolean,
override val listType: ClassRef<PositionTypeParameterRef>,
mutable: Boolean,
override val isChild: Boolean,
) : Field(name, mutable), AbstractListField {
override val typeRef: ClassRef<PositionTypeParameterRef>
@@ -94,7 +96,7 @@ class ListField(
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 {
Var,
@@ -64,7 +64,7 @@ abstract class AbstractField<Field : AbstractField<Field>> {
*
* Only has effect if [containsElement] is `true`.
*/
var isChild: Boolean = true
abstract val isChild: Boolean
open val overriddenTypes: MutableSet<TypeRefWithNullability> = mutableSetOf()
@@ -106,7 +106,6 @@ abstract class AbstractField<Field : AbstractField<Field>> {
copy.visibility = visibility
copy.fromParent = fromParent
copy.useInBaseTransformerDetection = useInBaseTransformerDetection
copy.isChild = isChild
copy.overriddenTypes += overriddenTypes
}
}
@@ -368,19 +368,10 @@ abstract class AbstractImplementationConfigurator<Implementation, Element, Imple
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() {
field.withGetter = withGetter
field.customSetter = customSetter
isMutable?.let { field.isMutable = it }
field.isChild = isChild
when {
value != null -> field.defaultValueInImplementation = value
@@ -61,8 +61,7 @@ abstract class AbstractSwiftIrTreeBuilder {
isChild: Boolean = true,
initializer: SimpleField.() -> Unit = {}
): SimpleField {
return SimpleField(name, type.copy(nullable), mutable).apply {
this.isChild = isChild
return SimpleField(name, type.copy(nullable), mutable, isChild).apply {
initializer()
}
}
@@ -77,8 +76,8 @@ abstract class AbstractSwiftIrTreeBuilder {
name = name,
baseType = baseType,
isMutable = false,
isChild = isChild,
).apply {
this.isChild = isChild
initializer()
}
}
@@ -49,17 +49,19 @@ class SimpleField(
name: String,
override val typeRef: TypeRefWithNullability,
isMutable: Boolean,
override val isChild: Boolean,
) : 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(
name: String,
override val baseType: TypeRef,
isMutable: Boolean,
override val isChild: Boolean,
) : Field(name, isMutable), ListField {
override val typeRef: ClassRef<PositionTypeParameterRef>
@@ -68,7 +70,7 @@ class ListField(
override val listType: ClassRef<PositionTypeParameterRef>
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)
}