Browse Source

Minor code cleanups in CLITypeReference.cs.

pull/1547/head
Joao Matos 5 years ago committed by João Matos
parent
commit
f58ca3b62f
  1. 37
      src/Generator/AST/ASTRecord.cs
  2. 2
      src/Generator/Generators/CLI/CLITypeReferences.cs

37
src/Generator/AST/ASTRecord.cs

@ -17,10 +17,7 @@ namespace CppSharp.Generators.AST
{ {
@out = default(T); @out = default(T);
if (Parent == null) var v = Parent?.Object;
return false;
var v = Parent.Object;
if (!(v is T)) if (!(v is T))
return false; return false;
@ -45,8 +42,7 @@ namespace CppSharp.Generators.AST
{ {
ancestors.Push(this); ancestors.Push(this);
T value; if (GetParent(out T value))
if (GetParent(out value))
{ {
ancestors.Push(value); ancestors.Push(value);
return true; return true;
@ -59,10 +55,7 @@ namespace CppSharp.Generators.AST
public class ASTRecord<T> : ASTRecord public class ASTRecord<T> : ASTRecord
{ {
public T Value public T Value => (T) Object;
{
get { return (T) Object; }
}
public override string ToString() public override string ToString()
{ {
@ -130,12 +123,10 @@ namespace CppSharp.Generators.AST
{ {
public static bool IsBaseClass(this ASTRecord record) public static bool IsBaseClass(this ASTRecord record)
{ {
Class decl; if (!record.GetParent(out Class decl))
if (!record.GetParent(out decl))
return false; return false;
var recordDecl = record.Object as Class; return record.Object is Class recordDecl && recordDecl == decl.BaseClass;
return recordDecl != null && recordDecl == decl.BaseClass;
} }
public static bool IsFieldValueType(this ASTRecord record) public static bool IsFieldValueType(this ASTRecord record)
@ -145,25 +136,22 @@ namespace CppSharp.Generators.AST
return false; return false;
var field = (Field)ancestors.Pop(); var field = (Field)ancestors.Pop();
return field.Type.Desugar().TryGetClass(out var decl) && decl.IsValueType;
Class decl;
return field.Type.Desugar().TryGetClass(out decl) && decl.IsValueType;
} }
public static bool IsEnumNestedInClass(this ASTRecord<Declaration> record) public static bool IsEnumNestedInClass(this ASTRecord<Declaration> record)
{ {
Enumeration @enum = record.Value as Enumeration; var @enum = record.Value as Enumeration;
var typedDecl = record.Value as ITypedDecl; var typedDecl = record.Value as ITypedDecl;
if (@enum != null if (@enum != null || (typedDecl?.Type?.TryGetEnum(out @enum)).GetValueOrDefault())
|| (typedDecl?.Type?.TryGetEnum(out @enum)).GetValueOrDefault())
{ {
return @enum.Namespace is Class; return @enum?.Namespace is Class;
} }
return false; return false;
} }
public static bool IsClassReturn(this ASTRecord record) public static bool FunctionReturnsClassByValue(this ASTRecord record)
{ {
var ancestors = new Stack<object>(); var ancestors = new Stack<object>();
if(!record.GetAncestors<Function>(ref ancestors)) if(!record.GetAncestors<Function>(ref ancestors))
@ -177,8 +165,7 @@ namespace CppSharp.Generators.AST
public static bool IsDelegate(this ASTRecord record) public static bool IsDelegate(this ASTRecord record)
{ {
var typedef = record.Object as TypedefDecl; return record.Object is TypedefDecl typedef && typedef.Type.GetPointee() is FunctionType;
return typedef != null && typedef.Type.GetPointee() is FunctionType;
} }
} }
@ -223,7 +210,7 @@ namespace CppSharp.Generators.AST
return false; return false;
} }
public bool ShouldVisitChilds(Declaration decl) private bool ShouldVisitChilds(Declaration decl)
{ {
if (decl == translationUnit) if (decl == translationUnit)
return true; return true;

2
src/Generator/Generators/CLI/CLITypeReferences.cs

@ -164,7 +164,7 @@ namespace CppSharp.Generators.CLI
return false; return false;
return record.IsBaseClass() || record.IsFieldValueType() || record.IsDelegate() return record.IsBaseClass() || record.IsFieldValueType() || record.IsDelegate()
|| record.IsEnumNestedInClass() || record.IsClassReturn(); || record.IsEnumNestedInClass() || record.FunctionReturnsClassByValue();
} }
public override bool VisitDeclaration(Declaration decl) public override bool VisitDeclaration(Declaration decl)

Loading…
Cancel
Save