Browse Source

Fixed handling of enum literals.

pull/37/head
Daniel Grunwald 15 years ago
parent
commit
7ca8e36f15
  1. 5
      ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs
  2. 12
      ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs

5
ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using ICSharpCode.NRefactory.Utils;
using Ast = ICSharpCode.NRefactory.CSharp; using Ast = ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.CSharp; using ICSharpCode.NRefactory.CSharp;
using Cecil = Mono.Cecil; using Cecil = Mono.Cecil;
@ -500,11 +501,11 @@ namespace Decompiler
return new Ast.PrimitiveExpression(false); return new Ast.PrimitiveExpression(false);
else if (byteCode.InferredType == typeSystem.Boolean && (int)operand == 1) else if (byteCode.InferredType == typeSystem.Boolean && (int)operand == 1)
return new Ast.PrimitiveExpression(true); return new Ast.PrimitiveExpression(true);
if (byteCode.InferredType != null && byteCode.InferredType.IsValueType) { if (byteCode.InferredType != null) { // cannot rely on IsValueType, it's not set for typerefs (but is set for typespecs)
TypeDefinition enumDefinition = byteCode.InferredType.Resolve(); TypeDefinition enumDefinition = byteCode.InferredType.Resolve();
if (enumDefinition != null && enumDefinition.IsEnum) { if (enumDefinition != null && enumDefinition.IsEnum) {
foreach (FieldDefinition field in enumDefinition.Fields) { foreach (FieldDefinition field in enumDefinition.Fields) {
if (field.IsStatic && object.Equals(field.Constant, operand)) if (field.IsStatic && object.Equals(CSharpPrimitiveCast.Cast(TypeCode.Int32, field.Constant, false), operand))
return AstBuilder.ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field); return AstBuilder.ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field);
} }
} }

12
ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs

@ -620,13 +620,11 @@ namespace Decompiler
{ {
if (type == null) if (type == null)
return null; return null;
if (type.IsValueType) { // unfortunately we cannot rely on type.IsValueType here - it's not set when the instruction operand is a typeref (as opposed to a typespec)
// value type might be an enum TypeDefinition typeDef = type.Resolve() as TypeDefinition;
TypeDefinition typeDef = type.Resolve() as TypeDefinition; if (typeDef != null && typeDef.IsEnum) {
if (typeDef != null && typeDef.IsEnum) { TypeReference underlyingType = typeDef.Fields.Single(f => f.IsRuntimeSpecialName && !f.IsStatic).FieldType;
TypeReference underlyingType = typeDef.Fields.Single(f => f.IsRuntimeSpecialName && !f.IsStatic).FieldType; return IsSigned(typeDef.Module.TypeSystem, underlyingType);
return IsSigned(typeDef.Module.TypeSystem, underlyingType);
}
} }
if (type == typeSystem.Byte || type == typeSystem.UInt16 || type == typeSystem.UInt32 || type == typeSystem.UInt64 || type == typeSystem.UIntPtr) if (type == typeSystem.Byte || type == typeSystem.UInt16 || type == typeSystem.UInt32 || type == typeSystem.UInt64 || type == typeSystem.UIntPtr)
return false; return false;

Loading…
Cancel
Save