Browse Source

Merge pull request #2749 from icsharpcode/unsafe-sizeof

pull/2755/head
Siegfried Pammer 3 years ago committed by GitHub
parent
commit
74a370d5f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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 @@ -207,9 +207,9 @@ namespace System.Runtime.CompilerServices
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static int SizeOf<T>()
public static int SizeOf<T>()
{
return sizeof(T);
return Unsafe.SizeOf<T>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -307,7 +307,7 @@ namespace System.Runtime.CompilerServices @@ -307,7 +307,7 @@ namespace System.Runtime.CompilerServices
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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)]
@ -343,7 +343,7 @@ namespace System.Runtime.CompilerServices @@ -343,7 +343,7 @@ namespace System.Runtime.CompilerServices
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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)]

19
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -648,9 +648,22 @@ namespace ICSharpCode.Decompiler.CSharp @@ -648,9 +648,22 @@ namespace ICSharpCode.Decompiler.CSharp
protected internal override TranslatedExpression VisitSizeOf(SizeOf inst, TranslationContext context)
{
return new SizeOfExpression(ConvertType(inst.Type))
.WithILInstruction(inst)
.WithRR(new SizeOfResolveResult(compilation.FindType(KnownTypeCode.Int32), inst.Type, null));
if (inst.Type.IsUnmanagedType(allowGenerics: settings.IntroduceUnmanagedConstraint))
{
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)

3
ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

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

Loading…
Cancel
Save