From d339bfe9d24063520e8355199cfc00f15cdfe858 Mon Sep 17 00:00:00 2001 From: duckdoom5 Date: Tue, 25 Feb 2025 16:32:23 +0100 Subject: [PATCH] Code cleanup --- src/AST/Type.cs | 117 ++++++-------- src/AST/TypeExtensions.cs | 148 ++++++++---------- src/AST/Typedef.cs | 2 +- src/CppParser/Sources.h | 26 +-- src/Generator/Generators/C/CppTypePrinter.cs | 49 ++---- src/Generator/Generators/CodeGenerator.cs | 18 ++- src/Generator/Generators/TypePrinter.cs | 17 +- .../Passes/GetterSetterToPropertyPass.cs | 14 +- 8 files changed, 165 insertions(+), 226 deletions(-) diff --git a/src/AST/Type.cs b/src/AST/Type.cs index d686f5d9..6a2ac237 100644 --- a/src/AST/Type.cs +++ b/src/AST/Type.cs @@ -26,7 +26,7 @@ namespace CppSharp.AST } public abstract T Visit(ITypeVisitor visitor, TypeQualifiers quals - = new TypeQualifiers()); + = new()); public override string ToString() { @@ -90,9 +90,9 @@ namespace CppSharp.AST public override bool Equals(object obj) { - if (!(obj is QualifiedType)) return false; + if (obj is not QualifiedType type) + return false; - var type = (QualifiedType)obj; return Type.Equals(type.Type) && Qualifiers.Equals(type.Qualifiers); } @@ -132,7 +132,7 @@ namespace CppSharp.AST public Declaration Declaration; - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitTagType(this, quals); } @@ -144,8 +144,8 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as TagType; - if (type == null) return false; + if (obj is not TagType type) + return false; return Declaration.Equals(type.Declaration); } @@ -198,7 +198,7 @@ namespace CppSharp.AST public Type Type => QualifiedType.Type; - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitArrayType(this, quals); } @@ -210,8 +210,9 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as ArrayType; - if (type == null) return false; + if (obj is not ArrayType type) + return false; + var equals = QualifiedType.Equals(type.QualifiedType) && SizeType.Equals(type.SizeType); if (SizeType == ArraySize.Constant) @@ -251,7 +252,7 @@ namespace CppSharp.AST public QualifiedType ReturnType; // Argument types. - public List Parameters { get; } = new List(); + public List Parameters { get; } = new(); public CallingConvention CallingConvention { get; set; } @@ -270,7 +271,7 @@ namespace CppSharp.AST IsDependent = type.IsDependent; } - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitFunctionType(this, quals); } @@ -282,8 +283,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as FunctionType; - if (type == null) return false; + if (obj is not FunctionType type) return false; return ReturnType.Equals(type.ReturnType) && Parameters.SequenceEqual(type.Parameters); } @@ -300,7 +300,7 @@ namespace CppSharp.AST /// public class PointerType : Type { - public PointerType(QualifiedType pointee = new QualifiedType()) + public PointerType(QualifiedType pointee = new()) { Modifier = TypeModifier.Pointer; QualifiedPointee = pointee; @@ -331,21 +331,14 @@ namespace CppSharp.AST Modifier = type.Modifier; } - public bool IsReference - { - get - { - return Modifier == TypeModifier.LVReference || - Modifier == TypeModifier.RVReference; - } - } + public bool IsReference => Modifier is TypeModifier.LVReference or TypeModifier.RVReference; public QualifiedType QualifiedPointee; - public Type Pointee { get { return QualifiedPointee.Type; } } + public Type Pointee => QualifiedPointee.Type; public TypeModifier Modifier; - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitPointerType(this, quals); } @@ -357,8 +350,8 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as PointerType; - if (type == null) return false; + if (obj is not PointerType type) + return false; return QualifiedPointee.Equals(type.QualifiedPointee) && Modifier == type.Modifier; @@ -386,12 +379,9 @@ namespace CppSharp.AST type.QualifiedPointee.Qualifiers); } - public Type Pointee - { - get { return QualifiedPointee.Type; } - } + public Type Pointee => QualifiedPointee.Type; - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitMemberPointerType(this, quals); } @@ -403,8 +393,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var pointer = obj as MemberPointerType; - if (pointer == null) return false; + if (obj is not MemberPointerType pointer) return false; return QualifiedPointee.Equals(pointer.QualifiedPointee); } @@ -434,7 +423,7 @@ namespace CppSharp.AST Declaration = type.Declaration; } - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitTypedefType(this, quals); } @@ -446,8 +435,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var typedef = obj as TypedefType; - if (typedef == null) + if (obj is not TypedefType typedef) return false; return Declaration.OriginalName == typedef.Declaration.OriginalName && @@ -485,7 +473,7 @@ namespace CppSharp.AST Equivalent = new QualifiedType((Type)type.Equivalent.Type.Clone(), type.Equivalent.Qualifiers); } - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitAttributedType(this, quals); } @@ -502,8 +490,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var attributed = obj as AttributedType; - if (attributed == null) return false; + if (obj is not AttributedType attributed) return false; return Modified.Equals(attributed.Modified) && Equivalent.Equals(attributed.Equivalent); @@ -538,7 +525,7 @@ namespace CppSharp.AST } public override T Visit(ITypeVisitor visitor, - TypeQualifiers quals = new TypeQualifiers()) + TypeQualifiers quals = new()) { return visitor.VisitDecayedType(this, quals); } @@ -550,8 +537,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var decay = obj as DecayedType; - if (decay == null) return false; + if (obj is not DecayedType decay) return false; return Original.Equals(decay.Original); } @@ -704,7 +690,7 @@ namespace CppSharp.AST } public override T Visit(ITypeVisitor visitor, - TypeQualifiers quals = new TypeQualifiers()) + TypeQualifiers quals = new()) { return visitor.VisitTemplateSpecializationType(this, quals); } @@ -716,8 +702,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as TemplateSpecializationType; - if (type == null) return false; + if (obj is not TemplateSpecializationType type) return false; return Arguments.SequenceEqual(type.Arguments) && ((Template != null && Template.Name == type.Template.Name) || @@ -759,7 +744,7 @@ namespace CppSharp.AST public QualifiedType Desugared; public override T Visit(ITypeVisitor visitor, - TypeQualifiers quals = new TypeQualifiers()) + TypeQualifiers quals = new()) { return visitor.VisitDependentTemplateSpecializationType(this, quals); } @@ -771,8 +756,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as TemplateSpecializationType; - if (type == null) return false; + if (obj is not TemplateSpecializationType type) return false; return Arguments.SequenceEqual(type.Arguments) && Desugared == type.Desugared; @@ -815,7 +799,7 @@ namespace CppSharp.AST } public override T Visit(ITypeVisitor visitor, - TypeQualifiers quals = new TypeQualifiers()) + TypeQualifiers quals = new()) { return visitor.VisitTemplateParameterType(this, quals); } @@ -827,8 +811,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as TemplateParameterType; - if (type == null) return false; + if (obj is not TemplateParameterType type) return false; return Parameter == type.Parameter && Depth == type.Depth @@ -865,7 +848,7 @@ namespace CppSharp.AST } public override T Visit(ITypeVisitor visitor, - TypeQualifiers quals = new TypeQualifiers()) + TypeQualifiers quals = new()) { return visitor.VisitTemplateParameterSubstitutionType(this, quals); } @@ -877,8 +860,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as TemplateParameterSubstitutionType; - if (type == null) return false; + if (obj is not TemplateParameterSubstitutionType type) return false; return ReplacedParameter.Equals(type.ReplacedParameter) && Replacement.Equals(type.Replacement); @@ -915,7 +897,7 @@ namespace CppSharp.AST public QualifiedType InjectedSpecializationType { get; set; } public override T Visit(ITypeVisitor visitor, - TypeQualifiers quals = new TypeQualifiers()) + TypeQualifiers quals = new()) { return visitor.VisitInjectedClassNameType(this, quals); } @@ -927,8 +909,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as InjectedClassNameType; - if (type == null) return false; + if (obj is not InjectedClassNameType type) return false; if (TemplateSpecialization == null || type.TemplateSpecialization == null) return TemplateSpecialization == type.TemplateSpecialization; @@ -962,7 +943,7 @@ namespace CppSharp.AST public string Identifier { get; set; } public override T Visit(ITypeVisitor visitor, - TypeQualifiers quals = new TypeQualifiers()) + TypeQualifiers quals = new()) { return visitor.VisitDependentNameType(this, quals); } @@ -999,7 +980,7 @@ namespace CppSharp.AST public System.Type Type; public override T Visit(ITypeVisitor visitor, - TypeQualifiers quals = new TypeQualifiers()) + TypeQualifiers quals = new()) { return visitor.VisitCILType(this, quals); } @@ -1011,8 +992,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as CILType; - if (type == null) return false; + if (obj is not CILType type) return false; return Type == type.Type; } @@ -1031,7 +1011,7 @@ namespace CppSharp.AST { } - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitPackExpansionType(this, quals); } @@ -1058,7 +1038,7 @@ namespace CppSharp.AST public QualifiedType Desugared { get; set; } public QualifiedType BaseType { get; set; } - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitUnaryTransformType(this, quals); } @@ -1085,7 +1065,7 @@ namespace CppSharp.AST public UnresolvedUsingTypename Declaration { get; set; } - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitUnresolvedUsingType(this, quals); } @@ -1112,7 +1092,7 @@ namespace CppSharp.AST public QualifiedType ElementType { get; set; } public uint NumElements { get; set; } - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitVectorType(this, quals); } @@ -1144,7 +1124,7 @@ namespace CppSharp.AST public string Description; - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitUnsupportedType(this, quals); } @@ -1243,7 +1223,7 @@ namespace CppSharp.AST // Primitive type of built-in type. public PrimitiveType Type; - public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new TypeQualifiers()) + public override T Visit(ITypeVisitor visitor, TypeQualifiers quals = new()) { return visitor.VisitBuiltinType(this, quals); } @@ -1255,8 +1235,7 @@ namespace CppSharp.AST public override bool Equals(object obj) { - var type = obj as BuiltinType; - if (type == null) return false; + if (obj is not BuiltinType type) return false; return Type == type.Type; } diff --git a/src/AST/TypeExtensions.cs b/src/AST/TypeExtensions.cs index d94551cf..4ffefbea 100644 --- a/src/AST/TypeExtensions.cs +++ b/src/AST/TypeExtensions.cs @@ -4,14 +4,12 @@ { public static bool IsPrimitiveType(this Type t) { - PrimitiveType type; - return t.IsPrimitiveType(out type); + return t.IsPrimitiveType(out PrimitiveType _); } public static bool IsPrimitiveType(this Type t, out PrimitiveType primitive) { - var builtin = t.Desugar() as BuiltinType; - if (builtin != null) + if (t.Desugar() is BuiltinType builtin) { primitive = builtin.Type; return true; @@ -32,9 +30,7 @@ public static bool IsEnumType(this Type t) { - var tag = t.Desugar() as TagType; - - if (tag == null) + if (t.Desugar() is not TagType tag) return false; return tag.Declaration is Enumeration; @@ -47,36 +43,28 @@ public static bool IsPointer(this Type t) { - var functionPointer = t as MemberPointerType; - if (functionPointer != null) + if (t is MemberPointerType) return true; - var pointer = t as PointerType; - if (pointer == null) + + if (t is not PointerType pointer) return false; + return pointer.Modifier == PointerType.TypeModifier.Pointer; } public static bool IsReference(this Type t) { - var pointer = t as PointerType; - if (pointer == null) - return false; - return pointer.IsReference; + return t is PointerType { IsReference: true }; } public static bool IsPointerToPrimitiveType(this Type t) { - var ptr = t as PointerType; - if (ptr == null) - return false; - PrimitiveType primitiveType; - return ptr.Pointee.IsPrimitiveType(out primitiveType); + return t is PointerType ptr && ptr.Pointee.IsPrimitiveType(out _); } public static bool IsPointerToPrimitiveType(this Type t, out PrimitiveType primitive) { - var ptr = t as PointerType; - if (ptr == null) + if (t is not PointerType ptr) { primitive = PrimitiveType.Null; return false; @@ -86,24 +74,21 @@ public static bool IsPointerToPrimitiveType(this Type t, PrimitiveType primitive) { - var ptr = t as PointerType; - if (ptr == null) + if (t is not PointerType ptr) return false; return ptr.Pointee.IsPrimitiveType(primitive); } public static bool IsPointerToEnum(this Type t) { - var ptr = t as PointerType; - if (ptr == null) + if (t is not PointerType ptr) return false; return ptr.Pointee.IsEnumType(); } public static bool IsPointerToEnum(this Type t, out Enumeration @enum) { - var ptr = t as PointerType; - if (ptr == null) + if (t is not PointerType ptr) { @enum = null; return false; @@ -111,23 +96,22 @@ return ptr.Pointee.TryGetEnum(out @enum); } - public static bool IsPointerTo(this Type t, out T type) where T : Type + public static bool IsPointerTo(this Type t, out T type) + where T : Type { - var pointee = t.GetPointee(); - type = pointee as T; - if (type == null) + type = t.GetPointee() switch { - var attributedType = pointee as AttributedType; - if (attributedType != null) - type = attributedType.Modified.Type as T; - } + T tType => tType, + AttributedType attributedType => attributedType.Modified.Type as T, + _ => null + }; + return type != null; } public static bool IsClass(this Type t) { - Class @class; - return t.TryGetClass(out @class); + return t.TryGetClass(out _); } public static bool TryGetClass(this Type t, out Class @class, Class value = null) @@ -135,11 +119,12 @@ return TryGetDeclaration(t, out @class, value); } - public static bool TryGetDeclaration(this Type t, out T decl, T value = null) where T : Declaration + public static bool TryGetDeclaration(this Type t, out T decl, T value = null) + where T : Declaration { t = t.Desugar(); - TagType tagType = null; + TagType tagType; if (t is TemplateSpecializationType type) { if (type.IsDependent) @@ -150,20 +135,20 @@ type.Desugared.Type.TryGetDeclaration(out decl, value); return decl != null; case ClassTemplate classTemplate: - { - var templatedClass = classTemplate.TemplatedClass; - decl = templatedClass.CompleteDeclaration == null - ? templatedClass as T - : (T)templatedClass.CompleteDeclaration; + { + var templatedClass = classTemplate.TemplatedClass; + decl = templatedClass.CompleteDeclaration == null + ? templatedClass as T + : (T)templatedClass.CompleteDeclaration; - if (decl == null) - return false; + if (decl == null) + return false; - if (value != null) - type.Template = new ClassTemplate { TemplatedDecl = value }; + if (value != null) + type.Template = new ClassTemplate { TemplatedDecl = value }; - return true; - } + return true; + } case TemplateTemplateParameter templateTemplateParameter: return (decl = templateTemplateParameter.TemplatedDecl as T) != null; } @@ -193,15 +178,12 @@ public static bool IsEnum(this Type t) { - Enumeration @enum; - return t.TryGetEnum(out @enum); + return t.TryGetEnum(out _); } public static bool TryGetEnum(this Type t, out Enumeration @enum) { - var tag = t.Desugar() as TagType; - - if (tag == null) + if (t.Desugar() is not TagType tag) { @enum = null; return false; @@ -269,13 +251,12 @@ /// public static Type GetPointee(this Type t) { - var ptr = t as PointerType; - if (ptr != null) - return ptr.Pointee; - var memberPtr = t as MemberPointerType; - if (memberPtr != null) - return memberPtr.QualifiedPointee.Type; - return null; + return t switch + { + PointerType ptr => ptr.Pointee, + MemberPointerType memberPtr => memberPtr.QualifiedPointee.Type, + _ => null + }; } /// @@ -296,17 +277,28 @@ return finalPointee; } + public static PointerType GetFinalPointer(this Type t) + { + if (t is not PointerType type) + return null; + + var pointee = type.Desugar().GetPointee(); + + if (pointee.IsPointer()) + return pointee.GetFinalPointer(); + + return type; + } + /// /// If t is a pointer type the type pointed to by t will be returned. /// Otherwise the default qualified type. /// public static QualifiedType GetQualifiedPointee(this Type t) { - var ptr = t as PointerType; - if (ptr != null) + if (t is PointerType ptr) return ptr.QualifiedPointee; - var memberPtr = t as MemberPointerType; - if (memberPtr != null) + if (t is MemberPointerType memberPtr) return memberPtr.QualifiedPointee; return new QualifiedType(); } @@ -329,21 +321,6 @@ return finalPointee; } - public static PointerType GetFinalPointer(this Type t) - { - var type = t as PointerType; - - if (type == null) - return null; - - var pointee = type.Desugar().GetPointee(); - - if (pointee.IsPointer()) - return pointee.GetFinalPointer(); - - return type; - } - public static bool ResolvesTo(this QualifiedType type, QualifiedType other) { if (!type.Qualifiers.Equals(other.Qualifiers)) @@ -351,9 +328,7 @@ var left = type.Type.Desugar(); var right = other.Type.Desugar(); - var leftPointer = left as PointerType; - var rightPointer = right as PointerType; - if (leftPointer != null && rightPointer != null) + if (left is PointerType leftPointer && right is PointerType rightPointer) { return leftPointer.Modifier == rightPointer.Modifier && leftPointer.QualifiedPointee.ResolvesTo(rightPointer.QualifiedPointee); @@ -388,8 +363,7 @@ qualifiers.IsConst = false; type.Qualifiers = qualifiers; - var ptr = type.Type as PointerType; - if (ptr != null) + if (type.Type is PointerType ptr) { var pointee = ptr.QualifiedPointee; var pointeeQualifiers = pointee.Qualifiers; diff --git a/src/AST/Typedef.cs b/src/AST/Typedef.cs index f0f270b9..41adf647 100644 --- a/src/AST/Typedef.cs +++ b/src/AST/Typedef.cs @@ -5,7 +5,7 @@ namespace CppSharp.AST /// public abstract class TypedefNameDecl : Declaration, ITypedDecl { - public Type Type { get { return QualifiedType.Type; } } + public Type Type => QualifiedType.Type; public QualifiedType QualifiedType { get; set; } public bool IsSynthetized { get; set; } } diff --git a/src/CppParser/Sources.h b/src/CppParser/Sources.h index 1c2941a4..11f37a7b 100644 --- a/src/CppParser/Sources.h +++ b/src/CppParser/Sources.h @@ -9,19 +9,19 @@ #include "Helpers.h" -namespace CppSharp { namespace CppParser { +namespace CppSharp::CppParser { - struct CS_API CS_VALUE_TYPE SourceLocation - { - SourceLocation(); - SourceLocation(unsigned ID); - unsigned ID; - }; +struct CS_API CS_VALUE_TYPE SourceLocation +{ + SourceLocation(); + SourceLocation(unsigned ID); + unsigned ID; +}; - struct CS_API SourceRange - { - SourceLocation beginLoc; - SourceLocation endLoc; - }; +struct CS_API SourceRange +{ + SourceLocation beginLoc; + SourceLocation endLoc; +}; -}} // namespace CppSharp::CppParser +} // namespace CppSharp::CppParser diff --git a/src/Generator/Generators/C/CppTypePrinter.cs b/src/Generator/Generators/C/CppTypePrinter.cs index 82ccdb8e..23f46c29 100644 --- a/src/Generator/Generators/C/CppTypePrinter.cs +++ b/src/Generator/Generators/C/CppTypePrinter.cs @@ -74,8 +74,7 @@ namespace CppSharp.Generators.C return true; } - public override TypePrinterResult VisitTagType(TagType tag, - TypeQualifiers quals) + public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals) { if (FindTypeMap(tag, out var result)) return result; @@ -122,8 +121,7 @@ namespace CppSharp.Generators.C return string.Empty; } - public override TypePrinterResult VisitPointerType(PointerType pointer, - TypeQualifiers quals) + public override TypePrinterResult VisitPointerType(PointerType pointer, TypeQualifiers quals) { if (FindTypeMap(pointer, out TypePrinterResult result)) return result; @@ -139,10 +137,10 @@ namespace CppSharp.Generators.C var paren = array != null && pointer.Modifier == PointerType.TypeModifier.LVReference; if (paren) - pointeeType.NamePrefix.Append("("); + pointeeType.NamePrefix.Append('('); pointeeType.NamePrefix.Append(mod); if (paren) - pointeeType.NameSuffix.Insert(0, ")"); + pointeeType.NameSuffix.Insert(0, ')'); var qual = GetStringQuals(quals, false); if (!string.IsNullOrEmpty(qual)) @@ -151,21 +149,18 @@ namespace CppSharp.Generators.C return pointeeType; } - public override TypePrinterResult VisitMemberPointerType(MemberPointerType member, - TypeQualifiers quals) + public override TypePrinterResult VisitMemberPointerType(MemberPointerType member, TypeQualifiers quals) { return string.Empty; } - public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, - TypeQualifiers quals) + public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals) { var qual = GetStringQuals(quals); return $"{qual}{VisitPrimitiveType(builtin.Type)}"; } - public override TypePrinterResult VisitPrimitiveType(PrimitiveType primitive, - TypeQualifiers quals) + public override TypePrinterResult VisitPrimitiveType(PrimitiveType primitive, TypeQualifiers quals) { var qual = GetStringQuals(quals); return $"{qual}{VisitPrimitiveType(primitive)}"; @@ -235,8 +230,7 @@ namespace CppSharp.Generators.C throw new NotSupportedException(); } - public override TypePrinterResult VisitTypedefType(TypedefType typedef, - TypeQualifiers quals) + public override TypePrinterResult VisitTypedefType(TypedefType typedef, TypeQualifiers quals) { var qual = GetStringQuals(quals); if (ResolveTypedefs && !typedef.Declaration.Type.IsPointerTo(out FunctionType _)) @@ -269,8 +263,7 @@ namespace CppSharp.Generators.C return decayed.Decayed.Visit(this); } - public override TypePrinterResult VisitTemplateSpecializationType( - TemplateSpecializationType template, TypeQualifiers quals) + public override TypePrinterResult VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals) { var specialization = template.GetClassTemplateSpecialization(); if (specialization == null) @@ -324,11 +317,11 @@ namespace CppSharp.Generators.C { if (unaryTransformType.Desugared.Type != null) return unaryTransformType.Desugared.Visit(this); + return unaryTransformType.BaseType.Visit(this); } - public override TypePrinterResult VisitVectorType(VectorType vectorType, - TypeQualifiers quals) + public override TypePrinterResult VisitVectorType(VectorType vectorType, TypeQualifiers quals) { // an incomplete implementation but we'd hardly need anything better return "__attribute__()"; @@ -606,24 +599,16 @@ namespace CppSharp.Generators.C public override TypePrinterResult VisitFunctionDecl(Function function) { - string @class; - switch (MethodScopeKind) + string @class = MethodScopeKind switch { - case TypePrintScopeKind.Qualified: - @class = $"{function.Namespace.Visit(this)}::"; - break; - case TypePrintScopeKind.GlobalQualified: - @class = $"::{function.Namespace.Visit(this)}::"; - break; - default: - @class = string.Empty; - break; - } + TypePrintScopeKind.Qualified => $"{function.Namespace.Visit(this)}::", + TypePrintScopeKind.GlobalQualified => $"::{function.Namespace.Visit(this)}::", + _ => string.Empty, + }; var @params = string.Join(", ", function.Parameters.Select(p => p.Visit(this))); var @const = function is Method method && method.IsConst ? " const" : string.Empty; - var name = function.OperatorKind == CXXOperatorKind.Conversion || - function.OperatorKind == CXXOperatorKind.ExplicitConversion ? + var name = function.OperatorKind is CXXOperatorKind.Conversion or CXXOperatorKind.ExplicitConversion ? $"operator {function.OriginalReturnType.Visit(this)}" : function.OriginalName; diff --git a/src/Generator/Generators/CodeGenerator.cs b/src/Generator/Generators/CodeGenerator.cs index b3d74a31..793b9873 100644 --- a/src/Generator/Generators/CodeGenerator.cs +++ b/src/Generator/Generators/CodeGenerator.cs @@ -1058,27 +1058,33 @@ namespace CppSharp.Generators throw new NotImplementedException(); } - public virtual bool VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr stmt){ + public virtual bool VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr stmt) + { throw new NotImplementedException(); } - public virtual bool VisitSourceLocExpr(SourceLocExpr stmt){ + public virtual bool VisitSourceLocExpr(SourceLocExpr stmt) + { throw new NotImplementedException(); } - public virtual bool VisitRecoveryExpr(RecoveryExpr stmt){ + public virtual bool VisitRecoveryExpr(RecoveryExpr stmt) + { throw new NotImplementedException(); } - public virtual bool VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator stmt){ + public virtual bool VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator stmt) + { throw new NotImplementedException(); } - public virtual bool VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr stmt){ + public virtual bool VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr stmt) + { throw new NotImplementedException(); } - public virtual bool VisitCXXParenListInitExpr(CXXParenListInitExpr stmt){ + public virtual bool VisitCXXParenListInitExpr(CXXParenListInitExpr stmt) + { throw new NotImplementedException(); } diff --git a/src/Generator/Generators/TypePrinter.cs b/src/Generator/Generators/TypePrinter.cs index 136b74d3..807b81b4 100644 --- a/src/Generator/Generators/TypePrinter.cs +++ b/src/Generator/Generators/TypePrinter.cs @@ -10,8 +10,8 @@ namespace CppSharp.Generators { public string Type { get; set; } public string Name { get; set; } = string.Empty; - public StringBuilder NamePrefix { get; set; } = new StringBuilder(); - public StringBuilder NameSuffix { get; set; } = new StringBuilder(); + public StringBuilder NamePrefix { get; set; } = new(); + public StringBuilder NameSuffix { get; set; } = new(); public TypeMap TypeMap { get; set; } public GeneratorKind Kind { get; set; } @@ -25,11 +25,11 @@ namespace CppSharp.Generators { var index = Type.LastIndexOf('.'); if (index != -1) - Type = Type.Substring(index + 1); + Type = Type[(index + 1)..]; } public static implicit operator TypePrinterResult(string type) => - new TypePrinterResult { Type = type }; + new() { Type = type }; public static implicit operator string(TypePrinterResult result) => result.ToString(); @@ -196,20 +196,17 @@ namespace CppSharp.Generators throw new NotImplementedException(); } - public virtual TypePrinterResult VisitFunctionTemplateDecl( - FunctionTemplate template) + public virtual TypePrinterResult VisitFunctionTemplateDecl(FunctionTemplate template) { throw new NotImplementedException(); } - public virtual TypePrinterResult VisitFunctionTemplateSpecializationDecl( - FunctionTemplateSpecialization specialization) + public virtual TypePrinterResult VisitFunctionTemplateSpecializationDecl(FunctionTemplateSpecialization specialization) { throw new NotImplementedException(); } - public virtual TypePrinterResult VisitFunctionType(FunctionType function, - TypeQualifiers quals) + public virtual TypePrinterResult VisitFunctionType(FunctionType function, TypeQualifiers quals) { throw new NotImplementedException(); } diff --git a/src/Generator/Passes/GetterSetterToPropertyPass.cs b/src/Generator/Passes/GetterSetterToPropertyPass.cs index 0653ab3a..231bab6a 100644 --- a/src/Generator/Passes/GetterSetterToPropertyPass.cs +++ b/src/Generator/Passes/GetterSetterToPropertyPass.cs @@ -143,14 +143,14 @@ namespace CppSharp.Passes var firstWord = GetFirstWord(property.GetMethod.Name); var isKeyword = firstWord.Length < property.GetMethod.Name.Length && - Match(firstWord, new[] {"get", "is", "has"}); + Match(firstWord, new[] { "get", "is", "has" }); switch (Options.PropertyDetectionMode) { case PropertyDetectionMode.Keywords: return isKeyword; case PropertyDetectionMode.Dictionary: - var isAction = Match(firstWord, new[] {"to", "new", "on"}) || Verbs.Contains(firstWord); + var isAction = Match(firstWord, new[] { "to", "new", "on" }) || Verbs.Contains(firstWord); return isKeyword || !isAction; default: return false; @@ -287,8 +287,7 @@ namespace CppSharp.Passes m => m.IsGenerated && m.Name == property.Name)) { var oldName = method.Name; - method.Name = $@"get{char.ToUpperInvariant(method.Name[0])}{ - method.Name.Substring(1)}"; + method.Name = $@"get{char.ToUpperInvariant(method.Name[0])}{method.Name.Substring(1)}"; Diagnostics.Debug("Method {0}::{1} renamed to {2}", method.Namespace.Name, oldName, method.Name); } @@ -296,8 +295,7 @@ namespace CppSharp.Passes e => e.Name == property.Name)) { var oldName = @event.Name; - @event.Name = $@"on{char.ToUpperInvariant(@event.Name[0])}{ - @event.Name.Substring(1)}"; + @event.Name = $@"on{char.ToUpperInvariant(@event.Name[0])}{@event.Name.Substring(1)}"; Diagnostics.Debug("Event {0}::{1} renamed to {2}", @event.Namespace.Name, oldName, @event.Name); } @@ -345,7 +343,7 @@ namespace CppSharp.Passes private static string GetPropertyName(string name) { var firstWord = GetFirstWord(name); - if (!Match(firstWord, new[] {"get"}) || + if (!Match(firstWord, new[] { "get" }) || (string.Compare(name, firstWord, StringComparison.InvariantCultureIgnoreCase) == 0) || char.IsNumber(name[3])) return name; @@ -368,7 +366,7 @@ namespace CppSharp.Passes private bool IsGetter(Method method) => !method.IsDestructor && - !method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void) && + !method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void) && method.Parameters.All(p => p.Kind == ParameterKind.IndirectReturnType); private static bool IsSetter(Method method)