Skip to content
← Back

src/semantic/dotnet/assemblies.ghul

1
@@IF.dotnet()
2
3
namespace Semantic.DotNet is
4
use TYPE = System.Type
5
6
use System.Reflection.PathAssemblyResolver
7
use System.Reflection.MetadataLoadContext
8
use System.Reflection.MetadataAssemblyResolver
9
use System.Reflection.Assembly
10
11
use IO.Std
12
13
use Collections.MAP
14
use Collections.SET
15
use Collections.LIST
16
use Collections.Iterable
17
18
use Ghul.Pipes
19
20
use Logging.TIMERS
21
22
// The load context comes from the assembly paths, which are not
23
// known until start.
24
@suppress("field-definite-assignment")
25
class ASSEMBLIES(
26
_timers: TIMERS,
27
_flags: Compiler.GLOBAL_BUILD_FLAGS,
28
_paths: Driver.PATH_CONFIG,
29
_ghul_symbol_table: Semantic.SYMBOL_TABLE,
30
_namespaces: NAMESPACES,
31
_type_name_map: TYPE_NAME_MAP,
32
_type_details_lookup: TYPE_DETAILS_LOOKUP
33
): TypeSource is
34
_callbacks: LIST[() -> void]
35
36
_metadata_load_context: MetadataLoadContext
37
38
_assemblies_by_name: MAP[string,Assembly]
39
_assemblies_by_ghul_namespace: MAP[string,SET[Assembly]]
40
41
// The file each assembly was loaded from, kept so
42
// `interface_nullability_bytes` can re-read the raw bytes —
43
// `Assembly.location` is empty for one loaded via
44
// `load_from_byte_array` below, so the path has to be tracked
45
// separately or it's lost the moment the byte array is.
46
_assembly_paths: MAP[string,string]
47
48
// A digest of each assembly in the set, by name, taken when it is
49
// first needed, so that naming the same assembly again can be told
50
// apart from naming a rebuilt one under the same name.
51
_digests: MAP[string,string]
52
53
// The assemblies this compile was handed by name, on the command
54
// line or added to a running compiler since, as against those it
55
// found for itself. Only one of these can be an earlier cell of
56
// a session.
57
_explicit_references: SET[string]
58
_session_cells: MAP[string,bool]
59
_has_access_attribute: bool?
60
61
// One `System.Reflection.PortableExecutable.PEReader` per
62
// assembly that has actually needed one — built, and its file
63
// read into memory, only once `interface_nullability_bytes`
64
// finds a type that implements a generic interface at all (its
65
// own cheap gate, checked before this cache is ever touched);
66
// an assembly contributing no such type is never opened here,
67
// however many of its types get imported. `null` is cached too,
68
// for an assembly whose path couldn't be read or parsed, so a
69
// failing lookup isn't retried on every call. Kept for the
70
// process's lifetime once built, so this only pays off in the
71
// common case where a nullable-argument-carrying assembly is
72
// asked about more than once.
73
//
74
// Cached as the `PEReader` itself, not the `MetadataReader` it
75
// hands out — a `MetadataReader` is a pointer into the memory
76
// block the `PEReader` owns and holds no reference back to it,
77
// so a `MetadataReader` cached alone outlives the pin on that
78
// block only until the `PEReader` is collected.
79
_interface_metadata_readers: MAP[string,System.Reflection.PortableExecutable.PEReader?]
80
81
_is_started: bool
82
_default_imports_are_needed: bool
83
84
blocked_assemblies: SET[string]
85
86
all_assemblies: Iterable[Assembly] => _assemblies_by_name.values
87
88
// A type by its full name, from whichever loaded assembly defines
89
// it, or null if none does.
90
find_type(full_name: string) -> TYPE? is
91
for assembly in _assemblies_by_name.values do
92
let type = assembly.get_type(full_name)
93
94
if type? then
95
return type
96
fi
97
od
98
99
return null
100
si
101
102
// The version of the assembly loaded under this name, in the
103
// colon-separated form a reference row records, or null if none
104
// was.
105
version_of(name: string) -> string? =>
106
if _assemblies_by_name.contains_key(name) then
107
if let version = _assemblies_by_name[name].get_name().version then
108
version.to_string().replace('.', ':')
109
else
110
null
111
fi
112
else
113
null
114
fi
115
116
// The public key token of the assembly loaded under this name,
117
// which a reference to a strong-named assembly has to record for
118
// a compiler to accept it. Null if none was loaded or it has none.
119
public_key_token_of(name: string) -> ubyte[]? =>
120
if _assemblies_by_name.contains_key(name) then
121
(let token = _assemblies_by_name[name].get_name().get_public_key_token()
122
123
if token? /\ token.count > 0 then token else null fi)
124
else
125
null
126
fi
127
128
init(..) is
129
_default_imports_are_needed = true
130
131
_callbacks = LIST()
132
133
_assemblies_by_name = MAP()
134
_assemblies_by_ghul_namespace = MAP[string,SET[Assembly]]()
135
_assembly_paths = MAP()
136
_digests = MAP()
137
_explicit_references = SET[string]()
138
_session_cells = MAP[string,bool]()
139
_interface_metadata_readers = MAP[string,System.Reflection.PortableExecutable.PEReader?]()
140
141
blocked_assemblies = SET()
142
block_all_unsupported_assemblies()
143
si
144
145
on_start(callback: () -> void) is
146
_callbacks.add(callback)
147
si
148
149
get_type(type_name: string) -> TYPE =>
150
get_type("System.Runtime", type_name)
151
152
get_type(assembly_name: string? mut, type_name: string) -> TYPE is
153
if assembly_name == null then
154
assembly_name = "System.Runtime"
155
fi
156
157
let result: TYPE? = _assemblies_by_name[assembly_name].get_type(type_name)
158
159
assert result? else "couldn't find type {type_name} in {assembly_name}"
160
161
return result
162
si
163
164
get_types(type_names: Iterable[string]) -> Collections.List[TYPE] =>
165
type_names |>
166
map(name => get_type(name)) |>
167
collect()
168
169
get_types(assembly_and_type_names: Iterable[(assembly_name: string, type_name: string)]) -> Collections.List[TYPE] =>
170
assembly_and_type_names |>
171
map(names => get_type(names.assembly_name, names.type_name)) |>
172
collect()
173
174
// See `INTERFACE_NULLABILITY` for why this exists at all: the
175
// nullability of a reference type argument in `declaring_type`'s
176
// own ancestor list (`class THING: MutableMap[string, string?]`)
177
// has no reflection-visible carrier, so `add_ancestors` needs
178
// this to recover it from the assembly's raw metadata instead.
179
// Empty when `declaring_type` implements no generic interface at
180
// all — the common case, and the check that keeps this from
181
// reading every referenced assembly's file into memory just
182
// because *some* type in it got imported — or the reader can't
183
// be built, or `declaring_type` implements nothing with a `?` in
184
// its type arguments.
185
interface_nullability_bytes(declaring_type: TYPE) -> MAP[TYPE,Collections.LIST[int]] is
186
if !_implements_generic_interface(declaring_type) then
187
return MAP[TYPE,Collections.LIST[int]]()
188
fi
189
190
let reader = _interface_metadata_reader(declaring_type)
191
192
if !reader? then
193
return MAP[TYPE,Collections.LIST[int]]()
194
fi
195
196
return INTERFACE_NULLABILITY.read_declared(reader, declaring_type, _assemblies_by_name)
197
si
198
199
// A cheap, reflection-only pre-check: does `declaring_type`
200
// implement any generic interface at all? Every case this whole
201
// mechanism exists for needs one — a non-generic interface has
202
// no type argument to be `?`. A false positive here (a generic
203
// interface with no nullable argument) still costs a metadata
204
// read; a false negative would silently drop a real `?`, so the
205
// check stays deliberately wide rather than trying to rule out
206
// value-type arguments here too.
207
_implements_generic_interface(type: TYPE) -> bool is
208
for i in type.get_interfaces() do
209
if i.is_generic_type then
210
return true
211
fi
212
od
213
214
return false
215
si
216
217
_interface_metadata_reader(type: TYPE) -> System.Reflection.Metadata.MetadataReader? is
218
let assembly_name = type.assembly.get_name().name ?? ""
219
220
let cached: System.Reflection.PortableExecutable.PEReader? mut
221
222
let pe_reader =
223
if _interface_metadata_readers.try_get_value(assembly_name, cached ref) then
224
cached
225
else
226
let loaded = _load_interface_reader(assembly_name)
227
228
_interface_metadata_readers.add(assembly_name, loaded)
229
230
loaded
231
fi
232
233
if !pe_reader? then
234
return null
235
fi
236
237
return System.Reflection.Metadata.PEReaderExtensions.get_metadata_reader(pe_reader)
238
si
239
240
// Re-reads the assembly's bytes rather than reusing what
241
// `_metadata_load_context` loaded with: those went into
242
// `load_from_byte_array` and aren't retained afterwards, and a
243
// fresh in-memory copy carries none of the open-file-handle risk
244
// that reading via a file stream would (see the comment on
245
// `load_from_byte_array` above) — the bytes are read once, into
246
// an `ImmutableArray`, and the file is never held open.
247
//
248
// Returns the `PEReader` itself, not a `MetadataReader` derived
249
// from it — see the comment on `_interface_metadata_readers`.
250
// The caller derives a fresh `MetadataReader` from the cached
251
// `PEReader` on every call, which is cheap: it just wraps the
252
// memory block the `PEReader` already read.
253
_load_interface_reader(assembly_name: string) -> System.Reflection.PortableExecutable.PEReader? is
254
let path: string mut
255
256
if !_assembly_paths.try_get_value(assembly_name, path ref) then
257
return null
258
fi
259
260
try
261
let bytes = IO.File.read_all_bytes(path)
262
let image = System.Collections.Immutable.ImmutableArray.create[ubyte](bytes)
263
264
return System.Reflection.PortableExecutable.PEReader(image)
265
catch ex: System.Exception
266
return null
267
yrt
268
si
269
270
import(assembly_names: Iterable[string]?) is
271
import(assembly_names, LIST[string]())
272
si
273
274
// `assembly_names` is the whole reference set, or empty for the
275
// one discovered from the SDK; `additional` is imported as well,
276
// whichever of the two that was.
277
import(assembly_names: Iterable[string]? mut, additional: Iterable[string]) is
278
if _is_started then
279
return
280
fi
281
282
_is_started = true
283
284
for path in additional do
285
_explicit_references.add(assembly_name_of(path))
286
od
287
288
assembly_names = REFERENCE_SET.resolve(assembly_names, additional, () => _default_imports())
289
290
let path_resolver = PathAssemblyResolver(assembly_names)
291
292
_metadata_load_context =
293
MetadataLoadContext(
294
LOADED_ASSEMBLY_RESOLVER(
295
name is
296
let assembly: Assembly mut = _
297
298
return if _assemblies_by_name.try_get_value(name, assembly ref) then assembly else null fi
299
si,
300
path_resolver),
301
null)
302
303
let to_process = LIST[(assembly: Assembly, name: string)]()
304
305
for path in assembly_names do
306
let name = assembly_name_of(path)
307
308
if _assemblies_by_name.contains_key(name) \/ blocked_assemblies.contains(name) then
309
continue
310
fi
311
312
// load_from_assembly_path memory-maps the file, holding it open and
313
// write-locked for the lifetime of the MetadataLoadContext; that blocks
314
// rebuilds of project-reference outputs from an analysis-mode session.
315
let assembly = _metadata_load_context.load_from_byte_array(IO.File.read_all_bytes(path))
316
317
to_process.add((assembly, name))
318
319
_assemblies_by_name.add(name, assembly)
320
_assembly_paths.add(name, path)
321
od
322
323
_type_name_map.start(self)
324
325
for c in _callbacks do
326
c()
327
od
328
329
for a in to_process do
330
import(a.assembly, a.name)
331
od
332
si
333
334
// Imports one more assembly into a reference set that is already
335
// loaded, for a process that outlives a single compile and is
336
// handed references as it goes. The same assembly named again is
337
// left as it is. A different assembly under a name the set already
338
// holds cannot be loaded beside it, and compiling against the one
339
// already loaded would silently use the wrong symbols, so that is an
340
// error.
341
import_additional(path: string) is
342
assert _is_started else "no reference set to add to yet"
343
344
let name = assembly_name_of(path)
345
346
if blocked_assemblies.contains(name) then
347
return
348
fi
349
350
_explicit_references.add(name)
351
352
let bytes = IO.File.read_all_bytes(path)
353
let digest = _digest(bytes)
354
355
if _assemblies_by_name.contains_key(name) then
356
if _digest_of_loaded(name) =~ digest then
357
return
358
fi
359
360
throw System.InvalidOperationException("the reference set already holds a different assembly named {name}")
361
fi
362
363
let assembly = _metadata_load_context.load_from_byte_array(bytes)
364
365
_assemblies_by_name.add(name, assembly)
366
_assembly_paths.add(name, path)
367
_digests.add(name, digest)
368
369
import(assembly, name)
370
si
371
372
// Whether `assembly` is an earlier cell of the REPL session this
373
// compile belongs to: named as one of its references, and marked
374
// as a session cell when it was compiled. A session is compiled a
375
// cell at a time but written as though it were one file, so what
376
// such an assembly declares is treated as this assembly's own:
377
// its assembly-private definitions are visible, and the one
378
// compiled here is given access to them.
379
is_session_cell(assembly: Assembly) -> bool is
380
let name = assembly.get_name().name
381
382
if !name? \/ !_explicit_references.contains(name) then
383
return false
384
fi
385
386
let known: bool mut = false
387
388
if _session_cells.try_get_value(name, known ref) then
389
return known
390
fi
391
392
let result = SESSION_CELL_MARKER.is_marked(assembly)
393
394
_session_cells.add(name, result)
395
396
return result
397
si
398
399
// Whether the assembly-private definitions of an earlier cell can
400
// be used from this compile: it is a session cell, and the runtime
401
// referenced declares the attribute that gives the access at run
402
// time. Without that attribute the cell's private names are left
403
// out, so a use of one is a compile error rather than a failure
404
// when the cell runs.
405
can_reach_privates_of(assembly: Assembly) -> bool is
406
if !is_session_cell(assembly) then
407
return false
408
fi
409
410
if !_has_access_attribute? then
411
_has_access_attribute = find_type(SESSION_CELL_ATTRIBUTES.ACCESS_ATTRIBUTE_NAME)?
412
fi
413
414
return _has_access_attribute ?? false
415
si
416
417
// The same question asked of a name rather than an assembly,
418
// for a symbol that knows which assembly it was imported from
419
// but not which Assembly object that was.
420
is_session_cell_named(name: string?) -> bool is
421
if !name? then
422
return false
423
fi
424
425
let assembly: Assembly mut = _
426
427
if !_assemblies_by_name.try_get_value(name, assembly ref) then
428
return false
429
fi
430
431
return is_session_cell(assembly)
432
si
433
434
// The names of the references that are earlier cells of the
435
// session.
436
session_cell_names: Collections.List[string] is
437
let result = LIST[string]()
438
439
for name in _explicit_references do
440
let assembly: Assembly mut = _
441
442
if _assemblies_by_name.try_get_value(name, assembly ref) /\ is_session_cell(assembly) then
443
result.add(name)
444
fi
445
od
446
447
result.sort()
448
449
return result
450
si
451
452
_digest(bytes: ubyte[]) -> string static =>
453
System.Convert.to_base64_string(System.Security.Cryptography.SHA256.hash_data(bytes))
454
455
// The digest of the assembly loaded under `name`: recorded when
456
// import_additional loaded it, and otherwise taken from the file the
457
// set loaded it from, since the reference set a process started with
458
// records no digests. Null when there is no such file to read.
459
_digest_of_loaded(name: string) -> string? is
460
let known: string mut = _
461
462
if _digests.try_get_value(name, known ref) then
463
return known
464
fi
465
466
let path: string mut = _
467
468
if !_assembly_paths.try_get_value(name, path ref) \/ !IO.File.exists(path) then
469
return null
470
fi
471
472
let digest = _digest(IO.File.read_all_bytes(path))
473
474
_digests.add(name, digest)
475
476
return digest
477
si
478
479
// The name an assembly file holds for itself, which is what anything
480
// referring to it records: the file can be called anything. Read
481
// from its metadata before it is loaded, since a load context holds
482
// one assembly of each name, so a duplicate or a blocked name has to
483
// be recognised first. Falls back to the file name for a file whose
484
// metadata cannot be read, leaving the load to report it.
485
assembly_name_of(path: string) -> string static is
486
try
487
if let name = System.Reflection.AssemblyName.get_assembly_name(path).name then
488
return name
489
fi
490
catch ex: System.Exception
491
yrt
492
493
return IO.Path.get_file_name_without_extension(path) ?? ""
494
si
495
496
// The whole reference pack, which is also what an MSBuild build
497
// passes: the SDK decides what the framework is, so a pack that
498
// gains an assembly needs no change here. Sorted so the load
499
// order does not depend on the file system.
500
_default_imports() -> Iterable[string] is
501
let sdk_path = _paths.sdk_reference_path ?? _paths.discover_sdk_reference_path()
502
503
assert sdk_path? else System.Exception("no reference assemblies supplied: pass -a <assembly> or --sdk-ref-path <directory>, or ensure the .NET SDK is installed")
504
505
_paths.sdk_reference_path = sdk_path
506
507
let result =
508
IO.Directory.get_files(sdk_path, "*.dll") |>
509
sort() |>
510
collect_mutable()
511
512
if !_flags.exclude_runtime_symbols then
513
result.add("{_paths.install_folder}ghul-runtime.dll")
514
fi
515
516
return result
517
si
518
519
import(assembly: Assembly, assembly_name: string?) is
520
assert assembly_name? /\ assembly_name.length > 0
521
522
let types_file = "{_paths.get_library_location(IO.Path.combine("dotnet", "refs"))}{assembly_name}.types"
523
524
let any_succeeded mut = false
525
let exported_ex: System.Exception? mut = null
526
let forwarded_ex: System.Exception? mut = null
527
528
try
529
for type in assembly.get_types() do
530
import_type(type, assembly_name)
531
532
any_succeeded = true
533
od
534
catch ex: System.Exception
535
exported_ex = exported_ex
536
yrt
537
538
try
539
for type in assembly.get_forwarded_types() do
540
import_type(type, assembly_name)
541
542
any_succeeded = true
543
od
544
catch ex: System.Exception
545
forwarded_ex = ex
546
yrt
547
548
if !any_succeeded then
549
Std.error.write_line("warning: couldn't enumerate any types in {assembly}")
550
fi
551
si
552
553
// Nested `family` and `famorassem` types stay reachable, matching the
554
// carve-out the method import makes for protected members. An enclosing
555
// type that is out of reach puts everything nested inside it out of
556
// reach too.
557
_is_externally_reachable(type: TYPE) -> bool is
558
// An earlier cell of the session is this assembly's own, all
559
// but what the compiler generated for it.
560
if can_reach_privates_of(type.assembly) then
561
return !type.name.contains('$') \/ type.is_public
562
fi
563
564
let current: TYPE? mut = type
565
566
while current? do
567
if current.is_nested then
568
if !(current.is_nested_public \/ current.is_nested_family \/ current.is_nested_fam_o_r_assem) then
569
return false
570
fi
571
elif !current.is_public then
572
return false
573
fi
574
575
current = current.declaring_type
576
od
577
578
return true
579
si
580
581
import_type(type: TYPE, assembly_name: string?) is
582
assert assembly_name? else "attempting to import type {type} with no assembly name supplied"
583
584
if !_is_externally_reachable(type) then
585
return
586
fi
587
588
try
589
let existing = _type_name_map.get_type_details(type)
590
591
if existing? then
592
existing.merge_assembly_reference(type, assembly_name)
593
594
return
595
elif !type.`namespace? \/ !type.full_name? then
596
return
597
fi
598
599
// A ghūl `union` emits each variant as a sibling class
600
// whose .NET `Namespace` is the union's full name. Left
601
// alone, the reflection loader would register that
602
// string as an actual namespace and clash with the
603
// union class itself. The compiler tags variants with
604
// `[Ghul.Internal.VARIANT_ATTRIBUTE]`; queue them under
605
// the union's full name so symbol_factory can
606
// materialize each one as a member of the union when
607
// the union's symbol is created.
608
if has_attribute(type, "Ghul.Internal.VARIANT_ATTRIBUTE") /\ type.`namespace? then
609
_type_details_lookup.register_variant(type.`namespace!, type)
610
return
611
fi
612
613
let namespace_details = _type_name_map.get_namespace_details(type)
614
let namespace_name: string mut
615
616
if namespace_details? then
617
namespace_name = namespace_details.ghul_name
618
else
619
namespace_name = type.`namespace!
620
fi
621
622
// The intrinsic declarations are made in a staging
623
// namespace - `Ghul.Internal` on assemblies built before
624
// `Ghul.Intrinsics.Staging` existed - so that assembly can
625
// declare them and still be compiled by a compiler that
626
// reflects them: the declaration and the registration it
627
// produces never occupy the same namespace. The language
628
// expects to find them in `Ghul.Intrinsics`, the namespace
629
// every compilation imports as its ambient set.
630
let is_globals_carrier =
631
GLOBALS_CARRIER.is_carrier(type.name, has_attribute(type, GLOBALS_CARRIER.attribute_name))
632
633
if
634
(namespace_name =~ "Ghul.Intrinsics.Staging" \/ namespace_name =~ "Ghul.Internal") /\
635
(is_globals_carrier \/ has_attribute(type, "Ghul.Internal.INTRINSIC_ATTRIBUTE"))
636
then
637
namespace_name = "Ghul.Intrinsics"
638
fi
639
640
// Build the ghūl-visible identifier by stripping the `\`N`
641
// generic-argument-count suffix that .NET reflection exposes
642
// for generic-type definitions (`Foo<T>` arrives as `Foo\`1`).
643
// For a nested type the suffix sits on each enclosing segment
644
// (`Dictionary\`2+ValueCollection`), so the strip is applied
645
// per `+`-separated segment before the segments are joined with
646
// `_`. Only the ghūl-visible identifier is cleaned up; IL
647
// emission and reflection load still see the .NET full_name
648
// with the suffixes.
649
let type_name mut = _strip_arity_suffix(type.name)
650
651
if type.is_nested then
652
let parts = type.full_name!.split(['.'])
653
let leaf = parts[parts.count-1]
654
655
let buffer = System.Text.StringBuilder()
656
let seen_any mut = false
657
658
for segment in leaf.split(['+']) do
659
if seen_any then
660
buffer.append('_')
661
fi
662
663
buffer.append(_strip_arity_suffix(segment))
664
seen_any = true
665
od
666
667
type_name = buffer.to_string()
668
fi
669
670
if type.`namespace =~ "Ghul.Internal" then
671
_register_constructor_intrinsics(type)
672
fi
673
674
let type_details = TYPE_DETAILS(type, namespace_name, type_name, null, assembly_name)
675
676
type_details.is_globals_carrier = is_globals_carrier
677
type_details.is_compiler_generated =
678
has_attribute(type, "System.Runtime.CompilerServices.CompilerGeneratedAttribute")
679
680
type_details.async_builder_type = _read_async_method_builder(type)
681
682
if type.is_enum then
683
type_details.has_flags_attribute = has_attribute(type, "System.FlagsAttribute")
684
fi
685
686
assert type_details.assembly_name? /\ type_details.assembly_name.length > 0 else "invalid assmbly name before add type {type_details}"
687
688
_type_details_lookup.add_type(type_details)
689
catch ex: System.Exception
690
Std.error.write_line("failed to import: {type} exception: {ex.to_string().replace('\n', ' ')}")
691
yrt
692
si
693
694
// A public static method the runtime marks as a `constructor.`
695
// intrinsic is offered as a constructor of the type it returns.
696
_register_constructor_intrinsics(type: TYPE) is
697
for method in type.get_methods(cast System.Reflection.BindingFlags(16 + 8 + 2)) do
698
let operation = INTRINSIC_OPERATION.of(method.get_custom_attributes_data())
699
700
if operation? /\ operation.starts_with("constructor.") then
701
_type_details_lookup.register_constructor_intrinsic(method)
702
fi
703
od
704
si
705
706
// The System.Type argument of the type.s AsyncMethodBuilderAttribute,
707
// or null when it carries none. The attribute names the builder an
708
// async function returning this type drives; its argument is an
709
// open generic type definition when the task-like is generic.
710
_read_async_method_builder(type: TYPE) -> TYPE? is
711
try
712
for attr in type.get_custom_attributes_data() do
713
if attr.attribute_type.full_name =~ "System.Runtime.CompilerServices.AsyncMethodBuilderAttribute" then
714
for arg in attr.constructor_arguments do
715
if let builder: TYPE = arg.value then
716
return builder
717
fi
718
od
719
fi
720
od
721
catch ex: System.Exception
722
// Mirrors has_attribute: an unreadable attribute set reads
723
// as no attribute.
724
yrt
725
726
return null
727
si
728
729
has_attribute(type: TYPE, attribute_name: string) -> bool is
730
try
731
for attr in type.get_custom_attributes_data() do
732
let attr_type = attr.attribute_type
733
734
if attr_type.full_name =~ attribute_name then
735
return true
736
fi
737
od
738
catch ex: System.Exception
739
// Some reflected types fail at GetCustomAttributesData()
740
// (e.g. with missing referenced assemblies). Treating
741
// the absence as an absent marker is safe — the worst
742
// case is the unmarked behaviour kicking in for that type.
743
yrt
744
745
return false
746
si
747
748
_strip_arity_suffix(name: string) -> string static is
749
let backtick = name.last_index_of('`')
750
751
if backtick <= 0 \/ backtick >= name.length - 1 then
752
return name
753
fi
754
755
for i in (backtick + 1)..name.length do
756
let c = name[i]
757
758
if c < '0' \/ c > '9' then
759
return name
760
fi
761
od
762
763
return name.substring(0, backtick)
764
si
765
766
block_all_unsupported_assemblies() is
767
blocked_assemblies.add("netstandard")
768
blocked_assemblies.add("mscorlib")
769
blocked_assemblies.add("WindowsBase")
770
blocked_assemblies.add("System.Configuration")
771
blocked_assemblies.add("System.Core")
772
blocked_assemblies.add("System.Data")
773
blocked_assemblies.add("System")
774
blocked_assemblies.add("System.Drawing")
775
blocked_assemblies.add("System.Net")
776
blocked_assemblies.add("System.Private.CoreLib")
777
blocked_assemblies.add("System.Security")
778
blocked_assemblies.add("System.ServiceModel.Web")
779
blocked_assemblies.add("System.ServiceProcess")
780
blocked_assemblies.add("System.Transactions")
781
blocked_assemblies.add("System.Configuration.ConfigurationManager")
782
blocked_assemblies.add("System.Runtime.Serialization")
783
si
784
si
785
si