GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
FunctionSymbol.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.List;
43
44import com.jogamp.gluegen.ASTLocusTag;
45import com.jogamp.gluegen.ASTLocusTag.ASTLocusTagProvider;
46import com.jogamp.gluegen.cgram.types.AliasedSymbol.AliasedSymbolImpl;
47import com.jogamp.gluegen.cgram.types.TypeComparator.AliasedSemanticSymbol;
48import com.jogamp.gluegen.cgram.types.TypeComparator.SemanticEqualityOp;
49
50
51/**
52 * Describes a function symbol, which includes the name and
53 * type. Since we are currently only concerned with processing
54 * functions this is the only symbol type, though plausibly more
55 * types should be added and a true symbol table constructed during parsing.
56 * <p>
57 * Note: Since C does not support method-overloading polymorphism like C++ or Java,
58 * we ignore the {@link FunctionType} attribute in {@link #equals(Object)} and {@link #hashCode()}.<br/>
59 * Hence we assume all method occurrences w/ same name are of equal or compatible type. <br/>
60 * Deep comparison can be performed via {@link #isCompletelyEqual(Object o)};
61 * </p>
62 **/
64
65 private final FunctionType type;
66 private final ASTLocusTag astLocus;
67
68 public FunctionSymbol(final String name, final FunctionType type) {
69 this(name, type, null);
70 }
71
72 public FunctionSymbol(final String name, final FunctionType type, final ASTLocusTag locus) {
73 super(name);
74 this.type = type;
75 this.astLocus = locus;
76 addAliasedName( type.getCName() ); // be reachable via typename
77 }
78
79 /** Shallow'ish copy, only aliased names are re-created. */
81 return new FunctionSymbol(o);
82 }
83 /** Warning: Shallow'ish copy, only aliased names are re-created. */
84 private FunctionSymbol(final FunctionSymbol o) {
85 super(o);
86 this.type = o.type;
87 this.astLocus = o.astLocus;
88 }
89
90 @Override
91 public ASTLocusTag getASTLocusTag() { return astLocus; }
92
93 /** Returns the type of this function. Do not add arguments to it
94 directly; use addArgument instead. */
96 return type;
97 }
98
99 /** Returns the return type of this function. */
101 return type.getReturnType();
102 }
103
104 public int getNumArguments() {
105 return type.getNumArguments();
106 }
107
108 /** Returns the name of the <i>i</i>th argument. May return null if
109 no argument names were available during parsing. */
110 public String getArgumentName(final int i) {
111 return type.getArgumentName(i);
112 }
113
114 /** Returns the type of the <i>i</i>th argument. */
115 public Type getArgumentType(final int i) {
116 return type.getArgumentType(i);
117 }
118
119 /** Add an argument's name and type. Use null for unknown argument
120 names. */
121 public void addArgument(final Type argumentType, final String argumentName) {
122 type.addArgument(argumentType, argumentName);
123 }
124
125 @Override
126 public String toString() {
127 return getType().toString(getName(), false);
128 }
129
130 /** Helper routine for emitting native javadoc tags */
131 public String toString(final boolean emitNativeTag) {
132 return getType().toString(getName(), emitNativeTag);
133 }
134
135 @Override
136 public int hashCode() {
137 if (getName() == null) {
138 return 0;
139 }
140 return getName().hashCode();
141 }
142
143 @Override
144 public boolean equals(final Object arg) {
145 if (arg == this) {
146 return true;
147 }
148 if ( !(arg instanceof FunctionSymbol) ) {
149 return false;
150 }
151 final FunctionSymbol other = (FunctionSymbol) arg;
152 if (getName() == null && other.getName() != null) {
153 return false;
154 }
155 return getName().equals(other.getName());
156 }
157
158 @Override
159 public int hashCodeSemantics() {
160 return type.hashCodeSemantics();
161 }
162 @Override
163 public final boolean equalSemantics(final SemanticEqualityOp arg) {
164 if (arg == this) {
165 return true;
166 }
167 if ( !(arg instanceof FunctionSymbol) ) {
168 return false;
169 }
170 final FunctionSymbol other = (FunctionSymbol) arg;
171 return type.equalSemantics(other.type);
172 }
173
174
175 public static boolean containsExactly(final List<FunctionSymbol> l, final FunctionSymbol s) {
176 return exactIndexOf(l, s) >= 0;
177 }
178
179 public static int exactIndexOf(final List<FunctionSymbol> l, final FunctionSymbol s) {
180 final int size = l.size();
181 for (int i = 0; i < size; i++) {
182 final FunctionSymbol e = l.get(i);
183 if( null == s && null == e ||
184 s.equals( e ) && s.type.equals(e.type) ) {
185 return i;
186 }
187 }
188 return -1;
189 }
190
191 /**
192 * Compares the function type as well, since {@link #equals(Object)}
193 * and {@link #hashCode()} won't.
194 */
195 public boolean exactlyEqual(final Object arg) {
196 if( !this.equals(arg) ) {
197 return false;
198 }
199 return type.equals( ((FunctionSymbol)arg).type );
200 }
201}
String getName()
Return the current-name, which is the last renamed-name if issued, or the original-name.
void addAliasedName(final String origName)
Add the given origName to the list of aliases if not equal current-name.
Describes a function symbol, which includes the name and type.
int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
ASTLocusTag getASTLocusTag()
Returns this instance's ASTLocusTag, if available, otherwise returns null.
static int exactIndexOf(final List< FunctionSymbol > l, final FunctionSymbol s)
final boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
static boolean containsExactly(final List< FunctionSymbol > l, final FunctionSymbol s)
static FunctionSymbol cloneWithDeepAliases(final FunctionSymbol o)
Shallow'ish copy, only aliased names are re-created.
FunctionType getType()
Returns the type of this function.
String toString(final boolean emitNativeTag)
Helper routine for emitting native javadoc tags.
FunctionSymbol(final String name, final FunctionType type)
Type getArgumentType(final int i)
Returns the type of the ith argument.
boolean exactlyEqual(final Object arg)
Compares the function type as well, since equals(Object) and hashCode() won't.
FunctionSymbol(final String name, final FunctionType type, final ASTLocusTag locus)
void addArgument(final Type argumentType, final String argumentName)
Add an argument's name and type.
Type getReturnType()
Returns the return type of this function.
String getArgumentName(final int i)
Returns the name of the ith argument.
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.
Type getReturnType()
Returns the return type of this function.
String toString()
Returns a string representation of this type.
Type getArgumentType(final int i)
Returns the type of the ith argument.
void addArgument(final Type argumentType, final String argumentName)
Add an argument's name and type.
final String getCName()
Returns the name of this type.
Definition: Type.java:132
final int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: Type.java:497
final boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: Type.java:514
final boolean equals(final Object arg)
Equality test for Types inclusive its given name.
Definition: Type.java:468
Interface tag for ASTLocusTag provider.
Supports common interface for SemanticEqualityOp and AliasedSymbol.
Supports semantic equality and hash functions for types.