Skip to content

Commit

Permalink
* Have Parser desugar ... varargs to array [] for return types…
Browse files Browse the repository at this point in the history
… as well (pull #682)
  • Loading branch information
HGuillemet committed May 21, 2023
1 parent bf5e5da commit 4c40a56
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Have `Parser` desugar `...` varargs to array `[]` for return types as well ([pull #682](https://github.com/bytedeco/javacpp/pull/682))
* Fix `Parser` failing on some `friend` functions for `operator` overloading ([pull #681](https://github.com/bytedeco/javacpp/pull/681))
* Fix `Parser` incorrectly casting `const` pointers to template arguments of pointer types ([pull #677](https://github.com/bytedeco/javacpp/pull/677))
* Fix `Parser` with `Info.enumerate` failing to translate `enum` values based on other `enum` values
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ static String removeAnnotations(String s) {
return s.substring(s.lastIndexOf(' ') + 1);
}

static String desugarVarargs(String s) {
return s.trim().endsWith("...") ? s.trim().substring(0, s.length() - 3) + "[]" : s;
}

static String upcastMethodName(String javaName) {
String shortName = javaName.substring(javaName.lastIndexOf('.') + 1);
return "as" + Character.toUpperCase(shortName.charAt(0)) + shortName.substring(1);
Expand Down Expand Up @@ -2519,7 +2523,7 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti
extraDecl = new Declaration();
extraDecl.signature = signature;
extraDecl.declarator = dcl; // Used in group to recognize friends
extraDecl.text = "public " + dcl.type.javaName + " " + dcl.javaName + "(" + argList + ") { "
extraDecl.text = "public " + desugarVarargs(dcl.type.javaName) + " " + dcl.javaName + "(" + argList + ") { "
+ (dcl.type.javaName.equals("void") ? "" : "return ") + dcl.javaName + "(" + staticArgList + "); }\n";
} else {
friendly = false;
Expand Down Expand Up @@ -2597,13 +2601,13 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti
sb.append(removeAnnotations(param.type.javaName)).append(" ").append(param.javaName);
}
decl.text += accessModifier + " " + (staticMethod ? "static " : "")
+ removeAnnotations(type.javaName) + " " + dcl.javaName + "(" + sb + ") { "
+ desugarVarargs(removeAnnotations(type.javaName)) + " " + dcl.javaName + "(" + sb + ") { "
+ (type.javaName.equals("void") ? "" : "return ")
+ (context.upcast && !staticMethod ? upcastMethodName(context.javaName) + "()." : "")
+ "_" + dcl.javaName + (dcl.parameters.names == null ? "()" : dcl.parameters.names) + "; }\n";
dcl.javaName = "_" + dcl.javaName;
}
decl.text += modifiers2 + type.annotations + context.shorten(type.javaName) + " " + dcl.javaName + dcl.parameters.list + ";\n";
decl.text += modifiers2 + type.annotations + context.shorten(desugarVarargs(type.javaName)) + " " + dcl.javaName + dcl.parameters.list + ";\n";
}
decl.signature = dcl.signature;

Expand Down

0 comments on commit 4c40a56

Please sign in to comment.