GlueGen: src/java/com/jogamp/gluegen/cgram/types/FunctionType.java Source File

GlueGen v2.6.0-rc-20250706 GlueGen, Native Binding Generator for Java™ (public API).

FunctionType.java
Go to the documentation of this file.
1/**
2 * Copyright (c) 2010-2023 JogAmp Community. All rights reserved.
3 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40package com.jogamp.gluegen.cgram.types;
41
42import java.util.*;
43
44import com.jogamp.gluegen.ASTLocusTag;
45
46/** Describes a function type, used to model both function
47declarations and (via PointerType) function pointers. */
48 public class FunctionType extends Type implements Cloneable {
49
50 private final Type returnType;
51 private ArrayList<Type> argumentTypes;
52 private ArrayList<String> argumentNames;
53
54 public FunctionType(final String name, final SizeThunk size, final Type returnType,
55 final int cvAttributes) {
56 this(name, size, returnType, cvAttributes, null);
57 }
58 public FunctionType(final String name, final SizeThunk size, final Type returnType,
59 final int cvAttributes, final ASTLocusTag astLocus) {
60 super(name, size, cvAttributes, astLocus);
61 this.returnType = returnType;
62 }
63
64 private FunctionType(final FunctionType o, final ASTLocusTag astLocus) {
65 super(o, o.getCVAttributes(), astLocus);
66 returnType = o.returnType;
67 if(null != o.argumentTypes) {
68 argumentTypes = new ArrayList<Type>(o.argumentTypes);
69 }
70 if(null != o.argumentNames) {
71 argumentNames = new ArrayList<String>(o.argumentNames);
72 }
73 }
74
75 @Override
76 Type newVariantImpl(final boolean newCVVariant, final int cvAttributes, final ASTLocusTag astLocus) {
77 if( newCVVariant ) {
78 // Functions don't have const/volatile attributes
79 return this;
80 } else {
81 return new FunctionType(this, astLocus);
82 }
83 }
84
85 @Override
86 protected int hashCodeImpl() {
87 // 31 * x == (x << 5) - x
88 final int hash = returnType.hashCode();
89 return ((hash << 5) - hash) + TypeComparator.listsHashCode(argumentTypes);
90 }
91
92 @Override
93 protected boolean equalsImpl(final Type arg) {
94 final FunctionType t = (FunctionType) arg;
95 return returnType.equals(t.returnType) &&
96 TypeComparator.listsEqual(argumentTypes, t.argumentTypes);
97 }
98
99 @Override
100 protected int hashCodeSemanticsImpl() {
101 // 31 * x == (x << 5) - x
102 final int hash = returnType.hashCodeSemantics();
103 return ((hash << 5) - hash) + TypeComparator.listsHashCodeSemantics(argumentTypes);
104 }
105
106 @Override
107 protected boolean equalSemanticsImpl(final Type arg) {
108 final FunctionType t = (FunctionType) arg;
109 return returnType.equalSemantics(t.returnType) &&
110 TypeComparator.listsEqualSemantics(argumentTypes, t.argumentTypes);
111 }
112
113 @Override
114 public FunctionType asFunction() {
115 return this;
116 }
117
118 /** Returns the return type of this function. */
119 public Type getReturnType() {
120 return returnType;
121 }
122
123 public int getNumArguments() {
124 return ((argumentTypes == null) ? 0 : argumentTypes.size());
125 }
126
127 /** Returns the name of the <i>i</i>th argument. May return null if
128 no argument names were available during parsing. */
129 public String getArgumentName(final int i) {
130 return argumentNames.get(i);
131 }
132
133 /** Returns the type of the <i>i</i>th argument. */
134 public Type getArgumentType(final int i) {
135 return argumentTypes.get(i);
136 }
137
138 /**
139 * Returns the function parameter list, i.e. a comma separated list of argument type and name.
140 * @param buf StringBuilder instance
141 * @param useTypedef if true and type is typedef'ed, use its name
142 * @param callingConvention optional calling-convention
143 * @return given StringBuilder instance
144 */
145 public StringBuilder getParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention) {
146 return getParameterList(buf, useTypedef, callingConvention, null);
147 }
148 /**
149 * Returns the function parameter list, i.e. a comma separated list of argument type and name.
150 * @param buf StringBuilder instance
151 * @param useTypedef if true and type is typedef'ed, use its name
152 * @param callingConvention optional calling-convention
153 * @param exclude optional list of excluded parameter indices
154 * @return given StringBuilder instance
155 */
156 public StringBuilder getParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention, final List<Integer> exclude) {
157 forEachParameter( ( final int idx, final int consumedCount, final Type cType, final String name ) -> {
158 if( !cType.isVoid() && ( null == exclude || !exclude.contains(idx) ) ) {
159 if( 0 < consumedCount ) {
160 buf.append(", ");
161 }
162 if( useTypedef && cType.isTypedef() ) {
163 buf.append( cType.getName() );
164 if (name != null) {
165 buf.append(" ");
166 buf.append(name);
167 }
168 } else if ( cType.isFunctionPointer() ) {
169 final FunctionType ft = cType.getTargetFunction();
170 buf.append( ft.toString(name, callingConvention, false, true) );
171 } else if (cType.isArray()) {
172 buf.append( cType.asArray().toString(name) );
173 } else {
174 buf.append( cType.getCName(true) );
175 if (name != null) {
176 buf.append(" ");
177 buf.append(name);
178 }
179 }
180 return true;
181 } else {
182 return false;
183 }
184 } );
185 return buf;
186 }
187
188 /** {@link #forEachParameter(ParameterConsumer)} Consumer */
189 public static interface ParameterConsumer {
190 /**
191 * Accept the arguments of the traversed collection element
192 * and return true if consumed. Consumed elements will increased passed `consumedCount` state.
193 * @param idx index of current element, ranges [0 .. size-1]
194 * @param consumedCount number of consumed elements, useful for e.g. `boolean needsSeparator = 0 < consumedCount`
195 * @param cType C Type of argument
196 * @param name argument name
197 * @return true to signal consumed and have traversing loop increment `consumedCount`, otherwise false
198 */
199 boolean accept(int idx, int consumedCount, Type cType, String name);
200 }
201 public int forEachParameter(final ParameterConsumer c) {
202 final int n = getNumArguments();
203 int consumedCount = 0;
204 for (int i = 0; i < n; i++) {
205 if( c.accept(i, consumedCount, getArgumentType(i), getArgumentName(i)) ) {
206 ++consumedCount;
207 }
208 }
209 return consumedCount;
210 }
211
212 /**
213 * Add an argument's name and type. Use null for unknown argument names.
214 */
215 public void addArgument(final Type argumentType, final String argumentName) {
216 if (argumentTypes == null) {
217 argumentTypes = new ArrayList<Type>();
218 argumentNames = new ArrayList<String>();
219 }
220 argumentTypes.add(argumentType);
221 argumentNames.add(argumentName);
222 clearCache();
223 }
224
225 public void setArgumentName(final int i, final String name) {
226 argumentNames.set(i, name);
227 clearCache();
228 }
229
230 @Override
231 public String toString() {
232 return toString(null, false);
233 }
234
235 public String toString(final String functionName, final boolean emitNativeTag) {
236 return toString(functionName, null, emitNativeTag, false);
237 }
238 public String toString(final String functionName, final boolean emitNativeTag, final boolean isPointer) {
239 return toString(functionName, null, emitNativeTag, isPointer);
240 }
241
242 public String toString(final String functionName, final String callingConvention,
243 final boolean emitNativeTag, final boolean isPointer) {
244 final StringBuilder res = new StringBuilder();
245 res.append(getReturnType().getCName(true));
246 res.append(" ");
247 if (isPointer) {
248 res.append("(");
249 if (callingConvention != null) {
250 res.append(callingConvention);
251 }
252 res.append("*");
253 }
254 if (functionName != null) {
255 if (emitNativeTag) {
256 // Emit @native tag for javadoc purposes
257 res.append("{@native ");
258 }
259 res.append(functionName);
260 if (emitNativeTag) {
261 res.append("}");
262 }
263 }
264 if (isPointer) {
265 res.append(")");
266 }
267 res.append("(");
268 getParameterList(res, true, callingConvention);
269 res.append(")");
270 return res.toString();
271 }
272
273 @Override
274 public void visit(final TypeVisitor arg) {
275 super.visit(arg);
276 returnType.visit(arg);
277 final int n = getNumArguments();
278 for (int i = 0; i < n; i++) {
279 getArgumentType(i).visit(arg);
280 }
281 }
282}
com.jogamp.gluegen.ASTLocusTag
An AST location tag.
Definition: ASTLocusTag.java:33
com.jogamp.gluegen.cgram.types.FunctionType
Describes a function type, used to model both function declarations and (via PointerType) function po...
Definition: FunctionType.java:48
com.jogamp.gluegen.cgram.types.FunctionType.getArgumentName
String getArgumentName(final int i)
Returns the name of the ith argument.
Definition: FunctionType.java:129
com.jogamp.gluegen.cgram.types.FunctionType.FunctionType
FunctionType(final String name, final SizeThunk size, final Type returnType, final int cvAttributes)
Definition: FunctionType.java:54
com.jogamp.gluegen.cgram.types.FunctionType.getReturnType
Type getReturnType()
Returns the return type of this function.
Definition: FunctionType.java:119
com.jogamp.gluegen.cgram.types.FunctionType.toString
String toString(final String functionName, final boolean emitNativeTag)
Definition: FunctionType.java:235
com.jogamp.gluegen.cgram.types.FunctionType.setArgumentName
void setArgumentName(final int i, final String name)
Definition: FunctionType.java:225
com.jogamp.gluegen.cgram.types.FunctionType.getParameterList
StringBuilder getParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention, final List< Integer > exclude)
Returns the function parameter list, i.e.
Definition: FunctionType.java:156
com.jogamp.gluegen.cgram.types.FunctionType.toString
String toString()
Returns a string representation of this type.
Definition: FunctionType.java:231
com.jogamp.gluegen.cgram.types.FunctionType.hashCodeImpl
int hashCodeImpl()
Definition: FunctionType.java:86
com.jogamp.gluegen.cgram.types.FunctionType.getArgumentType
Type getArgumentType(final int i)
Returns the type of the ith argument.
Definition: FunctionType.java:134
com.jogamp.gluegen.cgram.types.FunctionType.asFunction
FunctionType asFunction()
Casts this to a FunctionType or returns null if not a FunctionType.
Definition: FunctionType.java:114
com.jogamp.gluegen.cgram.types.FunctionType.forEachParameter
int forEachParameter(final ParameterConsumer c)
Definition: FunctionType.java:201
com.jogamp.gluegen.cgram.types.FunctionType.equalSemanticsImpl
boolean equalSemanticsImpl(final Type arg)
Definition: FunctionType.java:107
com.jogamp.gluegen.cgram.types.FunctionType.FunctionType
FunctionType(final String name, final SizeThunk size, final Type returnType, final int cvAttributes, final ASTLocusTag astLocus)
Definition: FunctionType.java:58
com.jogamp.gluegen.cgram.types.FunctionType.visit
void visit(final TypeVisitor arg)
Traverse this Type and all of its component types; for example, the return type and argument types of...
Definition: FunctionType.java:274
com.jogamp.gluegen.cgram.types.FunctionType.toString
String toString(final String functionName, final boolean emitNativeTag, final boolean isPointer)
Definition: FunctionType.java:238
com.jogamp.gluegen.cgram.types.FunctionType.toString
String toString(final String functionName, final String callingConvention, final boolean emitNativeTag, final boolean isPointer)
Definition: FunctionType.java:242
com.jogamp.gluegen.cgram.types.FunctionType.hashCodeSemanticsImpl
int hashCodeSemanticsImpl()
Definition: FunctionType.java:100
com.jogamp.gluegen.cgram.types.FunctionType.addArgument
void addArgument(final Type argumentType, final String argumentName)
Add an argument's name and type.
Definition: FunctionType.java:215
com.jogamp.gluegen.cgram.types.FunctionType.getParameterList
StringBuilder getParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention)
Returns the function parameter list, i.e.
Definition: FunctionType.java:145
com.jogamp.gluegen.cgram.types.FunctionType.getNumArguments
int getNumArguments()
Definition: FunctionType.java:123
com.jogamp.gluegen.cgram.types.FunctionType.equalsImpl
boolean equalsImpl(final Type arg)
Definition: FunctionType.java:93
com.jogamp.gluegen.cgram.types.SizeThunk
Provides a level of indirection between the definition of a type's size and the absolute value of thi...
Definition: SizeThunk.java:51
com.jogamp.gluegen.cgram.types.TypeComparator
Definition: TypeComparator.java:32
com.jogamp.gluegen.cgram.types.Type
Models a C type.
Definition: Type.java:54
com.jogamp.gluegen.cgram.types.Type.isArray
final boolean isArray()
Indicates whether this is an ArrayType.
Definition: Type.java:409
com.jogamp.gluegen.cgram.types.Type.hashCodeSemantics
final int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: Type.java:497
com.jogamp.gluegen.cgram.types.Type.Type
Type(final String name, final SizeThunk size, final int cvAttributes, final ASTLocusTag astLocus)
Definition: Type.java:67
com.jogamp.gluegen.cgram.types.Type.hashCode
final int hashCode()
Hashcode for Types.
Definition: Type.java:446
com.jogamp.gluegen.cgram.types.Type.isVoid
final boolean isVoid()
Indicates whether this is a VoidType.
Definition: Type.java:415
com.jogamp.gluegen.cgram.types.Type.isFunctionPointer
boolean isFunctionPointer()
Convenience routine indicating whether this Type is a pointer to a function.
Definition: Type.java:430
com.jogamp.gluegen.cgram.types.Type.equalSemantics
final boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: Type.java:514
com.jogamp.gluegen.cgram.types.Type.visit
void visit(final TypeVisitor visitor)
Traverse this Type and all of its component types; for example, the return type and argument types of...
Definition: Type.java:544
com.jogamp.gluegen.cgram.types.Type.equals
final boolean equals(final Object arg)
Equality test for Types inclusive its given name.
Definition: Type.java:468
com.jogamp.gluegen.cgram.types.Type.newCVVariant
final Type newCVVariant(final int cvAttributes)
Return a variant of this type matching the given const/volatile attributes.
Definition: Type.java:98
com.jogamp.gluegen.cgram.types.Type.getCVAttributes
final int getCVAttributes()
Definition: Type.java:548
com.jogamp.gluegen.cgram.types.FunctionType.ParameterConsumer
forEachParameter(ParameterConsumer) Consumer
Definition: FunctionType.java:189
com.jogamp.gluegen.cgram.types.FunctionType.ParameterConsumer.accept
boolean accept(int idx, int consumedCount, Type cType, String name)
Accept the arguments of the traversed collection element and return true if consumed.
com.jogamp.gluegen.cgram.types.TypeVisitor
A visitor for Type's visitor model.
Definition: TypeVisitor.java:45