Browse Source

fixed printing of enum values that cannot be mapped to enum named constants.

pull/52/head
Artur Zgodziski 15 years ago
parent
commit
464acf02ea
  1. 17
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 2
      ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
  3. 39
      ICSharpCode.Decompiler/Tests/Types/EnumSamples.cs
  4. 17
      ICSharpCode.Decompiler/Tests/Types/EnumTests.cs

17
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -515,6 +515,7 @@ namespace Decompiler @@ -515,6 +515,7 @@ namespace Decompiler
Body = AstMethodBodyBuilder.CreateMethodBody(propDef.SetMethod, context)
}.WithAnnotation(propDef.SetMethod);
}
ConvertCustomAtributes(astProp, propDef);
return astProp;
}
@ -552,6 +553,7 @@ namespace Decompiler @@ -552,6 +553,7 @@ namespace Decompiler
else
initializer.Initializer = new PrimitiveExpression(fieldDef.Constant);
}
ConvertCustomAtributes(astField, fieldDef);
return astField;
}
@ -619,12 +621,13 @@ namespace Decompiler @@ -619,12 +621,13 @@ namespace Decompiler
foreach (FieldDefinition field in enumDefinition.Fields)
{
if (field.IsStatic && object.Equals(CSharpPrimitiveCast.Cast(TypeCode.Int64, field.Constant, false), val))
return AstBuilder.ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field);
return ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field);
else if (!field.IsStatic && field.IsRuntimeSpecialName)
type = field.FieldType; // use primitive type of the enum
}
if (IsFlagsEnum(enumDefinition))
{
long enumValue = val;
Expression expr = null;
foreach (FieldDefinition field in enumDefinition.Fields.Where(fld => fld.IsStatic))
{
@ -632,18 +635,24 @@ namespace Decompiler @@ -632,18 +635,24 @@ namespace Decompiler
if (fieldValue == 0)
continue; // skip None enum value
if ((fieldValue & val) == fieldValue)
if ((fieldValue & enumValue) == fieldValue)
{
var fieldExpression = AstBuilder.ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field);
var fieldExpression = ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field);
if (expr == null)
expr = fieldExpression;
else
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.BitwiseOr, fieldExpression);
enumValue &= ~fieldValue;
if (enumValue == 0)
break;
}
}
if (expr != null)
if(enumValue == 0 && expr != null)
return expr;
}
TypeCode enumBaseTypeCode = TypeAnalysis.GetTypeCode(type);
return new Ast.PrimitiveExpression(CSharpPrimitiveCast.Cast(enumBaseTypeCode, val, false)).CastTo(ConvertType(enumDefinition));
}
}
TypeCode code = TypeAnalysis.GetTypeCode(type);

2
ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj

@ -56,6 +56,8 @@ @@ -56,6 +56,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="Types\EnumSamples.cs" />
<Compile Include="Types\EnumTests.cs" />
<Compile Include="Types\TypeTests.cs" />
<None Include="Types\DelegateConstruction.cs" />
<None Include="CustomAttributes\CustomAttributes.cs" />

39
ICSharpCode.Decompiler/Tests/Types/EnumSamples.cs

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
//$CS
using System;
//$CE
//$$ SingleValue
public class TS_SingleValue
{
public AttributeTargets Method()
{
return AttributeTargets.Class;
}
}
//$$ TwoValuesOr
public class TS_TwoValuesOr
{
public AttributeTargets Method()
{
return AttributeTargets.Class | AttributeTargets.Method;
}
}
//$$ ThreeValuesOr
public class TS_ThreeValuesOr
{
public AttributeTargets Method()
{
return AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Parameter;
}
}
//$$ UnknownNumericValue
public class TS_UnknownNumericValue
{
public AttributeTargets Method()
{
return (AttributeTargets)1000000;
}
}

17
ICSharpCode.Decompiler/Tests/Types/EnumTests.cs

@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MbUnit.Framework;
namespace ICSharpCode.Decompiler.Tests.Types
{
public class EnumTests : DecompilerTestBase
{
[StaticTestFactory]
public static IEnumerable<Test> EnumSamples()
{
return GenerateSectionTests(@"Types\EnumSamples.cs");
}
}
}
Loading…
Cancel
Save