Browse Source

Moved ignored declaration and type checking code from ResolveIncompleteDeclsPass to its own pass.

pull/1/head
triton 12 years ago
parent
commit
c83a87dbd4
  1. 4
      src/Generator/Driver.cs
  2. 216
      src/Generator/Passes/CheckIgnoredDecls.cs
  3. 160
      src/Generator/Passes/ResolveIncompleteDeclsPass.cs

4
src/Generator/Driver.cs

@ -138,7 +138,9 @@ namespace CppSharp
var passes = new PassBuilder(this); var passes = new PassBuilder(this);
passes.CleanUnit(Options); passes.CleanUnit(Options);
passes.SortDeclarations(); passes.SortDeclarations();
passes.ResolveIncompleteDecls(TypeDatabase); passes.ResolveIncompleteDecls();
passes.CheckIgnoredDecls();
passes.CheckTypeReferences(); passes.CheckTypeReferences();
passes.CheckFlagEnums(); passes.CheckFlagEnums();
passes.CheckAmbiguousOverloads(); passes.CheckAmbiguousOverloads();

216
src/Generator/Passes/CheckIgnoredDecls.cs

@ -0,0 +1,216 @@
using System;
namespace CppSharp.Passes
{
public class CheckIgnoredDeclsPass : TranslationUnitPass
{
public CheckIgnoredDeclsPass()
{
}
public override bool VisitDeclaration(Declaration decl)
{
if (decl.IsDependent)
{
decl.ExplicityIgnored = true;
Console.WriteLine("Decl '{0}' was ignored due to dependent context",
decl.Name);
}
return true;
}
public override bool VisitFieldDecl(Field field)
{
if (!VisitDeclaration(field))
return false;
var type = field.Type;
string msg;
if (!HasInvalidType(type, out msg))
return false;
field.ExplicityIgnored = true;
Console.WriteLine("Field '{0}' was ignored due to {1} type",
field.Name, msg);
return true;
}
public override bool VisitFunctionDecl(Function function)
{
if (!VisitDeclaration(function))
return false;
var ret = function.ReturnType;
string msg;
if (HasInvalidType(ret.Type, out msg))
{
function.ExplicityIgnored = true;
Console.WriteLine("Function '{0}' was ignored due to {1} return decl",
function.Name, msg);
return false;
}
foreach (var param in function.Parameters)
{
if (HasInvalidDecl(param, out msg))
{
function.ExplicityIgnored = true;
Console.WriteLine("Function '{0}' was ignored due to {1} param",
function.Name, msg);
return false;
}
if (HasInvalidType(param.Type, out msg))
{
function.ExplicityIgnored = true;
Console.WriteLine("Function '{0}' was ignored due to {1} param",
function.Name, msg);
return false;
}
}
return true;
}
public override bool VisitTypedefDecl(TypedefDecl typedef)
{
if (!VisitDeclaration(typedef))
return false;
string msg;
if (HasInvalidType(typedef.Type, out msg))
{
typedef.ExplicityIgnored = true;
Console.WriteLine("Typedef '{0}' was ignored due to {1} type",
typedef.Name, msg);
return false;
}
return true;
}
public override bool VisitProperty(Property property)
{
if (!VisitDeclaration(property))
return false;
string msg;
if (HasInvalidDecl(property, out msg))
{
property.ExplicityIgnored = true;
Console.WriteLine("Property '{0}' was ignored due to {1} decl",
property.Name, msg);
return false;
}
if (HasInvalidType(property.Type, out msg))
{
property.ExplicityIgnored = true;
Console.WriteLine("Property '{0}' was ignored due to {1} type",
property.Name, msg);
return false;
}
return true;
}
#region Helpers
/// <remarks>
/// Checks if a given type is invalid, which can happen for a number of
/// reasons: incomplete definitions, being explicitly ignored, or also
/// by being a type we do not know how to handle.
/// </remarks>
bool HasInvalidType(Type type, out string msg)
{
if (type == null)
{
msg = "null";
return true;
}
if (!IsTypeComplete(type))
{
msg = "incomplete";
return true;
}
if (IsTypeIgnored(type))
{
msg = "ignored";
return true;
}
msg = null;
return false;
}
bool HasInvalidDecl(Declaration decl, out string msg)
{
if (decl == null)
{
msg = "null";
return true;
}
if (!IsDeclComplete(decl))
{
msg = "incomplete";
return true;
}
if (IsDeclIgnored(decl))
{
msg = "ignored";
return true;
}
msg = null;
return false;
}
static bool IsTypeComplete(Type type)
{
var checker = new TypeCompletionChecker();
return type.Visit(checker);
}
static bool IsDeclComplete(Declaration decl)
{
var checker = new TypeCompletionChecker();
return decl.Visit(checker);
}
bool IsTypeIgnored(Type type)
{
var checker = new TypeIgnoreChecker(Driver.TypeDatabase);
type.Visit(checker);
return checker.IsIgnored;
}
bool IsDeclIgnored(Declaration decl)
{
var checker = new TypeIgnoreChecker(Driver.TypeDatabase);
decl.Visit(checker);
return checker.IsIgnored;
}
#endregion
}
public static class CheckIgnoredDeclsPassExtensions
{
public static void CheckIgnoredDecls(this PassBuilder builder)
{
var pass = new CheckIgnoredDeclsPass();
builder.AddPass(pass);
}
}
}

160
src/Generator/Passes/ResolveIncompleteDeclsPass.cs

@ -5,11 +5,8 @@ namespace CppSharp.Passes
{ {
public class ResolveIncompleteDeclsPass : TranslationUnitPass public class ResolveIncompleteDeclsPass : TranslationUnitPass
{ {
private readonly ITypeMapDatabase typeMapDatabase; public ResolveIncompleteDeclsPass()
public ResolveIncompleteDeclsPass(ITypeMapDatabase database)
{ {
typeMapDatabase = database;
} }
public override bool VisitClassDecl(Class @class) public override bool VisitClassDecl(Class @class)
@ -32,164 +29,13 @@ namespace CppSharp.Passes
return base.VisitClassDecl(@class); return base.VisitClassDecl(@class);
} }
public override bool VisitFieldDecl(Field field)
{
var type = field.Type;
string msg;
if (!HasInvalidType(type, out msg))
return false;
field.ExplicityIgnored = true;
Console.WriteLine("Field '{0}' was ignored due to {1} type",
field.Name, msg);
return true;
}
public override bool VisitFunctionDecl(Function function)
{
var ret = function.ReturnType;
string msg;
if (HasInvalidType(ret.Type, out msg))
{
function.ExplicityIgnored = true;
Console.WriteLine("Function '{0}' was ignored due to {1} return decl",
function.Name, msg);
return false;
}
foreach (var param in function.Parameters)
{
if (HasInvalidDecl(param, out msg))
{
function.ExplicityIgnored = true;
Console.WriteLine("Function '{0}' was ignored due to {1} param",
function.Name, msg);
return false;
}
if (HasInvalidType(param.Type, out msg))
{
function.ExplicityIgnored = true;
Console.WriteLine("Function '{0}' was ignored due to {1} param",
function.Name, msg);
return false;
}
}
return true;
}
public override bool VisitTypedefDecl(TypedefDecl typedef)
{
string msg;
if (HasInvalidType(typedef.Type, out msg))
{
typedef.ExplicityIgnored = true;
Console.WriteLine("Typedef '{0}' was ignored due to {1} type",
typedef.Name, msg);
return false;
}
return true;
}
#region Helpers
/// <remarks>
/// Checks if a given type is invalid, which can happen for a number of
/// reasons: incomplete definitions, being explicitly ignored, or also
/// by being a type we do not know how to handle.
/// </remarks>
bool HasInvalidType(Type type, out string msg)
{
if (type == null)
{
msg = "null";
return true;
}
if (!IsTypeComplete(type))
{
msg = "incomplete";
return true;
}
if (IsTypeIgnored(type))
{
msg = "ignored";
return true;
}
msg = null;
return false;
}
bool HasInvalidDecl(Declaration decl, out string msg)
{
if (decl == null)
{
msg = "null";
return true;
}
if (!IsDeclComplete(decl))
{
msg = "incomplete";
return true;
}
if (IsDeclIgnored(decl))
{
msg = "ignored";
return true;
}
msg = null;
return false;
}
static bool IsTypeComplete(Type type)
{
var checker = new TypeCompletionChecker();
return type.Visit(checker);
}
static bool IsDeclComplete(Declaration decl)
{
var checker = new TypeCompletionChecker();
return decl.Visit(checker);
}
bool IsTypeIgnored(Type type)
{
var checker = new TypeIgnoreChecker(typeMapDatabase);
type.Visit(checker);
return checker.IsIgnored;
}
bool IsDeclIgnored(Declaration decl)
{
var checker = new TypeIgnoreChecker(typeMapDatabase);
decl.Visit(checker);
return checker.IsIgnored;
}
#endregion
} }
public static class ResolveIncompleteDeclsExtensions public static class ResolveIncompleteDeclsExtensions
{ {
public static void ResolveIncompleteDecls(this PassBuilder builder, public static void ResolveIncompleteDecls(this PassBuilder builder)
ITypeMapDatabase database)
{ {
var pass = new ResolveIncompleteDeclsPass(database); var pass = new ResolveIncompleteDeclsPass();
builder.AddPass(pass); builder.AddPass(pass);
} }
} }

Loading…
Cancel
Save