Skip to content
← Back

src/ir/program/display_plan.ghul

1
namespace IR.Program is
2
use Collections.LIST
3
use Collections.MAP
4
5
use Semantic.Types.Type
6
use Semantic.Symbols.Symbol
7
use Semantic.Symbols.Classy
8
use Semantic.Symbols.Function
9
10
use IR.Emitter.SRM_MEMBER_ORDER
11
use IR.Emitter.SRM_EMITTED_TYPE
12
13
/// What a display plan needs to know about the program's classes from
14
/// the target that compiles them.
15
trait DisplayTarget open is
16
/// The class a value of `type` is an instance of, or null where the
17
/// program has none.
18
class_of(type: Type?) -> CLASS_INSTANCE?
19
20
/// The trait instance a value of `type` is, or null where `type` is
21
/// not a trait.
22
trait_of(type: Type) -> CLASS_INSTANCE?
23
24
/// The class `type` extends, or null where that is the root.
25
superclass_of(type: CLASS_INSTANCE) -> CLASS_INSTANCE?
26
27
/// The element type of the array type `type`.
28
array_element(type: CLASS_INSTANCE) -> Type
29
30
/// The core library's display state class, where the program
31
/// renders values for display.
32
display_state: CLASS_INSTANCE?
33
34
/// Whether `type` is a class with a representation of its own.
35
is_object_type(type: CLASS_INSTANCE) -> bool
36
37
/// Whether `type` is a scalar's box.
38
is_box(type: CLASS_INSTANCE) -> bool
39
40
/// Whether `type` is an array type.
41
is_array(type: CLASS_INSTANCE) -> bool
42
43
/// Whether `type` is the string type.
44
is_string(type: CLASS_INSTANCE) -> bool
45
46
/// Whether `type` is a struct.
47
is_struct(type: CLASS_INSTANCE) -> bool
48
49
/// Whether `type` is the built-in `bigint`.
50
is_bigint(type: CLASS_INSTANCE) -> bool
51
si
52
53
/// Which types get a displayer, and what each displayer calls: for
54
/// each type with no text of its own, a function that writes a value
55
/// of it for display through the core library's display state. A
56
/// sequence is written as its elements, a tuple as its parts, and
57
/// anything else as its type and public properties.
58
class DISPLAY_PLAN is
59
_target: DisplayTarget
60
_core: CoreClasses
61
62
/// A plan for the classes `target` compiles.
63
init(target: DisplayTarget, core: CoreClasses) is
64
_target = target
65
_core = core
66
si
67
68
/// The display state's methods a displayer calls, or null where the
69
/// program renders nothing for display, or the state lacks any of
70
/// them.
71
helpers: DISPLAY_HELPERS? =>
72
if let state = _target.display_state then DISPLAY_HELPERS.of(state) else null fi
73
74
/// What the displayer of `type` calls, in the order it calls them,
75
/// and nothing where `type` has no displayer.
76
calls(type: CLASS_INSTANCE, helpers: DISPLAY_HELPERS) -> Collections.List[FUNCTION_INSTANCE] is
77
let result = LIST[FUNCTION_INSTANCE]()
78
79
if let element = element_type(type) then
80
result.add(helpers.elements(element))
81
elif is_tuple(type) then
82
result.add(helpers.begin_parts)
83
84
for part in type.arguments do
85
result.add(helpers.part(part))
86
od
87
88
result.add(helpers.end_parts)
89
elif _shows_members(type) then
90
result.add(helpers.begin_members)
91
92
for member in members(type) do
93
if let getter = member.getter then
94
result.add(getter)
95
fi
96
97
result.add(helpers.member(member.type))
98
od
99
100
result.add(helpers.end_members)
101
fi
102
103
return result
104
si
105
106
/// The element type of `type` where a value of it is a sequence,
107
/// displayed as its elements: an array, or a class with no
108
/// `to_string` of its own that implements `Iterable`.
109
element_type(type: CLASS_INSTANCE) -> Type? is
110
if _target.is_array(type) then
111
return _target.array_element(type)
112
fi
113
114
if !_target.is_object_type(type) \/ _target.is_box(type) \/ !_has_no_text_of_its_own(type) then
115
return null
116
fi
117
118
if let iterable = _core.iterable_definition then
119
return _iterable_element(type.definition, type.type_map, iterable)
120
fi
121
122
return null
123
si
124
125
_iterable_element(from: Classy, map: Collections.Map[Symbol, Type], iterable: Symbol) -> Type? is
126
for ancestor in from.ancestors do
127
let specialized = TYPE_MAPS.specialize(ancestor, map)!
128
129
if let ancestor_class = _target.trait_of(specialized) ?? _target.class_of(specialized) then
130
if ancestor_class.definition == iterable /\ ancestor_class.arguments.count == 1 then
131
return ancestor_class.arguments[0]
132
fi
133
134
if let element = _iterable_element(ancestor_class.definition, ancestor_class.type_map, iterable) then
135
return element
136
fi
137
fi
138
od
139
140
return null
141
si
142
143
/// Whether `type` is a tuple: a struct the compiler constructs, whose
144
/// type arguments are its elements' types. bigint is constructed
145
/// too, but answers with its own `to_string`, so is not one.
146
is_tuple(type: CLASS_INSTANCE) -> bool =>
147
_target.is_struct(type) /\ type.definition.location == Source.LOCATION.internal /\ !_target.is_bigint(type)
148
149
// Whether a value of `type` is displayed as its type and members:
150
// a class or struct of the program's own, with no `to_string` of
151
// its own or inherited from anything but the root.
152
_shows_members(type: CLASS_INSTANCE) -> bool is
153
let definition = type.definition
154
155
if !_target.is_object_type(type) \/ _target.is_box(type) \/ _target.is_array(type) \/
156
_target.is_string(type) \/ definition.is_trait \/ definition.is_object \/
157
definition == _target.display_state?.definition
158
then
159
return false
160
fi
161
162
return _has_no_text_of_its_own(type)
163
si
164
165
// Whether neither `type` nor any ancestor but the root declares
166
// `to_string`.
167
_has_no_text_of_its_own(type: CLASS_INSTANCE) -> bool is
168
let current: CLASS_INSTANCE? mut = type
169
170
while current? /\ !current.definition.is_object do
171
for method in SRM_MEMBER_ORDER.methods(SRM_EMITTED_TYPE(current.definition)) do
172
if method.name =~ "to_string" /\ method.is_instance /\ method.arguments.count == 0 then
173
return false
174
fi
175
od
176
177
current = _target.superclass_of(current)
178
od
179
180
return true
181
si
182
183
/// The public instance properties of `type` and of its ancestors,
184
/// each with the getter that reads it at the class's type arguments,
185
/// and then their public instance fields, as .NET lists them.
186
members(type: CLASS_INSTANCE) -> Collections.List[(name: string, getter: FUNCTION_INSTANCE?, `field: Symbol?, type: Type)] is
187
let result = LIST[(name: string, getter: FUNCTION_INSTANCE?, `field: Symbol?, type: Type)]()
188
189
let current: CLASS_INSTANCE? mut = type
190
191
while current? /\ !current.definition.is_object do
192
for property in SRM_MEMBER_ORDER.properties(SRM_EMITTED_TYPE(current.definition)) do
193
if let getter = property.read_function /\ property.is_instance /\ _is_shown(property.name) then
194
if let member_type = current.specialize(property.type) then
195
result.add((name = property.name, getter = FUNCTION_INSTANCE(getter, current.type_map), `field = null, type = member_type))
196
fi
197
fi
198
od
199
200
current = _target.superclass_of(current)
201
od
202
203
current = type
204
205
while current? /\ !current.definition.is_object do
206
for `field in SRM_MEMBER_ORDER.fields(SRM_EMITTED_TYPE(current.definition)) do
207
if `field.is_instance /\ _is_shown(`field.name) then
208
if let member_type = current.specialize(`field.type) then
209
result.add((name = `field.name, getter = null, `field = `field, type = member_type))
210
fi
211
fi
212
od
213
214
current = _target.superclass_of(current)
215
od
216
217
return result
218
si
219
220
// A member a program names: public, and not one the compiler made
221
// up, which carries a `$`.
222
_is_shown(name: string) -> bool =>
223
name.length > 0 /\ !name.starts_with('_') /\ !name.contains('$')
224
225
/// The type as a program names it: a variant after its union's
226
/// name, and the type arguments after a generic's.
227
display_name(type: CLASS_INSTANCE) -> string is
228
let name mut = type.definition.name
229
230
if type.definition.is_variant then
231
if let owner = _target.superclass_of(type) then
232
name = "{owner.definition.name}.{name}"
233
fi
234
fi
235
236
if type.arguments.count > 0 then
237
name = "{name}[{type.arguments |> Ghul.Pipes.map(a => a.short_description) |> Ghul.Pipes.join(", ")}]"
238
fi
239
240
return name
241
si
242
si
243
244
/// The display state's methods a displayer calls, each at the type
245
/// arguments a call gives it.
246
class DISPLAY_HELPERS is
247
state: CLASS_INSTANCE
248
begin_members: FUNCTION_INSTANCE
249
end_members: FUNCTION_INSTANCE
250
begin_parts: FUNCTION_INSTANCE
251
end_parts: FUNCTION_INSTANCE
252
253
_member: Function
254
_part: Function
255
_elements: Function
256
257
init(
258
state: CLASS_INSTANCE,
259
begin_members: Function,
260
member: Function,
261
end_members: Function,
262
begin_parts: Function,
263
part: Function,
264
end_parts: Function,
265
elements: Function
266
) is
267
self.state = state
268
self.begin_members = FUNCTION_INSTANCE(begin_members, state.type_map)
269
self.end_members = FUNCTION_INSTANCE(end_members, state.type_map)
270
self.begin_parts = FUNCTION_INSTANCE(begin_parts, state.type_map)
271
self.end_parts = FUNCTION_INSTANCE(end_parts, state.type_map)
272
273
_member = member
274
_part = part
275
_elements = elements
276
si
277
278
/// The helpers `state` declares, or null where it lacks any of
279
/// them.
280
of(state: CLASS_INSTANCE) -> DISPLAY_HELPERS? static is
281
let methods = SRM_MEMBER_ORDER.methods(SRM_EMITTED_TYPE(state.definition))
282
283
let begin_members = _named(methods, "_begin_members")
284
let member = _named(methods, "_member")
285
let end_members = _named(methods, "_end_members")
286
let begin_parts = _named(methods, "_begin_parts")
287
let part = _named(methods, "_part")
288
let end_parts = _named(methods, "_end_parts")
289
let elements = _named(methods, "_elements")
290
291
if begin_members? /\ member? /\ end_members? /\ begin_parts? /\ part? /\ end_parts? /\ elements? then
292
return DISPLAY_HELPERS(state, begin_members, member, end_members, begin_parts, part, end_parts, elements)
293
fi
294
295
return null
296
si
297
298
/// The state's member method, at a member's type.
299
member(type: Type) -> FUNCTION_INSTANCE => _at(_member, type)
300
301
/// The state's part method, at a tuple element's type.
302
part(type: Type) -> FUNCTION_INSTANCE => _at(_part, type)
303
304
/// The state's elements method, at a sequence's element type.
305
elements(type: Type) -> FUNCTION_INSTANCE => _at(_elements, type)
306
307
_at(method: Function, type: Type) -> FUNCTION_INSTANCE is
308
let map = MAP[Symbol, Type]()
309
310
map[method.generic_arguments[0].symbol] = type
311
312
return FUNCTION_INSTANCE(method, map)
313
si
314
315
_named(methods: Collections.Iterable[Function], name: string) -> Function? static is
316
for method in methods do
317
if method.name =~ name then
318
return method
319
fi
320
od
321
322
return null
323
si
324
si
325
si