Permalink
Browse files

Improved the detection of used template specialisations.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information...
ddobrev committed Jul 14, 2017
1 parent a15aa8f commit 898001fecd0db605d1fba4828516dc08c85392bb
@@ -89,15 +89,11 @@ public static bool CheckIgnoreProperty(Property prop)
ITypeMapDatabase typeMaps, bool internalOnly = false)
{
type = type.Desugar();
type = type.GetFinalPointee() ?? type;
type = (type.GetFinalPointee() ?? type).Desugar();
ClassTemplateSpecialization specialization;
type.TryGetDeclaration(out specialization);
if (specialization == null || specialization.IsExplicitlyGenerated)
{
if (specialization != null)
addSpecialization(specialization);
if (specialization == null)
return;
}
TypeMap typeMap;
typeMaps.FindTypeMap(specialization, out typeMap);
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;
using CppSharp.AST.Extensions;
@@ -43,8 +44,7 @@ public override bool VisitClassDecl(Class @class)
if (!@class.TranslationUnit.IsSystemHeader)
return false;
if (!@class.IsExplicitlyGenerated)
@class.ExplicitlyIgnore();
@class.ExplicitlyIgnore();
if (!@class.IsDependent || @class.Specializations.Count == 0)
return false;
@@ -55,56 +55,67 @@ public override bool VisitClassDecl(Class @class)
case "basic_string":
foreach (var method in @class.Methods.Where(m => !m.IsDestructor && m.OriginalName != "c_str"))
method.ExplicitlyIgnore();
var basicString = @class.Specializations.First(s =>
s.Arguments[0].Type.Type.Desugar().IsPrimitiveType(PrimitiveType.Char));
basicString.GenerationKind = GenerationKind.Generate;
foreach (var method in basicString.Methods)
foreach (var basicString in GetCharSpecializations(@class))
{
if (method.IsDestructor || method.OriginalName == "c_str" ||
(method.IsConstructor && method.Parameters.Count == 2 &&
method.Parameters[0].Type.Desugar().IsPointerToPrimitiveType(PrimitiveType.Char) &&
!method.Parameters[1].Type.Desugar().IsPrimitiveType()))
basicString.GenerationKind = GenerationKind.Generate;
foreach (var method in basicString.Methods)
{
method.GenerationKind = GenerationKind.Generate;
method.Namespace.GenerationKind = GenerationKind.Generate;
method.InstantiatedFrom.GenerationKind = GenerationKind.Generate;
method.InstantiatedFrom.Namespace.GenerationKind = GenerationKind.Generate;
}
else
{
method.ExplicitlyIgnore();
if (method.IsDestructor || method.OriginalName == "c_str" ||
(method.IsConstructor && method.Parameters.Count == 2 &&
method.Parameters[0].Type.Desugar().IsPointerToPrimitiveType(PrimitiveType.Char) &&
!method.Parameters[1].Type.Desugar().IsPrimitiveType()))
{
method.GenerationKind = GenerationKind.Generate;
method.Namespace.GenerationKind = GenerationKind.Generate;
method.InstantiatedFrom.GenerationKind = GenerationKind.Generate;
method.InstantiatedFrom.Namespace.GenerationKind = GenerationKind.Generate;
}
else
{
method.ExplicitlyIgnore();
}
}
}
break;
case "allocator":
foreach (var method in @class.Methods.Where(m => !m.IsConstructor || m.Parameters.Any()))
method.ExplicitlyIgnore();
var allocator = @class.Specializations.First(s =>
s.Arguments[0].Type.Type.Desugar().IsPrimitiveType(PrimitiveType.Char));
allocator.GenerationKind = GenerationKind.Generate;
foreach (var method in allocator.Methods.Where(m => !m.IsDestructor && m.OriginalName != "c_str"))
method.ExplicitlyIgnore();
var ctor = allocator.Methods.Single(m => m.IsConstructor && !m.Parameters.Any());
ctor.GenerationKind = GenerationKind.Generate;
ctor.InstantiatedFrom.GenerationKind = GenerationKind.Generate;
ctor.InstantiatedFrom.Namespace.GenerationKind = GenerationKind.Generate;
foreach (var parameter in ctor.Parameters)
parameter.DefaultArgument = null;
foreach (var allocator in GetCharSpecializations(@class))
{
allocator.GenerationKind = GenerationKind.Generate;
foreach (var method in from method in allocator.Methods
where method.IsDestructor && method.OriginalName != "c_str"
select method)
method.ExplicitlyIgnore();
var ctor = allocator.Methods.Single(m => m.IsConstructor && !m.Parameters.Any());
ctor.GenerationKind = GenerationKind.Generate;
ctor.InstantiatedFrom.GenerationKind = GenerationKind.Generate;
ctor.InstantiatedFrom.Namespace.GenerationKind = GenerationKind.Generate;
foreach (var parameter in ctor.Parameters)
parameter.DefaultArgument = null;
}
break;
case "char_traits":
foreach (var method in @class.Methods)
method.ExplicitlyIgnore();
var charTraits = @class.Specializations.First(s =>
s.Arguments[0].Type.Type.Desugar().IsPrimitiveType(PrimitiveType.Char));
foreach (var method in charTraits.Methods)
method.ExplicitlyIgnore();
charTraits.GenerationKind = GenerationKind.Generate;
charTraits.TemplatedDecl.TemplatedDecl.GenerationKind = GenerationKind.Generate;
foreach (var charTraits in GetCharSpecializations(@class))
{
foreach (var method in charTraits.Methods)
method.ExplicitlyIgnore();
charTraits.GenerationKind = GenerationKind.Generate;
charTraits.TemplatedDecl.TemplatedDecl.GenerationKind = GenerationKind.Generate;
}
break;
}
return true;
}
private static IEnumerable<ClassTemplateSpecialization> GetCharSpecializations(Class @class)
{
return @class.Specializations.Where(s =>
s.Arguments[0].Type.Type.Desugar().IsPrimitiveType(PrimitiveType.Char));
}
public override bool VisitFunctionDecl(Function function)
{
if (!base.VisitFunctionDecl(function))
@@ -100,8 +100,8 @@ public override bool VisitFieldDecl(Field field)
private void CleanSpecializations(Class template)
{
template.Specializations.RemoveAll(s => !s.IsExplicitlyGenerated
&& !specializations.Contains(s) && !internalSpecializations.Contains(s));
template.Specializations.RemoveAll(s =>
!specializations.Contains(s) && !internalSpecializations.Contains(s));
foreach (var specialization in template.Specializations.Where(
s => !s.IsExplicitlyGenerated &&

0 comments on commit 898001f

Please sign in to comment.