Browse Source

Code cleanup

pull/1926/head
duckdoom5 5 months ago
parent
commit
c016d47745
  1. 104
      src/Generator/Passes/RenamePass.cs

104
src/Generator/Passes/RenamePass.cs

@ -43,10 +43,12 @@ namespace CppSharp.Passes
public virtual bool Rename(Declaration decl, out string newName) public virtual bool Rename(Declaration decl, out string newName)
{ {
if (decl is Method method && !method.IsStatic) switch (decl)
{
case Method { IsStatic: false } method:
{ {
Method rootBaseMethod; Method rootBaseMethod;
if (method.OriginalNamespace is Class @class && @class.IsInterface) if (method.OriginalNamespace is Class { IsInterface: true })
rootBaseMethod = (Method)method.OriginalFunction; rootBaseMethod = (Method)method.OriginalFunction;
else else
rootBaseMethod = method.GetRootBaseMethod(); rootBaseMethod = method.GetRootBaseMethod();
@ -55,9 +57,10 @@ namespace CppSharp.Passes
newName = rootBaseMethod.Name; newName = rootBaseMethod.Name;
return true; return true;
} }
}
if (decl is Property property && !property.IsStatic) break;
}
case Property { IsStatic: false } property:
{ {
var rootBaseProperty = ((Class)property.Namespace).GetBasePropertyByName(property); var rootBaseProperty = ((Class)property.Namespace).GetBasePropertyByName(property);
if (rootBaseProperty != null && rootBaseProperty != property) if (rootBaseProperty != null && rootBaseProperty != property)
@ -65,6 +68,9 @@ namespace CppSharp.Passes
newName = rootBaseProperty.Name; newName = rootBaseProperty.Name;
return true; return true;
} }
break;
}
} }
newName = decl.Name; newName = decl.Name;
@ -73,63 +79,47 @@ namespace CppSharp.Passes
public bool IsRenameableDecl(Declaration decl) public bool IsRenameableDecl(Declaration decl)
{ {
if (decl is Class) switch (decl)
return Targets.HasFlag(RenameTargets.Class);
var method = decl as Method;
if (method != null)
{ {
case Class:
return Targets.HasFlag(RenameTargets.Class);
case Method method:
return Targets.HasFlag(RenameTargets.Method) && return Targets.HasFlag(RenameTargets.Method) &&
method.Kind == CXXMethodKind.Normal && method.Kind == CXXMethodKind.Normal &&
method.Name != "dispose"; method.Name != "dispose";
} case Function function:
var function = decl as Function;
if (function != null)
{
// Special case the IDisposable.Dispose method. // Special case the IDisposable.Dispose method.
return Targets.HasFlag(RenameTargets.Function) && return Targets.HasFlag(RenameTargets.Function) &&
(!function.IsOperator && function.Name != "dispose"); (!function.IsOperator && function.Name != "dispose");
} case Parameter:
if (decl is Parameter)
return Targets.HasFlag(RenameTargets.Parameter); return Targets.HasFlag(RenameTargets.Parameter);
case Enumeration.Item:
if (decl is Enumeration.Item)
return Targets.HasFlag(RenameTargets.EnumItem); return Targets.HasFlag(RenameTargets.EnumItem);
case Enumeration:
if (decl is Enumeration)
return Targets.HasFlag(RenameTargets.Enum); return Targets.HasFlag(RenameTargets.Enum);
case Property property:
var property = decl as Property;
if (property != null)
return Targets.HasFlag(RenameTargets.Property) && !property.IsIndexer; return Targets.HasFlag(RenameTargets.Property) && !property.IsIndexer;
case Event:
if (decl is Event)
return Targets.HasFlag(RenameTargets.Event); return Targets.HasFlag(RenameTargets.Event);
case TypedefDecl:
if (decl is TypedefDecl)
return Targets.HasFlag(RenameTargets.Delegate); return Targets.HasFlag(RenameTargets.Delegate);
case Namespace when !(decl is TranslationUnit):
if (decl is Namespace && !(decl is TranslationUnit))
return Targets.HasFlag(RenameTargets.Namespace); return Targets.HasFlag(RenameTargets.Namespace);
case Variable:
if (decl is Variable)
return Targets.HasFlag(RenameTargets.Variable); return Targets.HasFlag(RenameTargets.Variable);
case Field when !Targets.HasFlag(RenameTargets.Field):
var field = decl as Field;
if (field != null)
{
if (!Targets.HasFlag(RenameTargets.Field))
return false; return false;
case Field field:
{
var fieldProperty = ((Class)field.Namespace).Properties.FirstOrDefault( var fieldProperty = ((Class)field.Namespace).Properties.FirstOrDefault(
p => p.Field == field); p => p.Field == field);
return (fieldProperty != null && return (fieldProperty != null &&
fieldProperty.IsInRefTypeAndBackedByValueClassField()); fieldProperty.IsInRefTypeAndBackedByValueClassField());
} }
default:
return false; return false;
} }
}
public override bool VisitDeclaration(Declaration decl) public override bool VisitDeclaration(Declaration decl)
{ {
@ -148,8 +138,7 @@ namespace CppSharp.Passes
private bool Rename(Declaration decl) private bool Rename(Declaration decl)
{ {
string newName; if (!Rename(decl, out var newName) || AreThereConflicts(decl, newName))
if (!Rename(decl, out newName) || AreThereConflicts(decl, newName))
return false; return false;
decl.Name = newName; decl.Name = newName;
@ -167,8 +156,7 @@ namespace CppSharp.Passes
declarations.AddRange(decl.Namespace.Events); declarations.AddRange(decl.Namespace.Events);
declarations.Add(decl.Namespace); declarations.Add(decl.Namespace);
var function = decl as Function; if (decl is Function function)
if (function != null)
// account for overloads // account for overloads
declarations.AddRange(GetFunctionsWithTheSameParams(function)); declarations.AddRange(GetFunctionsWithTheSameParams(function));
else else
@ -177,11 +165,10 @@ namespace CppSharp.Passes
declarations.AddRange(decl.Namespace.Variables); declarations.AddRange(decl.Namespace.Variables);
declarations.AddRange(from typedefDecl in decl.Namespace.Typedefs declarations.AddRange(from typedefDecl in decl.Namespace.Typedefs
let pointerType = typedefDecl.Type.Desugar() as PointerType let pointerType = typedefDecl.Type.Desugar() as PointerType
where pointerType != null && pointerType.GetFinalPointee() is FunctionType where pointerType?.GetFinalPointee() is FunctionType
select typedefDecl); select typedefDecl);
var specialization = decl as ClassTemplateSpecialization; if (decl is ClassTemplateSpecialization specialization)
if (specialization != null)
declarations.RemoveAll(d => specialization.TemplatedDecl.TemplatedDecl == d); declarations.RemoveAll(d => specialization.TemplatedDecl.TemplatedDecl == d);
var @class = decl.Namespace as Class; var @class = decl.Namespace as Class;
@ -201,8 +188,7 @@ namespace CppSharp.Passes
if (decl is Method && decl.IsGenerated) if (decl is Method && decl.IsGenerated)
return @class.GetPropertyByName(newName) != null; return @class.GetPropertyByName(newName) != null;
var property = decl as Property; if (decl is Property property)
if (property != null)
{ {
Property existingProperty = @class.Properties.Find( Property existingProperty = @class.Properties.Find(
p => p != decl && p.Name == newName); p => p != decl && p.Name == newName);
@ -216,8 +202,7 @@ namespace CppSharp.Passes
} }
} }
var enumItem = decl as Enumeration.Item; if (decl is Enumeration.Item enumItem)
if (enumItem != null)
return ((Enumeration)enumItem.Namespace).Items.Any( return ((Enumeration)enumItem.Namespace).Items.Any(
i => i != decl && i.Name == newName); i => i != decl && i.Name == newName);
@ -226,8 +211,7 @@ namespace CppSharp.Passes
private static IEnumerable<Function> GetFunctionsWithTheSameParams(Function function) private static IEnumerable<Function> GetFunctionsWithTheSameParams(Function function)
{ {
var method = function as Method; if (function is Method method)
if (method != null)
{ {
return ((Class)method.Namespace).Methods.Where( return ((Class)method.Namespace).Methods.Where(
m => !m.Ignore && m.Parameters.SequenceEqual(function.Parameters, new ParameterComparer())); m => !m.Ignore && m.Parameters.SequenceEqual(function.Parameters, new ParameterComparer()));
@ -391,14 +375,12 @@ namespace CppSharp.Passes
if (decl.Name.All(c => !char.IsLetter(c))) if (decl.Name.All(c => !char.IsLetter(c)))
return decl.Name; return decl.Name;
var typedef = decl as TypedefDecl; switch (decl)
if (typedef != null && typedef.IsSynthetized) {
return decl.Name; case TypedefDecl { IsSynthetized: true }:
case Property { GetMethod.SynthKind: FunctionSynthKind.InterfaceInstance }:
var property = decl as Property;
if (property != null && property.GetMethod != null &&
property.GetMethod.SynthKind == FunctionSynthKind.InterfaceInstance)
return decl.Name; return decl.Name;
}
var sb = new StringBuilder(decl.Name); var sb = new StringBuilder(decl.Name);
// check if it's been renamed to avoid a keyword // check if it's been renamed to avoid a keyword
@ -412,14 +394,14 @@ namespace CppSharp.Passes
{ {
case RenameCasePattern.UpperCamelCase: case RenameCasePattern.UpperCamelCase:
// ensure separation in enum items by not ending up with more capitals in a row than before // ensure separation in enum items by not ending up with more capitals in a row than before
if (sb.Length == 1 || !char.IsUpper(sb[1]) || !(decl is Enumeration.Item)) if (sb.Length == 1 || !char.IsUpper(sb[1]) || decl is not Enumeration.Item)
sb[0] = char.ToUpperInvariant(sb[0]); sb[0] = char.ToUpperInvariant(sb[0]);
if (@class != null && @class.Type == ClassType.Interface) if (@class is { Type: ClassType.Interface })
sb[1] = char.ToUpperInvariant(sb[1]); sb[1] = char.ToUpperInvariant(sb[1]);
break; break;
case RenameCasePattern.LowerCamelCase: case RenameCasePattern.LowerCamelCase:
sb[0] = char.ToLowerInvariant(sb[0]); sb[0] = char.ToLowerInvariant(sb[0]);
if (@class != null && @class.Type == ClassType.Interface) if (@class is { Type: ClassType.Interface })
sb[1] = char.ToLowerInvariant(sb[1]); sb[1] = char.ToLowerInvariant(sb[1]);
break; break;
} }

Loading…
Cancel
Save