Skip to content
← Back

src/ir/program/function_bodies.ghul

1
namespace IR.Program is
2
use Semantic.Symbols.Classy
3
use Semantic.Symbols.Function
4
5
/// Questions a whole-program target asks about the functions it is
6
/// handed bodies for.
7
class FUNCTION_BODIES is
8
/// The class `definition` is a member of, through the injection
9
/// scope of a `partial` or `impl` block where it is declared in one.
10
/// Null where the owner is not a class.
11
///
12
/// - definition: the member
13
owning_class(definition: Function) -> Classy? static =>
14
if let injection: Semantic.INJECTION_SCOPE = definition.owner then
15
injection.target
16
else
17
cast Classy?(definition.owner)
18
fi
19
20
/// The class whose members `function` is one of, where its body can
21
/// name that class's type parameters: the frame of a closure body,
22
/// or the class the function is a member of. Null for a function of
23
/// a namespace.
24
///
25
/// - function: the function whose body is compiled
26
class_of_body(function: Function) -> Classy? static =>
27
if let closure: Semantic.Symbols.Closure = function /\ closure.frame? then
28
closure.frame
29
else
30
owning_class(function)
31
fi
32
33
/// Whether `function`'s body names type parameters, its own or those
34
/// of a class or function enclosing it, so that it is compiled only
35
/// at an instantiation and never as declared.
36
///
37
/// - function: the function whose body is compiled
38
is_generic(function: Function) -> bool static is
39
if function.generic_arguments.count > 0 then
40
return true
41
fi
42
43
let scope: Semantic.Scope? mut = owning_class(function) ?? function.owner
44
45
while scope? do
46
if let classy: Classy = scope /\ classy.is_generic then
47
return true
48
elif let enclosing: Function = scope /\ enclosing.generic_arguments.count > 0 then
49
return true
50
fi
51
52
if let symbol: Semantic.Symbols.Symbol = scope /\ symbol.owner != scope then
53
scope = symbol.owner
54
else
55
scope = null
56
fi
57
od
58
59
return false
60
si
61
si
62
63
/// The methods something in the program calls through a vtable or the
64
/// trait table, so that every implementation of each can be reached.
65
class DISPATCHED_METHODS is
66
_methods: Collections.SET[Function]
67
68
init() is
69
_methods = Collections.SET[Function]()
70
si
71
72
/// Records that a call dispatches on `method`. A trait method that
73
/// overrides another trait's dispatches through the one it
74
/// overrides, so a class implementing only that one is reached too.
75
note(method: Function) is
76
let root = cast Function?(method.root_specialized_from)!
77
78
if !_methods.add(root) \/ !(cast Semantic.Symbols.Classy?(root.owner)?.is_trait ?? false) then
79
return
80
fi
81
82
if let overridees = root.overridees then
83
for overridee in overridees do
84
if let function: Function = overridee /\ (cast Semantic.Symbols.Classy?(function.owner)?.is_trait ?? false) then
85
note(function)
86
fi
87
od
88
fi
89
si
90
91
/// Whether `method` is, or overrides, a method a call dispatches on.
92
/// A class's method that implements a dispatched trait method by name
93
/// and signature counts too, as the trait table binds it; a trait
94
/// method of the same shape does not, and is noted in its own right.
95
is_dispatched_to(method: Function) -> bool is
96
if _overrides_dispatched(method) then
97
return true
98
fi
99
100
if cast Semantic.Symbols.Classy?(method.owner)?.is_trait ?? false then
101
return false
102
fi
103
104
for dispatched in _methods do
105
if TRAIT_SIGNATURE_MATCH.implements(method, dispatched) then
106
return true
107
fi
108
od
109
110
return false
111
si
112
113
_overrides_dispatched(method: Function) -> bool is
114
if _methods.contains(method) then
115
return true
116
fi
117
118
if let overridees = method.overridees then
119
for overridee in overridees do
120
if let function: Function = overridee /\ _overrides_dispatched(function) then
121
return true
122
fi
123
od
124
fi
125
126
return false
127
si
128
si
129
si