diff --git a/translator/src/main/java/com/google/devtools/j2objc/gen/TypeDeclarationGenerator.java b/translator/src/main/java/com/google/devtools/j2objc/gen/TypeDeclarationGenerator.java index 0a8bd2979d..0b29e3c85f 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/gen/TypeDeclarationGenerator.java +++ b/translator/src/main/java/com/google/devtools/j2objc/gen/TypeDeclarationGenerator.java @@ -44,6 +44,7 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; +import java.util.stream.Collectors; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; @@ -905,6 +906,30 @@ protected void printFunctionDeclaration(FunctionDeclaration function) { print(" J2OBJC_TEXT_SEGMENT"); } println(";"); + + String swiftName = nameTable.getSwiftFunctionNameFromAnnotation(function); + if (swiftName != null && !swiftName.isEmpty()) { + printSwiftNameInlineWrapper(function, swiftName); + } + } + + private void printSwiftNameInlineWrapper(FunctionDeclaration function, String swiftName) { + String signature = getFunctionSignature(function, true, false); + String funcName = function.getName(); + String inlineSignature = signature.replace(funcName + "(", funcName + "_swiftName("); + + String args = + function.getParameters().stream() + .map(param -> nameTable.getVariableShortName(param.getVariableElement())) + .collect(Collectors.joining(", ")); + + boolean isVoid = TypeUtil.isVoid(function.getReturnType().getTypeMirror()); + String returnPrefix = isVoid ? "" : "return "; + String returnsRetainedAttr = function.returnsRetained() ? " NS_RETURNS_RETAINED" : ""; + + printf( + "\nNS_INLINE %s%s%s {\n %s%s(%s);\n}\n", + inlineSignature, returnsRetainedAttr, swiftName, returnPrefix, funcName, args); } protected void printNonnullAuditedRegion(AuditedRegion state) { diff --git a/translator/src/main/java/com/google/devtools/j2objc/gen/TypeGenerator.java b/translator/src/main/java/com/google/devtools/j2objc/gen/TypeGenerator.java index 296dbcbe7c..b0e4e6d06f 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/gen/TypeGenerator.java +++ b/translator/src/main/java/com/google/devtools/j2objc/gen/TypeGenerator.java @@ -452,14 +452,6 @@ protected String getFunctionSignature( } } sb.append(')'); - - if (!isPrivate) { - String swiftName = nameTable.getSwiftFunctionNameFromAnnotation(function); - if (swiftName != null) { - sb.append(swiftName); - } - } - return sb.toString(); } diff --git a/translator/src/test/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGeneratorTest.java b/translator/src/test/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGeneratorTest.java index ca1be71d99..7515e3ce14 100644 --- a/translator/src/test/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGeneratorTest.java +++ b/translator/src/test/java/com/google/devtools/j2objc/gen/ObjectiveCHeaderGeneratorTest.java @@ -1715,14 +1715,53 @@ public void testSwiftNameAnnotationWithStaticFunctions() throws IOException { + " }" + " public static String builderWithExpectedSize(int expectedSize){ return \"\"; }" + " public static String builderWithName(String name){ return \"\"; }" + + " public static void doReset() {}" + + " public static FooBar newFooBar() { return null; }" + "}"; String translation = translateSourceFile(sourceContent, "FooBar", "com/foo/bar/FooBar.h"); assertInTranslation(translation, "NS_SWIFT_NAME(FooBar.init())"); + + assertInTranslation( + translation, + "FOUNDATION_EXPORT NSString *ComFooBarFooBar_builderWithExpectedSizeWithInt_(int32_t" + + " expectedSize);"); assertInTranslation( - translation, "NS_SWIFT_NAME(FooBar.builderWithExpectedSize(expectedSize:))"); - assertInTranslation(translation, "NS_SWIFT_NAME(FooBar.builderWithName(name:))"); + translation, + "NS_INLINE NSString *ComFooBarFooBar_builderWithExpectedSizeWithInt__swiftName(int32_t" + + " expectedSize) NS_SWIFT_NAME(FooBar.builderWithExpectedSize(expectedSize:)) {\n" + + " return ComFooBarFooBar_builderWithExpectedSizeWithInt_(expectedSize);\n" + + "}"); + + assertInTranslation( + translation, + "FOUNDATION_EXPORT NSString *ComFooBarFooBar_builderWithNameWithNSString_(NSString" + + " *name);"); + assertInTranslation( + translation, + "NS_INLINE NSString *ComFooBarFooBar_builderWithNameWithNSString__swiftName(NSString *name)" + + " NS_SWIFT_NAME(FooBar.builderWithName(name:)) {\n" + + " return ComFooBarFooBar_builderWithNameWithNSString_(name);\n" + + "}"); + + assertInTranslation(translation, "FOUNDATION_EXPORT void ComFooBarFooBar_doReset(void);"); + assertInTranslation( + translation, + "NS_INLINE void ComFooBarFooBar_doReset_swiftName(void)" + + " NS_SWIFT_NAME(FooBar.doReset()) {\n" + + " ComFooBarFooBar_doReset();\n" + + "}"); + + assertInTranslation( + translation, "FOUNDATION_EXPORT ComFooBarFooBar *ComFooBarFooBar_newFooBar(void);"); + assertInTranslation( + translation, + "NS_INLINE ComFooBarFooBar *ComFooBarFooBar_newFooBar_swiftName(void)" + + " NS_SWIFT_NAME(FooBar.newFooBar()) {\n" + + " return ComFooBarFooBar_newFooBar();\n" + + "}"); } + public void testSwiftNameAnnotationWithStaticFunctionsWithWapperMethods() throws IOException { options.setEmitWrapperMethods(true);