Skip to content
← Back

src/ir/program/class_set.ghul

1
namespace IR.Program is
2
use Collections.LIST
3
use Collections.MAP
4
5
use Source.LOCATION
6
7
use Semantic.Scope
8
use Semantic.Types.Type
9
use Semantic.Symbols.Symbol
10
use Semantic.Symbols.Classy
11
use Semantic.Symbols.GENERIC
12
13
use IR.Emitter.SRM_MEMBER_ORDER
14
use IR.Emitter.SRM_EMITTED_TYPE
15
16
/// The classes a whole program uses, one for each instantiation, with
17
/// the array types and the boxes scalars are held in, and the numbers
18
/// that place each in the class tree.
19
///
20
/// Classes are found in two steps. `collect` takes every class that is
21
/// not generic, and until `number` is called, every instantiation asked
22
/// about is added too. `number` then numbers them all, and nothing is
23
/// added afterwards.
24
class CLASS_SET is
25
_object: CLASS_INSTANCE?
26
_holds_element: (Type) -> bool
27
_core: CoreClasses
28
29
_classes: MAP[CLASS_INSTANCE, CLASS_INSTANCE]
30
_new_classes: LIST[CLASS_INSTANCE]
31
32
// Each array type the program uses, with its element type. An array
33
// type is numbered as a class under the root.
34
_arrays: MAP[CLASS_INSTANCE, Type]
35
36
// The scalar types a value is boxed from, keyed by the box.
37
_boxed: MAP[CLASS_INSTANCE, Type]
38
39
_fields_left: Collections.SET[CLASS_INSTANCE]
40
41
_numbering: CLASS_ID_NUMBERING[CLASS_INSTANCE]
42
_is_numbered: bool
43
44
/// A set whose classes derive from `object`.
45
///
46
/// - object: the root class, or null for a program with no classes
47
/// - holds_element: whether the target can hold an array of a type;
48
/// an array of any other type is not added
49
/// - core: the built-in types the set treats specially
50
init(object: Classy?, holds_element: (Type) -> bool, core: CoreClasses) is
51
_holds_element = holds_element
52
_core = core
53
54
_classes = MAP[CLASS_INSTANCE, CLASS_INSTANCE]()
55
_new_classes = LIST[CLASS_INSTANCE]()
56
_arrays = MAP[CLASS_INSTANCE, Type]()
57
_boxed = MAP[CLASS_INSTANCE, Type]()
58
_fields_left = Collections.SET[CLASS_INSTANCE]()
59
60
_numbering = CLASS_ID_NUMBERING[CLASS_INSTANCE]()
61
62
if let definition = object then
63
_object = intern(CLASS_INSTANCE.of(definition))
64
fi
65
si
66
67
/// The root class, or null for a set that knows no classes.
68
root: CLASS_INSTANCE? => _object
69
70
/// The numbers given to the classes, once `number` has run.
71
numbering: CLASS_ID_NUMBERING[CLASS_INSTANCE] => _numbering
72
73
/// The built-in types the set treats specially.
74
core: CoreClasses => _core
75
76
/// Whether `number` has run, after which nothing is added.
77
is_numbered: bool => _is_numbered
78
79
/// Every class held, boxes included, in the order added.
80
classes: Collections.Iterable[CLASS_INSTANCE] => _classes.values
81
82
/// Every array type held, with its element type, in the order added.
83
arrays: Collections.Iterable[Collections.KEY_VALUE_PAIR[CLASS_INSTANCE, Type]] => _arrays
84
85
/// Every box held, with the scalar type it holds, in the order
86
/// added.
87
boxes: Collections.Iterable[Collections.KEY_VALUE_PAIR[CLASS_INSTANCE, Type]] => _boxed
88
89
/// Every class held, and every array type.
90
classes_and_arrays: Collections.List[CLASS_INSTANCE] is
91
let result = LIST[CLASS_INSTANCE](_classes.values)
92
93
for (array, _) in _arrays do
94
result.add(array)
95
od
96
97
return result
98
si
99
100
/// The built-in scalar types, each of which a whole program boxes in a
101
/// box of its own.
102
innate_scalar_types() -> Collections.List[Type] static is
103
let lookup = IoC.CONTAINER.instance.innate_symbol_lookup
104
105
return [
106
lookup.get_bool_type(), lookup.get_char_type(),
107
lookup.get_byte_type(), lookup.get_ubyte_type(),
108
lookup.get_short_type(), lookup.get_ushort_type(),
109
lookup.get_int_type(), lookup.get_uint_type(),
110
lookup.get_long_type(), lookup.get_ulong_type(),
111
lookup.get_word_type(), lookup.get_uword_type(),
112
lookup.get_single_type(), lookup.get_double_type()
113
]
114
si
115
116
/// Takes every class declared under `root` that is not generic, a box
117
/// for each scalar, and a box for each enum.
118
///
119
/// - root: the scope the program's types are declared under
120
/// - scalars: the scalar types, each of which gets a box
121
collect(root: Scope, scalars: Collections.Iterable[Type]) is
122
for type in SRM_MEMBER_ORDER.types(root) do
123
if let declared = type.declared /\ is_object_type(declared) /\ !declared.is_generic then
124
class_of(declared, MAP[Symbol, Type]())
125
fi
126
od
127
128
for scalar in scalars do
129
if let box: Classy = scalar.symbol then
130
let boxed = intern(CLASS_INSTANCE.of(box))
131
132
_boxed[boxed] = scalar
133
fi
134
od
135
136
// An enum is boxed as a scalar is, in a box of its own, so a
137
// boxed value says which enum it holds.
138
for type in SRM_MEMBER_ORDER.types(root) do
139
if let declared = type.declared /\ declared.symbol_kind =~ Semantic.Symbols.SymbolKind.ENUM then
140
_boxed[intern(CLASS_INSTANCE.of(declared))] = Semantic.Types.NAMED(declared)
141
fi
142
od
143
si
144
145
/// The classes added since this was last asked, each once.
146
take_new_classes() -> Collections.List[CLASS_INSTANCE] is
147
let result = LIST[CLASS_INSTANCE](_new_classes)
148
149
_new_classes.clear()
150
151
return result
152
si
153
154
/// The one instance of `type` the set holds, adding it, and its
155
/// superclass, where it is new.
156
intern(type: CLASS_INSTANCE) -> CLASS_INSTANCE is
157
let existing: CLASS_INSTANCE mut
158
159
if _classes.try_get_value(type, existing ref) then
160
return existing
161
fi
162
163
_classes[type] = type
164
_new_classes.add(type)
165
166
superclass_of(type)
167
168
return type
169
si
170
171
/// Records that the types `type`'s fields hold are not added, because
172
/// the class is already reported as instantiating itself without end.
173
///
174
/// - type: the class whose field types are left out
175
leave_field_types(type: CLASS_INSTANCE) =>
176
_fields_left.add(type)
177
178
/// Whether the types `type`'s fields hold were left out.
179
field_types_left_out(type: CLASS_INSTANCE) -> bool => _fields_left.contains(type)
180
181
/// Adds the type each of `type`'s fields holds, instance and static,
182
/// since the fields are laid out wherever the class is, whether or
183
/// not anything creates a value of those types. A class this adds is
184
/// taken by the next `take_new_classes`.
185
///
186
/// - type: the class whose fields to add the types of
187
add_field_types(type: CLASS_INSTANCE) is
188
for `field in SRM_MEMBER_ORDER.fields(SRM_EMITTED_TYPE(type.definition)) do
189
_add_held(type.specialize(`field.type))
190
od
191
si
192
193
// An array's element type is added before the array, which needs it
194
// to choose how its elements are stored.
195
_add_held(held: Type?) is
196
if !held? then
197
return
198
fi
199
200
let type = if held.is_optional /\ !held.is_value_type then held.optional_inner_type ?? held else held fi
201
202
if let generic: GENERIC = type.symbol /\ cast Classy?(generic.unspecialized_symbol) == _core.array_definition /\ type.arguments.count == 1 then
203
_add_held(type.arguments[0])
204
fi
205
206
class_of(type)
207
si
208
209
/// The class `definition` at the type arguments `type_map` gives its
210
/// parameters, added until the set is numbered. Null where the set
211
/// does not hold it, or where `definition` is not a class the object
212
/// model represents.
213
class_of(definition: Classy, type_map: Collections.Map[Symbol, Type]) -> CLASS_INSTANCE? is
214
let arguments = LIST[Type]()
215
let map = MAP[Symbol, Type]()
216
217
for (parameter, argument) in type_map do
218
if parameter.owner == definition then
219
map[parameter] = argument
220
fi
221
od
222
223
for name in definition.argument_names do
224
for (parameter, argument) in map do
225
if parameter.name =~ name then
226
arguments.add(argument)
227
fi
228
od
229
od
230
231
// A closure's frame declares no type parameters of its own: its
232
// parameters are the enclosing function's, so they are owned by
233
// the function rather than by the frame.
234
if isa Semantic.Symbols.FRAME(definition) /\ arguments.count != definition.argument_names.count then
235
arguments.clear()
236
map.clear()
237
238
for i in 0..definition.argument_names.count do
239
if let parameter = definition.type_parameter_at(i) then
240
if let argument = TYPE_MAPS.argument_of(parameter, type_map) then
241
map[parameter] = argument
242
arguments.add(argument)
243
fi
244
fi
245
od
246
fi
247
248
let key = CLASS_INSTANCE(definition, arguments, map)
249
250
let existing: CLASS_INSTANCE mut
251
252
if _classes.try_get_value(key, existing ref) then
253
return existing
254
fi
255
256
if _is_numbered \/ !is_object_type(definition) \/ arguments.count != definition.argument_names.count then
257
return null
258
fi
259
260
// An instantiation is compiled only once every argument is a
261
// type rather than a type parameter still to be bound.
262
for argument in arguments do
263
if argument.is_type_variable then
264
return null
265
fi
266
od
267
268
return intern(key)
269
si
270
271
/// The class a value of `type` is an instance of, or null where it
272
/// is not one the set holds. `type` must have no type parameters
273
/// left in it.
274
class_of(type: Type?) -> CLASS_INSTANCE? is
275
if !type? then
276
return null
277
fi
278
279
if let array = array_of(type) then
280
return array
281
fi
282
283
let symbol = type.symbol
284
285
if let generic: GENERIC = symbol then
286
if let definition: Classy = generic.unspecialized_symbol then
287
return class_of(definition, generic.type_map)
288
fi
289
elif let definition: Classy = symbol then
290
return class_of(definition, MAP[Symbol, Type]())
291
fi
292
293
return null
294
si
295
296
/// The array type `type` is, added until the set is numbered. Null
297
/// where `type` is not an array, or where the target cannot hold its
298
/// elements.
299
array_of(type: Type?) -> CLASS_INSTANCE? is
300
let generic = cast GENERIC?(type?.symbol)
301
302
if !generic? then
303
return null
304
fi
305
306
let definition = cast Classy?(generic.unspecialized_symbol)
307
308
let arguments = type!.arguments
309
310
if !definition? \/ definition != _core.array_definition \/ arguments.count != 1 then
311
return null
312
fi
313
314
let element = arguments[0]
315
316
if element.is_type_variable \/ !_holds_element(element) then
317
return null
318
fi
319
320
let map = MAP[Symbol, Type]()
321
322
for (parameter, argument) in generic.type_map do
323
if parameter.owner == definition then
324
map[parameter] = argument
325
fi
326
od
327
328
let key = CLASS_INSTANCE(definition, LIST[Type]([element]), map)
329
330
if _arrays.contains_key(key) then
331
return key
332
fi
333
334
if _is_numbered then
335
return null
336
fi
337
338
_arrays[key] = element
339
340
return key
341
si
342
343
/// Whether `type` is an array type.
344
is_array(type: CLASS_INSTANCE) -> bool => _arrays.contains_key(type)
345
346
/// The element type of the array type `type`.
347
array_element(type: CLASS_INSTANCE) -> Type => _arrays[type]
348
349
/// Whether `type` is a scalar's box rather than a class.
350
is_box(type: CLASS_INSTANCE) -> bool => _boxed.contains_key(type)
351
352
/// The scalar type a box holds a value of, or null where `type` is
353
/// not a box.
354
boxed_type(type: CLASS_INSTANCE) -> Type? is
355
let boxed: Type mut
356
357
if _boxed.try_get_value(type, boxed ref) then
358
return boxed
359
fi
360
361
return null
362
si
363
364
/// The box a value of the scalar `type` is held in, or null where
365
/// `type` is not a scalar.
366
box_type(type: Type?) -> CLASS_INSTANCE? is
367
if let box: Classy = type?.symbol then
368
let key = CLASS_INSTANCE.of(box)
369
370
let existing: CLASS_INSTANCE mut
371
372
if _classes.try_get_value(key, existing ref) /\ _boxed.contains_key(existing) then
373
return existing
374
fi
375
fi
376
377
return null
378
si
379
380
/// The class `type` extends, where the program declares it, or null
381
/// where it extends the root.
382
superclass_of(type: CLASS_INSTANCE) -> CLASS_INSTANCE? is
383
for ancestor in type.definition.ancestors do
384
if ancestor.is_trait then
385
continue
386
fi
387
388
let specialized = type.specialize(ancestor)!
389
390
if let superclass = class_of(specialized) /\ superclass != type then
391
if superclass.definition.is_object then
392
return null
393
fi
394
395
return superclass
396
fi
397
od
398
399
return null
400
si
401
402
/// Numbers every class, box and array type held, depth first down
403
/// the class tree, with boxes and arrays under the root. Nothing is
404
/// added afterwards.
405
number() is
406
_is_numbered = true
407
408
let object = _object!
409
410
_numbering.add_root(object)
411
412
let added = Collections.SET[CLASS_INSTANCE]()
413
added.add(object)
414
415
for type in _classes.values do
416
if !_boxed.contains_key(type) then
417
_add_class(type, added)
418
fi
419
od
420
421
for (box, _) in _boxed do
422
_numbering.add(box, object)
423
od
424
425
for (array, _) in _arrays do
426
_numbering.add(array, object)
427
od
428
429
_numbering.number()
430
si
431
432
_add_class(type: CLASS_INSTANCE, added: Collections.SET[CLASS_INSTANCE]) is
433
if added.contains(type) then
434
return
435
fi
436
437
let parent = superclass_of(type) ?? _object!
438
439
if !added.contains(parent) then
440
_add_class(parent, added)
441
fi
442
443
_numbering.add(type, parent)
444
added.add(type)
445
si
446
447
/// Whether `type` is a class the object model represents: not the
448
/// root, nothing reflected from an assembly, and a built-in type the
449
/// compiler constructs only where the core library gives it a
450
/// representation of its own.
451
is_object_type(type: Classy) -> bool is
452
let is_frame = isa Semantic.Symbols.FRAME(type) \/ isa Semantic.Symbols.STATE_MACHINE_FRAME_BASE(type)
453
454
if type.is_object \/ type.is_reflected then
455
return false
456
fi
457
458
// bigint is constructed with no fields, and held as a box around
459
// the host's integer once the core library gives it members.
460
if type == _core.bigint then
461
return SRM_MEMBER_ORDER.methods(SRM_EMITTED_TYPE(type)) |> Ghul.Pipes.any(m => true)
462
fi
463
464
// A tuple family is constructed too, but held as a struct once
465
// the core library gives it fields.
466
if !is_frame /\ type.location == LOCATION.internal then
467
return _is_constructed_struct_with_fields(type)
468
fi
469
470
return isa Semantic.Symbols.CLASS(type) \/ isa Semantic.Symbols.STRUCT(type) \/ is_frame \/ type.is_union \/ type.is_variant
471
si
472
473
_is_constructed_struct_with_fields(type: Classy) -> bool static is
474
if !isa Semantic.Symbols.STRUCT(type) then
475
return false
476
fi
477
478
for `field in SRM_MEMBER_ORDER.fields(SRM_EMITTED_TYPE(type)) do
479
if `field.is_instance then
480
return true
481
fi
482
od
483
484
return false
485
si
486
si
487
si