GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
PointerType.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 com.jogamp.gluegen.ASTLocusTag;
43
44public class PointerType extends Type implements Cloneable {
45
46 private final Type targetType;
47
48 public PointerType(final SizeThunk size, final Type targetType, final int cvAttributes) {
49 this(size, targetType, cvAttributes, null);
50 }
51 public PointerType(final SizeThunk size, final Type targetType, final int cvAttributes, final ASTLocusTag astLocus) {
52 // can pass null for the final name parameter because the PointerType's getName()
53 // completely replaces superclass behavior
54 super(targetType.getName() + " *", size, cvAttributes, astLocus);
55 this.targetType = targetType;
56 }
57
58 private PointerType(final PointerType o, final int cvAttributes, final ASTLocusTag astLocus) {
59 super(o, cvAttributes, astLocus);
60 targetType = o.targetType;
61 }
62
63 @Override
64 Type newVariantImpl(final boolean newCVVariant, final int cvAttributes, final ASTLocusTag astLocus) {
65 return new PointerType(this, cvAttributes, astLocus);
66 }
67
68 @Override
69 protected int hashCodeImpl() {
70 return targetType.hashCode();
71 }
72
73 @Override
74 protected boolean equalsImpl(final Type arg) {
75 final PointerType t = (PointerType) arg;
76 return targetType.equals(t.targetType);
77 }
78
79 @Override
80 protected int hashCodeSemanticsImpl() {
81 return targetType.hashCodeSemantics();
82 }
83
84 @Override
85 protected boolean equalSemanticsImpl(final Type arg) {
86 final PointerType pt = (PointerType) arg;
87 return targetType.equalSemantics(pt.targetType);
88 }
89
90 @Override
91 public boolean isAnon() {
92 if ( isTypedef() ) {
93 return super.isAnon();
94 } else {
95 return targetType.isAnon();
96 }
97 }
98
99 @Override
100 public String getName(final boolean includeCVAttrs) {
101 if ( isTypedef() ) {
102 return super.getName(includeCVAttrs);
103 } else if (!includeCVAttrs) {
104 return targetType.getName(includeCVAttrs) + " *";
105 } else {
106 return targetType.getName(includeCVAttrs) + " * " + getCVAttributesString();
107 }
108 }
109
110 @Override
111 public String getCName(final boolean includeCVAttrs) {
112 if ( isTypedef() ) {
113 return super.getCName(includeCVAttrs);
114 } else if (!includeCVAttrs) {
115 return targetType.getCName(includeCVAttrs) + " *";
116 } else {
117 return targetType.getCName(includeCVAttrs) + " * " + getCVAttributesString();
118 }
119 }
120
121 @Override
122 public final PointerType asPointer() {
123 return this;
124 }
125
126 @Override
127 public final Type getTargetType() {
128 if( isFunctionPointer() ) {
129 return this;
130 } else {
131 return targetType;
132 }
133 }
134
135 @Override
136 public final Type getBaseType() {
137 if( isFunctionPointer() ) {
138 return this;
139 } else {
140 return targetType.getBaseType();
141 }
142 }
143
144 @Override
146 return getTargetType();
147 }
148
149 @Override
151 if( isFunctionPointer() ) {
152 return targetType.asFunction();
153 } else {
154 return null;
155 }
156 }
157 @Override
158 public final boolean isFunctionPointer() {
159 return targetType.isFunction();
160 }
161
162 @Override
163 public final int pointerDepth() {
164 return 1 + targetType.pointerDepth();
165 }
166
167 @Override
168 public String toString() {
169 if ( isTypedef() ) {
170 return super.getCName(true);
171 } else {
172 return toStringInt();
173 }
174 }
175 private String toStringInt() {
176 if (!isFunctionPointer()) {
177 return targetType.getCName(true) + " * " + getCVAttributesString();
178 } else {
179 // return toString(null, null); // this is a pointer to an unnamed function
180 return ((FunctionType) targetType).toString(null /* functionName */, null /* callingConvention */, false, true);
181 }
182 }
183
184 /** For use only when printing function pointers. Calling convention
185 string (i.e., "__stdcall") is optional and is generally only
186 needed on Windows. */
187 public String toString(final String functionName, final String callingConvention) {
188 if (!isFunctionPointer()) {
189 throw new RuntimeException("<Internal error or misuse> This method is only for use when printing function pointers");
190 }
191 return ((FunctionType) targetType).toString(functionName, callingConvention, false, true);
192 }
193
194 @Override
195 public void visit(final TypeVisitor arg) {
196 super.visit(arg);
197 targetType.visit(arg);
198 }
199}
Describes a function type, used to model both function declarations and (via PointerType) function po...
void visit(final TypeVisitor arg)
Traverse this Type and all of its component types; for example, the return type and argument types of...
final Type getTargetType()
Helper method to returns the target type of this type, in case another type is being referenced,...
FunctionType getTargetFunction()
Returns the target FunctionType if this type is isFunctionPointer().
String toString()
Returns a string representation of this type.
String getCName(final boolean includeCVAttrs)
Returns the name of this type, optionally including const/volatile attributes.
final Type getBaseType()
Helper method to returns the bottom-most element type of this type, i.e.
final PointerType asPointer()
Casts this to a PointerType or returns null if not a PointerType.
Type getArrayBaseOrPointerTargetType()
Return getBaseType() if isArray() or returns getTargetType() otherwise.
PointerType(final SizeThunk size, final Type targetType, final int cvAttributes)
String toString(final String functionName, final String callingConvention)
For use only when printing function pointers.
boolean equalSemanticsImpl(final Type arg)
String getName(final boolean includeCVAttrs)
Returns the name of this type, optionally including const/volatile attributes.
final int pointerDepth()
Helper method for determining how many pointer indirections this type represents (i....
PointerType(final SizeThunk size, final Type targetType, final int cvAttributes, final ASTLocusTag astLocus)
final boolean isFunctionPointer()
Convenience routine indicating whether this Type is a pointer to a function.
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
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
Type getBaseType()
Helper method to returns the bottom-most element type of this type, i.e.
Definition: Type.java:588
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
int pointerDepth()
Helper method for determining how many pointer indirections this type represents (i....
Definition: Type.java:564
final boolean isFunction()
Indicates whether this is a FunctionType.
Definition: Type.java:413
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
FunctionType asFunction()
Casts this to a FunctionType or returns null if not a FunctionType.
Definition: Type.java:392
final Type newCVVariant(final int cvAttributes)
Return a variant of this type matching the given const/volatile attributes.
Definition: Type.java:98
final boolean isTypedef()
Indicates whether this type is a typedef type, i.e.
Definition: Type.java:352
A visitor for Type's visitor model.