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

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 **/
63 public class FunctionSymbol extends AliasedSymbolImpl implements AliasedSemanticSymbol, ASTLocusTagProvider {
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. */
80 public static FunctionSymbol cloneWithDeepAliases(final FunctionSymbol o) {
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. */
95 public FunctionType getType() {
96 return type;
97 }
98
99 /** Returns the return type of this function. */
100 public Type getReturnType() {
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}
com.jogamp.gluegen.ASTLocusTag
An AST location tag.
Definition: ASTLocusTag.java:33
com.jogamp.gluegen.cgram.types.AliasedSymbol.AliasedSymbolImpl
Definition: AliasedSymbol.java:97
com.jogamp.gluegen.cgram.types.AliasedSymbol.AliasedSymbolImpl.getName
String getName()
Return the current-name, which is the last renamed-name if issued, or the original-name.
Definition: AliasedSymbol.java:142
com.jogamp.gluegen.cgram.types.AliasedSymbol.AliasedSymbolImpl.addAliasedName
void addAliasedName(final String origName)
Add the given origName to the list of aliases if not equal current-name.
Definition: AliasedSymbol.java:124
com.jogamp.gluegen.cgram.types.FunctionSymbol
Describes a function symbol, which includes the name and type.
Definition: FunctionSymbol.java:63
com.jogamp.gluegen.cgram.types.FunctionSymbol.hashCodeSemantics
int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: FunctionSymbol.java:159
com.jogamp.gluegen.cgram.types.FunctionSymbol.getASTLocusTag
ASTLocusTag getASTLocusTag()
Returns this instance's ASTLocusTag, if available, otherwise returns null.
Definition: FunctionSymbol.java:91
com.jogamp.gluegen.cgram.types.FunctionSymbol.equals
boolean equals(final Object arg)
Definition: FunctionSymbol.java:144
com.jogamp.gluegen.cgram.types.FunctionSymbol.hashCode
int hashCode()
Definition: FunctionSymbol.java:136
com.jogamp.gluegen.cgram.types.FunctionSymbol.exactIndexOf
static int exactIndexOf(final List< FunctionSymbol > l, final FunctionSymbol s)
Definition: FunctionSymbol.java:179
com.jogamp.gluegen.cgram.types.FunctionSymbol.getNumArguments
int getNumArguments()
Definition: FunctionSymbol.java:104
com.jogamp.gluegen.cgram.types.FunctionSymbol.equalSemantics
final boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: FunctionSymbol.java:163
com.jogamp.gluegen.cgram.types.FunctionSymbol.containsExactly
static boolean containsExactly(final List< FunctionSymbol > l, final FunctionSymbol s)
Definition: FunctionSymbol.java:175
com.jogamp.gluegen.cgram.types.FunctionSymbol.cloneWithDeepAliases
static FunctionSymbol cloneWithDeepAliases(final FunctionSymbol o)
Shallow'ish copy, only aliased names are re-created.
Definition: FunctionSymbol.java:80
com.jogamp.gluegen.cgram.types.FunctionSymbol.getType
FunctionType getType()
Returns the type of this function.
Definition: FunctionSymbol.java:95
com.jogamp.gluegen.cgram.types.FunctionSymbol.toString
String toString(final boolean emitNativeTag)
Helper routine for emitting native javadoc tags.
Definition: FunctionSymbol.java:131
com.jogamp.gluegen.cgram.types.FunctionSymbol.FunctionSymbol
FunctionSymbol(final String name, final FunctionType type)
Definition: FunctionSymbol.java:68
com.jogamp.gluegen.cgram.types.FunctionSymbol.getArgumentType
Type getArgumentType(final int i)
Returns the type of the ith argument.
Definition: FunctionSymbol.java:115
com.jogamp.gluegen.cgram.types.FunctionSymbol.exactlyEqual
boolean exactlyEqual(final Object arg)
Compares the function type as well, since equals(Object) and hashCode() won't.
Definition: FunctionSymbol.java:195
com.jogamp.gluegen.cgram.types.FunctionSymbol.FunctionSymbol
FunctionSymbol(final String name, final FunctionType type, final ASTLocusTag locus)
Definition: FunctionSymbol.java:72
com.jogamp.gluegen.cgram.types.FunctionSymbol.addArgument
void addArgument(final Type argumentType, final String argumentName)
Add an argument's name and type.
Definition: FunctionSymbol.java:121
com.jogamp.gluegen.cgram.types.FunctionSymbol.getReturnType
Type getReturnType()
Returns the return type of this function.
Definition: FunctionSymbol.java:100
com.jogamp.gluegen.cgram.types.FunctionSymbol.getArgumentName
String getArgumentName(final int i)
Returns the name of the ith argument.
Definition: FunctionSymbol.java:110
com.jogamp.gluegen.cgram.types.FunctionSymbol.toString
String toString()
Definition: FunctionSymbol.java:126
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.getReturnType
Type getReturnType()
Returns the return type of this function.
Definition: FunctionType.java:119
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.getArgumentType
Type getArgumentType(final int i)
Returns the type of the ith argument.
Definition: FunctionType.java:134
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.getNumArguments
int getNumArguments()
Definition: FunctionType.java:123
com.jogamp.gluegen.cgram.types.Type
Models a C type.
Definition: Type.java:54
com.jogamp.gluegen.cgram.types.Type.getCName
final String getCName()
Returns the name of this type.
Definition: Type.java:132
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.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.equals
final boolean equals(final Object arg)
Equality test for Types inclusive its given name.
Definition: Type.java:468
com.jogamp.gluegen.ASTLocusTag.ASTLocusTagProvider
Interface tag for ASTLocusTag provider.
Definition: ASTLocusTag.java:92
com.jogamp.gluegen.cgram.types.TypeComparator.AliasedSemanticSymbol
Supports common interface for SemanticEqualityOp and AliasedSymbol.
Definition: TypeComparator.java:52
com.jogamp.gluegen.cgram.types.TypeComparator.SemanticEqualityOp
Supports semantic equality and hash functions for types.
Definition: TypeComparator.java:36