Files
2026-08-27 10:56:38 -06:00

162 lines
5.7 KiB
C#

using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp;
internal static class NullableAnnotationExtensions
{
public const byte NotAnnotatedAttributeValue = 1;
public const byte AnnotatedAttributeValue = 2;
public const byte ObliviousAttributeValue = 0;
public static bool IsAnnotated(this NullableAnnotation annotation)
{
return annotation == NullableAnnotation.Annotated;
}
public static bool IsNotAnnotated(this NullableAnnotation annotation)
{
return annotation == NullableAnnotation.NotAnnotated;
}
public static bool IsOblivious(this NullableAnnotation annotation)
{
return annotation == NullableAnnotation.Oblivious;
}
public static NullableAnnotation Join(this NullableAnnotation a, NullableAnnotation b)
{
if ((int)a >= (int)b)
{
return a;
}
return b;
}
public static NullableAnnotation Meet(this NullableAnnotation a, NullableAnnotation b)
{
if ((int)a >= (int)b)
{
return b;
}
return a;
}
public static NullableAnnotation EnsureCompatible(this NullableAnnotation a, NullableAnnotation b)
{
if (a != NullableAnnotation.Oblivious)
{
if (b == NullableAnnotation.Oblivious)
{
return a;
}
return ((int)a < (int)b) ? a : b;
}
return b;
}
public static NullableAnnotation MergeNullableAnnotation(this NullableAnnotation a, NullableAnnotation b, VarianceKind variance)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Expected I4, but got Unknown
//IL_0032: Unknown result type (might be due to invalid IL or missing references)
return (int)variance switch
{
2 => a.Meet(b),
1 => a.Join(b),
0 => a.EnsureCompatible(b),
_ => throw ExceptionUtilities.UnexpectedValue((object)variance),
};
}
internal static NullabilityInfo ToNullabilityInfo(this NullableAnnotation annotation, TypeSymbol type)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
if ((int)annotation == 0)
{
return default(NullabilityInfo);
}
return annotation.ToInternalAnnotation().ToNullabilityInfo(type);
}
internal static NullabilityInfo ToNullabilityInfo(this NullableAnnotation annotation, TypeSymbol type)
{
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
//IL_0026: Unknown result type (might be due to invalid IL or missing references)
//IL_002b: Unknown result type (might be due to invalid IL or missing references)
NullableFlowState state = TypeWithAnnotations.Create(type, annotation).ToTypeWithState().State;
return new NullabilityInfo(ToPublicAnnotation(type, annotation), state.ToPublicFlowState());
}
internal static ITypeSymbol GetPublicSymbol(this TypeWithAnnotations type)
{
//IL_000e: Unknown result type (might be due to invalid IL or missing references)
return type.Type?.GetITypeSymbol(type.ToPublicAnnotation());
}
internal static ImmutableArray<ITypeSymbol> GetPublicSymbols(this ImmutableArray<TypeWithAnnotations> types)
{
return ImmutableArrayExtensions.SelectAsArray<TypeWithAnnotations, ITypeSymbol>(types, (Func<TypeWithAnnotations, ITypeSymbol>)((TypeWithAnnotations t) => t.GetPublicSymbol()));
}
internal static NullableAnnotation ToPublicAnnotation(this TypeWithAnnotations type)
{
//IL_000e: Unknown result type (might be due to invalid IL or missing references)
return ToPublicAnnotation(type.Type, type.NullableAnnotation);
}
internal static ImmutableArray<NullableAnnotation> ToPublicAnnotations(this ImmutableArray<TypeWithAnnotations> types)
{
return ImmutableArrayExtensions.SelectAsArray<TypeWithAnnotations, NullableAnnotation>(types, (Func<TypeWithAnnotations, NullableAnnotation>)((TypeWithAnnotations t) => t.ToPublicAnnotation()));
}
internal static NullableAnnotation ToPublicAnnotation(TypeSymbol? type, NullableAnnotation annotation)
{
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Unknown result type (might be due to invalid IL or missing references)
//IL_0045: Unknown result type (might be due to invalid IL or missing references)
//IL_0032: Unknown result type (might be due to invalid IL or missing references)
//IL_002e: Unknown result type (might be due to invalid IL or missing references)
switch (annotation)
{
case NullableAnnotation.Annotated:
return (NullableAnnotation)2;
case NullableAnnotation.NotAnnotated:
return (NullableAnnotation)1;
case NullableAnnotation.Oblivious:
if ((object)type != null && type.IsValueType)
{
return (NullableAnnotation)1;
}
return (NullableAnnotation)0;
case NullableAnnotation.Ignored:
return (NullableAnnotation)0;
default:
throw ExceptionUtilities.UnexpectedValue((object)annotation);
}
}
internal static NullableAnnotation ToInternalAnnotation(this NullableAnnotation annotation)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Expected I4, but got Unknown
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
return (int)annotation switch
{
0 => NullableAnnotation.Oblivious,
1 => NullableAnnotation.NotAnnotated,
2 => NullableAnnotation.Annotated,
_ => throw ExceptionUtilities.UnexpectedValue((object)annotation),
};
}
}