Browse Source

Use Unsafe.SizeOf when taking the size of a managed type.

pull/2749/head
Siegfried Pammer 3 years ago
parent
commit
48a8351e27
  1. 8
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Unsafe.cs
  2. 19
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  3. 3
      ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

8
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Unsafe.cs

@ -207,9 +207,9 @@ namespace System.Runtime.CompilerServices
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static int SizeOf<T>() public static int SizeOf<T>()
{ {
return sizeof(T); return Unsafe.SizeOf<T>();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -307,7 +307,7 @@ namespace System.Runtime.CompilerServices
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void* Add<T>(void* source, int elementOffset) public unsafe static void* Add<T>(void* source, int elementOffset)
{ {
return (byte*)source + (nint)elementOffset * (nint)sizeof(T); return (byte*)source + (nint)elementOffset * (nint)Unsafe.SizeOf<T>();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -343,7 +343,7 @@ namespace System.Runtime.CompilerServices
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void* Subtract<T>(void* source, int elementOffset) public unsafe static void* Subtract<T>(void* source, int elementOffset)
{ {
return (byte*)source - (nint)elementOffset * (nint)sizeof(T); return (byte*)source - (nint)elementOffset * (nint)Unsafe.SizeOf<T>();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

19
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -648,9 +648,22 @@ namespace ICSharpCode.Decompiler.CSharp
protected internal override TranslatedExpression VisitSizeOf(SizeOf inst, TranslationContext context) protected internal override TranslatedExpression VisitSizeOf(SizeOf inst, TranslationContext context)
{ {
return new SizeOfExpression(ConvertType(inst.Type)) if (inst.Type.IsUnmanagedType(allowGenerics: settings.IntroduceUnmanagedConstraint))
.WithILInstruction(inst) {
.WithRR(new SizeOfResolveResult(compilation.FindType(KnownTypeCode.Int32), inst.Type, null)); return new SizeOfExpression(ConvertType(inst.Type))
.WithILInstruction(inst)
.WithRR(new SizeOfResolveResult(compilation.FindType(KnownTypeCode.Int32), inst.Type, null));
}
else
{
return CallUnsafeIntrinsic(
name: "SizeOf",
arguments: Array.Empty<Expression>(),
returnType: compilation.FindType(KnownTypeCode.Int32),
inst: inst,
typeArguments: new[] { inst.Type }
);
}
} }
protected internal override TranslatedExpression VisitLdTypeToken(LdTypeToken inst, TranslationContext context) protected internal override TranslatedExpression VisitLdTypeToken(LdTypeToken inst, TranslationContext context)

3
ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

@ -290,13 +290,16 @@ namespace ICSharpCode.Decompiler.TypeSystem
{ {
if (types.Contains(f.Type)) if (types.Contains(f.Type))
{ {
types.Remove(type);
return false; return false;
} }
if (!IsUnmanagedTypeInternal(f.Type)) if (!IsUnmanagedTypeInternal(f.Type))
{ {
types.Remove(type);
return false; return false;
} }
} }
types.Remove(type);
return true; return true;
} }
return false; return false;

Loading…
Cancel
Save