Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down