diff options
author | Sven Gothel <[email protected]> | 2023-06-30 09:41:21 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-06-30 09:41:21 +0200 |
commit | b35d62425311ec50e6c80b07372bc411aa287bb4 (patch) | |
tree | d5904a514bbb38d977f73d86c848c7e112b89099 /src/java/com | |
parent | ad79dae90b94d0875416278e9dcedfc08a1de578 (diff) |
GlueGen FunctionType/MethodBinding: get*ParameterList(): Use a more flexible way via a ParameterConsumer visitor, also usable for other iterative parameter generator
Diffstat (limited to 'src/java/com')
8 files changed, 210 insertions, 84 deletions
diff --git a/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java b/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java index d802ee7..3975315 100644 --- a/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java +++ b/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java @@ -341,7 +341,7 @@ public class CMethodBindingEmitter extends FunctionEmitter { // javaCallback.cbFuncCEmitter.emitSignature(); unit.emit("static "+cReturnType.getCName()+" func"+jcbNativeBasename+"("); // javaCallback.cbFuncCEmitter.emitArguments(); - unit.emit(javaCallback.cbFuncBinding.getCParameterList(new StringBuilder(), null).toString()); + unit.emit(javaCallback.cbFuncBinding.getCParameterList(new StringBuilder(), false, null).toString()); unit.emitln(") {"); // javaCallback.cbFuncCEmitter.emitBody(); { @@ -360,7 +360,7 @@ public class CMethodBindingEmitter extends FunctionEmitter { // javaCallback.cbFuncCEmitter.emitBodyCallCFunction(); unit.emitln(" T_"+jcbNativeBasename+"* cb = (T_"+jcbNativeBasename+"*) "+userParamArgName+";"); - unit.emitln(" // C Params: "+javaCallback.cbFuncBinding.getCParameterList(new StringBuilder(), null).toString()); + unit.emitln(" // C Params: "+javaCallback.cbFuncBinding.getCParameterList(new StringBuilder(), false, null).toString()); unit.emitln(" // J Params: "+javaCallback.cbFuncBinding.getJavaParameterList(new StringBuilder()).toString()); // unit.emitln(" fprintf(stderr, \"YYY Callback01 user %p, id %ld, msg %s\\n\", cb, id, msg);"); diff --git a/src/java/com/jogamp/gluegen/CodeGenUtils.java b/src/java/com/jogamp/gluegen/CodeGenUtils.java index aa7a4e2..69a9001 100644 --- a/src/java/com/jogamp/gluegen/CodeGenUtils.java +++ b/src/java/com/jogamp/gluegen/CodeGenUtils.java @@ -1,4 +1,5 @@ /* + * Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -57,16 +58,6 @@ public class CodeGenUtils { return Character.toLowerCase(string.charAt(0)) + string.substring(1); } - /** Appends `param` to `buf` while prepending a comman separator if `needsComma[0]` is true. Sets `needsComma[0]=true` afterwards. */ - public static StringBuilder addParameterToList(final StringBuilder buf, final String param, final boolean[] needsComma) { - if( needsComma[0] ) { - buf.append(", "); - } - buf.append(param); - needsComma[0] = true; - return buf; - } - /** * Given a java package name (e.g., "java.lang"), return the package as a * directory path (i.e., "java/lang"). diff --git a/src/java/com/jogamp/gluegen/JavaType.java b/src/java/com/jogamp/gluegen/JavaType.java index 3bd4943..682a95a 100644 --- a/src/java/com/jogamp/gluegen/JavaType.java +++ b/src/java/com/jogamp/gluegen/JavaType.java @@ -1,6 +1,6 @@ -/* +/** + * Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. - * Copyright (c) 2010 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are diff --git a/src/java/com/jogamp/gluegen/MethodBinding.java b/src/java/com/jogamp/gluegen/MethodBinding.java index 44ed2e3..6059003 100644 --- a/src/java/com/jogamp/gluegen/MethodBinding.java +++ b/src/java/com/jogamp/gluegen/MethodBinding.java @@ -1,4 +1,4 @@ -/* +/** * Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * @@ -158,37 +158,50 @@ public class MethodBinding { /** * Returns the function parameter list, i.e. a comma separated list of argument type and name. * @param buf StringBuilder instance + * @param useTypedef if true and type is typedef'ed, use its name * @param callingConvention optional calling-convention * @return given StringBuilder instance */ - public StringBuilder getCParameterList(final StringBuilder buf, final String callingConvention) { - final int n = getNumArguments(); - final boolean[] needsComma = { false }; - for (int i = 0; i < n; i++) { - final Type t = getCArgumentType(i); - if( t.isVoid() ) { - // nop - } else if( t.isTypedef() ) { - CodeGenUtils.addParameterToList(buf, t.getName(), needsComma); - final String argumentName = getArgumentName(i); - if (argumentName != null) { - buf.append(" "); - buf.append(argumentName); + public StringBuilder getCParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention) { + return getCParameterList(buf, useTypedef, callingConvention, null); + } + /** + * Returns the function parameter list, i.e. a comma separated list of argument type and name. + * @param buf StringBuilder instance + * @param useTypedef if true and type is typedef'ed, use its name + * @param callingConvention optional calling-convention + * @param exclude optional list of excluded parameter indices + * @return given StringBuilder instance + */ + public StringBuilder getCParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention, final List<Integer> exclude) { + forEachParameter( ( final int idx, final int consumedCount, final Type cType, final JavaType jType, final String name ) -> { + if( !cType.isVoid() && ( null == exclude || !exclude.contains(idx) ) ) { + if( 0 < consumedCount ) { + buf.append(", "); } - } else if ( t.isFunctionPointer() ) { - final FunctionType ft = t.getTargetFunction(); - CodeGenUtils.addParameterToList(buf, ft.toString(getArgumentName(i), callingConvention, false, true), needsComma); - } else if (t.isArray()) { - CodeGenUtils.addParameterToList(buf, t.asArray().toString(getArgumentName(i)), needsComma); - } else { - CodeGenUtils.addParameterToList(buf, t.getCName(true), needsComma); - final String argumentName = getArgumentName(i); - if (argumentName != null) { - buf.append(" "); - buf.append(argumentName); + if( useTypedef && cType.isTypedef() ) { + buf.append( cType.getName() ); + if (name != null) { + buf.append(" "); + buf.append(name); + } + } else if ( cType.isFunctionPointer() ) { + final FunctionType ft = cType.getTargetFunction(); + buf.append( ft.toString(name, callingConvention, false, true) ); + } else if (cType.isArray()) { + buf.append( cType.asArray().toString(name) ); + } else { + buf.append( cType.getCName(true) ); + if (name != null) { + buf.append(" "); + buf.append(name); + } } + return true; + } else { + return false; } - } + } ); return buf; } @@ -198,23 +211,108 @@ public class MethodBinding { * @return given StringBuilder instance */ public StringBuilder getJavaParameterList(final StringBuilder buf) { - final int n = getNumArguments(); - final boolean[] needsComma = { false }; - for (int i = 0; i < n; i++) { - final JavaType t = getJavaArgumentType(i); - if( t.isVoid() ) { - // nop + return getJavaParameterList(buf, null); + } + /** + * Returns the function parameter list, i.e. a comma separated list of argument type and name. + * @param buf StringBuilder instance + * @param exclude optional list of excluded parameter indices + * @return given StringBuilder instance + */ + public StringBuilder getJavaParameterList(final StringBuilder buf, final List<Integer> exclude) { + forEachParameter( ( final int idx, final int consumedCount, final Type cType, final JavaType jType, final String name ) -> { + if( !cType.isVoid() && ( null == exclude || !exclude.contains(idx) ) ) { + if( 0 < consumedCount ) { + buf.append(", "); + } + buf.append(jType+" "+name); + return true; } else { - CodeGenUtils.addParameterToList(buf, t.getName(), needsComma); - final String argumentName = getArgumentName(i); - if (argumentName != null) { - buf.append(" "); - buf.append(argumentName); + return false; + } + } ); + return buf; + } + /** + * Returns the function parameter list, i.e. a comma separated list of argument type and name. + * @param buf StringBuilder instance + * @param include list of explicit included parameter indices + * @param addTailSeparator add a comma separator in the end if result is not empty + * @return given StringBuilder instance + */ + public StringBuilder getJavaSelectParameter(final StringBuilder buf, final List<Integer> include, final boolean addTailSeparator) { + forEachParameter( ( final int idx, final int consumedCount, final Type cType, final JavaType jType, final String name ) -> { + if( !cType.isVoid() && include.contains(idx) ) { + if( 0 < consumedCount ) { + buf.append(", "); } + buf.append(jType+" "+name); + return true; + } else { + return false; } + } ); + if( addTailSeparator && buf.length() > 0 ) { + buf.append(", "); } return buf; } + public StringBuilder getJavaCallArgumentList(final StringBuilder buf, final List<Integer> exclude) { + forEachParameter( ( final int idx, final int consumedCount, final Type cType, final JavaType jType, final String name ) -> { + if( !cType.isVoid() && ( null == exclude || !exclude.contains(idx) ) ) { + if( 0 < consumedCount ) { + buf.append(", "); + } + buf.append(name); + return true; + } else { + return false; + } + } ); + return buf; + } + public StringBuilder getJavaCallSelectArguments(final StringBuilder buf, final List<Integer> include, final boolean addTailSeparator) { + forEachParameter( ( final int idx, final int consumedCount, final Type cType, final JavaType jType, final String name ) -> { + if( !cType.isVoid() && include.contains(idx) ) { + if( 0 < consumedCount ) { + buf.append(", "); + } + buf.append(name); + return true; + } else { + return false; + } + } ); + if( addTailSeparator && buf.length() > 0 ) { + buf.append(", "); + } + return buf; + } + + /** {@link #forEachParameter(ParameterConsumer)} Consumer */ + public static interface ParameterConsumer { + /** + * Accept the arguments of the traversed collection element + * and return true if consumed. Consumed elements will increased passed `consumedCount` state. + * @param idx index of current element, ranges [0 .. size-1] + * @param consumedCount number of consumed elements, useful for e.g. `boolean needsSeparator = 0 < consumedCount` + * @param cType C Type of argument + * @param jType Java Type of argument + * @param name argument name + * @return true to signal consumed and have traversing loop increment `consumedCount`, otherwise false + */ + boolean accept(int idx, int consumedCount, Type cType, JavaType jType, String name); + } + public int forEachParameter(final ParameterConsumer c) { + final int n = getNumArguments(); + int consumedCount = 0; + for (int i = 0; i < n; i++) { + if( c.accept(i, consumedCount, getCArgumentType(i), getJavaArgumentType(i), getArgumentName(i)) ) { + ++consumedCount; + } + } + return consumedCount; + } public final boolean isReturnCompoundByValue() { final Type cReturnType = getCReturnType(); diff --git a/src/java/com/jogamp/gluegen/cgram/types/FunctionSymbol.java b/src/java/com/jogamp/gluegen/cgram/types/FunctionSymbol.java index f4cd15f..45f70c5 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/FunctionSymbol.java +++ b/src/java/com/jogamp/gluegen/cgram/types/FunctionSymbol.java @@ -1,4 +1,5 @@ -/* +/** + * Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/src/java/com/jogamp/gluegen/cgram/types/FunctionType.java b/src/java/com/jogamp/gluegen/cgram/types/FunctionType.java index aab106f..fded079 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/FunctionType.java +++ b/src/java/com/jogamp/gluegen/cgram/types/FunctionType.java @@ -1,6 +1,6 @@ -/* +/** + * Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. - * Copyright (c) 2010 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -42,7 +42,6 @@ package com.jogamp.gluegen.cgram.types; import java.util.*; import com.jogamp.gluegen.ASTLocusTag; -import com.jogamp.gluegen.CodeGenUtils; /** Describes a function type, used to model both function declarations and (via PointerType) function pointers. */ @@ -139,40 +138,77 @@ public class FunctionType extends Type implements Cloneable { /** * Returns the function parameter list, i.e. a comma separated list of argument type and name. * @param buf StringBuilder instance + * @param useTypedef if true and type is typedef'ed, use its name * @param callingConvention optional calling-convention * @return given StringBuilder instance */ - public StringBuilder getParameterList(final StringBuilder buf, final String callingConvention) { - final int n = getNumArguments(); - final boolean[] needsComma = { false }; - for (int i = 0; i < n; i++) { - final Type t = getArgumentType(i); - if( t.isVoid() ) { - // nop - } else if( t.isTypedef() ) { - CodeGenUtils.addParameterToList(buf, t.getName(), needsComma); - final String argumentName = getArgumentName(i); - if (argumentName != null) { - buf.append(" "); - buf.append(argumentName); + public StringBuilder getParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention) { + return getParameterList(buf, useTypedef, callingConvention, null); + } + /** + * Returns the function parameter list, i.e. a comma separated list of argument type and name. + * @param buf StringBuilder instance + * @param useTypedef if true and type is typedef'ed, use its name + * @param callingConvention optional calling-convention + * @param exclude optional list of excluded parameter indices + * @return given StringBuilder instance + */ + public StringBuilder getParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention, final List<Integer> exclude) { + forEachParameter( ( final int idx, final int consumedCount, final Type cType, final String name ) -> { + if( !cType.isVoid() && ( null == exclude || !exclude.contains(idx) ) ) { + if( 0 < consumedCount ) { + buf.append(", "); } - } else if ( t.isFunctionPointer() ) { - final FunctionType ft = t.getTargetFunction(); - CodeGenUtils.addParameterToList(buf, ft.toString(getArgumentName(i), callingConvention, false, true), needsComma); - } else if (t.isArray()) { - CodeGenUtils.addParameterToList(buf, t.asArray().toString(getArgumentName(i)), needsComma); - } else { - CodeGenUtils.addParameterToList(buf, t.getCName(true), needsComma); - final String argumentName = getArgumentName(i); - if (argumentName != null) { - buf.append(" "); - buf.append(argumentName); + if( useTypedef && cType.isTypedef() ) { + buf.append( cType.getName() ); + if (name != null) { + buf.append(" "); + buf.append(name); + } + } else if ( cType.isFunctionPointer() ) { + final FunctionType ft = cType.getTargetFunction(); + buf.append( ft.toString(name, callingConvention, false, true) ); + } else if (cType.isArray()) { + buf.append( cType.asArray().toString(name) ); + } else { + buf.append( cType.getCName(true) ); + if (name != null) { + buf.append(" "); + buf.append(name); + } } + return true; + } else { + return false; } - } + } ); return buf; } + /** {@link #forEachParameter(ParameterConsumer)} Consumer */ + public static interface ParameterConsumer { + /** + * Accept the arguments of the traversed collection element + * and return true if consumed. Consumed elements will increased passed `consumedCount` state. + * @param idx index of current element, ranges [0 .. size-1] + * @param consumedCount number of consumed elements, useful for e.g. `boolean needsSeparator = 0 < consumedCount` + * @param cType C Type of argument + * @param name argument name + * @return true to signal consumed and have traversing loop increment `consumedCount`, otherwise false + */ + boolean accept(int idx, int consumedCount, Type cType, String name); + } + public int forEachParameter(final ParameterConsumer c) { + final int n = getNumArguments(); + int consumedCount = 0; + for (int i = 0; i < n; i++) { + if( c.accept(i, consumedCount, getArgumentType(i), getArgumentName(i)) ) { + ++consumedCount; + } + } + return consumedCount; + } + /** * Add an argument's name and type. Use null for unknown argument names. */ @@ -229,7 +265,7 @@ public class FunctionType extends Type implements Cloneable { res.append(")"); } res.append("("); - getParameterList(res, callingConvention); + getParameterList(res, true, callingConvention); res.append(")"); return res.toString(); } diff --git a/src/java/com/jogamp/gluegen/cgram/types/PointerType.java b/src/java/com/jogamp/gluegen/cgram/types/PointerType.java index 4d5079d..5cf2d10 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/PointerType.java +++ b/src/java/com/jogamp/gluegen/cgram/types/PointerType.java @@ -1,6 +1,6 @@ -/* +/** + * Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. - * Copyright (c) 2010 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are diff --git a/src/java/com/jogamp/gluegen/cgram/types/Type.java b/src/java/com/jogamp/gluegen/cgram/types/Type.java index aa7f845..762dfbb 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/Type.java +++ b/src/java/com/jogamp/gluegen/cgram/types/Type.java @@ -1,6 +1,6 @@ /* + * Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. - * Copyright (c) 2010 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are |