Enable nullable: System.Management.Automation.Language.TypeName

This commit is contained in:
xtqqczze 2020-11-23 09:45:07 +00:00
parent fa8b94e6e9
commit 92b609911b

View File

@ -28,6 +28,7 @@ namespace System.Management.Automation.Language
using SwitchClause = Tuple<ExpressionAst, StatementBlockAst>;
using System.Runtime.CompilerServices;
using System.Reflection.Emit;
using System.Diagnostics;
internal interface ISupportsAssignment
{
@ -8175,12 +8176,13 @@ namespace System.Management.Automation.Language
/// <summary>
/// A simple type that is not an array or does not have generic arguments.
/// </summary>
#nullable enable
public sealed class TypeName : ITypeName, ISupportsTypeCaching
{
internal readonly string _name;
internal Type _type;
internal Type? _type;
internal readonly IScriptExtent _extent;
internal TypeDefinitionAst _typeDefinitionAst;
internal TypeDefinitionAst? _typeDefinitionAst;
/// <summary>
/// Construct a simple typename.
@ -8252,7 +8254,7 @@ namespace System.Management.Automation.Language
/// <summary>
/// The name of the assembly, if specified, otherwise null.
/// </summary>
public string AssemblyName { get; internal set; }
public string? AssemblyName { get; internal set; }
/// <summary>
/// Always returns false, array typenames are instances of <see cref="ArrayTypeName"/>.
@ -8273,7 +8275,7 @@ namespace System.Management.Automation.Language
{
if (_typeDefinitionAst == null)
{
Type reflectionType = GetReflectionType();
Type? reflectionType = GetReflectionType();
if (reflectionType == null)
{
// we are pessimistic about default ctor presence.
@ -8312,7 +8314,7 @@ namespace System.Management.Automation.Language
/// The <see cref="Type"/> if possible, null otherwise. Null may be returned for valid typenames if the assembly
/// containing the type has not been loaded.
/// </returns>
public Type GetReflectionType()
public Type? GetReflectionType()
{
if (_type == null)
{
@ -8347,7 +8349,7 @@ namespace System.Management.Automation.Language
/// The <see cref="Type"/> if possible, null otherwise. Null may be returned for valid typenames if the assembly
/// containing the type has not been loaded.
/// </returns>
public Type GetReflectionAttributeType()
public Type? GetReflectionAttributeType()
{
var result = GetReflectionType();
if (result == null || !typeof(Attribute).IsAssignableFrom(result))
@ -8379,7 +8381,7 @@ namespace System.Management.Automation.Language
}
/// <summary/>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (!(obj is TypeName other))
return false;
@ -8418,7 +8420,8 @@ namespace System.Management.Automation.Language
/// </remarks>
internal bool IsType(Type type)
{
string fullTypeName = type.FullName;
string? fullTypeName = type.FullName;
Debug.Assert(fullTypeName is not null);
if (fullTypeName.Equals(Name, StringComparison.OrdinalIgnoreCase))
return true;
int lastDotIndex = fullTypeName.LastIndexOf('.');
@ -8430,7 +8433,7 @@ namespace System.Management.Automation.Language
return false;
}
Type ISupportsTypeCaching.CachedType
Type? ISupportsTypeCaching.CachedType
{
get { return _type; }
@ -8443,8 +8446,8 @@ namespace System.Management.Automation.Language
/// </summary>
public sealed class GenericTypeName : ITypeName, ISupportsTypeCaching
{
private string _cachedFullName;
private Type _cachedType;
private string? _cachedFullName;
private Type? _cachedType;
/// <summary>
/// Construct a generic type name.
@ -8556,7 +8559,7 @@ namespace System.Management.Automation.Language
/// <summary>
/// The name of the assembly, if specified, otherwise null.
/// </summary>
public string AssemblyName { get { return TypeName.AssemblyName; } }
public string? AssemblyName { get { return TypeName.AssemblyName; } }
/// <summary>
/// Always returns false because this class does not represent arrays.
@ -8586,11 +8589,11 @@ namespace System.Management.Automation.Language
/// <summary>
/// Returns the <see cref="System.Type"/> that this typename represents, if such a type exists, null otherwise.
/// </summary>
public Type GetReflectionType()
public Type? GetReflectionType()
{
if (_cachedType == null)
{
Type generic = GetGenericType(TypeName.GetReflectionType());
Type? generic = GetGenericType(TypeName.GetReflectionType());
if (generic != null && generic.ContainsGenericParameters)
{
@ -8639,7 +8642,7 @@ namespace System.Management.Automation.Language
/// </summary>
/// <param name="generic"></param>
/// <returns></returns>
internal Type GetGenericType(Type generic)
internal Type? GetGenericType(Type? generic)
{
if (generic == null || !generic.ContainsGenericParameters)
{
@ -8662,12 +8665,12 @@ namespace System.Management.Automation.Language
/// The <see cref="Type"/> if possible, null otherwise. Null may be returned for valid typenames if the assembly
/// containing the type has not been loaded.
/// </returns>
public Type GetReflectionAttributeType()
public Type? GetReflectionAttributeType()
{
Type type = GetReflectionType();
Type? type = GetReflectionType();
if (type == null)
{
Type generic = TypeName.GetReflectionAttributeType();
Type? generic = TypeName.GetReflectionAttributeType();
if (generic == null || !generic.ContainsGenericParameters)
{
if (!TypeName.FullName.Contains('`'))
@ -8697,7 +8700,7 @@ namespace System.Management.Automation.Language
}
/// <summary/>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (!(obj is GenericTypeName other))
return false;
@ -8731,7 +8734,7 @@ namespace System.Management.Automation.Language
return hash;
}
Type ISupportsTypeCaching.CachedType
Type? ISupportsTypeCaching.CachedType
{
get { return _cachedType; }
@ -8744,8 +8747,8 @@ namespace System.Management.Automation.Language
/// </summary>
public sealed class ArrayTypeName : ITypeName, ISupportsTypeCaching
{
private string _cachedFullName;
private Type _cachedType;
private string? _cachedFullName;
private Type? _cachedType;
/// <summary>
/// Construct an ArrayTypeName.
@ -8836,7 +8839,7 @@ namespace System.Management.Automation.Language
/// <summary>
/// The name of the assembly, if specified, otherwise null.
/// </summary>
public string AssemblyName { get { return ElementType.AssemblyName; } }
public string? AssemblyName { get { return ElementType.AssemblyName; } }
/// <summary>
/// Returns true always as this class represents arrays.
@ -8866,14 +8869,14 @@ namespace System.Management.Automation.Language
/// <summary>
/// Returns the <see cref="System.Type"/> that this typename represents, if such a type exists, null otherwise.
/// </summary>
public Type GetReflectionType()
public Type? GetReflectionType()
{
try
{
RuntimeHelpers.EnsureSufficientExecutionStack();
if (_cachedType == null)
{
Type elementType = ElementType.GetReflectionType();
Type? elementType = ElementType.GetReflectionType();
if (elementType != null)
{
Type type = Rank == 1 ? elementType.MakeArrayType() : elementType.MakeArrayType(Rank);
@ -8908,7 +8911,7 @@ namespace System.Management.Automation.Language
/// <summary>
/// Always return null, arrays can never be an attribute.
/// </summary>
public Type GetReflectionAttributeType()
public Type? GetReflectionAttributeType()
{
return null;
}
@ -8922,7 +8925,7 @@ namespace System.Management.Automation.Language
}
/// <summary/>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (!(obj is ArrayTypeName other))
return false;
@ -8936,7 +8939,7 @@ namespace System.Management.Automation.Language
return Utils.CombineHashCodes(ElementType.GetHashCode(), Rank.GetHashCode());
}
Type ISupportsTypeCaching.CachedType
Type? ISupportsTypeCaching.CachedType
{
get { return _cachedType; }
@ -8981,7 +8984,7 @@ namespace System.Management.Automation.Language
/// <summary>
/// The name of the assembly.
/// </summary>
public string AssemblyName { get { return _type.Assembly.FullName; } }
public string? AssemblyName { get { return _type.Assembly.FullName; } }
/// <summary>
/// Returns true if the type is an array, false otherwise.
@ -9023,7 +9026,7 @@ namespace System.Management.Automation.Language
}
/// <summary/>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (!(obj is ReflectionTypeName other))
return false;
@ -9036,7 +9039,7 @@ namespace System.Management.Automation.Language
return _type.GetHashCode();
}
Type ISupportsTypeCaching.CachedType
Type? ISupportsTypeCaching.CachedType
{
get { return _type; }
@ -9101,6 +9104,7 @@ namespace System.Management.Automation.Language
#endregion Visitors
}
#nullable restore
/// <summary>
/// The ast representing a variable reference, either normal references, e.g. <c>$true</c>, or splatted references