7
7
import org .openzen .zencode .java .TypeVariableContext ;
8
8
import org .openzen .zencode .java .module .JavaAnnotatedType ;
9
9
import org .openzen .zencode .java .module .JavaNativePackageInfo ;
10
- import org .openzen .zencode .java .module .JavaRuntimeClass ;
11
10
import org .openzen .zencode .shared .CodePosition ;
12
11
import org .openzen .zencode .shared .LiteralSourceFile ;
13
12
import org .openzen .zenscript .codemodel .FunctionHeader ;
14
13
import org .openzen .zenscript .codemodel .GenericMapper ;
15
14
import org .openzen .zenscript .codemodel .Modifiers ;
16
- import org .openzen .zenscript .codemodel .ModuleSpace ;
17
15
import org .openzen .zenscript .codemodel .compilation .CompileContext ;
18
16
import org .openzen .zenscript .codemodel .definition .ClassDefinition ;
19
17
import org .openzen .zenscript .codemodel .definition .ZSPackage ;
@@ -68,15 +66,10 @@ public TypeID getType(TypeVariableContext context, AnnotatedType type) {
68
66
} else if (type .isAnnotationPresent (ZenCodeType .USize .class )) {
69
67
result = BasicTypeID .USIZE ;
70
68
} else {
71
- boolean unsigned = type .isAnnotationPresent (ZenCodeType .Unsigned .class );
72
- result = loadType (context , JavaAnnotatedType .of (type ), unsigned );
69
+ result = loadType (context , JavaAnnotatedType .of (type ));
73
70
}
74
71
75
- boolean isOptional = type .isAnnotationPresent (ZenCodeType .Nullable .class );
76
- if (isOptional && !result .isOptional ())
77
- result = new OptionalTypeID (result );
78
-
79
- return result ;
72
+ return optionallyWrap (type , result );
80
73
}
81
74
82
75
@ Override
@@ -106,19 +99,19 @@ public TypeID parseType(String type) {
106
99
}
107
100
}
108
101
109
- private TypeID loadType (TypeVariableContext context , JavaAnnotatedType type , boolean unsigned ) {
102
+ private TypeID loadType (TypeVariableContext context , JavaAnnotatedType type ) {
110
103
final JavaAnnotatedType .ElementType elementType = type .getElementType ();
111
104
112
105
try {
113
106
switch (elementType ) {
114
107
case ANNOTATED_PARAMETERIZED_TYPE :
115
- return loadAnnotatedParameterizedType (context , (AnnotatedParameterizedType ) type .getAnnotatedElement (), unsigned );
108
+ return loadAnnotatedParameterizedType (context , (AnnotatedParameterizedType ) type .getAnnotatedElement ());
116
109
case ANNOTATED_TYPE :
117
- return loadAnnotatedType (context , (AnnotatedType ) type .getAnnotatedElement (), unsigned );
110
+ return loadAnnotatedType (context , (AnnotatedType ) type .getAnnotatedElement ());
118
111
case CLASS :
119
- return loadClass (context , (Class <?>) type .getType (), unsigned );
112
+ return loadClass (context , (Class <?>) type .getType ());
120
113
case GENERIC_ARRAY :
121
- return loadGenericArray (context , (GenericArrayType ) type .getType (), unsigned );
114
+ return loadGenericArray (context , (GenericArrayType ) type .getType ());
122
115
case PARAMETERIZED_TYPE :
123
116
return loadParameterizedType (context , (ParameterizedType ) type .getType ());
124
117
case TYPE_VARIABLE :
@@ -133,56 +126,52 @@ private TypeID loadType(TypeVariableContext context, JavaAnnotatedType type, boo
133
126
throw new IllegalArgumentException ("Invalid type " + elementType + ": not yet implemented or foolery" );
134
127
}
135
128
136
- private TypeSymbol loadRawType (TypeVariableContext context , JavaAnnotatedType type , boolean unsigned ) {
129
+ private TypeSymbol loadRawType (JavaAnnotatedType type ) {
137
130
final JavaAnnotatedType .ElementType elementType = type .getElementType ();
138
131
139
132
try {
140
- switch (elementType ) {
141
- case CLASS :
142
- return findType ((Class <?>) type .getType ());
143
- default :
144
- throw new IllegalArgumentException ("Didn't expect to get this kind of type: " + type );
133
+ if (Objects .requireNonNull (elementType ) == JavaAnnotatedType .ElementType .CLASS ) {
134
+ return findType ((Class <?>) type .getType ());
145
135
}
136
+ throw new IllegalArgumentException ("Didn't expect to get this kind of type: " + type );
146
137
} catch (final IllegalArgumentException e ) {
147
138
throw new IllegalArgumentException ("Unable to analyze type: " + type , e );
148
139
}
149
140
}
150
141
151
- private TypeID loadAnnotatedParameterizedType (TypeVariableContext context , AnnotatedParameterizedType type , boolean unsigned ) {
142
+ private TypeID loadAnnotatedParameterizedType (TypeVariableContext context , AnnotatedParameterizedType type ) {
152
143
final ParameterizedType parameterizedType = this .getTypeIfValid (JavaAnnotatedType .of (type .getType ()), JavaAnnotatedType .ElementType .PARAMETERIZED_TYPE );
153
144
final JavaAnnotatedType rawType = JavaAnnotatedType .of (parameterizedType .getRawType ());
154
145
155
146
final JavaAnnotatedType [] actualTypeArguments = JavaAnnotatedType .arrayOf (type .getAnnotatedActualTypeArguments ());
156
147
final TypeID [] codeParameters = new TypeID [actualTypeArguments .length ];
157
148
158
149
for (int i = 0 ; i < actualTypeArguments .length ; i ++) {
159
- codeParameters [i ] = this .loadType (context , actualTypeArguments [i ], false );
150
+ codeParameters [i ] = this .loadType (context , actualTypeArguments [i ]);
160
151
}
161
152
162
153
if (rawType .getElementType () == JavaAnnotatedType .ElementType .CLASS ) {
154
+ //noinspection SuspiciousMethodCalls
163
155
if (specialTypes .containsKey (rawType .getType ())) {
156
+ //noinspection SuspiciousMethodCalls
164
157
return specialTypes .get (rawType .getType ()).apply (codeParameters );
165
158
}
166
159
167
- final TypeSymbol rawTypeSymbol = loadRawType (context , rawType , unsigned );
160
+ final TypeSymbol rawTypeSymbol = loadRawType (rawType );
168
161
return DefinitionTypeID .create (rawTypeSymbol , codeParameters );
169
162
}
170
- return this .loadType (context , JavaAnnotatedType .of (type ), unsigned );
163
+ return this .loadType (context , JavaAnnotatedType .of (type ));
171
164
}
172
165
173
- private TypeID loadAnnotatedType (TypeVariableContext context , AnnotatedType type , boolean unsigned ) {
166
+ private TypeID loadAnnotatedType (TypeVariableContext context , AnnotatedType type ) {
174
167
final JavaAnnotatedType annotatedType = JavaAnnotatedType .of (type .getType ());
175
- return this .loadType (context , annotatedType , unsigned );
168
+ TypeID result = this .loadType (context , annotatedType );
169
+ return optionallyWrap (type , result );
176
170
}
177
171
178
- private TypeID loadClass (TypeVariableContext context , Class <?> type , boolean unsigned ) {
179
- if (unsigned ) {
180
- return unsignedByClass .computeIfAbsent (type , it -> {
181
- throw new IllegalArgumentException ("This class cannot be used as unsigned: " + it );
182
- });
183
- }
172
+ private TypeID loadClass (TypeVariableContext context , Class <?> type ) {
184
173
if (type .isArray ()) {
185
- final TypeID baseType = this .loadType (context , JavaAnnotatedType .of (type .getComponentType ()), false );
174
+ final TypeID baseType = this .loadType (context , JavaAnnotatedType .of (type .getComponentType ()));
186
175
return new ArrayTypeID (baseType );
187
176
}
188
177
if (type .isAnnotationPresent (FunctionalInterface .class )) {
@@ -205,9 +194,9 @@ private TypeID loadClass(TypeVariableContext context, Class<?> type, boolean uns
205
194
return DefinitionTypeID .create (definition , typeParameters .toArray (TypeID .NONE ));
206
195
}
207
196
208
- private TypeID loadGenericArray (TypeVariableContext context , GenericArrayType type , boolean unsigned ) {
197
+ private TypeID loadGenericArray (TypeVariableContext context , GenericArrayType type ) {
209
198
final JavaAnnotatedType componentType = JavaAnnotatedType .of (type .getGenericComponentType ());
210
- final TypeID baseType = this .loadType (context , componentType , unsigned );
199
+ final TypeID baseType = this .loadType (context , componentType );
211
200
return new ArrayTypeID (baseType );
212
201
}
213
202
@@ -221,7 +210,7 @@ private TypeID loadParameterizedType(TypeVariableContext context, ParameterizedT
221
210
222
211
TypeID [] codeParameters = new TypeID [typeArguments .length ];
223
212
for (int i = 0 ; i < typeArguments .length ; i ++) {
224
- codeParameters [i ] = this .loadType (context , typeArguments [i ], false );
213
+ codeParameters [i ] = this .loadType (context , typeArguments [i ]);
225
214
}
226
215
227
216
if (rawType == Map .class ) {
@@ -342,7 +331,7 @@ private TypeID loadFunctionalInterface(TypeVariableContext loadContext, Class<?>
342
331
Map <TypeParameter , TypeID > mapping = new HashMap <>();
343
332
TypeVariable <?>[] javaParameters = cls .getTypeParameters ();
344
333
for (int i = 0 ; i < parameters .length ; i ++) {
345
- mapping .put (context .get (javaParameters [i ]), loadType (loadContext , parameters [i ], false ));
334
+ mapping .put (context .get (javaParameters [i ]), loadType (loadContext , parameters [i ]));
346
335
}
347
336
348
337
header = header .withGenericArguments (new GenericMapper (mapping , TypeID .NONE ));
@@ -469,4 +458,11 @@ private void fillSpecialTypes() {
469
458
specialTypes .put (ToLongFunction .class , args -> new FunctionTypeID (new FunctionHeader (BasicTypeID .LONG , args [0 ])));
470
459
specialTypes .put (UnaryOperator .class , args -> new FunctionTypeID (new FunctionHeader (args [0 ], args [0 ])));
471
460
}
461
+
462
+ private TypeID optionallyWrap (AnnotatedType type , TypeID result ) {
463
+ boolean isOptional = type .isAnnotationPresent (ZenCodeType .Nullable .class );
464
+ if (isOptional && !result .isOptional ())
465
+ result = new OptionalTypeID (result );
466
+ return result ;
467
+ }
472
468
}
0 commit comments