@@ -43,27 +43,33 @@ protected RenamePass(RenameTargets targets)
43
43
44
44
public virtual bool Rename ( Declaration decl , out string newName )
45
45
{
46
- if ( decl is Method method && ! method . IsStatic )
46
+ switch ( decl )
47
47
{
48
- Method rootBaseMethod ;
49
- if ( method . OriginalNamespace is Class @class && @class . IsInterface )
50
- rootBaseMethod = ( Method ) method . OriginalFunction ;
51
- else
52
- rootBaseMethod = method . GetRootBaseMethod ( ) ;
53
- if ( rootBaseMethod != null && rootBaseMethod != method )
48
+ case Method { IsStatic : false } method :
54
49
{
55
- newName = rootBaseMethod . Name ;
56
- return true ;
57
- }
58
- }
50
+ Method rootBaseMethod ;
51
+ if ( method . OriginalNamespace is Class { IsInterface : true } )
52
+ rootBaseMethod = ( Method ) method . OriginalFunction ;
53
+ else
54
+ rootBaseMethod = method . GetRootBaseMethod ( ) ;
55
+ if ( rootBaseMethod != null && rootBaseMethod != method )
56
+ {
57
+ newName = rootBaseMethod . Name ;
58
+ return true ;
59
+ }
59
60
60
- if ( decl is Property property && ! property . IsStatic )
61
- {
62
- var rootBaseProperty = ( ( Class ) property . Namespace ) . GetBasePropertyByName ( property ) ;
63
- if ( rootBaseProperty != null && rootBaseProperty != property )
61
+ break ;
62
+ }
63
+ case Property { IsStatic : false } property :
64
64
{
65
- newName = rootBaseProperty . Name ;
66
- return true ;
65
+ var rootBaseProperty = ( ( Class ) property . Namespace ) . GetBasePropertyByName ( property ) ;
66
+ if ( rootBaseProperty != null && rootBaseProperty != property )
67
+ {
68
+ newName = rootBaseProperty . Name ;
69
+ return true ;
70
+ }
71
+
72
+ break ;
67
73
}
68
74
}
69
75
@@ -73,62 +79,46 @@ public virtual bool Rename(Declaration decl, out string newName)
73
79
74
80
public bool IsRenameableDecl ( Declaration decl )
75
81
{
76
- if ( decl is Class )
77
- return Targets . HasFlag ( RenameTargets . Class ) ;
78
-
79
- var method = decl as Method ;
80
- if ( method != null )
82
+ switch ( decl )
81
83
{
82
- return Targets . HasFlag ( RenameTargets . Method ) &&
83
- method . Kind == CXXMethodKind . Normal &&
84
- method . Name != "dispose" ;
85
- }
86
-
87
- var function = decl as Function ;
88
- if ( function != null )
89
- {
90
- // Special case the IDisposable.Dispose method.
91
- return Targets . HasFlag ( RenameTargets . Function ) &&
92
- ( ! function . IsOperator && function . Name != "dispose" ) ;
93
- }
94
-
95
- if ( decl is Parameter )
96
- return Targets . HasFlag ( RenameTargets . Parameter ) ;
97
-
98
- if ( decl is Enumeration . Item )
99
- return Targets . HasFlag ( RenameTargets . EnumItem ) ;
100
-
101
- if ( decl is Enumeration )
102
- return Targets . HasFlag ( RenameTargets . Enum ) ;
103
-
104
- var property = decl as Property ;
105
- if ( property != null )
106
- return Targets . HasFlag ( RenameTargets . Property ) && ! property . IsIndexer ;
107
-
108
- if ( decl is Event )
109
- return Targets . HasFlag ( RenameTargets . Event ) ;
110
-
111
- if ( decl is TypedefDecl )
112
- return Targets . HasFlag ( RenameTargets . Delegate ) ;
113
-
114
- if ( decl is Namespace && ! ( decl is TranslationUnit ) )
115
- return Targets . HasFlag ( RenameTargets . Namespace ) ;
116
-
117
- if ( decl is Variable )
118
- return Targets . HasFlag ( RenameTargets . Variable ) ;
119
-
120
- var field = decl as Field ;
121
- if ( field != null )
122
- {
123
- if ( ! Targets . HasFlag ( RenameTargets . Field ) )
84
+ case Class :
85
+ return Targets . HasFlag ( RenameTargets . Class ) ;
86
+ case Method method :
87
+ return Targets . HasFlag ( RenameTargets . Method ) &&
88
+ method . Kind == CXXMethodKind . Normal &&
89
+ method . Name != "dispose" ;
90
+ case Function function :
91
+ // Special case the IDisposable.Dispose method.
92
+ return Targets . HasFlag ( RenameTargets . Function ) &&
93
+ ( ! function . IsOperator && function . Name != "dispose" ) ;
94
+ case Parameter :
95
+ return Targets . HasFlag ( RenameTargets . Parameter ) ;
96
+ case Enumeration . Item :
97
+ return Targets . HasFlag ( RenameTargets . EnumItem ) ;
98
+ case Enumeration :
99
+ return Targets . HasFlag ( RenameTargets . Enum ) ;
100
+ case Property property :
101
+ return Targets . HasFlag ( RenameTargets . Property ) && ! property . IsIndexer ;
102
+ case Event :
103
+ return Targets . HasFlag ( RenameTargets . Event ) ;
104
+ case TypedefDecl :
105
+ return Targets . HasFlag ( RenameTargets . Delegate ) ;
106
+ case Namespace when ! ( decl is TranslationUnit ) :
107
+ return Targets . HasFlag ( RenameTargets . Namespace ) ;
108
+ case Variable :
109
+ return Targets . HasFlag ( RenameTargets . Variable ) ;
110
+ case Field when ! Targets . HasFlag ( RenameTargets . Field ) :
111
+ return false ;
112
+ case Field field :
113
+ {
114
+ var fieldProperty = ( ( Class ) field . Namespace ) . Properties . FirstOrDefault (
115
+ p => p . Field == field ) ;
116
+ return ( fieldProperty != null &&
117
+ fieldProperty . IsInRefTypeAndBackedByValueClassField ( ) ) ;
118
+ }
119
+ default :
124
120
return false ;
125
- var fieldProperty = ( ( Class ) field . Namespace ) . Properties . FirstOrDefault (
126
- p => p . Field == field ) ;
127
- return ( fieldProperty != null &&
128
- fieldProperty . IsInRefTypeAndBackedByValueClassField ( ) ) ;
129
121
}
130
-
131
- return false ;
132
122
}
133
123
134
124
public override bool VisitDeclaration ( Declaration decl )
@@ -148,8 +138,7 @@ public override bool VisitDeclaration(Declaration decl)
148
138
149
139
private bool Rename ( Declaration decl )
150
140
{
151
- string newName ;
152
- if ( ! Rename ( decl , out newName ) || AreThereConflicts ( decl , newName ) )
141
+ if ( ! Rename ( decl , out var newName ) || AreThereConflicts ( decl , newName ) )
153
142
return false ;
154
143
155
144
decl . Name = newName ;
@@ -167,8 +156,7 @@ private static bool AreThereConflicts(Declaration decl, string newName)
167
156
declarations . AddRange ( decl . Namespace . Events ) ;
168
157
declarations . Add ( decl . Namespace ) ;
169
158
170
- var function = decl as Function ;
171
- if ( function != null )
159
+ if ( decl is Function function )
172
160
// account for overloads
173
161
declarations . AddRange ( GetFunctionsWithTheSameParams ( function ) ) ;
174
162
else
@@ -177,11 +165,10 @@ private static bool AreThereConflicts(Declaration decl, string newName)
177
165
declarations . AddRange ( decl . Namespace . Variables ) ;
178
166
declarations . AddRange ( from typedefDecl in decl . Namespace . Typedefs
179
167
let pointerType = typedefDecl . Type . Desugar ( ) as PointerType
180
- where pointerType != null && pointerType . GetFinalPointee ( ) is FunctionType
168
+ where pointerType ? . GetFinalPointee ( ) is FunctionType
181
169
select typedefDecl ) ;
182
170
183
- var specialization = decl as ClassTemplateSpecialization ;
184
- if ( specialization != null )
171
+ if ( decl is ClassTemplateSpecialization specialization )
185
172
declarations . RemoveAll ( d => specialization . TemplatedDecl . TemplatedDecl == d ) ;
186
173
187
174
var @class = decl . Namespace as Class ;
@@ -201,8 +188,7 @@ where typedefDecl.Type.Desugar() is FunctionType
201
188
if ( decl is Method && decl . IsGenerated )
202
189
return @class . GetPropertyByName ( newName ) != null ;
203
190
204
- var property = decl as Property ;
205
- if ( property != null )
191
+ if ( decl is Property property )
206
192
{
207
193
Property existingProperty = @class . Properties . Find (
208
194
p => p != decl && p . Name == newName ) ;
@@ -216,8 +202,7 @@ where typedefDecl.Type.Desugar() is FunctionType
216
202
}
217
203
}
218
204
219
- var enumItem = decl as Enumeration . Item ;
220
- if ( enumItem != null )
205
+ if ( decl is Enumeration . Item enumItem )
221
206
return ( ( Enumeration ) enumItem . Namespace ) . Items . Any (
222
207
i => i != decl && i . Name == newName ) ;
223
208
@@ -226,8 +211,7 @@ where typedefDecl.Type.Desugar() is FunctionType
226
211
227
212
private static IEnumerable < Function > GetFunctionsWithTheSameParams ( Function function )
228
213
{
229
- var method = function as Method ;
230
- if ( method != null )
214
+ if ( function is Method method )
231
215
{
232
216
return ( ( Class ) method . Namespace ) . Methods . Where (
233
217
m => ! m . Ignore && m . Parameters . SequenceEqual ( function . Parameters , new ParameterComparer ( ) ) ) ;
@@ -391,14 +375,12 @@ public static string ConvertCaseString(Declaration decl, RenameCasePattern patte
391
375
if ( decl . Name . All ( c => ! char . IsLetter ( c ) ) )
392
376
return decl . Name ;
393
377
394
- var typedef = decl as TypedefDecl ;
395
- if ( typedef != null && typedef . IsSynthetized )
396
- return decl . Name ;
397
-
398
- var property = decl as Property ;
399
- if ( property != null && property . GetMethod != null &&
400
- property . GetMethod . SynthKind == FunctionSynthKind . InterfaceInstance )
401
- return decl . Name ;
378
+ switch ( decl )
379
+ {
380
+ case TypedefDecl { IsSynthetized : true } :
381
+ case Property { GetMethod . SynthKind : FunctionSynthKind . InterfaceInstance } :
382
+ return decl . Name ;
383
+ }
402
384
403
385
var sb = new StringBuilder ( decl . Name ) ;
404
386
// check if it's been renamed to avoid a keyword
@@ -412,14 +394,14 @@ public static string ConvertCaseString(Declaration decl, RenameCasePattern patte
412
394
{
413
395
case RenameCasePattern . UpperCamelCase :
414
396
// ensure separation in enum items by not ending up with more capitals in a row than before
415
- if ( sb . Length == 1 || ! char . IsUpper ( sb [ 1 ] ) || ! ( decl is Enumeration . Item ) )
397
+ if ( sb . Length == 1 || ! char . IsUpper ( sb [ 1 ] ) || decl is not Enumeration . Item )
416
398
sb [ 0 ] = char . ToUpperInvariant ( sb [ 0 ] ) ;
417
- if ( @class != null && @class . Type == ClassType . Interface )
399
+ if ( @class is { Type : ClassType . Interface } )
418
400
sb [ 1 ] = char . ToUpperInvariant ( sb [ 1 ] ) ;
419
401
break ;
420
402
case RenameCasePattern . LowerCamelCase :
421
403
sb [ 0 ] = char . ToLowerInvariant ( sb [ 0 ] ) ;
422
- if ( @class != null && @class . Type == ClassType . Interface )
404
+ if ( @class is { Type : ClassType . Interface } )
423
405
sb [ 1 ] = char . ToLowerInvariant ( sb [ 1 ] ) ;
424
406
break ;
425
407
}
0 commit comments