Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.

#if !NET
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System
{
static class ArgumentNullExceptionShims
{
extension(ArgumentNullException)
{
public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (argument is null)
{
ThrowNullReferenceException(paramName);
}
}
}

[DoesNotReturn]
static void ThrowNullReferenceException(string? paramName) => throw new ArgumentNullException(paramName);
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.

#if !NET
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter)]
sealed class CallerArgumentExpressionAttribute(string parameterName) : Attribute
{
public string ParameterName { get; } = parameterName;
}
}
#endif
48 changes: 42 additions & 6 deletions src/TypeNameInterpretation/InsAssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public static class InsAssemblyExtensions
{
public static bool TryGetVersion(this InsAssembly assembly, [NotNullWhen(true)] out Version? version)
{
ArgumentNullException.ThrowIfNull(assembly);

if (!assembly.TryGetQualification(WellKnownQualificationNames.Version, out var value))
{
version = null;
Expand All @@ -26,6 +28,8 @@ public static bool TryGetVersion(this InsAssembly assembly, [NotNullWhen(true)]

public static bool TryGetPublicKey(this InsAssembly assembly, out byte[]? publicKey)
{
ArgumentNullException.ThrowIfNull(assembly);

if (!assembly.TryGetQualification(WellKnownQualificationNames.PublicKey, out var value))
{
publicKey = null;
Expand All @@ -42,6 +46,8 @@ public static bool TryGetPublicKey(this InsAssembly assembly, out byte[]? public

public static bool TryGetPublicKeyToken(this InsAssembly assembly, out byte[]? publicKeyToken)
{
ArgumentNullException.ThrowIfNull(assembly);

if (!assembly.TryGetQualification(WellKnownQualificationNames.PublicKeyToken, out var value))
{
publicKeyToken = null;
Expand All @@ -58,6 +64,8 @@ public static bool TryGetPublicKeyToken(this InsAssembly assembly, out byte[]? p

public static bool TryGetProcessorArchitecture(this InsAssembly assembly, out ProcessorArchitecture processorArchitecture)
{
ArgumentNullException.ThrowIfNull(assembly);

if (!assembly.TryGetQualification(WellKnownQualificationNames.ProcessorArchitecture, out var value))
{
processorArchitecture = default;
Expand All @@ -74,6 +82,9 @@ public static bool TryGetProcessorArchitecture(this InsAssembly assembly, out Pr

public static bool TryGetQualification(this InsAssembly assembly, string name, [NotNullWhen(true)] out string? value)
{
ArgumentNullException.ThrowIfNull(assembly);
ArgumentNullException.ThrowIfNull(name);

foreach (var qualification in assembly.Qualifications)
{
if (qualification.Name == name)
Expand All @@ -88,13 +99,22 @@ public static bool TryGetQualification(this InsAssembly assembly, string name, [
}

public static InsAssembly WithVersion(this InsAssembly assembly, Version version)
=> assembly.WithQualification(WellKnownQualificationNames.Version, version.ToString());
{
ArgumentNullException.ThrowIfNull(assembly);
ArgumentNullException.ThrowIfNull(version);
return assembly.WithQualification(WellKnownQualificationNames.Version, version.ToString());
}

public static InsAssembly WithPublicKey(this InsAssembly assembly, ReadOnlySpan<byte> publicKey)
=> assembly.WithQualification(WellKnownQualificationNames.PublicKey, FormatBlob(publicKey));
{
ArgumentNullException.ThrowIfNull(assembly);
return assembly.WithQualification(WellKnownQualificationNames.PublicKey, FormatBlob(publicKey));
}

public static InsAssembly WithPublicKey(this InsAssembly assembly, byte[]? publicKey)
{
ArgumentNullException.ThrowIfNull(assembly);

if (publicKey == null)
{
return assembly.WithQualification(WellKnownQualificationNames.PublicKey, NullBlob);
Expand All @@ -106,10 +126,15 @@ public static InsAssembly WithPublicKey(this InsAssembly assembly, byte[]? publi
}

public static InsAssembly WithPublicKeyToken(this InsAssembly assembly, ReadOnlySpan<byte> publicKeyToken)
=> assembly.WithQualification(WellKnownQualificationNames.PublicKeyToken, FormatBlob(publicKeyToken));
{
ArgumentNullException.ThrowIfNull(assembly);
return assembly.WithQualification(WellKnownQualificationNames.PublicKeyToken, FormatBlob(publicKeyToken));
}

public static InsAssembly WithPublicKeyToken(this InsAssembly assembly, byte[]? publicKeyToken)
{
ArgumentNullException.ThrowIfNull(assembly);

if (publicKeyToken == null)
{
return assembly.WithQualification(WellKnownQualificationNames.PublicKeyToken, NullBlob);
Expand All @@ -121,13 +146,24 @@ public static InsAssembly WithPublicKeyToken(this InsAssembly assembly, byte[]?
}

public static InsAssembly WithProcessorArchitecture(this InsAssembly assembly, ProcessorArchitecture processorArchitecture)
=> assembly.WithQualification(WellKnownQualificationNames.ProcessorArchitecture, processorArchitecture.ToString());
{
ArgumentNullException.ThrowIfNull(assembly);
return assembly.WithQualification(WellKnownQualificationNames.ProcessorArchitecture, processorArchitecture.ToString());
}

public static InsAssembly WithQualification(this InsAssembly assembly, string name, string value)
=> assembly.WithQualifications(assembly.Qualifications.WithQualification(name, value));
{
ArgumentNullException.ThrowIfNull(assembly);
ArgumentNullException.ThrowIfNull(name);
return assembly.WithQualifications(assembly.Qualifications.WithQualification(name, value));
}

public static InsAssembly WithoutQualification(this InsAssembly assembly, string name)
=> assembly.WithQualifications(assembly.Qualifications.WithoutQualification(name));
{
ArgumentNullException.ThrowIfNull(assembly);
ArgumentNullException.ThrowIfNull(name);
return assembly.WithQualifications(assembly.Qualifications.WithoutQualification(name));
}

static InsAssembly WithQualifications(this InsAssembly assembly, ImmutableArray<InsAssemblyQualification> newQualifications)
{
Expand Down
32 changes: 11 additions & 21 deletions src/TypeNameInterpretation/InsFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,28 @@ public static class InsFormatter
{
public static StringBuilder Write(StringBuilder builder, InsType type)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(type);
return Writer.Instance.WriteComplexType(type, builder);
}

public static StringBuilder Write(StringBuilder builder, InsAssembly assembly)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}

ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(assembly);
return Writer.WriteAssembly(assembly, builder);
}

public static string Format(InsType type) => Write(BuilderPool.Rent(), type).ToStringAndReturn();
public static string Format(InsType type)
{
ArgumentNullException.ThrowIfNull(type);
return Write(BuilderPool.Rent(), type).ToStringAndReturn();
}

public static string Format(InsAssembly assembly)
{
ArgumentNullException.ThrowIfNull(assembly);

if (assembly.Qualifications.Length == 0 && assembly.Name.AsSpan().IndexOfAny(Delimiters.All) < 0)
{
return assembly.Name;
Expand Down
42 changes: 7 additions & 35 deletions src/TypeNameInterpretation/InsRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ public abstract class InsRewriter<TArgument> : IInsTypeVisitor<TArgument, InsTyp
{
public virtual InsType VisitArray(InsArrayType type, TArgument argument)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

ArgumentNullException.ThrowIfNull(type);
var elementType = type.ElementType.Apply(this, argument);

if (elementType == type.ElementType)
Expand All @@ -25,11 +21,7 @@ public virtual InsType VisitArray(InsArrayType type, TArgument argument)

public virtual InsType VisitByRef(InsByRefType type, TArgument argument)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

ArgumentNullException.ThrowIfNull(type);
var inner = type.ElementType.Apply(this, argument);

if (inner == type.ElementType)
Expand All @@ -42,11 +34,7 @@ public virtual InsType VisitByRef(InsByRefType type, TArgument argument)

public virtual InsType VisitGeneric(InsGenericType type, TArgument argument)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

ArgumentNullException.ThrowIfNull(type);
var definition = (InsNamedType)type.Definition.Apply(this, argument);
var typeArguments = VisitTypes(type.TypeArguments, argument);

Expand All @@ -60,11 +48,7 @@ public virtual InsType VisitGeneric(InsGenericType type, TArgument argument)

public virtual InsType VisitNamed(InsNamedType type, TArgument argument)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

ArgumentNullException.ThrowIfNull(type);
if (type.DeclaringType != null)
{
var declaringType = (InsNamedType)type.DeclaringType.Apply(this, argument);
Expand All @@ -91,11 +75,7 @@ public virtual InsType VisitNamed(InsNamedType type, TArgument argument)

public virtual InsType VisitPointer(InsPointerType type, TArgument argument)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

ArgumentNullException.ThrowIfNull(type);
var inner = type.ElementType.Apply(this, argument);

if (inner == type.ElementType)
Expand All @@ -108,11 +88,7 @@ public virtual InsType VisitPointer(InsPointerType type, TArgument argument)

public virtual InsType VisitSZArray(InsSZArrayType type, TArgument argument)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

ArgumentNullException.ThrowIfNull(type);
var elementType = type.ElementType.Apply(this, argument);

if (elementType == type.ElementType)
Expand Down Expand Up @@ -149,11 +125,7 @@ public virtual ImmutableArray<InsType> VisitTypes(ImmutableArray<InsType> typeAr

public virtual InsAssembly VisitAssembly(InsAssembly assembly, TArgument argument)
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}

ArgumentNullException.ThrowIfNull(assembly);
var qualifications = VisitAssemblyQualifications(assembly.Qualifications, argument);

if (qualifications == assembly.Qualifications)
Expand Down
6 changes: 1 addition & 5 deletions src/TypeNameInterpretation/Tree/InsArrayType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ internal InsArrayType(InsType elementType, int rank)

public override TReturn Apply<TArgument, TReturn>(IInsTypeVisitor<TArgument, TReturn> visitor, TArgument argument)
{
if (visitor == null)
{
throw new ArgumentNullException(nameof(visitor));
}

ArgumentNullException.ThrowIfNull(visitor);
return visitor.VisitArray(this, argument);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/TypeNameInterpretation/Tree/InsAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public sealed class InsAssembly
{
internal InsAssembly(string name, ImmutableArray<InsAssemblyQualification> qualifications)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
ArgumentNullException.ThrowIfNull(name);
Name = name;
Qualifications = qualifications;
}

Expand Down
6 changes: 4 additions & 2 deletions src/TypeNameInterpretation/Tree/InsAssemblyQualification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ public sealed class InsAssemblyQualification
{
internal InsAssemblyQualification(string name, string value)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Value = value ?? throw new ArgumentNullException(nameof(value));
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(value);
Name = name;
Value = value;
}

public string Name { get; }
Expand Down
6 changes: 1 addition & 5 deletions src/TypeNameInterpretation/Tree/InsByRefType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ internal InsByRefType(InsType elementType)

public override TReturn Apply<TArgument, TReturn>(IInsTypeVisitor<TArgument, TReturn> visitor, TArgument argument)
{
if (visitor == null)
{
throw new ArgumentNullException(nameof(visitor));
}

ArgumentNullException.ThrowIfNull(visitor);
return visitor.VisitByRef(this, argument);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/TypeNameInterpretation/Tree/InsElementedType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public abstract class InsElementedType : InsType
{
private protected InsElementedType(InsType elementType)
{
ElementType = elementType ?? throw new ArgumentNullException(nameof(elementType));
ArgumentNullException.ThrowIfNull(elementType);
ElementType = elementType;
}

public InsType ElementType { get; }
Expand Down
9 changes: 3 additions & 6 deletions src/TypeNameInterpretation/Tree/InsGenericType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public sealed class InsGenericType : InsType
{
internal InsGenericType(InsNamedType definition, ImmutableArray<InsType> typeArguments)
{
Definition = definition ?? throw new ArgumentNullException(nameof(definition));
ArgumentNullException.ThrowIfNull(definition);
Definition = definition;
TypeArguments = typeArguments;
}

Expand All @@ -18,11 +19,7 @@ internal InsGenericType(InsNamedType definition, ImmutableArray<InsType> typeArg

public override TReturn Apply<TArgument, TReturn>(IInsTypeVisitor<TArgument, TReturn> visitor, TArgument argument)
{
if (visitor == null)
{
throw new ArgumentNullException(nameof(visitor));
}

ArgumentNullException.ThrowIfNull(visitor);
return visitor.VisitGeneric(this, argument);
}
}
Expand Down
Loading