Browse Source

Fixed 'as' and 'is' operators for value types.

pull/124/head
Daniel Grunwald 14 years ago
parent
commit
d550d55560
  1. 7
      ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs
  2. 8
      ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs
  3. 15
      ICSharpCode.Decompiler/Tests/ValueTypes.cs

7
ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs

@ -447,7 +447,12 @@ namespace ICSharpCode.Decompiler.Ast @@ -447,7 +447,12 @@ namespace ICSharpCode.Decompiler.Ast
case ILCode.Conv_Ovf_I_Un: return arg1.CastTo(typeof(IntPtr));
case ILCode.Conv_Ovf_U_Un: return arg1.CastTo(typeof(UIntPtr));
case ILCode.Castclass: return arg1.CastTo(operandAsTypeRef);
case ILCode.Unbox_Any: return arg1.CastTo(operandAsTypeRef);
case ILCode.Unbox_Any:
// unboxing does not require a cast if the argument was an isinst instruction
if (arg1 is AsExpression && byteCode.Arguments[0].Code == ILCode.Isinst && TypeAnalysis.IsSameType(operand as TypeReference, byteCode.Arguments[0].Operand as TypeReference))
return arg1;
else
return arg1.CastTo(operandAsTypeRef);
case ILCode.Isinst: return arg1.CastAs(operandAsTypeRef);
case ILCode.Box: return arg1;
case ILCode.Unbox: return InlineAssembly(byteCode, args);

8
ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs

@ -648,9 +648,15 @@ namespace ICSharpCode.Decompiler.ILAst @@ -648,9 +648,15 @@ namespace ICSharpCode.Decompiler.ILAst
case ILCode.Conv_R_Un:
return (expectedType != null && expectedType.MetadataType == MetadataType.Single) ? typeSystem.Single : typeSystem.Double;
case ILCode.Castclass:
case ILCode.Isinst:
case ILCode.Unbox_Any:
return (TypeReference)expr.Operand;
case ILCode.Isinst:
{
// isinst performs the equivalent of a cast only for reference types;
// value types still need to be unboxed after an isinst instruction
TypeReference tr = (TypeReference)expr.Operand;
return tr.IsValueType ? typeSystem.Object : tr;
}
case ILCode.Box:
if (forceInferChildren)
InferTypeForExpression(expr.Arguments.Single(), (TypeReference)expr.Operand);

15
ICSharpCode.Decompiler/Tests/ValueTypes.cs

@ -111,4 +111,19 @@ public static class ValueTypes @@ -111,4 +111,19 @@ public static class ValueTypes
{
ValueTypes.MakeArray()[Environment.TickCount]++;
}
public static bool Is(object obj)
{
return obj is ValueTypes.S;
}
public static bool IsNullable(object obj)
{
return obj is ValueTypes.S?;
}
public static ValueTypes.S? As(object obj)
{
return obj as ValueTypes.S?;
}
}

Loading…
Cancel
Save