Skip to content
← Back

src/semantic/dotnet/type_wrapper.ghul

1
@@IF.dotnet()
2
3
namespace Semantic.DotNet is
4
use IO.Std
5
6
use TYPE = System.Type
7
use Collections.LIST
8
use Collections.List
9
10
use Source.LOCATION
11
12
use Types.Type
13
14
class TYPE_WRAPPER: Types.NAMED is
15
_symbol_table: SYMBOL_TABLE
16
_dotnet_type: TYPE
17
18
symbol: Symbols.Symbol is
19
// _symbol is lazily materialized on first access
20
@suppress("presence-test-non-optional")
21
if !_symbol? then
22
materialize()
23
fi
24
25
return _symbol
26
si
27
28
init(
29
symbol_table: SYMBOL_TABLE,
30
dotnet_type: TYPE
31
) is
32
// No symbol yet - materialize() supplies it on first
33
// access, as in the generic wrappers below.
34
_symbol_table = symbol_table
35
_dotnet_type = dotnet_type
36
si
37
38
materialize() is
39
_symbol = _symbol_table.get_symbol(_dotnet_type)!
40
si
41
si
42
43
class GENERIC_TYPE_WRAPPER: Types.GENERIC is
44
_symbol_table: SYMBOL_TABLE
45
_type_mapper: TYPE_MAPPER
46
_dotnet_type: TYPE
47
48
symbol: Symbols.Symbol is
49
// _symbol is lazily materialized on first access
50
@suppress("presence-test-non-optional")
51
if !_symbol? then
52
materialize()
53
fi
54
55
return _symbol
56
si
57
58
init(
59
symbol_table: SYMBOL_TABLE,
60
type_mapper: TYPE_MAPPER,
61
dotnet_type: TYPE
62
) is
63
_symbol_table = symbol_table
64
_type_mapper = type_mapper
65
_dotnet_type = dotnet_type
66
si
67
68
materialize() is
69
let s = _symbol_table.get_symbol(_dotnet_type.get_generic_type_definition())
70
71
let unspecialized = cast Symbols.Classy?(s)!
72
73
let arguments = LIST()
74
75
for a in _dotnet_type.get_generic_arguments() do
76
arguments.add(_type_mapper.get_type(a))
77
od
78
79
_symbol = create_symbol(unspecialized, arguments)
80
si
81
82
create_symbol(unspecialized: Symbols.Classy, arguments: List[Types.Type]) -> Symbols.GENERIC =>
83
Symbols.GENERIC(Source.LOCATION.internal, unspecialized, arguments)
84
si
85
86
// A reflected `System.Nullable[T]` — the same value-type optional
87
// a ghūl-written `T?` produces (Types.NULLABLE), so it must behave
88
// identically: optional, widening from T, and rendered with the
89
// `?` sugar. Mirrors FUNCTION_TYPE_WRAPPER / TUPLE_TYPE_WRAPPER,
90
// which likewise re-declare their behaviour and delegate to the
91
// eager type's statics / constructor.
92
class NULLABLE_TYPE_WRAPPER: GENERIC_TYPE_WRAPPER is
93
is_optional: bool => true
94
95
optional_inner_type: Type? => arguments[0]
96
97
init(
98
symbol_table: SYMBOL_TABLE,
99
type_mapper: TYPE_MAPPER,
100
dotnet_type: TYPE
101
) is
102
super.init(symbol_table, type_mapper, dotnet_type)
103
si
104
105
compare(other: Type) -> Types.MATCH =>
106
Types.NULLABLE.compare_optional(self, other, super.compare(other))
107
108
create(
109
location: LOCATION,
110
symbol: Symbols.Classy,
111
arguments: Collections.List[Type]
112
) -> Types.GENERIC =>
113
Types.NULLABLE(location, symbol, arguments)
114
115
short_description: string => Types.NULLABLE.get_short_description(self)
116
117
to_string() -> string => "{arguments[0]}?"
118
si
119
120
// A reflected `Ghul.MAYBE[T]` — the runtime's unconstrained-T
121
// optional carrier. The Type-system flag is_maybe drives the
122
// implicit conversion to `T?` at slot boundaries; surfacing it as
123
// a wrapper subclass means recognition is `isa MAYBE_TYPE_WRAPPER`
124
// (no per-call symbol lookup, no behaviour on source-side MAYBE
125
// structs being newly compiled inside `ghul-runtime` itself).
126
class MAYBE_TYPE_WRAPPER: GENERIC_TYPE_WRAPPER is
127
is_maybe: bool => true
128
129
is_optional: bool => true
130
131
optional_inner_type: Type? => arguments[0]
132
133
init(
134
symbol_table: SYMBOL_TABLE,
135
type_mapper: TYPE_MAPPER,
136
dotnet_type: TYPE
137
) is
138
super.init(symbol_table, type_mapper, dotnet_type)
139
si
140
141
// Specialisation (`MAYBE[T]` → `MAYBE[CAT]`) hands off to the
142
// eager subclass so the is_maybe flag survives every rewrite —
143
// mirrors NULLABLE_TYPE_WRAPPER's hand-off to Types.NULLABLE.
144
create(
145
location: LOCATION,
146
symbol: Symbols.Classy,
147
arguments: Collections.List[Types.Type]
148
) -> Types.GENERIC =>
149
Types.MAYBE(location, symbol, arguments)
150
151
short_description: string => Types.MAYBE.get_short_description(self)
152
153
to_string() -> string => "{arguments[0]}?"
154
si
155
156
class HYBRID_GENERIC_TYPE_WRAPPER: Types.GENERIC is
157
_symbol_table: SYMBOL_TABLE
158
_dotnet_type: TYPE
159
_arguments: List[Type]
160
161
// _symbol: Symbols.GENERIC;
162
163
symbol: Symbols.Symbol is
164
// _symbol is lazily materialized on first access
165
@suppress("presence-test-non-optional")
166
if !_symbol? then
167
materialize()
168
fi
169
170
return _symbol
171
si
172
173
init(
174
symbol_table: SYMBOL_TABLE,
175
dotnet_type: TYPE,
176
arguments: List[Type]
177
) is
178
assert arguments.count > 0 else "expected at least one type argument"
179
180
_symbol_table = symbol_table
181
_dotnet_type = dotnet_type
182
_arguments = arguments
183
si
184
185
init(
186
symbol_table: SYMBOL_TABLE,
187
dotnet_type: TYPE,
188
argument: Type
189
) is
190
_symbol_table = symbol_table
191
_dotnet_type = dotnet_type
192
_arguments = LIST[Type]([argument])
193
si
194
195
materialize() is
196
let s = _symbol_table.get_symbol(_dotnet_type)
197
198
let unspecialized = cast Symbols.Classy?(s)!
199
200
_symbol = create_symbol(unspecialized, _arguments)
201
si
202
203
create_symbol(unspecialized: Symbols.Classy, arguments: List[Types.Type]) -> Symbols.GENERIC =>
204
Symbols.GENERIC(Source.LOCATION.internal, unspecialized, arguments)
205
si
206
207
class HYBRID_ARRAY_TYPE_WRAPPER: HYBRID_GENERIC_TYPE_WRAPPER is
208
short_description: string => Types.ARRAY.get_short_description(self)
209
210
init(
211
symbol_table: SYMBOL_TABLE,
212
dotnet_type: TYPE,
213
argument: Type
214
) is
215
super.init(
216
symbol_table,
217
dotnet_type,
218
argument
219
)
220
si
221
222
create(location: LOCATION, symbol: Symbols.Classy, arguments: Collections.List[Type]) -> Types.GENERIC =>
223
Types.ARRAY(location, symbol, arguments)
224
225
to_string() -> string => Types.ARRAY.get_short_description(self)
226
si
227
228
class HYBRID_REFERENCE_TYPE_WRAPPER: HYBRID_GENERIC_TYPE_WRAPPER is
229
short_description: string => Types.REFERENCE.get_short_description(self)
230
231
init(
232
symbol_table: SYMBOL_TABLE,
233
dotnet_type: TYPE,
234
argument: Type
235
) is
236
super.init(
237
symbol_table,
238
dotnet_type,
239
argument
240
)
241
si
242
243
create(location: LOCATION, symbol: Symbols.Classy, arguments: Collections.List[Type]) -> Types.GENERIC =>
244
Types.REFERENCE(location, symbol, arguments)
245
246
to_string() -> string => Types.REFERENCE.get_short_description(self)
247
si
248
249
class HYBRID_POINTER_TYPE_WRAPPER: HYBRID_GENERIC_TYPE_WRAPPER is
250
short_description: string => Types.POINTER.get_short_description(self)
251
252
init(
253
symbol_table: SYMBOL_TABLE,
254
dotnet_type: TYPE,
255
argument: Type
256
) is
257
super.init(
258
symbol_table,
259
dotnet_type,
260
argument
261
)
262
si
263
264
create(location: LOCATION, symbol: Symbols.Classy, arguments: Collections.List[Type]) -> Types.GENERIC =>
265
Types.POINTER(location, symbol, arguments)
266
267
to_string() -> string => Types.POINTER.get_short_description(self)
268
si
269
si