Skip to content

Commit

Permalink
[J2KT] Add missing KDoc comments in Names.kt and convert some functio…
Browse files Browse the repository at this point in the history
…ns to properties.

PiperOrigin-RevId: 585751755
  • Loading branch information
Googler authored and copybara-github committed Nov 27, 2023
1 parent 45b5a1a commit 578b271
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 46 additions & 20 deletions transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Names.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
/** A set of local names used in this type. */
internal val Type.localNamesSet: Set<String>
get() =
ktMembers
.mapNotNull { member ->
Expand All @@ -41,12 +42,15 @@ internal val Type.localNames: Set<String>
}
.toSet()

internal val CompilationUnit.topLevelQualifiedNames: Set<String>
/** A set of top-level qualified name strings in this compilation unit. */
internal val CompilationUnit.topLevelQualifiedNamesSet: Set<String>
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!!) {
Expand All @@ -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<String> = 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("_")
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 578b271

Please sign in to comment.