[klib] Move some duplicated code from subclasses to IdSignatureBuilder

This commit is contained in:
Sergej Jaskiewicz
2023-07-24 14:01:35 +02:00
committed by Space Team
parent ce6e904b70
commit b57b4e055e
3 changed files with 52 additions and 34 deletions
@@ -13,13 +13,50 @@ import org.jetbrains.kotlin.name.FqName
abstract class IdSignatureBuilder<Declaration : Any, Mangler : KotlinMangler<Declaration>> { abstract class IdSignatureBuilder<Declaration : Any, Mangler : KotlinMangler<Declaration>> {
protected var packageFqn: FqName = FqName.ROOT protected var packageFqn: FqName = FqName.ROOT
protected val classFqnSegments = mutableListOf<String>() protected val classFqnSegments = mutableListOf<String>()
/**
* Use [setHashIdAndDescriptionFor] or [setHashIdAndDescription] with `isPropertyAccessor = false` to set this property.
*
* This property is made private to enforce always setting [description] along with it.
*/
private var hashId: Long? = null private var hashId: Long? = null
/**
* Use [setHashIdAndDescriptionFor] or [setHashIdAndDescription] with `isPropertyAccessor = true` to set this property.
*
* This property is made private to enforce always setting [description] along with it.
*/
private var hashIdAcc: Long? = null private var hashIdAcc: Long? = null
protected var overridden: List<Declaration>? = null protected var overridden: List<Declaration>? = null
protected var mask = 0L protected var mask = 0L
/**
* For local or top-level private declarations, the signature of the containing declaration **or** [IdSignature.FileSignature]
* respectively.
*
* Used to build [IdSignature.CompositeSignature].
*/
protected var container: IdSignature? = null protected var container: IdSignature? = null
protected fun createContainer() {
container = container?.let {
buildContainerSignature(it)
} ?: build()
reset(false)
}
protected var description: String? = null protected var description: String? = null
protected abstract fun renderDeclarationForDescription(declaration: Declaration): String
protected fun setDescriptionIfLocalDeclaration(declaration: Declaration) {
if (container != null) {
description = renderDeclarationForDescription(declaration)
}
}
protected var isTopLevelPrivate: Boolean = false protected var isTopLevelPrivate: Boolean = false
protected abstract val currentFileSignature: IdSignature.FileSignature? protected abstract val currentFileSignature: IdSignature.FileSignature?
@@ -59,7 +96,7 @@ abstract class IdSignatureBuilder<Declaration : Any, Mangler : KotlinMangler<Dec
} }
protected fun buildContainerSignature(container: IdSignature): IdSignature.CompositeSignature { private fun buildContainerSignature(container: IdSignature): IdSignature.CompositeSignature {
val localName = classFqnSegments.joinToString(".") val localName = classFqnSegments.joinToString(".")
val localHash = hashId val localHash = hashId
return IdSignature.CompositeSignature(container, IdSignature.LocalSignature(localName, localHash, description)) return IdSignature.CompositeSignature(container, IdSignature.LocalSignature(localName, localHash, description))
@@ -30,23 +30,12 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
d.accept(this, null) d.accept(this, null)
} }
private fun createContainer() {
container = container?.let {
buildContainerSignature(it)
} ?: build()
reset(false)
}
private fun reportUnexpectedDescriptor(descriptor: DeclarationDescriptor) { private fun reportUnexpectedDescriptor(descriptor: DeclarationDescriptor) {
error("Unexpected descriptor $descriptor") error("Unexpected descriptor $descriptor")
} }
private fun setDescription(descriptor: DeclarationDescriptor) { override fun renderDeclarationForDescription(declaration: DeclarationDescriptor): String =
if (container != null) { DescriptorRenderer.SHORT_NAMES_IN_TYPES.render(declaration)
description = DescriptorRenderer.SHORT_NAMES_IN_TYPES.render(descriptor)
}
}
private fun collectParents(descriptor: DeclarationDescriptorNonRoot) { private fun collectParents(descriptor: DeclarationDescriptorNonRoot) {
descriptor.containingDeclaration.accept(this, null) descriptor.containingDeclaration.accept(this, null)
@@ -76,7 +65,9 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
collectParents(descriptor) collectParents(descriptor)
setHashIdAndDescriptionFor(descriptor, isPropertyAccessor = false) setHashIdAndDescriptionFor(descriptor, isPropertyAccessor = false)
isTopLevelPrivate = isTopLevelPrivate or descriptor.isTopLevelPrivate isTopLevelPrivate = isTopLevelPrivate or descriptor.isTopLevelPrivate
setDescription(descriptor)
// If this is a local function, overwrite `description` with the descriptor's rendered form.
setDescriptionIfLocalDeclaration(descriptor)
setExpected(descriptor.isExpect) setExpected(descriptor.isExpect)
platformSpecificFunction(descriptor) platformSpecificFunction(descriptor)
} }
@@ -103,7 +94,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
} }
} }
setDescription(descriptor) setDescriptionIfLocalDeclaration(descriptor)
setExpected(descriptor.isExpect) setExpected(descriptor.isExpect)
platformSpecificClass(descriptor) platformSpecificClass(descriptor)
} }
@@ -73,14 +73,6 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
d.acceptVoid(this) d.acceptVoid(this)
} }
private fun createContainer() {
container = container?.let {
buildContainerSignature(it)
} ?: build()
reset(false)
}
private fun collectParents(declaration: IrDeclarationWithName) { private fun collectParents(declaration: IrDeclarationWithName) {
declaration.parent.acceptVoid(this) declaration.parent.acceptVoid(this)
if (declaration !is IrClass || !declaration.isFacadeClass) { if (declaration !is IrClass || !declaration.isFacadeClass) {
@@ -88,17 +80,13 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
} }
} }
private fun setDescription(declaration: IrDeclaration) { override fun renderDeclarationForDescription(declaration: IrDeclaration): String = declaration.render()
if (container != null) {
description = declaration.render()
}
}
override fun visitElement(element: IrElement) = override fun visitElement(element: IrElement) =
error("Unexpected element ${element.render()}") error("Unexpected element ${element.render()}")
override fun visitErrorDeclaration(declaration: IrErrorDeclaration) { override fun visitErrorDeclaration(declaration: IrErrorDeclaration) {
description = declaration.render() description = renderDeclarationForDescription(declaration)
} }
override fun visitPackageFragment(declaration: IrPackageFragment) { override fun visitPackageFragment(declaration: IrPackageFragment) {
@@ -115,7 +103,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
if (declaration.kind == ClassKind.ENUM_ENTRY) { if (declaration.kind == ClassKind.ENUM_ENTRY) {
classFqnSegments.add(MangleConstant.ENUM_ENTRY_CLASS_NAME) classFqnSegments.add(MangleConstant.ENUM_ENTRY_CLASS_NAME)
} }
setDescription(declaration) setDescriptionIfLocalDeclaration(declaration)
setExpected(declaration.isExpect) setExpected(declaration.isExpect)
} }
@@ -132,7 +120,9 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
collectParents(declaration) collectParents(declaration)
isTopLevelPrivate = isTopLevelPrivate || declaration.isTopLevelPrivate isTopLevelPrivate = isTopLevelPrivate || declaration.isTopLevelPrivate
setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false) setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false)
setDescription(declaration)
// If this is a local function, overwrite `description` with the IR function's rendered form.
setDescriptionIfLocalDeclaration(declaration)
} }
setExpected(declaration.isExpect) setExpected(declaration.isExpect)
} }
@@ -178,7 +168,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
} else { } else {
classFqnSegments.add(MangleConstant.TYPE_PARAMETER_MARKER_NAME) classFqnSegments.add(MangleConstant.TYPE_PARAMETER_MARKER_NAME)
} }
setHashIdAndDescription(declaration.index.toLong(), declaration.render(), isPropertyAccessor = false) setHashIdAndDescription(declaration.index.toLong(), renderDeclarationForDescription(declaration), isPropertyAccessor = false)
} }
override fun visitField(declaration: IrField) { override fun visitField(declaration: IrField) {
@@ -189,7 +179,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
prop.acceptVoid(this) prop.acceptVoid(this)
createContainer() createContainer()
classFqnSegments.add(MangleConstant.BACKING_FIELD_NAME) classFqnSegments.add(MangleConstant.BACKING_FIELD_NAME)
description = declaration.render() setDescriptionIfLocalDeclaration(declaration)
} else { } else {
collectParents(declaration) collectParents(declaration)
setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false) setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false)