Browse Source

Code cleanup.

pull/1580/head
Joao Matos 4 years ago
parent
commit
5eebab2f94
  1. 58
      src/AST/Class.cs
  2. 54
      src/AST/Declaration.cs
  3. 70
      src/AST/DeclarationsList.cs
  4. 15
      src/AST/Function.cs
  5. 2
      src/AST/Method.cs
  6. 54
      src/AST/Template.cs
  7. 14
      src/AST/Type.cs
  8. 38
      src/AST/TypeExtensions.cs
  9. 25
      src/Generator/AST/Utils.cs
  10. 13
      src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs
  11. 3
      src/Generator/Passes/CheckAmbiguousFunctions.cs
  12. 20
      src/Generator/Passes/CheckDuplicatedNamesPass.cs
  13. 8
      src/Parser/ASTConverter.cs

58
src/AST/Class.cs

@ -47,19 +47,12 @@ namespace CppSharp.AST
{ {
get get
{ {
Class @class; Type.TryGetClass(out var @class);
Type.TryGetClass(out @class);
return @class; return @class;
} }
} }
public bool IsClass public bool IsClass => Type.IsClass();
{
get
{
return Type.IsClass();
}
}
} }
public enum ClassType public enum ClassType
@ -69,7 +62,7 @@ namespace CppSharp.AST
Interface Interface
} }
// Represents a C++ record Decl. // Represents a C++ record decl.
public class Class : DeclarationContext public class Class : DeclarationContext
{ {
public List<BaseClassSpecifier> Bases; public List<BaseClassSpecifier> Bases;
@ -104,10 +97,7 @@ namespace CppSharp.AST
public bool IsOpaque public bool IsOpaque
{ {
get => isOpaque ?? (IsIncomplete && CompleteDeclaration == null); get => isOpaque ?? (IsIncomplete && CompleteDeclaration == null);
set set => isOpaque = value;
{
isOpaque = value;
}
} }
// True if the class is dynamic. // True if the class is dynamic.
@ -163,39 +153,22 @@ namespace CppSharp.AST
} }
} }
public bool HasNonIgnoredBase public bool HasNonIgnoredBase =>
{ HasBaseClass && !IsValueType
get && BaseClass != null
{ && !BaseClass.IsValueType
return HasBaseClass && !IsValueType && BaseClass.IsGenerated;
&& BaseClass != null
&& !BaseClass.IsValueType
&& BaseClass.IsGenerated;
}
}
public bool NeedsBase public bool NeedsBase => HasNonIgnoredBase && IsGenerated;
{
get { return HasNonIgnoredBase && IsGenerated; }
}
// When we have an interface, this is the class mapped to that interface. // When we have an interface, this is the class mapped to that interface.
public Class OriginalClass { get; set; } public Class OriginalClass { get; set; }
public bool IsValueType public bool IsValueType => Type == ClassType.ValueType || IsUnion;
{
get { return Type == ClassType.ValueType || IsUnion; }
}
public bool IsRefType public bool IsRefType => Type == ClassType.RefType && !IsUnion;
{
get { return Type == ClassType.RefType && !IsUnion; }
}
public bool IsInterface public bool IsInterface => Type == ClassType.Interface;
{
get { return Type == ClassType.Interface; }
}
public bool IsAbstractImpl public bool IsAbstractImpl
{ {
@ -261,10 +234,7 @@ namespace CppSharp.AST
} }
} }
public bool IsTemplate public bool IsTemplate => IsDependent && TemplateParameters.Count > 0;
{
get { return IsDependent && TemplateParameters.Count > 0; }
}
public override IEnumerable<Function> FindOperator(CXXOperatorKind kind) public override IEnumerable<Function> FindOperator(CXXOperatorKind kind)
{ {

54
src/AST/Declaration.cs

@ -53,24 +53,14 @@ namespace CppSharp.AST
public virtual GenerationKind GenerationKind public virtual GenerationKind GenerationKind
{ {
get get => generationKind ?? GenerationKind.Generate;
{ set => generationKind = value;
if (generationKind.HasValue)
return generationKind.Value;
return GenerationKind.Generate;
}
set
{
generationKind = value;
}
} }
/// <summary> /// <summary>
/// Whether the declaration should be generated. /// Whether the declaration should be generated.
/// </summary> /// </summary>
public virtual bool IsGenerated => GenerationKind == GenerationKind.Generate; public bool IsGenerated => GenerationKind == GenerationKind.Generate;
/// <summary> /// <summary>
/// Whether the declaration has an explicit set generation kind. /// Whether the declaration has an explicit set generation kind.
@ -81,8 +71,7 @@ namespace CppSharp.AST
/// Whether the declaration was explicitly set to be generated via /// Whether the declaration was explicitly set to be generated via
/// the GenerationKind property as opposed to the default generated state. /// the GenerationKind property as opposed to the default generated state.
/// </summary> /// </summary>
public virtual bool IsExplicitlyGenerated => public bool IsExplicitlyGenerated => generationKind == GenerationKind.Generate;
generationKind.HasValue && generationKind.Value == GenerationKind.Generate;
/// <summary> /// <summary>
/// Whether the declaration internal bindings should be generated. /// Whether the declaration internal bindings should be generated.
@ -92,9 +81,8 @@ namespace CppSharp.AST
/// <summary> /// <summary>
/// Whether a binded version of this declaration is available. /// Whether a binded version of this declaration is available.
/// </summary> /// </summary>
public bool IsDeclared => public bool IsDeclared => GenerationKind == GenerationKind.Generate
GenerationKind == GenerationKind.Generate || GenerationKind == GenerationKind.Internal;
|| GenerationKind == GenerationKind.Internal;
public void ExplicitlyIgnore() public void ExplicitlyIgnore()
{ {
@ -104,13 +92,13 @@ namespace CppSharp.AST
[Obsolete("Replace set by ExplicitlyIgnore(). Replace get by GenerationKind == GenerationKind.None.")] [Obsolete("Replace set by ExplicitlyIgnore(). Replace get by GenerationKind == GenerationKind.None.")]
public bool ExplicityIgnored public bool ExplicityIgnored
{ {
get { return GenerationKind == GenerationKind.None; } get => GenerationKind == GenerationKind.None;
set { if (value) ExplicitlyIgnore(); } set { if (value) ExplicitlyIgnore(); }
} }
public virtual bool Ignore public virtual bool Ignore
{ {
get { return GenerationKind == GenerationKind.None; } get => GenerationKind == GenerationKind.None;
set { if (value) ExplicitlyIgnore(); } set { if (value) ExplicitlyIgnore(); }
} }
} }
@ -135,12 +123,11 @@ namespace CppSharp.AST
// Namespace the declaration is contained in. // Namespace the declaration is contained in.
public DeclarationContext Namespace public DeclarationContext Namespace
{ {
get { return @namespace; } get => @namespace;
set set
{ {
@namespace = value; @namespace = value;
if (OriginalNamespace == null) OriginalNamespace ??= @namespace;
OriginalNamespace = @namespace;
} }
} }
@ -177,12 +164,11 @@ namespace CppSharp.AST
public virtual string Name public virtual string Name
{ {
get { return name; } get => name;
set set
{ {
name = value; name = value;
if (OriginalName == null) OriginalName ??= name;
OriginalName = name;
} }
} }
@ -192,19 +178,13 @@ namespace CppSharp.AST
/// the real and the "effective" name of the declaration to properly /// the real and the "effective" name of the declaration to properly
/// support things like inline namespaces when handling type maps. /// support things like inline namespaces when handling type maps.
/// </summary> /// </summary>
public virtual string LogicalName public virtual string LogicalName => Name;
{
get { return Name; }
}
/// <summary> /// <summary>
/// The effective original name of a declaration is the logical /// The effective original name of a declaration is the logical
/// original name for the declaration. /// original name for the declaration.
/// </summary> /// </summary>
public virtual string LogicalOriginalName public virtual string LogicalOriginalName => OriginalName;
{
get { return OriginalName; }
}
public static string QualifiedNameSeparator = "::"; public static string QualifiedNameSeparator = "::";
@ -233,8 +213,7 @@ namespace CppSharp.AST
var currentNamespace = @namespace; var currentNamespace = @namespace;
while (currentNamespace != null) while (currentNamespace != null)
{ {
var isInlineNamespace = currentNamespace is Namespace && var isInlineNamespace = currentNamespace is Namespace {IsInline: true};
((Namespace) currentNamespace).IsInline;
if (!isInlineNamespace) if (!isInlineNamespace)
namespaces.Push(currentNamespace); namespaces.Push(currentNamespace);
currentNamespace = currentNamespace.Namespace; currentNamespace = currentNamespace.Namespace;
@ -280,8 +259,7 @@ namespace CppSharp.AST
private static string GetDeclName(Declaration decl, string name) private static string GetDeclName(Declaration decl, string name)
{ {
var specialization = decl as ClassTemplateSpecialization; if (decl is ClassTemplateSpecialization specialization)
if (specialization != null)
return string.Format("{0}<{1}>", name, return string.Format("{0}<{1}>", name,
string.Join(", ", specialization.Arguments.Select( string.Join(", ", specialization.Arguments.Select(
a => a.Type.Type == null ? string.Empty : a.Type.ToString()))); a => a.Type.Type == null ? string.Empty : a.Type.ToString())));

70
src/AST/DeclarationsList.cs

@ -23,7 +23,7 @@ namespace CppSharp.AST
public void AddRange(IEnumerable<Declaration> declarations) public void AddRange(IEnumerable<Declaration> declarations)
{ {
foreach (Declaration declaration in declarations) foreach (var declaration in declarations)
{ {
Add(declaration); Add(declaration);
} }
@ -31,8 +31,9 @@ namespace CppSharp.AST
protected override void InsertItem(int index, Declaration item) protected override void InsertItem(int index, Declaration item)
{ {
Kind kind = GetKind(item); var kind = GetKind(item);
int offset = GetOffset(kind); var offset = GetOffset(kind);
// USR null means an artificial declaration, add at the end // USR null means an artificial declaration, add at the end
if (item.USR == null) if (item.USR == null)
{ {
@ -40,10 +41,11 @@ namespace CppSharp.AST
} }
else else
{ {
int i = BinarySearch(GetStart(kind), offset, item); var i = BinarySearch(GetStart(kind), offset, item);
base.InsertItem(i, item); base.InsertItem(i, item);
} }
for (Kind i = kind; i <= Kind.Event; i++)
for (var i = kind; i <= Kind.Event; i++)
{ {
if (offsets.ContainsKey(i)) if (offsets.ContainsKey(i))
{ {
@ -55,7 +57,7 @@ namespace CppSharp.AST
protected override void RemoveItem(int index) protected override void RemoveItem(int index)
{ {
base.RemoveItem(index); base.RemoveItem(index);
for (Kind i = Kind.Namespace; i <= Kind.Event; i++) for (var i = Kind.Namespace; i <= Kind.Event; i++)
{ {
if (offsets.ContainsKey(i) && index < offsets[i]) if (offsets.ContainsKey(i) && index < offsets[i])
{ {
@ -64,14 +66,16 @@ namespace CppSharp.AST
} }
} }
private IEnumerable<T> OfType<T>(Kind kind) where T : Declaration private IEnumerable<T> OfType<T>(Kind kind) where T : Declaration
{ {
if (!offsets.ContainsKey(kind)) if (!offsets.ContainsKey(kind))
{ {
yield break; yield break;
} }
int offset = offsets[kind];
for (int i = GetStart(kind); i < offset; i++) var offset = offsets[kind];
for (var i = GetStart(kind); i < offset; i++)
{ {
yield return (T) this[i]; yield return (T) this[i];
} }
@ -79,47 +83,41 @@ namespace CppSharp.AST
private static Kind GetKind(Declaration item) private static Kind GetKind(Declaration item)
{ {
if (item is Namespace) return item switch
return Kind.Namespace; {
if (item is Enumeration) Namespace _ => Kind.Namespace,
return Kind.Enum; Enumeration _ => Kind.Enum,
if (item is Function) Function _ => Kind.Function,
return Kind.Function; Class _ => Kind.Class,
if (item is Class) Template _ => Kind.Template,
return Kind.Class; TypedefNameDecl _ => Kind.Typedef,
if (item is Template) Variable _ => Kind.Variable,
return Kind.Template; Friend _ => Kind.Friend,
if (item is TypedefNameDecl) Event _ => Kind.Event,
return Kind.Typedef; _ => throw new System.ArgumentOutOfRangeException(nameof(item), "Unsupported type of declaration.")
if (item is Variable) };
return Kind.Variable;
if (item is Friend)
return Kind.Friend;
if (item is Event)
return Kind.Event;
throw new System.ArgumentOutOfRangeException(nameof(item),
"Unsupported type of declaration.");
} }
private int GetOffset(Kind kind) private int GetOffset(Kind kind)
{ {
if (!offsets.ContainsKey(kind)) if (offsets.ContainsKey(kind))
return offsets[kind];
for (var i = kind - 1; i >= Kind.Namespace; i--)
{ {
for (Kind i = kind - 1; i >= Kind.Namespace; i--) if (offsets.ContainsKey(i))
{ {
if (offsets.ContainsKey(i)) return offsets[kind] = offsets[i];
{
return offsets[kind] = offsets[i];
}
} }
offsets[kind] = 0;
} }
offsets[kind] = 0;
return offsets[kind]; return offsets[kind];
} }
private int GetStart(Kind kind) private int GetStart(Kind kind)
{ {
for (Kind i = kind - 1; i >= Kind.Namespace; i--) for (var i = kind - 1; i >= Kind.Namespace; i--)
{ {
if (offsets.ContainsKey(i)) if (offsets.ContainsKey(i))
{ {

15
src/AST/Function.cs

@ -111,10 +111,7 @@ namespace CppSharp.AST
/// It's false in Clang to begin with. I tried fixing it to no avail. /// It's false in Clang to begin with. I tried fixing it to no avail.
/// I don't have any more time at the moment. /// I don't have any more time at the moment.
/// </summary> /// </summary>
public bool IsConst public bool IsConst => DebugText.StartsWith("const ", System.StringComparison.Ordinal);
{
get { return DebugText.StartsWith("const ", System.StringComparison.Ordinal); }
}
ExpressionObsolete defaultArgument; ExpressionObsolete defaultArgument;
private Stmt defaultValue; private Stmt defaultValue;
@ -193,8 +190,10 @@ namespace CppSharp.AST
FunctionType = function.FunctionType; FunctionType = function.FunctionType;
if (function.SpecializationInfo != null) if (function.SpecializationInfo != null)
{ {
SpecializationInfo = new FunctionTemplateSpecialization(function.SpecializationInfo); SpecializationInfo = new FunctionTemplateSpecialization(function.SpecializationInfo)
SpecializationInfo.SpecializedFunction = function; {
SpecializedFunction = function
};
} }
} }
@ -279,8 +278,8 @@ namespace CppSharp.AST
public Type Type => ReturnType.Type; public Type Type => ReturnType.Type;
public QualifiedType QualifiedType public QualifiedType QualifiedType
{ {
get { return ReturnType; } get => ReturnType;
set { ReturnType = value; } set => ReturnType = value;
} }
} }
} }

2
src/AST/Method.cs

@ -196,7 +196,7 @@ namespace CppSharp.AST
public override string ToString() public override string ToString()
{ {
return DebugText; return !string.IsNullOrEmpty(DebugText) ? DebugText : Name;
} }
} }
} }

54
src/AST/Template.cs

@ -35,7 +35,7 @@ namespace CppSharp.AST
} }
/// <summary> /// <summary>
/// Represents a template parameter /// Represents a type template parameter.
/// </summary> /// </summary>
public class TypeTemplateParameter : Declaration public class TypeTemplateParameter : Declaration
{ {
@ -70,7 +70,7 @@ namespace CppSharp.AST
} }
/// <summary> /// <summary>
/// Represents a hard-coded template parameter /// Represents a non-type template parameter.
/// </summary> /// </summary>
public class NonTypeTemplateParameter : Declaration public class NonTypeTemplateParameter : Declaration
{ {
@ -139,12 +139,7 @@ namespace CppSharp.AST
// Name of the declaration. // Name of the declaration.
public override string Name public override string Name
{ {
get get => TemplatedDecl != null ? TemplatedDecl.Name : base.Name;
{
if (TemplatedDecl != null)
return TemplatedDecl.Name;
return base.Name;
}
set set
{ {
base.Name = value; base.Name = value;
@ -166,7 +161,7 @@ namespace CppSharp.AST
public Declaration TemplatedDecl; public Declaration TemplatedDecl;
public List<Declaration> Parameters; public readonly List<Declaration> Parameters;
public override string ToString() public override string ToString()
{ {
@ -190,12 +185,9 @@ namespace CppSharp.AST
/// </summary> /// </summary>
public class ClassTemplate : Template public class ClassTemplate : Template
{ {
public List<ClassTemplateSpecialization> Specializations; public readonly List<ClassTemplateSpecialization> Specializations;
public Class TemplatedClass public Class TemplatedClass => TemplatedDecl as Class;
{
get { return TemplatedDecl as Class; }
}
public ClassTemplate() public ClassTemplate()
{ {
@ -215,12 +207,7 @@ namespace CppSharp.AST
public override string Name public override string Name
{ {
get get => TemplatedDecl != null ? TemplatedClass.Name : base.Name;
{
if(TemplatedDecl != null)
return TemplatedClass.Name;
return base.Name;
}
set set
{ {
if(TemplatedDecl != null) if(TemplatedDecl != null)
@ -232,12 +219,7 @@ namespace CppSharp.AST
public override string OriginalName public override string OriginalName
{ {
get get => TemplatedDecl != null ? TemplatedClass.OriginalName : base.OriginalName;
{
if(TemplatedDecl != null)
return TemplatedClass.OriginalName;
return base.OriginalName;
}
set set
{ {
if(TemplatedDecl != null) if(TemplatedDecl != null)
@ -293,7 +275,7 @@ namespace CppSharp.AST
{ {
public ClassTemplate TemplatedDecl; public ClassTemplate TemplatedDecl;
public List<TemplateArgument> Arguments; public readonly List<TemplateArgument> Arguments;
public TemplateSpecializationKind SpecializationKind; public TemplateSpecializationKind SpecializationKind;
@ -327,7 +309,7 @@ namespace CppSharp.AST
/// </summary> /// </summary>
public class FunctionTemplate : Template public class FunctionTemplate : Template
{ {
public List<FunctionTemplateSpecialization> Specializations; public readonly List<FunctionTemplateSpecialization> Specializations;
public FunctionTemplate() public FunctionTemplate()
{ {
@ -340,10 +322,7 @@ namespace CppSharp.AST
Specializations = new List<FunctionTemplateSpecialization>(); Specializations = new List<FunctionTemplateSpecialization>();
} }
public Function TemplatedFunction public Function TemplatedFunction => TemplatedDecl as Function;
{
get { return TemplatedDecl as Function; }
}
public override T Visit<T>(IDeclVisitor<T> visitor) public override T Visit<T>(IDeclVisitor<T> visitor)
{ {
@ -359,7 +338,7 @@ namespace CppSharp.AST
{ {
public FunctionTemplate Template; public FunctionTemplate Template;
public List<TemplateArgument> Arguments; public readonly List<TemplateArgument> Arguments;
public Function SpecializedFunction; public Function SpecializedFunction;
@ -390,12 +369,9 @@ namespace CppSharp.AST
/// </summary> /// </summary>
public class VarTemplate : Template public class VarTemplate : Template
{ {
public List<VarTemplateSpecialization> Specializations; public readonly List<VarTemplateSpecialization> Specializations;
public Variable TemplatedVariable public Variable TemplatedVariable => TemplatedDecl as Variable;
{
get { return TemplatedDecl as Variable; }
}
public VarTemplate() public VarTemplate()
{ {
@ -421,7 +397,7 @@ namespace CppSharp.AST
{ {
public VarTemplate TemplatedDecl; public VarTemplate TemplatedDecl;
public List<TemplateArgument> Arguments; public readonly List<TemplateArgument> Arguments;
public TemplateSpecializationKind SpecializationKind; public TemplateSpecializationKind SpecializationKind;

14
src/AST/Type.cs

@ -191,10 +191,7 @@ namespace CppSharp.AST
Size = type.Size; Size = type.Size;
} }
public Type Type public Type Type => QualifiedType.Type;
{
get { return 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 TypeQualifiers())
{ {
@ -690,12 +687,10 @@ namespace CppSharp.AST
{ {
var finalType = Desugared.Type.GetFinalPointee() ?? Desugared.Type; var finalType = Desugared.Type.GetFinalPointee() ?? Desugared.Type;
var tagType = finalType as TagType; if (finalType is TagType tagType)
if (tagType != null)
return tagType.Declaration; return tagType.Declaration;
var injectedClassNameType = finalType as InjectedClassNameType; if (!(finalType is InjectedClassNameType injectedClassNameType))
if (injectedClassNameType == null)
return null; return null;
var injectedSpecializationType = (TemplateSpecializationType) var injectedSpecializationType = (TemplateSpecializationType)
@ -801,11 +796,14 @@ namespace CppSharp.AST
: base(type) : base(type)
{ {
if (type.Parameter != null) if (type.Parameter != null)
{
Parameter = new TypeTemplateParameter Parameter = new TypeTemplateParameter
{ {
Constraint = type.Parameter.Constraint, Constraint = type.Parameter.Constraint,
Name = type.Parameter.Name Name = type.Parameter.Name
}; };
}
Depth = type.Depth; Depth = type.Depth;
Index = type.Index; Index = type.Index;
IsParameterPack = type.IsParameterPack; IsParameterPack = type.IsParameterPack;

38
src/AST/TypeExtensions.cs

@ -121,37 +121,33 @@
t = t.Desugar(); t = t.Desugar();
TagType tagType = null; TagType tagType = null;
var type = t as TemplateSpecializationType; if (t is TemplateSpecializationType type)
if (type != null)
{ {
if (type.IsDependent) if (type.IsDependent)
{ {
if (type.Template is TypeAliasTemplate) switch (type.Template)
{
type.Desugared.Type.TryGetDeclaration(out decl, value);
decl = decl as T;
return decl != null;
}
var classTemplate = type.Template as ClassTemplate;
if (classTemplate != null)
{ {
var templatedClass = classTemplate.TemplatedClass; case TypeAliasTemplate _:
decl = templatedClass.CompleteDeclaration == null type.Desugared.Type.TryGetDeclaration(out decl, value);
? templatedClass as T return decl != null;
: (T) templatedClass.CompleteDeclaration; case ClassTemplate classTemplate:
if (decl != null)
{ {
var templatedClass = classTemplate.TemplatedClass;
decl = templatedClass.CompleteDeclaration == null
? templatedClass as T
: (T) templatedClass.CompleteDeclaration;
if (decl == null)
return false;
if (value != null) if (value != null)
type.Template = new ClassTemplate { TemplatedDecl = value }; type.Template = new ClassTemplate { TemplatedDecl = value };
return true; return true;
} }
return false; case TemplateTemplateParameter templateTemplateParameter:
return (decl = templateTemplateParameter.TemplatedDecl as T) != null;
} }
var templateTemplateParameter = type.Template as TemplateTemplateParameter;
if (templateTemplateParameter != null)
return (decl = templateTemplateParameter.TemplatedDecl as T) != null;
} }
tagType = (type.Desugared.Type.GetFinalPointee() ?? type.Desugared.Type) as TagType; tagType = (type.Desugared.Type.GetFinalPointee() ?? type.Desugared.Type) as TagType;
} }

25
src/Generator/AST/Utils.cs

@ -11,8 +11,8 @@ namespace CppSharp.AST
{ {
if (!function.IsGenerated) return true; if (!function.IsGenerated) return true;
if (function is Method) if (function is Method method)
return CheckIgnoreMethod(function as Method); return CheckIgnoreMethod(method);
return false; return false;
} }
@ -127,9 +127,8 @@ namespace CppSharp.AST
private static bool UsesAdditionalTypeParam(Method method) private static bool UsesAdditionalTypeParam(Method method)
{ {
var specialization = method.Namespace as ClassTemplateSpecialization;
Class template; Class template;
if (specialization != null) if (method.Namespace is ClassTemplateSpecialization specialization)
template = specialization.TemplatedDecl.TemplatedClass; template = specialization.TemplatedDecl.TemplatedClass;
else else
{ {
@ -143,8 +142,7 @@ namespace CppSharp.AST
if (!p.IsDependent) if (!p.IsDependent)
return false; return false;
var desugared = p.Type.Desugar(); var desugared = p.Type.Desugar();
var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar() var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar() as TemplateParameterType;
as TemplateParameterType;
return finalType != null && !typeParams.Contains(finalType.Parameter.Name); return finalType != null && !typeParams.Contains(finalType.Parameter.Name);
}); });
} }
@ -165,8 +163,7 @@ namespace CppSharp.AST
ITypeMapDatabase typeMaps, bool internalOnly, Type type, ITypeMapDatabase typeMaps, bool internalOnly, Type type,
ClassTemplateSpecialization specialization) ClassTemplateSpecialization specialization)
{ {
TypeMap typeMap; typeMaps.FindTypeMap(type, out var typeMap);
typeMaps.FindTypeMap(type, out typeMap);
return (!internalOnly && (((specialization.Ignore || return (!internalOnly && (((specialization.Ignore ||
specialization.TemplatedDecl.TemplatedClass.Ignore) && typeMap == null) || specialization.TemplatedDecl.TemplatedClass.Ignore) && typeMap == null) ||
@ -179,8 +176,7 @@ namespace CppSharp.AST
private static ClassTemplateSpecialization GetParentSpecialization(Type type) private static ClassTemplateSpecialization GetParentSpecialization(Type type)
{ {
Declaration declaration; if (type.TryGetDeclaration(out Declaration declaration))
if (type.TryGetDeclaration(out declaration))
{ {
ClassTemplateSpecialization specialization = null; ClassTemplateSpecialization specialization = null;
do do
@ -207,8 +203,7 @@ namespace CppSharp.AST
public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps, Type type) public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps, Type type)
{ {
TypeMap typeMap; if (!typeMaps.FindTypeMap(type, out var typeMap))
if (!typeMaps.FindTypeMap(type, out typeMap))
return false; return false;
var typePrinterContext = new TypePrinterContext { Type = type }; var typePrinterContext = new TypePrinterContext { Type = type };
@ -258,16 +253,14 @@ namespace CppSharp.AST
public static bool IsBuiltinOperator(CXXOperatorKind kind) public static bool IsBuiltinOperator(CXXOperatorKind kind)
{ {
bool isBuiltin; GetOperatorIdentifier(kind, out var isBuiltin);
GetOperatorIdentifier(kind, out isBuiltin);
return isBuiltin; return isBuiltin;
} }
public static string GetOperatorIdentifier(CXXOperatorKind kind) public static string GetOperatorIdentifier(CXXOperatorKind kind)
{ {
bool isBuiltin; return GetOperatorIdentifier(kind, out _);
return GetOperatorIdentifier(kind, out isBuiltin);
} }
public static string GetOperatorIdentifier(CXXOperatorKind kind, public static string GetOperatorIdentifier(CXXOperatorKind kind,

13
src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs

@ -30,8 +30,7 @@ namespace CppSharp.Generators.CSharp
parameter.DefaultArgument.Class == StatementClass.BinaryOperator)) parameter.DefaultArgument.Class == StatementClass.BinaryOperator))
return $"({desugared.Visit(typePrinter)}) {expression}"; return $"({desugared.Visit(typePrinter)}) {expression}";
var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar(); var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar();
Class @class; if (finalType.TryGetClass(out var @class) && @class.IsInterface)
if (finalType.TryGetClass(out @class) && @class.IsInterface)
return $@"({@class.Visit(typePrinter)}) ({ return $@"({@class.Visit(typePrinter)}) ({
@class.OriginalClass.Visit(typePrinter)}) {expression}"; @class.OriginalClass.Visit(typePrinter)}) {expression}";
return expression; return expression;
@ -46,17 +45,13 @@ namespace CppSharp.Generators.CSharp
switch (callExpr.Declaration.GenerationKind) switch (callExpr.Declaration.GenerationKind)
{ {
case GenerationKind.Generate: case GenerationKind.Generate:
return string.Format("{0}.{1}({2})", var args = string.Join(", ", callExpr.Arguments.Select(VisitExpression));
typePrinter.VisitDeclaration(callExpr.Declaration.Namespace), return $"{typePrinter.VisitDeclaration(callExpr.Declaration.Namespace)}.{callExpr.Declaration.Name}({args})";
callExpr.Declaration.Name,
string.Join(", ", callExpr.Arguments.Select(VisitExpression)));
case GenerationKind.Internal: case GenerationKind.Internal:
// a non-ctor can only be internal if it's been converted to a property // a non-ctor can only be internal if it's been converted to a property
var property = ((Class) callExpr.Declaration.Namespace).Properties.First( var property = ((Class) callExpr.Declaration.Namespace).Properties.First(
p => p.GetMethod == callExpr.Declaration); p => p.GetMethod == callExpr.Declaration);
return string.Format("{0}.{1}", return $"{typePrinter.VisitDeclaration(callExpr.Declaration.Namespace)}.{property.Name}";
typePrinter.VisitDeclaration(callExpr.Declaration.Namespace),
property.Name);
default: default:
return expr.String; return expr.String;
} }

3
src/Generator/Passes/CheckAmbiguousFunctions.cs

@ -62,8 +62,7 @@ namespace CppSharp.Passes
} }
if (function.IsAmbiguous) if (function.IsAmbiguous)
Diagnostics.Debug("Found ambiguous overload: {0}", Diagnostics.Message($"Found ambiguous overload: {function.QualifiedOriginalName}");
function.QualifiedOriginalName);
return true; return true;
} }

20
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -23,14 +23,12 @@ namespace CppSharp.Passes
public bool UpdateName(Declaration decl) public bool UpdateName(Declaration decl)
{ {
var function = decl as Function; if (decl is Function function)
if (function != null)
{ {
return UpdateName(function); return UpdateName(function);
} }
var property = decl as Property; var isIndexer = decl is Property property && property.Parameters.Count > 0;
var isIndexer = property != null && property.Parameters.Count > 0;
if (isIndexer) if (isIndexer)
{ {
return false; return false;
@ -131,10 +129,8 @@ namespace CppSharp.Passes
private static bool CheckForSpecializations(Type leftPointee, Type rightPointee) private static bool CheckForSpecializations(Type leftPointee, Type rightPointee)
{ {
Class leftClass; if (!leftPointee.TryGetDeclaration(out Class leftClass) ||
Class rightClass; !rightPointee.TryGetDeclaration(out Class rightClass))
if (!leftPointee.TryGetDeclaration(out leftClass) ||
!rightPointee.TryGetDeclaration(out rightClass))
return false; return false;
var leftSpecialization = leftClass as ClassTemplateSpecialization ?? var leftSpecialization = leftClass as ClassTemplateSpecialization ??
@ -206,10 +202,7 @@ namespace CppSharp.Passes
switch (kind) switch (kind)
{ {
case GeneratorKind.C: case GeneratorKind.C:
typePrinter = new CppTypePrinter(Context) typePrinter = new CppTypePrinter(Context) { PrintFlavorKind = CppTypePrintFlavorKind.C };
{
PrintFlavorKind = CppTypePrintFlavorKind.C
};
break; break;
case GeneratorKind.CPlusPlus: case GeneratorKind.CPlusPlus:
case GeneratorKind.QuickJS: case GeneratorKind.QuickJS:
@ -310,8 +303,7 @@ namespace CppSharp.Passes
return; return;
var fullName = decl.QualifiedName; var fullName = decl.QualifiedName;
var function = decl as Function; if (decl is Function function)
if (function != null)
fullName = DeclarationName.FixSignatureForConversions(function, fullName); fullName = DeclarationName.FixSignatureForConversions(function, fullName);
// If the name is not yet on the map, then add it. // If the name is not yet on the map, then add it.

8
src/Parser/ASTConverter.cs

@ -2185,9 +2185,11 @@ namespace CppSharp
protected override AST.Comment VisitInlineCommandComment(InlineCommandComment comment) protected override AST.Comment VisitInlineCommandComment(InlineCommandComment comment)
{ {
var inlineCommandComment = new AST.InlineCommandComment(); var inlineCommandComment = new AST.InlineCommandComment
inlineCommandComment.HasTrailingNewline = comment.HasTrailingNewline; {
inlineCommandComment.CommandId = comment.CommandId; HasTrailingNewline = comment.HasTrailingNewline,
CommandId = comment.CommandId
};
switch (comment.CommentRenderKind) switch (comment.CommentRenderKind)
{ {
case InlineCommandComment.RenderKind.RenderNormal: case InlineCommandComment.RenderKind.RenderNormal:

Loading…
Cancel
Save