Skip to content

Commit

Permalink
[J2KT] Do not mangle @Kt-annotated members.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 699275536
  • Loading branch information
Googler authored and copybara-github committed Nov 22, 2024
1 parent 92281f9 commit a8e1403
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,9 @@ public boolean isKtProperty() {
return isField() || getKtInfo().isProperty();
}

public String getKtName() {
String ktName = getKtInfo().getName();
if (ktName != null) {
return ktName;
}
String name = getName();
return getKtInfo().isProperty() ? KtInfo.computePropertyName(name) : name;
@Nullable
public String getExplicitKtName() {
return getKtInfo().getName();
}

public boolean isKtDisabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.google.j2cl.transpiler.backend.kotlin

import com.google.j2cl.transpiler.ast.AstUtils
import com.google.j2cl.transpiler.ast.AstUtils.isJsEnumCustomValueField
import com.google.j2cl.transpiler.ast.FieldDescriptor
import com.google.j2cl.transpiler.ast.HasName
import com.google.j2cl.transpiler.ast.MemberDescriptor
Expand Down Expand Up @@ -156,8 +156,13 @@ internal data class Environment(

/** Kotlin mangled name for this member descriptor. */
fun ktMangledName(memberDescriptor: MemberDescriptor): String =
if (AstUtils.isJsEnumCustomValueField(memberDescriptor)) memberDescriptor.name!!
else memberDescriptor.ktName + ktNameSuffix(memberDescriptor)
memberDescriptor.explicitKtName
?: when {
isJsEnumCustomValueField(memberDescriptor) -> memberDescriptor.name!!
memberDescriptor.enclosingTypeDescriptor.typeDeclaration.isKtNative ->
memberDescriptor.ktName
else -> memberDescriptor.ktName + ktNameSuffix(memberDescriptor)
}

/** Kotlin name suffix for this member descriptor. */
private fun ktNameSuffix(memberDescriptor: MemberDescriptor): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ import com.google.j2cl.transpiler.ast.ArrayTypeDescriptor
import com.google.j2cl.transpiler.ast.CompilationUnit
import com.google.j2cl.transpiler.ast.DeclaredTypeDescriptor
import com.google.j2cl.transpiler.ast.FieldDescriptor
import com.google.j2cl.transpiler.ast.KtInfo.computePropertyName
import com.google.j2cl.transpiler.ast.MemberDescriptor
import com.google.j2cl.transpiler.ast.PrimitiveTypeDescriptor
import com.google.j2cl.transpiler.ast.PrimitiveTypes
import com.google.j2cl.transpiler.ast.Type
import com.google.j2cl.transpiler.ast.TypeDeclaration
import com.google.j2cl.transpiler.ast.TypeDescriptor
import com.google.j2cl.transpiler.backend.kotlin.common.letIf

internal val MemberDescriptor.ktName: String
get() = explicitKtName ?: name!!.letIf(isKtProperty) { computePropertyName(it) }

/** Map entry from simple name to qualified name. */
internal val TypeDeclaration.nameMapEntry: Pair<String, String>
Expand All @@ -48,7 +53,7 @@ internal val Type.localTypeNameMap: Map<String, String>

/** A set of field names used in this type. */
internal val Type.localFieldNames: Set<String>
get() = fields.map { it.descriptor.ktName!! }.toSet()
get() = fields.map { it.descriptor.ktName }.toSet()

/** A set of top-level qualified name strings in this compilation unit. */
internal val CompilationUnit.localTypeNames: Map<String, String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package collisions;

import javaemul.internal.annotations.KtName;
import javaemul.internal.annotations.KtNative;
import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsPackage;
Expand All @@ -34,8 +35,10 @@ class Blah {
}

class KtBlah {
@KtName("m")
static void m() {}

@KtName("getN")
static double getN() {
return 1.0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ open class KtBlah internal constructor() {
@ObjCName("J2ktCollisionsKtBlahCompanion", exact = true)
companion object {
@JvmStatic
internal fun m_pp_collisions() {}
internal fun m() {}

@JvmStatic
internal fun getN_pp_collisions(): Double {
internal fun getN(): Double {
return 1.0
}
}
Expand All @@ -58,8 +58,8 @@ open class bar internal constructor() {
val goog: Int = 0
val flip: Int = 0
val window: Int = 0
KtBlah.m_pp_collisions()
KtBlah.getN_pp_collisions()
KtBlah.m()
KtBlah.getN()
KtBlah()
collisions.goog()
collisions.foo()
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public void memberAccess() {
topLevel.instanceField = "foo";
Object fooStaticField = topLevel.staticField;
topLevel.staticField = "foo";
// TODO(b/380317304): Uncomment when fixed.
// topLevel.nonPublicField = 0;
topLevel.nonPublicField = 0;
int i1 = topLevel.fieldToRename;
int i2 = topLevel.methodToRename();
int i3 = topLevel.getMethodAsProperty();
Expand All @@ -45,8 +44,7 @@ public void memberAccess() {
boolean i6 = topLevel.isFieldToRename;
boolean i7 = topLevel.isMethodAsProperty();
int i8 = topLevel.getstartingmethodAsProperty();
// TODO(b/380317304): Uncomment when fixed.
// topLevel.nonPublicMethod();
topLevel.nonPublicMethod();

NativeTopLevel.Nested<String> nested = new NativeTopLevel.Nested<>("foo");
String nestedInstanceMethod = nested.instanceMethod("foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public KInner(I i) {}
public static Object staticField;
public int renamedField;

@KtName("nonPublicField")
int nonPublicField;

public int renamedMethod() {
Expand Down Expand Up @@ -92,6 +93,7 @@ public O instanceMethod(O o) {
return o;
}

@KtName("nonPublicMethod")
void nonPublicMethod() {}

public static <S> S staticMethod(S s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ open class Main {
topLevel!!.instanceField = "foo"
val fooStaticField: Any? = KTopLevel.staticField
KTopLevel.staticField = "foo"
topLevel!!.nonPublicField = 0
val i1: Int = topLevel!!.renamedField
val i2: Int = topLevel!!.renamedMethod()
val i3: Int = topLevel!!.methodAsProperty
Expand All @@ -81,6 +82,7 @@ open class Main {
val i6: Boolean = topLevel!!.isRenamedField
val i7: Boolean = topLevel!!.isMethodAsProperty
val i8: Int = topLevel!!.getstartingmethodAsProperty
topLevel!!.nonPublicMethod()
val nested: KNested<String?>? = KNested<String?>("foo")
val nestedInstanceMethod: String? = nested!!.instanceMethod("foo")
val nestedStaticMethod: String? = KNested.staticMethod<String?>("foo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ open class KTopLevel<O> {
var renamedField: Int = 0

@JvmField
internal var nonPublicField_pp_nativekttypes_nativekt: Int = 0
internal var nonPublicField: Int = 0

@ObjCName("renamedMethod")
open fun renamedMethod(): Int {
Expand Down Expand Up @@ -98,7 +98,7 @@ open class KTopLevel<O> {
return o
}

internal open fun nonPublicMethod_pp_nativekttypes_nativekt() {}
internal open fun nonPublicMethod() {}

@ObjCName("J2ktNativekttypesNativektKTopLevelCompanion", exact = true)
companion object {
Expand Down

0 comments on commit a8e1403

Please sign in to comment.