Skip to content
← Back

src/semantic/symbols/method.ghul

1
namespace Semantic.Symbols is
2
use IO.Std
3
4
use System.Text.StringBuilder
5
6
use Collections.Iterable
7
use Collections.LIST
8
9
use IoC
10
use Logging
11
use Source
12
13
use IR.Values.Value
14
15
use Types.Type
16
17
class Method: Function abstract is
18
_throws_not_supported: bool
19
20
throws_not_supported: bool => _throws_not_supported
21
22
mark_throws_not_supported() is
23
_throws_not_supported = true
24
si
25
26
// An imported method carries the name its assembly gives it,
27
// which says whether it is a constructor; a declared one is a
28
// constructor when it is an instance init.
29
is_constructor: bool =>
30
if let imported_name = il_name_override then
31
imported_name =~ ".ctor"
32
else
33
name =~ "init" /\ !is_static_constructor
34
fi
35
36
// Set at import for a reflected method that a call can never
37
// dispatch away from: a static that is not a static-virtual
38
// interface slot, or an instance member the CLR binds
39
// statically (non-virtual, final, declared on a struct, or on
40
// a sealed owner). No override in any assembly can stand
41
// behind a call to such a method, which is what lets the
42
// reachability tiers in IMPORT_ARGUMENTS trust one from its
43
// signature alone.
44
cannot_be_overridden: bool public
45
46
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
47
super.init(location, span, owner, name, enclosing_scope)
48
si
49
50
// Underscore access policy for the state-machine method kinds
51
// (generator / async). Those are already distinct method kinds and so
52
// cannot be the dedicated PRIVATE_/PROTECTED_ classes; they consult the
53
// name and policy here instead. Regular methods use the dedicated kinds
54
// chosen in Classy._make_instance_method / _make_static_method.
55
_is_underscore_non_public: bool =>
56
name.starts_with('_') /\
57
IoC.CONTAINER.instance.build_flags.underscore_access != Compiler.UnderscoreAccess.LEGACY
58
59
_gen_underscore_access(buffer: StringBuilder) is
60
if _is_underscore_non_public then
61
buffer.append("assembly ")
62
else
63
buffer.append("public ")
64
fi
65
si
66
67
// Shared by every default-trait-method kind: the plain one and the
68
// generator / async ones, which extend the state-machine method
69
// classes instead and so cannot inherit it.
70
_check_ineffective_trait_override(into: Classy, overrider: Function, logger: Logger) is
71
// Property accessors flow through handle_property_overridee, which
72
// raises this diagnostic at the property level. Skip the accessor
73
// here so we don't double-fire with the mangled `$get_*`/`$set_*`
74
// name.
75
if name.starts_with('$') then
76
return
77
fi
78
79
if let owner_classy: Classy = owner then
80
INEFFECTIVE_TRAIT_OVERRIDE_CHECKER()
81
.check(into, overrider.location, overrider.to_string(), self, name, owner_classy, logger)
82
fi
83
si
84
85
_underscore_is_accessible_to(accessor: Classy?) -> bool is
86
if !_is_underscore_non_public then
87
return true
88
fi
89
90
let o = cast Classy?(owner?.unspecialized_symbol)
91
92
if IoC.CONTAINER.instance.build_flags.underscore_access == Compiler.UnderscoreAccess.PRIVATE then
93
return is_accessible_to_declaring_type(accessor)
94
fi
95
96
// PROTECTED: a member reached through a specialization is the
97
// declaring type under a distinct symbol, so normalise before
98
// the assignability test - same specialisation-identity case
99
// as the private branch above.
100
let owning_type = o?.type
101
102
return accessor? /\ owning_type? /\ accessor.type? /\ owning_type.is_assignable_from(accessor.type)
103
si
104
si
105
106
class INSTANCE_METHOD: Method is
107
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
108
_describe_function(context, !is_constructor)
109
110
describe_kind(context: DESCRIBE_CONTEXT) -> string? =>
111
if is_constructor then "constructor" else "{pure_prefix}{access_prefix}method" fi
112
113
symbol_kind: SymbolKind => SymbolKind.METHOD
114
completion_kind: CompletionKind => CompletionKind.METHOD
115
116
is_instance: bool => true
117
is_virtual: bool => true
118
119
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
120
super.init(location, span, owner, name, enclosing_scope)
121
si
122
123
declare_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
124
declare_closure_symbol(location, Symbols.INSTANCE_CLOSURE(location, owner, name, enclosing, is_recursive))
125
126
declare_async_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
127
declare_closure_symbol(location, Symbols.INSTANCE_ASYNC_CLOSURE(location, owner, name, enclosing, is_recursive))
128
129
130
declare_generator_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
131
declare_closure_symbol(location, Symbols.INSTANCE_GENERATOR_CLOSURE(location, owner, name, enclosing, is_recursive))
132
133
load(location: LOCATION, from: Value?, loader: SYMBOL_LOADER) -> Value => loader.load_instance_method(location, from, self)
134
135
load_self(location: LOCATION, loader: SYMBOL_LOADER) -> Value is
136
let context = IoC.CONTAINER.instance.symbol_table.current_instance_context
137
138
return IR.Values.Load.REFERENCE_SELF(context!, context.own_instantiation)
139
si
140
141
call(location: Source.LOCATION, from: Value?, arguments: Collections.List[Value], type: Type?, caller: FUNCTION_CALLER) -> Value is
142
return caller.call_instance_method(location, from, self, arguments, self.arguments, type)
143
si
144
145
try_override(into: Classy, overridee: Function, logger: Logging.Logger) is
146
overridee.try_instance_override_me(into, self, logger)
147
si
148
149
try_instance_override_me(into: Classy, overrider: Function, logger: Logger) is
150
// A class member whose return type the inherited class member's
151
// does not accept hides that member rather than overriding it: it
152
// takes a slot of its own, and a caller holding the base type
153
// reaches the base's.
154
if _is_hidden_by_return_type(into, overrider) then
155
if !into.is_reflected then
156
logger.warn(overrider.location, "hides-inherited", "hides {self} due to different return type {overrider.return_type}", location, "hidden member declared here")
157
fi
158
159
return
160
fi
161
162
let return_type_matches = overrider.ensure_return_type_matches(into, self, true, logger)
163
164
overrider.ensure_arguments_accept_optionals(into, self, true, logger)
165
166
let il_name_matches = overrider.ensure_il_name_matches(into, self, "override", logger)
167
168
overrider.add_overridee(self)
169
self.add_overrider(overrider)
170
si
171
172
_is_hidden_by_return_type(into: Classy, overrider: Function) -> bool =>
173
!into.is_trait /\ !isa STRUCT(into) /\
174
!(cast Classy?(owner)?.is_trait ?? true) /\
175
!overrider.has_overriding_return_type(self)
176
177
try_struct_override_me(into: Classy, overrider: Function, logger: Logger) is
178
let return_type_matches = overrider.ensure_return_type_matches(into, self, true, logger)
179
180
overrider.ensure_arguments_accept_optionals(into, self, true, logger)
181
182
let il_name_matches = overrider.ensure_il_name_matches(into, self, "override", logger)
183
184
overrider.add_overridee(self)
185
self.add_overrider(overrider)
186
si
187
188
try_abstract_override_me(into: Classy, overrider: Function, logger: Logger) is
189
// A trait restating a member it inherits leaves the
190
// obligation where it was, and an imported type settled its
191
// own hierarchy in the assembly it came from, where nobody
192
// compiling against it can change anything - so a
193
// diagnostic there would be unactionable.
194
if into.is_trait \/ overrider.is_reflected then
195
logger.info(overrider.location, "hides {self}")
196
197
return
198
fi
199
200
// A trait's default is withdrawn by a class that writes the
201
// member again with no body: the class is abstract and its
202
// subclasses owe an implementation, which is what the
203
// declaration says. A class member with a body is the case
204
// that cannot go back to an abstract row - a caller holding
205
// the base type would still reach the override - so that
206
// one is given a body that throws, saying at the call what
207
// the declaration says at the class.
208
if !is_default_trait_method then
209
overrider.mark_throws_unimplemented()
210
fi
211
212
record_implementing_override(into, overrider, logger)
213
si
214
215
si
216
217
// An underscore-prefixed instance method under the PROTECTED policy: a
218
// normal virtual method that a subclass in the same assembly can override,
219
// but nothing outside the assembly can see.
220
class PROTECTED_METHOD: INSTANCE_METHOD is
221
is_public_readable: bool => false
222
223
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
224
super.init(location, span, owner, name, enclosing_scope)
225
si
226
227
is_accessible_to(accessor: Classy?) -> bool =>
228
accessor? /\
229
let o = cast Classy?(owner) in
230
o? /\ o.type? /\ accessor.type? /\ o.type.is_assignable_from(accessor.type)
231
232
access_prefix: string => "protected "
233
si
234
235
// An underscore-prefixed instance method under the PRIVATE policy: an
236
// implementation detail of its declaring type. Not virtual, so a same-named
237
// method in a subclass is a distinct method rather than an override, and
238
// calls resolve statically.
239
class PRIVATE_METHOD: INSTANCE_METHOD is
240
is_virtual: bool => false
241
242
is_public_readable: bool => false
243
244
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
245
super.init(location, span, owner, name, enclosing_scope)
246
si
247
248
try_override(into: Classy, overridee: Function, logger: Logging.Logger) is
249
si
250
251
is_accessible_to(accessor: Classy?) -> bool =>
252
is_accessible_to_declaring_type(accessor)
253
254
access_prefix: string => "private "
255
si
256
257
class STRUCT_METHOD: INSTANCE_METHOD is
258
// STRUCT_METHOD inherits describe / describe_kind from
259
// INSTANCE_METHOD unchanged.
260
symbol_kind: SymbolKind => SymbolKind.METHOD
261
completion_kind: CompletionKind => CompletionKind.METHOD
262
263
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
264
super.init(location, span, owner, name, enclosing_scope)
265
si
266
267
load(location: LOCATION, from: Value?, loader: SYMBOL_LOADER) -> Value => loader.load_struct_method(location, from, self)
268
269
load_self(location: LOCATION, loader: SYMBOL_LOADER) -> Value is
270
let context = IoC.CONTAINER.instance.symbol_table.current_instance_context
271
272
return IR.Values.Load.REFERENCE_SELF(context!, context.own_instantiation)
273
si
274
275
call(location: Source.LOCATION, from: Value?, arguments: Collections.List[Value], type: Type?, caller: FUNCTION_CALLER) -> Value =>
276
caller.call_struct_method(location, from, self, arguments, self.arguments, type)
277
278
try_override(into: Classy, overridee: Function, logger: Logging.Logger) =>
279
overridee.try_struct_override_me(into, self, logger)
280
281
try_struct_override_me(into: Classy, overrider: Function, logger: Logger) =>
282
logger.error(overrider.location, "cannot override struct method {self}", self.location, "declared here")
283
284
try_instance_override_me(into: Classy, overrider: Function, logger: Logger) =>
285
logger.error(overrider.location, "cannot override struct method {self}", self.location, "declared here")
286
287
try_abstract_override_me(into: Classy, overrider: Function, logger: Logger) =>
288
logger.error(overrider.location, "cannot override struct method {self}", self.location, "declared here")
289
290
si
291
292
class ABSTRACT_METHOD: INSTANCE_METHOD is
293
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
294
_describe_function(context, true)
295
296
describe_kind(context: DESCRIBE_CONTEXT) -> string? =>
297
"{pure_prefix}abstract method"
298
299
// Set when this declaration overrides a member that has a body:
300
// the slot is already implemented, so the row carries a
301
// throwing body rather than being abstract.
302
_throws_unimplemented: bool
303
304
throws_unimplemented: bool => _throws_unimplemented
305
306
307
mark_throws_unimplemented() is
308
if _throws_unimplemented then
309
return
310
fi
311
312
_throws_unimplemented = true
313
314
// The member has a body after all, so it is not what makes
315
// the class abstract.
316
if let owner_class: CLASS = owner then
317
owner_class.unmark_has_bodyless_method()
318
fi
319
si
320
321
is_abstract: bool => !_throws_unimplemented
322
323
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
324
super.init(location, span, owner, name, enclosing_scope)
325
si
326
327
call(location: Source.LOCATION, from: Value?, arguments: Collections.List[Value], type: Type?, caller: FUNCTION_CALLER) -> Value => caller.call_abstract_method(location, from, self, arguments, self.arguments, type)
328
329
try_pull_down_into(
330
into: Classy,
331
other_overridee_symbols: Collections.Iterable[Symbol],
332
logger: Logging.Logger
333
) is
334
// An abstract class is entitled to leave it, and a trait
335
// carries it onward; a reflected type answered for itself
336
// in the assembly it came from.
337
if !into.is_trait /\ !into.is_reflected /\ !into.is_abstract /\ !_throws_unimplemented then
338
logger.error(into.location, "must implement {self}", self.location, "member declared here")
339
fi
340
341
into.add_member(self)
342
si
343
344
try_override(into: Classy, overridee: Function, logger: Logging.Logger) =>
345
overridee.try_abstract_override_me(into, self, logger)
346
347
try_instance_override_me(into: Classy, overrider: Function, logger: Logger) =>
348
record_implementing_override(into, overrider, logger)
349
350
try_struct_override_me(into: Classy, overrider: Function, logger: Logger) =>
351
record_implementing_override(into, overrider, logger)
352
353
try_abstract_override_me(into: Classy, overrider: Function, logger: Logger) is
354
// A trait redeclaring a member it inherits states the
355
// contract again for its own readers and leaves the
356
// obligation where it was, and an imported type answered
357
// for itself in the assembly it came from. A class
358
// redeclaring one is overriding it: the member it declares
359
// is what its own subclasses have to satisfy, and the
360
// contracts the trait member carries have to reach them
361
// through it.
362
if into.is_trait \/ overrider.is_reflected then
363
logger.info(overrider.location, "hides {self}")
364
365
return
366
fi
367
368
record_implementing_override(into, overrider, logger)
369
si
370
371
si
372
373
class DEFAULT_TRAIT_METHOD: INSTANCE_METHOD is
374
describe_kind(context: DESCRIBE_CONTEXT) -> string? =>
375
"{pure_prefix}default trait method"
376
377
is_default_trait_method: bool => true
378
379
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
380
super.init(location, span, owner, name, enclosing_scope)
381
si
382
383
try_instance_override_me(into: Classy, overrider: Function, logger: Logger) is
384
super.try_instance_override_me(into, overrider, logger)
385
386
_check_ineffective_trait_override(into, overrider, logger)
387
si
388
si
389
390
class STATIC_METHOD: Method is
391
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
392
_describe_function(context, true)
393
394
describe_kind(context: DESCRIBE_CONTEXT) -> string? =>
395
"{pure_prefix}{access_prefix}class method"
396
symbol_kind: SymbolKind => SymbolKind.METHOD
397
completion_kind: CompletionKind => CompletionKind.METHOD
398
399
// Set for a reflected static virtual/abstract interface member -
400
// the only shape of static method the CLR allows to be
401
// dispatched through an as-yet-unresolved type parameter.
402
is_static_interface_virtual: bool public
403
is_virtual: bool => is_static_interface_virtual
404
405
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
406
super.init(location, span, owner, name, enclosing_scope)
407
si
408
409
declare_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
410
declare_closure_symbol(location, Symbols.STATIC_CLOSURE(location, owner, name, enclosing, is_recursive))
411
412
declare_async_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
413
declare_closure_symbol(location, Symbols.STATIC_ASYNC_CLOSURE(location, owner, name, enclosing, is_recursive))
414
415
416
declare_generator_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
417
declare_closure_symbol(location, Symbols.STATIC_GENERATOR_CLOSURE(location, owner, name, enclosing, is_recursive))
418
419
load(location: LOCATION, from: Value?, loader: SYMBOL_LOADER) -> Value => loader.load_static_method(self)
420
421
call(location: Source.LOCATION, from: Value?, arguments: Collections.List[Value], type: Type?, caller: FUNCTION_CALLER) -> Value is
422
if is_static_interface_virtual then
423
let dispatch_type = _static_interface_dispatch_type(from)
424
425
if dispatch_type? then
426
return caller.call_static_interface_method(self, dispatch_type, arguments, self.arguments, type)
427
fi
428
429
// A static virtual member has no body on its declaring
430
// interface - implementations are found through the type
431
// argument the call dispatches through. Without one there
432
// is nothing to call, and emitting a memberref against the
433
// open interface writes its own type parameter into code
434
// that has no generic scope to resolve it.
435
IoC.CONTAINER.instance.logger.error(location, "cannot infer type here")
436
437
return IR.Values.DUMMY(Types.ERROR(), location)
438
fi
439
440
return caller.call_static_method(self, arguments, self.arguments, type)
441
si
442
443
// The type to dispatch through. Written as `T.member(...)` the
444
// receiver names it. Reached any other way - an operator, or the
445
// member imported into scope by name - there is no receiver, and
446
// the self type of the interface carries it instead: every static
447
// virtual interface member is declared on an interface whose first
448
// type argument is the implementing type. That type is a type
449
// parameter where the call is generic and a concrete type where
450
// the interface is instantiated at one; `constrained.` takes
451
// either.
452
_static_interface_dispatch_type(from: Value?) -> Types.Type? is
453
if from? /\ from.type? then
454
return from.type
455
fi
456
457
if let owner_generic = cast GENERIC?(owner) /\ owner_generic.arguments.count > 0 then
458
return owner_generic.arguments[0]
459
fi
460
461
return null
462
si
463
464
try_override(into: Classy, overridee: Function, logger: Logging.Logger) is
465
// This is arguably not an issue as the hidden instance remains available via super.method()
466
if !is_internal /\ overridee.is_instance then
467
logger.warn(self.location, "hides-inherited", "static method hides {overridee}", overridee.location, "hidden method declared here")
468
fi
469
si
470
471
// Don't believe it makes sense to report an instance methods hiding a static method
472
// as the static method remains available via CLASS.method()
473
try_instance_override_me(into: Classy, overrider: Function, logger: Logger) is
474
si
475
476
try_struct_override_me(into: Classy, overrider: Function, logger: Logger) is
477
si
478
479
try_abstract_override_me(into: Classy, overrider: Function, logger: Logger) is
480
si
481
si
482
483
// Underscore-prefixed static methods under the PROTECTED / PRIVATE
484
// policies. Static methods are already non-virtual, so only the emitted
485
// accessibility differs from STATIC_METHOD.
486
class PROTECTED_STATIC_METHOD: STATIC_METHOD is
487
is_public_readable: bool => false
488
489
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
490
super.init(location, span, owner, name, enclosing_scope)
491
si
492
493
is_accessible_to(accessor: Classy?) -> bool =>
494
accessor? /\
495
let o = cast Classy?(owner) in
496
o? /\ o.type? /\ accessor.type? /\ o.type.is_assignable_from(accessor.type)
497
498
access_prefix: string => "protected "
499
si
500
501
class PRIVATE_STATIC_METHOD: STATIC_METHOD is
502
is_public_readable: bool => false
503
504
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
505
super.init(location, span, owner, name, enclosing_scope)
506
si
507
508
is_accessible_to(accessor: Classy?) -> bool =>
509
is_accessible_to_declaring_type(accessor)
510
511
access_prefix: string => "private "
512
si
513
514
// A static constructor: `init() static`. Emitted as the owning
515
// type's `.cctor`, which the CLR runs once before the type is first
516
// used. It takes no parameters and no receiver; a parameterised
517
// static init is rejected during declare-symbols.
518
class STATIC_CONSTRUCTOR: STATIC_METHOD is
519
is_static_constructor: bool => true
520
521
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
522
_describe_function(context, false)
523
524
describe_kind(context: DESCRIBE_CONTEXT) -> string? => "static constructor"
525
526
init(location: LOCATION, span: LOCATION, owner: Scope, name: string, enclosing_scope: Scope) is
527
super.init(location, span, owner, name, enclosing_scope)
528
si
529
si
530
si