Browse Source

Printing initializers of enum members.

pull/52/head
Artur Zgodziski 15 years ago
parent
commit
fde3c114ec
  1. 6
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 65
      ICSharpCode.Decompiler/Tests/Types/EnumSamples.cs
  3. 7
      NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

6
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -161,6 +161,7 @@ namespace Decompiler @@ -161,6 +161,7 @@ namespace Decompiler
if (typeDef.IsEnum) {
long expectedEnumMemberValue = 0;
foreach (FieldDefinition field in typeDef.Fields) {
if (field.IsRuntimeSpecialName) {
// the value__ field
@ -168,6 +169,11 @@ namespace Decompiler @@ -168,6 +169,11 @@ namespace Decompiler
} else {
EnumMemberDeclaration enumMember = new EnumMemberDeclaration();
enumMember.Name = CleanName(field.Name);
long memberValue = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, field.Constant, false);
if (memberValue != expectedEnumMemberValue) {
enumMember.AddChild(new PrimitiveExpression(field.Constant), EnumMemberDeclaration.InitializerRole);
}
expectedEnumMemberValue = memberValue + 1;
astType.AddChild(enumMember, TypeDeclaration.MemberRole);
}
}

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

@ -37,3 +37,68 @@ public class TS_UnknownNumericValue @@ -37,3 +37,68 @@ public class TS_UnknownNumericValue
return (AttributeTargets)1000000;
}
}
//$$ AllValue
public class TS_AllValue
{
public AttributeTargets Method()
{
return AttributeTargets.All;
}
}
//$$ ZeroValue
public class TS_ZeroValue
{
public AttributeTargets Method()
{
return (AttributeTargets)0;
}
}
//$$ PreservingTypeWhenBoxed
public class TS_PreservingTypeWhenBoxed
{
public object Method()
{
return AttributeTargets.Delegate;
}
}
//$$ PreservingTypeWhenBoxedTwoEnum
public class TS_PreservingTypeWhenBoxedTwoEnum
{
public object Method()
{
return AttributeTargets.Class | AttributeTargets.Delegate;
}
}
//$$ DeclarationSimpleEnum
public enum TS_DeclarationSimpleEnum
{
Item1,
Item2
}
//$$ DeclarationLongBasedEnum
public enum TS_DeclarationLongBasedEnum : long
{
Item1,
Item2
}
//$$ DeclarationLongWithInitializers
public enum TS_DeclarationLongWithInitializers : long
{
Item1,
Item2 = 20L,
Item3
}
//$$ DeclarationShortWithInitializers
public enum TS_DeclarationShortWithInitializers : short
{
Item1,
Item2 = 20,
Item3
}
//$$ DeclarationByteWithInitializers
public enum TS_DeclarationByteWithInitializers : byte
{
Item1,
Item2 = 20,
Item3
}

7
NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

@ -146,13 +146,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -146,13 +146,14 @@ namespace ICSharpCode.NRefactory.CSharp
/// Writes a comma.
/// </summary>
/// <param name="nextNode">The next node after the comma.</param>
void Comma(AstNode nextNode)
/// <param name="noSpacesAfterComma">When set prevents printing a space after comma.</param>
void Comma(AstNode nextNode, bool noSpacesAfterComma = false)
{
WriteSpecialsUpToRole(AstNode.Roles.Comma, nextNode);
Space(policy.SpacesBeforeComma);
formatter.WriteToken(",");
lastWritten = LastWritten.Other;
Space(policy.SpacesAfterComma);
Space(!noSpacesAfterComma && policy.SpacesAfterComma);
}
void WriteCommaSeparatedList(IEnumerable<AstNode> list)
@ -1187,7 +1188,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1187,7 +1188,7 @@ namespace ICSharpCode.NRefactory.CSharp
if (first) {
first = false;
} else {
Comma(member);
Comma(member, noSpacesAfterComma: true);
NewLine();
}
member.AcceptVisitor(this, data);

Loading…
Cancel
Save