41package com.jogamp.gluegen;
43import com.jogamp.gluegen.JavaConfiguration.JavaCallbackInfo;
44import com.jogamp.gluegen.cgram.HeaderParser;
45import com.jogamp.gluegen.cgram.types.AliasedSymbol;
46import com.jogamp.gluegen.cgram.types.ArrayType;
47import com.jogamp.gluegen.cgram.types.EnumType;
48import com.jogamp.gluegen.cgram.types.FunctionSymbol;
49import com.jogamp.gluegen.cgram.types.Type;
51import java.io.PrintWriter;
52import java.text.MessageFormat;
53import java.util.Iterator;
74 private final String runtimeExceptionType;
75 private final String unsupportedExceptionType;
76 private final boolean useNIOOnly;
77 private final boolean isNativeMethod;
78 private final boolean isUnimplemented;
80 private boolean emitBody;
81 private boolean eraseBufferAndArrayTypes;
82 private boolean isPrivateNativeMethod;
83 private boolean forDirectBufferImplementation;
84 private boolean forIndirectBufferAndArrayImplementation;
95 private String returnedArrayLengthExpression;
96 private boolean returnedArrayLengthExpressionOnlyForComments =
false;
102 private static final String COMPOUND_ARRAY_SUFFIX =
"_buf_array_copy";
106 final String runtimeExceptionType,
107 final String unsupportedExceptionType,
108 final boolean emitBody,
110 final boolean eraseBufferAndArrayTypes,
111 final boolean useNIOOnly,
113 final boolean forDirectBufferImplementation,
114 final boolean forIndirectBufferAndArrayImplementation,
115 final boolean isUnimplemented,
116 final boolean isInterface,
117 final boolean isNativeMethod,
120 this.runtimeExceptionType = runtimeExceptionType;
121 this.unsupportedExceptionType = unsupportedExceptionType;
122 this.emitBody = emitBody;
124 this.eraseBufferAndArrayTypes = eraseBufferAndArrayTypes;
125 this.useNIOOnly = useNIOOnly;
127 this.forDirectBufferImplementation = forDirectBufferImplementation;
128 this.forIndirectBufferAndArrayImplementation = forIndirectBufferAndArrayImplementation;
129 this.isUnimplemented = isUnimplemented;
130 this.isNativeMethod = isNativeMethod;
131 this.isPrivateNativeMethod = isPrivateNativeMethod;
132 if (isPrivateNativeMethod) {
138 if(
null != javaCallback ) {
141 javaCallbackEmitter =
null;
148 runtimeExceptionType = arg.runtimeExceptionType;
149 unsupportedExceptionType = arg.unsupportedExceptionType;
150 emitBody = arg.emitBody;
152 eraseBufferAndArrayTypes = arg.eraseBufferAndArrayTypes;
153 useNIOOnly = arg.useNIOOnly;
155 isNativeMethod = arg.isNativeMethod;
156 isPrivateNativeMethod = arg.isPrivateNativeMethod;
157 forDirectBufferImplementation = arg.forDirectBufferImplementation;
158 forIndirectBufferAndArrayImplementation = arg.forIndirectBufferAndArrayImplementation;
159 isUnimplemented = arg.isUnimplemented;
162 returnedArrayLengthExpression = arg.returnedArrayLengthExpression;
163 returnedArrayLengthExpressionOnlyForComments = arg.returnedArrayLengthExpressionOnlyForComments;
164 javaCallbackEmitter = arg.javaCallbackEmitter;
198 return runtimeExceptionType;
202 return unsupportedExceptionType;
213 returnedArrayLengthExpression = expr;
214 returnedArrayLengthExpressionOnlyForComments =
false;
217 returnedArrayLengthExpression = expr;
218 returnedArrayLengthExpressionOnlyForComments = onlyForComments;
221 return returnedArrayLengthExpressionOnlyForComments ? null : returnedArrayLengthExpression;
224 return returnedArrayLengthExpression;
246 this.emitBody = emitBody;
251 this.eraseBufferAndArrayTypes = erase;
256 this.isPrivateNativeMethod = v;
261 this.forDirectBufferImplementation = direct;
266 this.forIndirectBufferAndArrayImplementation = indirect;
275 if (eraseBufferAndArrayTypes) {
306 return "ByteBuffer[]";
312 throw new IllegalArgumentException(
"null type name: "+type.
getDebugString());
314 final int index = name.lastIndexOf(
'.')+1;
315 name = name.substring(index);
334 (eraseBufferAndArrayTypes &&
343 protected StringBuilder
appendName(
final StringBuilder buf) {
344 if (isPrivateNativeMethod) {
356 boolean needComma =
false;
360 buf.append(
"final Class<?> _clazzBuffers");
369 buf.append(
"ByteBuffer ");
381 throw new InternalError(
382 "\"void\" argument type found in " +
383 "multi-argument function \"" +
binding +
"\"");
405 if (forDirectBufferImplementation || forIndirectBufferAndArrayImplementation) {
419 throw new RuntimeException(
"NIO[Direct]Only "+
binding+
" is set, but "+
getArgumentName(i)+
" is a primitive array");
425 null != javaCallbackEmitter )
444 return s +
"_byte_offset";
452 return s +
"_is_direct";
465 if(
null != javaCallbackEmitter && !isPrivateNativeMethod ) {
478 if (isUnimplemented) {
493 for (
final String str : code) {
495 final MessageFormat fmt =
new MessageFormat(str);
497 }
catch (
final IllegalArgumentException e) {
525 "\\\" length (\" + ");
545 unit.
emitln(
" final int[] " + arrayName +
" = new int[" + argName +
".length];");
547 unit.
emitln(
" if (" + argName +
" != null) {");
548 unit.
emitln(
" for (int _ctr = 0; _ctr < " + argName +
".length; _ctr++) {");
549 unit.
emitln(
" if (!Buffers.isDirect(" + argName +
"[_ctr])) {");
551 "(\"Element \" + _ctr + \" of argument \\\"" +
555 unit.emit (
" " + arrayName +
"[_ctr] = Buffers.getDirectBufferByteOffset(");
562 unit.
emitln(
" if(" + argName +
" != null && " + argName +
".length <= " + offsetArg +
")");
564 unit.
emitln(
"(\"array offset argument \\\"" + offsetArg +
"\\\" (\" + " + offsetArg +
565 " + \") equals or exceeds array length (\" + " + argName +
".length + \")\");");
574 if (
binding.signatureUsesArraysOfCompoundTypeWrappers()) {
579 final String tempArrayName = argName + COMPOUND_ARRAY_SUFFIX;
580 unit.
emitln(
" final ByteBuffer[] " + tempArrayName +
" = new ByteBuffer[" + argName +
".length];");
581 unit.
emitln(
" for (int _ctr = 0; _ctr < + " + argName +
".length; _ctr++) {");
583 unit.
emitln(
" " + tempArrayName +
"[_ctr] = ((_tmp == null) ? null : _tmp.getBuffer());");
601 boolean needsResultAssignment =
false;
603 if(
null != javaCallbackEmitter ) {
606 if (!returnType.
isVoid()) {
611 needsResultAssignment =
true;
614 needsResultAssignment =
true;
616 binding.signatureUsesArraysOfCompoundTypeWrappers()) {
620 needsResultAssignment =
true;
624 if (needsResultAssignment) {
628 if (!returnType.
isVoid()) {
638 if (needsResultAssignment) {
644 boolean needComma =
false;
645 int numArgsEmitted = 0;
648 unit.
emit(
"com.jogamp.common.nio.Buffers.class");
722 unit.
emit(
", Buffers.SIZEOF_FLOAT * ");
724 unit.
emit(
", Buffers.SIZEOF_DOUBLE * ");
728 unit.
emit(
", Buffers.SIZEOF_LONG * ");
730 unit.
emit(
", Buffers.SIZEOF_SHORT * ");
732 unit.
emit(
", Buffers.SIZEOF_INT * ");
734 throw new GlueGenException(
"Unsupported type for calculating array offset argument for " +
736 " -- error occurred while processing Java glue code for " +
getCSymbol().getAliasedString(),
757 if(
null != javaCallbackEmitter ) {
761 final StringBuilder buf =
new StringBuilder();
765 return numArgsEmitted;
769 if (
binding.signatureUsesArraysOfCompoundTypeWrappers()) {
777 unit.
emitln(
" for (int _ctr = 0; _ctr < " + argName +
".length; _ctr++) {");
778 unit.
emitln(
" if ((" + argName +
"[_ctr] == null && " + argName + COMPOUND_ARRAY_SUFFIX +
"[_ctr] == null) ||");
779 unit.
emitln(
" (" + argName +
"[_ctr] != null && " + argName +
"[_ctr].getBuffer() == " + argName + COMPOUND_ARRAY_SUFFIX +
"[_ctr])) {");
782 unit.
emitln(
" if (" + argName + COMPOUND_ARRAY_SUFFIX +
"[_ctr] == null) {");
783 unit.
emitln(
" " + argName +
"[_ctr] = null;");
785 unit.
emitln(
" " + argName +
"[_ctr] = " + javaArgType.
getName() +
".create(" + argName + COMPOUND_ARRAY_SUFFIX +
"[_ctr]);");
799 unit.
emitln(
" if (_res == null) return null;");
800 unit.
emitln(
" return " + returnType.
getName() +
".create(Buffers.nativeOrder(_res));");
802 unit.
emitln(
" if (_res == null) return null;");
806 if (
getBinding().getCReturnType().pointerDepth() >= 2) {
808 unit.
emitln(
" return PointerBuffer.wrap(_res);");
813 ": can not legally make pointers opaque to anything but PointerBuffer or LongBuffer/long",
819 final String returnTypeName = returnType.
getName().substring(
"java.nio.".length());
820 unit.
emitln(
" return _res.as" + returnTypeName +
"();");
826 unit.
emitln(
" if (_res == null) return null;");
828 unit.
emitln(
" for (int _count = 0; _count < _res.length; _count++) {");
845 argumentNames[i] = argumentNames[i] +
", " +
offsetArgName(i);
848 return argumentNames;
860 final StringBuilder sb =
new StringBuilder();
863 for (
final Iterator<String> iter = methodDocs.iterator(); iter.hasNext(); ) {
866 if( methodDocs.size() > 0 ) {
870 return sb.toString();
887 if (aliases !=
null && aliases.size() > 0 ) {
889 sb.append(
"Alias for: <code>");
890 for (
final String alias : aliases) {
892 sb.append(
"</code>, <code>");
897 sb.append(
"</code>");
907 if(
null != arrayLengthExpr ) {
908 writer.print(
", covering an array of length <code>"+arrayLengthExpr+
"</code>");
913 writer.print(
"Entry point to C language function: ");
917 writer.print(
"<code>");
919 writer.print(
"</code><br>");
939 writer.print(
"@param ");
941 writer.print(
" valid values are: <code>");
943 if (j>0) writer.print(
", ");
946 writer.println(
"</code>");
951 writer.print(
"@param ");
954 writer.print(
" a direct only {@link " + javaType.
getName() +
"}");
956 writer.print(
" a direct or array-backed {@link " + javaType.
getName() +
"}");
966 writer.print(
"Interface to C language function: <br> ");
General code unit (a generated C or Java source file), covering multiple FunctionEmitter allowing to ...
void emit(final String s)
Generic function emitter to produce C (JNI) or Java code stubs to its CodeUnit, invoking a native fun...
void setCommentEmitter(final CommentEmitter cEmitter)
Set the object that will emit the comment for this function.
StringBuilder appendSignature(final StringBuilder buf)
final void emitReturnType()
final JavaConfiguration cfg
final boolean isInterface()
boolean hasModifier(final EmissionModifier m)
String getBaseIndentString()
final MethodBinding binding
final MethodBinding getBinding()
A generic exception for Jogamp errors used throughout the binding as a substitute for RuntimeExceptio...
void emitJavaSetFuncPreCall(final CodeUnit unit)
final int appendJavaAdditionalJNIParameter(final StringBuilder buf)
void emitJavaAdditionalCode(final CodeUnit unit, final boolean isInterface)
final int appendJavaAdditionalJNIArguments(final StringBuilder buf)
JavaCallback compile time information, produced by JavaEmitter#beginFunctions(TypeDictionary,...
Parses and provides access to the contents of .cfg files for the JavaEmitter.
Set< String > getAliasedDocNames(final AliasedSymbol symbol)
Return a set of aliased-name for comment in docs.
List< String > javadocForMethod(final String methodName)
Emits the Java-side component (interface and.or implementation) of the Java<->C JNI binding to its Co...
String byteOffsetArgName(final int i)
void setEpilogue(final List< String > epilogue)
Sets the manually-generated epilogue code for this emitter.
FunctionSymbol getCSymbol()
void emitCall(final MethodBinding binding)
int emitCallArguments(final MethodBinding binding)
void setForDirectBufferImplementation(final boolean direct)
Accessor for subclasses.
boolean isPrivateNativeMethod()
final boolean tagNativeBinding
static final EmissionModifier PRIVATE
void setEmitBody(final boolean emitBody)
Accessor for subclasses.
void setReturnedArrayLengthExpression(final String expr)
If the underlying function returns an array (currently only arrays of compound types are supported) a...
String getCommentEndString()
void emitAdditionalCode()
String getRuntimeExceptionType()
The type of exception (must subclass java.lang.RuntimeException) raised if runtime checks fail in the...
void emitReturnVariableSetupAndCall(final MethodBinding binding)
boolean isForDirectBufferImplementation()
static final EmissionModifier SYNCHRONIZED
static String javaThisArgumentName()
int appendArguments(final StringBuilder buf)
Returns the number of arguments emitted.
void emitPostCallCleanup(final MethodBinding binding)
void emitCompoundArrayCopies(final MethodBinding binding)
JavaMethodBindingEmitter(final JavaMethodBindingEmitter arg)
StringBuilder appendReturnType(final StringBuilder buf)
String erasedTypeString(final JavaType type, final boolean skipBuffers)
void setPrologue(final List< String > prologue)
Sets the manually-generated prologue code for this emitter.
String getUnsupportedExceptionType()
String offsetArgName(final int i)
static final EmissionModifier ABSTRACT
String getArgumentName(final int i)
void emitCallResultReturn(final MethodBinding binding)
void emitPreCallSetup(final MethodBinding binding)
static final EmissionModifier FINAL
void setForIndirectBufferAndArrayImplementation(final boolean indirect)
Accessor for subclasses.
boolean signatureOnly()
Indicates whether this emitter will print only a signature, or whether it will emit Java code for the...
void setPrivateNativeMethod(final boolean v)
Accessor for subclasses.
void setReturnedArrayLengthExpression(final String expr, final boolean onlyForComments)
String byteOffsetArrayArgName(final int i)
String getReturnedArrayLengthComment()
String getReturnTypeString(final boolean skipArray)
String[] argumentNameArray()
final boolean useNIODirectOnly
JavaMethodBindingEmitter(final MethodBinding binding, final CodeUnit unit, final String runtimeExceptionType, final String unsupportedExceptionType, final boolean emitBody, final boolean tagNativeBinding, final boolean eraseBufferAndArrayTypes, final boolean useNIOOnly, final boolean useNIODirectOnly, final boolean forDirectBufferImplementation, final boolean forIndirectBufferAndArrayImplementation, final boolean isUnimplemented, final boolean isInterface, final boolean isNativeMethod, final boolean isPrivateNativeMethod, final JavaConfiguration configuration)
String isNIOArgName(final String s)
static final EmissionModifier PUBLIC
String getNativeImplMethodName()
static String byteOffsetArgName(final String s)
StringBuilder appendName(final StringBuilder buf)
static final EmissionModifier NATIVE
final CommentEmitter defaultJavaCommentEmitter
String getCommentStartString()
String getReturnedArrayLengthExpression()
final CommentEmitter defaultInterfaceCommentEmitter
String getInterfaceName()
static final EmissionModifier PROTECTED
String getBaseIndentString()
boolean isForIndirectBufferAndArrayImplementation()
void emitPrologueOrEpilogue(final List< String > code)
String isNIOArgName(final int i)
void emitArrayLengthAndNIOBufferChecks(final MethodBinding binding)
void setEraseBufferAndArrayTypes(final boolean erase)
Accessor for subclasses.
Describes a java-side representation of a type that is used to represent the same data on both the Ja...
boolean isNIOPointerBuffer()
boolean isNIOBufferArray()
String getName()
Returns the Java type name corresponding to this type.
boolean isNIOLongBuffer()
boolean isCompoundTypeWrapper()
boolean isPrimitiveArray()
boolean isArrayOfCompoundTypeWrappers()
boolean isNIOByteBuffer()
Represents the binding of a C function to a Java method.
Type getCArgumentType(final int i)
String getArgumentName(final int i)
Returns either the argument name specified by the underlying FunctionSymbol or a fabricated argument ...
String getImplName()
Returns the FunctionSymbol's name for the implementation, which is the current aliased API name per d...
JavaType getContainingType()
Retrieves the containing type of this MethodBinding if it is for a function pointer contained in a st...
JavaType getJavaArgumentType(final int i)
JavaType getJavaReturnType()
String getInterfaceName()
Returns the FunctionSymbol's current aliased API name for the interface.
String getName()
Returns the FunctionSymbol's current aliased API name.
boolean hasContainingType()
Indicates whether this MethodBinding is for a function pointer contained in a struct,...
boolean isArgumentThisPointer(final int i)
Indicates whether the ith argument to this MethodBinding is actually a "this" pointer.
FunctionSymbol getCSymbol()
Returns the FunctionSymbol.
final boolean isReturnCompoundByValue()
String getNativeName()
Returns the FunctionSymbol's name for the native function which is the original C API name per defaul...
Represents an array type.
Describes enumerated types.
int getNumEnumerates()
Number of enumerates defined in this enum.
Enumerator getEnum(final int i)
Fetch ith (0..getNumEnumerates() - 1) Enumerator.
Describes a function symbol, which includes the name and type.
final boolean isArray()
Indicates whether this is an ArrayType.
final boolean isBaseTypeConst()
Checks the base type of pointer-to-pointer, pointer, array or plain for const-ness.
final String getName()
Returns the name of this type.
final boolean isEnum()
Indicates whether this is an EnumType.
ArrayType asArray()
Casts this to an ArrayType or returns null if not an ArrayType.
Supports symbol aliasing, i.e.