From 578b2715928467af4312b13e623332a07f40a9c3 Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 27 Nov 2023 13:55:35 -0800 Subject: [PATCH] [J2KT] Add missing KDoc comments in Names.kt and convert some functions to properties. PiperOrigin-RevId: 585751755 --- .../backend/kotlin/KotlinGeneratorStage.kt | 2 +- .../j2cl/transpiler/backend/kotlin/Names.kt | 66 +++++++++++++------ .../backend/kotlin/TypeDescriptorRenderer.kt | 4 +- .../transpiler/backend/kotlin/TypeRenderer.kt | 2 +- 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/KotlinGeneratorStage.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/KotlinGeneratorStage.kt index bf342562ac..26ad3c29ff 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/KotlinGeneratorStage.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/KotlinGeneratorStage.kt @@ -76,7 +76,7 @@ class KotlinGeneratorStage(private val output: OutputUtils.Output, private val p Renderer( environment, problems, - topLevelQualifiedNames = compilationUnit.topLevelQualifiedNames + topLevelQualifiedNames = compilationUnit.topLevelQualifiedNamesSet ) // Render types, collecting qualified names to import diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Names.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Names.kt index 28cf323063..c6ab92c919 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Names.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Names.kt @@ -29,7 +29,8 @@ import com.google.j2cl.transpiler.ast.TypeDescriptor import com.google.j2cl.transpiler.ast.Visibility import com.google.j2cl.transpiler.backend.kotlin.ast.Member -internal val Type.localNames: Set +/** A set of local names used in this type. */ +internal val Type.localNamesSet: Set get() = ktMembers .mapNotNull { member -> @@ -41,12 +42,15 @@ internal val Type.localNames: Set } .toSet() -internal val CompilationUnit.topLevelQualifiedNames: Set +/** A set of top-level qualified name strings in this compilation unit. */ +internal val CompilationUnit.topLevelQualifiedNamesSet: Set get() = types.map { it.declaration }.filter { !it.isKtNative }.map { it.ktQualifiedName }.toSet() +/** Kotlin mangled name for this member descriptor. */ internal val MemberDescriptor.ktMangledName: String get() = ktName + ktNameSuffix +/** Kotlin name suffix for this member descriptor. */ private val MemberDescriptor.ktNameSuffix: String get() = when (visibility!!) { @@ -58,54 +62,76 @@ private val MemberDescriptor.ktNameSuffix: String "_private_${enclosingTypeDescriptor.typeDeclaration.privateMemberSuffix}" } +/** Kotlin property name suffix for this member descriptor. */ private val MemberDescriptor.ktPropertyNameSuffix: String get() = if (this is FieldDescriptor && hasConflictingKtProperty) "_ktPropertyConflict" else "" +/** Whether this field descriptor has property with conflicting name in Kotlin. */ private val FieldDescriptor.hasConflictingKtProperty: Boolean get() = enclosingTypeDescriptor.polymorphicMethods.any { it.isKtProperty && it.ktName == ktName } +/** A suffix for private members in this type declaration. */ private val TypeDeclaration.privateMemberSuffix: String get() = if (isInterface) mangledName else "$classHierarchyDepth" +/** Original qualified name of this type declaration. */ private val TypeDeclaration.originalQualifiedName: String get() = if (isLocal) originalSimpleSourceName!! else qualifiedSourceName +/** Kotlin qualified name for this type declaration. */ internal val TypeDeclaration.ktQualifiedName: String get() = ktNativeQualifiedName ?: originalQualifiedName +/** Kotlin qualified name for this type declaration when used as a super-type. */ internal val TypeDeclaration.ktQualifiedNameAsSuperType: String get() = ktBridgeQualifiedName ?: ktQualifiedName +/** Kotlin simple name for this type declaration. */ internal val TypeDeclaration.ktSimpleName: String get() = ktQualifiedName.qualifiedNameToSimpleName() +/** + * Kotlin qualified name for this type declaration. + * + * @param asSuperType whether to use bridge name for super-type if present. + */ internal fun TypeDeclaration.ktQualifiedName(asSuperType: Boolean = false) = if (asSuperType) ktQualifiedNameAsSuperType else ktQualifiedName +/** + * Kotlin simple name for this type declaration. + * + * @param asSuperType whether to use bridge name for super-type if present. + */ internal fun TypeDeclaration.ktSimpleName(asSuperType: Boolean = false) = ktQualifiedName(asSuperType = asSuperType).qualifiedNameToSimpleName() -internal fun TypeDescriptor.ktQualifiedName(): String = - when (this) { - is PrimitiveTypeDescriptor -> toBoxedType().ktQualifiedName() - is ArrayTypeDescriptor -> - when (componentTypeDescriptor!!) { - PrimitiveTypes.BOOLEAN -> "kotlin.BooleanArray" - PrimitiveTypes.CHAR -> "kotlin.CharArray" - PrimitiveTypes.BYTE -> "kotlin.ByteArray" - PrimitiveTypes.SHORT -> "kotlin.ShortArray" - PrimitiveTypes.INT -> "kotlin.IntArray" - PrimitiveTypes.LONG -> "kotlin.LongArray" - PrimitiveTypes.FLOAT -> "kotlin.FloatArray" - PrimitiveTypes.DOUBLE -> "kotlin.DoubleArray" - else -> "kotlin.Array" - } - is DeclaredTypeDescriptor -> typeDeclaration.ktQualifiedName - else -> null - } ?: error("$this.ktQualifiedName()") +/** Kotlin qualified name for this type declaration. */ +internal val TypeDescriptor.ktQualifiedName: String + get() = + when (this) { + is PrimitiveTypeDescriptor -> toBoxedType().ktQualifiedName + is ArrayTypeDescriptor -> + when (componentTypeDescriptor!!) { + PrimitiveTypes.BOOLEAN -> "kotlin.BooleanArray" + PrimitiveTypes.CHAR -> "kotlin.CharArray" + PrimitiveTypes.BYTE -> "kotlin.ByteArray" + PrimitiveTypes.SHORT -> "kotlin.ShortArray" + PrimitiveTypes.INT -> "kotlin.IntArray" + PrimitiveTypes.LONG -> "kotlin.LongArray" + PrimitiveTypes.FLOAT -> "kotlin.FloatArray" + PrimitiveTypes.DOUBLE -> "kotlin.DoubleArray" + else -> "kotlin.Array" + } + is DeclaredTypeDescriptor -> typeDeclaration.ktQualifiedName + else -> null + } ?: error("$this.ktQualifiedName()") +/** A list of components for this qualified name string. */ internal fun String.qualifiedNameComponents(): List = split(".") +/** Simple name for this qualified name string. */ internal fun String.qualifiedNameToSimpleName(): String = qualifiedNameComponents().last() +/** Alias for this qualified name string. */ internal fun String.qualifiedNameToAlias(): String = qualifiedNameComponents().joinToString("_") diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeDescriptorRenderer.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeDescriptorRenderer.kt index 37215ac5a4..d15338bb5c 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeDescriptorRenderer.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeDescriptorRenderer.kt @@ -94,10 +94,10 @@ internal fun Renderer.qualifiedNameSource( qualifiedNameSource(enclosingTypeDescriptor), identifierSource(typeDeclaration.ktSimpleName()) ) - else -> topLevelQualifiedNameSource(typeDescriptor.ktQualifiedName()) + else -> topLevelQualifiedNameSource(typeDescriptor.ktQualifiedName) } } else { - topLevelQualifiedNameSource(typeDescriptor.ktQualifiedName()) + topLevelQualifiedNameSource(typeDescriptor.ktQualifiedName) } /** diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeRenderer.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeRenderer.kt index abac226ef0..db0df6c637 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeRenderer.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/TypeRenderer.kt @@ -175,7 +175,7 @@ internal fun Renderer.forTypeBody(type: Type): Renderer = copy( currentType = type, renderThisReferenceWithLabel = false, - localNames = localNames + type.localNames + localNames = localNames + type.localNamesSet ) private fun Renderer.enumValuesSource(type: Type): Source =