GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
Field.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistribution of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * Neither the name of Sun Microsystems, Inc. or the names of
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * This software is provided "AS IS," without a warranty of any kind. ALL
20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
23 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
24 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
25 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
26 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
27 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
28 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
29 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
30 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31 *
32 * You acknowledge that this software is not designed or intended for use
33 * in the design, construction, operation or maintenance of any nuclear
34 * facility.
35 *
36 * Sun gratefully acknowledges that this software was originally authored
37 * and developed by Kenneth Bradley Russell and Christopher John Kline.
38 */
39
40package com.jogamp.gluegen.cgram.types;
41
42import com.jogamp.common.os.MachineDataInfo;
43import com.jogamp.gluegen.cgram.types.TypeComparator.SemanticEqualityOp;
44
45/** Represents a field in a struct or union. */
46
47public class Field implements SemanticEqualityOp {
48 private final String name;
49 private final Type type;
50 private SizeThunk offset;
51
52 public Field(final String name, final Type type, final SizeThunk offset) {
53 this.name = name;
54 this.type = type;
55 this.offset = offset;
56 }
57
58 @Override
59 public int hashCode() {
60 // 31 * x == (x << 5) - x
61 final int hash = 31 + ( null != name ? name.hashCode() : 0 );
62 return ((hash << 5) - hash) + type.hashCode();
63 }
64
65 @Override
66 public boolean equals(final Object arg) {
67 if ( !(arg instanceof Field) ) {
68 return false;
69 }
70
71 final Field f = (Field) arg;
72 // Note: don't know how to examine offset any more since it's
73 // implemented in terms of code and they're not canonicalized
74 return ( ( name != null && name.equals(f.name) ) ||
75 ( name == null && f.name == null )
76 ) &&
77 type.equals(f.type);
78 }
79
80 @Override
81 public int hashCodeSemantics() {
82 return type.hashCodeSemantics();
83 }
84
85 @Override
86 public boolean equalSemantics(final SemanticEqualityOp arg) {
87 if ( !(arg instanceof Field) ) {
88 return false;
89 }
90
91 final Field f = (Field) arg;
92 // Note: don't know how to examine offset any more since it's
93 // implemented in terms of code and they're not canonicalized
94 return type.equalSemantics(f.type);
95 }
96
97 /** Name of this field in the containing data structure. */
98 public String getName() { return name; }
99
100 /** Type of this field. */
101 public Type getType() { return type; }
102
103 /** SizeThunk computing offset, in bytes, of this field in the containing data structure. */
104 public SizeThunk getOffset() { return offset; }
105
106 /** Offset, in bytes, of this field in the containing data structure
107 given the specified MachineDataInfo. */
108 public long getOffset(final MachineDataInfo machDesc) { return offset.computeSize(machDesc); }
109
110 /** Sets the offset of this field in the containing data structure. */
111 public void setOffset(final SizeThunk offset) { this.offset = offset; }
112
113 @Override
114 public String toString() {
115 if (!getType().isFunctionPointer()) {
116 if (getName() == null &&
117 getType().asCompound() != null &&
118 getType().asCompound().isUnion()) {
119 return "" + getType() + ";";
120 }
121 return "" + getType() + " " + getName() + ";";
122 } else {
124 // FIXME: pick up calling convention?
125 return ft.toString(getName(), null, false, true) + ";";
126 }
127 }
128}
Machine data description for alignment and size onle, see com.jogamp.gluegen.
Represents a field in a struct or union.
Definition: Field.java:47
int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: Field.java:81
boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: Field.java:86
SizeThunk getOffset()
SizeThunk computing offset, in bytes, of this field in the containing data structure.
Definition: Field.java:104
String getName()
Name of this field in the containing data structure.
Definition: Field.java:98
Field(final String name, final Type type, final SizeThunk offset)
Definition: Field.java:52
boolean equals(final Object arg)
Definition: Field.java:66
Type getType()
Type of this field.
Definition: Field.java:101
long getOffset(final MachineDataInfo machDesc)
Offset, in bytes, of this field in the containing data structure given the specified MachineDataInfo.
Definition: Field.java:108
void setOffset(final SizeThunk offset)
Sets the offset of this field in the containing data structure.
Definition: Field.java:111
Describes a function type, used to model both function declarations and (via PointerType) function po...
String toString()
Returns a string representation of this type.
Provides a level of indirection between the definition of a type's size and the absolute value of thi...
Definition: SizeThunk.java:51
abstract long computeSize(MachineDataInfo machDesc)
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 equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: Type.java:514
FunctionType getTargetFunction()
Returns the target FunctionType if this type is isFunctionPointer().
Definition: Type.java:619
final boolean equals(final Object arg)
Equality test for Types inclusive its given name.
Definition: Type.java:468
Supports semantic equality and hash functions for types.