Skip to content
← Back

src/syntax/process/compile-expressions/attribute_resolver.ghul

1
namespace Syntax.Process is
2
use Logging
3
use Source
4
use IoC
5
6
use Semantic.Types.Type
7
8
use System.Text.StringBuilder
9
10
// Resolves an attribute pragma — a pragma whose name is not a built-in
11
// compiler pragma — to a .NET attribute type and constructor, and
12
// attaches the resolved CUSTOM_ATTRIBUTE to the target symbol, from
13
// which the emitter encodes the attribute's value blob.
14
//
15
// The `format_*` half renders the arguments as assembler source. The
16
// rendering itself is unused (see CUSTOM_ATTRIBUTE), but an argument
17
// shape it cannot render makes it answer null, and that is what
18
// rejects the attribute — so it is the argument validation, spelled
19
// as a renderer.
20
class ATTRIBUTE_RESOLVER is
21
_logger: Logger
22
_innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup
23
_overload_resolver: Semantic.OVERLOAD_RESOLVER
24
_visitor: ScopedVisitor
25
26
init(
27
logger: Logger,
28
innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup,
29
overload_resolver: Semantic.OVERLOAD_RESOLVER,
30
visitor: ScopedVisitor
31
) is
32
super.init()
33
34
_logger = logger
35
_innate_symbol_lookup = innate_symbol_lookup
36
_overload_resolver = overload_resolver
37
_visitor = visitor
38
_unsettled_offsets = Collections.MAP[Semantic.Symbols.Field, Source.LOCATION]()
39
si
40
41
// A built-in pragma is handled by the compiler itself; anything
42
// else is taken to name a .NET attribute.
43
is_built_in(name: string) -> bool =>
44
IL_PRAGMAS.is_il_pragma(name) \/
45
name.starts_with("IF.") \/
46
name =~ "precedence" \/
47
name =~ "entry" \/
48
name =~ "suppress" \/
49
name =~ "equality" \/
50
name =~ "intrinsic" \/
51
name =~ "trusted_store_free" \/
52
name =~ "finalizer"
53
54
resolve(pragma: Trees.Pragmas.PRAGMA, target: Semantic.Symbols.Symbol?) is
55
let name = pragma.name.to_string()
56
57
// The test pragmas emitted MSTest 3 attributes against an
58
// assembly name MSTest 4 no longer ships, so the tests they
59
// marked were silently not discovered. The attributes are
60
// written as attributes now instead. The name is rejected
61
// whatever the pragma was written against, so a use inside a
62
// body — which resolves against no target — reports too.
63
if name =~ "test" \/ name =~ "test_class" \/ name =~ "test_method" then
64
_logger.error(
65
pragma.name.location,
66
"{name} is not supported",
67
pragma.name.location,
68
"help: use the test framework's attributes, as in @TestClass() on the class and @TestMethod() on the method")
69
return
70
fi
71
72
// A pragma in the `IL.` family the compiler does not act on is
73
// a misspelling or one that has been removed. Left to the
74
// built-in test below it would be acted on as one and emit
75
// nothing: the program would compile and quietly mean something
76
// other than what it says. Reported whatever the pragma was
77
// written against, so a use inside a body — which resolves
78
// against no target — reports too.
79
if
80
IL_PRAGMAS.is_il_pragma(name) /\
81
!IL_PRAGMAS.is_known(name)
82
then
83
_logger.warn(
84
pragma.name.location, "unknown-pragma", "unknown pragma {name}")
85
86
return
87
fi
88
89
// A pragma naming a fact about a .NET assembly is a use of a
90
// feature the target may not carry. Reported after the
91
// unknown-pragma check, so a misspelling draws the one
92
// diagnostic about it being unknown and not a second one about
93
// the target; and before the target check, so one inside a
94
// body reports too. The compiler's own synthesised pragma is
95
// not a use the program wrote.
96
if IL_PRAGMAS.is_il_pragma(name) /\ !pragma.is_synthesised then
97
IoC.CONTAINER.instance.capability_checker.check(
98
Semantic.Capability.IL_PRAGMA,
99
pragma.name.location,
100
"pragma {name}")
101
fi
102
103
if !target? then
104
return
105
fi
106
107
// `intrinsic` is the compiler's own spelling of the intrinsic
108
// marker. The operation it names is recorded from the pragma
109
// whatever the target; where this compilation's reference set
110
// holds Ghul.Internal.INTRINSIC_ATTRIBUTE — .NET, whose
111
// reflected compilations read the marker back — the pragma is
112
// emitted as that attribute, and where nothing holds it, it
113
// emits nothing.
114
if name =~ "intrinsic" then
115
let intrinsic_attribute = _intrinsic_attribute()
116
117
if intrinsic_attribute? then
118
_record_attribute(pragma, target, intrinsic_attribute)
119
fi
120
121
return
122
fi
123
124
if name =~ "trusted_store_free" then
125
_resolve_trusted_store_free(pragma, target)
126
return
127
fi
128
129
if name =~ "finalizer" then
130
_resolve_finalizer(pragma, target)
131
return
132
fi
133
134
if is_built_in(name) then
135
return
136
fi
137
138
let attribute_type = resolve_attribute_type(pragma.name)
139
140
if !attribute_type? then
141
_logger.error(pragma.name.location, "unknown attribute {name}")
142
return
143
fi
144
145
_record_attribute(pragma, target, attribute_type)
146
si
147
148
// Resolves the pragma's arguments against the attribute type's
149
// constructor and records the attribute on the target, shared by
150
// the ordinary pragma-as-attribute path and the `intrinsic`
151
// spelling.
152
_record_attribute(
153
pragma: Trees.Pragmas.PRAGMA,
154
target: Semantic.Symbols.Symbol,
155
attribute_type: Semantic.Symbols.Classy
156
) is
157
let positional = gather_positional_arguments(pragma)
158
159
let constructor = resolve_constructor(pragma, attribute_type, positional)
160
161
if !constructor? then
162
return
163
fi
164
165
let completed = complete_positional_arguments(constructor, positional)
166
167
let named = resolve_named_arguments(attribute_type, pragma)
168
169
if !named? then
170
return
171
fi
172
173
if !format_arguments(pragma, constructor, completed, named)? then
174
return
175
fi
176
177
// `DllImport` is not carried as a custom attribute: it is a
178
// pseudo-attribute, whose whole content becomes flags on the
179
// method row and a row in the ImplMap table.
180
@IF.dotnet()
181
if attribute_type == _dll_import_attribute() then
182
_resolve_pinvoke(pragma, target, completed, named)
183
return
184
fi
185
186
// `MethodImpl` is a pseudo-attribute too: the runtime reads
187
// the options it names from the method row's implementation
188
// flags, so carrying it as a custom attribute leaves the JIT
189
// seeing nothing.
190
@IF.dotnet()
191
if attribute_type == _method_impl_attribute() then
192
_resolve_method_impl(pragma, target, completed)
193
return
194
fi
195
196
// `StructLayout` and `FieldOffset` are pseudo-attributes as well:
197
// the runtime lays a type out from its flags and its ClassLayout
198
// and FieldLayout rows, and reads neither as a custom attribute.
199
@IF.dotnet()
200
if attribute_type == _struct_layout_attribute() then
201
_resolve_struct_layout(pragma, target, completed, named)
202
return
203
fi
204
205
if attribute_type == _field_offset_attribute() then
206
_resolve_field_offset(pragma, target, completed)
207
return
208
fi
209
210
if !target.custom_attributes? then
211
target.custom_attributes = Collections.LIST[Semantic.CUSTOM_ATTRIBUTE]()
212
fi
213
214
let attributes = target.custom_attributes!
215
216
for i in 0..attributes.count do
217
if attributes[i].pragma == pragma then
218
attributes.remove_at(i)
219
break
220
fi
221
od
222
223
attributes.add(
224
Semantic.CUSTOM_ATTRIBUTE(constructor, completed, named, pragma))
225
226
if attribute_type == _obsolete_attribute() then
227
target.set_deprecation(_deprecation_of(completed))
228
fi
229
si
230
231
_obsolete_attribute_resolved: bool
232
_obsolete: Semantic.Symbols.Symbol?
233
234
_obsolete_attribute() -> Semantic.Symbols.Symbol? is
235
if !_obsolete_attribute_resolved then
236
_obsolete_attribute_resolved = true
237
_obsolete = _visitor.find("System")?.find_member("ObsoleteAttribute")
238
fi
239
240
return _obsolete
241
si
242
243
_intrinsic_attribute_resolved: bool
244
_intrinsic_attribute_symbol: Semantic.Symbols.Classy?
245
246
// Resolved through the symbol table rather than the scope: the
247
// namespace is deliberately unnameable from source, so no `use`
248
// can import it. The reflected table holds the type where the
249
// reference set carries a runtime that declares it; the runtime
250
// itself instead declares it in this compilation's source, so its
251
// own symbols are the fallback. Null where neither holds it.
252
_intrinsic_attribute() -> Semantic.Symbols.Classy? is
253
if !_intrinsic_attribute_resolved then
254
_intrinsic_attribute_resolved = true
255
256
@IF.dotnet()
257
_intrinsic_attribute_symbol =
258
cast Semantic.Symbols.Classy?(
259
IoC.CONTAINER.instance.dotnet_symbol_table.value
260
.get_symbol("Ghul.Internal.INTRINSIC_ATTRIBUTE"))
261
262
if !_intrinsic_attribute_symbol? then
263
if let internal_namespace = IoC.CONTAINER.instance.namespaces
264
.try_find_namespace(".Ghul.Internal") then
265
_intrinsic_attribute_symbol =
266
cast Semantic.Symbols.Classy?(
267
internal_namespace.find_direct("INTRINSIC_ATTRIBUTE"))
268
fi
269
fi
270
fi
271
272
return _intrinsic_attribute_symbol
273
si
274
275
// `trusted_store_free` marks a library member whose own body is
276
// trusted store-free, as a .NET member on the curated import list
277
// is. It is the libraries' to write: a build that declares library
278
// sources accepts it only in one of them, and a build that declares
279
// none is its own core, as for the declarations only a core makes.
280
_resolve_trusted_store_free(pragma: Trees.Pragmas.PRAGMA, target: Semantic.Symbols.Symbol) is
281
let library_sources = IoC.CONTAINER.instance.build_flags.library_sources
282
283
if library_sources? /\ !library_sources.can_declare_core(pragma.location.file_name) then
284
_logger.error(pragma.name.location, "trusted_store_free is only accepted in a library source")
285
return
286
fi
287
288
if let function: Semantic.Symbols.Function = target then
289
function.mark_trusted_store_free()
290
elif let property: Semantic.Symbols.Property = target then
291
if let getter = property.read_function then
292
getter.mark_trusted_store_free()
293
fi
294
else
295
_logger.error(pragma.name.location, "trusted_store_free applies only to a function, method or property")
296
fi
297
si
298
299
// `finalizer` marks the method a target with a collector calls once
300
// an instance of the class is unreachable, to release a host
301
// resource the instance holds. Like `trusted_store_free` it is the
302
// libraries' to write. One class declares at most one, on a method
303
// of its own taking no arguments, and the class is not generic.
304
_resolve_finalizer(pragma: Trees.Pragmas.PRAGMA, target: Semantic.Symbols.Symbol) is
305
let library_sources = IoC.CONTAINER.instance.build_flags.library_sources
306
307
if library_sources? /\ !library_sources.can_declare_core(pragma.location.file_name) then
308
_logger.error(pragma.name.location, "finalizer is only accepted in a library source")
309
return
310
fi
311
312
if let function: Semantic.Symbols.Function = target, owner: Semantic.Symbols.Classy = function.owner /\
313
function.is_instance /\ function.arguments.count == 0 /\ owner.is_class /\ !owner.is_generic
314
then
315
if owner.finalizer? then
316
_logger.error(pragma.name.location, "a class declares at most one finalizer")
317
return
318
fi
319
320
owner.finalizer = cast Semantic.Symbols.Function?(function.root_specialized_from) ?? function
321
else
322
_logger.error(pragma.name.location, "finalizer applies only to an instance method of a non-generic class, taking no arguments")
323
fi
324
si
325
326
// ObsoleteAttribute's arguments, in constructor order: a message,
327
// then whether a use is an error. Either can be left out.
328
_deprecation_of(arguments: Collections.List[IR.Values.Value]) -> Semantic.DEPRECATION is
329
let message =
330
if arguments.count > 0 then
331
if let text: IR.Values.Literal.STRING = arguments[0] then text.value else null fi
332
else
333
null
334
fi
335
336
let is_error =
337
if arguments.count > 1 then
338
if let number: IR.Values.Literal.NUMBER = arguments[1] then
339
if let i4: IR.Values.Literal.CONSTANT.I4 = number.constant then i4.value != 0 else false fi
340
else
341
false
342
fi
343
else
344
false
345
fi
346
347
return Semantic.DEPRECATION(message, is_error)
348
si
349
350
// Every type in the signature has to be one the runtime can
351
// carry into the library. Reported at the method, naming the
352
// type that cannot cross.
353
_check_pinvoke_signature(
354
pragma: Trees.Pragmas.PRAGMA,
355
function: Semantic.Symbols.Function
356
) -> bool is
357
let signature = Semantic.PINVOKE_SIGNATURE(_innate_symbol_lookup)
358
359
let names = function.argument_names
360
361
for i in 0..function.arguments.count do
362
if !signature.can_take(function.arguments[i]) then
363
_logger.error(
364
pragma.name.location,
365
"{names[i]} is a {function.arguments[i]}, which cannot be passed to a shared library")
366
367
return false
368
fi
369
od
370
371
if !signature.can_return(function.return_type) then
372
_logger.error(
373
pragma.name.location,
374
"{function.return_type} cannot be returned from a shared library")
375
376
return false
377
fi
378
379
return true
380
si
381
382
// The attribute type itself, resolved once through the symbol
383
// table and compared by identity: a type is recognised by which
384
// symbol it is rather than by what it renders as, so a user type
385
// that happens to qualify the same way is not taken for this one
386
// and a change to how a type is spelled cannot quietly stop it
387
// being recognised.
388
_dll_import: Semantic.Symbols.Symbol?
389
_dll_import_resolved: bool
390
391
_dll_import_attribute() -> Semantic.Symbols.Symbol? is
392
if _dll_import_resolved then
393
return _dll_import
394
fi
395
396
_dll_import_resolved = true
397
398
let system = _visitor.find("System")
399
400
if !system? then
401
return null
402
fi
403
404
let runtime = system.find_member("Runtime")
405
406
if !runtime? then
407
return null
408
fi
409
410
let interop = runtime.find_member("InteropServices")
411
412
if !interop? then
413
return null
414
fi
415
416
_dll_import = interop.find_member("DllImportAttribute")
417
418
return _dll_import
419
si
420
421
_method_impl: Semantic.Symbols.Symbol?
422
_method_impl_resolved: bool
423
424
_method_impl_attribute() -> Semantic.Symbols.Symbol? is
425
if _method_impl_resolved then
426
return _method_impl
427
fi
428
429
_method_impl_resolved = true
430
431
let system = _visitor.find("System")
432
433
if !system? then
434
return null
435
fi
436
437
let runtime = system.find_member("Runtime")
438
439
if !runtime? then
440
return null
441
fi
442
443
let compiler_services = runtime.find_member("CompilerServices")
444
445
if !compiler_services? then
446
return null
447
fi
448
449
_method_impl = compiler_services.find_member("MethodImplAttribute")
450
451
return _method_impl
452
si
453
454
// Reads a `MethodImpl` into the flags the method row carries.
455
// `MethodImplOptions` and `MethodImplAttributes` number the same
456
// bits, so the value the attribute names is the value the row
457
// takes.
458
@IF.dotnet()
459
_resolve_method_impl(
460
pragma: Trees.Pragmas.PRAGMA,
461
target: Semantic.Symbols.Symbol,
462
positional: Collections.List[IR.Values.Value]
463
) is
464
let function = cast Semantic.Symbols.Function?(target)
465
466
if !function? then
467
_logger.error(pragma.name.location, "only a method can carry MethodImpl")
468
return
469
fi
470
471
// Written with no options at all, the attribute asks for
472
// nothing and the row keeps its defaults. Written with one
473
// this cannot read, saying so beats leaving the method with
474
// the flags it would have had anyway.
475
if positional.count == 0 then
476
return
477
fi
478
479
let options = _number_argument(positional, 0)
480
481
if !options? then
482
_logger.error(pragma.name.location, "cannot read the options this MethodImpl names")
483
return
484
fi
485
486
function.method_impl_attributes =
487
cast System.Reflection.MethodImplAttributes(options)
488
si
489
490
_interop_services: Semantic.Symbols.Symbol?
491
_interop_services_resolved: bool
492
493
_interop_services_namespace() -> Semantic.Symbols.Symbol? is
494
if !_interop_services_resolved then
495
_interop_services_resolved = true
496
_interop_services = _visitor.find("System")?.find_member("Runtime")?.find_member("InteropServices")
497
fi
498
499
return _interop_services
500
si
501
502
_struct_layout: Semantic.Symbols.Symbol?
503
_struct_layout_resolved: bool
504
505
_struct_layout_attribute() -> Semantic.Symbols.Symbol? is
506
if !_struct_layout_resolved then
507
_struct_layout_resolved = true
508
_struct_layout = _interop_services_namespace()?.find_member("StructLayoutAttribute")
509
fi
510
511
return _struct_layout
512
si
513
514
_field_offset: Semantic.Symbols.Symbol?
515
_field_offset_resolved: bool
516
517
_field_offset_attribute() -> Semantic.Symbols.Symbol? is
518
if !_field_offset_resolved then
519
_field_offset_resolved = true
520
_field_offset = _interop_services_namespace()?.find_member("FieldOffsetAttribute")
521
fi
522
523
return _field_offset
524
si
525
526
// Reads a `StructLayout` into the layout the type is emitted with.
527
// A type's own pragmas are resolved after its members', so every
528
// `FieldOffset` in it is already recorded, and an explicit layout
529
// that leaves a field without one - which the runtime refuses to
530
// load - is reported here.
531
@IF.dotnet()
532
_resolve_struct_layout(
533
pragma: Trees.Pragmas.PRAGMA,
534
target: Semantic.Symbols.Symbol,
535
positional: Collections.List[IR.Values.Value],
536
named: Collections.List[Semantic.NAMED_ATTRIBUTE_ARGUMENT]
537
) is
538
let type = cast Semantic.Symbols.Classy?(target)
539
540
if
541
!type? \/
542
type.is_trait \/
543
isa Semantic.Symbols.UNION(type) \/
544
isa Semantic.Symbols.VARIANT(type) \/
545
isa Semantic.Symbols.ENUM_STRUCT(type)
546
then
547
_logger.error(pragma.name.location, "only a class or a struct can carry StructLayout")
548
return
549
fi
550
551
let layout =
552
Semantic.TYPE_LAYOUT(
553
cast System.Runtime.InteropServices.LayoutKind(_number_argument(positional, 0) ?? 0),
554
System.Math.max(_named_number(named, "Pack"), 0),
555
System.Math.max(_named_number(named, "Size"), 0),
556
System.Math.max(_named_number(named, "CharSet"), 0))
557
558
type.layout = layout
559
560
_settle_offsets(type, layout.is_explicit)
561
562
if layout.is_explicit then
563
let missing = Collections.LIST[string]()
564
565
for symbol in type.symbols do
566
if let `field = _instance_field(symbol) /\ `field.owner == type /\ !`field.layout_offset? then
567
missing.add(if `field.name.starts_with("$") then `field.name.substring(1) else `field.name fi)
568
fi
569
od
570
571
if missing.count > 0 then
572
missing.sort()
573
574
_logger.error(
575
pragma.name.location,
576
"explicit layout needs a FieldOffset on {missing |> Ghul.Pipes.join(", ")}")
577
fi
578
fi
579
si
580
581
// A field each instance holds its own copy of, in a class or in a
582
// struct: the only kind a layout places.
583
_instance_field(symbol: Semantic.Symbols.Symbol) -> Semantic.Symbols.Field? =>
584
if isa Semantic.Symbols.INSTANCE_FIELD(symbol) \/ isa Semantic.Symbols.STRUCT_FIELD(symbol) then
585
cast Semantic.Symbols.Field(symbol)
586
else
587
null
588
fi
589
590
_resolve_field_offset(
591
pragma: Trees.Pragmas.PRAGMA,
592
target: Semantic.Symbols.Symbol,
593
positional: Collections.List[IR.Values.Value]
594
) is
595
let `field = _instance_field(target)
596
597
if !`field? then
598
_logger.error(pragma.name.location, "only an instance field can carry FieldOffset")
599
return
600
fi
601
602
`field.layout_offset = _number_argument(positional, 0)
603
604
_unsettled_offsets[`field] = pragma.name.location
605
si
606
607
// Offsets whose type has not yet said whether it is laid out
608
// explicitly. A type's own pragmas are resolved after its members',
609
// so its StructLayout settles them, and any left when the type's file
610
// has been walked belong to a type that never asked for explicit
611
// layout.
612
_unsettled_offsets: Collections.MAP[Semantic.Symbols.Field, Source.LOCATION]
613
614
_settle_offsets(type: Semantic.Symbols.Classy, explicit: bool) is
615
let settled = Collections.LIST[Semantic.Symbols.Field]()
616
617
for (`field, location) in _unsettled_offsets do
618
if `field.owner == type then
619
settled.add(`field)
620
621
if !explicit then
622
_report_offset_without_explicit_layout(location)
623
fi
624
fi
625
od
626
627
for `field in settled do
628
_unsettled_offsets.remove(`field)
629
od
630
si
631
632
// Reports the offsets still unsettled on types declared in
633
// `file_name`, whose pragmas have all been resolved by now.
634
report_unsettled_offsets(file_name: string) is
635
let reported = Collections.LIST[Semantic.Symbols.Field]()
636
637
for (`field, location) in _unsettled_offsets do
638
if let owner: Semantic.Symbols.Symbol = `field.owner /\ owner.location.file_name =~ file_name then
639
reported.add(`field)
640
_report_offset_without_explicit_layout(location)
641
fi
642
od
643
644
for `field in reported do
645
_unsettled_offsets.remove(`field)
646
od
647
si
648
649
_report_offset_without_explicit_layout(location: Source.LOCATION) is
650
_logger.error(location, "a field with a FieldOffset needs a type with explicit layout")
651
si
652
653
// Reads a `DllImport` into the import the method is emitted
654
// from, and reports the declarations it cannot be written on.
655
// Reported here rather than at emission, so a program that
656
// cannot be called into says so before any IL is written.
657
@IF.dotnet()
658
_resolve_pinvoke(
659
pragma: Trees.Pragmas.PRAGMA,
660
target: Semantic.Symbols.Symbol,
661
positional: Collections.List[IR.Values.Value],
662
named: Collections.List[Semantic.NAMED_ATTRIBUTE_ARGUMENT]
663
) is
664
let function = cast Semantic.Symbols.Function?(target)
665
666
if !function? then
667
_logger.error(pragma.name.location, "only a method can carry DllImport")
668
return
669
fi
670
671
if function.is_instance then
672
_logger.error(pragma.name.location, "a method carrying DllImport must be static")
673
return
674
fi
675
676
let module = _string_argument(positional, 0)
677
678
if !module? \/ module.length == 0 then
679
_logger.error(pragma.name.location, "the module to call into is missing from this DllImport")
680
return
681
fi
682
683
if !function.is_declared_without_body then
684
_logger.error(
685
pragma.name.location,
686
"a method carrying DllImport is the call into the library, so it takes no body of its own")
687
return
688
fi
689
690
if !_check_pinvoke_signature(pragma, function) then
691
return
692
fi
693
694
let entry_point = _named_string(named, "EntryPoint") ?? function.name
695
696
function.pinvoke =
697
Semantic.PINVOKE_IMPORT(module, entry_point, _import_attributes(named))
698
si
699
700
// The flags the runtime marshals the call by, read off the
701
// attribute's named arguments. What is not written takes the
702
// platform's default, which is what leaving the bit clear says.
703
@IF.dotnet()
704
_import_attributes(
705
named: Collections.List[Semantic.NAMED_ATTRIBUTE_ARGUMENT]
706
) -> System.Reflection.MethodImportAttributes is
707
let flags mut = 0
708
709
if _named_number(named, "SetLastError") == 1 then
710
flags = flags | cast int(System.Reflection.MethodImportAttributes.SET_LAST_ERROR)
711
fi
712
713
if _named_number(named, "ExactSpelling") == 1 then
714
flags = flags | cast int(System.Reflection.MethodImportAttributes.EXACT_SPELLING)
715
fi
716
717
case _named_number(named, "CharSet")
718
when 2 then
719
flags = flags | cast int(System.Reflection.MethodImportAttributes.CHAR_SET_ANSI)
720
when 3 then
721
flags = flags | cast int(System.Reflection.MethodImportAttributes.CHAR_SET_UNICODE)
722
when 4 then
723
flags = flags | cast int(System.Reflection.MethodImportAttributes.CHAR_SET_AUTO)
724
else
725
esac
726
727
case _named_number(named, "CallingConvention")
728
when 1 then
729
flags = flags | cast int(System.Reflection.MethodImportAttributes.CALLING_CONVENTION_WIN_API)
730
when 2 then
731
flags = flags | cast int(System.Reflection.MethodImportAttributes.CALLING_CONVENTION_C_DECL)
732
when 3 then
733
flags = flags | cast int(System.Reflection.MethodImportAttributes.CALLING_CONVENTION_STD_CALL)
734
when 4 then
735
flags = flags | cast int(System.Reflection.MethodImportAttributes.CALLING_CONVENTION_THIS_CALL)
736
when 5 then
737
flags = flags | cast int(System.Reflection.MethodImportAttributes.CALLING_CONVENTION_FAST_CALL)
738
else
739
esac
740
741
cast System.Reflection.MethodImportAttributes(flags)
742
si
743
744
_string_argument(values: Collections.List[IR.Values.Value], index: int) -> string? is
745
if index >= values.count then
746
return null
747
fi
748
749
if let literal: IR.Values.Literal.STRING = values[index] then
750
return literal.value
751
fi
752
753
null
754
si
755
756
_number_argument(values: Collections.List[IR.Values.Value], index: int) -> int? is
757
if index >= values.count then
758
return null
759
fi
760
761
if let literal: IR.Values.Literal.NUMBER = values[index] then
762
if let i4: IR.Values.Literal.CONSTANT.I4 = literal.constant then
763
return i4.value
764
fi
765
fi
766
767
null
768
si
769
770
_named_string(
771
named: Collections.List[Semantic.NAMED_ATTRIBUTE_ARGUMENT],
772
il_name: string
773
) -> string? is
774
for argument in named do
775
if argument.il_name =~ il_name then
776
if let literal: IR.Values.Literal.STRING = argument.value then
777
return literal.value
778
fi
779
fi
780
od
781
782
null
783
si
784
785
// A named argument written as a number - an enum member, or a
786
// bool - or -1 when the attribute did not write one.
787
_named_number(
788
named: Collections.List[Semantic.NAMED_ATTRIBUTE_ARGUMENT],
789
il_name: string
790
) -> int is
791
for argument in named do
792
if argument.il_name =~ il_name then
793
if let literal: IR.Values.Literal.NUMBER = argument.value then
794
let rendered = literal.rendered
795
let value mut = 0
796
797
if int.try_parse(rendered, value ref) then
798
return value
799
fi
800
fi
801
fi
802
od
803
804
-1
805
si
806
807
// The constructor's parameters filled out from what the call
808
// site supplied, so that both back ends encode the same
809
// arguments in the same order rather than each deciding what an
810
// omitted trailing parameter meant.
811
//
812
// A parameter beyond what was supplied is only reachable through
813
// resolve_constructor_with_omitted_optionals, which has already
814
// established that every one of them carries a usable literal
815
// default.
816
complete_positional_arguments(
817
constructor: Semantic.Symbols.Function,
818
positional: Collections.List[IR.Values.Value]
819
) -> Collections.List[IR.Values.Value] is
820
let parameters = constructor.arguments
821
let completed = Collections.LIST[IR.Values.Value]()
822
823
for i in 0..parameters.count do
824
if i < positional.count then
825
completed.add(positional[i])
826
else
827
completed.add(
828
DEFAULT_ARGUMENT_VALUES.build(
829
constructor.argument_defaults[i],
830
parameters[i],
831
_innate_symbol_lookup
832
))
833
fi
834
od
835
836
return completed
837
si
838
839
// Every `name = value` argument resolved against the attribute's
840
// own members, or null once one of them has been reported as
841
// naming nothing, naming something that is neither a field nor a
842
// property, or having no usable type.
843
resolve_named_arguments(
844
attribute_type: Semantic.Symbols.Classy,
845
pragma: Trees.Pragmas.PRAGMA
846
) -> Collections.List[Semantic.NAMED_ATTRIBUTE_ARGUMENT]? is
847
let resolved = Collections.LIST[Semantic.NAMED_ATTRIBUTE_ARGUMENT]()
848
849
let named = pragma.named_arguments
850
851
if !named? then
852
return resolved
853
fi
854
855
for named_argument in named do
856
let name = named_argument.name.to_string()
857
858
let member = attribute_type.find_member(name)
859
860
if !member? then
861
_logger.error(
862
named_argument.name.location,
863
"attribute {attribute_type.name} has no field or property {name}")
864
865
return null
866
fi
867
868
let is_field = member.symbol_kind == Semantic.Symbols.SymbolKind.FIELD
869
870
if
871
!is_field /\
872
member.symbol_kind != Semantic.Symbols.SymbolKind.PROPERTY
873
then
874
_logger.error(
875
named_argument.name.location,
876
"{name} is not a field or property of attribute {attribute_type.name}")
877
878
return null
879
fi
880
881
let member_type = member.type
882
883
if !member_type? then
884
_logger.error(
885
named_argument.name.location,
886
"attribute argument {name} has an unsupported type")
887
888
return null
889
fi
890
891
resolved.add(
892
Semantic.NAMED_ATTRIBUTE_ARGUMENT(
893
is_field,
894
member.il_name_override ?? name,
895
member_type,
896
named_argument.value.value,
897
named_argument.value.location))
898
od
899
900
return resolved
901
si
902
903
// Resolve the pragma name to a .NET attribute class, accepting the
904
// `Foo` short form for a `FooAttribute` — whether `Foo` is written
905
// bare or fully qualified (`A.B.Foo` resolves `A.B.FooAttribute`).
906
resolve_attribute_type(name: Trees.Identifiers.Identifier) -> Semantic.Symbols.Classy? is
907
let direct = as_classy(_visitor.try_find(name))
908
909
if direct? then
910
return direct
911
fi
912
913
let suffixed_name = "{name.name}Attribute"
914
915
if name.is_qualified then
916
let qualifier = _visitor.try_find(name.qualifier!)
917
918
if !qualifier? then
919
return null
920
fi
921
922
return as_classy(qualifier.find_member(suffixed_name))
923
fi
924
925
return as_classy(_visitor.find(suffixed_name))
926
si
927
928
as_classy(symbol: Semantic.Symbols.Symbol?) -> Semantic.Symbols.Classy? is
929
if !symbol? then
930
return null
931
fi
932
933
// A name shared across generic arities resolves to a TYPE_GROUP.
934
// Applying an attribute supplies no type arguments, so the
935
// non-generic member is the intended one.
936
if let group: Semantic.Symbols.TYPE_GROUP = symbol then
937
return group.find_by_generic_arguments_count(0)
938
fi
939
940
return cast Semantic.Symbols.Classy?(symbol)
941
si
942
943
gather_positional_arguments(pragma: Trees.Pragmas.PRAGMA) -> Collections.LIST[IR.Values.Value] is
944
let result = Collections.LIST[IR.Values.Value]()
945
946
for argument in pragma.arguments.expressions do
947
if argument.value? then
948
result.add(argument.value)
949
fi
950
od
951
952
return result
953
si
954
955
resolve_constructor(
956
pragma: Trees.Pragmas.PRAGMA,
957
attribute_type: Semantic.Symbols.Classy,
958
positional: Collections.List[IR.Values.Value]
959
) -> Semantic.Symbols.Function? is
960
let init_group = cast Semantic.Symbols.FUNCTION_GROUP?(attribute_type.find_member("init"))
961
962
if !init_group? then
963
_logger.error(pragma.location, "attribute {attribute_type.name} has no usable constructor")
964
return null
965
fi
966
967
let argument_types = Collections.LIST[Type]()
968
969
for argument in positional do
970
if argument.type? then
971
argument_types.add(argument.type)
972
else
973
argument_types.add(Semantic.Types.ERROR())
974
fi
975
od
976
977
_logger.speculate()
978
979
let overload_result = _overload_resolver.resolve(pragma.location, init_group, argument_types, false, true, true)
980
981
if overload_result? then
982
_logger.commit()
983
return overload_result.function
984
fi
985
986
let use retry_site = RETRY_SITE_STATS.enter("attributes.constructor_fallback")
987
let with_empty_arrays = resolve_constructor_with_empty_arrays(init_group, positional, argument_types)
988
989
if with_empty_arrays? then
990
_logger.roll_back()
991
return with_empty_arrays
992
fi
993
994
let with_omitted_optionals = resolve_constructor_with_omitted_optionals(init_group, argument_types)
995
996
if with_omitted_optionals? then
997
_logger.roll_back()
998
return with_omitted_optionals
999
fi
1000
1001
_logger.commit()
1002
_logger.error(pragma.location, "no constructor of attribute {attribute_type.name} matches the supplied arguments")
1003
return null
1004
si
1005
1006
// An empty array literal argument carries no element type of its
1007
// own, so it falls back to object[] and fails to match a more
1008
// specific array parameter. An empty array is compatible with any
1009
// array type, so when the direct resolution fails, retry accepting
1010
// each empty-array argument against any array parameter of a
1011
// uniquely matching constructor. The empty array is then formatted
1012
// with the parameter's element type in format_argument.
1013
resolve_constructor_with_empty_arrays(
1014
init_group: Semantic.Symbols.FUNCTION_GROUP,
1015
positional: Collections.List[IR.Values.Value],
1016
argument_types: Collections.List[Type]
1017
) -> Semantic.Symbols.Function? is
1018
let have_empty_array mut = false
1019
1020
for argument in positional do
1021
if is_empty_array(argument) then
1022
have_empty_array = true
1023
fi
1024
od
1025
1026
if !have_empty_array then
1027
return null
1028
fi
1029
1030
let match: Semantic.Symbols.Function? mut = null
1031
1032
for candidate in init_group.functions do
1033
if candidate.arguments.count != positional.count then
1034
continue
1035
fi
1036
1037
let ok mut = true
1038
1039
for i in 0..positional.count do
1040
let parameter = candidate.arguments[i]
1041
1042
if is_empty_array(positional[i]) then
1043
if !parameter.get_element_type()? then
1044
ok = false
1045
fi
1046
elif !parameter.is_assignable_from(argument_types[i]) then
1047
ok = false
1048
fi
1049
od
1050
1051
if ok then
1052
if match? then
1053
return null
1054
fi
1055
1056
match = candidate
1057
fi
1058
od
1059
1060
return match
1061
si
1062
1063
// ghūl cannot omit an optional parameter in an ordinary call,
1064
// so a constructor called with fewer positional arguments
1065
// than its arity — e.g. an attribute whose .NET constructor
1066
// carries `CallerFilePath`/`CallerLineNumber` defaults —
1067
// would otherwise need every trailing default spelled out at
1068
// the call site. Retry accepting the supplied prefix against
1069
// a uniquely matching constructor whose unsupplied trailing
1070
// parameters all declare a default value; format_arguments
1071
// fills those defaults in.
1072
//
1073
// Only a concrete literal default qualifies — the "default"
1074
// sentinel (a ghūl source `= _` parameter, or a reflected
1075
// parameter whose CLR default is itself `default(T)`) is
1076
// excluded, since format_argument has no ilasm literal to
1077
// render it as for an arbitrary parameter type.
1078
resolve_constructor_with_omitted_optionals(
1079
init_group: Semantic.Symbols.FUNCTION_GROUP,
1080
argument_types: Collections.List[Type]
1081
) -> Semantic.Symbols.Function? is
1082
let match: Semantic.Symbols.Function? mut = null
1083
1084
for candidate in init_group.functions do
1085
if candidate.arguments.count <= argument_types.count then
1086
continue
1087
fi
1088
1089
let ok mut = true
1090
1091
for i in 0..argument_types.count do
1092
if !candidate.arguments[i].is_assignable_from(argument_types[i]) then
1093
ok = false
1094
fi
1095
od
1096
1097
if ok then
1098
for i in argument_types.count..candidate.arguments.count do
1099
if
1100
i >= candidate.argument_defaults.count \/
1101
!candidate.argument_defaults[i]? \/
1102
candidate.argument_defaults[i] =~ "default"
1103
then
1104
ok = false
1105
fi
1106
od
1107
fi
1108
1109
if ok then
1110
if match? then
1111
return null
1112
fi
1113
1114
match = candidate
1115
fi
1116
od
1117
1118
return match
1119
si
1120
1121
is_empty_array(value: IR.Values.Value) -> bool is
1122
if let sequence: IR.Values.SEQUENCE = value then
1123
return sequence.values.count == 0
1124
fi
1125
1126
return false
1127
si
1128
1129
// The ilasm textual custom-attribute body: `( 01 00 00 00 )` when
1130
// there are no arguments at all, otherwise `{ <fixed>... <named>... }`.
1131
// Returns null if any argument is not a compile-time constant.
1132
format_arguments(
1133
pragma: Trees.Pragmas.PRAGMA,
1134
constructor: Semantic.Symbols.Function,
1135
positional: Collections.List[IR.Values.Value],
1136
named: Collections.List[Semantic.NAMED_ATTRIBUTE_ARGUMENT]
1137
) -> string? is
1138
let have_named = named.count > 0
1139
1140
if constructor.arguments.count == 0 /\ !have_named then
1141
return "( 01 00 00 00 )"
1142
fi
1143
1144
let parameter_types = constructor.arguments
1145
1146
let buffer = StringBuilder()
1147
1148
buffer.append("{{")
1149
1150
for i in 0..parameter_types.count do
1151
let formatted = format_argument(parameter_types[i], positional[i])
1152
1153
if !formatted? then
1154
_logger.error(pragma.location, "attribute argument {i + 1} is not a compile-time constant")
1155
return null
1156
fi
1157
1158
buffer.append(" ").append(formatted)
1159
od
1160
1161
if have_named then
1162
for named_argument in named do
1163
let formatted = format_named_argument(named_argument)
1164
1165
if !formatted? then
1166
return null
1167
fi
1168
1169
buffer.append(" ").append(formatted)
1170
od
1171
fi
1172
1173
buffer.append(" }}")
1174
1175
return buffer.to_string()
1176
si
1177
1178
// One `name = value` argument in the ilasm textual form —
1179
// `property <type> 'Name' = <value>` or `field <type> 'Name' = ...`.
1180
// Returns null (after logging) if the member's type or the value
1181
// cannot be rendered.
1182
format_named_argument(
1183
named_argument: Semantic.NAMED_ATTRIBUTE_ARGUMENT
1184
) -> string? is
1185
let name = named_argument.il_name
1186
1187
let keyword = if named_argument.is_field then "field" else "property" fi
1188
1189
let type_token = named_argument_type_token(named_argument.member_type)
1190
1191
if !type_token? then
1192
_logger.error(named_argument.location, "attribute argument {name} has an unsupported type")
1193
return null
1194
fi
1195
1196
let formatted_value =
1197
format_argument(named_argument.member_type, named_argument.value)
1198
1199
if !formatted_value? then
1200
_logger.error(named_argument.location, "attribute argument {name} is not a compile-time constant")
1201
return null
1202
fi
1203
1204
return "{keyword} {type_token} '{name}' = {formatted_value}"
1205
si
1206
1207
// One positional or named-value argument in the ilasm textual
1208
// form, or null if `value` is not a supported compile-time
1209
// constant — a string, a number (optionally negated), a bool, an
1210
// enum member, `null`, or an array of those.
1211
format_argument(parameter_type: Type?, value: IR.Values.Value?) -> string? is
1212
if !value? then
1213
return null
1214
fi
1215
1216
if parameter_type? /\ parameter_type.matches(_innate_symbol_lookup.get_object_type()) then
1217
let inner = format_typed_value(value)
1218
1219
if inner? then
1220
return "object({inner})"
1221
fi
1222
1223
return null
1224
fi
1225
1226
if isa IR.Values.NULL(value) then
1227
if parameter_type? /\ parameter_type.matches(_innate_symbol_lookup.get_string_type()) then
1228
return "string(nullref)"
1229
fi
1230
1231
return null
1232
fi
1233
1234
if let string_value: IR.Values.Literal.STRING = value then
1235
return "string({quote(string_value.value)})"
1236
fi
1237
1238
let number_text = try_get_number_text(value)
1239
1240
if number_text? then
1241
let keyword = serialization_keyword(parameter_type)
1242
1243
if keyword =~ "bool" then
1244
return "bool({number_text_to_bool(number_text)})"
1245
fi
1246
1247
return "{keyword}({number_text})"
1248
fi
1249
1250
if let sequence: IR.Values.SEQUENCE = value then
1251
// An empty array literal carried no elements to infer from
1252
// and fell back to object[]; take its element type from the
1253
// parameter instead.
1254
if sequence.values.count == 0 /\ parameter_type? then
1255
let element_type = parameter_type.get_element_type()
1256
1257
if element_type? then
1258
let element_token = named_argument_type_token(element_type)
1259
1260
if element_token? then
1261
return "{element_token}[0]()"
1262
fi
1263
fi
1264
fi
1265
1266
return format_array(sequence)
1267
fi
1268
1269
if let typeof_value: IR.Values.TYPEOF = value then
1270
// A typeof argument can name any type; only a class/struct/
1271
// enum or an array of one serializes to a blob name, so a
1272
// typeof of anything else is rejected here rather than
1273
// reaching the encoder as a name it cannot serialize.
1274
if _typeof_arg_is_serializable(typeof_value.typeof_type) then
1275
return "type"
1276
fi
1277
1278
return null
1279
fi
1280
1281
return null
1282
si
1283
1284
// The ilasm `<element>[N](e0 e1 ...)` form for an array argument.
1285
format_array(sequence: IR.Values.SEQUENCE) -> string? is
1286
let element_token = named_argument_type_token(sequence.element_type)
1287
1288
if !element_token? then
1289
return null
1290
fi
1291
1292
let buffer = StringBuilder()
1293
1294
buffer
1295
.append(element_token)
1296
.append("[")
1297
.append(sequence.values.count)
1298
.append("](")
1299
1300
for i in 0..sequence.values.count do
1301
if i > 0 then
1302
buffer.append(" ")
1303
fi
1304
1305
let element = format_array_element(sequence.element_type, sequence.values[i])
1306
1307
if !element? then
1308
return null
1309
fi
1310
1311
buffer.append(element)
1312
od
1313
1314
buffer.append(")")
1315
1316
return buffer.to_string()
1317
si
1318
1319
// A single array element — the bare form, without the
1320
// `keyword(...)` wrapper that a scalar argument carries. An
1321
// element of an `object[]` is itself self-describing, so for
1322
// an `object`-typed element type each element is emitted in its
1323
// value-typed form (`int32(5)`, `string('x')`, …) rather than
1324
// bare — that is what `format_typed_value` produces.
1325
format_array_element(element_type: Type?, value: IR.Values.Value) -> string? is
1326
if element_type? /\ element_type.matches(_innate_symbol_lookup.get_object_type()) then
1327
return format_typed_value(value)
1328
fi
1329
1330
if let string_value: IR.Values.Literal.STRING = value then
1331
return quote(string_value.value)
1332
fi
1333
1334
let number_text = try_get_number_text(value)
1335
1336
if number_text? then
1337
if serialization_keyword(element_type) =~ "bool" then
1338
return number_text_to_bool(number_text)
1339
fi
1340
1341
return number_text
1342
fi
1343
1344
return null
1345
si
1346
1347
// The ilasm typed self-describing form for a value, used as the
1348
// inner of an `object(...)` wrapper and as each element of an
1349
// `object[]` array. Keys off the value's own type rather than
1350
// any enclosing slot type, because that is exactly the
1351
// information that has to be re-asserted inline when the slot's
1352
// declared type is `System.Object`.
1353
format_typed_value(value: IR.Values.Value) -> string? is
1354
if isa IR.Values.NULL(value) then
1355
// No top-level `nullref` form inside ilasm's `object(...)`;
1356
// a null reference is encoded as a null string (the form
1357
// C# emits for `[Foo((object)null)]`).
1358
return "string(nullref)"
1359
fi
1360
1361
if let string_value: IR.Values.Literal.STRING = value then
1362
return "string({quote(string_value.value)})"
1363
fi
1364
1365
let number_text = try_get_number_text(value)
1366
1367
if number_text? then
1368
if value.type? then
1369
let primitive = try_primitive_keyword(value.type)
1370
1371
if primitive? then
1372
if primitive =~ "bool" then
1373
return "bool({number_text_to_bool(number_text)})"
1374
fi
1375
1376
return "{primitive}({number_text})"
1377
fi
1378
1379
// An enum-typed value inside an `object` slot would
1380
// require encoding the enum's type-name SerString
1381
// into the blob, and ilasm's `{...}` grammar has no
1382
// production for `enum` inside `object(...)` or as
1383
// an object-array element — the whole `.custom`
1384
// would have to be emitted as raw bytes. Reject for
1385
// now; the caller surfaces "not a compile-time
1386
// constant".
1387
fi
1388
1389
return null
1390
fi
1391
1392
if let typeof_value: IR.Values.TYPEOF = value then
1393
// A typeof argument can name any type; only a class/struct/
1394
// enum or an array of one serializes to a blob name, so a
1395
// typeof of anything else is rejected here rather than
1396
// reaching the encoder as a name it cannot serialize.
1397
if _typeof_arg_is_serializable(typeof_value.typeof_type) then
1398
return "type"
1399
fi
1400
1401
return null
1402
fi
1403
1404
if let sequence: IR.Values.SEQUENCE = value then
1405
return format_array(sequence)
1406
fi
1407
1408
return null
1409
si
1410
1411
// The numeric text of a NUMBER literal, or of a unary minus
1412
// applied to one — `-5` parses as a `-` innate over `5`, not as a
1413
// negative literal — or null if `value` is not a numeric constant.
1414
try_get_number_text(value: IR.Values.Value) -> string? is
1415
if let number: IR.Values.Literal.NUMBER = value then
1416
return number.rendered
1417
fi
1418
1419
if let innate_call: IR.Values.Call.INNATE = value then
1420
if
1421
innate_call.function.name =~ "-" /\
1422
innate_call.arguments.count == 1
1423
then
1424
if let number: IR.Values.Literal.NUMBER = innate_call.arguments[0] then
1425
return "-{number.rendered}"
1426
fi
1427
fi
1428
fi
1429
1430
return null
1431
si
1432
1433
number_text_to_bool(number_text: string) -> string =>
1434
if number_text =~ "0" then "false" else "true" fi
1435
1436
// The ilasm type token for a named-argument member, an array
1437
// member, or an array element — a primitive keyword, `string`,
1438
// `enum <name>`, or `<element>[]`; null if the type cannot be
1439
// encoded into a custom-attribute blob from a textual literal.
1440
named_argument_type_token(type: Type?) -> string? is
1441
if !type? then
1442
return null
1443
fi
1444
1445
if type.matches(_innate_symbol_lookup.get_string_type()) then
1446
return "string"
1447
fi
1448
1449
if type.matches(_innate_symbol_lookup.get_type_type()) then
1450
return "type"
1451
fi
1452
1453
if type.matches(_innate_symbol_lookup.get_object_type()) then
1454
return "object"
1455
fi
1456
1457
let primitive = try_primitive_keyword(type)
1458
1459
if primitive? then
1460
return primitive
1461
fi
1462
1463
if let array: Semantic.Types.ARRAY = type then
1464
let element_token = named_argument_type_token(array.arguments[0])
1465
1466
if element_token? then
1467
return "{element_token}[]"
1468
fi
1469
1470
return null
1471
fi
1472
1473
if type.symbol.symbol_kind == Semantic.Symbols.SymbolKind.ENUM then
1474
if let classy: Semantic.Symbols.Classy = type.symbol then
1475
if classy.il_assembly_name? then
1476
return "enum [{classy.il_assembly_name}]{classy.qualified_name}"
1477
fi
1478
1479
return "enum {classy.qualified_name}"
1480
fi
1481
fi
1482
1483
return null
1484
si
1485
1486
// Whether a typeof argument's type can be serialized into an
1487
// attribute blob. The answer is the serializer's own - the same
1488
// one the encoder uses - so what is accepted here is exactly
1489
// what the blob can spell, and a typeof of anything else (a
1490
// pointer, a type parameter) is rejected as a diagnostic rather
1491
// than reaching the encoder.
1492
@IF.dotnet()
1493
_typeof_arg_is_serializable(type: Type?) -> bool is
1494
if !type? then
1495
return false
1496
fi
1497
1498
return Semantic.DotNet.REFLECTION_TYPE_NAME.try_serialize(type)?
1499
si
1500
1501
// A target with no assembly writes no attribute blob, so any type a
1502
// typeof names can be its argument.
1503
@IF.not.dotnet()
1504
_typeof_arg_is_serializable(type: Type?) -> bool => type?
1505
1506
// The serialization keyword for a primitive value type, or null
1507
// for anything that is not a primitive. The keyword itself
1508
// reaches only discarded text; what matters is the null/non-null
1509
// answer, which gates whether the attribute argument is accepted.
1510
// Classification is by type identity, matching the binary blob
1511
// encoder, not by rendering the type and string-matching.
1512
try_primitive_keyword(type: Type?) -> string? is
1513
if !type? then
1514
return null
1515
fi
1516
1517
let t = type
1518
1519
if t.matches(_innate_symbol_lookup.get_bool_type()) then return "bool"; fi
1520
if t.matches(_innate_symbol_lookup.get_char_type()) then return "char"; fi
1521
if t.matches(_innate_symbol_lookup.get_byte_type()) then return "int8"; fi
1522
if t.matches(_innate_symbol_lookup.get_ubyte_type()) then return "uint8"; fi
1523
if t.matches(_innate_symbol_lookup.get_short_type()) then return "int16"; fi
1524
if t.matches(_innate_symbol_lookup.get_ushort_type()) then return "uint16"; fi
1525
if t.matches(_innate_symbol_lookup.get_int_type()) then return "int32"; fi
1526
if t.matches(_innate_symbol_lookup.get_uint_type()) then return "uint32"; fi
1527
if t.matches(_innate_symbol_lookup.get_long_type()) then return "int64"; fi
1528
if t.matches(_innate_symbol_lookup.get_ulong_type()) then return "uint64"; fi
1529
if t.matches(_innate_symbol_lookup.get_single_type()) then return "float32"; fi
1530
if t.matches(_innate_symbol_lookup.get_double_type()) then return "float64"; fi
1531
1532
return null
1533
si
1534
1535
// The ilasm serialization keyword for a numeric or bool value:
1536
// the parameter type's primitive keyword, or `int32` for an enum,
1537
// whose fixed-argument form encodes the underlying value.
1538
serialization_keyword(parameter_type: Type?) -> string is
1539
let primitive = try_primitive_keyword(parameter_type)
1540
1541
if primitive? then
1542
return primitive
1543
fi
1544
1545
return "int32"
1546
si
1547
1548
quote(value: string) -> string =>
1549
Semantic.DotNet.IL_ATTRIBUTE_ARGUMENTS.quote(value)
1550
si
1551
si