-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtypes.jou
369 lines (299 loc) · 13 KB
/
types.jou
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
import "stdlib/mem.jou"
import "stdlib/str.jou"
import "./errors_and_warnings.jou"
class ClassField:
name: byte[100]
type: Type*
# If multiple fields have the same union_id, they belong to the same union.
# It means that only one of the fields can be used at a time.
union_id: int
class ClassData:
fields: ClassField*
nfields: int
methods: Signature*
nmethods: int
class ArrayType:
item_type: Type*
len: int
class EnumType:
count: int
names: byte[100]*
enum TypeKind:
SignedInteger
UnsignedInteger
Bool
FloatingPoint # float or double
Pointer
VoidPointer
Array
Class
OpaqueClass # class with unknown members, used temporarily during type checking
Enum
class Type:
name: byte[500] # All types have a name for error messages and debugging.
kind: TypeKind
union:
size_in_bits: int # SignedInteger, UnsignedInteger, FloatingPoint
value_type: Type* # Pointer
classdata: ClassData # Class
array: ArrayType # Array
enummembers: EnumType
def is_integer_type(self) -> bool:
return self->kind == TypeKind.SignedInteger or self->kind == TypeKind.UnsignedInteger
def is_number_type(self) -> bool:
return self->is_integer_type() or self->kind == TypeKind.FloatingPoint
def is_pointer_type(self) -> bool:
return self->kind == TypeKind.Pointer or self->kind == TypeKind.VoidPointer
# Returns a type that represents pointer to the original type.
# Result is cached. Don't worry about freeing it.
def pointer_type(self) -> Type*:
info = self as TypeInfo*
assert self == &info->type # the 'type' field is first member and has 0 bytes offset
if info->pointer == NULL:
ptr: TypeInfo* = calloc(1, sizeof *ptr)
ptr->type = Type{kind=TypeKind.Pointer, value_type=self}
snprintf(ptr->type.name, sizeof ptr->type.name, "%s*", self->name)
info->pointer = ptr
return &info->pointer->type
# Returns a type that represents array where items are the original type.
# Result is cached. Don't worry about freeing it.
def array_type(self, len: int) -> Type*:
info = self as TypeInfo*
assert &info->type == self
assert len > 0
for existing = info->arrays; existing < &info->arrays[info->narrays]; existing++:
if (*existing)->type.array.len == len:
return &(*existing)->type
arr: TypeInfo* = calloc(1, sizeof *arr)
arr->type = Type{kind = TypeKind.Array, array = ArrayType{item_type = self, len = len}}
snprintf(arr->type.name, sizeof arr->type.name, "%s[%d]", self->name, len)
info->arrays = realloc(info->arrays, sizeof(info->arrays[0]) * (info->narrays + 1))
assert info->arrays != NULL
info->arrays[info->narrays++] = arr
return &arr->type
def short_description(self) -> byte*:
match self->kind:
case TypeKind.OpaqueClass | TypeKind.Class:
return "a class"
case TypeKind.Enum:
return "an enum"
case TypeKind.Pointer | TypeKind.VoidPointer:
return "a pointer type"
case TypeKind.SignedInteger | TypeKind.UnsignedInteger | TypeKind.FloatingPoint:
return "a number type"
case TypeKind.Array:
return "an array type"
case TypeKind.Bool:
return "the built-in bool type"
def find_method(self, name: byte*) -> Signature*:
if self->kind != TypeKind.Class:
return NULL
for m = self->classdata.methods; m < &self->classdata.methods[self->classdata.nmethods]; m++:
if strcmp(m->name, name) == 0:
return m
return NULL
# The TypeInfo for type T contains the type T* (if it has been used)
# and all array and pointer types with element type T.
class TypeInfo:
type: Type
pointer: TypeInfo*
arrays: TypeInfo**
narrays: long
def free_pointer_and_array_types(self) -> None:
if self->pointer != NULL:
free_type(&self->pointer->type)
for arrtype = self->arrays; arrtype < &self->arrays[self->narrays]; arrtype++:
assert &(*arrtype)->type != NULL
free_type(&(*arrtype)->type)
free(self->arrays)
# Types are cached into global state. This makes a lot of things easier
# because you don't need to copy and free the types everywhere. This is
# important: previously it was a lot of work to find forgotten copies and
# frees with valgrind.
#
# This also simplifies checking whether two types are the same type: you
# can simply use "==" between two "const Type *" pointers.
#
# Class types are a bit different. When you make a class, you get a
# pointer that you must pass to free_type() later. You can still "=="
# compare types, because two different classes with the same members are
# not the same type.
class GlobalTypeState:
integers: TypeInfo[2][65] # integers[i][j] = i-bit integer, j=1 for signed, j=0 for unsigned
boolean: TypeInfo
doublelele: TypeInfo
floater: TypeInfo
voidptr: TypeInfo
other_types: TypeInfo**
n_other_types: int
global global_type_state: GlobalTypeState
global boolType: Type* # bool
global shortType: Type* # short (16-bit signed)
global intType: Type* # int (32-bit signed)
global longType: Type* # long (64-bit signed)
global byteType: Type* # byte (8-bit unsigned)
global floatType: Type* # float (32-bit)
global doubleType: Type* # double (64-bit)
global voidPtrType: Type* # void*
@public
def init_types() -> None:
memset(&global_type_state, 0, sizeof(global_type_state))
boolType = &global_type_state.boolean.type
shortType = &global_type_state.integers[16][1].type
intType = &global_type_state.integers[32][1].type
longType = &global_type_state.integers[64][1].type
byteType = &global_type_state.integers[8][0].type
floatType = &global_type_state.floater.type
doubleType = &global_type_state.doublelele.type
voidPtrType = &global_type_state.voidptr.type
global_type_state.boolean.type = Type{name = "bool", kind = TypeKind.Bool }
global_type_state.voidptr.type = Type{name = "void*", kind = TypeKind.VoidPointer }
global_type_state.floater.type = Type{name = "float", kind = TypeKind.FloatingPoint, size_in_bits = 32 }
global_type_state.doublelele.type = Type{name = "double", kind = TypeKind.FloatingPoint, size_in_bits = 64 }
for size = 8; size <= 64; size *= 2:
global_type_state.integers[size][0].type.kind = TypeKind.UnsignedInteger
global_type_state.integers[size][1].type.kind = TypeKind.SignedInteger
global_type_state.integers[size][0].type.size_in_bits = size
global_type_state.integers[size][1].type.size_in_bits = size
sprintf(global_type_state.integers[size][0].type.name, "<%d-bit unsigned integer>", size)
sprintf(global_type_state.integers[size][1].type.name, "<%d-bit signed integer>", size)
strcpy(global_type_state.integers[8][0].type.name, "byte")
strcpy(global_type_state.integers[16][1].type.name, "short")
strcpy(global_type_state.integers[32][1].type.name, "int")
strcpy(global_type_state.integers[64][1].type.name, "long")
# Usually you don't need to free a type. This is in a function to make you
# think twice about it.
@public
def free_type(t: Type*) -> None:
assert t != NULL
if t->kind == TypeKind.Class:
for m = t->classdata.methods; m < &t->classdata.methods[t->classdata.nmethods]; m++:
m->free()
free(t->classdata.fields)
free(t->classdata.methods)
ti = t as TypeInfo*
assert &ti->type == t
ti->free_pointer_and_array_types()
free(t)
@public
def free_global_type_state() -> None:
global_type_state.boolean.free_pointer_and_array_types()
global_type_state.floater.free_pointer_and_array_types()
global_type_state.doublelele.free_pointer_and_array_types()
global_type_state.voidptr.free_pointer_and_array_types()
for size = 8; size <= 64; size *= 2:
for is_signed = 0; is_signed <= 1; is_signed++:
global_type_state.integers[size][is_signed].free_pointer_and_array_types()
for i = 0; i < global_type_state.n_other_types; i++:
free_type(&global_type_state.other_types[i]->type)
free(global_type_state.other_types)
@public
def get_integer_type(size_in_bits: int, is_signed: bool) -> Type*:
assert size_in_bits==8 or size_in_bits==16 or size_in_bits==32 or size_in_bits==64
return &global_type_state.integers[size_in_bits][is_signed as int].type
# Ensures that the type is freed when compiler is done. Operating system would
# do it anyway, but communicating this to valgrind is complicated because of
# how pointers and arrays work.
def satisfy_valgrind(ti: TypeInfo*) -> None:
arr = &global_type_state.other_types
global_type_state.other_types = realloc(
global_type_state.other_types,
sizeof(global_type_state.other_types[0]) * (global_type_state.n_other_types + 1),
)
assert global_type_state.other_types != NULL
global_type_state.other_types[global_type_state.n_other_types++] = ti
@public
def create_opaque_class(name: byte*) -> Type*:
result: TypeInfo* = calloc(1, sizeof *result)
result->type = Type{kind = TypeKind.OpaqueClass}
assert strlen(name) < sizeof result->type.name
strcpy(result->type.name, name)
satisfy_valgrind(result)
return &result->type
@public
def create_enum(name: byte*, membercount: int, membernames: byte[100]*) -> Type*:
result: TypeInfo* = calloc(1, sizeof *result)
result->type = Type{
kind = TypeKind.Enum,
enummembers = EnumType{count=membercount, names=membernames},
}
assert strlen(name) < sizeof result->type.name
strcpy(result->type.name, name)
satisfy_valgrind(result)
return &result->type
class Signature:
name: byte[100] # Function or method name. For methods it does not include the name of the class.
nargs: int
argtypes: Type**
argnames: byte[100]*
takes_varargs: bool # true for functions like printf()
# TODO: rename to return_type
returntype: Type* # NULL, if does not return a value
is_noreturn: bool
# TODO: rename to return_type_location
returntype_location: Location # meaningful even if returntype is NULL
def free(self) -> None:
free(self->argnames)
free(self->argtypes)
def get_self_class(self) -> Type*:
if self->nargs > 0 and strcmp(self->argnames[0], "self") == 0:
match self->argtypes[0]->kind:
case TypeKind.Pointer:
return self->argtypes[0]->value_type
case TypeKind.Class:
return self->argtypes[0]
case _:
assert False
return NULL
def is_main_function(self) -> bool:
return self->get_self_class() == NULL and strcmp(self->name, "main") == 0
# Useful for error messages, not much else.
def function_or_method(self) -> byte*:
if self->get_self_class() == NULL:
return "function"
else:
return "method"
def to_string(self, include_return_type: bool, include_self: bool) -> byte*:
result = strdup(self->name)
assert result != NULL
result = realloc(result, strlen(result) + 2)
assert result != NULL
strcat(result, "(")
for i = 0; i < self->nargs; i++:
if strcmp(self->argnames[i], "self") == 0 and not include_self:
continue
assert sizeof self->argnames[i] == 100
assert sizeof self->argtypes[i]->name == 500
result = realloc(result, strlen(result) + 1000)
assert result != NULL
strcat(result, self->argnames[i])
strcat(result, ": ")
strcat(result, self->argtypes[i]->name)
if i < self->nargs - 1:
strcat(result, ", ")
result = realloc(result, strlen(result) + 100)
assert result != NULL
if self->takes_varargs:
if self->nargs != 0:
strcat(result, ", ")
strcat(result, "...")
strcat(result, ")")
if include_return_type:
assert sizeof(self->returntype->name) == 500
result = realloc(result, strlen(result) + 600)
assert result != NULL
strcat(result, " -> ")
if self->is_noreturn:
strcat(result, "noreturn")
elif self->returntype == NULL:
strcat(result, "void")
else:
strcat(result, self->returntype->name)
return result
def copy(self) -> Signature:
result = *self
result.argtypes = malloc(sizeof(result.argtypes[0]) * result.nargs)
memcpy(result.argtypes, self->argtypes, sizeof(result.argtypes[0]) * result.nargs)
result.argnames = malloc(sizeof(result.argnames[0]) * result.nargs)
memcpy(result.argnames, self->argnames, sizeof(result.argnames[0]) * result.nargs)
return result