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

GlueGen v2.6.0-rc-20250721 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
50 public 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
125 public Type getArrayBaseOrPointerTargetType() {
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}
com.jogamp.gluegen.ASTLocusTag
An AST location tag.
Definition: ASTLocusTag.java:33
com.jogamp.gluegen.cgram.types.ArrayType
Represents an array type.
Definition: ArrayType.java:50
com.jogamp.gluegen.cgram.types.ArrayType.toString
String toString(final String variableName)
Definition: ArrayType.java:153
com.jogamp.gluegen.cgram.types.ArrayType.toString
String toString()
Returns a string representation of this type.
Definition: ArrayType.java:149
com.jogamp.gluegen.cgram.types.ArrayType.getTargetType
final Type getTargetType()
Helper method to returns the target type of this type, in case another type is being referenced,...
Definition: ArrayType.java:115
com.jogamp.gluegen.cgram.types.ArrayType.getName
String getName(final boolean includeCVAttrs)
Returns the name of this type, optionally including const/volatile attributes.
Definition: ArrayType.java:107
com.jogamp.gluegen.cgram.types.ArrayType.hashCodeSemanticsImpl
int hashCodeSemanticsImpl()
Definition: ArrayType.java:90
com.jogamp.gluegen.cgram.types.ArrayType.ArrayType
ArrayType(final Type elementType, final SizeThunk sizeInBytes, final int length, final int cvAttributes)
Definition: ArrayType.java:54
com.jogamp.gluegen.cgram.types.ArrayType.isAnon
boolean isAnon()
Definition: ArrayType.java:104
com.jogamp.gluegen.cgram.types.ArrayType.asArray
final ArrayType asArray()
Casts this to an ArrayType or returns null if not an ArrayType.
Definition: ArrayType.java:112
com.jogamp.gluegen.cgram.types.ArrayType.hasLength
boolean hasLength()
Definition: ArrayType.java:117
com.jogamp.gluegen.cgram.types.ArrayType.getBaseType
final Type getBaseType()
Helper method to returns the bottom-most element type of this type, i.e.
Definition: ArrayType.java:120
com.jogamp.gluegen.cgram.types.ArrayType.equalsImpl
boolean equalsImpl(final Type arg)
Definition: ArrayType.java:83
com.jogamp.gluegen.cgram.types.ArrayType.arrayDimension
final int arrayDimension()
Helper method for determining how many array dimentions this type represents (i.e....
Definition: ArrayType.java:134
com.jogamp.gluegen.cgram.types.ArrayType.ArrayType
ArrayType(final Type elementType, final SizeThunk sizeInBytes, final int length, final int cvAttributes, final ASTLocusTag astLocus)
Definition: ArrayType.java:58
com.jogamp.gluegen.cgram.types.ArrayType.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: ArrayType.java:170
com.jogamp.gluegen.cgram.types.ArrayType.equalSemanticsImpl
boolean equalSemanticsImpl(final Type arg)
Definition: ArrayType.java:97
com.jogamp.gluegen.cgram.types.ArrayType.hashCodeImpl
int hashCodeImpl()
Definition: ArrayType.java:76
com.jogamp.gluegen.cgram.types.ArrayType.getArrayBaseOrPointerTargetType
Type getArrayBaseOrPointerTargetType()
Return getBaseType() if isArray() or returns getTargetType() otherwise.
Definition: ArrayType.java:125
com.jogamp.gluegen.cgram.types.ArrayType.getLength
int getLength()
Definition: ArrayType.java:116
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.SizeThunk.constant
static SizeThunk constant(final int constant)
Definition: SizeThunk.java:390
com.jogamp.gluegen.cgram.types.SizeThunk.mul
static SizeThunk mul(final SizeThunk thunk1, final SizeThunk thunk2)
Definition: SizeThunk.java:289
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.hashCode
final int hashCode()
Hashcode for Types.
Definition: Type.java:446
com.jogamp.gluegen.cgram.types.Type.isConst
final boolean isConst()
Indicates whether this type is const.
Definition: Type.java:420
com.jogamp.gluegen.cgram.types.Type.getBaseType
Type getBaseType()
Helper method to returns the bottom-most element type of this type, i.e.
Definition: Type.java:588
com.jogamp.gluegen.cgram.types.Type.arrayDimension
int arrayDimension()
Helper method for determining how many array dimentions this type represents (i.e....
Definition: Type.java:571
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.getName
final String getName()
Returns the name of this type.
Definition: Type.java:142
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.equals
final boolean equals(final Object arg)
Equality test for Types inclusive its given name.
Definition: Type.java:468
com.jogamp.gluegen.cgram.types.Type.getSize
final SizeThunk getSize()
SizeThunk which computes size of this type in bytes.
Definition: Type.java:360
com.jogamp.gluegen.cgram.types.Type.newCVVariant
final Type newCVVariant(final int cvAttributes)
Return a variant of this type matching the given const/volatile attributes.
Definition: Type.java:98
com.jogamp.gluegen.cgram.types.Type.asArray
ArrayType asArray()
Casts this to an ArrayType or returns null if not an ArrayType.
Definition: Type.java:388
com.jogamp.gluegen.cgram.types.Type.isPointer
final boolean isPointer()
Indicates whether this is a PointerType.
Definition: Type.java:407
com.jogamp.gluegen.cgram.types.Type.isAnon
boolean isAnon()
Definition: Type.java:127
com.jogamp.gluegen.cgram.types.TypeVisitor
A visitor for Type's visitor model.
Definition: TypeVisitor.java:45