Skip to content
← Back

src/ir/emitter/srm_member_order.ghul

1
namespace IR.Emitter is
2
use Semantic.Symbols.Symbol
3
use Semantic.Symbols.Scope
4
use Semantic.Symbols.Classy
5
use Semantic.Symbols.Function
6
use Semantic.Symbols.Field
7
use Semantic.Symbols.Property
8
use Semantic.Symbols.FUNCTION_GROUP
9
use Semantic.Symbols.TYPE_GROUP
10
use Semantic.Symbols.NAMESPACE
11
12
// One type in the emitted assembly.
13
//
14
// Most are declared: a class, struct, trait, union or variant the
15
// source wrote, and a symbol the emitter can read everything from.
16
// A namespace holding global functions or variables has no such
17
// symbol - `$globals` is a naming convention the back end applies,
18
// not a declaration - so the namespace stands in for its carrier and
19
// supplies the same answers.
20
class SRM_EMITTED_TYPE is
21
declared: Classy?
22
globals_of: NAMESPACE?
23
24
init(declared: Classy) is
25
self.declared = declared
26
si
27
28
init(globals_of: NAMESPACE) is
29
self.globals_of = globals_of
30
si
31
32
is_globals_carrier: bool => globals_of?
33
34
// The metadata name, which for an arity sibling carries the `N
35
// suffix that keeps the pair apart.
36
name: string =>
37
if let d = declared then d.il_metadata_name else Semantic.DotNet.GLOBALS_CARRIER.name fi
38
39
// The scope whose members become this type's members, and whose
40
// owner supplies the metadata namespace. For a carrier both are
41
// the namespace itself.
42
scope: Scope => if let d = declared then d else globals_of! fi
43
44
owning_namespace: Scope? =>
45
if let d = declared then d.owner else globals_of! fi
46
si
47
48
// The order in which types and their members are written to
49
// metadata.
50
//
51
// Two separate passes read this: one to assign row numbers before
52
// any body is encoded, and one to write the rows afterwards. They
53
// must produce the same sequence or every pre-computed handle names
54
// the wrong row, so both go through here rather than iterating a
55
// scope directly.
56
//
57
// A scope stores its members in a map keyed by name, so iterating it
58
// yields neither declaration order nor an order that is obviously
59
// stable against unrelated edits. Sorting on source position
60
// recovers declaration order, which a struct's sequential layout
61
// depends on, and gives the same answer however the map chose to
62
// enumerate. Overloads share one name, so a scope yields a
63
// FUNCTION_GROUP where the tables want one row per function; the
64
// group is flattened here.
65
class SRM_MEMBER_ORDER is
66
// The closures under each scope a search for closures starts from,
67
// found once: every emitted type in a namespace starts the same
68
// search, from its owning namespace, and the closures there do not
69
// change once declarations are final.
70
_closures_under: Collections.MAP[Scope, Collections.List[Semantic.Symbols.Closure]]? static
71
72
/// Forgets the closures found so far, for a build that has declared
73
/// its symbols afresh.
74
clear() static is
75
_closures_under = null
76
si
77
78
// Every type the assembly defines. Imported types are reachable
79
// from the same root and are excluded: they are referenced,
80
// never defined, here.
81
types(root: Scope) -> Collections.List[SRM_EMITTED_TYPE] static is
82
let declared = Collections.LIST[Classy]()
83
let namespaces = Collections.LIST[NAMESPACE]()
84
85
_collect(root, declared, namespaces, Collections.SET[Classy]())
86
87
_sort_by_declaration(declared)
88
_sort_by_declaration(namespaces)
89
90
let result = Collections.LIST[SRM_EMITTED_TYPE]()
91
92
for type in declared do
93
result.add(SRM_EMITTED_TYPE(type))
94
od
95
96
// Closure frames are types in the emitted assembly, but no
97
// scope holds them: a frame's owner is the enclosing
98
// namespace, and nothing declares it there. Declaring one
99
// would make `$frame_0` a name that resolution, completion
100
// and symbol search could all see, so they are collected
101
// from the closures that own them instead. A closure is
102
// declared into the function the literal was written in,
103
// and is itself a scope that can hold further closures.
104
for frame in _frames(declared, namespaces) do
105
result.add(SRM_EMITTED_TYPE(frame))
106
od
107
108
// Carriers last, so that adding a global to a namespace
109
// cannot renumber any declared type's members.
110
for `namespace in namespaces do
111
if _has_globals(`namespace) then
112
result.add(SRM_EMITTED_TYPE(`namespace))
113
fi
114
od
115
116
return result
117
si
118
119
// A type's scope can reach back to a type already collected -
120
// a union names itself from each of its variants - so a type is
121
// descended into once.
122
_collect(
123
scope: Scope,
124
declared: Collections.LIST[Classy],
125
namespaces: Collections.LIST[NAMESPACE],
126
seen: Collections.SET[Classy]
127
) static is
128
for symbol in scope.symbols do
129
if symbol.is_reflected then
130
continue
131
fi
132
133
if let `namespace: NAMESPACE = symbol then
134
namespaces.add(`namespace)
135
136
_collect(`namespace, declared, namespaces, seen)
137
elif let group: TYPE_GROUP = symbol then
138
// Same-name types at different argument counts are
139
// one symbol in the scope and separate types in the
140
// assembly, so the group is descended into rather
141
// than emitted.
142
for type in group.classies do
143
_collect_type(type, declared, namespaces, seen)
144
od
145
elif let type: Classy = symbol then
146
_collect_type(type, declared, namespaces, seen)
147
fi
148
od
149
si
150
151
_collect_type(
152
type: Classy,
153
declared: Collections.LIST[Classy],
154
namespaces: Collections.LIST[NAMESPACE],
155
seen: Collections.SET[Classy]
156
) static is
157
if seen.contains(type) then
158
return
159
fi
160
161
seen.add(type)
162
declared.add(type)
163
164
// A union's variants are declared into the union's own
165
// scope rather than into the namespace, and are types in
166
// their own right in the emitted assembly.
167
_collect(type, declared, namespaces, seen)
168
si
169
170
_frames(
171
declared: Collections.LIST[Classy],
172
namespaces: Collections.LIST[NAMESPACE]
173
) -> Collections.List[Classy] static is
174
let result = Collections.LIST[Classy]()
175
176
for type in declared do
177
_collect_frames(type, result)
178
od
179
180
for `namespace in namespaces do
181
_collect_frames(`namespace, result)
182
od
183
184
_sort_by_declaration(result)
185
186
return result
187
si
188
189
_collect_frames(scope: Scope, into: Collections.LIST[Classy]) static is
190
for symbol in scope.symbols do
191
if let group: FUNCTION_GROUP = symbol then
192
for function in group.functions do
193
_collect_frames_from_function(function, into)
194
od
195
elif let function: Function = symbol then
196
_collect_frames_from_function(function, into)
197
fi
198
od
199
si
200
201
_collect_frames_from_function(function: Function, into: Collections.LIST[Classy]) static is
202
// A generator or async function compiles into a state
203
// machine whose frame is a type of its own, reached from
204
// the function rather than from any scope.
205
if let state_machine = Semantic.Symbols.state_machine_for(function) then
206
if let frame = state_machine.frame then
207
into.add(frame)
208
fi
209
fi
210
211
if let async_state_machine = Semantic.Symbols.async_state_machine_for(function) then
212
if let frame = async_state_machine.frame then
213
into.add(frame)
214
fi
215
fi
216
217
for symbol in function.symbols do
218
if let closure: Semantic.Symbols.Closure = symbol then
219
if let frame = closure.frame then
220
into.add(frame)
221
fi
222
223
// A closure body can declare closures of its own.
224
_collect_frames_from_function(closure, into)
225
fi
226
od
227
si
228
229
_has_globals(`namespace: NAMESPACE) -> bool static =>
230
fields(SRM_EMITTED_TYPE(`namespace)).count > 0 \/
231
methods(SRM_EMITTED_TYPE(`namespace)).count > 0
232
233
fields(type: SRM_EMITTED_TYPE) -> Collections.List[Field] static is
234
let result = Collections.LIST[Field]()
235
236
for symbol in type.scope.symbols do
237
// A scope holds members it does not declare: pulling a
238
// super's symbols down adds them here, and a member
239
// whose owner is elsewhere belongs to that owner's rows,
240
// not to these. A member with no overloads arrives as
241
// itself and is answered for here; one with overloads
242
// arrives inside a group, which is dealt with below.
243
if !_is_owned_by(symbol, type) then
244
continue
245
fi
246
247
if symbol.is_reflected then
248
continue
249
fi
250
251
if let `field: Field = symbol then
252
result.add(`field)
253
fi
254
od
255
256
// A memoized stateless delegate caches in a static field
257
// built on demand while expressions were compiled. It is a
258
// real symbol but is declared into no scope, so it is
259
// collected from the closures that own it - and it lands on
260
// the enclosing class, or on the namespace's carrier for a
261
// global one, which is not always where the closure is
262
// written.
263
_collect_delegate_caches(type, result)
264
265
_sort_by_declaration(result, `field => _declared_at(type, `field))
266
267
return result
268
si
269
270
_collect_delegate_caches(type: SRM_EMITTED_TYPE, into: Collections.LIST[Field]) static is
271
// A cache field lands on the enclosing class, or on the
272
// namespace's carrier when the closure is a global anonymous
273
// function - and a class method can hold one of those - so
274
// the search starts from the owning namespace rather than
275
// from the type, and the owner check below selects.
276
let closures = _closures_in(type.owning_namespace ?? type.scope)
277
278
for closure in closures do
279
if !closure.has_delegate_cache_field then
280
continue
281
fi
282
283
let `field = closure.delegate_cache_field
284
285
if _is_owned_by(`field, type) then
286
into.add(`field)
287
fi
288
od
289
si
290
291
// The closures under `root`, in the order a walk of it finds them.
292
_closures_in(root: Scope) -> Collections.List[Semantic.Symbols.Closure] static is
293
let cache = _closures_under ?? Collections.MAP[Scope, Collections.List[Semantic.Symbols.Closure]]()
294
295
_closures_under = cache
296
297
if cache.contains_key(root) then
298
return cache[root]
299
fi
300
301
let closures = Collections.LIST[Semantic.Symbols.Closure]()
302
303
_collect_closures(root, closures, Collections.SET[Scope]())
304
305
cache[root] = closures
306
307
return closures
308
si
309
310
_collect_closures(
311
scope: Scope,
312
into: Collections.LIST[Semantic.Symbols.Closure],
313
seen: Collections.SET[Scope]
314
) static is
315
if seen.contains(scope) then
316
return
317
fi
318
319
seen.add(scope)
320
321
for symbol in scope.symbols do
322
if let group: FUNCTION_GROUP = symbol then
323
for function in group.functions do
324
_collect_closures(function, into, seen)
325
od
326
elif let closure: Semantic.Symbols.Closure = symbol then
327
into.add(closure)
328
329
_collect_closures(closure, into, seen)
330
else
331
_collect_closures(symbol, into, seen)
332
fi
333
od
334
si
335
336
// The properties a type declares, in the order their rows are
337
// written.
338
//
339
// Unlike the field and method runs this order is not agreed
340
// across the two passes: nothing outside the property's own rows
341
// names it, so the rows and the map that points at them are both
342
// written in the same iteration and cannot disagree.
343
//
344
// Sorted all the same. A scope enumerates by name-keyed map
345
// order, so without this an unrelated edit elsewhere in the same
346
// scope reorders the emitted rows while nothing about the
347
// properties has changed — which the planned byte-comparison of
348
// two bootstrap assemblies would read as a difference.
349
properties(type: SRM_EMITTED_TYPE) -> Collections.List[Property] static is
350
let result = Collections.LIST[Property]()
351
352
for symbol in type.scope.symbols do
353
if !_is_owned_by(symbol, type) then
354
continue
355
fi
356
357
if symbol.is_reflected then
358
continue
359
fi
360
361
if let property: Property = symbol then
362
result.add(property)
363
fi
364
od
365
366
_sort_by_declaration(result)
367
368
return result
369
si
370
371
methods(type: SRM_EMITTED_TYPE) -> Collections.List[Function] static is
372
let result = Collections.LIST[Function]()
373
374
// An enum emits no methods. Its ordering operator is innate,
375
// compiled at each call site against the underlying value,
376
// and its symbol carries no body — emitting a row for it
377
// yields a method with no code, which makes the whole type
378
// fail to load.
379
if isa Semantic.Symbols.ENUM_STRUCT(type.declared) then
380
return result
381
fi
382
383
// A frame's closure method belongs to the frame in the
384
// emitted assembly but is declared elsewhere, so the walk
385
// over the frame's own symbols does not reach it.
386
if let frame: Semantic.Symbols.FRAME = type.declared then
387
result.add(frame.closure)
388
fi
389
390
for symbol in type.scope.symbols do
391
// A scope holds members it does not declare: pulling a
392
// super's symbols down adds them here, and a member
393
// whose owner is elsewhere belongs to that owner's rows,
394
// not to these. A member with no overloads arrives as
395
// itself and is answered for here; one with overloads
396
// arrives inside a group, which is dealt with below.
397
if !_is_owned_by(symbol, type) then
398
continue
399
fi
400
401
if symbol.is_reflected then
402
continue
403
fi
404
405
if let group: FUNCTION_GROUP = symbol then
406
// The group is the subclass's own, but it holds the
407
// functions it inherits alongside the ones declared
408
// here, and those still belong to the type that
409
// declared them. Checking the group answers for the
410
// group, not for what is in it.
411
for function in group.functions do
412
if _is_owned_by(function, type) /\ _is_emitted(function) then
413
result.add(function)
414
fi
415
od
416
elif let function: Function = symbol then
417
if _is_emitted(function) then
418
result.add(function)
419
fi
420
fi
421
od
422
423
_collect_anonymous_functions(type, result)
424
425
_sort_by_declaration(result)
426
427
return result
428
si
429
430
// A function literal with no frame is emitted onto the type that
431
// encloses it - as a static method when it captures nothing, as
432
// an instance one when all it captures is `self` - but it is
433
// declared into the function it was written in. `owner` points
434
// at the emitted home without the symbol store there holding it,
435
// so the walk over that home's own symbols does not reach one.
436
// Same shape as a closure frame, and as the cache field a
437
// memoized one stores itself in.
438
_collect_anonymous_functions(
439
type: SRM_EMITTED_TYPE,
440
into: Collections.LIST[Function]
441
) static is
442
let closures = _closures_in(type.owning_namespace ?? type.scope)
443
444
for closure in closures do
445
if !closure.frame? /\ _is_owned_by(closure, type) then
446
into.add(closure)
447
fi
448
od
449
si
450
451
// Whether a member is emitted onto this type.
452
//
453
// A member's owner is sometimes the namespace symbol and
454
// sometimes the scope standing for it, and the two are distinct
455
// objects, so comparing owners directly answers false for a
456
// member that does belong here. Both sides are resolved to the
457
// namespace before comparing.
458
// An `impl` or `partial` block's scope stands for the type it
459
// injects into in the same way, so it is resolved here too: a
460
// closure written in an injected member is emitted onto the
461
// target, not onto the block.
462
// An innate is a call site's instruction sequence wearing a
463
// symbol, not a method: it has no body to encode, and the
464
// declaration it stands for is emitted separately by whichever
465
// type actually declares it. A compilation that declares its own
466
// intrinsics gets one registered into `Ghul.Intrinsics` for every
467
// intrinsic it declares, all of them owned by that namespace - so
468
// the ownership test above admits them and only this turns them
469
// away.
470
_is_emitted(function: Function) -> bool static => !function.is_innate
471
472
_is_owned_by(member: Symbol, type: SRM_EMITTED_TYPE) -> bool static =>
473
_emitted_home(member.owner) == _emitted_home(type.scope)
474
475
_emitted_home(scope: Scope?) -> Scope? static =>
476
if let injection: Semantic.INJECTION_SCOPE = scope then
477
injection.target
478
else
479
_as_namespace(scope) ?? scope
480
fi
481
482
_as_namespace(scope: Scope?) -> Scope? static =>
483
if let declared: NAMESPACE = scope then
484
declared
485
elif let namespace_scope: Semantic.NAMESPACE_SCOPE = scope then
486
namespace_scope.containing_namespace
487
else
488
null
489
fi
490
491
// Where a field was declared. An auto-property's backing field is
492
// declared at the internal location, which every such field shares,
493
// so it takes its property's position: a struct's sequential layout
494
// follows the order the properties are written in.
495
_declared_at(type: SRM_EMITTED_TYPE, `field: Field) -> Source.LOCATION static is
496
if `field.location.is_internal /\ `field.name.starts_with("$") then
497
if let property: Property = type.scope.find_direct(`field.name.substring(1)) then
498
return property.location
499
fi
500
fi
501
502
return `field.location
503
si
504
505
_sort_by_declaration[T: Symbol](symbols: Collections.LIST[T]) static =>
506
_sort_by_declaration(symbols, symbol => symbol.location)
507
508
// Overloads declared on one line, and synthesised members that
509
// share the internal location, would otherwise order
510
// arbitrarily; name and then description break the tie so the
511
// sequence is reproducible.
512
_sort_by_declaration[T: Symbol](symbols: Collections.LIST[T], location_of: (T) -> Source.LOCATION) static is
513
symbols.sort(
514
(left: T, right: T) -> int is
515
let left_location = location_of(left)
516
let right_location = location_of(right)
517
518
let by_file = left_location.file_name <> right_location.file_name
519
520
if by_file != 0 then
521
return by_file
522
fi
523
524
let by_position = left_location.start - right_location.start
525
526
if by_position != 0 then
527
return by_position
528
fi
529
530
let by_name = left.name <> right.name
531
532
if by_name != 0 then
533
return by_name
534
fi
535
536
return left.description <> right.description
537
si)
538
si
539
si
540
si