Skip to content

Commit 8a0b08d

Browse files
committed
* Fix Generator for cases when a FunctionPointer returns another FunctionPointer
1 parent 57574c2 commit 8a0b08d

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
* Fix `Generator` for cases when a `FunctionPointer` returns another `FunctionPointer`
23
* Fix `Parser` failure with `auto` keyword of C++11 used as placeholder type specifier or for trailing return type ([issue #407](https://github.com/bytedeco/javacpp/issues/407))
34
* Add `Builder.configDirectory` option to let `Generator` output files that GraalVM needs for AOT compilation ([issue eclipse/deeplearning4j#7362](https://github.com/eclipse/deeplearning4j/issues/7362))
45
* Fix `Parser` error on `template<>` containing non-type parameters without names ([issue bytedeco/javacpp-presets#889](https://github.com/bytedeco/javacpp-presets/issues/889))

src/main/java/org/bytedeco/javacpp/tools/Generator.java

+30-15
Original file line numberDiff line numberDiff line change
@@ -1353,15 +1353,20 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
13531353
for (Class c : functions) {
13541354
String[] typeName = cppTypeName(c);
13551355
String[] returnConvention = typeName[0].split("\\(");
1356+
String[] returnType = {returnConvention[0] + (returnConvention.length > 2 ? "(*" : ""),
1357+
returnConvention.length > 2 ? ")(" + returnConvention[2] : ""};
1358+
if (returnConvention.length > 2) {
1359+
returnConvention = Arrays.copyOfRange(returnConvention, 2, returnConvention.length);
1360+
}
13561361
returnConvention[1] = constValueTypeName(returnConvention[1]);
13571362
String parameterDeclaration = typeName[1].substring(1);
13581363
String instanceTypeName = functionClassName(c);
13591364
out.println("struct JavaCPP_hidden " + instanceTypeName + " {");
13601365
out.println(" " + instanceTypeName + "() : ptr(NULL), obj(NULL) { }");
13611366
if (parameterDeclaration != null && parameterDeclaration.length() > 0) {
1362-
out.println(" " + returnConvention[0] + "operator()" + parameterDeclaration + ";");
1367+
out.println(" " + returnType[0] + "operator()" + parameterDeclaration + returnType[1] + ";");
13631368
}
1364-
out.println(" " + typeName[0] + "ptr" + typeName[1] + ";");
1369+
out.println(" " + returnType[0] + "(" + returnConvention[1] + "*ptr)" + parameterDeclaration + returnType[1] + ";");
13651370
out.println(" jobject obj; static jmethodID mid;");
13661371
out.println("};");
13671372
out.println("jmethodID " + instanceTypeName + "::mid = NULL;");
@@ -2772,6 +2777,11 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
27722777
String instanceTypeName = functionClassName(cls);
27732778
String[] callbackTypeName = cppFunctionTypeName(callbackMethod);
27742779
String[] returnConvention = callbackTypeName[0].split("\\(");
2780+
String[] returnType = {returnConvention[0] + (returnConvention.length > 2 ? "(*" : ""),
2781+
returnConvention.length > 2 ? ")(" + returnConvention[2] : ""};
2782+
if (returnConvention.length > 2) {
2783+
returnConvention = Arrays.copyOfRange(returnConvention, 2, returnConvention.length);
2784+
}
27752785
returnConvention[1] = constValueTypeName(returnConvention[1]);
27762786
String parameterDeclaration = callbackTypeName[1].substring(1);
27772787
String fieldName = mangle(callbackMethod.getName()) + "__" + mangle(signature(callbackMethod.getParameterTypes()));
@@ -2818,9 +2828,9 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
28182828
if (needUsing) {
28192829
member += usingLine + "\n ";
28202830
}
2821-
member += "virtual " + returnConvention[0] + (returnConvention.length > 1 ? returnConvention[1] : "")
2822-
+ methodInfo.memberName[0] + parameterDeclaration + " JavaCPP_override;\n "
2823-
+ returnConvention[0] + "super_" + methodInfo.name + nonconstParamDeclaration + " { ";
2831+
member += "virtual " + returnType[0] + (returnConvention.length > 1 ? returnConvention[1] : "")
2832+
+ methodInfo.memberName[0] + parameterDeclaration + returnType[1] + " JavaCPP_override;\n "
2833+
+ returnType[0] + "super_" + methodInfo.name + nonconstParamDeclaration + returnType[1] + " { ";
28242834
if (methodInfo.method.getAnnotation(Virtual.class).value()) {
28252835
member += "throw JavaCPP_exception(\"Cannot call pure virtual function " + valueTypeName + "::" + methodInfo.memberName[0] + "().\"); }";
28262836
} else {
@@ -2833,8 +2843,8 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
28332843
}
28342844
member += "); }";
28352845
}
2836-
firstLine = returnConvention[0] + (returnConvention.length > 1 ? returnConvention[1] : "")
2837-
+ subType + "::" + methodInfo.memberName[0] + parameterDeclaration + " {";
2846+
firstLine = returnType[0] + (returnConvention.length > 1 ? returnConvention[1] : "")
2847+
+ subType + "::" + methodInfo.memberName[0] + parameterDeclaration + returnType[1] + " {";
28382848
functionList.add(fieldName);
28392849
}
28402850
memberList.add(member);
@@ -2849,11 +2859,11 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
28492859
}
28502860
for (int i = 0; i < allocatorMax; i++) {
28512861
if (out2 != null) {
2852-
out2.println("JNIIMPORT " + returnConvention[0] + (returnConvention.length > 1 ?
2853-
returnConvention[1] : "") + callbackName + (i > 0 ? i : "") + parameterDeclaration + ";");
2862+
out2.println("JNIIMPORT " + returnType[0] + (returnConvention.length > 1 ?
2863+
returnConvention[1] : "") + callbackName + (i > 0 ? i : "") + parameterDeclaration + returnType[1] + ";");
28542864
}
2855-
out.println("JNIEXPORT " + returnConvention[0] + (returnConvention.length > 1 ?
2856-
returnConvention[1] : "") + callbackName + (i > 0 ? i : "") + parameterDeclaration + " {");
2865+
out.println("JNIEXPORT " + returnType[0] + (returnConvention.length > 1 ?
2866+
returnConvention[1] : "") + callbackName + (i > 0 ? i : "") + parameterDeclaration + returnType[1] + " {");
28572867
out.print((callbackReturnType != void.class ? " return " : " ") + instanceTypeName + "_instances[" + i + "](");
28582868
for (int j = 0; j < callbackParameterTypes.length; j++) {
28592869
out.print("arg" + j);
@@ -2870,8 +2880,8 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
28702880
out2.println("}");
28712881
}
28722882
}
2873-
out.println("static " + returnConvention[0] + "(" + (returnConvention.length > 1 ?
2874-
returnConvention[1] : "") + "*" + callbackName + "s[" + allocatorMax + "])" + parameterDeclaration + " = {");
2883+
out.println("static " + returnType[0] + "(" + (returnConvention.length > 1 ?
2884+
returnConvention[1] : "") + "*" + callbackName + "s[" + allocatorMax + "])" + parameterDeclaration + returnType[1] + " = {");
28752885
for (int i = 0; i < allocatorMax; i++) {
28762886
out.print(" " + callbackName + (i > 0 ? i : ""));
28772887
if (i + 1 < allocatorMax) {
@@ -2880,7 +2890,7 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
28802890
}
28812891
out.println(" };");
28822892

2883-
firstLine = returnConvention[0] + instanceTypeName + "::operator()" + parameterDeclaration + " {";
2893+
firstLine = returnType[0] + instanceTypeName + "::operator()" + parameterDeclaration + returnType[1] + " {";
28842894
}
28852895

28862896
if (!needDefinition) {
@@ -3160,6 +3170,11 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
31603170
")(rarg == NULL ? 0 : env->Get" + S + "Field(rarg, JavaCPP_" + s + "ValueFID));");
31613171
}
31623172
} else if (Pointer.class.isAssignableFrom(callbackReturnType)) {
3173+
if (FunctionPointer.class.isAssignableFrom(callbackReturnType)) {
3174+
functions.index(callbackReturnType);
3175+
returnTypeName[0] = functionClassName(callbackReturnType) + "*";
3176+
returnTypeName[1] = "";
3177+
}
31633178
out.println(" " + returnTypeName[0] + " rptr" + returnTypeName[1] + " = rarg == NULL ? NULL : (" +
31643179
returnTypeName[0] + returnTypeName[1] + ")jlong_to_ptr(env->GetLongField(rarg, JavaCPP_addressFID));");
31653180
if (returnAdapterInfo != null) {
@@ -3228,7 +3243,7 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
32283243
} else if (returnBy instanceof ByVal || returnBy instanceof ByRef) {
32293244
out.println(" if (rptr == NULL) {");
32303245
out.println(" JavaCPP_log(\"Return pointer address is NULL in callback for " + cls.getCanonicalName() + ".\");");
3231-
out.println(" static " + constValueTypeName(returnConvention[0].trim()) + " empty" + returnTypeName[1] + ";");
3246+
out.println(" static " + constValueTypeName((returnType[0] + returnType[1]).trim()) + " empty" + returnTypeName[1] + ";");
32323247
out.println(" return empty;");
32333248
out.println(" } else {");
32343249
out.println(" return *" + callbackReturnCast + "rptr;");

0 commit comments

Comments
 (0)