-
Notifications
You must be signed in to change notification settings - Fork 856
Expand file tree
/
Copy pathTypedTreeOps.Attributes.fs
More file actions
2545 lines (2121 loc) · 109 KB
/
TypedTreeOps.Attributes.fs
File metadata and controls
2545 lines (2121 loc) · 109 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
/// TypedTreeOps.Attributes: IL extensions, attribute helpers, and debug printing.
namespace FSharp.Compiler.TypedTreeOps
open System
open System.CodeDom.Compiler
open System.Collections.Generic
open System.Collections.Immutable
open Internal.Utilities
open Internal.Utilities.Collections
open Internal.Utilities.Library
open Internal.Utilities.Library.Extras
open Internal.Utilities.Rational
open FSharp.Compiler
open FSharp.Compiler.IO
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.CompilerGlobalState
open FSharp.Compiler.DiagnosticsLogger
open FSharp.Compiler.Features
open FSharp.Compiler.Syntax
open FSharp.Compiler.Syntax.PrettyNaming
open FSharp.Compiler.SyntaxTreeOps
open FSharp.Compiler.TcGlobals
open FSharp.Compiler.Text
open FSharp.Compiler.Text.Range
open FSharp.Compiler.Text.Layout
open FSharp.Compiler.Text.LayoutRender
open FSharp.Compiler.Text.TaggedText
open FSharp.Compiler.Xml
open FSharp.Compiler.TypedTree
open FSharp.Compiler.TypedTreeBasics
#if !NO_TYPEPROVIDERS
open FSharp.Compiler.TypeProviders
#endif
[<AutoOpen>]
module internal ILExtensions =
//----------------------------------------------------------------------------
// Detect attributes
//----------------------------------------------------------------------------
// AbsIL view of attributes (we read these from .NET binaries)
let isILAttribByName (tencl: string list, tname: string) (attr: ILAttribute) =
(attr.Method.DeclaringType.TypeSpec.Name = tname)
&& (attr.Method.DeclaringType.TypeSpec.Enclosing = tencl)
// AbsIL view of attributes (we read these from .NET binaries). The comparison is done by name.
let isILAttrib (tref: ILTypeRef) (attr: ILAttribute) =
isILAttribByName (tref.Enclosing, tref.Name) attr
// REVIEW: consider supporting querying on Abstract IL custom attributes.
// These linear iterations cost us a fair bit when there are lots of attributes
// on imported types. However this is fairly rare and can also be solved by caching the
// results of attribute lookups in the TAST
let HasILAttribute tref (attrs: ILAttributes) =
attrs.AsArray() |> Array.exists (isILAttrib tref)
let TryDecodeILAttribute tref (attrs: ILAttributes) =
attrs.AsArray()
|> Array.tryPick (fun x ->
if isILAttrib tref x then
Some(decodeILAttribData x)
else
None)
// F# view of attributes (these get converted to AbsIL attributes in ilxgen)
let IsMatchingFSharpAttribute g (AttribInfo(_, tcref)) (Attrib(tcref2, _, _, _, _, _, _)) = tyconRefEq g tcref tcref2
let HasFSharpAttribute g tref attrs =
List.exists (IsMatchingFSharpAttribute g tref) attrs
let TryFindFSharpAttribute g tref attrs =
List.tryFind (IsMatchingFSharpAttribute g tref) attrs
[<return: Struct>]
let (|ExtractAttribNamedArg|_|) nm args =
args
|> List.tryPick (function
| AttribNamedArg(nm2, _, _, v) when nm = nm2 -> Some v
| _ -> None)
|> ValueOption.ofOption
[<return: Struct>]
let (|ExtractILAttributeNamedArg|_|) nm (args: ILAttributeNamedArg list) =
args
|> List.tryPick (function
| nm2, _, _, v when nm = nm2 -> Some v
| _ -> None)
|> ValueOption.ofOption
[<return: Struct>]
let (|StringExpr|_|) =
function
| Expr.Const(Const.String n, _, _) -> ValueSome n
| _ -> ValueNone
[<return: Struct>]
let (|AttribInt32Arg|_|) =
function
| AttribExpr(_, Expr.Const(Const.Int32 n, _, _)) -> ValueSome n
| _ -> ValueNone
[<return: Struct>]
let (|AttribInt16Arg|_|) =
function
| AttribExpr(_, Expr.Const(Const.Int16 n, _, _)) -> ValueSome n
| _ -> ValueNone
[<return: Struct>]
let (|AttribBoolArg|_|) =
function
| AttribExpr(_, Expr.Const(Const.Bool n, _, _)) -> ValueSome n
| _ -> ValueNone
[<return: Struct>]
let (|AttribStringArg|_|) =
function
| AttribExpr(_, Expr.Const(Const.String n, _, _)) -> ValueSome n
| _ -> ValueNone
let (|AttribElemStringArg|_|) =
function
| ILAttribElem.String(n) -> n
| _ -> None
let TryFindILAttribute (AttribInfo(atref, _)) attrs = HasILAttribute atref attrs
let IsILAttrib (AttribInfo(builtInAttrRef, _)) attr = isILAttrib builtInAttrRef attr
let inline hasFlag (flags: ^F) (flag: ^F) : bool when ^F: enum<uint64> =
let f = LanguagePrimitives.EnumToValue flags
let v = LanguagePrimitives.EnumToValue flag
f &&& v <> 0uL
/// Compute well-known attribute flags for an ILAttributes collection.
/// Classify a single IL attribute, returning its well-known flag (or None).
let classifyILAttrib (attr: ILAttribute) : WellKnownILAttributes =
let atref = attr.Method.DeclaringType.TypeSpec.TypeRef
if not atref.Enclosing.IsEmpty then
WellKnownILAttributes.None
else
let name = atref.Name
if name.StartsWith("System.Runtime.CompilerServices.") then
match name with
| "System.Runtime.CompilerServices.IsReadOnlyAttribute" -> WellKnownILAttributes.IsReadOnlyAttribute
| "System.Runtime.CompilerServices.IsUnmanagedAttribute" -> WellKnownILAttributes.IsUnmanagedAttribute
| "System.Runtime.CompilerServices.ExtensionAttribute" -> WellKnownILAttributes.ExtensionAttribute
| "System.Runtime.CompilerServices.IsByRefLikeAttribute" -> WellKnownILAttributes.IsByRefLikeAttribute
| "System.Runtime.CompilerServices.InternalsVisibleToAttribute" -> WellKnownILAttributes.InternalsVisibleToAttribute
| "System.Runtime.CompilerServices.CallerMemberNameAttribute" -> WellKnownILAttributes.CallerMemberNameAttribute
| "System.Runtime.CompilerServices.CallerFilePathAttribute" -> WellKnownILAttributes.CallerFilePathAttribute
| "System.Runtime.CompilerServices.CallerLineNumberAttribute" -> WellKnownILAttributes.CallerLineNumberAttribute
| "System.Runtime.CompilerServices.RequiresLocationAttribute" -> WellKnownILAttributes.RequiresLocationAttribute
| "System.Runtime.CompilerServices.NullableAttribute" -> WellKnownILAttributes.NullableAttribute
| "System.Runtime.CompilerServices.NullableContextAttribute" -> WellKnownILAttributes.NullableContextAttribute
| "System.Runtime.CompilerServices.IDispatchConstantAttribute" -> WellKnownILAttributes.IDispatchConstantAttribute
| "System.Runtime.CompilerServices.IUnknownConstantAttribute" -> WellKnownILAttributes.IUnknownConstantAttribute
| "System.Runtime.CompilerServices.SetsRequiredMembersAttribute" -> WellKnownILAttributes.SetsRequiredMembersAttribute
| "System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute" ->
WellKnownILAttributes.CompilerFeatureRequiredAttribute
| "System.Runtime.CompilerServices.RequiredMemberAttribute" -> WellKnownILAttributes.RequiredMemberAttribute
| _ -> WellKnownILAttributes.None
elif name.StartsWith("Microsoft.FSharp.Core.") then
match name with
| "Microsoft.FSharp.Core.AllowNullLiteralAttribute" -> WellKnownILAttributes.AllowNullLiteralAttribute
| "Microsoft.FSharp.Core.ReflectedDefinitionAttribute" -> WellKnownILAttributes.ReflectedDefinitionAttribute
| "Microsoft.FSharp.Core.AutoOpenAttribute" -> WellKnownILAttributes.AutoOpenAttribute
| "Microsoft.FSharp.Core.CompilerServices.NoEagerConstraintApplicationAttribute" ->
WellKnownILAttributes.NoEagerConstraintApplicationAttribute
| _ -> WellKnownILAttributes.None
else
match name with
| "System.ParamArrayAttribute" -> WellKnownILAttributes.ParamArrayAttribute
| "System.Reflection.DefaultMemberAttribute" -> WellKnownILAttributes.DefaultMemberAttribute
| "System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute" ->
// Also at System.Runtime.CompilerServices (line above); .NET defines it in both namespaces
WellKnownILAttributes.SetsRequiredMembersAttribute
| "System.ObsoleteAttribute" -> WellKnownILAttributes.ObsoleteAttribute
| "System.Diagnostics.CodeAnalysis.ExperimentalAttribute" -> WellKnownILAttributes.ExperimentalAttribute
| "System.AttributeUsageAttribute" -> WellKnownILAttributes.AttributeUsageAttribute
| _ -> WellKnownILAttributes.None
/// Compute well-known attribute flags for an ILAttributes collection.
let computeILWellKnownFlags (_g: TcGlobals) (attrs: ILAttributes) : WellKnownILAttributes =
let mutable flags = WellKnownILAttributes.None
for attr in attrs.AsArray() do
flags <- flags ||| classifyILAttrib attr
flags
/// Find the first IL attribute matching a specific well-known flag and decode it.
let tryFindILAttribByFlag (flag: WellKnownILAttributes) (cattrs: ILAttributes) =
cattrs.AsArray()
|> Array.tryPick (fun attr ->
if classifyILAttrib attr &&& flag <> WellKnownILAttributes.None then
Some(decodeILAttribData attr)
else
None)
/// Active pattern: find and decode a well-known IL attribute.
/// Returns decoded (ILAttribElem list * ILAttributeNamedArg list).
[<return: Struct>]
let (|ILAttribDecoded|_|) (flag: WellKnownILAttributes) (cattrs: ILAttributes) =
tryFindILAttribByFlag flag cattrs |> ValueOption.ofOption
type ILAttributesStored with
member x.HasWellKnownAttribute(g: TcGlobals, flag: WellKnownILAttributes) =
x.HasWellKnownAttribute(flag, computeILWellKnownFlags g)
type ILTypeDef with
member x.HasWellKnownAttribute(g: TcGlobals, flag: WellKnownILAttributes) =
x.CustomAttrsStored.HasWellKnownAttribute(g, flag)
type ILMethodDef with
member x.HasWellKnownAttribute(g: TcGlobals, flag: WellKnownILAttributes) =
x.CustomAttrsStored.HasWellKnownAttribute(g, flag)
type ILFieldDef with
member x.HasWellKnownAttribute(g: TcGlobals, flag: WellKnownILAttributes) =
x.CustomAttrsStored.HasWellKnownAttribute(g, flag)
type ILAttributes with
/// Non-caching (unlike ILAttributesStored.HasWellKnownAttribute which caches).
member x.HasWellKnownAttribute(flag: WellKnownILAttributes) =
x.AsArray()
|> Array.exists (fun attr -> classifyILAttrib attr &&& flag <> WellKnownILAttributes.None)
[<AutoOpen>]
module internal AttributeHelpers =
/// Resolve the FSharp.Core path for an attribute's type reference.
/// Returns struct(bclPath, fsharpCorePath). Exactly one will be ValueSome, or both ValueNone.
let inline resolveAttribPath (g: TcGlobals) (tcref: TyconRef) : struct (string[] voption * string[] voption) =
if not tcref.IsLocalRef then
let nlr = tcref.nlr
if ccuEq nlr.Ccu g.fslibCcu then
struct (ValueNone, ValueSome nlr.Path)
else
struct (ValueSome nlr.Path, ValueNone)
elif g.compilingFSharpCore then
match tcref.Deref.PublicPath with
| Some(PubPath pp) -> struct (ValueNone, ValueSome pp)
| None -> struct (ValueNone, ValueNone)
else
struct (ValueNone, ValueNone)
/// Decode a bool-arg attribute and set the appropriate true/false flag.
let inline decodeBoolAttribFlag (attrib: Attrib) trueFlag falseFlag defaultFlag =
match attrib with
| Attrib(_, _, [ AttribBoolArg b ], _, _, _, _) -> if b then trueFlag else falseFlag
| _ -> defaultFlag
/// Classify a single Entity-level attribute, returning its well-known flag (or None).
let classifyEntityAttrib (g: TcGlobals) (attrib: Attrib) : WellKnownEntityAttributes =
let (Attrib(tcref, _, _, _, _, _, _)) = attrib
let struct (bclPath, fsharpCorePath) = resolveAttribPath g tcref
match bclPath with
| ValueSome path ->
match path with
| [| "System"; "Runtime"; "CompilerServices"; name |] ->
match name with
| "ExtensionAttribute" -> WellKnownEntityAttributes.ExtensionAttribute
| "IsReadOnlyAttribute" -> WellKnownEntityAttributes.IsReadOnlyAttribute
| "SkipLocalsInitAttribute" -> WellKnownEntityAttributes.SkipLocalsInitAttribute
| "IsByRefLikeAttribute" -> WellKnownEntityAttributes.IsByRefLikeAttribute
| _ -> WellKnownEntityAttributes.None
| [| "System"; "Runtime"; "InteropServices"; name |] ->
match name with
| "StructLayoutAttribute" -> WellKnownEntityAttributes.StructLayoutAttribute
| "DllImportAttribute" -> WellKnownEntityAttributes.DllImportAttribute
| "ComVisibleAttribute" ->
decodeBoolAttribFlag
attrib
WellKnownEntityAttributes.ComVisibleAttribute_True
WellKnownEntityAttributes.ComVisibleAttribute_False
WellKnownEntityAttributes.ComVisibleAttribute_True
| "ComImportAttribute" ->
decodeBoolAttribFlag
attrib
WellKnownEntityAttributes.ComImportAttribute_True
WellKnownEntityAttributes.None
WellKnownEntityAttributes.ComImportAttribute_True
| _ -> WellKnownEntityAttributes.None
| [| "System"; "Diagnostics"; name |] ->
match name with
| "DebuggerDisplayAttribute" -> WellKnownEntityAttributes.DebuggerDisplayAttribute
| "DebuggerTypeProxyAttribute" -> WellKnownEntityAttributes.DebuggerTypeProxyAttribute
| _ -> WellKnownEntityAttributes.None
| [| "System"; "ComponentModel"; name |] ->
match name with
| "EditorBrowsableAttribute" -> WellKnownEntityAttributes.EditorBrowsableAttribute
| _ -> WellKnownEntityAttributes.None
| [| "System"; name |] ->
match name with
| "AttributeUsageAttribute" -> WellKnownEntityAttributes.AttributeUsageAttribute
| "ObsoleteAttribute" -> WellKnownEntityAttributes.ObsoleteAttribute
| _ -> WellKnownEntityAttributes.None
| _ -> WellKnownEntityAttributes.None
| ValueNone ->
match fsharpCorePath with
| ValueSome path ->
match path with
| [| "Microsoft"; "FSharp"; "Core"; name |] ->
match name with
| "SealedAttribute" ->
decodeBoolAttribFlag
attrib
WellKnownEntityAttributes.SealedAttribute_True
WellKnownEntityAttributes.SealedAttribute_False
WellKnownEntityAttributes.SealedAttribute_True
| "AbstractClassAttribute" -> WellKnownEntityAttributes.AbstractClassAttribute
| "RequireQualifiedAccessAttribute" -> WellKnownEntityAttributes.RequireQualifiedAccessAttribute
| "AutoOpenAttribute" -> WellKnownEntityAttributes.AutoOpenAttribute
| "NoEqualityAttribute" -> WellKnownEntityAttributes.NoEqualityAttribute
| "NoComparisonAttribute" -> WellKnownEntityAttributes.NoComparisonAttribute
| "StructuralEqualityAttribute" -> WellKnownEntityAttributes.StructuralEqualityAttribute
| "StructuralComparisonAttribute" -> WellKnownEntityAttributes.StructuralComparisonAttribute
| "CustomEqualityAttribute" -> WellKnownEntityAttributes.CustomEqualityAttribute
| "CustomComparisonAttribute" -> WellKnownEntityAttributes.CustomComparisonAttribute
| "ReferenceEqualityAttribute" -> WellKnownEntityAttributes.ReferenceEqualityAttribute
| "DefaultAugmentationAttribute" ->
decodeBoolAttribFlag
attrib
WellKnownEntityAttributes.DefaultAugmentationAttribute_True
WellKnownEntityAttributes.DefaultAugmentationAttribute_False
WellKnownEntityAttributes.DefaultAugmentationAttribute_True
| "CLIMutableAttribute" -> WellKnownEntityAttributes.CLIMutableAttribute
| "AutoSerializableAttribute" ->
decodeBoolAttribFlag
attrib
WellKnownEntityAttributes.AutoSerializableAttribute_True
WellKnownEntityAttributes.AutoSerializableAttribute_False
WellKnownEntityAttributes.AutoSerializableAttribute_True
| "ReflectedDefinitionAttribute" -> WellKnownEntityAttributes.ReflectedDefinitionAttribute
| "AllowNullLiteralAttribute" ->
decodeBoolAttribFlag
attrib
WellKnownEntityAttributes.AllowNullLiteralAttribute_True
WellKnownEntityAttributes.AllowNullLiteralAttribute_False
WellKnownEntityAttributes.AllowNullLiteralAttribute_True
| "WarnOnWithoutNullArgumentAttribute" -> WellKnownEntityAttributes.WarnOnWithoutNullArgumentAttribute
| "ClassAttribute" -> WellKnownEntityAttributes.ClassAttribute
| "InterfaceAttribute" -> WellKnownEntityAttributes.InterfaceAttribute
| "StructAttribute" -> WellKnownEntityAttributes.StructAttribute
| "MeasureAttribute" -> WellKnownEntityAttributes.MeasureAttribute
| "MeasureAnnotatedAbbreviationAttribute" -> WellKnownEntityAttributes.MeasureableAttribute
| "CLIEventAttribute" -> WellKnownEntityAttributes.CLIEventAttribute
| "CompilerMessageAttribute" -> WellKnownEntityAttributes.CompilerMessageAttribute
| "ExperimentalAttribute" -> WellKnownEntityAttributes.ExperimentalAttribute
| "UnverifiableAttribute" -> WellKnownEntityAttributes.UnverifiableAttribute
| "CompiledNameAttribute" -> WellKnownEntityAttributes.CompiledNameAttribute
| "CompilationRepresentationAttribute" ->
match attrib with
| Attrib(_, _, [ AttribInt32Arg v ], _, _, _, _) ->
let mutable flags = WellKnownEntityAttributes.None
if v &&& 0x01 <> 0 then
flags <- flags ||| WellKnownEntityAttributes.CompilationRepresentation_Static
if v &&& 0x02 <> 0 then
flags <- flags ||| WellKnownEntityAttributes.CompilationRepresentation_Instance
if v &&& 0x04 <> 0 then
flags <- flags ||| WellKnownEntityAttributes.CompilationRepresentation_ModuleSuffix
if v &&& 0x08 <> 0 then
flags <- flags ||| WellKnownEntityAttributes.CompilationRepresentation_PermitNull
flags
| _ -> WellKnownEntityAttributes.None
| _ -> WellKnownEntityAttributes.None
| _ -> WellKnownEntityAttributes.None
| ValueNone -> WellKnownEntityAttributes.None
/// Classify a single assembly-level attribute, returning its well-known flag (or None).
let classifyAssemblyAttrib (g: TcGlobals) (attrib: Attrib) : WellKnownAssemblyAttributes =
let (Attrib(tcref, _, _, _, _, _, _)) = attrib
let struct (bclPath, fsharpCorePath) = resolveAttribPath g tcref
match bclPath with
| ValueSome path ->
match path with
| [| "System"; "Runtime"; "CompilerServices"; name |] ->
match name with
| "InternalsVisibleToAttribute" -> WellKnownAssemblyAttributes.InternalsVisibleToAttribute
| _ -> WellKnownAssemblyAttributes.None
| [| "System"; "Reflection"; name |] ->
match name with
| "AssemblyCultureAttribute" -> WellKnownAssemblyAttributes.AssemblyCultureAttribute
| "AssemblyVersionAttribute" -> WellKnownAssemblyAttributes.AssemblyVersionAttribute
| _ -> WellKnownAssemblyAttributes.None
| _ -> WellKnownAssemblyAttributes.None
| ValueNone ->
match fsharpCorePath with
| ValueSome path ->
match path with
| [| "Microsoft"; "FSharp"; "Core"; name |] ->
match name with
| "AutoOpenAttribute" -> WellKnownAssemblyAttributes.AutoOpenAttribute
| _ -> WellKnownAssemblyAttributes.None
| [| "Microsoft"; "FSharp"; "Core"; "CompilerServices"; name |] ->
match name with
| "TypeProviderAssemblyAttribute" -> WellKnownAssemblyAttributes.TypeProviderAssemblyAttribute
| _ -> WellKnownAssemblyAttributes.None
| _ -> WellKnownAssemblyAttributes.None
| ValueNone -> WellKnownAssemblyAttributes.None
// ---------------------------------------------------------------
// Well-Known Attribute APIs — Navigation Guide
// ---------------------------------------------------------------
//
// This section provides O(1) cached lookups for well-known attributes.
// Choose the right API based on what you have and what you need:
//
// EXISTENCE CHECKS (cached, O(1) after first call):
// EntityHasWellKnownAttribute g flag entity — Entity (type/module)
// ValHasWellKnownAttribute g flag v — Val (value/member)
// ArgReprInfoHasWellKnownAttribute g flag arg — ArgReprInfo (parameter)
//
// AD-HOC CHECKS (no cache, re-scans each call):
// attribsHaveEntityFlag g flag attribs — raw Attrib list, entity flags
// attribsHaveValFlag g flag attribs — raw Attrib list, val flags
//
// DATA EXTRACTION (active patterns):
// (|EntityAttrib|_|) g flag attribs — returns full Attrib
// (|ValAttrib|_|) g flag attribs — returns full Attrib
// (|EntityAttribInt|_|) g flag attribs — extracts int32 argument
// (|EntityAttribString|_|) g flag attribs — extracts string argument
// (|ValAttribInt|_|) g flag attribs — extracts int32 argument
// (|ValAttribString|_|) g flag attribs — extracts string argument
//
// BOOL ATTRIBUTE QUERIES (three-state: Some true / Some false / None):
// EntityTryGetBoolAttribute g trueFlag falseFlag entity
// ValTryGetBoolAttribute g trueFlag falseFlag v
//
// IL-LEVEL (operates on ILAttribute / ILAttributes):
// classifyILAttrib attr — classify a single IL attr
// (|ILAttribDecoded|_|) flag cattrs — find & decode by flag
// ILAttributes.HasWellKnownAttribute(flag) — existence check (no cache)
// ILAttributesStored.HasWellKnownAttribute(g, flag) — cached existence
//
// CROSS-METADATA (IL + F# + Provided type dispatch):
// TyconRefHasWellKnownAttribute g flag tcref
// TyconRefAllowsNull g tcref
//
// CROSS-METADATA (in AttributeChecking.fs):
// MethInfoHasWellKnownAttribute g m ilFlag valFlag attribSpec minfo
// MethInfoHasWellKnownAttributeSpec g m spec minfo — convenience wrapper
//
// CLASSIFICATION (maps attribute → flag enum):
// classifyEntityAttrib g attrib — Attrib → WellKnownEntityAttributes
// classifyValAttrib g attrib — Attrib → WellKnownValAttributes
// classifyILAttrib attr — ILAttribute → WellKnownILAttributes
// ---------------------------------------------------------------
/// Shared combinator: find first attrib matching a flag via a classify function.
let inline internal tryFindAttribByClassifier
([<InlineIfLambda>] classify: TcGlobals -> Attrib -> 'Flag)
(none: 'Flag)
(g: TcGlobals)
(flag: 'Flag)
(attribs: Attribs)
: Attrib option =
attribs |> List.tryFind (fun attrib -> classify g attrib &&& flag <> none)
/// Shared combinator: check if any attrib in a list matches a flag via a classify function.
let inline internal attribsHaveFlag
([<InlineIfLambda>] classify: TcGlobals -> Attrib -> 'Flag)
(none: 'Flag)
(g: TcGlobals)
(flag: 'Flag)
(attribs: Attribs)
: bool =
attribs |> List.exists (fun attrib -> classify g attrib &&& flag <> none)
/// Compute well-known attribute flags for an Entity's Attrib list.
let computeEntityWellKnownFlags (g: TcGlobals) (attribs: Attribs) : WellKnownEntityAttributes =
let mutable flags = WellKnownEntityAttributes.None
for attrib in attribs do
flags <- flags ||| classifyEntityAttrib g attrib
flags
/// Find the first attribute matching a specific well-known entity flag.
let tryFindEntityAttribByFlag g flag attribs =
tryFindAttribByClassifier classifyEntityAttrib WellKnownEntityAttributes.None g flag attribs
/// Active pattern: find a well-known entity attribute and return the full Attrib.
[<return: Struct>]
let (|EntityAttrib|_|) (g: TcGlobals) (flag: WellKnownEntityAttributes) (attribs: Attribs) =
tryFindEntityAttribByFlag g flag attribs |> ValueOption.ofOption
/// Active pattern: extract a single int32 argument from a well-known entity attribute.
[<return: Struct>]
let (|EntityAttribInt|_|) (g: TcGlobals) (flag: WellKnownEntityAttributes) (attribs: Attribs) =
match attribs with
| EntityAttrib g flag (Attrib(_, _, [ AttribInt32Arg v ], _, _, _, _)) -> ValueSome v
| _ -> ValueNone
/// Active pattern: extract a single string argument from a well-known entity attribute.
[<return: Struct>]
let (|EntityAttribString|_|) (g: TcGlobals) (flag: WellKnownEntityAttributes) (attribs: Attribs) =
match attribs with
| EntityAttrib g flag (Attrib(_, _, [ AttribStringArg s ], _, _, _, _)) -> ValueSome s
| _ -> ValueNone
/// Map a WellKnownILAttributes flag to its entity flag + provided-type AttribInfo equivalents.
let mapILFlag (g: TcGlobals) (flag: WellKnownILAttributes) : struct (WellKnownEntityAttributes * BuiltinAttribInfo option) =
match flag with
| WellKnownILAttributes.IsReadOnlyAttribute ->
struct (WellKnownEntityAttributes.IsReadOnlyAttribute, Some g.attrib_IsReadOnlyAttribute)
| WellKnownILAttributes.IsByRefLikeAttribute ->
struct (WellKnownEntityAttributes.IsByRefLikeAttribute, g.attrib_IsByRefLikeAttribute_opt)
| WellKnownILAttributes.ExtensionAttribute ->
struct (WellKnownEntityAttributes.ExtensionAttribute, Some g.attrib_ExtensionAttribute)
| WellKnownILAttributes.AllowNullLiteralAttribute ->
struct (WellKnownEntityAttributes.AllowNullLiteralAttribute_True, Some g.attrib_AllowNullLiteralAttribute)
| WellKnownILAttributes.AutoOpenAttribute -> struct (WellKnownEntityAttributes.AutoOpenAttribute, Some g.attrib_AutoOpenAttribute)
| WellKnownILAttributes.ReflectedDefinitionAttribute ->
struct (WellKnownEntityAttributes.ReflectedDefinitionAttribute, Some g.attrib_ReflectedDefinitionAttribute)
| WellKnownILAttributes.ObsoleteAttribute -> struct (WellKnownEntityAttributes.ObsoleteAttribute, None)
| _ -> struct (WellKnownEntityAttributes.None, None)
/// Check if a raw attribute list has a specific well-known entity flag (ad-hoc, non-caching).
let attribsHaveEntityFlag g (flag: WellKnownEntityAttributes) (attribs: Attribs) =
attribsHaveFlag classifyEntityAttrib WellKnownEntityAttributes.None g flag attribs
/// Map a WellKnownILAttributes flag to its WellKnownValAttributes equivalent.
/// Check if an Entity has a specific well-known attribute, computing and caching flags if needed.
let EntityHasWellKnownAttribute (g: TcGlobals) (flag: WellKnownEntityAttributes) (entity: Entity) : bool =
entity.HasWellKnownAttribute(flag, computeEntityWellKnownFlags g)
/// Get the computed well-known attribute flags for an entity.
let GetEntityWellKnownFlags (g: TcGlobals) (entity: Entity) : WellKnownEntityAttributes =
entity.GetWellKnownEntityFlags(computeEntityWellKnownFlags g)
/// Classify a single Val-level attribute, returning its well-known flag (or None).
let classifyValAttrib (g: TcGlobals) (attrib: Attrib) : WellKnownValAttributes =
let (Attrib(tcref, _, _, _, _, _, _)) = attrib
let struct (bclPath, fsharpCorePath) = resolveAttribPath g tcref
match bclPath with
| ValueSome path ->
match path with
| [| "System"; "Runtime"; "CompilerServices"; name |] ->
match name with
| "SkipLocalsInitAttribute" -> WellKnownValAttributes.SkipLocalsInitAttribute
| "ExtensionAttribute" -> WellKnownValAttributes.ExtensionAttribute
| "CallerMemberNameAttribute" -> WellKnownValAttributes.CallerMemberNameAttribute
| "CallerFilePathAttribute" -> WellKnownValAttributes.CallerFilePathAttribute
| "CallerLineNumberAttribute" -> WellKnownValAttributes.CallerLineNumberAttribute
| "MethodImplAttribute" -> WellKnownValAttributes.MethodImplAttribute
| _ -> WellKnownValAttributes.None
| [| "System"; "Runtime"; "InteropServices"; name |] ->
match name with
| "DllImportAttribute" -> WellKnownValAttributes.DllImportAttribute
| "InAttribute" -> WellKnownValAttributes.InAttribute
| "OutAttribute" -> WellKnownValAttributes.OutAttribute
| "MarshalAsAttribute" -> WellKnownValAttributes.MarshalAsAttribute
| "DefaultParameterValueAttribute" -> WellKnownValAttributes.DefaultParameterValueAttribute
| "OptionalAttribute" -> WellKnownValAttributes.OptionalAttribute
| "PreserveSigAttribute" -> WellKnownValAttributes.PreserveSigAttribute
| "FieldOffsetAttribute" -> WellKnownValAttributes.FieldOffsetAttribute
| _ -> WellKnownValAttributes.None
| [| "System"; "Diagnostics"; name |] ->
match name with
| "ConditionalAttribute" -> WellKnownValAttributes.ConditionalAttribute
| _ -> WellKnownValAttributes.None
| [| "System"; name |] ->
match name with
| "ThreadStaticAttribute" -> WellKnownValAttributes.ThreadStaticAttribute
| "ContextStaticAttribute" -> WellKnownValAttributes.ContextStaticAttribute
| "ParamArrayAttribute" -> WellKnownValAttributes.ParamArrayAttribute
| "NonSerializedAttribute" -> WellKnownValAttributes.NonSerializedAttribute
| _ -> WellKnownValAttributes.None
| _ -> WellKnownValAttributes.None
| ValueNone ->
match fsharpCorePath with
| ValueSome path ->
match path with
| [| "Microsoft"; "FSharp"; "Core"; name |] ->
match name with
| "EntryPointAttribute" -> WellKnownValAttributes.EntryPointAttribute
| "LiteralAttribute" -> WellKnownValAttributes.LiteralAttribute
| "ReflectedDefinitionAttribute" ->
decodeBoolAttribFlag
attrib
WellKnownValAttributes.ReflectedDefinitionAttribute_True
WellKnownValAttributes.ReflectedDefinitionAttribute_False
WellKnownValAttributes.ReflectedDefinitionAttribute_False
| "RequiresExplicitTypeArgumentsAttribute" -> WellKnownValAttributes.RequiresExplicitTypeArgumentsAttribute
| "DefaultValueAttribute" ->
decodeBoolAttribFlag
attrib
WellKnownValAttributes.DefaultValueAttribute_True
WellKnownValAttributes.DefaultValueAttribute_False
WellKnownValAttributes.DefaultValueAttribute_True
| "VolatileFieldAttribute" -> WellKnownValAttributes.VolatileFieldAttribute
| "NoDynamicInvocationAttribute" ->
decodeBoolAttribFlag
attrib
WellKnownValAttributes.NoDynamicInvocationAttribute_True
WellKnownValAttributes.NoDynamicInvocationAttribute_False
WellKnownValAttributes.NoDynamicInvocationAttribute_False
| "OptionalArgumentAttribute" -> WellKnownValAttributes.OptionalArgumentAttribute
| "ProjectionParameterAttribute" -> WellKnownValAttributes.ProjectionParameterAttribute
| "InlineIfLambdaAttribute" -> WellKnownValAttributes.InlineIfLambdaAttribute
| "StructAttribute" -> WellKnownValAttributes.StructAttribute
| "NoCompilerInliningAttribute" -> WellKnownValAttributes.NoCompilerInliningAttribute
| "GeneralizableValueAttribute" -> WellKnownValAttributes.GeneralizableValueAttribute
| "CLIEventAttribute" -> WellKnownValAttributes.CLIEventAttribute
| "CompiledNameAttribute" -> WellKnownValAttributes.CompiledNameAttribute
| "WarnOnWithoutNullArgumentAttribute" -> WellKnownValAttributes.WarnOnWithoutNullArgumentAttribute
| "ValueAsStaticPropertyAttribute" -> WellKnownValAttributes.ValueAsStaticPropertyAttribute
| "TailCallAttribute" -> WellKnownValAttributes.TailCallAttribute
| _ -> WellKnownValAttributes.None
| [| "Microsoft"; "FSharp"; "Core"; "CompilerServices"; name |] ->
match name with
| "NoEagerConstraintApplicationAttribute" -> WellKnownValAttributes.NoEagerConstraintApplicationAttribute
| _ -> WellKnownValAttributes.None
| _ -> WellKnownValAttributes.None
| ValueNone -> WellKnownValAttributes.None
let computeValWellKnownFlags (g: TcGlobals) (attribs: Attribs) : WellKnownValAttributes =
let mutable flags = WellKnownValAttributes.None
for attrib in attribs do
flags <- flags ||| classifyValAttrib g attrib
flags
/// Find the first attribute in a list that matches a specific well-known val flag.
let tryFindValAttribByFlag g flag attribs =
tryFindAttribByClassifier classifyValAttrib WellKnownValAttributes.None g flag attribs
/// Active pattern: find a well-known val attribute and return the full Attrib.
[<return: Struct>]
let (|ValAttrib|_|) (g: TcGlobals) (flag: WellKnownValAttributes) (attribs: Attribs) =
tryFindValAttribByFlag g flag attribs |> ValueOption.ofOption
/// Active pattern: extract a single int32 argument from a well-known val attribute.
[<return: Struct>]
let (|ValAttribInt|_|) (g: TcGlobals) (flag: WellKnownValAttributes) (attribs: Attribs) =
match attribs with
| ValAttrib g flag (Attrib(_, _, [ AttribInt32Arg v ], _, _, _, _)) -> ValueSome v
| _ -> ValueNone
/// Active pattern: extract a single string argument from a well-known val attribute.
[<return: Struct>]
let (|ValAttribString|_|) (g: TcGlobals) (flag: WellKnownValAttributes) (attribs: Attribs) =
match attribs with
| ValAttrib g flag (Attrib(_, _, [ AttribStringArg s ], _, _, _, _)) -> ValueSome s
| _ -> ValueNone
/// Check if a raw attribute list has a specific well-known val flag (ad-hoc, non-caching).
let attribsHaveValFlag g (flag: WellKnownValAttributes) (attribs: Attribs) =
attribsHaveFlag classifyValAttrib WellKnownValAttributes.None g flag attribs
/// Filter out well-known attributes from a list. Single-pass using classify functions.
/// Attributes matching ANY set bit in entityMask or valMask are removed.
let filterOutWellKnownAttribs
(g: TcGlobals)
(entityMask: WellKnownEntityAttributes)
(valMask: WellKnownValAttributes)
(attribs: Attribs)
=
attribs
|> List.filter (fun attrib ->
(entityMask = WellKnownEntityAttributes.None
|| classifyEntityAttrib g attrib &&& entityMask = WellKnownEntityAttributes.None)
&& (valMask = WellKnownValAttributes.None
|| classifyValAttrib g attrib &&& valMask = WellKnownValAttributes.None))
/// Check if an ArgReprInfo has a specific well-known attribute, computing and caching flags if needed.
let ArgReprInfoHasWellKnownAttribute (g: TcGlobals) (flag: WellKnownValAttributes) (argInfo: ArgReprInfo) : bool =
let struct (result, waNew, changed) =
argInfo.Attribs.CheckFlag(flag, computeValWellKnownFlags g)
if changed then
argInfo.Attribs <- waNew
result
/// Check if a Val has a specific well-known attribute, computing and caching flags if needed.
let ValHasWellKnownAttribute (g: TcGlobals) (flag: WellKnownValAttributes) (v: Val) : bool =
v.HasWellKnownAttribute(flag, computeValWellKnownFlags g)
/// Query a three-state bool attribute on an entity. Returns bool option.
let EntityTryGetBoolAttribute
(g: TcGlobals)
(trueFlag: WellKnownEntityAttributes)
(falseFlag: WellKnownEntityAttributes)
(entity: Entity)
: bool option =
if not (entity.HasWellKnownAttribute(trueFlag ||| falseFlag, computeEntityWellKnownFlags g)) then
Option.None
else
let struct (hasTrue, _, _) =
entity.EntityAttribs.CheckFlag(trueFlag, computeEntityWellKnownFlags g)
if hasTrue then Some true else Some false
/// Query a three-state bool attribute on a Val. Returns bool option.
let ValTryGetBoolAttribute
(g: TcGlobals)
(trueFlag: WellKnownValAttributes)
(falseFlag: WellKnownValAttributes)
(v: Val)
: bool option =
if not (v.HasWellKnownAttribute(trueFlag ||| falseFlag, computeValWellKnownFlags g)) then
Option.None
else
let struct (hasTrue, _, _) =
v.ValAttribs.CheckFlag(trueFlag, computeValWellKnownFlags g)
if hasTrue then Some true else Some false
/// Shared core for binding attributes on type definitions, supporting an optional
/// WellKnownILAttributes flag for O(1) early exit on the IL metadata path.
let private tryBindTyconRefAttributeCore
g
(m: range)
(ilFlag: WellKnownILAttributes voption)
(AttribInfo(atref, _) as args)
(tcref: TyconRef)
f1
f2
(f3: obj option list * (string * obj option) list -> 'a option)
: 'a option =
ignore m
ignore f3
match metadataOfTycon tcref.Deref with
#if !NO_TYPEPROVIDERS
| ProvidedTypeMetadata info ->
let provAttribs =
info.ProvidedType.PApply((fun a -> (a :> IProvidedCustomAttributeProvider)), m)
match
provAttribs.PUntaint(
(fun a -> a.GetAttributeConstructorArgs(provAttribs.TypeProvider.PUntaintNoFailure id, atref.FullName)),
m
)
with
| Some args -> f3 args
| None -> None
#endif
| ILTypeMetadata(TILObjectReprData(_, _, tdef)) ->
match ilFlag with
| ValueSome flag when not (tdef.HasWellKnownAttribute(g, flag)) -> None
| _ ->
match TryDecodeILAttribute atref tdef.CustomAttrs with
| Some attr -> f1 attr
| _ -> None
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata ->
match TryFindFSharpAttribute g args tcref.Attribs with
| Some attr -> f2 attr
| _ -> None
/// Analyze three cases for attributes declared on type definitions: IL-declared attributes, F#-declared attributes and
/// provided attributes.
//
// This is used for AttributeUsageAttribute, DefaultMemberAttribute and ConditionalAttribute (on attribute types)
let TryBindTyconRefAttribute g (m: range) args (tcref: TyconRef) f1 f2 f3 : 'a option =
tryBindTyconRefAttributeCore g m ValueNone args tcref f1 f2 f3
let TryFindTyconRefBoolAttribute g m attribSpec tcref =
TryBindTyconRefAttribute
g
m
attribSpec
tcref
(function
| [], _ -> Some true
| [ ILAttribElem.Bool v ], _ -> Some v
| _ -> None)
(function
| Attrib(_, _, [], _, _, _, _) -> Some true
| Attrib(_, _, [ AttribBoolArg v ], _, _, _, _) -> Some v
| _ -> None)
(function
| [], _ -> Some true
| [ Some(:? bool as v: obj) ], _ -> Some v
| _ -> None)
/// Try to find the resolved attributeusage for an type by walking its inheritance tree and picking the correct attribute usage value
let TryFindAttributeUsageAttribute g m tcref =
[| yield tcref; yield! supersOfTyconRef tcref |]
|> Array.tryPick (fun tcref ->
TryBindTyconRefAttribute
g
m
g.attrib_AttributeUsageAttribute
tcref
(fun (_, named) ->
named
|> List.tryPick (function
| "AllowMultiple", _, _, ILAttribElem.Bool res -> Some res
| _ -> None))
(fun (Attrib(_, _, _, named, _, _, _)) ->
named
|> List.tryPick (function
| AttribNamedArg("AllowMultiple", _, _, AttribBoolArg res) -> Some res
| _ -> None))
(fun (_, named) ->
named
|> List.tryPick (function
| "AllowMultiple", Some(:? bool as res: obj) -> Some res
| _ -> None)))
/// Try to find a specific attribute on a type definition, where the attribute accepts a string argument.
///
/// This is used to detect the 'DefaultMemberAttribute' and 'ConditionalAttribute' attributes (on type definitions)
let TryFindTyconRefStringAttribute g m attribSpec tcref =
TryBindTyconRefAttribute
g
m
attribSpec
tcref
(function
| [ ILAttribElem.String(Some msg) ], _ -> Some msg
| _ -> None)
(function
| Attrib(_, _, [ AttribStringArg msg ], _, _, _, _) -> Some msg
| _ -> None)
(function
| [ Some(:? string as msg: obj) ], _ -> Some msg
| _ -> None)
/// Like TryBindTyconRefAttribute but with a fast-path flag check on the IL metadata path.
/// Skips the full attribute scan if the cached flag indicates the attribute is absent.
let TryBindTyconRefAttributeWithILFlag g (m: range) (ilFlag: WellKnownILAttributes) args (tcref: TyconRef) f1 f2 f3 : 'a option =
tryBindTyconRefAttributeCore g m (ValueSome ilFlag) args tcref f1 f2 f3
/// Like TryFindTyconRefStringAttribute but with a fast-path flag check on the IL path.
/// Use this when the attribute has a corresponding WellKnownILAttributes flag for O(1) early exit.
let TryFindTyconRefStringAttributeFast g m ilFlag attribSpec tcref =
TryBindTyconRefAttributeWithILFlag
g
m
ilFlag
attribSpec
tcref
(function
| [ ILAttribElem.String(Some msg) ], _ -> Some msg
| _ -> None)
(function
| Attrib(_, _, [ AttribStringArg msg ], _, _, _, _) -> Some msg
| _ -> None)
(function
| [ Some(:? string as msg: obj) ], _ -> Some msg
| _ -> None)
/// Check if a type definition has a specific attribute
let TyconRefHasAttribute g m attribSpec tcref =
TryBindTyconRefAttribute g m attribSpec tcref (fun _ -> Some()) (fun _ -> Some()) (fun _ -> Some())
|> Option.isSome
/// Check if a TyconRef has a well-known attribute, handling both IL and F# metadata.
/// Uses O(1) flag tests on both paths.
let TyconRefHasWellKnownAttribute (g: TcGlobals) (flag: WellKnownILAttributes) (tcref: TyconRef) : bool =
match metadataOfTycon tcref.Deref with
#if !NO_TYPEPROVIDERS
| ProvidedTypeMetadata _ ->
let struct (_, attribInfoOpt) = mapILFlag g flag
match attribInfoOpt with
| Some attribInfo -> TyconRefHasAttribute g tcref.Range attribInfo tcref
| None -> false
#endif
| ILTypeMetadata(TILObjectReprData(_, _, tdef)) -> tdef.HasWellKnownAttribute(g, flag)
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata ->
let struct (entityFlag, _) = mapILFlag g flag
if entityFlag <> WellKnownEntityAttributes.None then
EntityHasWellKnownAttribute g entityFlag tcref.Deref
else
false
let HasDefaultAugmentationAttribute g (tcref: TyconRef) =
match
EntityTryGetBoolAttribute
g
WellKnownEntityAttributes.DefaultAugmentationAttribute_True
WellKnownEntityAttributes.DefaultAugmentationAttribute_False
tcref.Deref
with
| Some b -> b
| None -> true
/// Check if a TyconRef has AllowNullLiteralAttribute, returning Some true/Some false/None.
let TyconRefAllowsNull (g: TcGlobals) (tcref: TyconRef) : bool option =
match metadataOfTycon tcref.Deref with
#if !NO_TYPEPROVIDERS
| ProvidedTypeMetadata _ -> TryFindTyconRefBoolAttribute g tcref.Range g.attrib_AllowNullLiteralAttribute tcref
#endif
| ILTypeMetadata(TILObjectReprData(_, _, tdef)) ->
if tdef.HasWellKnownAttribute(g, WellKnownILAttributes.AllowNullLiteralAttribute) then
Some true
else
None
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata ->
EntityTryGetBoolAttribute
g
WellKnownEntityAttributes.AllowNullLiteralAttribute_True
WellKnownEntityAttributes.AllowNullLiteralAttribute_False
tcref.Deref
/// Check if a type definition has an attribute with a specific full name
let TyconRefHasAttributeByName (m: range) attrFullName (tcref: TyconRef) =
ignore m
match metadataOfTycon tcref.Deref with
#if !NO_TYPEPROVIDERS
| ProvidedTypeMetadata info ->
let provAttribs =
info.ProvidedType.PApply((fun a -> (a :> IProvidedCustomAttributeProvider)), m)
provAttribs
.PUntaint((fun a -> a.GetAttributeConstructorArgs(provAttribs.TypeProvider.PUntaintNoFailure id, attrFullName)), m)
.IsSome
#endif
| ILTypeMetadata(TILObjectReprData(_, _, tdef)) ->
tdef.CustomAttrs.AsArray()
|> Array.exists (fun attr -> isILAttribByName ([], attrFullName) attr)
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata ->
tcref.Attribs
|> List.exists (fun attr ->
match attr.TyconRef.CompiledRepresentation with
| CompiledTypeRepr.ILAsmNamed(typeRef, _, _) -> typeRef.Enclosing.IsEmpty && typeRef.Name = attrFullName
| CompiledTypeRepr.ILAsmOpen _ -> false)
type ValRef with
member vref.IsDispatchSlot =
match vref.MemberInfo with
| Some membInfo -> membInfo.MemberFlags.IsDispatchSlot
| None -> false
[<return: Struct>]
let (|UnopExpr|_|) (_g: TcGlobals) expr =
match expr with
| Expr.App(Expr.Val(vref, _, _), _, _, [ arg1 ], _) -> ValueSome(vref, arg1)
| _ -> ValueNone
[<return: Struct>]
let (|BinopExpr|_|) (_g: TcGlobals) expr =
match expr with
| Expr.App(Expr.Val(vref, _, _), _, _, [ arg1; arg2 ], _) -> ValueSome(vref, arg1, arg2)
| _ -> ValueNone
[<return: Struct>]
let (|SpecificUnopExpr|_|) g vrefReqd expr =
match expr with
| UnopExpr g (vref, arg1) when valRefEq g vref vrefReqd -> ValueSome arg1
| _ -> ValueNone
[<return: Struct>]
let (|SignedConstExpr|_|) expr =
match expr with
| Expr.Const(Const.Int32 _, _, _)
| Expr.Const(Const.SByte _, _, _)
| Expr.Const(Const.Int16 _, _, _)
| Expr.Const(Const.Int64 _, _, _)
| Expr.Const(Const.Single _, _, _)
| Expr.Const(Const.Double _, _, _) -> ValueSome()
| _ -> ValueNone
[<return: Struct>]
let (|IntegerConstExpr|_|) expr =
match expr with
| Expr.Const(Const.Int32 _, _, _)