GlueGen v2.6.0-rc-20250712
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. */
48public 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
115 return this;
116 }
117
118 /** Returns the return type of this function. */
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 }
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}
Describes a function type, used to model both function declarations and (via PointerType) function po...
String getArgumentName(final int i)
Returns the name of the ith argument.
FunctionType(final String name, final SizeThunk size, final Type returnType, final int cvAttributes)
Type getReturnType()
Returns the return type of this function.
String toString(final String functionName, final boolean emitNativeTag)
void setArgumentName(final int i, final String name)
StringBuilder getParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention, final List< Integer > exclude)
Returns the function parameter list, i.e.
String toString()
Returns a string representation of this type.
Type getArgumentType(final int i)
Returns the type of the ith argument.
FunctionType asFunction()
Casts this to a FunctionType or returns null if not a FunctionType.
int forEachParameter(final ParameterConsumer c)
FunctionType(final String name, final SizeThunk size, final Type returnType, final int cvAttributes, final ASTLocusTag astLocus)
void visit(final TypeVisitor arg)
Traverse this Type and all of its component types; for example, the return type and argument types of...
String toString(final String functionName, final boolean emitNativeTag, final boolean isPointer)
String toString(final String functionName, final String callingConvention, final boolean emitNativeTag, final boolean isPointer)
void addArgument(final Type argumentType, final String argumentName)
Add an argument's name and type.
StringBuilder getParameterList(final StringBuilder buf, final boolean useTypedef, final String callingConvention)
Returns the function parameter list, i.e.
Provides a level of indirection between the definition of a type's size and the absolute value of thi...
Definition: SizeThunk.java:51
final boolean isArray()
Indicates whether this is an ArrayType.
Definition: Type.java:409
final int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: Type.java:497
Type(final String name, final SizeThunk size, final int cvAttributes, final ASTLocusTag astLocus)
Definition: Type.java:67
final int hashCode()
Hashcode for Types.
Definition: Type.java:446
final boolean isVoid()
Indicates whether this is a VoidType.
Definition: Type.java:415
boolean isFunctionPointer()
Convenience routine indicating whether this Type is a pointer to a function.
Definition: Type.java:430
final boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: Type.java:514
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
final boolean equals(final Object arg)
Equality test for Types inclusive its given name.
Definition: Type.java:468
final Type newCVVariant(final int cvAttributes)
Return a variant of this type matching the given const/volatile attributes.
Definition: Type.java:98
forEachParameter(ParameterConsumer) Consumer
boolean accept(int idx, int consumedCount, Type cType, String name)
Accept the arguments of the traversed collection element and return true if consumed.
A visitor for Type's visitor model.