-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertedType.java
More file actions
206 lines (169 loc) · 4.45 KB
/
ConvertedType.java
File metadata and controls
206 lines (169 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package gir2java;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.bridj.Pointer;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JType;
/**
* Tell various information about types found in .girs that help generating appropriate
* bindings for the elements that use it.
*
* @author relek
*
*/
public class ConvertedType {
private static Set<String> knownBridjPrimitives = new HashSet<String>(Arrays.asList(
"Boolean",
"Byte",
"Char",
"CLong",
"Double",
"Enum",
"Float",
"Int",
"Long",
"NativeObject",
"Pointer",
"Short",
"SizeT",
"TypedPointer"
));
private String namespace;
private String type;
private String ctype;
private String bridjAnnotation;
private boolean isEnum;
private JCodeModel cm;
private JType jType;
public ConvertedType(JCodeModel cm, String namespace, String type, String ctype, boolean isEnum) {
this.namespace = namespace;
this.type = type;
this.ctype = ctype;
this.cm = cm;
this.isEnum = isEnum;
}
public ConvertedType(ConvertedType other) {
this.namespace = other.getNamespace();
this.type = other.getType();
this.ctype = other.getCtype();
this.isEnum = other.isEnum();
this.cm = other.cm;
this.jType = other.getJType();
}
public boolean isPointer() {
if ( (getJType() != null) && getJType().erasure().name().equals("Pointer")) {
return true;
}
return (ctype != null) && (ctype.contains("*"));
}
public boolean isEnum() {
return isEnum;
}
public boolean isStructByValue() {
if (isEnum() || isPointer()) {
return false;
}
if ( (getJType() != null) && (getJType().isPrimitive() || getJType().erasure().name().equals("Pointer")) ) {
return false;
}
return true;
}
public String getType() {
return type;
}
public String getCtype() {
return ctype;
}
public void setCtype(String newCtype) {
this.ctype = newCtype;
}
public String getNamespace() {
return namespace;
}
public String getBridjAnnotation() {
return bridjAnnotation;
}
public void setBridjAnnotation(String bridjAnnotation) {
this.bridjAnnotation = bridjAnnotation;
}
public JType getJType() {
return jType;
}
public void setJType(JType jType) {
this.jType = jType;
}
public ConvertedType forCType(ParsingContext context, String ctype) {
if (context.getCm().ref(Pointer.class).equals(getJType())) {
return this;
}
String ctypeAfterTypedef = context.applyTypedefs(ctype);
ConvertedType pointerConvType = new ConvertedType(this);
JType currentJType = getJType();
int currentIndirection = 0;
while ( (currentJType != null) && currentJType.name().startsWith("Pointer<")) {
currentIndirection++;
if ( ((JClass)currentJType).getTypeParameters().size() == 0 ) {
break;
}
currentJType = ((JClass)currentJType).getTypeParameters().get(0);
}
JType indirectJType = getJType();
int indirection = NameUtils.getIndirectionLevel(ctypeAfterTypedef) - currentIndirection;
for (; indirection > 0; indirection--) {
indirectJType = context.getCm().ref(Pointer.class).narrow(indirectJType);
}
pointerConvType.setJType(indirectJType);
pointerConvType.setCtype(ctypeAfterTypedef);
return pointerConvType;
}
public String getQualifiedType() {
StringBuilder sb = new StringBuilder();
if (getNamespace() != null) {
sb.append(getNamespace());
sb.append('.');
}
sb.append(getType());
return sb.toString();
}
public ConvertedType pointerTypeTo() {
ConvertedType ret = new ConvertedType(this);
ret.setCtype(this.getCtype() + "*");
return ret;
}
public String bridjMethodifyTypeName() {
if (isEnum()) {
return "Enum";
// } else if (type.isAssignableFrom(TypedPointer.class)) {
// return "TypedPointer";
} else if (isPointer()) {
return "Pointer";
}
String camelType = NameUtils.toCamel(getJType().name());
if (knownBridjPrimitives.contains(camelType)) {
return camelType;
} else {
return "NativeObject";
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("ConvertedType[ ");
sb.append("namespace = ");
sb.append(getNamespace());
sb.append(", name = ");
sb.append(getType());
sb.append(", c:type = ");
sb.append(getCtype());
sb.append(", mapped type = ");
if (getBridjAnnotation() != null) {
sb.append('@');
sb.append(getBridjAnnotation());
sb.append(' ');
}
sb.append(getJType());
sb.append(']');
return sb.toString();
}
}