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

GlueGen v2.6.0 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
51 public 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}
com.jogamp.gluegen.ASTLocusTag
An AST location tag.
Definition: ASTLocusTag.java:33
com.jogamp.gluegen.cgram.types.CompoundType
Models all compound types, i.e., those containing fields: structs and unions.
Definition: CompoundType.java:51
com.jogamp.gluegen.cgram.types.CompoundType.hashCodeSemanticsImpl
int hashCodeSemanticsImpl()
Definition: CompoundType.java:140
com.jogamp.gluegen.cgram.types.CompoundType.rename
void rename(final String newName)
Rename this symbol with the given newName if not equal current-name.
Definition: CompoundType.java:59
com.jogamp.gluegen.cgram.types.CompoundType.setBodyParsed
void setBodyParsed()
Indicates to this CompoundType that its body has been parsed and that no more addField operations wil...
Definition: CompoundType.java:211
com.jogamp.gluegen.cgram.types.CompoundType.getAliasedString
String getAliasedString()
Return this object's toString() wrapped w/ the current-name and all aliases.
Definition: CompoundType.java:75
com.jogamp.gluegen.cgram.types.CompoundType.hashCodeImpl
int hashCodeImpl()
Definition: CompoundType.java:124
com.jogamp.gluegen.cgram.types.CompoundType.equalSemanticsImpl
boolean equalSemanticsImpl(final Type arg)
Definition: CompoundType.java:146
com.jogamp.gluegen.cgram.types.CompoundType.getField
Field getField(final int i)
Returns the ith field of this type.
Definition: CompoundType.java:178
com.jogamp.gluegen.cgram.types.CompoundType.getOrigName
String getOrigName()
Return the original-name as set at creation.
Definition: CompoundType.java:79
com.jogamp.gluegen.cgram.types.CompoundType.addField
void addField(final Field f)
Adds a field to this type.
Definition: CompoundType.java:193
com.jogamp.gluegen.cgram.types.CompoundType.getField
Field getField(final String name)
Returns the name matching field of this type.
Definition: CompoundType.java:183
com.jogamp.gluegen.cgram.types.CompoundType.getNumFields
int getNumFields()
Returns the number of fields in this type.
Definition: CompoundType.java:173
com.jogamp.gluegen.cgram.types.CompoundType.getStructName
String getStructName()
Returns the struct name of this CompoundType, i.e.
Definition: CompoundType.java:153
com.jogamp.gluegen.cgram.types.CompoundType.addAliasedName
void addAliasedName(final String origName)
Add the given origName to the list of aliases if not equal current-name.
Definition: CompoundType.java:63
com.jogamp.gluegen.cgram.types.CompoundType.getStructString
String getStructString()
Definition: CompoundType.java:256
com.jogamp.gluegen.cgram.types.CompoundType.getAliasedNames
Set< String > getAliasedNames()
Return all aliases for this symbol, i.e.
Definition: CompoundType.java:71
com.jogamp.gluegen.cgram.types.CompoundType.getCName
String getCName(final boolean includeCVAttrs)
Returns the name of this type, optionally including const/volatile attributes.
Definition: CompoundType.java:161
com.jogamp.gluegen.cgram.types.CompoundType.isStruct
abstract boolean isStruct()
Indicates whether this type was declared as a struct.
com.jogamp.gluegen.cgram.types.CompoundType.toString
String toString()
Returns a string representation of this type.
Definition: CompoundType.java:226
com.jogamp.gluegen.cgram.types.CompoundType.equalsImpl
boolean equalsImpl(final Type arg)
Definition: CompoundType.java:131
com.jogamp.gluegen.cgram.types.CompoundType.create
static CompoundType create(final String structName, final SizeThunk size, final CompoundTypeKind kind, final int cvAttributes, final ASTLocusTag astLocus)
Definition: CompoundType.java:90
com.jogamp.gluegen.cgram.types.CompoundType.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: CompoundType.java:239
com.jogamp.gluegen.cgram.types.CompoundType.isUnion
abstract boolean isUnion()
Indicates whether this type was declared as a union.
com.jogamp.gluegen.cgram.types.CompoundType.hasAliases
boolean hasAliases()
Returns true if this symbol has aliases, i.e.
Definition: CompoundType.java:67
com.jogamp.gluegen.cgram.types.CompoundType.asCompound
CompoundType asCompound()
Casts this to a CompoundType or returns null if not a CompoundType.
Definition: CompoundType.java:158
com.jogamp.gluegen.cgram.types.Field
Represents a field in a struct or union.
Definition: Field.java:47
com.jogamp.gluegen.cgram.types.Field.getType
Type getType()
Type of this field.
Definition: Field.java:101
com.jogamp.gluegen.cgram.types.MemoryLayoutType
Definition: MemoryLayoutType.java:32
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.StructType
Definition: StructType.java:32
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.getCName
final String getCName()
Returns the name of this type.
Definition: Type.java:132
com.jogamp.gluegen.cgram.types.Type.getCVAttributesString
final String getCVAttributesString()
Returns a string indicating the const/volatile attributes of this type.
Definition: Type.java:554
com.jogamp.gluegen.cgram.types.Type.clearCache
final void clearCache()
Definition: Type.java:87
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.isTypedef
final boolean isTypedef()
Indicates whether this type is a typedef type, i.e.
Definition: Type.java:352
com.jogamp.gluegen.cgram.types.UnionType
Definition: UnionType.java:32
com.jogamp.gluegen.cgram.types.CompoundTypeKind
Type-safe enum for discriminating between structs and unions represented as compound types.
Definition: CompoundTypeKind.java:33
com.jogamp.gluegen.cgram.types.AliasedSymbol
Supports symbol aliasing, i.e.
Definition: AliasedSymbol.java:37
com.jogamp.gluegen.cgram.types.AliasedSymbol.getName
String getName()
Return the current-name, which is the last renamed-name if issued, or the original-name.
com.jogamp.gluegen.cgram.types.TypeVisitor
A visitor for Type's visitor model.
Definition: TypeVisitor.java:45