Skip to content
← Back

src/semantic/dotnet/interface_nullability.ghul

1
@@IF.dotnet()
2
3
namespace Semantic.DotNet is
4
use Collections.MAP
5
use Collections.LIST
6
7
use TYPE = System.Type
8
use ASSEMBLY = System.Reflection.Assembly
9
10
use MDR = System.Reflection.Metadata.MetadataReader
11
use System.Reflection.Metadata.HandleKind
12
use System.Reflection.Metadata.TypeReferenceHandle
13
use System.Reflection.Metadata.TypeDefinitionHandle
14
use System.Reflection.Metadata.TypeSpecificationHandle
15
use System.Reflection.Metadata.MemberReferenceHandle
16
use System.Reflection.Metadata.BlobHandle
17
use System.Reflection.Metadata.InterfaceImplementation
18
use System.Reflection.Metadata.CustomAttribute
19
use System.Reflection.Metadata.Ecma335.MetadataTokens
20
21
// Reads the `NullableAttribute` an interface implementation carries
22
// on its own `InterfaceImpl` metadata row — the one reference-`?`
23
// carrier `NULLABILITY.resolve` can't reach cross-assembly, because
24
// .NET reflection has no `MemberInfo` for "this type's
25
// implementation of interface X" to hang `CustomAttributeData` off.
26
// A class's field, property, parameter and return slots all round-
27
// trip a `T?` type argument fine on import — this is specifically
28
// about a type argument in the class's own ancestor list, e.g.
29
// `class THING: MutableMap[string, string?]`.
30
//
31
// Reads it the way the emitter writes it (`SRM_STRUCTURE_WALK`'s
32
// `_nullable_slot_attributes` on the `InterfaceImplementationHandle`
33
// `SRM_ASSEMBLY_EMITTER.add_interface_implementation` returns): via
34
// `System.Reflection.Metadata` directly against the assembly's raw
35
// bytes, since ordinary reflection (`System.Type`,
36
// `CustomAttributeData`) has no way to address this row at all.
37
//
38
// The result is keyed by the interface's own closed `System.Type`.
39
// `INTERFACE_TYPE_PROVIDER` decodes the full instantiation to that
40
// `Type`, via the same already-loaded assemblies the rest of the
41
// compiler reflects through, so the caller compares actual type
42
// identity rather than a name rendered from either side.
43
class INTERFACE_NULLABILITY is
44
// One entry per interface `declaring_type`'s own `TypeDef`
45
// implements directly (not one it only inherits via a base
46
// class), keyed by the interface's own closed `Type` — see
47
// `key_for`, which reflects the same interface to the same
48
// `Type` for comparison at the call site. The byte lists follow
49
// the same pre-order convention `NULLABILITY.apply_bytes`
50
// already reads: whatever the row's own head byte says, only a
51
// `2` anywhere in the tree ever turns a position optional, so
52
// this is safe to feed straight in.
53
//
54
// Empty when `reader` can't resolve `declaring_type`'s own
55
// `TypeDef`, or that `TypeDef` declares no interfaces with a
56
// `NullableAttribute` on their row — the caller's fallback,
57
// leaving every position at its non-optional default, is
58
// correct either way.
59
read_declared(
60
reader: MDR,
61
declaring_type: TYPE,
62
assembly_names: MAP[string,ASSEMBLY]
63
) -> MAP[TYPE, LIST[int]] static is
64
let result = MAP[TYPE, LIST[int]]()
65
66
let type_def = _type_definition(reader, declaring_type)
67
68
if !type_def? then
69
// `reader` can't resolve `declaring_type`'s own `TypeDef`
70
// — every interface argument stays at its non-optional
71
// default, the same outcome as an assembly with no
72
// nullability metadata at all.
73
return result
74
fi
75
76
let provider = INTERFACE_TYPE_PROVIDER(declaring_type.assembly, assembly_names)
77
78
for impl_handle in type_def.get_interface_implementations() do
79
// One interface implementation this reader can't resolve
80
// or decode leaves that one interface's arguments at
81
// their non-optional default — never the others in the
82
// same list.
83
try
84
let implementation = reader.get_interface_implementation(impl_handle)
85
let interface_type = _resolve(provider, reader, declaring_type, implementation.`interface)
86
87
if let bytes = _nullable_bytes(reader, implementation) then
88
result[interface_type] = bytes
89
fi
90
catch ex: System.Exception
91
yrt
92
od
93
94
return result
95
si
96
97
_type_definition(reader: MDR, declaring_type: TYPE) -> System.Reflection.Metadata.TypeDefinition? static is
98
try
99
return reader.get_type_definition(MetadataTokens.type_definition_handle(declaring_type.metadata_token))
100
catch ex: System.Exception
101
return null
102
yrt
103
si
104
105
// The matching key for a reflected interface `Type` from
106
// `type.get_interfaces()` — identity, unreduced: it already is
107
// the closed instantiation `read_declared` resolves through
108
// `INTERFACE_TYPE_PROVIDER`, so the two sides compare equal for
109
// the same interface directly.
110
key_for(interface_type: TYPE) -> TYPE static => interface_type
111
112
// `handle` is an `InterfaceImplementation`'s own `Interface`
113
// field: a bare `TypeReference`/`TypeDefinition` for a
114
// non-generic interface, or a `TypeSpecification` — a
115
// signature, not a handle a provider method resolves directly —
116
// for a closed generic one, decoded via `TypeSpecification`'s
117
// own `decode_signature`, which is what makes `provider`'s
118
// recursive calls back into itself well-typed for nested
119
// generics.
120
_resolve(
121
provider: INTERFACE_TYPE_PROVIDER,
122
reader: MDR,
123
declaring_type: TYPE,
124
handle: System.Reflection.Metadata.EntityHandle
125
) -> TYPE static is
126
case handle.kind
127
when HandleKind.TYPE_SPECIFICATION then
128
return reader.get_type_specification(cast TypeSpecificationHandle(handle))
129
.decode_signature[TYPE, TYPE](provider, declaring_type)
130
when HandleKind.TYPE_DEFINITION then
131
return provider.get_type_from_definition(reader, cast TypeDefinitionHandle(handle), 0ub)
132
when HandleKind.TYPE_REFERENCE then
133
return provider.get_type_from_reference(reader, cast TypeReferenceHandle(handle), 0ub)
134
else
135
throw System.NotSupportedException("unsupported interface handle kind {handle.kind}")
136
esac
137
si
138
139
// The pre-order nullability bytes off `implementation`'s own
140
// `NullableAttribute`, or absent when the row carries none, or
141
// carries some other attribute only.
142
_nullable_bytes(reader: MDR, implementation: InterfaceImplementation) -> LIST[int]? static is
143
for attribute_handle in implementation.get_custom_attributes() do
144
let attribute = reader.get_custom_attribute(attribute_handle)
145
146
if _is_nullable_attribute(reader, attribute) then
147
return _decode_value(reader, attribute.value)
148
fi
149
od
150
151
return null
152
si
153
154
_is_nullable_attribute(reader: MDR, attribute: CustomAttribute) -> bool static is
155
let constructor = attribute.constructor
156
157
if constructor.kind != HandleKind.MEMBER_REFERENCE then
158
return false
159
fi
160
161
let member_ref = reader.get_member_reference(cast MemberReferenceHandle(constructor))
162
let parent = member_ref.parent
163
164
if parent.kind != HandleKind.TYPE_REFERENCE then
165
return false
166
fi
167
168
let type_ref = reader.get_type_reference(cast TypeReferenceHandle(parent))
169
170
return reader.get_string(type_ref.`namespace) =~ "System.Runtime.CompilerServices" /\
171
reader.get_string(type_ref.name) =~ "NullableAttribute"
172
si
173
174
// `SRM_ATTRIBUTE_BLOB_ENCODER.encode_byte_argument` /
175
// `encode_byte_array_argument` write exactly two shapes, and
176
// their total lengths never overlap: prolog(2) + one byte +
177
// num-named(2) = 5 for a single-position attribute, or
178
// prolog(2) + count(4) + `count` bytes + num-named(2) = 8 +
179
// count otherwise. That is enough to tell the two apart without
180
// decoding the constructor's own signature.
181
_decode_value(reader: MDR, value_handle: BlobHandle) -> LIST[int]? static is
182
let blob mut = reader.get_blob_reader(value_handle)
183
let total = blob.length
184
185
if total < 5 \/ blob.read_u_int16() != 1us then
186
return null
187
fi
188
189
let result = LIST[int]()
190
191
if total == 5 then
192
result.add(cast int(blob.read_byte()))
193
elif total >= 8 then
194
let count = cast int(blob.read_u_int32())
195
196
if total != 8 + count then
197
return null
198
fi
199
200
for i in 0..count do
201
result.add(cast int(blob.read_byte()))
202
od
203
else
204
return null
205
fi
206
207
return result
208
si
209
si
210
si