-
Notifications
You must be signed in to change notification settings - Fork 10
/
java_types.py
377 lines (280 loc) · 9.79 KB
/
java_types.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# pylint: disable=abstract-method, useless-super-delegation,too-many-ancestors
# pylint: disable=too-few-public-methods
from src.ir.types import Builtin
import src.ir.builtins as bt
import src.ir.types as tp
class JavaBuiltinFactory(bt.BuiltinFactory):
def get_language(self):
return "java"
def get_builtin(self):
return JavaBuiltin
def get_void_type(self):
return VoidType()
def get_any_type(self):
return ObjectType()
def get_number_type(self):
return NumberType()
def get_integer_type(self):
return IntegerType(primitive=False)
def get_byte_type(self):
return ByteType(primitive=False)
def get_short_type(self):
return ShortType(primitive=False)
def get_long_type(self):
return LongType(primitive=False)
def get_float_type(self):
return FloatType(primitive=False)
def get_double_type(self):
return DoubleType(primitive=False)
def get_big_decimal_type(self):
return DoubleType(primitive=False)
def get_boolean_type(self):
return BooleanType(primitive=False)
def get_char_type(self):
return CharType(primitive=False)
def get_string_type(self):
return StringType()
def get_array_type(self):
return ArrayType()
def get_big_integer_type(self):
return IntegerType(primitive=False)
def get_function_type(self, nr_parameters=0):
return FunctionType(nr_parameters)
def get_primitive_types(self):
return [
ByteType(primitive=True),
ShortType(primitive=True),
IntegerType(primitive=True),
LongType(primitive=True),
FloatType(primitive=True),
DoubleType(primitive=True),
CharType(primitive=True),
BooleanType(primitive=True)
]
def get_non_nothing_types(self):
return super().get_non_nothing_types() + self.get_primitive_types()
def get_number_types(self):
return super().get_number_types() + self.get_primitive_types()[:-1]
class JavaBuiltin(Builtin):
def __init__(self, name, primitive):
super().__init__(name)
self.primitive = primitive
def __str__(self):
if not self.is_primitive():
return str(self.name) + "(java-builtin)"
return str(self.name).lower() + "(java-primitive)"
def is_primitive(self):
return self.primitive
def box_type(self):
raise NotImplementedError('box_type() must be implemented')
class ObjectType(JavaBuiltin):
def __init__(self, name="Object"):
super().__init__(name, False)
def get_builtin_type(self):
return bt.Any
def box_type(self):
return self
class VoidType(JavaBuiltin):
def __init__(self, name="void", primitive=False):
super().__init__(name, primitive)
if not self.primitive:
self.supertypes.append(ObjectType())
else:
self.supertypes = set()
def get_builtin_type(self):
return bt.Void
def box_type(self):
return VoidType(self.name, primitive=False)
class NumberType(ObjectType):
def __init__(self, name="Number"):
super().__init__(name)
self.supertypes.append(ObjectType())
def get_builtin_type(self):
return bt.Number
def box_type(self):
return self
class IntegerType(NumberType):
def __init__(self, name="Integer", primitive=False):
super().__init__(name)
self.primitive = primitive
if not self.primitive:
self.supertypes.append(NumberType())
else:
self.supertypes = set()
def get_builtin_type(self):
return bt.Integer
def box_type(self):
return IntegerType(self.name, primitive=False)
def is_assignable(self, other):
assignable_types = (NumberType, IntegerType,)
return self.is_subtype(other) or type(other) in assignable_types
def get_name(self):
if self.is_primitive():
return "int"
return super().get_name()
class ShortType(NumberType):
def __init__(self, name="Short", primitive=False):
super().__init__(name)
self.primitive = primitive
if not self.primitive:
self.supertypes.append(NumberType())
else:
self.supertypes = set()
def get_builtin_type(self):
return bt.Short
def box_type(self):
return ShortType(self.name, primitive=False)
def is_assignable(self, other):
assignable_types = (NumberType, ShortType,)
return self.is_subtype(other) or type(other) in assignable_types
def get_name(self):
if self.is_primitive():
return "short"
return super().get_name()
class LongType(NumberType):
def __init__(self, name="Long", primitive=False):
super().__init__(name)
self.primitive = primitive
if not self.primitive:
self.supertypes.append(NumberType())
else:
self.supertypes = set()
def get_builtin_type(self):
return bt.Long
def box_type(self):
return LongType(self.name, primitive=False)
def is_assignable(self, other):
assignable_types = (NumberType, LongType,)
return self.is_subtype(other) or type(other) in assignable_types
def get_name(self):
if self.is_primitive():
return "long"
return super().get_name()
class ByteType(NumberType):
def __init__(self, name="Byte", primitive=False):
super().__init__(name)
self.primitive = primitive
if not self.primitive:
self.supertypes.append(NumberType())
else:
self.supertypes = set()
def get_builtin_type(self):
return bt.Byte
def box_type(self):
return ByteType(self.name, primitive=False)
def is_assignable(self, other):
assignable_types = (NumberType, ByteType,)
return self.is_subtype(other) or type(other) in assignable_types
def get_name(self):
if self.is_primitive():
return "byte"
return super().get_name()
class FloatType(NumberType):
def __init__(self, name="Float", primitive=False):
super().__init__(name)
self.primitive = primitive
if not self.primitive:
self.supertypes.append(NumberType())
else:
self.supertypes = set()
def get_builtin_type(self):
return bt.Float
def box_type(self):
return FloatType(self.name, primitive=False)
def is_assignable(self, other):
assignable_types = (NumberType, FloatType,)
return self.is_subtype(other) or type(other) in assignable_types
def get_name(self):
if self.is_primitive():
return "float"
return super().get_name()
class DoubleType(NumberType):
def __init__(self, name="Double", primitive=False):
super().__init__(name)
self.primitive = primitive
if not self.primitive:
self.supertypes.append(NumberType())
else:
self.supertypes = set()
def get_builtin_type(self):
return bt.Double
def box_type(self):
return DoubleType(self.name, primitive=False)
def is_assignable(self, other):
assignable_types = (NumberType, DoubleType,)
return self.is_subtype(other) or type(other) in assignable_types
def get_name(self):
if self.is_primitive():
return "double"
return super().get_name()
class CharType(ObjectType):
def __init__(self, name="Character", primitive=False):
super().__init__(name)
self.primitive = primitive
if not self.primitive:
self.supertypes.append(ObjectType())
else:
self.supertypes = set()
def get_builtin_type(self):
return bt.Char
def box_type(self):
return CharType(self.name, primitive=False)
def get_name(self):
if self.is_primitive():
return "char"
return super().get_name()
class StringType(ObjectType):
def __init__(self, name="String"):
super().__init__(name)
self.supertypes.append(ObjectType())
def get_builtin_type(self):
return bt.String
def box_type(self):
return self
class BooleanType(ObjectType):
def __init__(self, name="Boolean", primitive=False):
super().__init__(name)
self.primitive = primitive
if not self.primitive:
self.supertypes.append(ObjectType())
else:
self.supertypes = []
def get_builtin_type(self):
return bt.Boolean
def box_type(self):
return BooleanType(self.name, primitive=False)
def get_name(self):
if self.is_primitive():
return "boolean"
return super().get_name()
class ArrayType(tp.TypeConstructor, ObjectType):
def __init__(self, name="Array"):
# In Java, arrays are covariant.
super().__init__(name, [tp.TypeParameter(
"T", variance=tp.Covariant)])
self.supertypes.append(ObjectType())
class FunctionType(tp.TypeConstructor):
def __init__(self, nr_type_parameters: int):
name = "Function" + str(nr_type_parameters)
type_parameters = [
tp.TypeParameter("A" + str(i))
for i in range(1, nr_type_parameters + 1)
] + [tp.TypeParameter("R")]
self.nr_type_parameters = nr_type_parameters
super().__init__(name, type_parameters)
### WARNING: use them only for testing ###
Object = ObjectType()
Void = VoidType()
Number = NumberType()
Integer = IntegerType()
Short = ShortType()
Long = LongType()
Byte = ByteType()
Float = FloatType()
Double = DoubleType()
Char = CharType()
String = StringType()
Boolean = BooleanType()
Array = ArrayType()
NonNothingTypes = [Object, Number, Integer, Short, Long, Byte, Float,
Double, Char, String, Boolean, Array]