diff --git a/src/Generator/Driver.cs b/src/Generator/Driver.cs index 94e43b38..0d6f2af5 100644 --- a/src/Generator/Driver.cs +++ b/src/Generator/Driver.cs @@ -138,7 +138,9 @@ namespace CppSharp var passes = new PassBuilder(this); passes.CleanUnit(Options); passes.SortDeclarations(); - passes.ResolveIncompleteDecls(TypeDatabase); + passes.ResolveIncompleteDecls(); + passes.CheckIgnoredDecls(); + passes.CheckTypeReferences(); passes.CheckFlagEnums(); passes.CheckAmbiguousOverloads(); diff --git a/src/Generator/Passes/CheckIgnoredDecls.cs b/src/Generator/Passes/CheckIgnoredDecls.cs new file mode 100644 index 00000000..3e04747d --- /dev/null +++ b/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 + + /// + /// 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. + /// + 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); + } + } +} diff --git a/src/Generator/Passes/ResolveIncompleteDeclsPass.cs b/src/Generator/Passes/ResolveIncompleteDeclsPass.cs index 7c94dec8..14d637f1 100644 --- a/src/Generator/Passes/ResolveIncompleteDeclsPass.cs +++ b/src/Generator/Passes/ResolveIncompleteDeclsPass.cs @@ -5,11 +5,8 @@ namespace CppSharp.Passes { public class ResolveIncompleteDeclsPass : TranslationUnitPass { - private readonly ITypeMapDatabase typeMapDatabase; - - public ResolveIncompleteDeclsPass(ITypeMapDatabase database) + public ResolveIncompleteDeclsPass() { - typeMapDatabase = database; } public override bool VisitClassDecl(Class @class) @@ -32,164 +29,13 @@ namespace CppSharp.Passes 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 - - /// - /// 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. - /// - 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 void ResolveIncompleteDecls(this PassBuilder builder, - ITypeMapDatabase database) + public static void ResolveIncompleteDecls(this PassBuilder builder) { - var pass = new ResolveIncompleteDeclsPass(database); + var pass = new ResolveIncompleteDeclsPass(); builder.AddPass(pass); } }