Skip to content
← Back

src/ir/emitter/srm_flags.ghul

1
@@IF.dotnet()
2
3
namespace IR.Emitter is
4
use System.Reflection.TypeAttributes
5
use System.Reflection.MethodAttributes
6
use System.Reflection.FieldAttributes
7
8
use Semantic.Symbols.Classy
9
use Semantic.Symbols.Function
10
use Semantic.Symbols.Field
11
12
// The metadata flag words for a definition, derived from the
13
// symbol's own predicates. Kept together here, and out of the
14
// symbol classes, so what a symbol means and how it is encoded
15
// stay separable.
16
//
17
// Instance methods are emitted virtual without newslot, so a
18
// same-named member in a subclass overrides by name and signature,
19
// which is the dispatch behaviour ghūl source already relies on.
20
class SRM_FLAGS is
21
_PUBLIC_TYPE: int static => 1
22
_INTERFACE: int static => 32
23
_ABSTRACT_TYPE: int static => 128
24
_SEALED: int static => 256
25
_SEQUENTIAL_LAYOUT: int static => 8
26
_EXPLICIT_LAYOUT: int static => 16
27
_LAYOUT_MASK: int static => 24
28
_UNICODE_CLASS: int static => 65536
29
_AUTO_CLASS: int static => 131072
30
_BEFORE_FIELD_INIT: int static => 1048576
31
32
type_attributes(type: Classy) -> TypeAttributes static is
33
let flags mut = _PUBLIC_TYPE
34
35
// A closure frame or a state machine is reached only by the
36
// code the compiler generated alongside it, and no source
37
// can name it. Emitted public it sits in every other
38
// language's view of the assembly and in anything that
39
// enumerates the public surface. The globals carrier is not
40
// one of these: it takes its flags from
41
// globals_carrier_type_attributes, since a consumer's call
42
// to a global function goes through it.
43
if !type.is_public_readable \/ type.is_internal then
44
flags = 0
45
fi
46
47
if type.is_trait then
48
return cast TypeAttributes(flags | _INTERFACE | _ABSTRACT_TYPE)
49
fi
50
51
// An enum is sealed like any other value type, but takes
52
// auto layout: the runtime treats it as its underlying
53
// scalar, and a sequential-layout enum is rejected as
54
// malformed rather than merely laid out differently.
55
if isa Semantic.Symbols.ENUM_STRUCT(type) then
56
flags = flags | _SEALED
57
elif type.is_value_type then
58
flags = flags | _SEALED | _SEQUENTIAL_LAYOUT
59
elif type.is_abstract then
60
flags = flags | _ABSTRACT_TYPE
61
fi
62
63
if let layout = type.layout then
64
flags = _with_layout(flags, layout)
65
fi
66
67
// A type declaring its own static constructor must not
68
// carry beforefieldinit, which lets the CLR run the
69
// `.cctor` at an unspecified point before first use rather
70
// than precisely before it.
71
if type.has_static_constructor then
72
return cast TypeAttributes(flags)
73
fi
74
75
return cast TypeAttributes(flags | _BEFORE_FIELD_INIT)
76
si
77
78
// The layout and string-format bits a `StructLayout` sets, in place
79
// of the ones the type would otherwise take.
80
_with_layout(flags: int, layout: Semantic.TYPE_LAYOUT) -> int static is
81
let result mut = flags & \_LAYOUT_MASK
82
83
case layout.kind
84
when System.Runtime.InteropServices.LayoutKind.SEQUENTIAL then
85
result = result | _SEQUENTIAL_LAYOUT
86
when System.Runtime.InteropServices.LayoutKind.EXPLICIT then
87
result = result | _EXPLICIT_LAYOUT
88
else
89
esac
90
91
case layout.char_set
92
when 3 then
93
result = result | _UNICODE_CLASS
94
when 4 then
95
result = result | _AUTO_CLASS
96
else
97
esac
98
99
return result
100
si
101
102
globals_carrier_type_attributes() -> TypeAttributes static =>
103
cast TypeAttributes(_PUBLIC_TYPE | _ABSTRACT_TYPE | _SEALED)
104
105
_PRIVATE_METHOD: int static => 1
106
_ASSEMBLY_METHOD: int static => 3
107
_PUBLIC_METHOD: int static => 6
108
_STATIC: int static => 16
109
_FINAL: int static => 32
110
_VIRTUAL: int static => 64
111
_HIDE_BY_SIG: int static => 128
112
_NEW_SLOT: int static => 256
113
_ABSTRACT_METHOD: int static => 1024
114
_SPECIAL_NAME: int static => 2048
115
_RT_SPECIAL_NAME: int static => 4096
116
117
_PINVOKE_IMPL: int static => 0x2000
118
119
method_attributes(method: Function) -> MethodAttributes static is
120
// A `.cctor` is reached by the runtime rather than by any
121
// call site, and only the rtspecialname mark tells it so.
122
// Without it the row is an ordinary static method that
123
// happens to be named `.cctor`, and nothing ever runs it.
124
if method.is_static_constructor then
125
return static_initializer_attributes()
126
fi
127
128
let flags mut =
129
if method.is_public_readable then _PUBLIC_METHOD else _ASSEMBLY_METHOD fi
130
131
flags = flags | _HIDE_BY_SIG
132
133
// A constructor is never virtual: it is reached through the
134
// type it constructs, so there is no slot for a subclass to
135
// take over, and the runtime rejects the combination.
136
if !method.is_emitted_with_receiver then
137
flags = flags | _STATIC
138
elif method.is_virtual /\ !method.is_constructor then
139
flags = flags | _VIRTUAL
140
fi
141
142
// A constructor's IL name is reserved, which is what
143
// rtspecialname asserts; the runtime rejects a `.ctor`
144
// without it.
145
if method.is_constructor then
146
flags = flags | _SPECIAL_NAME | _RT_SPECIAL_NAME
147
elif method.is_internal then
148
flags = flags | _SPECIAL_NAME
149
fi
150
151
// A body-less declaration — an interface member, or an
152
// abstract method — has no body for the row to point at.
153
if method.is_abstract then
154
flags = flags | _ABSTRACT_METHOD | _VIRTUAL
155
fi
156
157
// A method that calls into a shared library: the row points
158
// at no body and the runtime builds the call from the
159
// ImplMap row that names the module and the entry point.
160
if method.pinvoke? then
161
flags = flags | _PINVOKE_IMPL
162
fi
163
164
return cast MethodAttributes(flags)
165
si
166
167
// A state-machine frame's synthesised members. Each satisfies an
168
// interface the frame implements, so each is a newslot virtual;
169
// ToString overrides Object's and takes the slot it already has.
170
frame_member_attributes(member: FrameMember) -> MethodAttributes static is
171
if member == FrameMember.TO_STRING then
172
return cast MethodAttributes(_PUBLIC_METHOD | _VIRTUAL | _HIDE_BY_SIG)
173
fi
174
175
let flags mut =
176
_PUBLIC_METHOD | _FINAL | _VIRTUAL | _HIDE_BY_SIG | _NEW_SLOT
177
178
if member == FrameMember.GET_CURRENT \/ member == FrameMember.GET_ENUMERATOR then
179
flags = flags | _SPECIAL_NAME
180
fi
181
182
return cast MethodAttributes(flags)
183
si
184
185
// An explicit interface implementation: private, because it is
186
// reached only through the interface, and newslot because it
187
// takes a slot of its own rather than overriding anything the
188
// base declares. Final, since a name carrying dots cannot be
189
// overridden by a subclass member anyway.
190
explicit_implementation_attributes(is_special_name: bool) -> MethodAttributes static is
191
let flags mut =
192
_PRIVATE_METHOD | _FINAL | _VIRTUAL | _HIDE_BY_SIG | _NEW_SLOT
193
194
if is_special_name then
195
flags = flags | _SPECIAL_NAME
196
fi
197
198
return cast MethodAttributes(flags)
199
si
200
201
// A method taking a slot of its own rather than the one its name
202
// and signature would find.
203
with_new_slot(attributes: MethodAttributes) -> MethodAttributes static =>
204
cast MethodAttributes(cast int(attributes) | _NEW_SLOT)
205
206
// A static constructor: private, static, and reserved-named.
207
// Shared by a declared `init() static` and by the one the back
208
// end synthesises to intern a unit variant.
209
static_initializer_attributes() -> MethodAttributes static =>
210
cast MethodAttributes(
211
_PRIVATE_METHOD | _HIDE_BY_SIG | _STATIC | _SPECIAL_NAME | _RT_SPECIAL_NAME)
212
213
// The name a method has in metadata: the one it was given, or
214
// the CLR's name for a constructor.
215
method_name(method: Function) -> string static =>
216
if let name = method.il_name_override then
217
name
218
elif method.is_static_constructor then
219
".cctor"
220
elif method.is_constructor then
221
".ctor"
222
else
223
method.name
224
fi
225
226
property_name(property: Semantic.Symbols.Property) -> string static =>
227
property.il_name
228
229
_COVARIANT: int static => 1
230
_CONTRAVARIANT: int static => 2
231
_REFERENCE_TYPE_CONSTRAINT: int static => 4
232
_VALUE_TYPE_CONSTRAINT: int static => 8
233
_DEFAULT_CONSTRUCTOR_CONSTRAINT: int static => 16
234
235
// What a GenericParam row says about one type parameter beyond
236
// its name: which way it varies, which kind of type can
237
// instantiate it, and whether it must be constructible.
238
//
239
// All three are read back by an importing compiler and enforced
240
// by the CLR, so a row left at zero silently turns a covariant
241
// trait invariant and drops a `class`, `struct` or `init` bound.
242
// A bound naming a type is a GenericParamConstraint row instead,
243
// and cannot be expressed here.
244
type_parameter_attributes(
245
variance: Semantic.Types.TypeVariance,
246
constraint: Semantic.Symbols.TypeParameterConstraintKind,
247
has_constructor_constraint: bool
248
) -> System.Reflection.GenericParameterAttributes static is
249
let flags mut = 0
250
251
if has_constructor_constraint then
252
flags = flags | _DEFAULT_CONSTRUCTOR_CONSTRAINT
253
fi
254
255
if variance == Semantic.Types.TypeVariance.COVARIANT then
256
flags = flags | _COVARIANT
257
elif variance == Semantic.Types.TypeVariance.CONTRAVARIANT then
258
flags = flags | _CONTRAVARIANT
259
fi
260
261
// OPTIONAL constrains what the parameter can hold rather
262
// than which kind of type it is, and has no CLR counterpart.
263
if constraint == Semantic.Symbols.TypeParameterConstraintKind.REFERENCE then
264
flags = flags | _REFERENCE_TYPE_CONSTRAINT
265
elif constraint == Semantic.Symbols.TypeParameterConstraintKind.VALUE then
266
flags = flags | _VALUE_TYPE_CONSTRAINT
267
fi
268
269
return cast System.Reflection.GenericParameterAttributes(flags)
270
si
271
272
_PRIVATE_FIELD: int static => 1
273
_ASSEMBLY_FIELD: int static => 3
274
_PUBLIC_FIELD: int static => 6
275
_STATIC_FIELD: int static => 16
276
277
_LITERAL_FIELD: int static => 64
278
_HAS_DEFAULT_FIELD: int static => 32768
279
280
// FieldAttributes numbers these differently from
281
// MethodAttributes, which uses 0x0800 / 0x1000 for the same two
282
// marks.
283
_SPECIAL_NAME_FIELD: int static => 512
284
_RT_SPECIAL_NAME_FIELD: int static => 1024
285
286
unit_variant_instance_attributes() -> FieldAttributes static =>
287
cast FieldAttributes(_PUBLIC_FIELD | _STATIC_FIELD)
288
289
// An enum's members are compile-time constants: each is a static
290
// literal of the enum's own type, whose value lives in a Constant
291
// row rather than in any initializer.
292
enum_member_attributes() -> FieldAttributes static =>
293
cast FieldAttributes(
294
_PUBLIC_FIELD | _STATIC_FIELD | _LITERAL_FIELD | _HAS_DEFAULT_FIELD)
295
296
// The instance field an enum's value is actually stored in. Its
297
// name is fixed by ECMA-335 and the runtime finds it by the
298
// rtspecialname mark rather than by position.
299
enum_value_attributes() -> FieldAttributes static =>
300
cast FieldAttributes(_PUBLIC_FIELD | _SPECIAL_NAME_FIELD | _RT_SPECIAL_NAME_FIELD)
301
302
field_attributes(`field: Field) -> FieldAttributes static is
303
// An auto-property's backing field, a cached delegate and a
304
// state machine's state are all read through something else
305
// that is public in its own right, so nothing outside the
306
// assembly has any business reaching them.
307
let flags mut =
308
if `field.is_public_readable /\ !`field.is_internal then
309
_PUBLIC_FIELD
310
else
311
_ASSEMBLY_FIELD
312
fi
313
314
if isa Semantic.Symbols.STATIC_FIELD(`field) \/
315
isa Semantic.Symbols.GLOBAL_VARIABLE(`field)
316
then
317
flags = flags | _STATIC_FIELD
318
fi
319
320
return cast FieldAttributes(flags)
321
si
322
si
323
si