Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for name duplicates in containers #741

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

* Prevent `Parser` from producing duplicate declarations for basic containers ([pull #741](https://github.com/bytedeco/javacpp/pull/741))

### January 29, 2024 version 1.5.10
* Move native `Loader` methods to `Helper` class to avoid deadlocks ([issue #737](https://github.com/bytedeco/javacpp/issues/737))
* Fix `Parser` failing to pick up `Info` for constructors with template arguments ([pull #739](https://github.com/bytedeco/javacpp/pull/739))
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,19 @@ public Parser(Logger logger, Properties properties, String encoding, String line
/** Java names of classes recognized as polymorphic. */
Set<String> polymorphicClasses = new HashSet<>();

private void addDowncast(String base, Type from, boolean virtual) {
void addDowncast(String base, Type from, boolean virtual) {
Map<Type, Boolean> inheritance = downcasts.get(base);
if (inheritance == null) {
downcasts.put(base, inheritance = new HashMap<>(1));
}
inheritance.put(from, virtual);
}

/** De-deduplicate an array of strings, keeping order. */
static String[] removeDuplicates(String[] a) {
return a != null && a.length > 1 ? new LinkedHashSet<>(Arrays.asList(a)).toArray(new String[0]) : a;
}

static String removeAnnotations(String s) {
return s.substring(s.lastIndexOf(' ') + 1);
}
Expand Down Expand Up @@ -339,16 +344,16 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
+ " public " + containerType.javaName + "(Pointer p) { super(p); }\n";
boolean purify = info != null && info.purify;
if (!constant && !purify && (dim == 0 || (containerType.arguments.length == 1 && indexType != null)) && firstType != null && secondType != null) {
String[] firstNames = firstType.javaNames != null ? firstType.javaNames : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? secondType.javaNames : new String[] {secondType.javaName};
String[] firstNames = firstType.javaNames != null ? removeDuplicates(firstType.javaNames) : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? removeDuplicates(secondType.javaNames) : new String[] {secondType.javaName};
String brackets = arrayBrackets + (dim > 0 ? "[]" : "");
for (int n = 0; n < firstNames.length || n < secondNames.length; n++) {
decl.text += " public " + containerType.javaName + "(" + firstNames[Math.min(n, firstNames.length - 1)] + brackets + " firstValue, "
+ secondNames[Math.min(n, secondNames.length - 1)] + brackets + " secondValue) "
+ "{ this(" + (dim > 0 ? "Math.min(firstValue.length, secondValue.length)" : "") + "); put(firstValue, secondValue); }\n";
}
} else if (resizable && !purify && firstType == null && secondType == null) {
for (String javaName : valueType.javaNames != null ? valueType.javaNames : new String[] {valueType.javaName}) {
for (String javaName : valueType.javaNames != null ? removeDuplicates(valueType.javaNames) : new String[] {valueType.javaName}) {
if (dim < 2 && !javaName.equals("int") && !javaName.equals("long")) {
decl.text += " public " + containerType.javaName + "(" + javaName + " value) { this(1); put(0, value); }\n";
}
Expand All @@ -363,7 +368,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
valueNames2 += separator + "value" + n;
separator = ", ";
n++;
} else for (String javaName : type.javaNames != null ? type.javaNames : new String[] {type.javaName}) {
} else for (String javaName : type.javaNames != null ? removeDuplicates(type.javaNames) : new String[] {type.javaName}) {
// variant, optional, etc
if (!javaName.substring(javaName.indexOf(' ') + 1).equals("Pointer")) {
decl.text += " public " + containerType.javaName + "(" + javaName + " value) { this(); put(value); }\n";
Expand Down Expand Up @@ -416,6 +421,8 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
+ " public native " + containerType.javaName + " first(" + params + separator + removeAnnotations(firstType.javaName) + " first);\n"
+ " " + indexAnnotation + "public native " + secondType.annotations + secondType.javaName + " second(" + params + "); "
+ " public native " + containerType.javaName + " second(" + params + separator + removeAnnotations(secondType.javaName) + " second);\n";
firstType.javaNames = removeDuplicates(firstType.javaNames);
secondType.javaNames = removeDuplicates(secondType.javaNames);
for (int i = 1; !constant && firstType.javaNames != null && i < firstType.javaNames.length; i++) {
decl.text += " @MemberSetter @Index" + indexFunction + " public native " + containerType.javaName + " first(" + params + separator + firstType.annotations + firstType.javaNames[i] + " first);\n";
}
Expand All @@ -433,6 +440,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
if (!constant) {
decl.text += " public native " + containerType.javaName + " put(" + params + separator + removeAnnotations(valueType.javaName) + " value);\n";
}
valueType.javaNames = removeDuplicates(valueType.javaNames);
for (int i = 1; !constant && valueType.javaNames != null && i < valueType.javaNames.length; i++) {
decl.text += " @ValueSetter @Index" + indexFunction + " public native " + containerType.javaName + " put(" + params + separator + valueType.annotations + valueType.javaNames[i] + " value);\n";
}
Expand All @@ -452,6 +460,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
if (!constant && !tuple) {
decl.text += " @ValueSetter public native " + containerType.javaName + " put(" + type.annotations + type.javaName + " value);\n";
}
type.javaNames = removeDuplicates(type.javaNames);
for (int i = 1; !constant && !tuple && type.javaNames != null && i < type.javaNames.length; i++) {
decl.text += " @ValueSetter public native " + containerType.javaName + " put(" + type.annotations + type.javaNames[i] + " value);\n";
}
Expand Down Expand Up @@ -532,8 +541,8 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
}

if (!constant && (dim == 0 || (containerType.arguments.length == 1 && indexType != null)) && firstType != null && secondType != null) {
String[] firstNames = firstType.javaNames != null ? firstType.javaNames : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? secondType.javaNames : new String[] {secondType.javaName};
String[] firstNames = firstType.javaNames != null ? removeDuplicates(firstType.javaNames) : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? removeDuplicates(secondType.javaNames) : new String[] {secondType.javaName};
String brackets = arrayBrackets + (dim > 0 ? "[]" : "");
for (int n = 0; n < firstNames.length || n < secondNames.length; n++) {
String firstName = firstNames[Math.min(n, firstNames.length - 1)];
Expand Down Expand Up @@ -566,7 +575,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
}
} else if (resizable && firstType == null && secondType == null) {
boolean first = true;
for (String javaName : valueType.javaNames != null ? valueType.javaNames : new String[] {valueType.javaName}) {
for (String javaName : valueType.javaNames != null ? removeDuplicates(valueType.javaNames) : new String[] {valueType.javaName}) {
javaName = removeAnnotations(javaName);
decl.text += "\n";
if (dim < 2) {
Expand Down