Skip to content

Commit

Permalink
* Allow Parser to use Info.javaNames for function templates with…
Browse files Browse the repository at this point in the history
… parameters as well (issue #491)
  • Loading branch information
saudet committed Jun 8, 2021
1 parent b4cf59a commit b7dbf1c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
15 changes: 13 additions & 2 deletions src/main/java/org/bytedeco/javacpp/tools/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class Context {

/** Return all likely combinations of namespaces and template arguments for this C++ type */
String[] qualify(String cppName) {
return qualify(cppName, null);
}
/** or function, if parameters != null */
String[] qualify(String cppName, String parameters) {
if (cppName == null || cppName.length() == 0) {
return new String[0];
}
Expand All @@ -86,9 +90,16 @@ String[] qualify(String cppName) {
String ns = namespace != null ? namespace : "";
while (ns != null) {
String name = ns.length() > 0 ? ns + "::" + cppName : cppName;
if (parameters != null && name.endsWith(parameters)) {
name = name.substring(0, name.length() - parameters.length());
}
TemplateMap map = templateMap;
while (map != null) {
if (name.equals(map.getName())) {
String name2 = map.getName();
if (parameters != null && name2 != null && name2.endsWith(parameters)) {
name2 = name2.substring(0, name2.length() - parameters.length());
}
if (name.equals(name2)) {
String args = "<", separator = "";
for (Type t : map.values()) {
// assume that missing arguments have default values
Expand All @@ -97,7 +108,7 @@ String[] qualify(String cppName) {
separator = ",";
}
}
names.add(name + args + (args.endsWith(">") ? " >" : ">"));
names.add(name + args + (args.endsWith(">") ? " >" : ">") + (parameters != null ? parameters : ""));
break;
}
map = map.parent;
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/org/bytedeco/javacpp/tools/DeclarationList.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,29 @@ String rescan(String lines) {
}

@Override public boolean add(Declaration decl) {
return add(decl, null);
}
public boolean add(Declaration decl, String fullName) {
boolean add = true;
if (templateMap != null && templateMap.empty() && !decl.custom && (decl.type != null || decl.declarator != null)) {
// method templates cannot be declared in Java, but make sure to make their
// info available on request (when Info.javaNames is set) to be able to create instances
if (infoIterator == null) {
Type type = templateMap.type = decl.type;
Declarator dcl = templateMap.declarator = decl.declarator;
List<Info> infoList = infoMap.get(dcl != null ? dcl.cppName : type.cppName);
boolean hasJavaName = false;
for (Info info : infoList) {
hasJavaName |= info.javaNames != null && info.javaNames.length > 0;
}
if (!decl.function || hasJavaName) {
infoIterator = infoList.size() > 0 ? infoList.listIterator() : null;
for (String name : new String[] {fullName, dcl != null ? dcl.cppName : type.cppName}) {
if (name == null) {
continue;
}
List<Info> infoList = infoMap.get(name);
boolean hasJavaName = false;
for (Info info : infoList) {
hasJavaName |= info.javaNames != null && info.javaNames.length > 0;
}
if (!decl.function || hasJavaName) {
infoIterator = infoList.size() > 0 ? infoList.listIterator() : null;
break;
}
}
}
add = false;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/bytedeco/javacpp/tools/InfoMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ String normalize(String name, boolean unconst, boolean untemplate) {
name += " " + tokens[i].value;
}
} else if (untemplate) {
int count = 0, lastColon = -1, template = -1;
int count = 0, lastColon = -1, template = -1, parameters = -1;
for (int i = 0; i < n; i++) {
if (tokens[i].match('<')) {
count++;
Expand All @@ -228,8 +228,8 @@ String normalize(String name, boolean unconst, boolean untemplate) {
count++;
} else if (i > lastColon && tokens[i].match('>')) {
count--;
if (count == 0 && i + 1 != n) {
template = -1;
if (count == 0) {
parameters = i + 1;
}
}
}
Expand All @@ -238,6 +238,9 @@ String normalize(String name, boolean unconst, boolean untemplate) {
for (int i = 0; i < template; i++) {
name += tokens[i];
}
for (int i = parameters; i < n; i++) {
name += tokens[i].spacing + tokens[i];
}
}
}
if (unconst && foundConst) {
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2189,9 +2189,9 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti
separator = ", ";
}
}
info = fullInfo = infoMap.getFirst(fullname += ")");
info = fullInfo = infoMap.getFirst(fullname += ")", false);
if (info == null) {
info = infoMap.getFirst(fullname2 += ")");
info = infoMap.getFirst(fullname2 += ")", false);
}
}
if (info == null) {
Expand All @@ -2212,7 +2212,7 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti
info = infoMap.getFirst(dcl.cppName);
}
if (!type.constructor && !type.destructor && !type.operator && (context.templateMap == null || context.templateMap.full())) {
infoMap.put(info != null ? new Info(info).cppNames(fullname) : new Info(fullname));
infoMap.put(info != null ? new Info(info).cppNames(fullname).javaNames(null) : new Info(fullname));
}
}
String localName = dcl.cppName;
Expand Down Expand Up @@ -2341,11 +2341,28 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti
}

// use Java names that we may get here but that declarator() did not catch
if (fullInfo != null && fullInfo.javaNames != null && fullInfo.javaNames.length > 0 && !dcl.javaName.equals(fullInfo.javaNames[0])) {
String parameters = fullname.substring(dcl.cppName.length());
for (String name : context.qualify(dcl.cppName, parameters)) {
if ((infoMap.getFirst(name, false)) != null) {
dcl.cppName = name;
break;
} else if (infoMap.getFirst(name) != null) {
dcl.cppName = name;
}
}
String localName2 = dcl.cppName;
if (context.namespace != null && localName2.startsWith(context.namespace + "::")) {
localName2 = dcl.cppName.substring(context.namespace.length() + 2);
}
if (localName2.endsWith(parameters)) {
localName2 = localName2.substring(0, localName2.length() - parameters.length());
}
if (fullInfo != null && fullInfo.javaNames != null && fullInfo.javaNames.length > 0) {
dcl.javaName = fullInfo.javaNames[0];
dcl.signature = dcl.javaName + dcl.parameters.signature;
if (!localName.equals(dcl.javaName) && !type.annotations.contains("@Name(")) {
type.annotations += "@Name(\"" + localName + "\") ";
if (!localName2.equals(dcl.javaName) && (!localName2.contains("::") || context.javaName == null)) {
type.annotations = type.annotations.replaceAll("@Name\\(.*\\) ", "");
type.annotations += "@Name(\"" + localName2 + "\") ";
}
}

Expand Down Expand Up @@ -2461,7 +2478,7 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti
found |= dcl.signature.equals(d.signature);
}
if (dcl.javaName.length() > 0 && !found && (!type.destructor || (info != null && info.javaText != null))) {
if (declList.add(decl)) {
if (declList.add(decl, fullname)) {
first = false;
}
if (type.virtual && context.virtualize) {
Expand Down

0 comments on commit b7dbf1c

Please sign in to comment.