Browse Source

Code cleanup

pull/1914/head
duckdoom5 4 months ago
parent
commit
d339bfe9d2
  1. 117
      src/AST/Type.cs
  2. 126
      src/AST/TypeExtensions.cs
  3. 2
      src/AST/Typedef.cs
  4. 16
      src/CppParser/Sources.h
  5. 49
      src/Generator/Generators/C/CppTypePrinter.cs
  6. 18
      src/Generator/Generators/CodeGenerator.cs
  7. 17
      src/Generator/Generators/TypePrinter.cs
  8. 12
      src/Generator/Passes/GetterSetterToPropertyPass.cs

117
src/AST/Type.cs

@ -26,7 +26,7 @@ namespace CppSharp.AST
} }
public abstract T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals public abstract T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals
= new TypeQualifiers()); = new());
public override string ToString() public override string ToString()
{ {
@ -90,9 +90,9 @@ namespace CppSharp.AST
public override bool Equals(object obj) 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); return Type.Equals(type.Type) && Qualifiers.Equals(type.Qualifiers);
} }
@ -132,7 +132,7 @@ namespace CppSharp.AST
public Declaration Declaration; public Declaration Declaration;
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitTagType(this, quals); return visitor.VisitTagType(this, quals);
} }
@ -144,8 +144,8 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as TagType; if (obj is not TagType type)
if (type == null) return false; return false;
return Declaration.Equals(type.Declaration); return Declaration.Equals(type.Declaration);
} }
@ -198,7 +198,7 @@ namespace CppSharp.AST
public Type Type => QualifiedType.Type; public Type Type => QualifiedType.Type;
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitArrayType(this, quals); return visitor.VisitArrayType(this, quals);
} }
@ -210,8 +210,9 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as ArrayType; if (obj is not ArrayType type)
if (type == null) return false; return false;
var equals = QualifiedType.Equals(type.QualifiedType) && SizeType.Equals(type.SizeType); var equals = QualifiedType.Equals(type.QualifiedType) && SizeType.Equals(type.SizeType);
if (SizeType == ArraySize.Constant) if (SizeType == ArraySize.Constant)
@ -251,7 +252,7 @@ namespace CppSharp.AST
public QualifiedType ReturnType; public QualifiedType ReturnType;
// Argument types. // Argument types.
public List<Parameter> Parameters { get; } = new List<Parameter>(); public List<Parameter> Parameters { get; } = new();
public CallingConvention CallingConvention { get; set; } public CallingConvention CallingConvention { get; set; }
@ -270,7 +271,7 @@ namespace CppSharp.AST
IsDependent = type.IsDependent; IsDependent = type.IsDependent;
} }
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitFunctionType(this, quals); return visitor.VisitFunctionType(this, quals);
} }
@ -282,8 +283,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as FunctionType; if (obj is not FunctionType type) return false;
if (type == null) return false;
return ReturnType.Equals(type.ReturnType) && Parameters.SequenceEqual(type.Parameters); return ReturnType.Equals(type.ReturnType) && Parameters.SequenceEqual(type.Parameters);
} }
@ -300,7 +300,7 @@ namespace CppSharp.AST
/// </summary> /// </summary>
public class PointerType : Type public class PointerType : Type
{ {
public PointerType(QualifiedType pointee = new QualifiedType()) public PointerType(QualifiedType pointee = new())
{ {
Modifier = TypeModifier.Pointer; Modifier = TypeModifier.Pointer;
QualifiedPointee = pointee; QualifiedPointee = pointee;
@ -331,21 +331,14 @@ namespace CppSharp.AST
Modifier = type.Modifier; Modifier = type.Modifier;
} }
public bool IsReference public bool IsReference => Modifier is TypeModifier.LVReference or TypeModifier.RVReference;
{
get
{
return Modifier == TypeModifier.LVReference ||
Modifier == TypeModifier.RVReference;
}
}
public QualifiedType QualifiedPointee; public QualifiedType QualifiedPointee;
public Type Pointee { get { return QualifiedPointee.Type; } } public Type Pointee => QualifiedPointee.Type;
public TypeModifier Modifier; public TypeModifier Modifier;
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitPointerType(this, quals); return visitor.VisitPointerType(this, quals);
} }
@ -357,8 +350,8 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as PointerType; if (obj is not PointerType type)
if (type == null) return false; return false;
return QualifiedPointee.Equals(type.QualifiedPointee) return QualifiedPointee.Equals(type.QualifiedPointee)
&& Modifier == type.Modifier; && Modifier == type.Modifier;
@ -386,12 +379,9 @@ namespace CppSharp.AST
type.QualifiedPointee.Qualifiers); type.QualifiedPointee.Qualifiers);
} }
public Type Pointee public Type Pointee => QualifiedPointee.Type;
{
get { return QualifiedPointee.Type; }
}
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitMemberPointerType(this, quals); return visitor.VisitMemberPointerType(this, quals);
} }
@ -403,8 +393,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var pointer = obj as MemberPointerType; if (obj is not MemberPointerType pointer) return false;
if (pointer == null) return false;
return QualifiedPointee.Equals(pointer.QualifiedPointee); return QualifiedPointee.Equals(pointer.QualifiedPointee);
} }
@ -434,7 +423,7 @@ namespace CppSharp.AST
Declaration = type.Declaration; Declaration = type.Declaration;
} }
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitTypedefType(this, quals); return visitor.VisitTypedefType(this, quals);
} }
@ -446,8 +435,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var typedef = obj as TypedefType; if (obj is not TypedefType typedef)
if (typedef == null)
return false; return false;
return Declaration.OriginalName == typedef.Declaration.OriginalName && return Declaration.OriginalName == typedef.Declaration.OriginalName &&
@ -485,7 +473,7 @@ namespace CppSharp.AST
Equivalent = new QualifiedType((Type)type.Equivalent.Type.Clone(), type.Equivalent.Qualifiers); Equivalent = new QualifiedType((Type)type.Equivalent.Type.Clone(), type.Equivalent.Qualifiers);
} }
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitAttributedType(this, quals); return visitor.VisitAttributedType(this, quals);
} }
@ -502,8 +490,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var attributed = obj as AttributedType; if (obj is not AttributedType attributed) return false;
if (attributed == null) return false;
return Modified.Equals(attributed.Modified) return Modified.Equals(attributed.Modified)
&& Equivalent.Equals(attributed.Equivalent); && Equivalent.Equals(attributed.Equivalent);
@ -538,7 +525,7 @@ namespace CppSharp.AST
} }
public override T Visit<T>(ITypeVisitor<T> visitor, public override T Visit<T>(ITypeVisitor<T> visitor,
TypeQualifiers quals = new TypeQualifiers()) TypeQualifiers quals = new())
{ {
return visitor.VisitDecayedType(this, quals); return visitor.VisitDecayedType(this, quals);
} }
@ -550,8 +537,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var decay = obj as DecayedType; if (obj is not DecayedType decay) return false;
if (decay == null) return false;
return Original.Equals(decay.Original); return Original.Equals(decay.Original);
} }
@ -704,7 +690,7 @@ namespace CppSharp.AST
} }
public override T Visit<T>(ITypeVisitor<T> visitor, public override T Visit<T>(ITypeVisitor<T> visitor,
TypeQualifiers quals = new TypeQualifiers()) TypeQualifiers quals = new())
{ {
return visitor.VisitTemplateSpecializationType(this, quals); return visitor.VisitTemplateSpecializationType(this, quals);
} }
@ -716,8 +702,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as TemplateSpecializationType; if (obj is not TemplateSpecializationType type) return false;
if (type == null) return false;
return Arguments.SequenceEqual(type.Arguments) && return Arguments.SequenceEqual(type.Arguments) &&
((Template != null && Template.Name == type.Template.Name) || ((Template != null && Template.Name == type.Template.Name) ||
@ -759,7 +744,7 @@ namespace CppSharp.AST
public QualifiedType Desugared; public QualifiedType Desugared;
public override T Visit<T>(ITypeVisitor<T> visitor, public override T Visit<T>(ITypeVisitor<T> visitor,
TypeQualifiers quals = new TypeQualifiers()) TypeQualifiers quals = new())
{ {
return visitor.VisitDependentTemplateSpecializationType(this, quals); return visitor.VisitDependentTemplateSpecializationType(this, quals);
} }
@ -771,8 +756,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as TemplateSpecializationType; if (obj is not TemplateSpecializationType type) return false;
if (type == null) return false;
return Arguments.SequenceEqual(type.Arguments) && return Arguments.SequenceEqual(type.Arguments) &&
Desugared == type.Desugared; Desugared == type.Desugared;
@ -815,7 +799,7 @@ namespace CppSharp.AST
} }
public override T Visit<T>(ITypeVisitor<T> visitor, public override T Visit<T>(ITypeVisitor<T> visitor,
TypeQualifiers quals = new TypeQualifiers()) TypeQualifiers quals = new())
{ {
return visitor.VisitTemplateParameterType(this, quals); return visitor.VisitTemplateParameterType(this, quals);
} }
@ -827,8 +811,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as TemplateParameterType; if (obj is not TemplateParameterType type) return false;
if (type == null) return false;
return Parameter == type.Parameter return Parameter == type.Parameter
&& Depth == type.Depth && Depth == type.Depth
@ -865,7 +848,7 @@ namespace CppSharp.AST
} }
public override T Visit<T>(ITypeVisitor<T> visitor, public override T Visit<T>(ITypeVisitor<T> visitor,
TypeQualifiers quals = new TypeQualifiers()) TypeQualifiers quals = new())
{ {
return visitor.VisitTemplateParameterSubstitutionType(this, quals); return visitor.VisitTemplateParameterSubstitutionType(this, quals);
} }
@ -877,8 +860,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as TemplateParameterSubstitutionType; if (obj is not TemplateParameterSubstitutionType type) return false;
if (type == null) return false;
return ReplacedParameter.Equals(type.ReplacedParameter) && return ReplacedParameter.Equals(type.ReplacedParameter) &&
Replacement.Equals(type.Replacement); Replacement.Equals(type.Replacement);
@ -915,7 +897,7 @@ namespace CppSharp.AST
public QualifiedType InjectedSpecializationType { get; set; } public QualifiedType InjectedSpecializationType { get; set; }
public override T Visit<T>(ITypeVisitor<T> visitor, public override T Visit<T>(ITypeVisitor<T> visitor,
TypeQualifiers quals = new TypeQualifiers()) TypeQualifiers quals = new())
{ {
return visitor.VisitInjectedClassNameType(this, quals); return visitor.VisitInjectedClassNameType(this, quals);
} }
@ -927,8 +909,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as InjectedClassNameType; if (obj is not InjectedClassNameType type) return false;
if (type == null) return false;
if (TemplateSpecialization == null || type.TemplateSpecialization == null) if (TemplateSpecialization == null || type.TemplateSpecialization == null)
return TemplateSpecialization == type.TemplateSpecialization; return TemplateSpecialization == type.TemplateSpecialization;
@ -962,7 +943,7 @@ namespace CppSharp.AST
public string Identifier { get; set; } public string Identifier { get; set; }
public override T Visit<T>(ITypeVisitor<T> visitor, public override T Visit<T>(ITypeVisitor<T> visitor,
TypeQualifiers quals = new TypeQualifiers()) TypeQualifiers quals = new())
{ {
return visitor.VisitDependentNameType(this, quals); return visitor.VisitDependentNameType(this, quals);
} }
@ -999,7 +980,7 @@ namespace CppSharp.AST
public System.Type Type; public System.Type Type;
public override T Visit<T>(ITypeVisitor<T> visitor, public override T Visit<T>(ITypeVisitor<T> visitor,
TypeQualifiers quals = new TypeQualifiers()) TypeQualifiers quals = new())
{ {
return visitor.VisitCILType(this, quals); return visitor.VisitCILType(this, quals);
} }
@ -1011,8 +992,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as CILType; if (obj is not CILType type) return false;
if (type == null) return false;
return Type == type.Type; return Type == type.Type;
} }
@ -1031,7 +1011,7 @@ namespace CppSharp.AST
{ {
} }
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitPackExpansionType(this, quals); return visitor.VisitPackExpansionType(this, quals);
} }
@ -1058,7 +1038,7 @@ namespace CppSharp.AST
public QualifiedType Desugared { get; set; } public QualifiedType Desugared { get; set; }
public QualifiedType BaseType { get; set; } public QualifiedType BaseType { get; set; }
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitUnaryTransformType(this, quals); return visitor.VisitUnaryTransformType(this, quals);
} }
@ -1085,7 +1065,7 @@ namespace CppSharp.AST
public UnresolvedUsingTypename Declaration { get; set; } public UnresolvedUsingTypename Declaration { get; set; }
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitUnresolvedUsingType(this, quals); return visitor.VisitUnresolvedUsingType(this, quals);
} }
@ -1112,7 +1092,7 @@ namespace CppSharp.AST
public QualifiedType ElementType { get; set; } public QualifiedType ElementType { get; set; }
public uint NumElements { get; set; } public uint NumElements { get; set; }
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitVectorType(this, quals); return visitor.VisitVectorType(this, quals);
} }
@ -1144,7 +1124,7 @@ namespace CppSharp.AST
public string Description; public string Description;
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitUnsupportedType(this, quals); return visitor.VisitUnsupportedType(this, quals);
} }
@ -1243,7 +1223,7 @@ namespace CppSharp.AST
// Primitive type of built-in type. // Primitive type of built-in type.
public PrimitiveType Type; public PrimitiveType Type;
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers()) public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new())
{ {
return visitor.VisitBuiltinType(this, quals); return visitor.VisitBuiltinType(this, quals);
} }
@ -1255,8 +1235,7 @@ namespace CppSharp.AST
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as BuiltinType; if (obj is not BuiltinType type) return false;
if (type == null) return false;
return Type == type.Type; return Type == type.Type;
} }

126
src/AST/TypeExtensions.cs

@ -4,14 +4,12 @@
{ {
public static bool IsPrimitiveType(this Type t) public static bool IsPrimitiveType(this Type t)
{ {
PrimitiveType type; return t.IsPrimitiveType(out PrimitiveType _);
return t.IsPrimitiveType(out type);
} }
public static bool IsPrimitiveType(this Type t, out PrimitiveType primitive) public static bool IsPrimitiveType(this Type t, out PrimitiveType primitive)
{ {
var builtin = t.Desugar() as BuiltinType; if (t.Desugar() is BuiltinType builtin)
if (builtin != null)
{ {
primitive = builtin.Type; primitive = builtin.Type;
return true; return true;
@ -32,9 +30,7 @@
public static bool IsEnumType(this Type t) public static bool IsEnumType(this Type t)
{ {
var tag = t.Desugar() as TagType; if (t.Desugar() is not TagType tag)
if (tag == null)
return false; return false;
return tag.Declaration is Enumeration; return tag.Declaration is Enumeration;
@ -47,36 +43,28 @@
public static bool IsPointer(this Type t) public static bool IsPointer(this Type t)
{ {
var functionPointer = t as MemberPointerType; if (t is MemberPointerType)
if (functionPointer != null)
return true; return true;
var pointer = t as PointerType;
if (pointer == null) if (t is not PointerType pointer)
return false; return false;
return pointer.Modifier == PointerType.TypeModifier.Pointer; return pointer.Modifier == PointerType.TypeModifier.Pointer;
} }
public static bool IsReference(this Type t) public static bool IsReference(this Type t)
{ {
var pointer = t as PointerType; return t is PointerType { IsReference: true };
if (pointer == null)
return false;
return pointer.IsReference;
} }
public static bool IsPointerToPrimitiveType(this Type t) public static bool IsPointerToPrimitiveType(this Type t)
{ {
var ptr = t as PointerType; return t is PointerType ptr && ptr.Pointee.IsPrimitiveType(out _);
if (ptr == null)
return false;
PrimitiveType primitiveType;
return ptr.Pointee.IsPrimitiveType(out primitiveType);
} }
public static bool IsPointerToPrimitiveType(this Type t, out PrimitiveType primitive) public static bool IsPointerToPrimitiveType(this Type t, out PrimitiveType primitive)
{ {
var ptr = t as PointerType; if (t is not PointerType ptr)
if (ptr == null)
{ {
primitive = PrimitiveType.Null; primitive = PrimitiveType.Null;
return false; return false;
@ -86,24 +74,21 @@
public static bool IsPointerToPrimitiveType(this Type t, PrimitiveType primitive) public static bool IsPointerToPrimitiveType(this Type t, PrimitiveType primitive)
{ {
var ptr = t as PointerType; if (t is not PointerType ptr)
if (ptr == null)
return false; return false;
return ptr.Pointee.IsPrimitiveType(primitive); return ptr.Pointee.IsPrimitiveType(primitive);
} }
public static bool IsPointerToEnum(this Type t) public static bool IsPointerToEnum(this Type t)
{ {
var ptr = t as PointerType; if (t is not PointerType ptr)
if (ptr == null)
return false; return false;
return ptr.Pointee.IsEnumType(); return ptr.Pointee.IsEnumType();
} }
public static bool IsPointerToEnum(this Type t, out Enumeration @enum) public static bool IsPointerToEnum(this Type t, out Enumeration @enum)
{ {
var ptr = t as PointerType; if (t is not PointerType ptr)
if (ptr == null)
{ {
@enum = null; @enum = null;
return false; return false;
@ -111,23 +96,22 @@
return ptr.Pointee.TryGetEnum(out @enum); return ptr.Pointee.TryGetEnum(out @enum);
} }
public static bool IsPointerTo<T>(this Type t, out T type) where T : Type public static bool IsPointerTo<T>(this Type t, out T type)
where T : Type
{ {
var pointee = t.GetPointee(); type = t.GetPointee() switch
type = pointee as T;
if (type == null)
{ {
var attributedType = pointee as AttributedType; T tType => tType,
if (attributedType != null) AttributedType attributedType => attributedType.Modified.Type as T,
type = attributedType.Modified.Type as T; _ => null
} };
return type != null; return type != null;
} }
public static bool IsClass(this Type t) public static bool IsClass(this Type t)
{ {
Class @class; return t.TryGetClass(out _);
return t.TryGetClass(out @class);
} }
public static bool TryGetClass(this Type t, out Class @class, Class value = null) public static bool TryGetClass(this Type t, out Class @class, Class value = null)
@ -135,11 +119,12 @@
return TryGetDeclaration(t, out @class, value); return TryGetDeclaration(t, out @class, value);
} }
public static bool TryGetDeclaration<T>(this Type t, out T decl, T value = null) where T : Declaration public static bool TryGetDeclaration<T>(this Type t, out T decl, T value = null)
where T : Declaration
{ {
t = t.Desugar(); t = t.Desugar();
TagType tagType = null; TagType tagType;
if (t is TemplateSpecializationType type) if (t is TemplateSpecializationType type)
{ {
if (type.IsDependent) if (type.IsDependent)
@ -193,15 +178,12 @@
public static bool IsEnum(this Type t) public static bool IsEnum(this Type t)
{ {
Enumeration @enum; return t.TryGetEnum(out _);
return t.TryGetEnum(out @enum);
} }
public static bool TryGetEnum(this Type t, out Enumeration @enum) public static bool TryGetEnum(this Type t, out Enumeration @enum)
{ {
var tag = t.Desugar() as TagType; if (t.Desugar() is not TagType tag)
if (tag == null)
{ {
@enum = null; @enum = null;
return false; return false;
@ -269,13 +251,12 @@
/// </summary> /// </summary>
public static Type GetPointee(this Type t) public static Type GetPointee(this Type t)
{ {
var ptr = t as PointerType; return t switch
if (ptr != null) {
return ptr.Pointee; PointerType ptr => ptr.Pointee,
var memberPtr = t as MemberPointerType; MemberPointerType memberPtr => memberPtr.QualifiedPointee.Type,
if (memberPtr != null) _ => null
return memberPtr.QualifiedPointee.Type; };
return null;
} }
/// <summary> /// <summary>
@ -296,17 +277,28 @@
return finalPointee; 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;
}
/// <summary> /// <summary>
/// If t is a pointer type the type pointed to by t will be returned. /// If t is a pointer type the type pointed to by t will be returned.
/// Otherwise the default qualified type. /// Otherwise the default qualified type.
/// </summary> /// </summary>
public static QualifiedType GetQualifiedPointee(this Type t) public static QualifiedType GetQualifiedPointee(this Type t)
{ {
var ptr = t as PointerType; if (t is PointerType ptr)
if (ptr != null)
return ptr.QualifiedPointee; return ptr.QualifiedPointee;
var memberPtr = t as MemberPointerType; if (t is MemberPointerType memberPtr)
if (memberPtr != null)
return memberPtr.QualifiedPointee; return memberPtr.QualifiedPointee;
return new QualifiedType(); return new QualifiedType();
} }
@ -329,21 +321,6 @@
return finalPointee; 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) public static bool ResolvesTo(this QualifiedType type, QualifiedType other)
{ {
if (!type.Qualifiers.Equals(other.Qualifiers)) if (!type.Qualifiers.Equals(other.Qualifiers))
@ -351,9 +328,7 @@
var left = type.Type.Desugar(); var left = type.Type.Desugar();
var right = other.Type.Desugar(); var right = other.Type.Desugar();
var leftPointer = left as PointerType; if (left is PointerType leftPointer && right is PointerType rightPointer)
var rightPointer = right as PointerType;
if (leftPointer != null && rightPointer != null)
{ {
return leftPointer.Modifier == rightPointer.Modifier && return leftPointer.Modifier == rightPointer.Modifier &&
leftPointer.QualifiedPointee.ResolvesTo(rightPointer.QualifiedPointee); leftPointer.QualifiedPointee.ResolvesTo(rightPointer.QualifiedPointee);
@ -388,8 +363,7 @@
qualifiers.IsConst = false; qualifiers.IsConst = false;
type.Qualifiers = qualifiers; type.Qualifiers = qualifiers;
var ptr = type.Type as PointerType; if (type.Type is PointerType ptr)
if (ptr != null)
{ {
var pointee = ptr.QualifiedPointee; var pointee = ptr.QualifiedPointee;
var pointeeQualifiers = pointee.Qualifiers; var pointeeQualifiers = pointee.Qualifiers;

2
src/AST/Typedef.cs

@ -5,7 +5,7 @@ namespace CppSharp.AST
/// </summary> /// </summary>
public abstract class TypedefNameDecl : Declaration, ITypedDecl public abstract class TypedefNameDecl : Declaration, ITypedDecl
{ {
public Type Type { get { return QualifiedType.Type; } } public Type Type => QualifiedType.Type;
public QualifiedType QualifiedType { get; set; } public QualifiedType QualifiedType { get; set; }
public bool IsSynthetized { get; set; } public bool IsSynthetized { get; set; }
} }

16
src/CppParser/Sources.h

@ -9,19 +9,19 @@
#include "Helpers.h" #include "Helpers.h"
namespace CppSharp { namespace CppParser { namespace CppSharp::CppParser {
struct CS_API CS_VALUE_TYPE SourceLocation struct CS_API CS_VALUE_TYPE SourceLocation
{ {
SourceLocation(); SourceLocation();
SourceLocation(unsigned ID); SourceLocation(unsigned ID);
unsigned ID; unsigned ID;
}; };
struct CS_API SourceRange struct CS_API SourceRange
{ {
SourceLocation beginLoc; SourceLocation beginLoc;
SourceLocation endLoc; SourceLocation endLoc;
}; };
}} // namespace CppSharp::CppParser } // namespace CppSharp::CppParser

49
src/Generator/Generators/C/CppTypePrinter.cs

@ -74,8 +74,7 @@ namespace CppSharp.Generators.C
return true; return true;
} }
public override TypePrinterResult VisitTagType(TagType tag, public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals)
TypeQualifiers quals)
{ {
if (FindTypeMap(tag, out var result)) if (FindTypeMap(tag, out var result))
return result; return result;
@ -122,8 +121,7 @@ namespace CppSharp.Generators.C
return string.Empty; return string.Empty;
} }
public override TypePrinterResult VisitPointerType(PointerType pointer, public override TypePrinterResult VisitPointerType(PointerType pointer, TypeQualifiers quals)
TypeQualifiers quals)
{ {
if (FindTypeMap(pointer, out TypePrinterResult result)) if (FindTypeMap(pointer, out TypePrinterResult result))
return result; return result;
@ -139,10 +137,10 @@ namespace CppSharp.Generators.C
var paren = array != null && pointer.Modifier == PointerType.TypeModifier.LVReference; var paren = array != null && pointer.Modifier == PointerType.TypeModifier.LVReference;
if (paren) if (paren)
pointeeType.NamePrefix.Append("("); pointeeType.NamePrefix.Append('(');
pointeeType.NamePrefix.Append(mod); pointeeType.NamePrefix.Append(mod);
if (paren) if (paren)
pointeeType.NameSuffix.Insert(0, ")"); pointeeType.NameSuffix.Insert(0, ')');
var qual = GetStringQuals(quals, false); var qual = GetStringQuals(quals, false);
if (!string.IsNullOrEmpty(qual)) if (!string.IsNullOrEmpty(qual))
@ -151,21 +149,18 @@ namespace CppSharp.Generators.C
return pointeeType; return pointeeType;
} }
public override TypePrinterResult VisitMemberPointerType(MemberPointerType member, public override TypePrinterResult VisitMemberPointerType(MemberPointerType member, TypeQualifiers quals)
TypeQualifiers quals)
{ {
return string.Empty; return string.Empty;
} }
public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals)
TypeQualifiers quals)
{ {
var qual = GetStringQuals(quals); var qual = GetStringQuals(quals);
return $"{qual}{VisitPrimitiveType(builtin.Type)}"; return $"{qual}{VisitPrimitiveType(builtin.Type)}";
} }
public override TypePrinterResult VisitPrimitiveType(PrimitiveType primitive, public override TypePrinterResult VisitPrimitiveType(PrimitiveType primitive, TypeQualifiers quals)
TypeQualifiers quals)
{ {
var qual = GetStringQuals(quals); var qual = GetStringQuals(quals);
return $"{qual}{VisitPrimitiveType(primitive)}"; return $"{qual}{VisitPrimitiveType(primitive)}";
@ -235,8 +230,7 @@ namespace CppSharp.Generators.C
throw new NotSupportedException(); throw new NotSupportedException();
} }
public override TypePrinterResult VisitTypedefType(TypedefType typedef, public override TypePrinterResult VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
TypeQualifiers quals)
{ {
var qual = GetStringQuals(quals); var qual = GetStringQuals(quals);
if (ResolveTypedefs && !typedef.Declaration.Type.IsPointerTo(out FunctionType _)) if (ResolveTypedefs && !typedef.Declaration.Type.IsPointerTo(out FunctionType _))
@ -269,8 +263,7 @@ namespace CppSharp.Generators.C
return decayed.Decayed.Visit(this); return decayed.Decayed.Visit(this);
} }
public override TypePrinterResult VisitTemplateSpecializationType( public override TypePrinterResult VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals)
TemplateSpecializationType template, TypeQualifiers quals)
{ {
var specialization = template.GetClassTemplateSpecialization(); var specialization = template.GetClassTemplateSpecialization();
if (specialization == null) if (specialization == null)
@ -324,11 +317,11 @@ namespace CppSharp.Generators.C
{ {
if (unaryTransformType.Desugared.Type != null) if (unaryTransformType.Desugared.Type != null)
return unaryTransformType.Desugared.Visit(this); return unaryTransformType.Desugared.Visit(this);
return unaryTransformType.BaseType.Visit(this); return unaryTransformType.BaseType.Visit(this);
} }
public override TypePrinterResult VisitVectorType(VectorType vectorType, public override TypePrinterResult VisitVectorType(VectorType vectorType, TypeQualifiers quals)
TypeQualifiers quals)
{ {
// an incomplete implementation but we'd hardly need anything better // an incomplete implementation but we'd hardly need anything better
return "__attribute__()"; return "__attribute__()";
@ -606,24 +599,16 @@ namespace CppSharp.Generators.C
public override TypePrinterResult VisitFunctionDecl(Function function) public override TypePrinterResult VisitFunctionDecl(Function function)
{ {
string @class; string @class = MethodScopeKind switch
switch (MethodScopeKind)
{ {
case TypePrintScopeKind.Qualified: TypePrintScopeKind.Qualified => $"{function.Namespace.Visit(this)}::",
@class = $"{function.Namespace.Visit(this)}::"; TypePrintScopeKind.GlobalQualified => $"::{function.Namespace.Visit(this)}::",
break; _ => string.Empty,
case TypePrintScopeKind.GlobalQualified: };
@class = $"::{function.Namespace.Visit(this)}::";
break;
default:
@class = string.Empty;
break;
}
var @params = string.Join(", ", function.Parameters.Select(p => p.Visit(this))); var @params = string.Join(", ", function.Parameters.Select(p => p.Visit(this)));
var @const = function is Method method && method.IsConst ? " const" : string.Empty; var @const = function is Method method && method.IsConst ? " const" : string.Empty;
var name = function.OperatorKind == CXXOperatorKind.Conversion || var name = function.OperatorKind is CXXOperatorKind.Conversion or CXXOperatorKind.ExplicitConversion ?
function.OperatorKind == CXXOperatorKind.ExplicitConversion ?
$"operator {function.OriginalReturnType.Visit(this)}" : $"operator {function.OriginalReturnType.Visit(this)}" :
function.OriginalName; function.OriginalName;

18
src/Generator/Generators/CodeGenerator.cs

@ -1058,27 +1058,33 @@ namespace CppSharp.Generators
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual bool VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr stmt){ public virtual bool VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr stmt)
{
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual bool VisitSourceLocExpr(SourceLocExpr stmt){ public virtual bool VisitSourceLocExpr(SourceLocExpr stmt)
{
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual bool VisitRecoveryExpr(RecoveryExpr stmt){ public virtual bool VisitRecoveryExpr(RecoveryExpr stmt)
{
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual bool VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator stmt){ public virtual bool VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator stmt)
{
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual bool VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr stmt){ public virtual bool VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr stmt)
{
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual bool VisitCXXParenListInitExpr(CXXParenListInitExpr stmt){ public virtual bool VisitCXXParenListInitExpr(CXXParenListInitExpr stmt)
{
throw new NotImplementedException(); throw new NotImplementedException();
} }

17
src/Generator/Generators/TypePrinter.cs

@ -10,8 +10,8 @@ namespace CppSharp.Generators
{ {
public string Type { get; set; } public string Type { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public StringBuilder NamePrefix { get; set; } = new StringBuilder(); public StringBuilder NamePrefix { get; set; } = new();
public StringBuilder NameSuffix { get; set; } = new StringBuilder(); public StringBuilder NameSuffix { get; set; } = new();
public TypeMap TypeMap { get; set; } public TypeMap TypeMap { get; set; }
public GeneratorKind Kind { get; set; } public GeneratorKind Kind { get; set; }
@ -25,11 +25,11 @@ namespace CppSharp.Generators
{ {
var index = Type.LastIndexOf('.'); var index = Type.LastIndexOf('.');
if (index != -1) if (index != -1)
Type = Type.Substring(index + 1); Type = Type[(index + 1)..];
} }
public static implicit operator TypePrinterResult(string type) => public static implicit operator TypePrinterResult(string type) =>
new TypePrinterResult { Type = type }; new() { Type = type };
public static implicit operator string(TypePrinterResult result) => public static implicit operator string(TypePrinterResult result) =>
result.ToString(); result.ToString();
@ -196,20 +196,17 @@ namespace CppSharp.Generators
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual TypePrinterResult VisitFunctionTemplateDecl( public virtual TypePrinterResult VisitFunctionTemplateDecl(FunctionTemplate template)
FunctionTemplate template)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual TypePrinterResult VisitFunctionTemplateSpecializationDecl( public virtual TypePrinterResult VisitFunctionTemplateSpecializationDecl(FunctionTemplateSpecialization specialization)
FunctionTemplateSpecialization specialization)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual TypePrinterResult VisitFunctionType(FunctionType function, public virtual TypePrinterResult VisitFunctionType(FunctionType function, TypeQualifiers quals)
TypeQualifiers quals)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

12
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -143,14 +143,14 @@ namespace CppSharp.Passes
var firstWord = GetFirstWord(property.GetMethod.Name); var firstWord = GetFirstWord(property.GetMethod.Name);
var isKeyword = firstWord.Length < property.GetMethod.Name.Length && var isKeyword = firstWord.Length < property.GetMethod.Name.Length &&
Match(firstWord, new[] {"get", "is", "has"}); Match(firstWord, new[] { "get", "is", "has" });
switch (Options.PropertyDetectionMode) switch (Options.PropertyDetectionMode)
{ {
case PropertyDetectionMode.Keywords: case PropertyDetectionMode.Keywords:
return isKeyword; return isKeyword;
case PropertyDetectionMode.Dictionary: 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; return isKeyword || !isAction;
default: default:
return false; return false;
@ -287,8 +287,7 @@ namespace CppSharp.Passes
m => m.IsGenerated && m.Name == property.Name)) m => m.IsGenerated && m.Name == property.Name))
{ {
var oldName = method.Name; var oldName = method.Name;
method.Name = $@"get{char.ToUpperInvariant(method.Name[0])}{ method.Name = $@"get{char.ToUpperInvariant(method.Name[0])}{method.Name.Substring(1)}";
method.Name.Substring(1)}";
Diagnostics.Debug("Method {0}::{1} renamed to {2}", Diagnostics.Debug("Method {0}::{1} renamed to {2}",
method.Namespace.Name, oldName, method.Name); method.Namespace.Name, oldName, method.Name);
} }
@ -296,8 +295,7 @@ namespace CppSharp.Passes
e => e.Name == property.Name)) e => e.Name == property.Name))
{ {
var oldName = @event.Name; var oldName = @event.Name;
@event.Name = $@"on{char.ToUpperInvariant(@event.Name[0])}{ @event.Name = $@"on{char.ToUpperInvariant(@event.Name[0])}{@event.Name.Substring(1)}";
@event.Name.Substring(1)}";
Diagnostics.Debug("Event {0}::{1} renamed to {2}", Diagnostics.Debug("Event {0}::{1} renamed to {2}",
@event.Namespace.Name, oldName, @event.Name); @event.Namespace.Name, oldName, @event.Name);
} }
@ -345,7 +343,7 @@ namespace CppSharp.Passes
private static string GetPropertyName(string name) private static string GetPropertyName(string name)
{ {
var firstWord = GetFirstWord(name); var firstWord = GetFirstWord(name);
if (!Match(firstWord, new[] {"get"}) || if (!Match(firstWord, new[] { "get" }) ||
(string.Compare(name, firstWord, StringComparison.InvariantCultureIgnoreCase) == 0) || (string.Compare(name, firstWord, StringComparison.InvariantCultureIgnoreCase) == 0) ||
char.IsNumber(name[3])) return name; char.IsNumber(name[3])) return name;

Loading…
Cancel
Save