[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>> {
protected var packageFqn: FqName = FqName.ROOT
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
/**
* 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
protected var overridden: List<Declaration>? = null
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 fun createContainer() {
container = container?.let {
buildContainerSignature(it)
} ?: build()
reset(false)
}
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 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 localHash = hashId
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)
}
private fun createContainer() {
container = container?.let {
buildContainerSignature(it)
} ?: build()
reset(false)
}
private fun reportUnexpectedDescriptor(descriptor: DeclarationDescriptor) {
error("Unexpected descriptor $descriptor")
}
private fun setDescription(descriptor: DeclarationDescriptor) {
if (container != null) {
description = DescriptorRenderer.SHORT_NAMES_IN_TYPES.render(descriptor)
}
}
override fun renderDeclarationForDescription(declaration: DeclarationDescriptor): String =
DescriptorRenderer.SHORT_NAMES_IN_TYPES.render(declaration)
private fun collectParents(descriptor: DeclarationDescriptorNonRoot) {
descriptor.containingDeclaration.accept(this, null)
@@ -76,7 +65,9 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
collectParents(descriptor)
setHashIdAndDescriptionFor(descriptor, isPropertyAccessor = false)
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)
platformSpecificFunction(descriptor)
}
@@ -103,7 +94,7 @@ open class IdSignatureDescriptor(override val mangler: KotlinMangler.DescriptorM
}
}
setDescription(descriptor)
setDescriptionIfLocalDeclaration(descriptor)
setExpected(descriptor.isExpect)
platformSpecificClass(descriptor)
}
@@ -73,14 +73,6 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
d.acceptVoid(this)
}
private fun createContainer() {
container = container?.let {
buildContainerSignature(it)
} ?: build()
reset(false)
}
private fun collectParents(declaration: IrDeclarationWithName) {
declaration.parent.acceptVoid(this)
if (declaration !is IrClass || !declaration.isFacadeClass) {
@@ -88,17 +80,13 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
}
}
private fun setDescription(declaration: IrDeclaration) {
if (container != null) {
description = declaration.render()
}
}
override fun renderDeclarationForDescription(declaration: IrDeclaration): String = declaration.render()
override fun visitElement(element: IrElement) =
error("Unexpected element ${element.render()}")
override fun visitErrorDeclaration(declaration: IrErrorDeclaration) {
description = declaration.render()
description = renderDeclarationForDescription(declaration)
}
override fun visitPackageFragment(declaration: IrPackageFragment) {
@@ -115,7 +103,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
if (declaration.kind == ClassKind.ENUM_ENTRY) {
classFqnSegments.add(MangleConstant.ENUM_ENTRY_CLASS_NAME)
}
setDescription(declaration)
setDescriptionIfLocalDeclaration(declaration)
setExpected(declaration.isExpect)
}
@@ -132,7 +120,9 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
collectParents(declaration)
isTopLevelPrivate = isTopLevelPrivate || declaration.isTopLevelPrivate
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)
}
@@ -178,7 +168,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
} else {
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) {
@@ -189,7 +179,7 @@ class PublicIdSignatureComputer(val mangler: KotlinMangler.IrMangler) : IdSignat
prop.acceptVoid(this)
createContainer()
classFqnSegments.add(MangleConstant.BACKING_FIELD_NAME)
description = declaration.render()
setDescriptionIfLocalDeclaration(declaration)
} else {
collectParents(declaration)
setHashIdAndDescriptionFor(declaration, isPropertyAccessor = false)