Skip to content

Commit

Permalink
Fix generate of @const on function
Browse files Browse the repository at this point in the history
  • Loading branch information
louxiu committed Feb 3, 2018
1 parent 13c8c88 commit d229c4a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 38 deletions.
14 changes: 9 additions & 5 deletions src/main/java/org/bytedeco/javacpp/annotation/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.bytedeco.javacpp.FunctionPointer;
import org.bytedeco.javacpp.tools.Generator;

/**
* A shortcut annotation to {@link Cast} that simply adds {@code const} to the parameter type.
* Can also be declared on a {@link FunctionPointer} in the case of {@code const} functions.
* A shortcut annotation to {@link Cast} that simply adds {@code const} to the parameter type, function or class.
*
* @see Generator
* <p><ul>
* <li>For parameter type, the first one is for a value like {@code const char*} and the second for a pointer like {@code char const *}.
* <li>For function, the first and third one are used. The first is applied to the return value/pointer.
* The third one determines whether the function is {@code const} or not. For compatible problem, we keep the third value empty.
* <li>For class, only the first one is used, if it is {@code true}, it means the functions are all {@code const}
* </ul></p>
*
* @see {@link org.bytedeco.javacpp.tools.Generator}
*
* @author Samuel Audet
*/
Expand Down
76 changes: 49 additions & 27 deletions src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,6 @@

package org.bytedeco.javacpp.tools;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.bytedeco.javacpp.BoolPointer;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.CLongPointer;
Expand Down Expand Up @@ -90,6 +64,30 @@
import org.bytedeco.javacpp.annotation.ValueSetter;
import org.bytedeco.javacpp.annotation.Virtual;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
* The Generator is where all the C++ source code that we need gets generated.
* It has not been designed in any meaningful way since the requirements were
Expand Down Expand Up @@ -3210,6 +3208,27 @@ static String valueTypeName(String ... typeName) {
return type;
}

static boolean constFunction(Class<?> classType, Method functionMethod) {
if (classType.isAnnotationPresent(Const.class)) {
return true;
}

if (!functionMethod.isAnnotationPresent(Const.class)) {
return false;
}

for (Annotation a : functionMethod.getDeclaredAnnotations()) {
if (a instanceof Const) {
boolean[] b = ((Const) a).value();
if (b.length > 2 && b[2]) {
return true;
}
return false;
}
}
return false;
}

String[] cppAnnotationTypeName(Class<?> type, Annotation ... annotations) {
String[] typeName = cppCastTypeName(type, annotations);
String prefix = typeName[0];
Expand Down Expand Up @@ -3387,6 +3406,9 @@ String[] cppFunctionTypeName(Method... functionMethods) {
for (int j = namespace == null ? 0 : 1; j < parameterTypes.length; j++) {
String[] paramTypeName = cppAnnotationTypeName(parameterTypes[j], parameterAnnotations[j]);
AdapterInformation paramAdapterInfo = adapterInformation(false, valueTypeName(paramTypeName), parameterAnnotations[j]);
if (paramAdapterInfo != null && paramAdapterInfo.constant) {
suffix += "const ";
}
if (paramAdapterInfo != null && paramAdapterInfo.cast.length() > 0) {
suffix += paramAdapterInfo.cast + " arg" + j;
} else {
Expand All @@ -3398,7 +3420,7 @@ String[] cppFunctionTypeName(Method... functionMethods) {
}
suffix += ")";
}
if (type.isAnnotationPresent(Const.class)) {
if (constFunction(type, functionMethod)) {
suffix += " const";
}
return new String[] { prefix, suffix };
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

package org.bytedeco.javacpp.tools;

import org.bytedeco.javacpp.ClassProperties;
import org.bytedeco.javacpp.Loader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -32,8 +35,6 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.bytedeco.javacpp.ClassProperties;
import org.bytedeco.javacpp.Loader;

/**
* The Parser, just like the Generator, is a mess that is not meant to support the
Expand Down Expand Up @@ -1907,7 +1908,7 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti

// add @Virtual annotation on user request only, inherited through context
if (type.virtual && context.virtualize) {
modifiers = "@Virtual" + (decl.abstractMember ? "(true) " : " ")
modifiers = (decl.constMember ? "@Const({false, false, true}) " : " ") + "@Virtual" + (decl.abstractMember ? "(true) " : " ")
+ (context.inaccessible ? "protected native " : "public native ");
}

Expand Down Expand Up @@ -2733,7 +2734,7 @@ boolean group(Context context, DeclarationList declList) throws ParserException
String modifiers = "public static ", constructors = "";
boolean implicitConstructor = true, defaultConstructor = false, longConstructor = false,
pointerConstructor = false, abstractClass = info != null && info.purify && !ctx.virtualize,
havePureConst = false, haveVariables = false;
allPureConst = true, haveVariables = false;
for (Declaration d : declList2) {
if (d.declarator != null && d.declarator.type != null && d.declarator.type.using && decl.text != null) {
// inheriting constructors
Expand All @@ -2754,10 +2755,10 @@ boolean group(Context context, DeclarationList declList) throws ParserException
pointerConstructor |= paramDcls.length == 1 && paramDcls[0].type.javaName.equals("Pointer") && !d.inaccessible;
}
abstractClass |= d.abstractMember;
havePureConst |= d.constMember && d.abstractMember;
allPureConst &= d.constMember && d.abstractMember;
haveVariables |= d.variable;
}
if (havePureConst && ctx.virtualize) {
if (allPureConst && ctx.virtualize) {
modifiers = "@Const " + modifiers;
}
if (!anonymous) {
Expand Down

0 comments on commit d229c4a

Please sign in to comment.