GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
CompoundType.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (c) 2010 JogAmp Community. 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 */
40
41package com.jogamp.gluegen.cgram.types;
42
43import java.util.*;
44
45import com.jogamp.gluegen.ASTLocusTag;
46
47/** Models all compound types, i.e., those containing fields: structs
48 and unions. The boolean type accessors indicate how the type is
49 really defined. */
50
51public abstract class CompoundType extends MemoryLayoutType implements Cloneable, AliasedSymbol {
52 // The name "foo" in the construct "struct foo { ... }";
53 private final String structName;
54 private ArrayList<Field> fields;
55 private boolean visiting;
56 private boolean bodyParsed;
57
58 @Override
59 public void rename(final String newName) {
60 throw new UnsupportedOperationException();
61 }
62 @Override
63 public void addAliasedName(final String origName) {
64 throw new UnsupportedOperationException();
65 }
66 @Override
67 public boolean hasAliases() {
68 return false;
69 }
70 @Override
71 public Set<String> getAliasedNames() {
72 return null;
73 }
74 @Override
75 public String getAliasedString() {
76 return toString();
77 }
78 @Override
79 public String getOrigName() {
80 return getName();
81 }
82 /**
83 * @param structName struct name of this CompoundType, i.e. the "foo" in the
84 construct {@code struct foo { int a, ... };} or {@code struct foo;} <i>even</i> for anonymous structs.
85 * @param size
86 * @param kind
87 * @param cvAttributes
88 * @return
89 */
90 public static CompoundType create(final String structName, final SizeThunk size,
91 final CompoundTypeKind kind, final int cvAttributes,
92 final ASTLocusTag astLocus)
93 {
94 final CompoundType res;
95 switch (kind) {
96 case STRUCT:
97 res = new StructType(null, size, cvAttributes, structName, astLocus);
98 break;
99 case UNION:
100 res = new UnionType(null, size, cvAttributes, structName, astLocus);
101 break;
102 default:
103 throw new RuntimeException("OO relation "+kind+" / Compount not yet supported");
104 }
105 return res;
106 }
107
108 CompoundType(final String name, final SizeThunk size, final int cvAttributes,
109 final String structName, final ASTLocusTag astLocus) {
110 super(null == name ? structName : name, size, cvAttributes, astLocus);
111 this.structName = structName;
112 }
113
114 CompoundType(final CompoundType o, final int cvAttributes, final ASTLocusTag astLocus) {
115 super(o, cvAttributes, astLocus);
116 this.structName = o.structName;
117 if(null != o.fields) {
118 fields = new ArrayList<Field>(o.fields);
119 }
120 bodyParsed = o.bodyParsed;
121 }
122
123 @Override
124 protected int hashCodeImpl() {
125 // 31 * x == (x << 5) - x
126 final int hash = 31 + ( null != structName ? structName.hashCode() : 0 );
127 return ((hash << 5) - hash) + TypeComparator.listsHashCode(fields);
128 }
129
130 @Override
131 protected boolean equalsImpl(final Type arg) {
132 final CompoundType ct = (CompoundType) arg;
133 return ( (structName == null ? ct.structName == null : structName.equals(ct.structName)) ||
134 (structName != null && structName.equals(ct.structName))
135 ) &&
136 TypeComparator.listsEqual(fields, ct.fields);
137 }
138
139 @Override
140 protected int hashCodeSemanticsImpl() {
141 // 31 * x == (x << 5) - x
142 return TypeComparator.listsHashCodeSemantics(fields);
143 }
144
145 @Override
146 protected boolean equalSemanticsImpl(final Type arg) {
147 final CompoundType ct = (CompoundType) arg;
148 return TypeComparator.listsEqualSemantics(fields, ct.fields);
149 }
150
151 /** Returns the struct name of this CompoundType, i.e. the "foo" in
152 the construct "struct foo { ... };". */
153 public String getStructName() {
154 return structName;
155 }
156
157 @Override
158 public CompoundType asCompound() { return this; }
159
160 @Override
161 public String getCName(final boolean includeCVAttrs) {
162 if( isTypedef() ) {
163 return getName(includeCVAttrs);
164 } else {
165 return (isStruct() ? "struct " : "union ")+getName(includeCVAttrs);
166 }
167 }
168
169 ArrayList<Field> getFields() { return fields; }
170 void setFields(final ArrayList<Field> fields) { this.fields = fields; clearCache(); }
171
172 /** Returns the number of fields in this type. */
173 public int getNumFields() {
174 return ((fields == null) ? 0 : fields.size());
175 }
176
177 /** Returns the <i>i</i>th field of this type. */
178 public Field getField(final int i) {
179 return fields.get(i);
180 }
181
182 /** Returns the <i>name</i> matching field of this type. */
183 public Field getField(final String name) {
184 for(final Field f : fields ) {
185 if( f.getName().equals(name) ) {
186 return f;
187 }
188 }
189 return null;
190 }
191
192 /** Adds a field to this type. */
193 public void addField(final Field f) {
194 if (bodyParsed) {
195 throw new IllegalStateException(String.format(
196 "Body of this CompoundType (%s) has been already closed (Field supplied %s)", this, f
197 ));
198 }
199 if (fields == null) {
200 fields = new ArrayList<Field>();
201 }
202 fields.add(f);
203 clearCache();
204 }
205
206 /**
207 * Indicates to this CompoundType that its body has been parsed and
208 * that no more {@link #addField} operations will be made.
209 * @throws IllegalStateException If called twice.
210 */
211 public void setBodyParsed() throws IllegalStateException {
212 if (bodyParsed) {
213 throw new IllegalStateException(String.format(
214 "Body of this CompoundType (%s) has been already closed", this
215 ));
216 }
217 bodyParsed = true;
218 }
219
220 /** Indicates whether this type was declared as a struct. */
221 public abstract boolean isStruct();
222 /** Indicates whether this type was declared as a union. */
223 public abstract boolean isUnion();
224
225 @Override
226 public String toString() {
227 final String cvAttributesString = getCVAttributesString();
228 final String cname = getCName();
229 if ( null != cname ) {
230 return cvAttributesString + cname;
231 } else if (getStructName() != null) {
232 return cvAttributesString + "struct " + getStructName();
233 } else {
234 return cvAttributesString + getStructString();
235 }
236 }
237
238 @Override
239 public void visit(final TypeVisitor arg) {
240 if (visiting) {
241 return;
242 }
243 try {
244 visiting = true;
245 super.visit(arg);
246 final int n = getNumFields();
247 for (int i = 0; i < n; i++) {
248 getField(i).getType().visit(arg);
249 }
250 } finally {
251 visiting = false;
252 }
253 return;
254 }
255
256 public String getStructString() {
257 if (visiting) {
258 if (getName() != null) {
259 return getName();
260 }
261 return "struct {/*Recursive type reference*/}";
262 }
263
264 try {
265 visiting = true;
266 final String kind = (isStruct() ? "struct {" : "union {");
267 final StringBuilder res = new StringBuilder();
268 res.append(kind);
269 final int n = getNumFields();
270 for (int i = 0; i < n; i++) {
271 res.append(" ");
272 res.append(getField(i));
273 }
274 res.append(" }");
275 return res.toString();
276 } finally {
277 visiting = false;
278 }
279 }
280}
Models all compound types, i.e., those containing fields: structs and unions.
void rename(final String newName)
Rename this symbol with the given newName if not equal current-name.
void setBodyParsed()
Indicates to this CompoundType that its body has been parsed and that no more addField operations wil...
String getAliasedString()
Return this object's toString() wrapped w/ the current-name and all aliases.
Field getField(final int i)
Returns the ith field of this type.
String getOrigName()
Return the original-name as set at creation.
void addField(final Field f)
Adds a field to this type.
Field getField(final String name)
Returns the name matching field of this type.
int getNumFields()
Returns the number of fields in this type.
String getStructName()
Returns the struct name of this CompoundType, i.e.
void addAliasedName(final String origName)
Add the given origName to the list of aliases if not equal current-name.
Set< String > getAliasedNames()
Return all aliases for this symbol, i.e.
String getCName(final boolean includeCVAttrs)
Returns the name of this type, optionally including const/volatile attributes.
abstract boolean isStruct()
Indicates whether this type was declared as a struct.
String toString()
Returns a string representation of this type.
static CompoundType create(final String structName, final SizeThunk size, final CompoundTypeKind kind, 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...
abstract boolean isUnion()
Indicates whether this type was declared as a union.
boolean hasAliases()
Returns true if this symbol has aliases, i.e.
CompoundType asCompound()
Casts this to a CompoundType or returns null if not a CompoundType.
Represents a field in a struct or union.
Definition: Field.java:47
Type getType()
Type of this field.
Definition: Field.java:101
Provides a level of indirection between the definition of a type's size and the absolute value of thi...
Definition: SizeThunk.java:51
final String getCName()
Returns the name of this type.
Definition: Type.java:132
final String getCVAttributesString()
Returns a string indicating the const/volatile attributes of this type.
Definition: Type.java:554
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 isTypedef()
Indicates whether this type is a typedef type, i.e.
Definition: Type.java:352
Type-safe enum for discriminating between structs and unions represented as compound types.
String getName()
Return the current-name, which is the last renamed-name if issued, or the original-name.
A visitor for Type's visitor model.