Skip to content

Commit 96caa4e

Browse files
Fix: #3407 Add "private protected" feature for 7.2 decompiler options
1 parent 128f83d commit 96caa4e

File tree

8 files changed

+67
-9
lines changed

8 files changed

+67
-9
lines changed

ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ static TypeSystemAstBuilder CreateAstBuilder(DecompilerSettings settings)
528528
{
529529
var typeSystemAstBuilder = new TypeSystemAstBuilder();
530530
typeSystemAstBuilder.ShowAttributes = true;
531+
typeSystemAstBuilder.UsePrivateProtectedAccessibility = settings.IntroducePrivateProtectedAccessibility;
531532
typeSystemAstBuilder.SortAttributes = settings.SortCustomAttributes;
532533
typeSystemAstBuilder.AlwaysUseShortTypeNames = true;
533534
typeSystemAstBuilder.AddResolveResultAnnotations = true;

ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpAmbience.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ TypeSystemAstBuilder CreateAstBuilder()
236236
astBuilder.ShowTypeParametersForUnboundTypes = true;
237237
astBuilder.ShowModifiers = (ConversionFlags & ConversionFlags.ShowModifiers) == ConversionFlags.ShowModifiers;
238238
astBuilder.ShowAccessibility = (ConversionFlags & ConversionFlags.ShowAccessibility) == ConversionFlags.ShowAccessibility;
239+
astBuilder.UsePrivateProtectedAccessibility = (ConversionFlags & ConversionFlags.UsePrivateProtectedAccessibility) == ConversionFlags.UsePrivateProtectedAccessibility;
239240
astBuilder.AlwaysUseShortTypeNames = (ConversionFlags & ConversionFlags.UseFullyQualifiedTypeNames) != ConversionFlags.UseFullyQualifiedTypeNames;
240241
astBuilder.ShowParameterNames = (ConversionFlags & ConversionFlags.ShowParameterNames) == ConversionFlags.ShowParameterNames;
241242
astBuilder.UseNullableSpecifierForValueTypes = (ConversionFlags & ConversionFlags.UseNullableSpecifierForValueTypes) != 0;

ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void InitProperties()
7070
this.UseKeywordsForBuiltinTypes = true;
7171
this.UseNullableSpecifierForValueTypes = true;
7272
this.ShowAccessibility = true;
73+
this.UsePrivateProtectedAccessibility = true;
7374
this.ShowModifiers = true;
7475
this.ShowBaseTypes = true;
7576
this.ShowTypeParameters = true;
@@ -93,13 +94,19 @@ void InitProperties()
9394
public bool AddResolveResultAnnotations { get; set; }
9495

9596
/// <summary>
96-
/// Controls the accessibility modifiers are shown.
97+
/// Controls whether accessibility modifiers are shown.
9798
/// The default value is <see langword="true" />.
9899
/// </summary>
99100
public bool ShowAccessibility { get; set; }
100101

101102
/// <summary>
102-
/// Controls the non-accessibility modifiers are shown.
103+
/// Controls whether "private protected" accessibility modifiers are shown.
104+
/// The default value is <see langword="true" />.
105+
/// </summary>
106+
public bool UsePrivateProtectedAccessibility { get; set; }
107+
108+
/// <summary>
109+
/// Controls whether non-accessibility modifiers are shown.
103110
/// The default value is <see langword="true" />.
104111
/// </summary>
105112
public bool ShowModifiers { get; set; }
@@ -1805,7 +1812,7 @@ EntityDeclaration ConvertTypeDefinition(ITypeDefinition typeDefinition)
18051812
Modifiers modifiers = Modifiers.None;
18061813
if (this.ShowAccessibility)
18071814
{
1808-
modifiers |= ModifierFromAccessibility(typeDefinition.Accessibility);
1815+
modifiers |= ModifierFromAccessibility(typeDefinition.Accessibility, UsePrivateProtectedAccessibility);
18091816
}
18101817
if (this.ShowModifiers)
18111818
{
@@ -2073,7 +2080,7 @@ Accessor ConvertAccessor(IMethod accessor, MethodSemanticsAttributes kind, Acces
20732080
}
20742081
}
20752082
if (this.ShowAccessibility && accessor.Accessibility != ownerAccessibility)
2076-
decl.Modifiers = ModifierFromAccessibility(accessor.Accessibility);
2083+
decl.Modifiers = ModifierFromAccessibility(accessor.Accessibility, UsePrivateProtectedAccessibility);
20772084
if (this.ShowModifiers && accessor.HasReadonlyModifier())
20782085
decl.Modifiers |= Modifiers.Readonly;
20792086
TokenRole keywordRole = kind switch {
@@ -2342,7 +2349,7 @@ DestructorDeclaration ConvertDestructor(IMethod dtor)
23422349
#endregion
23432350

23442351
#region Convert Modifiers
2345-
public static Modifiers ModifierFromAccessibility(Accessibility accessibility)
2352+
public static Modifiers ModifierFromAccessibility(Accessibility accessibility, bool usePrivateProtected)
23462353
{
23472354
switch (accessibility)
23482355
{
@@ -2357,7 +2364,7 @@ public static Modifiers ModifierFromAccessibility(Accessibility accessibility)
23572364
case Accessibility.ProtectedOrInternal:
23582365
return Modifiers.Protected | Modifiers.Internal;
23592366
case Accessibility.ProtectedAndInternal:
2360-
return Modifiers.Private | Modifiers.Protected;
2367+
return usePrivateProtected ? Modifiers.Private | Modifiers.Protected : Modifiers.Protected;
23612368
default:
23622369
return Modifiers.None;
23632370
}
@@ -2388,7 +2395,7 @@ Modifiers GetMemberModifiers(IMember member)
23882395
Modifiers m = Modifiers.None;
23892396
if (this.ShowAccessibility && NeedsAccessibility(member))
23902397
{
2391-
m |= ModifierFromAccessibility(member.Accessibility);
2398+
m |= ModifierFromAccessibility(member.Accessibility, UsePrivateProtectedAccessibility);
23922399
}
23932400
if (this.ShowModifiers)
23942401
{

ICSharpCode.Decompiler/DecompilerSettings.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public void SetLanguageVersion(CSharp.LanguageVersion languageVersion)
112112
introduceRefModifiersOnStructs = false;
113113
nonTrailingNamedArguments = false;
114114
refExtensionMethods = false;
115+
introducePrivateProtectedAccessibilty = false;
115116
}
116117
if (languageVersion < CSharp.LanguageVersion.CSharp7_3)
117118
{
@@ -185,7 +186,7 @@ public CSharp.LanguageVersion GetMinimumRequiredVersion()
185186
|| patternBasedFixedStatement)
186187
return CSharp.LanguageVersion.CSharp7_3;
187188
if (introduceRefModifiersOnStructs || introduceReadonlyAndInModifiers
188-
|| nonTrailingNamedArguments || refExtensionMethods)
189+
|| nonTrailingNamedArguments || refExtensionMethods || introducePrivateProtectedAccessibilty)
189190
return CSharp.LanguageVersion.CSharp7_2;
190191
// C# 7.1 missing
191192
if (outVariables || throwExpressions || tupleTypes || tupleConversions
@@ -1418,6 +1419,24 @@ public bool IntroduceReadonlyAndInModifiers {
14181419
}
14191420
}
14201421

1422+
bool introducePrivateProtectedAccessibilty = true;
1423+
1424+
/// <summary>
1425+
/// Gets/Sets whether "private protected" should be used.
1426+
/// </summary>
1427+
[Category("C# 7.2 / VS 2017.4")]
1428+
[Description("DecompilerSettings.IntroducePrivateProtectedAccessibility")]
1429+
public bool IntroducePrivateProtectedAccessibility {
1430+
get { return introducePrivateProtectedAccessibilty; }
1431+
set {
1432+
if (introducePrivateProtectedAccessibilty != value)
1433+
{
1434+
introducePrivateProtectedAccessibilty = value;
1435+
OnPropertyChanged();
1436+
}
1437+
}
1438+
}
1439+
14211440
bool readOnlyMethods = true;
14221441

14231442
[Category("C# 8.0 / VS 2019")]

ICSharpCode.Decompiler/Output/IAmbience.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,14 @@ public enum ConversionFlags
117117
/// Support C# 11 <c>operator checked</c>.
118118
/// </summary>
119119
SupportOperatorChecked = 0x100000,
120+
/// <summary>
121+
/// Support C# 7.2 <c>private protected</c>.
122+
/// </summary>
123+
UsePrivateProtectedAccessibility = 0x200000,
120124

121125
StandardConversionFlags = ShowParameterNames |
122126
ShowAccessibility |
127+
UsePrivateProtectedAccessibility |
123128
ShowParameterList |
124129
ShowParameterModifiers |
125130
ShowParameterDefaultValues |

ILSpy/Properties/Resources.Designer.cs

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ILSpy/Properties/Resources.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ Are you sure you want to continue?</value>
595595
<value>Enable folding on all blocks in braces</value>
596596
</data>
597597
<data name="EnableSmoothScrolling" xml:space="preserve">
598-
<value>Enable smooth scrolling</value>
598+
<value>Enable smooth scrolling</value>
599599
</data>
600600
<data name="EnableWordWrap" xml:space="preserve">
601601
<value>Enable word wrap</value>
@@ -1108,4 +1108,7 @@ Do you want to continue?</value>
11081108
<data name="_Window" xml:space="preserve">
11091109
<value>_Window</value>
11101110
</data>
1111+
<data name="DecompilerSettings.IntroducePrivateProtectedAccessibility" xml:space="preserve">
1112+
<value>Introduce 'private protected' accessibility</value>
1113+
</data>
11111114
</root>

ILSpy/Properties/Resources.zh-Hans.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@
369369
<data name="DecompilerSettings.IntroduceLocalFunctions" xml:space="preserve">
370370
<value>引入局部函数(local functions)</value>
371371
</data>
372+
<data name="DecompilerSettings.IntroducePrivateProtectedAccessibility" xml:space="preserve">
373+
<value />
374+
</data>
372375
<data name="DecompilerSettings.IntroduceStaticLocalFunctions" xml:space="preserve">
373376
<value>引入静态局部函数(static local functions)</value>
374377
</data>

0 commit comments

Comments
 (0)