9
9
using Mono . Cecil ;
10
10
using Mono . Cecil . Cil ;
11
11
12
+ #nullable enable
13
+
12
14
namespace Mono . Linker . Dataflow
13
15
{
14
16
class FlowAnnotations
@@ -74,15 +76,15 @@ public bool ShouldWarnWhenAccessedForReflection (IMemberDefinition provider) =>
74
76
75
77
public DynamicallyAccessedMemberTypes GetGenericParameterAnnotation ( GenericParameter genericParameter )
76
78
{
77
- TypeDefinition declaringType = _context . Resolve ( genericParameter . DeclaringType ) ;
79
+ TypeDefinition ? declaringType = _context . Resolve ( genericParameter . DeclaringType ) ;
78
80
if ( declaringType != null ) {
79
81
if ( GetAnnotations ( declaringType ) . TryGetAnnotation ( genericParameter , out var annotation ) )
80
82
return annotation ;
81
83
82
84
return DynamicallyAccessedMemberTypes . None ;
83
85
}
84
86
85
- MethodDefinition declaringMethod = _context . Resolve ( genericParameter . DeclaringMethod ) ;
87
+ MethodDefinition ? declaringMethod = _context . Resolve ( genericParameter . DeclaringMethod ) ;
86
88
if ( declaringMethod != null && GetAnnotations ( declaringMethod . DeclaringType ) . TryGetAnnotation ( declaringMethod , out var methodTypeAnnotations ) &&
87
89
methodTypeAnnotations . TryGetAnnotation ( genericParameter , out var methodAnnotation ) )
88
90
return methodAnnotation ;
@@ -159,7 +161,7 @@ static bool IsDynamicallyAccessedMembersAttribute (CustomAttribute attribute)
159
161
return attributeType . Name == "DynamicallyAccessedMembersAttribute" && attributeType . Namespace == "System.Diagnostics.CodeAnalysis" ;
160
162
}
161
163
162
- DynamicallyAccessedMemberTypes GetMemberTypesForDynamicallyAccessedMembersAttribute ( IMemberDefinition member , ICustomAttributeProvider providerIfNotMember = null )
164
+ DynamicallyAccessedMemberTypes GetMemberTypesForDynamicallyAccessedMembersAttribute ( IMemberDefinition member , ICustomAttributeProvider ? providerIfNotMember = null )
163
165
{
164
166
ICustomAttributeProvider provider = providerIfNotMember ?? member ;
165
167
if ( ! _context . CustomAttributes . HasAny ( provider ) )
@@ -209,7 +211,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
209
211
// Next go over all methods with an explicit annotation
210
212
if ( type . HasMethods ) {
211
213
foreach ( MethodDefinition method in type . Methods ) {
212
- DynamicallyAccessedMemberTypes [ ] paramAnnotations = null ;
214
+ DynamicallyAccessedMemberTypes [ ] ? paramAnnotations = null ;
213
215
214
216
// We convert indices from metadata space to IL space here.
215
217
// IL space assigns index 0 to the `this` parameter on instance methods.
@@ -267,7 +269,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
267
269
2106 , method , subcategory : MessageSubCategory . TrimAnalysis ) ;
268
270
}
269
271
270
- DynamicallyAccessedMemberTypes [ ] genericParameterAnnotations = null ;
272
+ DynamicallyAccessedMemberTypes [ ] ? genericParameterAnnotations = null ;
271
273
if ( method . HasGenericParameters ) {
272
274
for ( int genericParameterIndex = 0 ; genericParameterIndex < method . GenericParameters . Count ; genericParameterIndex ++ ) {
273
275
var genericParameter = method . GenericParameters [ genericParameterIndex ] ;
@@ -313,7 +315,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
313
315
continue ;
314
316
}
315
317
316
- FieldDefinition backingFieldFromSetter = null ;
318
+ FieldDefinition ? backingFieldFromSetter = null ;
317
319
318
320
// Propagate the annotation to the setter method
319
321
MethodDefinition setMethod = property . SetMethod ;
@@ -342,7 +344,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
342
344
}
343
345
}
344
346
345
- FieldDefinition backingFieldFromGetter = null ;
347
+ FieldDefinition ? backingFieldFromGetter = null ;
346
348
347
349
// Propagate the annotation to the getter method
348
350
MethodDefinition getMethod = property . GetMethod ;
@@ -366,7 +368,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
366
368
}
367
369
}
368
370
369
- FieldDefinition backingField ;
371
+ FieldDefinition ? backingField ;
370
372
if ( backingFieldFromGetter != null && backingFieldFromSetter != null &&
371
373
backingFieldFromGetter != backingFieldFromSetter ) {
372
374
_context . LogWarning (
@@ -389,7 +391,7 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
389
391
}
390
392
}
391
393
392
- DynamicallyAccessedMemberTypes [ ] typeGenericParameterAnnotations = null ;
394
+ DynamicallyAccessedMemberTypes [ ] ? typeGenericParameterAnnotations = null ;
393
395
if ( type . HasGenericParameters ) {
394
396
for ( int genericParameterIndex = 0 ; genericParameterIndex < type . GenericParameters . Count ; genericParameterIndex ++ ) {
395
397
var genericParameter = type . GenericParameters [ genericParameterIndex ] ;
@@ -405,13 +407,13 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
405
407
return new TypeAnnotations ( type , typeAnnotation , annotatedMethods . ToArray ( ) , annotatedFields . ToArray ( ) , typeGenericParameterAnnotations ) ;
406
408
}
407
409
408
- bool ScanMethodBodyForFieldAccess ( MethodBody body , bool write , out FieldDefinition found )
410
+ bool ScanMethodBodyForFieldAccess ( MethodBody body , bool write , out FieldDefinition ? found )
409
411
{
410
412
// Tries to find the backing field for a property getter/setter.
411
413
// Returns true if this is a method body that we can unambiguously analyze.
412
414
// The found field could still be null if there's no backing store.
413
415
414
- FieldReference foundReference = null ;
416
+ FieldReference ? foundReference = null ;
415
417
416
418
foreach ( Instruction instruction in body . Instructions ) {
417
419
switch ( instruction . OpCode . Code ) {
@@ -464,7 +466,7 @@ bool IsTypeInterestingForDataflow (TypeReference typeReference)
464
466
if ( typeReference . MetadataType == MetadataType . String )
465
467
return true ;
466
468
467
- TypeDefinition type = _context . TryResolve ( typeReference ) ;
469
+ TypeDefinition ? type = _context . TryResolve ( typeReference ) ;
468
470
return type != null && (
469
471
_hierarchyInfo . IsSystemType ( type ) ||
470
472
_hierarchyInfo . IsSystemReflectionIReflect ( type ) ) ;
@@ -480,9 +482,9 @@ internal void ValidateMethodAnnotationsAreSame (MethodDefinition method, MethodD
480
482
481
483
if ( methodAnnotations . ParameterAnnotations != null || baseMethodAnnotations . ParameterAnnotations != null ) {
482
484
if ( methodAnnotations . ParameterAnnotations == null )
483
- ValidateMethodParametersHaveNoAnnotations ( ref baseMethodAnnotations , method , baseMethod , method ) ;
485
+ ValidateMethodParametersHaveNoAnnotations ( baseMethodAnnotations . ParameterAnnotations ! , method , baseMethod , method ) ;
484
486
else if ( baseMethodAnnotations . ParameterAnnotations == null )
485
- ValidateMethodParametersHaveNoAnnotations ( ref methodAnnotations , method , baseMethod , method ) ;
487
+ ValidateMethodParametersHaveNoAnnotations ( methodAnnotations . ParameterAnnotations , method , baseMethod , method ) ;
486
488
else {
487
489
if ( methodAnnotations . ParameterAnnotations . Length != baseMethodAnnotations . ParameterAnnotations . Length )
488
490
return ;
@@ -499,9 +501,9 @@ internal void ValidateMethodAnnotationsAreSame (MethodDefinition method, MethodD
499
501
500
502
if ( methodAnnotations . GenericParameterAnnotations != null || baseMethodAnnotations . GenericParameterAnnotations != null ) {
501
503
if ( methodAnnotations . GenericParameterAnnotations == null )
502
- ValidateMethodGenericParametersHaveNoAnnotations ( ref baseMethodAnnotations , method , baseMethod , method ) ;
504
+ ValidateMethodGenericParametersHaveNoAnnotations ( baseMethodAnnotations . GenericParameterAnnotations ! , method , baseMethod , method ) ;
503
505
else if ( baseMethodAnnotations . GenericParameterAnnotations == null )
504
- ValidateMethodGenericParametersHaveNoAnnotations ( ref methodAnnotations , method , baseMethod , method ) ;
506
+ ValidateMethodGenericParametersHaveNoAnnotations ( methodAnnotations . GenericParameterAnnotations , method , baseMethod , method ) ;
505
507
else {
506
508
if ( methodAnnotations . GenericParameterAnnotations . Length != baseMethodAnnotations . GenericParameterAnnotations . Length )
507
509
return ;
@@ -518,10 +520,10 @@ internal void ValidateMethodAnnotationsAreSame (MethodDefinition method, MethodD
518
520
}
519
521
}
520
522
521
- void ValidateMethodParametersHaveNoAnnotations ( ref MethodAnnotations methodAnnotations , MethodDefinition method , MethodDefinition baseMethod , IMemberDefinition origin )
523
+ void ValidateMethodParametersHaveNoAnnotations ( DynamicallyAccessedMemberTypes [ ] parameterAnnotations , MethodDefinition method , MethodDefinition baseMethod , IMemberDefinition origin )
522
524
{
523
- for ( int parameterIndex = 0 ; parameterIndex < methodAnnotations . ParameterAnnotations . Length ; parameterIndex ++ ) {
524
- var annotation = methodAnnotations . ParameterAnnotations [ parameterIndex ] ;
525
+ for ( int parameterIndex = 0 ; parameterIndex < parameterAnnotations . Length ; parameterIndex ++ ) {
526
+ var annotation = parameterAnnotations [ parameterIndex ] ;
525
527
if ( annotation != DynamicallyAccessedMemberTypes . None )
526
528
LogValidationWarning (
527
529
DiagnosticUtilities . GetMethodParameterFromIndex ( method , parameterIndex ) ,
@@ -530,10 +532,10 @@ void ValidateMethodParametersHaveNoAnnotations (ref MethodAnnotations methodAnno
530
532
}
531
533
}
532
534
533
- void ValidateMethodGenericParametersHaveNoAnnotations ( ref MethodAnnotations methodAnnotations , MethodDefinition method , MethodDefinition baseMethod , IMemberDefinition origin )
535
+ void ValidateMethodGenericParametersHaveNoAnnotations ( DynamicallyAccessedMemberTypes [ ] genericParameterAnnotations , MethodDefinition method , MethodDefinition baseMethod , IMemberDefinition origin )
534
536
{
535
- for ( int genericParameterIndex = 0 ; genericParameterIndex < methodAnnotations . GenericParameterAnnotations . Length ; genericParameterIndex ++ ) {
536
- if ( methodAnnotations . GenericParameterAnnotations [ genericParameterIndex ] != DynamicallyAccessedMemberTypes . None ) {
537
+ for ( int genericParameterIndex = 0 ; genericParameterIndex < genericParameterAnnotations . Length ; genericParameterIndex ++ ) {
538
+ if ( genericParameterAnnotations [ genericParameterIndex ] != DynamicallyAccessedMemberTypes . None ) {
537
539
LogValidationWarning (
538
540
method . GenericParameters [ genericParameterIndex ] ,
539
541
baseMethod . GenericParameters [ genericParameterIndex ] ,
@@ -589,14 +591,14 @@ readonly struct TypeAnnotations
589
591
readonly DynamicallyAccessedMemberTypes _typeAnnotation ;
590
592
readonly MethodAnnotations [ ] _annotatedMethods ;
591
593
readonly FieldAnnotation [ ] _annotatedFields ;
592
- readonly DynamicallyAccessedMemberTypes [ ] _genericParameterAnnotations ;
594
+ readonly DynamicallyAccessedMemberTypes [ ] ? _genericParameterAnnotations ;
593
595
594
596
public TypeAnnotations (
595
597
TypeDefinition type ,
596
598
DynamicallyAccessedMemberTypes typeAnnotation ,
597
599
MethodAnnotations [ ] annotatedMethods ,
598
600
FieldAnnotation [ ] annotatedFields ,
599
- DynamicallyAccessedMemberTypes [ ] genericParameterAnnotations )
601
+ DynamicallyAccessedMemberTypes [ ] ? genericParameterAnnotations )
600
602
=> ( _type , _typeAnnotation , _annotatedMethods , _annotatedFields , _genericParameterAnnotations )
601
603
= ( type , typeAnnotation , annotatedMethods , annotatedFields , genericParameterAnnotations ) ;
602
604
@@ -659,15 +661,15 @@ public bool TryGetAnnotation (GenericParameter genericParameter, out Dynamically
659
661
readonly struct MethodAnnotations
660
662
{
661
663
public readonly MethodDefinition Method ;
662
- public readonly DynamicallyAccessedMemberTypes [ ] ParameterAnnotations ;
664
+ public readonly DynamicallyAccessedMemberTypes [ ] ? ParameterAnnotations ;
663
665
public readonly DynamicallyAccessedMemberTypes ReturnParameterAnnotation ;
664
- public readonly DynamicallyAccessedMemberTypes [ ] GenericParameterAnnotations ;
666
+ public readonly DynamicallyAccessedMemberTypes [ ] ? GenericParameterAnnotations ;
665
667
666
668
public MethodAnnotations (
667
669
MethodDefinition method ,
668
- DynamicallyAccessedMemberTypes [ ] paramAnnotations ,
670
+ DynamicallyAccessedMemberTypes [ ] ? paramAnnotations ,
669
671
DynamicallyAccessedMemberTypes returnParamAnnotations ,
670
- DynamicallyAccessedMemberTypes [ ] genericParameterAnnotations )
672
+ DynamicallyAccessedMemberTypes [ ] ? genericParameterAnnotations )
671
673
=> ( Method , ParameterAnnotations , ReturnParameterAnnotation , GenericParameterAnnotations ) =
672
674
( method , paramAnnotations , returnParamAnnotations , genericParameterAnnotations ) ;
673
675
0 commit comments