@@ -43,27 +43,33 @@ protected RenamePass(RenameTargets targets)
4343
4444 public virtual bool Rename ( Declaration decl , out string newName )
4545 {
46- if ( decl is Method method && ! method . IsStatic )
46+ switch ( decl )
4747 {
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 :
5449 {
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+ }
5960
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 :
6464 {
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 ;
6773 }
6874 }
6975
@@ -73,62 +79,46 @@ public virtual bool Rename(Declaration decl, out string newName)
7379
7480 public bool IsRenameableDecl ( Declaration decl )
7581 {
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 )
8183 {
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 :
124120 return false ;
125- var fieldProperty = ( ( Class ) field . Namespace ) . Properties . FirstOrDefault (
126- p => p . Field == field ) ;
127- return ( fieldProperty != null &&
128- fieldProperty . IsInRefTypeAndBackedByValueClassField ( ) ) ;
129121 }
130-
131- return false ;
132122 }
133123
134124 public override bool VisitDeclaration ( Declaration decl )
@@ -148,8 +138,7 @@ public override bool VisitDeclaration(Declaration decl)
148138
149139 private bool Rename ( Declaration decl )
150140 {
151- string newName ;
152- if ( ! Rename ( decl , out newName ) || AreThereConflicts ( decl , newName ) )
141+ if ( ! Rename ( decl , out var newName ) || AreThereConflicts ( decl , newName ) )
153142 return false ;
154143
155144 decl . Name = newName ;
@@ -167,8 +156,7 @@ private static bool AreThereConflicts(Declaration decl, string newName)
167156 declarations . AddRange ( decl . Namespace . Events ) ;
168157 declarations . Add ( decl . Namespace ) ;
169158
170- var function = decl as Function ;
171- if ( function != null )
159+ if ( decl is Function function )
172160 // account for overloads
173161 declarations . AddRange ( GetFunctionsWithTheSameParams ( function ) ) ;
174162 else
@@ -177,11 +165,10 @@ private static bool AreThereConflicts(Declaration decl, string newName)
177165 declarations . AddRange ( decl . Namespace . Variables ) ;
178166 declarations . AddRange ( from typedefDecl in decl . Namespace . Typedefs
179167 let pointerType = typedefDecl . Type . Desugar ( ) as PointerType
180- where pointerType != null && pointerType . GetFinalPointee ( ) is FunctionType
168+ where pointerType ? . GetFinalPointee ( ) is FunctionType
181169 select typedefDecl ) ;
182170
183- var specialization = decl as ClassTemplateSpecialization ;
184- if ( specialization != null )
171+ if ( decl is ClassTemplateSpecialization specialization )
185172 declarations . RemoveAll ( d => specialization . TemplatedDecl . TemplatedDecl == d ) ;
186173
187174 var @class = decl . Namespace as Class ;
@@ -201,8 +188,7 @@ where typedefDecl.Type.Desugar() is FunctionType
201188 if ( decl is Method && decl . IsGenerated )
202189 return @class . GetPropertyByName ( newName ) != null ;
203190
204- var property = decl as Property ;
205- if ( property != null )
191+ if ( decl is Property property )
206192 {
207193 Property existingProperty = @class . Properties . Find (
208194 p => p != decl && p . Name == newName ) ;
@@ -216,8 +202,7 @@ where typedefDecl.Type.Desugar() is FunctionType
216202 }
217203 }
218204
219- var enumItem = decl as Enumeration . Item ;
220- if ( enumItem != null )
205+ if ( decl is Enumeration . Item enumItem )
221206 return ( ( Enumeration ) enumItem . Namespace ) . Items . Any (
222207 i => i != decl && i . Name == newName ) ;
223208
@@ -226,8 +211,7 @@ where typedefDecl.Type.Desugar() is FunctionType
226211
227212 private static IEnumerable < Function > GetFunctionsWithTheSameParams ( Function function )
228213 {
229- var method = function as Method ;
230- if ( method != null )
214+ if ( function is Method method )
231215 {
232216 return ( ( Class ) method . Namespace ) . Methods . Where (
233217 m => ! m . Ignore && m . Parameters . SequenceEqual ( function . Parameters , new ParameterComparer ( ) ) ) ;
@@ -391,14 +375,12 @@ public static string ConvertCaseString(Declaration decl, RenameCasePattern patte
391375 if ( decl . Name . All ( c => ! char . IsLetter ( c ) ) )
392376 return decl . Name ;
393377
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+ }
402384
403385 var sb = new StringBuilder ( decl . Name ) ;
404386 // check if it's been renamed to avoid a keyword
@@ -412,14 +394,14 @@ public static string ConvertCaseString(Declaration decl, RenameCasePattern patte
412394 {
413395 case RenameCasePattern . UpperCamelCase :
414396 // 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 )
416398 sb [ 0 ] = char . ToUpperInvariant ( sb [ 0 ] ) ;
417- if ( @class != null && @class . Type == ClassType . Interface )
399+ if ( @class is { Type : ClassType . Interface } )
418400 sb [ 1 ] = char . ToUpperInvariant ( sb [ 1 ] ) ;
419401 break ;
420402 case RenameCasePattern . LowerCamelCase :
421403 sb [ 0 ] = char . ToLowerInvariant ( sb [ 0 ] ) ;
422- if ( @class != null && @class . Type == ClassType . Interface )
404+ if ( @class is { Type : ClassType . Interface } )
423405 sb [ 1 ] = char . ToLowerInvariant ( sb [ 1 ] ) ;
424406 break ;
425407 }
0 commit comments