Skip to content
← Back

src/semantic/symbols/classy.ghul

1
namespace Semantic.Symbols is
2
use IO.Std
3
4
use System.Text.StringBuilder
5
6
use Collections.SET
7
8
use Ghul.Pipes
9
10
use IoC
11
use Logging
12
use Source
13
14
use IR.Values.Value
15
16
use Types.Type
17
18
class Classy: ScopedWithEnclosingScope, ClosureContext abstract is
19
// The unspecialized builder an async function returning this type
20
// drives, resolved from its AsyncMethodBuilderAttribute - read at
21
// import for a reflected type, from the attribute pragma during
22
// resolve-type-expressions for a source-declared one. Null for
23
// everything that is not a task-like.
24
async_builder: Types.Type? public
25
26
// What a `StructLayout` on the type says, null where it has none.
27
@IF.dotnet()
28
layout: Semantic.TYPE_LAYOUT? public
29
30
// Whether the type is visible outside its declaring assembly.
31
// Mirrors gen_access: an underscore-prefixed type under the
32
// private or protected policy is assembly-internal.
33
is_public_readable: bool => (
34
let policy = IoC.CONTAINER.instance.build_flags.underscore_access
35
36
!(name.starts_with('_') /\
37
(policy == Compiler.UnderscoreAccess.PRIVATE \/ policy == Compiler.UnderscoreAccess.PROTECTED))
38
)
39
40
_ancestors: Collections.LIST[Type]
41
_implementors: Collections.LIST[Symbol]?
42
_enclosing_symbols: Collections.MAP[string,Symbol]
43
_closures: Collections.SET[Closure]?
44
45
// Parameters for a generic that does not declare them into its
46
// own scope, minted lazily and cached here so every reference to
47
// the same parameter — a member's formal and a substitution
48
// map's key alike — carries one symbol. Type substitution
49
// matches on symbol identity, so a fresh symbol per request
50
// would silently miss.
51
_positional_type_parameters: Collections.LIST[GenericArgument]?
52
53
_are_overrides_resolved: bool
54
55
// The reversible record of what this class's pull-down did, so
56
// reset_pulled_down_symbols can undo it exactly. Null until the
57
// class resolves its overrides, and for classes marked resolved
58
// without a pull-down (reflected imports).
59
_inheritance_journal: INHERITANCE_JOURNAL?
60
61
type: Type?
62
set_type(value: Type) is type = value; si
63
64
span: LOCATION
65
66
// Incremental body re-walk override: also shift the declaration
67
// span when the retained interface symbol is relocated.
68
set_span(span_location: LOCATION) is
69
span = span_location
70
si
71
72
_depth: int
73
depth: int is
74
if _depth > 0 then
75
return _depth
76
fi
77
78
_depth = _calc_depth()
79
return _depth
80
si
81
82
argument_names: Collections.List[string]
83
argument_variances: Collections.List[Types.TypeVariance]? public
84
argument_constraint_kinds: Collections.List[TypeParameterConstraintKind] public
85
argument_has_constructor_constraint: Collections.List[bool] public
86
// Parallel to argument_names: the type bound (`where T : SomeBase`)
87
// one list of bounds per type parameter, empty when that parameter
88
// is unbounded. Populated at import for .NET-defined generics whose
89
// parameter symbols don't live in the symbol's own scope;
90
// ghūl-declared generics keep the bounds on the parameter symbol
91
// itself.
92
argument_type_bounds: Collections.List[Collections.List[Type]] public
93
94
// True when this type has its own accessible parameterless
95
// constructor — a zero-argument `init` for a ghūl-declared type,
96
// a public parameterless `.ctor` for an imported one. Used to
97
// check an `init` type-parameter constraint. Set when the type
98
// is declared / imported, since a ghūl constructor's signature
99
// is not resolved early enough to inspect at constraint-check
100
// time.
101
has_parameterless_constructor: bool public
102
103
// The constructor synthesised from this type's primary-constructor
104
// header, when it has one. Recorded by declare-symbols so a hover
105
// on the declaration can show the primary parameters; null for a
106
// type without a primary header (secondary constructors are not
107
// recorded here).
108
primary_constructor: Function? public
109
110
/// The method a library marked `@finalizer()` on this class, which
111
/// a target with a collector calls once an instance is
112
/// unreachable; null where the class declares none.
113
finalizer: Function? public
114
115
// Set by TYPE_GROUP when the type joins a group of same-named
116
// sibling types distinguished by generic-argument count.
117
// Triggers `\`N` suffixing in IL emission so .NET sees distinct
118
// names — lone types (no siblings) emit unchanged so the
119
// universal compiler-source path is byte-identical.
120
has_argument_count_siblings: bool public
121
122
is_generic: bool => argument_names.count > 0
123
124
get_argument_variance(index: int) -> Types.TypeVariance =>
125
if !argument_variances? \/ index < 0 \/ index >= argument_variances.count then
126
Types.TypeVariance.INVARIANT
127
else
128
argument_variances[index]
129
fi
130
131
// FIXME: not safe to expose unspecialized ancestors
132
ancestors: Collections.List[Type] => _ancestors
133
134
implementors: Collections.Iterable[Symbol]? => _implementors
135
136
il_assembly_name: string? public
137
138
// The reflected full name of an imported type - reflection's own
139
// spelling, with nesting joined by '+' - captured when the symbol
140
// is loaded, and null for a type declared in source. A
141
// custom-attribute blob names a typeof argument in this form, so
142
// it is kept as read rather than reconstructed from the IL name,
143
// whose nesting separator differs.
144
dotnet_full_name: string? public
145
146
is_workspace_visible: bool => true
147
is_capture_context: bool => true
148
is_type: bool => true
149
is_classy: bool => true
150
is_instance_context: bool => false
151
152
// True when extension across assembly boundaries is permitted -
153
// subclassing for a class, implementing or deriving for a trait.
154
// Resolved explicitly at declare-symbols time (from the `open`
155
// modifier) for ghūl-declared classes and traits, and at import
156
// time (from the presence of the `[Ghul.Internal.CLOSED_ATTRIBUTE]`
157
// marker) for imported ones. With neither path having set the
158
// state, `_is_open_by_default` decides.
159
_is_open: bool
160
_is_open_set: bool
161
162
is_open: bool =>
163
if _is_open_set then _is_open
164
else _is_open_by_default
165
fi
166
167
// Whether a later cell of the same session can extend this type
168
// even though it is closed: a session's cells keep their emitted
169
// code while later cells subclass and override, so a closed
170
// in-compilation hierarchy is still open at the dispatch trust
171
// the store-free solve and the narrowing crossings rely on. An
172
// imported type never answers true - a later cell cannot extend
173
// what another assembly declared. Narrowing complements are
174
// unaffected: the build sees every cell, so the alternatives
175
// stay enumerable, and the guards a session emits carry what
176
// that costs.
177
open_to_a_later_cell: bool =>
178
!is_reflected /\ IoC.CONTAINER.instance.build_flags.want_session
179
180
// True when the type is declared `pure`: its instance members
181
// must be pure (proven store-free or declared), with only
182
// constructors, statics and assign accessors exempt. Set from
183
// the `pure` modifier during declare-symbols. Not carried across
184
// assemblies: member-level PURE_ATTRIBUTE crosses already, and
185
// the pure-override contract binds implementors member-wise.
186
_is_pure: bool
187
188
is_pure: bool => _is_pure
189
190
mark_pure() is
191
_is_pure = true
192
si
193
194
// The answer for a kind whose open/closed state is never set
195
// explicitly. False for structs, unions, variants and the
196
// synthesised kinds - none of them can be extended from outside
197
// their declaring assembly. Structs cannot be subclassed at all;
198
// a union's only subclasses are its own compiler-generated
199
// variants, and members reach a union or a single variant only
200
// through same-assembly partial and impl blocks. `CLASS` and
201
// `TRAIT` override this with `is_reflected`, so that an
202
// imported type carrying no marker is treated as open (it was
203
// built by a compiler that does not emit one) while a
204
// ghūl-declared one resolves from the `open` modifier.
205
_is_open_by_default: bool => false
206
207
// True when the source says `open`. A submission's classes and
208
// traits are open without saying so, since a later step of the
209
// session can extend them, and a rule about what the author
210
// wrote asks this rather than `is_open`.
211
is_declared_open: bool
212
213
mark_declared_open() is
214
is_declared_open = true
215
mark_open()
216
si
217
218
mark_open() is
219
_is_open = true
220
_is_open_set = true
221
si
222
223
mark_closed() is
224
_is_open = false
225
_is_open_set = true
226
si
227
228
// A .NET delegate type: set at import by walking the base chain
229
// to System.MulticastDelegate. A delegate member runs whatever
230
// its target is, so no reading of its shape vouches for what it
231
// writes, and the trust tiers decline it on this.
232
is_delegate: bool => _is_delegate
233
234
_is_delegate: bool
235
236
mark_delegate() is
237
_is_delegate = true
238
si
239
240
// True when this Classy itself cannot appear as a runtime type
241
// (only its subclasses can). Default false — traits/structs/
242
// unions/variants are never "abstract" in this sense for
243
// narrowing purposes (unions enumerate via variants; trait
244
// implementors aren't enumerable to begin with). `Symbols.CLASS`
245
// overrides this with a settable field, set from the `abstract`
246
// modifier during declare-symbols and from
247
// `TypeAttributes.Abstract` on import.
248
is_abstract: bool => false
249
250
// True when abstractness was inferred from a body-less instance
251
// method rather than written as the `abstract` modifier. Only a
252
// class can be abstract by inference; everything else answers
253
// false because nothing else infers it.
254
is_implicitly_abstract: bool => false
255
256
// The set of direct subclasses declared in the same assembly as
257
// this Classy. Used by the variant-complement narrowing path to
258
// enumerate a closed root's possible dynamic types. Returns an
259
// empty list when the root is open — callers gate on `is_open`
260
// before consulting this. Built from `implementors`, filtered
261
// to ghūl-declared CLASS subclasses (trait implementors and
262
// imported subclasses don't count for closure).
263
closed_subclasses: Collections.Iterable[Classy] is
264
let result = Collections.LIST[Classy]()
265
266
if !_implementors? then
267
return result
268
fi
269
270
for s in _implementors do
271
if isa CLASS(s) then
272
let sub = cast Classy(s)
273
if !sub.is_reflected then
274
result.add(sub)
275
fi
276
fi
277
od
278
279
return result
280
si
281
282
// True when this Classy is the root of a closed set of subtypes
283
// narrowing can complement against. Two shapes qualify: a union
284
// (variants are the closed set), and a ghūl-declared closed
285
// class (`closed_subclasses` is the closed set). Open classes,
286
// traits, structs, enums and imported types do not.
287
is_closed_root: bool =>
288
is_union \/ (is_class /\ !is_open)
289
290
// The subtypes that make up this closed root's in-set. Returns
291
// the variants for a union, the direct in-assembly subclasses
292
// for a closed class, an empty list otherwise. Callers gate on
293
// `is_closed_root` first; the empty fallback is a safety net.
294
closed_alternatives: Collections.Iterable[Classy] is
295
let result = Collections.LIST[Classy]()
296
297
if is_union then
298
for s in symbols do
299
if s.is_variant then
300
result.add(cast Classy?(s)!)
301
fi
302
od
303
304
return result
305
fi
306
307
if is_class /\ !is_open then
308
for s in closed_subclasses do
309
result.add(s)
310
od
311
fi
312
313
return result
314
si
315
316
is_derived_from_iterable_trait: bool
317
=> find_ancestor(IoC.CONTAINER.instance.innate_symbol_lookup.get_unspecialized_iterable_type())?
318
319
is_derived_from_iterator_trait: bool
320
=> find_ancestor(IoC.CONTAINER.instance.innate_symbol_lookup.get_unspecialized_iterator_type())?
321
322
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, argument_names: Collections.List[string], enclosing_scope: Scope) is
323
super.init(location, owner, name, enclosing_scope)
324
325
_ancestors = Collections.LIST[Type]()
326
_enclosing_symbols = Collections.MAP[string,Symbol]()
327
argument_constraint_kinds = Collections.LIST[TypeParameterConstraintKind](0)
328
argument_has_constructor_constraint = Collections.LIST[bool](0)
329
argument_type_bounds = Collections.LIST[Collections.LIST[Type]](0)
330
331
self.span = span
332
self.argument_names = argument_names
333
334
type = Types.NAMED(self)
335
si
336
337
get_ancestor(i: int) -> Type
338
=> ancestors[i].specialize(Collections.MAP[Symbol,Type]())
339
340
add_ancestor(ancestor: Type) is
341
assert ancestor? else "adding null ancestor to {name}"
342
343
_ancestors.add(ancestor)
344
si
345
346
// Removes `ancestor`, the very type added, rather than one equal
347
// to it.
348
remove_ancestor(ancestor: Type) is
349
for i in 0.._ancestors.count do
350
if _ancestors[i] == ancestor then
351
_ancestors.remove_at(i)
352
return
353
fi
354
od
355
si
356
357
// The root's zero comes from CLASS's is_object branch, so every
358
// other kind is measured from its ancestry.
359
_calc_depth() -> int => _max_ancestor_depth() + 1
360
361
// Distance from the root of the inheritance graph, counting
362
// implemented traits as well as the superclass chain: a
363
// concrete type reached only through the traits it implements
364
// has to rank below them.
365
_max_ancestor_depth() -> int =>
366
ancestors |> map(a => a.depth) |> reduce(0, (max, n) => if n > max then n else max fi)
367
368
add_implementor(symbol: Symbol) is
369
let implementors mut = _implementors
370
371
if !implementors? then
372
implementors = Collections.LIST[Symbol]()
373
_implementors = implementors
374
elif implementors.contains(symbol) then
375
return
376
fi
377
378
implementors.add(symbol)
379
380
if let journal = INHERITANCE_JOURNAL.current then
381
journal.record(InheritanceOp.IMPLEMENTOR_ADDED(self, symbol))
382
fi
383
si
384
385
remove_implementor(symbol: Symbol) is
386
let implementors = _implementors
387
388
if implementors? then
389
implementors.remove(symbol)
390
fi
391
si
392
393
push_ancestor(ancestor: Type) is
394
let na = Collections.LIST[Type]()
395
396
na.add(ancestor)
397
na.add_range(_ancestors)
398
399
_ancestors = na
400
si
401
402
add_closure(closure: Closure) is
403
if !_closures? then
404
_closures = Collections.SET[Closure]()
405
fi
406
407
if !_closures.contains(closure) then
408
_closures.add(closure)
409
fi
410
si
411
412
get_closures() -> Collections.Iterable[Closure] =>
413
if _closures? then _closures else Collections.LIST[Closure](0) fi
414
415
find_member(name: string) -> Symbol? => find_direct(name)
416
417
// The synthetic `$globals` class hosts namespace-level functions and
418
// fields in IL but is not user-spellable. Hide it from qualified
419
// names so HOVER and diagnostics show `NS.member` rather than
420
// `NS.$globals.member`.
421
qualified_name: string =>
422
if name =~ "$globals" /\ owner? then
423
owner.qualified_name
424
elif owner? then
425
owner.qualify(name)
426
else
427
name
428
fi
429
430
qualify(name: string) -> string =>
431
if self.name =~ "$globals" /\ owner? then
432
owner.qualify(name)
433
else
434
"{qualified_name}.{name}"
435
fi
436
437
// The unqualified name this type is emitted under. A generic
438
// that shares its name with an arity sibling carries the `N
439
// suffix .NET uses to tell such a pair apart: without it the
440
// two occupy one metadata name, and every consumer that finds a
441
// type by name - Type.GetType, a custom-attribute blob, another
442
// assembly importing this one - resolves whichever row it
443
// reaches first. A generic with no sibling keeps the bare name
444
// it has always been emitted under.
445
il_metadata_name: string =>
446
if has_argument_count_siblings /\ is_generic then
447
"{name}`{argument_names.count}"
448
else
449
name
450
fi
451
452
// The emitted fully-qualified name, unquoted: the owner chain
453
// joined with dots and this type's emitted name. This is the
454
// name a custom-attribute blob carries for a typeof(T)
455
// argument; the binary encoder reads it here rather than
456
// rendering the type and stripping the quotes back off. An
457
// `il_name_override` of the `[assembly]Full.Name` form an
458
// imported type carries is taken verbatim past the bracket.
459
il_qualified_name: string is
460
let iln = il_name_override
461
462
if iln? then
463
let parts = iln.split(['[',']'])
464
465
if parts.count == 3 then
466
return parts[2]
467
fi
468
469
if owner? /\ !iln.contains('.') then
470
return "{_owner_il_qualified_name}.{iln}"
471
fi
472
473
return iln
474
fi
475
476
// An owner that contributes no name of its own - the root
477
// namespace, or a scope that is not a namespace or a type -
478
// leaves nothing to separate, so the name stands alone
479
// rather than picking up a leading dot.
480
if owner? then
481
let qualifier = _owner_il_qualified_name
482
483
if qualifier.length > 0 then
484
return "{qualifier}.{il_metadata_name}"
485
fi
486
fi
487
488
return il_metadata_name
489
si
490
491
// The owner chain's qualified IL name. A type's owner is a
492
// namespace or an enclosing type. A namespace's `qualified_name`
493
// carries a leading-dot root marker and is left intact by
494
// `qualify` only for non-synthetic namespaces, so read it
495
// directly and strip the marker; an enclosing type recurses.
496
_owner_il_qualified_name: string =>
497
if let ns = cast NAMESPACE?(owner) then
498
let q = ns.qualified_name
499
if q.starts_with('.') then q.substring(1) else q fi
500
elif let enclosing = cast Classy?(owner) then
501
enclosing.il_qualified_name
502
else
503
""
504
fi
505
506
// The member store backs the find_enclosing memo below; any
507
// mutation invalidates it. Batch builds never mutate a class
508
// after its expressions compile, but an analysis-mode
509
// incremental edit replaces members on a retained class — a
510
// memoized name would keep resolving to the outgoing symbol's
511
// (emptied) function group, so calls through it stop resolving.
512
declare(location: LOCATION, symbol: Symbol, symbol_definition_listener: SymbolDefinitionListener?) is
513
_enclosing_symbols.clear()
514
super.declare(location, symbol, symbol_definition_listener)
515
si
516
517
remove_direct(name: string) is
518
_enclosing_symbols.clear()
519
super.remove_direct(name)
520
si
521
522
put_direct(name: string, symbol: Symbol) is
523
_enclosing_symbols.clear()
524
super.put_direct(name, symbol)
525
si
526
527
find_enclosing(name: string) -> Symbol? is
528
if _enclosing_symbols.contains_key(name) then
529
return _enclosing_symbols[name]
530
fi
531
532
let result = find_direct(name)
533
534
if result? then
535
if !isa FUNCTION_GROUP(result) then
536
_enclosing_symbols[name] = result
537
538
return result
539
fi
540
541
let outer = find_enclosing_only(name)
542
543
if !outer? \/ !isa FUNCTION_GROUP(outer) then
544
_enclosing_symbols[name] = result
545
546
return result
547
fi
548
549
let combined = result.merged_over(outer)
550
551
_enclosing_symbols[name] = combined
552
553
return combined
554
fi
555
556
return find_enclosing_only(name)
557
si
558
559
find_ancestor_matches(prefix: string, matches: Collections.MutableMap[string, Symbols.Symbol]) is
560
assert _are_overrides_resolved
561
562
for a in ancestors do
563
if let a.scope? then
564
scope.find_member_matches(prefix, matches)
565
fi
566
od
567
si
568
569
find_member_matches(prefix: string, matches: Collections.MutableMap[string, Symbols.Symbol]) is
570
assert _are_overrides_resolved
571
572
find_direct_matches(prefix, matches)
573
si
574
575
find_enclosing_matches(prefix: string, matches: Collections.MutableMap[string, Symbols.Symbol]) is
576
assert _are_overrides_resolved
577
578
find_member_matches(prefix, matches)
579
find_enclosing_only_matches(prefix, matches)
580
si
581
582
assert_symbols_pulled_down() is
583
if !_are_overrides_resolved then
584
pull_down_super_symbols()
585
fi
586
si
587
588
mark_overrides_resolved() is
589
_are_overrides_resolved = true
590
si
591
592
pull_down_super_symbols() is
593
if _are_overrides_resolved then
594
return
595
fi
596
597
_are_overrides_resolved = true
598
599
let journal = INHERITANCE_JOURNAL(self)
600
601
// A reflected class is import-lifetime and never reset, so its
602
// journal is discarded after the pull-down. It is still pushed
603
// while the pull-down runs: a recursive ancestor resolution
604
// must never record its own mutations into the journal of the
605
// class that happened to trigger it.
606
if !is_reflected then
607
_inheritance_journal = journal
608
fi
609
610
INHERITANCE_JOURNAL.push(journal)
611
612
try
613
for i in 0..ancestors.count do
614
let symbol = get_ancestor(i).symbol
615
616
symbol.pull_down_super_symbols()
617
symbol.add_implementor(self)
618
od
619
620
let resolver = SYMBOL_INHERITANCE_RESOLVER(self)
621
622
resolver.pull_down_super_symbols_into()
623
finally
624
INHERITANCE_JOURNAL.pop()
625
yrt
626
si
627
628
// Reverse this class's pull_down_super_symbols exactly: remove the
629
// pulled-down members, override links, implementor registrations
630
// and inherited IL-name / assign-accessor state it recorded, and
631
// clear the resolved flag so resolve-overrides can run again for
632
// this class against a retained symbol table.
633
reset_pulled_down_symbols() is
634
if !_are_overrides_resolved then
635
return
636
fi
637
638
_are_overrides_resolved = false
639
640
if let journal = _inheritance_journal then
641
journal.undo()
642
643
_inheritance_journal = null
644
fi
645
si
646
647
try_specialize(
648
location: LOCATION,
649
logger: Logger,
650
actual_type_arguments: Collections.List[Type]
651
) -> Symbol? is
652
if !is_generic then
653
logger.error(location, "cannot explicitly specialize non-generic type")
654
return null
655
elif actual_type_arguments.count != argument_names.count then
656
logger.error(location, "expected {argument_names.count} explicit generic type arguments")
657
return null
658
fi
659
660
check_argument_constraints(location, logger, actual_type_arguments)
661
662
return GENERIC(location, self, actual_type_arguments)
663
si
664
665
// This type applied to its own type parameters.
666
//
667
// It is what its members' receiver is from inside its own
668
// methods, and what a subtype of it extends. The open type a
669
// generic is declared as is not a type anything can name at run
670
// time, so a row or a reference that needs to name this one has
671
// to construct it. A non-generic type is already itself.
672
own_instantiation: Type is
673
if argument_names.count == 0 then
674
return type!
675
fi
676
677
let arguments = Collections.LIST[Type]()
678
679
for index in 0..argument_names.count do
680
let parameter = type_parameter_at(index)
681
682
assert parameter? else
683
"generic type '{name}' has no type parameter '{argument_names[index]}'"
684
685
arguments.add(parameter.type!)
686
od
687
688
return Types.GENERIC(LOCATION.internal, self, arguments)
689
si
690
691
// The symbol standing for the type parameter at `index`, or null
692
// when there is none to be had.
693
//
694
// A type that writes its parameters declares them into its own
695
// scope, so the name it wrote finds them. A closure frame is the
696
// exception: its parameters are the enclosing function's,
697
// reached at a position the lowering installs rather than
698
// redeclared, so it answers with those symbols instead.
699
type_parameter_at(index: int) -> Symbol? is
700
if index < 0 \/ index >= argument_names.count then
701
return null
702
fi
703
704
if let declared = find_direct(argument_names[index]) then
705
return declared
706
fi
707
708
// Several kinds of generic carry their parameters
709
// positionally without declaring them into their own scope:
710
// an imported one, the innate types the compiler registers
711
// itself, and a tuple. There is no name to find, so the
712
// position is the whole answer. The parameter minted for it
713
// is cached, because every request has to answer with the
714
// same symbol — type substitution matches on identity, and a
715
// fresh symbol each time would silently fail to match.
716
if !_positional_type_parameters? then
717
let parameters = Collections.LIST[GenericArgument](argument_names.count)
718
719
for i in 0..argument_names.count do
720
parameters.add(CLASSY_GENERIC_ARGUMENT(location, self, argument_names[i], i))
721
od
722
723
_positional_type_parameters = parameters
724
fi
725
726
return _positional_type_parameters[index]
727
si
728
729
// The kind constraint of the type parameter at `index`. Imported
730
// generics carry it in the `argument_constraint_kinds` list (the
731
// type-parameter symbols are not declared in scope); ghūl-declared
732
// generics carry it on the type-parameter symbol itself.
733
get_argument_constraint_kind(index: int) -> TypeParameterConstraintKind is
734
if index >= 0 /\ index < argument_constraint_kinds.count then
735
return argument_constraint_kinds[index]
736
fi
737
738
if index >= 0 /\ index < argument_names.count then
739
let argument = type_parameter_at(index)
740
741
if argument? then
742
return argument.constraint_kind
743
fi
744
fi
745
746
return TypeParameterConstraintKind.NONE
747
si
748
749
// Whether the type parameter at `index` carries a parameterless-
750
// constructor (`init`) constraint. Imported .NET generics carry
751
// it in `argument_has_constructor_constraint` (populated by
752
// symbol_factory at import time); ghūl-declared generics carry it
753
// on the type-parameter symbol itself (set by declare_symbols
754
// from the parsed `[T: init]` NTE flag).
755
get_argument_has_constructor_constraint(index: int) -> bool is
756
if index >= 0 /\ index < argument_has_constructor_constraint.count then
757
return argument_has_constructor_constraint[index]
758
fi
759
760
if index >= 0 /\ index < argument_names.count then
761
let argument = type_parameter_at(index)
762
763
if argument? then
764
return argument.has_constructor_constraint
765
fi
766
fi
767
768
return false
769
si
770
771
// The bounds (`[T: A /\ B]`) of the type parameter at `index`,
772
// empty when unbounded. Imported generics carry them in
773
// `argument_type_bounds`; ghūl-declared generics carry them on the
774
// type-parameter symbol as its ancestors, with the meaningless
775
// `object` default bound dropped.
776
get_argument_type_bounds(index: int) -> Collections.List[Type] is
777
if index >= 0 /\ index < argument_type_bounds.count then
778
return argument_type_bounds[index]
779
fi
780
781
if index >= 0 /\ index < argument_names.count then
782
let argument = type_parameter_at(index)
783
784
if argument? /\ argument.is_type_variable then
785
let result = Collections.LIST[Type](0)
786
787
for ancestor in argument.ancestors do
788
if !ancestor.is_object then
789
result.add(ancestor)
790
fi
791
od
792
793
return result
794
fi
795
fi
796
797
return Collections.LIST[Type](0)
798
si
799
800
check_argument_constraints(
801
location: LOCATION,
802
logger: Logger,
803
actual_type_arguments: Collections.List[Type]
804
) is
805
GENERIC_CONSTRAINT_CHECKER().check_arguments(
806
location,
807
logger,
808
self,
809
argument_names,
810
actual_type_arguments
811
)
812
si
813
814
check_argument_type_bounds(
815
location: LOCATION,
816
logger: Logger,
817
actual_type_arguments: Collections.List[Type]
818
) is
819
GENERIC_CONSTRAINT_CHECKER().check_argument_type_bounds(
820
location,
821
logger,
822
self,
823
argument_names,
824
actual_type_arguments
825
)
826
si
827
828
check_argument_kinds(
829
location: LOCATION,
830
logger: Logger,
831
actual_type_arguments: Collections.List[Type]
832
) is
833
GENERIC_CONSTRAINT_CHECKER().check_argument_kinds(
834
location,
835
logger,
836
self,
837
argument_names,
838
actual_type_arguments
839
)
840
si
841
842
// True when this type declares an explicit static constructor
843
// (`init() static`). Such a type must not be emitted with
844
// `beforefieldinit`, which would let the CLR run the `.cctor`
845
// at an unspecified point before first use rather than
846
// precisely before it.
847
has_static_constructor: bool is
848
if let group: Symbols.FUNCTION_GROUP = find_direct("init") then
849
for f in group.functions do
850
if f.is_static_constructor then
851
return true
852
fi
853
od
854
fi
855
856
return false
857
si
858
859
// Shared head rendering for the classy kinds. Produces
860
// `<keyword> <name>[<type params>]`, optionally followed by the
861
// primary constructor arguments, so a hover on a declaration shows
862
// the shape you would write. The concrete kinds supply their
863
// keyword and whether a constructor applies.
864
// The name part already carries the type parameters through
865
// `render_name` (`render_type_argument_suffix`), so the head is just
866
// keyword + name + optional primary constructor arguments.
867
_describe_classy_head(keyword: string, include_constructor: bool) -> SignaturePart is
868
let parts = Collections.LIST[SignaturePart]()
869
parts.add(PARTS.literal(keyword))
870
parts.add(PARTS.name(self))
871
if include_constructor then
872
parts.add(_describe_constructor_arguments())
873
fi
874
return SignaturePart.SEQUENCE(parts)
875
si
876
877
// A generic classy always renders its formal type parameters.
878
render_type_argument_suffix() -> string is
879
if argument_names.count == 0 then
880
return ""
881
fi
882
let result = System.Text.StringBuilder()
883
result.append('[')
884
let first mut = true
885
for name in argument_names do
886
if !first then
887
result.append(',')
888
fi
889
first = false
890
result.append(name)
891
od
892
result.append(']')
893
return result.to_string()
894
si
895
896
_describe_constructor_arguments() -> SignaturePart is
897
let constructor = primary_constructor
898
if !constructor? \/ constructor.arguments.count == 0 then
899
return PARTS.nil()
900
fi
901
let items = Collections.LIST[SignaturePart]()
902
for i in 0..constructor.arguments.count do
903
items.add(PARTS.sequence([
904
PARTS.literal("{constructor.argument_names[i]}: "),
905
PARTS.type_ref(constructor.arguments[i])
906
]))
907
od
908
return SignaturePart.WRAPPABLE("(", ",", false, ")", items)
909
si
910
911
_make_instance_method(location: LOCATION, span: LOCATION, name: string, is_underscore: bool, enclosing: Scope) -> Function is
912
if is_underscore then
913
let policy = IoC.CONTAINER.instance.build_flags.underscore_access
914
915
if policy == Compiler.UnderscoreAccess.PRIVATE then
916
return Symbols.PRIVATE_METHOD(location, span, self, name, enclosing)
917
elif policy == Compiler.UnderscoreAccess.PROTECTED then
918
return Symbols.PROTECTED_METHOD(location, span, self, name, enclosing)
919
fi
920
fi
921
922
return Symbols.INSTANCE_METHOD(location, span, self, name, enclosing)
923
si
924
925
_make_static_method(location: LOCATION, span: LOCATION, name: string, is_underscore: bool, enclosing: Scope) -> Function is
926
if name =~ "init" then
927
return Symbols.STATIC_CONSTRUCTOR(location, span, self, name, enclosing)
928
fi
929
930
if is_underscore then
931
let policy = IoC.CONTAINER.instance.build_flags.underscore_access
932
933
if policy == Compiler.UnderscoreAccess.PRIVATE then
934
return Symbols.PRIVATE_STATIC_METHOD(location, span, self, name, enclosing)
935
elif policy == Compiler.UnderscoreAccess.PROTECTED then
936
return Symbols.PROTECTED_STATIC_METHOD(location, span, self, name, enclosing)
937
fi
938
fi
939
940
return Symbols.STATIC_METHOD(location, span, self, name, enclosing)
941
si
942
si
943
944
class CLASS: Classy, Types.Typed is
945
short_description: string => "class {name}"
946
947
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
948
_describe_classy_head("class ", true)
949
950
describe_kind(context: DESCRIBE_CONTEXT) -> string? => "class"
951
952
symbol_kind: SymbolKind => SymbolKind.CLASS
953
completion_kind: CompletionKind => CompletionKind.CLASS
954
955
is_inheritable: bool => true
956
is_class: bool => true
957
is_object: bool
958
is_root_value_type: bool
959
is_root_array_type: bool
960
961
is_instance_context: bool => true
962
963
_is_open_by_default: bool => is_reflected
964
965
// Set from the `abstract` modifier during declare-symbols for
966
// ghūl-declared classes, and from `TypeAttributes.Abstract` at
967
// import time for reflected classes. An abstract class cannot
968
// be instantiated directly (constructor calls are rejected),
969
// and the closed-narrowing path excludes it from the
970
// complement universe so the narrow can reach subtype-only
971
// members.
972
//
973
// Beyond the explicit mark, a class is implicitly abstract
974
// when it carries any user-written body-less method — the
975
// user wrote a method without a body, clearly meaning it as
976
// a contract for subclasses to satisfy, and a bare instance
977
// of this class would throw on calling it. Today's ghūl
978
// synthesises a throw body for body-less ghūl-class methods
979
// rather than emitting them as real `ABSTRACT_METHOD`
980
// symbols, so the user-intent signal has to be captured
981
// explicitly via `mark_has_bodyless_method` during
982
// declare-symbols. (Inherited-but-unimplemented abstract
983
// methods aren't covered here — for those, the user should
984
// either override or mark the class explicitly `abstract`.)
985
_is_abstract: bool
986
_bodyless_method_count: int
987
988
_has_bodyless_method: bool => _bodyless_method_count > 0
989
990
is_abstract: bool =>
991
_is_abstract \/ _has_bodyless_method
992
993
is_implicitly_abstract: bool =>
994
_has_bodyless_method /\ !_is_abstract
995
996
mark_has_bodyless_method() is
997
_bodyless_method_count = _bodyless_method_count + 1
998
si
999
1000
// Called when one of them turns out to override an implemented
1001
// member, so it carries a throwing body rather than being a
1002
// contract, and stops counting towards the class's abstractness.
1003
unmark_has_bodyless_method() is
1004
if _bodyless_method_count > 0 then
1005
_bodyless_method_count = _bodyless_method_count - 1
1006
fi
1007
si
1008
1009
mark_abstract() is
1010
_is_abstract = true
1011
si
1012
1013
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, arguments: Collections.List[string], enclosing_scope: Scope) is
1014
super.init(location, span, owner, name, arguments, enclosing_scope)
1015
1016
self.span = span
1017
si
1018
1019
mark_is_object() is
1020
is_object = true
1021
si
1022
1023
mark_is_root_value_type() is
1024
is_root_value_type = true
1025
si
1026
1027
mark_is_root_array_type() is
1028
is_root_array_type = true
1029
si
1030
1031
_calc_depth() -> int =>
1032
if is_object then
1033
0
1034
else
1035
_max_ancestor_depth() + 1
1036
fi
1037
1038
load(location: LOCATION, from: Value?, loader: SYMBOL_LOADER) -> Value => loader.load_class(self)
1039
1040
declare_type(location: LOCATION, name: string, index: int, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1041
let result = Symbols.CLASSY_GENERIC_ARGUMENT(location, self, name, index)
1042
1043
declare(location, result, symbol_definition_listener)
1044
1045
return result
1046
si
1047
1048
declare_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, is_property_accessor: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1049
let result: Function mut
1050
1051
if is_static then
1052
result = _make_static_method(location, span, name, is_private, enclosing)
1053
elif !has_body /\ !is_property_accessor then
1054
// A method the user wrote with no body at all is a
1055
// contract for a subclass to satisfy: it has nothing
1056
// to run, so the class is abstract and a concrete
1057
// subclass owes an implementation. A property's
1058
// accessor is not one of these - a body-less accessor
1059
// reads or writes the backing field.
1060
result = Symbols.ABSTRACT_METHOD(location, span, self, name, enclosing)
1061
else
1062
result = _make_instance_method(location, span, name, is_private, enclosing)
1063
fi
1064
1065
declare_function_group(location, result, symbol_definition_listener)
1066
1067
return result
1068
si
1069
1070
declare_generator_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1071
let result: Function mut
1072
1073
if is_static then
1074
result = Symbols.STATIC_GENERATOR_METHOD(location, span, self, name, enclosing)
1075
else
1076
result = Symbols.INSTANCE_GENERATOR_METHOD(location, span, self, name, enclosing)
1077
fi
1078
1079
declare_function_group(location, result, symbol_definition_listener)
1080
1081
return result
1082
si
1083
1084
declare_async_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1085
let result: Function mut
1086
1087
if is_static then
1088
result = Symbols.STATIC_ASYNC_METHOD(location, span, self, name, enclosing)
1089
else
1090
result = Symbols.INSTANCE_ASYNC_METHOD(location, span, self, name, enclosing)
1091
fi
1092
1093
declare_function_group(location, result, symbol_definition_listener)
1094
1095
return result
1096
si
1097
1098
declare_innate(location: LOCATION, name: string, innate_name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1099
let result = Symbols.INNATE_METHOD(location, self, name, enclosing, innate_name)
1100
1101
declare_function_group(location, result, symbol_definition_listener)
1102
1103
return result
1104
si
1105
1106
declare_variable(location: LOCATION, name: string, is_static: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1107
let result: Variable mut
1108
1109
if is_static then
1110
result = Symbols.STATIC_FIELD(location, self, name)
1111
else
1112
result = Symbols.INSTANCE_FIELD(location, self, name)
1113
fi
1114
1115
declare(location, result, symbol_definition_listener)
1116
1117
return result
1118
si
1119
1120
declare_property(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1121
let result: Property mut
1122
1123
if is_static then
1124
result = Symbols.STATIC_PROPERTY(location, span, self, name, is_assignable, is_private)
1125
else
1126
result = Symbols.INSTANCE_PROPERTY(location, span, self, name, is_assignable, is_private)
1127
fi
1128
1129
declare(location, result, symbol_definition_listener)
1130
1131
return result
1132
si
1133
1134
si
1135
1136
class TRAIT: Classy, Types.Typed is
1137
short_description: string => "trait {name}"
1138
1139
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
1140
_describe_classy_head("trait ", false)
1141
1142
describe_kind(context: DESCRIBE_CONTEXT) -> string? => "trait"
1143
1144
symbol_kind: SymbolKind => SymbolKind.INTERFACE
1145
completion_kind: CompletionKind => CompletionKind.INTERFACE
1146
1147
is_inheritable: bool => true
1148
is_trait: bool => true
1149
1150
_is_open_by_default: bool => is_reflected
1151
1152
is_instance_context: bool => true
1153
1154
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, arguments: Collections.List[string], enclosing_scope: Scope) is
1155
super.init(location, span, owner, name, arguments, enclosing_scope)
1156
si
1157
1158
_calc_depth() -> int => _max_ancestor_depth() + 1
1159
1160
declare_type(location: LOCATION, name: string, index: int, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1161
let result = Symbols.CLASSY_GENERIC_ARGUMENT(location, self, name, index)
1162
1163
declare(location, result, symbol_definition_listener)
1164
1165
return result
1166
si
1167
1168
load(location: LOCATION, from: IR.Values.Value?, loader: SYMBOL_LOADER) -> IR.Values.Value is
1169
return loader.load_trait(self)
1170
si
1171
1172
declare_innate(location: LOCATION, name: string, innate_name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1173
let result = Symbols.INNATE_FUNCTION(location, self, name, enclosing, innate_name)
1174
1175
declare_function_group(location, result, symbol_definition_listener)
1176
1177
return result
1178
si
1179
1180
declare_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, is_property_accessor: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1181
let result: Function mut
1182
1183
if is_static then
1184
IoC.CONTAINER.instance.logger.error(location, "cannot declare static method in trait")
1185
result = _make_static_method(location, span, name, is_private, enclosing)
1186
elif has_body then
1187
result = Symbols.DEFAULT_TRAIT_METHOD(location, span, self, name, enclosing)
1188
else
1189
result = Symbols.ABSTRACT_METHOD(location, span, self, name, enclosing)
1190
fi
1191
1192
declare_function_group(location, result, symbol_definition_listener)
1193
1194
return result
1195
si
1196
1197
declare_generator_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1198
let result: Function mut
1199
1200
if is_static then
1201
IoC.CONTAINER.instance.logger.error(location, "cannot declare static method in trait")
1202
result = Symbols.STATIC_GENERATOR_METHOD(location, span, self, name, enclosing)
1203
else
1204
result = Symbols.DEFAULT_TRAIT_GENERATOR_METHOD(location, span, self, name, enclosing)
1205
fi
1206
1207
declare_function_group(location, result, symbol_definition_listener)
1208
1209
return result
1210
si
1211
1212
declare_async_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1213
let result: Function mut
1214
1215
if is_static then
1216
IoC.CONTAINER.instance.logger.error(location, "cannot declare static method in trait")
1217
result = Symbols.STATIC_ASYNC_METHOD(location, span, self, name, enclosing)
1218
else
1219
result = Symbols.DEFAULT_TRAIT_ASYNC_METHOD(location, span, self, name, enclosing)
1220
fi
1221
1222
declare_function_group(location, result, symbol_definition_listener)
1223
1224
return result
1225
si
1226
1227
declare_property(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1228
let result: Property mut
1229
1230
if is_static then
1231
IoC.CONTAINER.instance.logger.error(location, "cannot declare static property in trait")
1232
result = Symbols.STATIC_PROPERTY(location, span, self, name, is_assignable, is_private)
1233
else
1234
result = Symbols.INSTANCE_PROPERTY(location, span, self, name, is_assignable, is_private)
1235
fi
1236
1237
declare(location, result, symbol_definition_listener)
1238
1239
return result
1240
si
1241
1242
si
1243
1244
class STRUCT: Classy, Types.Typed is
1245
short_description: string => "struct {name}"
1246
1247
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
1248
_describe_classy_head("struct ", true)
1249
1250
describe_kind(context: DESCRIBE_CONTEXT) -> string? => "struct"
1251
1252
symbol_kind: SymbolKind => SymbolKind.STRUCT
1253
completion_kind: CompletionKind => CompletionKind.STRUCT
1254
1255
is_value_type: bool => true
1256
1257
is_instance_context: bool => true
1258
1259
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, arguments: Collections.List[string], enclosing_scope: Scope) is
1260
super.init(location, span, owner, name, arguments, enclosing_scope)
1261
si
1262
1263
_calc_depth() -> int => 2 // object -> value type -> struct
1264
1265
load(location: LOCATION, from: IR.Values.Value?, loader: SYMBOL_LOADER) -> IR.Values.Value is
1266
return loader.load_struct(self)
1267
si
1268
1269
// FIXME: most of these can be folded into Classy:
1270
1271
declare_type(location: LOCATION, name: string, index: int, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1272
let result = Symbols.CLASSY_GENERIC_ARGUMENT(location, self, name, index)
1273
1274
declare(location, result, symbol_definition_listener)
1275
1276
return result
1277
si
1278
1279
declare_innate(location: LOCATION, name: string, innate_name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1280
let result = Symbols.INNATE_METHOD(location, self, name, enclosing, innate_name)
1281
1282
declare_function_group(location, result, symbol_definition_listener)
1283
1284
return result
1285
si
1286
1287
declare_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, is_property_accessor: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1288
let result: Function mut
1289
1290
if is_static then
1291
result = _make_static_method(location, span, name, is_private, enclosing)
1292
else
1293
result = Symbols.STRUCT_METHOD(location, span, self, name, enclosing)
1294
fi
1295
1296
declare_function_group(location, result, symbol_definition_listener)
1297
1298
return result
1299
si
1300
1301
declare_variable(location: LOCATION, name: string, is_static: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1302
let result: Variable mut
1303
1304
if is_static then
1305
result = Symbols.STATIC_FIELD(location, self, name)
1306
else
1307
result = Symbols.STRUCT_FIELD(location, self, name)
1308
fi
1309
1310
declare(location, result, symbol_definition_listener)
1311
1312
return result
1313
si
1314
1315
declare_property(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1316
let result: Property mut
1317
1318
if is_static then
1319
result = Symbols.STATIC_PROPERTY(location, span, self, name, is_assignable, is_private)
1320
else
1321
result = Symbols.INSTANCE_PROPERTY(location, span, self, name, is_assignable, is_private)
1322
fi
1323
1324
declare(location, result, symbol_definition_listener)
1325
1326
return result
1327
si
1328
1329
si
1330
1331
class UNION: Classy, Types.Typed is
1332
short_description: string => "union {name}"
1333
1334
describe(context: DESCRIBE_CONTEXT) -> SignaturePart is
1335
let parts = Collections.LIST[SignaturePart]()
1336
parts.add(_describe_classy_head("union ", false))
1337
1338
// A union's traits are not visible from its body - variants
1339
// only - and an impl block can add one from another file
1340
// entirely, so the hover lists them.
1341
let first mut = true
1342
for a in ancestors do
1343
if !a.is_trait then
1344
continue
1345
fi
1346
1347
parts.add(PARTS.literal(if first then ": " else ", " fi))
1348
parts.add(PARTS.type_ref(a))
1349
first = false
1350
od
1351
1352
return SignaturePart.SEQUENCE(parts)
1353
si
1354
1355
describe_kind(context: DESCRIBE_CONTEXT) -> string? => "union"
1356
1357
symbol_kind: SymbolKind => SymbolKind.CLASS
1358
completion_kind: CompletionKind => CompletionKind.CLASS
1359
1360
is_union: bool => true
1361
is_instance_context: bool => true
1362
1363
// The variant `?` and `!` target. `compile_access` reads
1364
// this to lower `u?` to `isa Default(u)` and `u!` to a cast
1365
// (plus single-field projection). Source unions get this
1366
// set during declare_members; cross-assembly unions get it
1367
// via the `DEFAULT_VARIANT_ATTRIBUTE` marker read in
1368
// `SYMBOL_FACTORY.materialize_variant`. When null — a
1369
// source union with no default, or a cross-asm union from
1370
// an older compiler that predates the marker — `?` short-
1371
// circuits to a bare null check and `!` to a plain
1372
// non-null assert.
1373
default_variant: VARIANT? public
1374
1375
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, arguments: Collections.List[string], enclosing_scope: Scope) is
1376
super.init(location, span, owner, name, arguments, enclosing_scope)
1377
si
1378
1379
_calc_depth() -> int => 1 // object -> union
1380
1381
load(location: LOCATION, from: IR.Values.Value?, loader: SYMBOL_LOADER) -> IR.Values.Value =>
1382
loader.load_union(self)
1383
1384
// FIXME: most of these can be folded into Classy:
1385
declare_type(location: LOCATION, name: string, index: int, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1386
let result = Symbols.CLASSY_GENERIC_ARGUMENT(location, self, name, index)
1387
1388
declare(location, result, symbol_definition_listener)
1389
1390
return result
1391
si
1392
1393
declare_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, is_property_accessor: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1394
let result: Function mut
1395
1396
if is_static then
1397
result = _make_static_method(location, span, name, is_private, enclosing)
1398
else
1399
result = _make_instance_method(location, span, name, is_private, enclosing)
1400
fi
1401
1402
declare_function_group(location, result, symbol_definition_listener)
1403
1404
return result
1405
si
1406
1407
declare_generator_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1408
let result: Function mut
1409
1410
if is_static then
1411
result = Symbols.STATIC_GENERATOR_METHOD(location, span, self, name, enclosing)
1412
else
1413
result = Symbols.INSTANCE_GENERATOR_METHOD(location, span, self, name, enclosing)
1414
fi
1415
1416
declare_function_group(location, result, symbol_definition_listener)
1417
1418
return result
1419
si
1420
1421
declare_async_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1422
let result: Function mut
1423
1424
if is_static then
1425
result = Symbols.STATIC_ASYNC_METHOD(location, span, self, name, enclosing)
1426
else
1427
result = Symbols.INSTANCE_ASYNC_METHOD(location, span, self, name, enclosing)
1428
fi
1429
1430
declare_function_group(location, result, symbol_definition_listener)
1431
1432
return result
1433
si
1434
1435
declare_property(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1436
let result: Property mut
1437
1438
if is_static then
1439
result = Symbols.STATIC_PROPERTY(location, span, self, name, is_assignable, is_private)
1440
else
1441
result = Symbols.INSTANCE_PROPERTY(location, span, self, name, is_assignable, is_private)
1442
fi
1443
1444
declare(location, result, symbol_definition_listener)
1445
1446
return result
1447
si
1448
1449
// Supports the backing fields the auto-property lowering
1450
// synthesises for primary-constructor parameters. User-written
1451
// fields inside a union body aren't reachable from the parser,
1452
// so this is exercised only for synthesised members.
1453
declare_variable(location: LOCATION, name: string, is_static: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1454
let result: Variable mut
1455
1456
if is_static then
1457
result = Symbols.STATIC_FIELD(location, self, name)
1458
else
1459
result = Symbols.INSTANCE_FIELD(location, self, name)
1460
fi
1461
1462
declare(location, result, symbol_definition_listener)
1463
1464
return result
1465
si
1466
1467
declare_variant(location: LOCATION, span: LOCATION, name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1468
let result = Symbols.VARIANT(location, span, self, name, argument_names, enclosing)
1469
1470
declare(location, result, symbol_definition_listener)
1471
1472
return result
1473
si
1474
1475
si
1476
1477
class VARIANT: Classy, Types.Typed is
1478
_field_names: Collections.LIST[string]
1479
1480
short_description: string => "variant {name}"
1481
1482
// No `variant` keyword: a variant has no standalone declaration
1483
// syntax outside its union, so the head is just the union-qualified
1484
// name plus any fields, and the kind is carried by `describe_kind`.
1485
describe(context: DESCRIBE_CONTEXT) -> SignaturePart is
1486
let parts = Collections.LIST[SignaturePart]()
1487
parts.add(PARTS.name(self))
1488
if _field_names.count > 0 then
1489
let items = Collections.LIST[SignaturePart]()
1490
for name in _field_names do
1491
let member = find_member(name)
1492
if member? then
1493
items.add(PARTS.sequence([
1494
PARTS.literal("{member.name}: "),
1495
PARTS.type_ref(member.type!)
1496
]))
1497
fi
1498
od
1499
parts.add(SignaturePart.WRAPPABLE("(", ",", false, ")", items))
1500
fi
1501
return SignaturePart.SEQUENCE(parts)
1502
si
1503
1504
describe_kind(context: DESCRIBE_CONTEXT) -> string? => "variant"
1505
1506
symbol_kind: SymbolKind => SymbolKind.CLASS
1507
completion_kind: CompletionKind => CompletionKind.CLASS
1508
1509
is_variant: bool => true
1510
is_unit_variant: bool => _field_names.count == 0
1511
is_instance_context: bool => true
1512
can_accept_actual_type_arguments: bool => true
1513
1514
// Total field count (own + inherited). `own_field_count`
1515
// excludes inherited primary fields; this property includes
1516
// them so the single-field unwrap projection considers the
1517
// variant's full surface — a `BOX(payload: T, ..)` against
1518
// a union with primary params is multi-field overall and
1519
// unwraps to the variant itself, not the lone own field.
1520
field_count: int => _field_names.count
1521
1522
// True for the variant the union nominated as its default —
1523
// either via the `default` modifier, or as the lone non-unit
1524
// variant fallback. Set by declare_symbols from the AST flag.
1525
is_default: bool public
1526
1527
// Own-field count (own + inherited-primary kept in
1528
// `_field_names`; `own_field_count` excludes the inherited
1529
// ones so the implicit-default rule "exactly one non-unit
1530
// variant" measures variants by what they declare themselves
1531
// — a variant whose only fields come from the union's
1532
// primary-constructor splice is still a unit variant for
1533
// that rule.
1534
own_field_count: int public
1535
1536
field_descriptions: string =>
1537
if _field_names.count == 0 then
1538
""
1539
else
1540
"({_field_names |> map(name => let member = find_member(name)! in "{member.name}: {member.type}") |> join(", ")})"
1541
fi
1542
1543
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, arguments: Collections.List[string], enclosing_scope: Scope) is
1544
super.init(location, span, owner, name, arguments, enclosing_scope)
1545
1546
_field_names = Collections.LIST[string]()
1547
si
1548
1549
_calc_depth() -> int => 2 // object -> union -> variant
1550
1551
get_destructure_member_name(index: int) -> string? =>
1552
if index < _field_names.count then
1553
_field_names[index]
1554
else
1555
null
1556
fi
1557
1558
load(location: LOCATION, from: IR.Values.Value?, loader: SYMBOL_LOADER) -> IR.Values.Value =>
1559
loader.load_variant(self)
1560
1561
// FIXME: most of these can be folded into Classy:
1562
declare_type(location: LOCATION, name: string, index: int, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1563
let result = Symbols.CLASSY_GENERIC_ARGUMENT(location, self, name, index)
1564
1565
declare(location, result, symbol_definition_listener)
1566
1567
return result
1568
si
1569
1570
declare_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, is_property_accessor: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1571
let result: Function mut
1572
1573
if is_static then
1574
result = _make_static_method(location, span, name, is_private, enclosing)
1575
else
1576
result = _make_instance_method(location, span, name, is_private, enclosing)
1577
fi
1578
1579
declare_function_group(location, result, symbol_definition_listener)
1580
1581
return result
1582
si
1583
1584
declare_generator_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1585
let result: Function mut
1586
1587
if is_static then
1588
result = Symbols.STATIC_GENERATOR_METHOD(location, span, self, name, enclosing)
1589
else
1590
result = Symbols.INSTANCE_GENERATOR_METHOD(location, span, self, name, enclosing)
1591
fi
1592
1593
declare_function_group(location, result, symbol_definition_listener)
1594
1595
return result
1596
si
1597
1598
declare_async_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1599
let result: Function mut
1600
1601
if is_static then
1602
result = Symbols.STATIC_ASYNC_METHOD(location, span, self, name, enclosing)
1603
else
1604
result = Symbols.INSTANCE_ASYNC_METHOD(location, span, self, name, enclosing)
1605
fi
1606
1607
declare_function_group(location, result, symbol_definition_listener)
1608
1609
return result
1610
si
1611
1612
declare_innate(location: LOCATION, name: string, innate_name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1613
let result = Symbols.INNATE_METHOD(location, self, name, enclosing, innate_name)
1614
1615
declare_function_group(location, result, symbol_definition_listener)
1616
1617
return result
1618
si
1619
1620
declare_variable(location: LOCATION, name: string, is_static: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1621
let result = Symbols.VARIANT_FIELD(location, self, name)
1622
1623
_field_names.add(name)
1624
own_field_count = own_field_count + 1
1625
1626
declare(location, result, symbol_definition_listener)
1627
1628
return result
1629
si
1630
1631
// Used by the reflection layer: when a cross-assembly
1632
// variant's symbol is materialized, its fields are added
1633
// via `add_member` (which doesn't touch `_field_names`).
1634
// Positional destructure (`let (v, r) = step`) consults
1635
// `_field_names` via `get_destructure_member_name(index)`,
1636
// so reflected variants need this list populated in source
1637
// order. The reflection caller iterates the variant's
1638
// instance fields in their .NET-metadata declaration order
1639
// (which matches source order in practice) and calls this
1640
// for each.
1641
register_reflected_field_name(name: string) is
1642
_field_names.add(name)
1643
own_field_count = own_field_count + 1
1644
si
1645
1646
// Called by declare_members for variant fields produced by
1647
// expanding a `..` splice against the enclosing union's
1648
// primary-constructor parameters. No VARIANT_FIELD is created
1649
// — the union base owns the storage — but the name still
1650
// contributes to the variant's arity (`is_unit_variant`,
1651
// positional destructure, field-description rendering).
1652
register_inherited_primary_field_name(name: string) is
1653
_field_names.add(name)
1654
si
1655
1656
declare_property(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
1657
let result: Property mut
1658
1659
if is_static then
1660
result = Symbols.STATIC_PROPERTY(location, span, self, name, is_assignable, is_private)
1661
else
1662
result = Symbols.INSTANCE_PROPERTY(location, span, self, name, is_assignable, is_private)
1663
fi
1664
1665
declare(location, result, symbol_definition_listener)
1666
1667
return result
1668
si
1669
1670
specialize(arguments: Collections.List[Type]) -> Symbol is
1671
return GENERIC(location, self, arguments)
1672
si
1673
1674
si
1675
1676
class ENUM_STRUCT: STRUCT is
1677
_next_value: int
1678
1679
next_value: int is
1680
let result = _next_value
1681
1682
_next_value = _next_value + 1
1683
1684
return result
1685
si
1686
1687
// The type an imported enum was declared over - `enum Wide :
1688
// long`. Absent for a ghūl-declared enum, which is always
1689
// int32-backed, and every reader of an enum value treats the
1690
// absent record as int32.
1691
underlying_type: Types.Type? public
1692
1693
// Whether the enum carries `System.FlagsAttribute`, so a value
1694
// can combine its members and reads as the names it combines.
1695
is_flags: bool public
1696
1697
// Whether a value of this enum is read wider than int32. Only
1698
// an enum imported with a 64-bit underlying type is, so every
1699
// other enum - including any that recorded nothing - is not.
1700
is_wider_than_int32(lookup: Lookups.InnateSymbolLookup) -> bool is
1701
if let declared = underlying_type then
1702
return
1703
declared.matches(lookup.get_long_type()) \/
1704
declared.matches(lookup.get_ulong_type())
1705
fi
1706
1707
return false
1708
si
1709
1710
short_description: string => "enum {name}"
1711
1712
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
1713
PARTS.sequence([PARTS.literal("enum "), PARTS.name(self)])
1714
1715
describe_kind(context: DESCRIBE_CONTEXT) -> string? => "enum"
1716
1717
symbol_kind: SymbolKind => SymbolKind.ENUM
1718
completion_kind: CompletionKind => CompletionKind.ENUM
1719
1720
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
1721
super.init(location, span, owner, name, Collections.LIST[string](), enclosing_scope)
1722
si
1723
1724
load(location: LOCATION, from: IR.Values.Value?, loader: SYMBOL_LOADER) -> IR.Values.Value is
1725
return loader.load_struct(self)
1726
si
1727
1728
declare_enum_member(location: LOCATION, name: string, value: string? mut, symbol_definition_listener: SymbolDefinitionListener?) is
1729
if !value? then
1730
value = "{next_value}"
1731
fi
1732
1733
let result = Symbols.ENUM_STRUCT_MEMBER(location, self, name, value)
1734
1735
declare(location, result, symbol_definition_listener)
1736
si
1737
1738
si
1739
1740
class VOID_STRUCT: STRUCT is
1741
is_void: bool => true
1742
1743
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, arguments: Collections.List[string], enclosing_scope: Scope) is
1744
super.init(location, span, owner, name, arguments, enclosing_scope)
1745
si
1746
1747
load(location: LOCATION, from: IR.Values.Value?, loader: SYMBOL_LOADER) -> IR.Values.Value is
1748
IoC.CONTAINER.instance.logger.error(location, "cannot use void value here")
1749
1750
return loader.load_struct(self)
1751
si
1752
si
1753
1754
class FRAME: Classy is
1755
_next_id: int static
1756
1757
_closure: Closure
1758
_constructor: Method?
1759
_captures: Collections.LIST[Field]
1760
1761
// The closure whose captures this frame holds. Its method is a
1762
// member of the frame in the emitted assembly, but it is
1763
// declared into the function the literal was written in, so a
1764
// walk over the frame's own symbols does not find it.
1765
closure: Closure => _closure
1766
1767
next_id: int static is
1768
let result = _next_id
1769
1770
_next_id = _next_id + 1
1771
1772
return result
1773
si
1774
1775
init(owner: Scope, closure: Closure) is
1776
let owner_owner: Scope mut
1777
1778
if isa Symbol(owner) then
1779
let owner_symbol = owner
1780
1781
owner_owner = owner_symbol.owner!
1782
else
1783
owner_owner = owner
1784
fi
1785
1786
super.init(LOCATION.internal, LOCATION.internal, owner_owner, "$frame_{next_id}", [], owner)
1787
1788
assert closure.captured_values?
1789
1790
_closure = closure
1791
_captures = Collections.LIST[Field](closure.captured_values!.count)
1792
1793
set_type(Types.NAMED(self))
1794
si
1795
1796
declare() is
1797
let symbol_definition_listener = IoC.CONTAINER.instance.symbol_definition_locations
1798
1799
if !_constructor? then
1800
declare_constructor(symbol_definition_listener)
1801
1802
type = Types.NAMED(self)
1803
else
1804
// Iterative-inference body retry: a field's type may
1805
// have been refreshed by Closure.find_or_add_capture
1806
// when the source variable's type resolved between
1807
// iterations. The constructor's argument types are a
1808
// snapshot from the first declare() call — refresh
1809
// them so the frame ctor's IL signature matches the
1810
// resolved field types instead of the iter-1
1811
// placeholder.
1812
refresh_constructor_argument_types()
1813
fi
1814
si
1815
1816
declare_constructor(symbol_definition_listener: SymbolDefinitionListener?) is
1817
let constructor = Symbols.INSTANCE_METHOD(LOCATION.internal, LOCATION.internal, self, "init", self)
1818
1819
let argument_names = Collections.LIST[string]()
1820
let arguments = Collections.LIST[Type]()
1821
1822
for `field in symbols |> filter(s => s.is_variable /\ s.name !~ "$recurse") |> map(s => cast Variable?(s)!) do
1823
argument_names.add(`field.name)
1824
// Defensive: a frame field captured during a closure-load
1825
// for a forward-referenced variable (e.g. mutual recursion
1826
// between two let-bound lambdas) may not yet have its
1827
// type set. set_arguments asserts no null entries, so
1828
// default to ERROR. The upstream "variable is not
1829
// defined here" diagnostic is already emitted; the IL
1830
// signature emission stage produces a `/* error type */`
1831
// comment harmlessly.
1832
arguments.add(if `field.type? then `field.type else Types.ERROR() fi)
1833
od
1834
1835
constructor.set_arguments(argument_names, arguments)
1836
1837
constructor.set_void_return_type()
1838
1839
declare(LOCATION.internal, constructor, symbol_definition_listener)
1840
1841
_constructor = constructor
1842
si
1843
1844
refresh_constructor_argument_types() is
1845
let argument_names = Collections.LIST[string]()
1846
let arguments = Collections.LIST[Type]()
1847
1848
for `field in symbols |> filter(s => s.is_variable /\ s.name !~ "$recurse") |> map(s => cast Variable?(s)!) do
1849
argument_names.add(`field.name)
1850
arguments.add(if `field.type? then `field.type else Types.ERROR() fi)
1851
od
1852
1853
_constructor!.set_arguments(argument_names, arguments)
1854
si
1855
1856
get_captured(name: string) -> Field? is
1857
for c in _captures do
1858
if c.name =~ name then
1859
return c
1860
fi
1861
od
1862
return null
1863
si
1864
1865
declare_captured(name: string, type: Type?, symbol_definition_listener: SymbolDefinitionListener?) -> Field is
1866
// type may be null during early inference iterations — the
1867
// body-retry loop walks again once the source's type resolves,
1868
// and the field's type is set on the later walk. Mirrors the
1869
// pre-strict-mode behaviour where Type was passed through
1870
// unchecked.
1871
let `field = Symbols.INSTANCE_FIELD(LOCATION.internal, self, name)
1872
1873
declare(location, `field, symbol_definition_listener)
1874
1875
if type? then
1876
`field.set_type(type)
1877
fi
1878
1879
_captures.add(`field)
1880
1881
return `field
1882
si
1883
1884
declare_recurse(symbol_definition_listener: SymbolDefinitionListener?) -> Field is
1885
let `field = Symbols.INSTANCE_FIELD(LOCATION.internal, self, "$recurse")
1886
1887
`field.is_recurse_capture = true
1888
1889
declare(location, `field, symbol_definition_listener)
1890
1891
`field.set_type(_closure.type!)
1892
1893
_captures.add(`field)
1894
1895
return `field
1896
si
1897
1898
try_update_recurse_type(type: Type) is
1899
let `field = get_captured("$recurse")
1900
1901
if `field? then
1902
`field.set_type(type)
1903
fi
1904
si
1905
1906
// The enclosing function's type-parameter symbols, in the order
1907
// this frame's own parameters mirror them. A frame does not
1908
// redeclare them: the closure installs an emitted position on
1909
// each for as long as its body is being emitted, which is what
1910
// makes them render at the frame's index rather than at their
1911
// own.
1912
_type_parameters: Collections.List[Symbol]?
1913
1914
type_parameter_at(index: int) -> Symbol? is
1915
if let self._type_parameters? /\ index >= 0 /\ index < _type_parameters.count then
1916
return _type_parameters[index]
1917
fi
1918
1919
return super.type_parameter_at(index)
1920
si
1921
1922
set_type_arguments(arguments: Collections.Iterable[Symbol]) is
1923
let symbols = arguments |> collect_mutable()
1924
1925
_type_parameters = symbols
1926
self.argument_names = symbols |> map(a => a.name) |> collect()
1927
si
1928
1929
constructor: Method => _constructor!
1930
1931
// The frame's constructor stores each argument into the capture
1932
// it is named for.
1933
constructor_body(symbol_loader: SYMBOL_LOADER) -> Collections.List[IR.Values.Value] is
1934
let internal = LOCATION.internal
1935
let result = Collections.LIST[IR.Values.Value]()
1936
1937
for c in _captures |> filter(c => c.name !~ "$recurse") do
1938
let argument = LOCAL_ARGUMENT(internal, _constructor!, c.name)
1939
argument.set_type(c.type!)
1940
1941
result.add(
1942
c.store(
1943
internal,
1944
IR.Values.Load.REFERENCE_SELF(self),
1945
argument.load(LOCATION.internal, null, symbol_loader),
1946
symbol_loader,
1947
true
1948
))
1949
od
1950
1951
return result
1952
si
1953
1954
get_create_instance(
1955
actual_arguments: Collections.List[Value],
1956
type_arguments: Collections.List[Type]?
1957
) -> IR.Values.Value? is
1958
declare()
1959
1960
try
1961
return
1962
if type_arguments? then
1963
let specialized_self = GENERIC(location, self, type_arguments)
1964
let ctor = cast Function?(specialized_self.find_member("init"))!
1965
1966
IR.Values.NEW(specialized_self.type, ctor, actual_arguments)
1967
else
1968
IR.Values.NEW(type!, _constructor!, actual_arguments)
1969
fi
1970
catch ex: System.Exception
1971
CONTAINER.instance.logger.exception(self.location, ex, "trying to generate frame instance")
1972
yrt
1973
return null
1974
si
1975
1976
si
1977
1978
class ENUM_STRUCT_MEMBER: Symbol, Types.Typed is
1979
_enum: ENUM_STRUCT => cast ENUM_STRUCT?(owner)!
1980
1981
type: Type => _enum.type!
1982
1983
// The literal exactly as written, kept for diagnostics.
1984
value: string
1985
1986
// The number it denotes, absent when `value` is not an integer
1987
// in int32's range. `value` is the source token verbatim, so it
1988
// can carry digit grouping, a radix prefix and a type suffix,
1989
// none of which a consumer can emit: an enum member's value has
1990
// to reach metadata as a number, not as the text that was
1991
// written for it.
1992
//
1993
// Absent rather than zero, because zero is a legitimate enum
1994
// value. A reflected member of an enum wider than int32 arrives
1995
// unreadable and is ordinary - `[Flags] enum : uint` with the
1996
// high bit set reaches here as "2147483648".
1997
numeric_value: int?
1998
1999
// The value as a consumer should emit it: the number where there
2000
// is one, and otherwise the text untouched.
2001
emitted_value: string =>
2002
if let number = numeric_value then "{number}" else value fi
2003
2004
short_description: string => name
2005
2006
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
2007
PARTS.sequence([
2008
PARTS.name(self),
2009
PARTS.literal(" = {value}")
2010
])
2011
2012
describe_kind(context: DESCRIBE_CONTEXT) -> string? => "enum member"
2013
2014
symbol_kind: SymbolKind => SymbolKind.ENUM_MEMBER
2015
completion_kind: CompletionKind => CompletionKind.ENUM_MEMBER
2016
2017
init(location: LOCATION, owner: ENUM_STRUCT, name: string, value: string) is
2018
super.init(location, owner, name)
2019
2020
self.value = value
2021
2022
numeric_value = _parse_value(value)
2023
si
2024
2025
@IF.dotnet()
2026
_to_int64(digits: string, radix: int) -> long static => System.Convert.to_int64(digits, radix)
2027
2028
// The digits as a `long` in base 10 or 16, throwing where they hold
2029
// none or more than 64 bits; sixteen hex digits wrap to a negative
2030
// value, as `Convert` reads them.
2031
@IF.not.dotnet()
2032
_to_int64(digits: string, radix: int) -> long static is
2033
let value mut = 0UL
2034
2035
if radix == 16 /\ digits.length > 16 then
2036
throw Ghul.OverflowException("value was either too large or too small for an Int64")
2037
fi
2038
2039
for c in digits do
2040
let digit =
2041
if c >= '0' /\ c <= '9' then cast int(c) - 48
2042
elif radix == 16 /\ c >= 'a' /\ c <= 'f' then cast int(c) - 87
2043
elif radix == 16 /\ c >= 'A' /\ c <= 'F' then cast int(c) - 55
2044
else -1
2045
fi
2046
2047
if digit < 0 then
2048
throw Ghul.FormatException("could not find any recognizable digits")
2049
fi
2050
2051
if radix == 10 /\ value > (9223372036854775807UL - cast ulong(digit)) / 10UL then
2052
throw Ghul.OverflowException("value was either too large or too small for an Int64")
2053
fi
2054
2055
value = value * cast ulong(radix) + cast ulong(digit)
2056
od
2057
2058
return cast long(value)
2059
si
2060
2061
// Reports nothing. A source member that arrives unreadable has
2062
// already been rejected, and a reflected one is passed through
2063
// untouched, so there is nothing here to diagnose.
2064
//
2065
// The suffix rule is the numeric literal classifier's, so this
2066
// reads a literal the way the rest of the compiler does.
2067
_parse_value(text: string) -> int? static is
2068
let stripped =
2069
Syntax.Process.NUMERIC_LITERAL_CLASSIFIER.strip_suffix(
2070
text.replace("_", ""))
2071
2072
let negative = stripped.starts_with("-")
2073
let digits mut = if negative then stripped.substring(1) else stripped fi
2074
2075
let radix mut = 10
2076
2077
if digits.starts_with("0x") \/ digits.starts_with("0X") then
2078
radix = 16
2079
digits = digits.substring(2)
2080
fi
2081
2082
let result mut = 0L
2083
let parsed mut = digits.length > 0
2084
2085
if parsed then
2086
try
2087
result = _to_int64(digits, radix)
2088
catch ex: System.Exception
2089
parsed = false
2090
yrt
2091
fi
2092
2093
if negative then
2094
result = -result
2095
fi
2096
2097
if !parsed \/ result > 2147483647L \/ result < -2147483648L then
2098
return null
2099
fi
2100
2101
return cast int(result)
2102
si
2103
2104
specialize(type_map: Collections.Map[string,Symbols.Symbol], owner: GENERIC) -> Symbol
2105
=> self
2106
2107
load(location: LOCATION, from: Value?, loader: SYMBOL_LOADER) -> Value is
2108
let result = loader.load_enum_struct_member(self)
2109
2110
return result
2111
si
2112
2113
si
2114
si