GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
ArrayType.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 com.jogamp.gluegen.ASTLocusTag;
44
45/** Represents an array type. This differs from a pointer type in C
46 syntax by the use of "[]" rather than "*". The length may or may
47 not be known; if the length is unknown then a negative number
48 should be passed in to the constructor. */
49
50public class ArrayType extends MemoryLayoutType implements Cloneable {
51 private final Type elementType;
52 private final int length;
53
54 public ArrayType(final Type elementType, final SizeThunk sizeInBytes, final int length,
55 final int cvAttributes) {
56 this(elementType, sizeInBytes, length, cvAttributes, null);
57 }
58 public ArrayType(final Type elementType, final SizeThunk sizeInBytes, final int length,
59 final int cvAttributes, final ASTLocusTag astLocus) {
60 super(elementType.getName() + " *", sizeInBytes, cvAttributes, astLocus);
61 this.elementType = elementType;
62 this.length = length;
63 }
64 private ArrayType(final ArrayType o, final int cvAttributes, final ASTLocusTag astLocus) {
65 super(o, cvAttributes, astLocus);
66 elementType = o.elementType;
67 length = o.length;
68 }
69
70 @Override
71 Type newVariantImpl(final boolean newCVVariant, final int cvAttributes, final ASTLocusTag astLocus) {
72 return new ArrayType(this, cvAttributes, astLocus);
73 }
74
75 @Override
76 protected int hashCodeImpl() {
77 // 31 * x == (x << 5) - x
78 final int hash = elementType.hashCode();
79 return ((hash << 5) - hash) + length;
80 }
81
82 @Override
83 protected boolean equalsImpl(final Type arg) {
84 final ArrayType t = (ArrayType) arg;
85 return elementType.equals(t.elementType) &&
86 length == t.length;
87 }
88
89 @Override
90 protected int hashCodeSemanticsImpl() {
91 // 31 * x == (x << 5) - x
92 final int hash = elementType.hashCodeSemantics();
93 return ((hash << 5) - hash) + length;
94 }
95
96 @Override
97 protected boolean equalSemanticsImpl(final Type arg) {
98 final ArrayType t = (ArrayType) arg;
99 return elementType.equalSemantics(t.elementType) &&
100 length == t.length;
101 }
102
103 @Override
104 public boolean isAnon() { return elementType.isAnon(); }
105
106 @Override
107 public String getName(final boolean includeCVAttrs) {
108 return elementType.getName() + " *";
109 }
110
111 @Override
112 public final ArrayType asArray() { return this; }
113
114 @Override
115 public final Type getTargetType() { return elementType; }
116 public int getLength() { return length; }
117 public boolean hasLength() { return length >= 0; }
118
119 @Override
120 public final Type getBaseType() {
121 return elementType.getBaseType();
122 }
123
124 @Override
126 if( elementType.isPointer() ) {
127 return getTargetType();
128 } else {
129 return getBaseType();
130 }
131 }
132
133 @Override
134 public final int arrayDimension() {
135 return 1 + elementType.arrayDimension();
136 }
137
138 /** Recompute the size of this array if necessary. This needs to be
139 done when the base element type is a compound type after layouting. */
140 void recomputeSize() {
141 final ArrayType arrayElementType = getTargetType().asArray();
142 if (arrayElementType != null) {
143 arrayElementType.recomputeSize();
144 }
145 super.setSize(SizeThunk.mul(SizeThunk.constant(getLength()), elementType.getSize()));
146 }
147
148 @Override
149 public String toString() {
150 return toString(null);
151 }
152
153 public String toString(final String variableName) {
154 final StringBuilder buf = new StringBuilder();
155 if(elementType.isConst()) {
156 buf.append("const ");
157 }
158 buf.append(elementType.getCName());
159 if (variableName != null) {
160 buf.append(" ");
161 buf.append(variableName);
162 }
163 buf.append("[");
164 buf.append(length);
165 buf.append("]");
166 return buf.toString();
167 }
168
169 @Override
170 public void visit(final TypeVisitor arg) {
171 super.visit(arg);
172 elementType.visit(arg);
173 }
174}
String toString(final String variableName)
Definition: ArrayType.java:153
String toString()
Returns a string representation of this type.
Definition: ArrayType.java:149
final Type getTargetType()
Helper method to returns the target type of this type, in case another type is being referenced,...
Definition: ArrayType.java:115
String getName(final boolean includeCVAttrs)
Returns the name of this type, optionally including const/volatile attributes.
Definition: ArrayType.java:107
ArrayType(final Type elementType, final SizeThunk sizeInBytes, final int length, final int cvAttributes)
Definition: ArrayType.java:54
final ArrayType asArray()
Casts this to an ArrayType or returns null if not an ArrayType.
Definition: ArrayType.java:112
final Type getBaseType()
Helper method to returns the bottom-most element type of this type, i.e.
Definition: ArrayType.java:120
boolean equalsImpl(final Type arg)
Definition: ArrayType.java:83
final int arrayDimension()
Helper method for determining how many array dimentions this type represents (i.e....
Definition: ArrayType.java:134
ArrayType(final Type elementType, final SizeThunk sizeInBytes, final int length, final int cvAttributes, final ASTLocusTag astLocus)
Definition: ArrayType.java:58
void visit(final TypeVisitor arg)
Traverse this Type and all of its component types; for example, the return type and argument types of...
Definition: ArrayType.java:170
boolean equalSemanticsImpl(final Type arg)
Definition: ArrayType.java:97
Type getArrayBaseOrPointerTargetType()
Return getBaseType() if isArray() or returns getTargetType() otherwise.
Definition: ArrayType.java:125
Provides a level of indirection between the definition of a type's size and the absolute value of thi...
Definition: SizeThunk.java:51
static SizeThunk constant(final int constant)
Definition: SizeThunk.java:390
static SizeThunk mul(final SizeThunk thunk1, final SizeThunk thunk2)
Definition: SizeThunk.java:289
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 int hashCode()
Hashcode for Types.
Definition: Type.java:446
final boolean isConst()
Indicates whether this type is const.
Definition: Type.java:420
Type getBaseType()
Helper method to returns the bottom-most element type of this type, i.e.
Definition: Type.java:588
int arrayDimension()
Helper method for determining how many array dimentions this type represents (i.e....
Definition: Type.java:571
final boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: Type.java:514
final String getName()
Returns the name of this type.
Definition: Type.java:142
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 SizeThunk getSize()
SizeThunk which computes size of this type in bytes.
Definition: Type.java:360
final Type newCVVariant(final int cvAttributes)
Return a variant of this type matching the given const/volatile attributes.
Definition: Type.java:98
ArrayType asArray()
Casts this to an ArrayType or returns null if not an ArrayType.
Definition: Type.java:388
final boolean isPointer()
Indicates whether this is a PointerType.
Definition: Type.java:407
A visitor for Type's visitor model.