Skip to content

Commit

Permalink
Improved the detection of used template specialisations.
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Jul 14, 2017
1 parent a15aa8f commit 898001f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
8 changes: 2 additions & 6 deletions src/Generator/AST/Utils.cs
Expand Up @@ -89,15 +89,11 @@ public static bool CheckIgnoreProperty(Property prop)
ITypeMapDatabase typeMaps, bool internalOnly = false) ITypeMapDatabase typeMaps, bool internalOnly = false)
{ {
type = type.Desugar(); type = type.Desugar();
type = type.GetFinalPointee() ?? type; type = (type.GetFinalPointee() ?? type).Desugar();
ClassTemplateSpecialization specialization; ClassTemplateSpecialization specialization;
type.TryGetDeclaration(out specialization); type.TryGetDeclaration(out specialization);
if (specialization == null || specialization.IsExplicitlyGenerated) if (specialization == null)
{
if (specialization != null)
addSpecialization(specialization);
return; return;
}


TypeMap typeMap; TypeMap typeMap;
typeMaps.FindTypeMap(specialization, out typeMap); typeMaps.FindTypeMap(specialization, out typeMap);
Expand Down
83 changes: 47 additions & 36 deletions src/Generator/Passes/IgnoreSystemDeclarationsPass.cs
@@ -1,4 +1,5 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions; using CppSharp.AST.Extensions;


Expand Down Expand Up @@ -43,8 +44,7 @@ public override bool VisitClassDecl(Class @class)
if (!@class.TranslationUnit.IsSystemHeader) if (!@class.TranslationUnit.IsSystemHeader)
return false; return false;


if (!@class.IsExplicitlyGenerated) @class.ExplicitlyIgnore();
@class.ExplicitlyIgnore();


if (!@class.IsDependent || @class.Specializations.Count == 0) if (!@class.IsDependent || @class.Specializations.Count == 0)
return false; return false;
Expand All @@ -55,56 +55,67 @@ public override bool VisitClassDecl(Class @class)
case "basic_string": case "basic_string":
foreach (var method in @class.Methods.Where(m => !m.IsDestructor && m.OriginalName != "c_str")) foreach (var method in @class.Methods.Where(m => !m.IsDestructor && m.OriginalName != "c_str"))
method.ExplicitlyIgnore(); method.ExplicitlyIgnore();
var basicString = @class.Specializations.First(s => foreach (var basicString in GetCharSpecializations(@class))
s.Arguments[0].Type.Type.Desugar().IsPrimitiveType(PrimitiveType.Char));
basicString.GenerationKind = GenerationKind.Generate;
foreach (var method in basicString.Methods)
{ {
if (method.IsDestructor || method.OriginalName == "c_str" || basicString.GenerationKind = GenerationKind.Generate;
(method.IsConstructor && method.Parameters.Count == 2 && foreach (var method in basicString.Methods)
method.Parameters[0].Type.Desugar().IsPointerToPrimitiveType(PrimitiveType.Char) &&
!method.Parameters[1].Type.Desugar().IsPrimitiveType()))
{ {
method.GenerationKind = GenerationKind.Generate; if (method.IsDestructor || method.OriginalName == "c_str" ||
method.Namespace.GenerationKind = GenerationKind.Generate; (method.IsConstructor && method.Parameters.Count == 2 &&
method.InstantiatedFrom.GenerationKind = GenerationKind.Generate; method.Parameters[0].Type.Desugar().IsPointerToPrimitiveType(PrimitiveType.Char) &&
method.InstantiatedFrom.Namespace.GenerationKind = GenerationKind.Generate; !method.Parameters[1].Type.Desugar().IsPrimitiveType()))
} {
else method.GenerationKind = GenerationKind.Generate;
{ method.Namespace.GenerationKind = GenerationKind.Generate;
method.ExplicitlyIgnore(); method.InstantiatedFrom.GenerationKind = GenerationKind.Generate;
method.InstantiatedFrom.Namespace.GenerationKind = GenerationKind.Generate;
}
else
{
method.ExplicitlyIgnore();
}
} }
} }
break; break;
case "allocator": case "allocator":
foreach (var method in @class.Methods.Where(m => !m.IsConstructor || m.Parameters.Any())) foreach (var method in @class.Methods.Where(m => !m.IsConstructor || m.Parameters.Any()))
method.ExplicitlyIgnore(); method.ExplicitlyIgnore();
var allocator = @class.Specializations.First(s => foreach (var allocator in GetCharSpecializations(@class))
s.Arguments[0].Type.Type.Desugar().IsPrimitiveType(PrimitiveType.Char)); {
allocator.GenerationKind = GenerationKind.Generate; allocator.GenerationKind = GenerationKind.Generate;
foreach (var method in allocator.Methods.Where(m => !m.IsDestructor && m.OriginalName != "c_str")) foreach (var method in from method in allocator.Methods
method.ExplicitlyIgnore(); where method.IsDestructor && method.OriginalName != "c_str"
var ctor = allocator.Methods.Single(m => m.IsConstructor && !m.Parameters.Any()); select method)
ctor.GenerationKind = GenerationKind.Generate; method.ExplicitlyIgnore();
ctor.InstantiatedFrom.GenerationKind = GenerationKind.Generate; var ctor = allocator.Methods.Single(m => m.IsConstructor && !m.Parameters.Any());
ctor.InstantiatedFrom.Namespace.GenerationKind = GenerationKind.Generate; ctor.GenerationKind = GenerationKind.Generate;
foreach (var parameter in ctor.Parameters) ctor.InstantiatedFrom.GenerationKind = GenerationKind.Generate;
parameter.DefaultArgument = null; ctor.InstantiatedFrom.Namespace.GenerationKind = GenerationKind.Generate;
foreach (var parameter in ctor.Parameters)
parameter.DefaultArgument = null;
}
break; break;
case "char_traits": case "char_traits":
foreach (var method in @class.Methods) foreach (var method in @class.Methods)
method.ExplicitlyIgnore(); method.ExplicitlyIgnore();
var charTraits = @class.Specializations.First(s => foreach (var charTraits in GetCharSpecializations(@class))
s.Arguments[0].Type.Type.Desugar().IsPrimitiveType(PrimitiveType.Char)); {
foreach (var method in charTraits.Methods) foreach (var method in charTraits.Methods)
method.ExplicitlyIgnore(); method.ExplicitlyIgnore();
charTraits.GenerationKind = GenerationKind.Generate; charTraits.GenerationKind = GenerationKind.Generate;
charTraits.TemplatedDecl.TemplatedDecl.GenerationKind = GenerationKind.Generate; charTraits.TemplatedDecl.TemplatedDecl.GenerationKind = GenerationKind.Generate;
}
break; break;
} }
return true; 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) public override bool VisitFunctionDecl(Function function)
{ {
if (!base.VisitFunctionDecl(function)) if (!base.VisitFunctionDecl(function))
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Passes/TrimSpecializationsPass.cs
Expand Up @@ -100,8 +100,8 @@ public override bool VisitFieldDecl(Field field)


private void CleanSpecializations(Class template) private void CleanSpecializations(Class template)
{ {
template.Specializations.RemoveAll(s => !s.IsExplicitlyGenerated template.Specializations.RemoveAll(s =>
&& !specializations.Contains(s) && !internalSpecializations.Contains(s)); !specializations.Contains(s) && !internalSpecializations.Contains(s));


foreach (var specialization in template.Specializations.Where( foreach (var specialization in template.Specializations.Where(
s => !s.IsExplicitlyGenerated && s => !s.IsExplicitlyGenerated &&
Expand Down

0 comments on commit 898001f

Please sign in to comment.