Skip to content
← Back

src/semantic/lookups/reflection_innate_symbol_lookup.ghul

1
@@IF.dotnet()
2
3
namespace Semantic.Lookups is
4
use IO.Std
5
use TYPE = System.Type
6
use System.ValueType
7
use System.Exception
8
9
use Logging
10
11
use Collections.List
12
use Collections.LIST
13
14
use Ghul.Pipes
15
16
use Source.LOCATION
17
18
use Types
19
use ARRAY_TYPE = Types.ARRAY
20
21
class REFLECTION_INNATE_SYMBOL_LOOKUP(
22
_type_mapper: DotNet.TYPE_MAPPER,
23
_symbol_table: DotNet.SYMBOL_TABLE,
24
_ghul_innate_type_lookup: GHUL_INNATE_TYPE_LOOKUP,
25
_type_source: DotNet.TypeSource
26
): InnateSymbolLookup is
27
_tuple_types: List[TYPE]
28
_function_types: List[TYPE]
29
_action_types: List[TYPE]
30
_resource_sources: RESOURCE_SOURCES
31
32
init(..) is
33
_tuple_types = _type_source.get_types((1::7) |> map(i => "System.ValueTuple`{i}"))
34
_function_types = _type_source.get_types((1::17) |> map(i => "System.Func`{i}"))
35
_action_types = _type_source.get_types((1::16) |> map(i => "System.Action`{i}"))
36
_resource_sources = RESOURCE_SOURCES(_type_mapper, _type_source)
37
si
38
39
get_tuple_type(types: Collections.List[Type], names: Collections.List[string?]?) -> Type =>
40
DotNet.HYBRID_TUPLE_TYPE_WRAPPER(_symbol_table, _tuple_types[types.count - 1], types, names)
41
42
get_function_type(types: Collections.List[Type]) -> Type is
43
assert types.count > 0 /\ types.count <= _function_types.count
44
45
let return_type = types[types.count - 1]
46
47
if return_type.is_sentinel \/ return_type.is_type_variable \/ !return_type.matches(get_void_type()) then
48
return
49
DotNet.HYBRID_FUNCTION_TYPE_WRAPPER(_symbol_table, _function_types[types.count - 1], types)
50
elif types.count == 1 then
51
return
52
_type_mapper.get_type(_type_source.get_type("System.Action"))
53
else
54
let without_void_return_type = LIST[Type](types.count - 1)
55
56
for i in 0..types.count - 1 do
57
without_void_return_type.add(types[i])
58
od
59
60
return
61
DotNet.HYBRID_ACTION_TYPE_WRAPPER(_symbol_table, _action_types[types.count - 2], without_void_return_type)
62
fi
63
si
64
65
get_function_type(types: Collections.List[Type], is_pure: bool) -> Type is
66
if !is_pure then
67
return get_function_type(types)
68
fi
69
70
assert types.count > 0 /\ types.count <= _function_types.count
71
72
let return_type = types[types.count - 1]
73
74
if return_type.is_sentinel \/ return_type.is_type_variable \/ !return_type.matches(get_void_type()) then
75
return
76
DotNet.PURE_HYBRID_FUNCTION_TYPE_WRAPPER(_symbol_table, _function_types[types.count - 1], types)
77
elif types.count == 1 then
78
// the zero-argument void form maps to the non-generic
79
// System.Action, which has no pure-marked wrapper —
80
// dropping the marker is conservative: callers get no
81
// purity guarantee and callees keep no facts
82
return
83
_type_mapper.get_type(_type_source.get_type("System.Action"))
84
else
85
let without_void_return_type = LIST[Type](types.count - 1)
86
87
for i in 0..types.count - 1 do
88
without_void_return_type.add(types[i])
89
od
90
91
return
92
DotNet.PURE_HYBRID_ACTION_TYPE_WRAPPER(_symbol_table, _action_types[types.count - 2], without_void_return_type)
93
fi
94
si
95
96
get_enum_type() -> Type =>
97
_type_mapper.get_type(_type_source.get_type("System.Enum"))
98
99
get_array_type(type: Type) -> Type =>
100
_ghul_innate_type_lookup.get_array_type(type)
101
102
get_optional_type(type: Type) -> Type =>
103
let classy =
104
cast Semantic.Symbols.Classy?(
105
_symbol_table.get_symbol(_type_source.get_type("System.Nullable`1")))!
106
in
107
Types.NULLABLE(LOCATION.internal, classy, LIST[Type]([type]))
108
109
get_maybe_type(type: Type) -> Type? is
110
// Mirrors the MAYBE_TYPE_CREATOR registration in
111
// TYPE_MAPPER.start(): the assembly is absent when the
112
// compiler is building ghul-runtime itself, so the
113
// lookup throws. Treat that as "MAYBE unavailable" and
114
// let the caller surface an error appropriate for the
115
// context.
116
try
117
let dotnet_type = _type_source.get_type("ghul-runtime", "Ghul.MAYBE")
118
let classy = cast Semantic.Symbols.Classy?(_symbol_table.get_symbol(dotnet_type))!
119
return Types.MAYBE(LOCATION.internal, classy, LIST[Type]([type]))
120
catch ex: System.Exception
121
return null
122
yrt
123
si
124
125
get_equality_comparer_type(type: Type) -> Type? is
126
try
127
let dotnet_type =
128
_type_source.get_type("System.Collections", "System.Collections.Generic.EqualityComparer`1")
129
130
let classy = cast Semantic.Symbols.Classy?(_symbol_table.get_symbol(dotnet_type))!
131
132
return Types.GENERIC(LOCATION.internal, classy, LIST[Type]([type]))
133
catch ex: System.Exception
134
return null
135
yrt
136
si
137
138
get_list_type(type: Type) -> Type? is
139
try
140
let dotnet_type =
141
_type_source.get_type("System.Collections", "System.Collections.Generic.List`1")
142
143
let classy = cast Semantic.Symbols.Classy?(_symbol_table.get_symbol(dotnet_type))!
144
145
return Types.GENERIC(LOCATION.internal, classy, LIST[Type]([type]))
146
catch ex: System.Exception
147
return null
148
yrt
149
si
150
151
get_pointer_type(type: Type) -> Type =>
152
_ghul_innate_type_lookup.get_pointer_type(type)
153
154
get_reference_type(type: Type) -> Type =>
155
_ghul_innate_type_lookup.get_reference_type(type)
156
157
get_bool_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Boolean"))
158
get_char_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Char"))
159
get_byte_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.SByte"))
160
get_ubyte_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Byte"))
161
get_short_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Int16"))
162
get_ushort_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.UInt16"))
163
get_int_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Int32"))
164
get_uint_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.UInt32"))
165
get_long_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Int64"))
166
get_ulong_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.UInt64"))
167
get_word_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.IntPtr"))
168
get_uword_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.UIntPtr"))
169
get_single_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Single"))
170
get_double_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Double"))
171
get_decimal_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Decimal"))
172
get_bigint_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Runtime.Numerics", "System.Numerics.BigInteger"))
173
get_void_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Void"))
174
get_object_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Object"))
175
get_value_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.ValueType"))
176
get_string_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.String"))
177
get_exception_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Exception"))
178
find_exception_type() -> Type? => get_exception_type()
179
get_invalid_cast_exception_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.InvalidCastException"))
180
get_overflow_exception_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.OverflowException"))
181
get_type_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Type"))
182
find_type_type() -> Type? => get_type_type()
183
find_environment_type() -> Type? => _type_mapper.get_type(_type_source.get_type("ghul-runtime", "Ghul.ENVIRONMENT"))
184
185
// .NET declares the failures in System, and the runtime its own.
186
find_runtime_exception_type(kind: RuntimeException) -> Type? =>
187
if kind == RuntimeException.ASSERT_FAILED then
188
_type_mapper.get_type(_type_source.get_type("ghul-runtime", "Ghul.AssertFailedException"))
189
else
190
_type_mapper.get_type(_type_source.get_type("System.{RUNTIME_EXCEPTIONS.name_of(kind)}"))
191
fi
192
get_unspecialized_iterable_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Collections.Generic.IEnumerable`1"))
193
194
get_unspecialized_list_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Collections.Generic.IReadOnlyList`1"))
195
get_unspecialized_iterator_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Collections.Generic.IEnumerator`1"))
196
get_unspecialized_mutable_list_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Collections", "System.Collections.Generic.List`1"))
197
// ghūl emits its own generic types without the .NET `N
198
// arity suffix that BCL types carry, so the reflected name is
199
// bare `Ghul.Pipes.Pipe` (an arity-1 generic definition).
200
//
201
// The assembly is absent when the compiler is building
202
// ghul-runtime itself, so the lookup throws. Treat that as "Pipe
203
// unavailable" and return null (mirrors get_maybe_type): pipe
204
// fusion and the other Pipe-typed queries then simply do not
205
// apply while compiling the pipe library.
206
get_unspecialized_pipe_type() -> Type? is
207
try
208
return _type_mapper.get_type(_type_source.get_type("ghul-runtime", "Ghul.Pipes.Pipe"))
209
catch ex: System.Exception
210
return null
211
yrt
212
si
213
get_unspecialized_task_type() -> Type? => _type_mapper.get_type(_type_source.get_type("System.Threading.Tasks.Task`1"))
214
215
get_interpolated_string_handler_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Runtime.CompilerServices.DefaultInterpolatedStringHandler"))
216
217
get_idisposable_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.IDisposable"))
218
219
get_multicast_delegate_type() -> Type? => _type_mapper.get_type(_type_source.get_type("System.MulticastDelegate"))
220
221
get_slice_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _ghul_innate_type_lookup.slice_functions
222
223
get_join_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _ghul_innate_type_lookup.join_functions
224
225
get_display_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _ghul_innate_type_lookup.display_functions
226
get_enum_text_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _ghul_innate_type_lookup.enum_text_functions
227
get_formattable_symbol() -> Semantic.Symbols.Symbol? => _ghul_innate_type_lookup.formattable_symbol
228
229
get_render_elements_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _ghul_innate_type_lookup.render_elements_functions
230
231
get_pipes_fusion_functions() -> Collections.MAP[Semantic.Symbols.Symbol, string] => _ghul_innate_type_lookup.fusion_functions
232
233
get_pipes_unbounded_sources() -> Collections.SET[Semantic.Symbols.Symbol] => _ghul_innate_type_lookup.unbounded_sources
234
235
is_pipes_function(function: Semantic.Symbols.Function) -> bool => _ghul_innate_type_lookup.is_pipes_function(function)
236
237
stops_early(function: Semantic.Symbols.Function) -> bool =>
238
_source_read(function) =~ true
239
240
reads_whole_source(function: Semantic.Symbols.Function) -> bool =>
241
_source_read(function) =~ false
242
243
_source_read(function: Semantic.Symbols.Function) -> bool? is
244
let stops_early: bool mut
245
246
if _ghul_innate_type_lookup.source_reads.try_get_value(function.root_specialized_from, stops_early ref) then
247
return stops_early
248
fi
249
250
return null
251
si
252
253
holds_resource(function: Semantic.Symbols.Function) -> bool => _resource_sources.holds_resource(function)
254
255
get_range_type() -> Type => _type_mapper.get_type(_type_source.get_type("System.Range"))
256
257
get_int_range_type() -> Type =>
258
_type_mapper.get_type(_type_source.get_type("ghul-runtime", "Ghul.INT_RANGE"))
259
260
get_int_range_inclusive_type() -> Type =>
261
_type_mapper.get_type(_type_source.get_type("ghul-runtime", "Ghul.INT_RANGE_INCLUSIVE"))
262
263
get_int_range_types() -> (exclusive: Type, inclusive: Type)? is
264
try
265
return (get_int_range_type(), get_int_range_inclusive_type())
266
catch ex: System.Exception
267
return null
268
yrt
269
si
270
271
get_box_type(type: Type) -> Type =>
272
// `Ghul.BOX[T]` lives in the ghul-runtime assembly,
273
// not System.Runtime (the default for the single-arg
274
// overload), so name the assembly explicitly. The
275
// assembly's name is derived from its file name —
276
// see `assemblies.ghul`. Note: ghūl-emitted generic
277
// types drop the .NET-style backtick-arity suffix
278
// (per the arity-overloading change), so the .NET
279
// metadata name is plain `Ghul.BOX`, not
280
// `Ghul.BOX`1`.
281
let classy =
282
cast Semantic.Symbols.Classy?(
283
_symbol_table.get_symbol(_type_source.get_type("ghul-runtime", "Ghul.BOX")))!
284
in
285
Types.GENERIC(LOCATION.internal, classy, LIST[Type]([type]))
286
287
get_task_type(type: Type) -> Type? is
288
// Constructed `System.Threading.Tasks.Task[type]` —
289
// reuses the same unspecialized Task symbol identity
290
// that `try_get_task_element_type` (in compile_bindings)
291
// checks against, so a freshly-constructed Task[T] from
292
// here will be recognised as already-a-Task and not
293
// double-wrapped on subsequent return-position passes.
294
let unspec = get_unspecialized_task_type()
295
296
if !unspec? then
297
return null
298
fi
299
300
let classy = cast Semantic.Symbols.Classy?(unspec.symbol.unspecialized_symbol)
301
302
if !classy? then
303
return null
304
fi
305
306
return Types.GENERIC(LOCATION.internal, classy, LIST[Type]([type]))
307
si
308
309
get_void_task_type() -> Type? =>
310
// Non-generic `System.Threading.Tasks.Task` — the
311
// type-name map at type_name_map.ghul:174 already
312
// aliases this to ghūl `Tasks.TASK` (the arity-zero
313
// form alongside the arity-one Task`1).
314
_type_mapper.get_type(_type_source.get_type("System.Threading.Tasks.Task"))
315
316
get_async_task_method_builder_type(type: Type) -> Type? is
317
// `System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1`
318
// constructed over the value-async result type.
319
let unspec = _type_mapper.get_type(_type_source.get_type("System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1"))
320
321
let classy = cast Semantic.Symbols.Classy?(unspec.symbol.unspecialized_symbol)
322
323
if !classy? then
324
return null
325
fi
326
327
return Types.GENERIC(LOCATION.internal, classy, LIST[Type]([type]))
328
si
329
330
get_async_task_method_builder_void_type() -> Type? =>
331
_type_mapper.get_type(_type_source.get_type("System.Runtime.CompilerServices.AsyncTaskMethodBuilder"))
332
333
get_async_state_machine_interface_type() -> Type? =>
334
_type_mapper.get_type(_type_source.get_type("System.Runtime.CompilerServices.IAsyncStateMachine"))
335
336
get_notify_completion_type() -> Type? =>
337
_type_mapper.get_type(_type_source.get_type("System.Runtime.CompilerServices.INotifyCompletion"))
338
339
get_critical_notify_completion_type() -> Type? =>
340
_type_mapper.get_type(_type_source.get_type("System.Runtime.CompilerServices.ICriticalNotifyCompletion"))
341
si
342
si