Browse Source

Separated IsTagDecl into TryGetClass and TryGetEnum, added IsClass() and IsEnum().

pull/241/head
marcos henrich 11 years ago
parent
commit
de764599fa
  1. 22
      src/AST/Class.cs
  2. 12
      src/AST/ClassExtensions.cs
  3. 2
      src/AST/PropertyExtensions.cs
  4. 45
      src/AST/TypeExtensions.cs
  5. 2
      src/Generator/AST/ASTRecord.cs
  6. 8
      src/Generator/Generators/CLI/CLIMarshal.cs
  7. 2
      src/Generator/Generators/CLI/CLITypePrinter.cs
  8. 4
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  9. 10
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  10. 4
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  11. 5
      src/Generator/Passes/CheckIgnoredDecls.cs
  12. 2
      src/Generator/Passes/ConstructorToConversionOperatorPass.cs
  13. 2
      src/Generator/Passes/FunctionToInstanceMethodPass.cs

22
src/AST/Class.cs

@ -34,25 +34,8 @@ namespace CppSharp.AST @@ -34,25 +34,8 @@ namespace CppSharp.AST
get
{
Class @class;
if (Type.IsTagDecl(out @class))
Type.TryGetClass(out @class);
return @class;
var type = Type.Desugar() as TemplateSpecializationType;
if (type == null)
{
TypedefType typedef;
if (Type.IsPointerTo(out typedef))
{
type = (TemplateSpecializationType) typedef.Desugar();
}
else
{
Type.IsPointerTo(out type);
}
}
var templatedClass = ((ClassTemplate) type.Template).TemplatedClass;
return templatedClass.CompleteDeclaration == null ?
templatedClass : (Class) templatedClass.CompleteDeclaration;
}
}
@ -60,8 +43,7 @@ namespace CppSharp.AST @@ -60,8 +43,7 @@ namespace CppSharp.AST
{
get
{
Class @class;
return Type.IsTagDecl(out @class);
return Type.IsClass();
}
}
}

12
src/AST/ClassExtensions.cs

@ -83,9 +83,9 @@ namespace CppSharp.AST @@ -83,9 +83,9 @@ namespace CppSharp.AST
Property property = c.Properties.FirstOrDefault(m => m.Name == propertyName);
if (property != null)
return property;
Declaration decl;
foreach (var baseClassSpecifier in c.Bases.Where(
b => b.Type.IsTagDecl(out decl) && b.Class.IsDeclared))
b => b.Type.IsClass() && b.Class.IsDeclared))
{
property = baseClassSpecifier.Class.GetPropertyByName(propertyName);
if (property != null)
@ -102,8 +102,8 @@ namespace CppSharp.AST @@ -102,8 +102,8 @@ namespace CppSharp.AST
property = c.Properties.FirstOrDefault(p => p.SetMethod == method);
if (property != null)
return property;
Declaration decl;
foreach (BaseClassSpecifier @base in c.Bases.Where(b => b.Type.IsTagDecl(out decl)))
foreach (BaseClassSpecifier @base in c.Bases.Where(b => b.Type.IsClass()))
{
property = @base.Class.GetPropertyByConstituentMethod(method);
if (property != null)
@ -119,8 +119,8 @@ namespace CppSharp.AST @@ -119,8 +119,8 @@ namespace CppSharp.AST
m => string.Compare(m.Name, methodName, StringComparison.OrdinalIgnoreCase) == 0);
if (method != null)
return method;
Declaration decl;
foreach (BaseClassSpecifier @base in c.Bases.Where(b => b.Type.IsTagDecl(out decl)))
foreach (BaseClassSpecifier @base in c.Bases.Where(b => b.Type.IsClass()))
{
method = @base.Class.GetMethodByName(methodName);
if (method != null)

2
src/AST/PropertyExtensions.cs

@ -14,7 +14,7 @@ namespace CppSharp.AST @@ -14,7 +14,7 @@ namespace CppSharp.AST
type = type ?? p.Field.Type;
Class decl;
return type.IsTagDecl(out decl) && decl.IsValueType;
return type.TryGetClass(out decl) && decl.IsValueType;
}
}
}

45
src/AST/TypeExtensions.cs

@ -105,18 +105,55 @@ @@ -105,18 +105,55 @@
return type != null;
}
public static bool IsTagDecl<T>(this Type t, out T decl) where T : Declaration
public static bool IsClass(this Type t)
{
Class @class;
return t.TryGetClass(out @class);
}
public static bool TryGetClass(this Type t, out Class @class)
{
t = t.Desugar();
var tag = t as TagType;
if (tag != null)
{
@class = tag.Declaration as Class;
return @class != null;
}
var type = t as TemplateSpecializationType;
if (type != null)
{
var templatedClass = ((ClassTemplate)type.Template).TemplatedClass;
@class = templatedClass.CompleteDeclaration == null
? templatedClass
: (Class)templatedClass.CompleteDeclaration;
return @class != null;
}
@class = null;
return false;
}
public static bool IsEnum(this Type t)
{
Enumeration @enum;
return t.TryGetEnum(out @enum);
}
public static bool TryGetEnum(this Type t, out Enumeration @enum)
{
var tag = t.Desugar() as TagType;
if (tag == null)
{
decl = null;
@enum = null;
return false;
}
decl = tag.Declaration as T;
return decl != null;
@enum = tag.Declaration as Enumeration;
return @enum != null;
}
public static Type Desugar(this Type t)

2
src/Generator/AST/ASTRecord.cs

@ -147,7 +147,7 @@ namespace CppSharp.Generators.AST @@ -147,7 +147,7 @@ namespace CppSharp.Generators.AST
var field = (Field)ancestors.Pop();
Class decl;
return field.Type.Desugar().IsTagDecl(out decl) && decl.IsValueType;
return field.Type.Desugar().TryGetClass(out decl) && decl.IsValueType;
}
}

8
src/Generator/Generators/CLI/CLIMarshal.cs

@ -107,7 +107,7 @@ namespace CppSharp.Generators.CLI @@ -107,7 +107,7 @@ namespace CppSharp.Generators.CLI
Context.Driver.TypeDatabase.FindTypeMap(pointee, out typeMap);
Class @class;
if (pointee.IsTagDecl(out @class) && typeMap == null)
if (pointee.TryGetClass(out @class) && typeMap == null)
{
var instance = (pointer.IsReference) ? "&" + Context.ReturnVarName
: Context.ReturnVarName;
@ -426,7 +426,7 @@ namespace CppSharp.Generators.CLI @@ -426,7 +426,7 @@ namespace CppSharp.Generators.CLI
}
Enumeration @enum;
if (pointee.IsTagDecl(out @enum))
if (pointee.TryGetEnum(out @enum))
{
ArgumentPrefix.Write("&");
Context.Return.Write("(::{0})*{1}", @enum.QualifiedOriginalName,
@ -435,7 +435,7 @@ namespace CppSharp.Generators.CLI @@ -435,7 +435,7 @@ namespace CppSharp.Generators.CLI
}
Class @class;
if (pointee.IsTagDecl(out @class) && @class.IsValueType)
if (pointee.TryGetClass(out @class) && @class.IsValueType)
{
if (Context.Function == null)
Context.Return.Write("&");
@ -655,7 +655,7 @@ namespace CppSharp.Generators.CLI @@ -655,7 +655,7 @@ namespace CppSharp.Generators.CLI
Type type;
Class @class;
var isRef = property.Type.IsPointerTo(out type) &&
!(type.IsTagDecl(out @class) && @class.IsValueType) &&
!(type.TryGetClass(out @class) && @class.IsValueType) &&
!type.IsPrimitiveType();
if (isRef)

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

@ -161,7 +161,7 @@ namespace CppSharp.Generators.CLI @@ -161,7 +161,7 @@ namespace CppSharp.Generators.CLI
}
Enumeration @enum;
if (pointee.IsTagDecl(out @enum))
if (pointee.TryGetEnum(out @enum))
{
var typeName = @enum.Visit(this);
return string.Format("{0}*", typeName);

4
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -439,7 +439,7 @@ namespace CppSharp.Generators.CSharp @@ -439,7 +439,7 @@ namespace CppSharp.Generators.CSharp
}
Class @class;
if (pointee.IsTagDecl(out @class) && @class.IsValueType)
if (pointee.TryGetClass(out @class) && @class.IsValueType)
{
if (Context.Parameter.Usage == ParameterUsage.Out)
{
@ -582,7 +582,7 @@ namespace CppSharp.Generators.CSharp @@ -582,7 +582,7 @@ namespace CppSharp.Generators.CSharp
if (type.IsAddress())
{
Class decl;
if (type.IsTagDecl(out decl) && decl.IsValueType)
if (type.TryGetClass(out decl) && decl.IsValueType)
Context.Return.Write("{0}.{1}", param, Helpers.InstanceIdentifier);
else
Context.Return.Write("{0} == ({2}) null ? global::System.IntPtr.Zero : {0}.{1}", param,

10
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -661,7 +661,7 @@ namespace CppSharp.Generators.CSharp @@ -661,7 +661,7 @@ namespace CppSharp.Generators.CSharp
Type type;
Class @class;
var isRef = property.Type.IsPointerTo(out type) &&
!(type.IsTagDecl(out @class) && @class.IsValueType) &&
!(type.TryGetClass(out @class) && @class.IsValueType) &&
!type.IsPrimitiveType();
if (isRef)
@ -771,7 +771,7 @@ namespace CppSharp.Generators.CSharp @@ -771,7 +771,7 @@ namespace CppSharp.Generators.CSharp
// we do not support dependent fields yet, see https://github.com/mono/CppSharp/issues/197
Class @class;
if (field.Type.IsDependent && !field.Type.IsPointer() &&
!(field.Type.IsTagDecl(out @class) && @class.IsUnion))
!(field.Type.TryGetClass(out @class) && @class.IsUnion))
return;
var safeIdentifier = Helpers.SafeIdentifier(field.OriginalName);
@ -2234,7 +2234,7 @@ namespace CppSharp.Generators.CSharp @@ -2234,7 +2234,7 @@ namespace CppSharp.Generators.CSharp
parameter => parameter.Kind == ParameterKind.IndirectReturnType);
Class retClass;
indirectRetType.Type.Desugar().IsTagDecl(out retClass);
indirectRetType.Type.Desugar().TryGetClass(out retClass);
WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"),
QualifiedIdentifier(retClass.OriginalClass ?? retClass));
@ -2337,7 +2337,7 @@ namespace CppSharp.Generators.CSharp @@ -2337,7 +2337,7 @@ namespace CppSharp.Generators.CSharp
pointee = pointee.Desugar();
string @null;
Class @class;
if (pointee.IsTagDecl(out @class) && @class.IsValueType)
if (pointee.TryGetClass(out @class) && @class.IsValueType)
{
@null = string.Format("new {0}()", pointee);
}
@ -2468,7 +2468,7 @@ namespace CppSharp.Generators.CSharp @@ -2468,7 +2468,7 @@ namespace CppSharp.Generators.CSharp
var paramType = param.Type;
Class @class;
if (paramType.Desugar().IsTagDecl(out @class) && @class.IsRefType)
if (paramType.Desugar().TryGetClass(out @class) && @class.IsRefType)
{
WriteLine("{0} = new {1}();", param.Name, paramType);
}

4
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -232,13 +232,13 @@ namespace CppSharp.Generators.CSharp @@ -232,13 +232,13 @@ namespace CppSharp.Generators.CSharp
}
Enumeration @enum;
if (desugared.IsTagDecl(out @enum))
if (desugared.TryGetEnum(out @enum))
{
return @enum.Name + "*";
}
Class @class;
if ((desugared.IsDependent || desugared.IsTagDecl(out @class))
if ((desugared.IsDependent || desugared.TryGetClass(out @class))
&& ContextKind == CSharpTypePrinterContextKind.Native)
{
return "global::System.IntPtr";

5
src/Generator/Passes/CheckIgnoredDecls.cs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using CppSharp.AST;
using System;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Types;
@ -125,7 +126,7 @@ namespace CppSharp.Passes @@ -125,7 +126,7 @@ namespace CppSharp.Passes
if (param.Kind == ParameterKind.IndirectReturnType)
{
Class retClass;
param.Type.Desugar().IsTagDecl(out retClass);
param.Type.Desugar().TryGetClass(out retClass);
if (retClass == null)
{
function.ExplicitlyIgnore();

2
src/Generator/Passes/ConstructorToConversionOperatorPass.cs

@ -27,7 +27,7 @@ namespace CppSharp.Passes @@ -27,7 +27,7 @@ namespace CppSharp.Passes
return false;
var qualifiedPointee = parameterType.QualifiedPointee;
Class castFromClass;
if (!qualifiedPointee.Type.IsTagDecl(out castFromClass))
if (!qualifiedPointee.Type.TryGetClass(out castFromClass))
return false;
var castToClass = method.OriginalNamespace as Class;
if (castToClass == null)

2
src/Generator/Passes/FunctionToInstanceMethodPass.cs

@ -79,7 +79,7 @@ namespace CppSharp.Passes @@ -79,7 +79,7 @@ namespace CppSharp.Passes
return true;
}
return classParam.Type.IsTagDecl(out @class);
return classParam.Type.TryGetClass(out @class);
}
}
}

Loading…
Cancel
Save