Browse Source

Merge pull request #2749 from icsharpcode/unsafe-sizeof

pull/2755/head
Siegfried Pammer 4 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. 13
      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)]

13
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

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