From 9e61409658358834c2ba26c69c3efcbb5755c128 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 23 Jun 2023 06:31:56 +0200 Subject: GlueGen: Type: Have get{Base,Target}Type() stop at isFunctionPointer(), enhance API doc; Add getArrayBaseOrPointerTargetType() and getTargetFunction() Added getArrayBaseOrPointerTargetType() returns getBaseType() for arrays or getTargetType() for pointer, i.e. stops traversing if an elementType is a pointer and returns the elementType as target-type. This resolves 'int* intPtrArray[10]', but also simplifies all cases of 'int** intPtrPtr' and 'int intPtr[10]' etc. Since get{Base,Target}Type() returns the functionPointer, getTargetFunction() allows to retrieve the actual target function type. --- .../com/jogamp/gluegen/CMethodBindingEmitter.java | 11 +++----- src/java/com/jogamp/gluegen/JavaEmitter.java | 28 ++++++------------- .../com/jogamp/gluegen/cgram/types/ArrayType.java | 9 +++++++ src/java/com/jogamp/gluegen/cgram/types/Field.java | 2 +- .../jogamp/gluegen/cgram/types/FunctionType.java | 3 +-- .../jogamp/gluegen/cgram/types/PointerType.java | 31 +++++++++++++++++++--- src/java/com/jogamp/gluegen/cgram/types/Type.java | 30 ++++++++++++++++++--- 7 files changed, 76 insertions(+), 38 deletions(-) (limited to 'src/java/com') diff --git a/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java b/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java index a7de97d..b48d77a 100644 --- a/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java +++ b/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java @@ -652,7 +652,7 @@ public class CMethodBindingEmitter extends FunctionEmitter { } } } else if( cArgType.isArray() ) { - cArgElementType = cArgType.asArray().getBaseType(); + cArgElementType = cArgType.getBaseType(); cArgElementType2 = null; } else { cArgElementType = null; @@ -814,7 +814,7 @@ public class CMethodBindingEmitter extends FunctionEmitter { unit.emitln(" for (_copyIndex = 0; _copyIndex < _tmpArrayLen; ++_copyIndex) {"); unit.emitln(" _tmpObj = (*env)->GetObjectArrayElement(env, " + javaArgName + ", _copyIndex);"); emitReturnDirectBufferAddress("_tmpObj", - cArgType.asArray().getBaseType().getCName(), + cArgType.getBaseType().getCName(), "("+convName + "_copy + _copyIndex)", false /* receivingIsPtrPtr */, null); @@ -1112,12 +1112,7 @@ public class CMethodBindingEmitter extends FunctionEmitter { unit.emitln(" " + arrayRes + " = (*env)->NewObjectArray(env, " + arrayResLength + ", (*env)->FindClass(env, \"java/nio/ByteBuffer\"), NULL);"); unit.emitln(" for (" + arrayIdx + " = 0; " + arrayIdx + " < " + arrayResLength + "; " + arrayIdx + "++) {"); final Type retType = binding.getCSymbol().getReturnType(); - Type pointerType; - if (retType.isPointer()) { - pointerType = retType.asPointer().getTargetType(); - } else { - pointerType = retType.asArray().getBaseType(); - } + final Type pointerType = retType.getArrayBaseOrPointerTargetType(); unit.emitln(" (*env)->SetObjectArrayElement(env, " + arrayRes + ", " + arrayIdx + ", (*env)->NewDirectByteBuffer(env, (void *)_res[" + arrayIdx + "], sizeof(" + pointerType.getCName() + ")));"); unit.emitln(" }"); diff --git a/src/java/com/jogamp/gluegen/JavaEmitter.java b/src/java/com/jogamp/gluegen/JavaEmitter.java index 0bf5725..110bf50 100644 --- a/src/java/com/jogamp/gluegen/JavaEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaEmitter.java @@ -983,7 +983,7 @@ public class JavaEmitter implements GlueEmitter { } generateOffsetAndSizeArrays(javaUnit, " ", fieldName, fieldType, field, null); } else if (fieldType.isArray()) { - final Type baseElementType = field.getType().asArray().getBaseType(); + final Type baseElementType = fieldType.getBaseType(); if( GlueGen.debug() ) { System.err.printf("SE.os.%02d: %s / %s, %s (%s)%n", (i+1), field, cfgFieldName1, fieldType.getDebugString(), "array"); System.err.printf("SE.os.%02d: baseType %s%n", (i+1), baseElementType.getDebugString()); @@ -1249,11 +1249,11 @@ public class JavaEmitter implements GlueEmitter { isArray = false; referencedType = null; relationship = "being"; - } else if( fieldType.pointerDepth() > 0 ) { + } else if( fieldType.isPointer() ) { isArray = true; referencedType = fieldType.getBaseType(); relationship = "referencing"; - } else if( fieldType.arrayDimension() > 0 ) { + } else if( fieldType.isArray() ) { isArray = true; referencedType = null; relationship = "being"; @@ -2390,14 +2390,8 @@ public class JavaEmitter implements GlueEmitter { if (info != null) { boolean isPointerPointer = false; if (cType.pointerDepth() > 0 || cType.arrayDimension() > 0) { - Type targetType; // target type - if (cType.isPointer()) { - // t is *, we need to get - targetType = cType.asPointer().getTargetType(); - } else { - // t is [], we need to get - targetType = cType.asArray().getBaseType(); - } + // t is `*`, `[]` or `[][]`, we need to get + final Type targetType = cType.getArrayBaseOrPointerTargetType(); if (cType.pointerDepth() == 2 || cType.arrayDimension() == 2) { // Get the target type of the target type (targetType was computer earlier // as to be a pointer to the target type, so now we need to get its @@ -2435,14 +2429,8 @@ public class JavaEmitter implements GlueEmitter { } else if (cType.isVoid()) { return javaType(Void.TYPE); } else if (cType.pointerDepth() > 0 || cType.arrayDimension() > 0) { - Type targetType; // target type - if (cType.isPointer()) { - // t is *, we need to get - targetType = cType.asPointer().getTargetType(); - } else { - // t is [], we need to get - targetType = cType.asArray().getBaseType(); - } + // [][] + final Type targetType = cType.getArrayBaseOrPointerTargetType(); // Handle Types of form pointer-to-type or array-of-type, like // char* or int[]; these are expanded out into Java primitive @@ -2521,7 +2509,7 @@ public class JavaEmitter implements GlueEmitter { return JavaType.forNIOPointerBufferClass(); } else if(targetType.isArray()) { // t is[][], targetType is [], we need to get - bottomType = targetType.asArray().getBaseType(); + bottomType = targetType.getBaseType(); if( GlueGen.debug() ) { LOG.log(INFO, cType.getASTLocusTag(), "typeToJavaType(ptr-ptr.array): {0}, targetType: {1}, bottomType: {2}", cType.getDebugString(), targetType.getDebugString(), bottomType.getDebugString()); diff --git a/src/java/com/jogamp/gluegen/cgram/types/ArrayType.java b/src/java/com/jogamp/gluegen/cgram/types/ArrayType.java index 03cb64d..8b5432c 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/ArrayType.java +++ b/src/java/com/jogamp/gluegen/cgram/types/ArrayType.java @@ -121,6 +121,15 @@ public class ArrayType extends MemoryLayoutType implements Cloneable { return elementType.getBaseType(); } + @Override + public Type getArrayBaseOrPointerTargetType() { + if( elementType.isPointer() ) { + return getTargetType(); + } else { + return getBaseType(); + } + } + @Override public final int arrayDimension() { return 1 + elementType.arrayDimension(); diff --git a/src/java/com/jogamp/gluegen/cgram/types/Field.java b/src/java/com/jogamp/gluegen/cgram/types/Field.java index a8fc599..d11e7e7 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/Field.java +++ b/src/java/com/jogamp/gluegen/cgram/types/Field.java @@ -120,7 +120,7 @@ public class Field implements SemanticEqualityOp { } return "" + getType() + " " + getName() + ";"; } else { - final FunctionType ft = getType().asPointer().getTargetType().asFunction(); + final FunctionType ft = getType().getTargetFunction(); // FIXME: pick up calling convention? return ft.toString(getName(), null, false, true) + ";"; } diff --git a/src/java/com/jogamp/gluegen/cgram/types/FunctionType.java b/src/java/com/jogamp/gluegen/cgram/types/FunctionType.java index 2b9dec7..01d58ae 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/FunctionType.java +++ b/src/java/com/jogamp/gluegen/cgram/types/FunctionType.java @@ -192,8 +192,7 @@ public class FunctionType extends Type implements Cloneable { for (int i = 0; i < n; i++) { final Type t = getArgumentType(i); if (t.isFunctionPointer()) { - final Type targetType = t.asPointer().getTargetType(); - final FunctionType ft = targetType.asFunction(); + final FunctionType ft = t.getTargetFunction(); res.append(ft.toString(getArgumentName(i), callingConvention, false, true)); } else if (t.isArray()) { res.append(t.asArray().toString(getArgumentName(i))); diff --git a/src/java/com/jogamp/gluegen/cgram/types/PointerType.java b/src/java/com/jogamp/gluegen/cgram/types/PointerType.java index eb89500..4d5079d 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/PointerType.java +++ b/src/java/com/jogamp/gluegen/cgram/types/PointerType.java @@ -124,13 +124,36 @@ public class PointerType extends Type implements Cloneable { } @Override - public final Type getTargetType() { return targetType; } + public final Type getTargetType() { + if( isFunctionPointer() ) { + return this; + } else { + return targetType; + } + } @Override public final Type getBaseType() { - return targetType.getBaseType(); + if( isFunctionPointer() ) { + return this; + } else { + return targetType.getBaseType(); + } + } + + @Override + public Type getArrayBaseOrPointerTargetType() { + return getTargetType(); } + @Override + public FunctionType getTargetFunction() { + if( isFunctionPointer() ) { + return targetType.asFunction(); + } else { + return null; + } + } @Override public final boolean isFunctionPointer() { return targetType.isFunction(); @@ -150,7 +173,7 @@ public class PointerType extends Type implements Cloneable { } } private String toStringInt() { - if (!targetType.isFunction()) { + if (!isFunctionPointer()) { return targetType.getCName(true) + " * " + getCVAttributesString(); } else { // return toString(null, null); // this is a pointer to an unnamed function @@ -162,7 +185,7 @@ public class PointerType extends Type implements Cloneable { string (i.e., "__stdcall") is optional and is generally only needed on Windows. */ public String toString(final String functionName, final String callingConvention) { - if (!targetType.isFunction()) { + if (!isFunctionPointer()) { throw new RuntimeException(" This method is only for use when printing function pointers"); } return ((FunctionType) targetType).toString(functionName, callingConvention, false, true); diff --git a/src/java/com/jogamp/gluegen/cgram/types/Type.java b/src/java/com/jogamp/gluegen/cgram/types/Type.java index 5906b19..ae377dc 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/Type.java +++ b/src/java/com/jogamp/gluegen/cgram/types/Type.java @@ -570,25 +570,49 @@ public abstract class Type implements SemanticEqualityOp, ASTLocusTagProvider { } /** - * Helper method to returns the bottom-most element type of this type. + * Helper method to returns the bottom-most element type of this type, + * i.e. returns `{@code type}` if this-type is `{@code type}*`, `{@code type}**`, `{@code type}[]` or `{@code type}[][]`. *

* If this is a multidimensional array or pointer method returns the bottom-most element type, * otherwise this. *

+ *

+ * In case a {@link #isFunctionPointer()} type is reached, traversing ends and the function {@link PointerType} is returned. + *

* @see #getTargetType() + * @see #getTargetFunction() */ public Type getBaseType() { return this; } /** - * Helper method to returns the target type of this type, in case another type is being referenced. + * Helper method to returns the target type of this type, in case another type is being referenced, + * i.e. returns `{@code type}` if this-type is `{@code type}*` or `{@code type}[]` + * and returns `{@code type}*` if this-type is `{@code type}**` or `{@code type}[][]`. + *

+ * If this is an array or pointer method returns the next target element type, otherwise `this`. + *

*

- * If this is an array or pointer method returns the next target element type, otherwise this. + * In this is a {@link #isFunctionPointer()} type, `this` function {#link PointerType} is returned. *

* @see #getBaseType() + * @see #getTargetFunction() */ public Type getTargetType() { return this; } + + /** + * Return {@link #getBaseType()} if {@link #isArray()} or returns {@link #getTargetType()} otherwise. + */ + public Type getArrayBaseOrPointerTargetType() { + return this; + } + + /** + * Returns the target {@link FunctionType} if this type is {@link #isFunctionPointer()}. + */ + public FunctionType getTargetFunction() { return null; } + } -- cgit v1.2.3