mirror of https://github.com/mono/CppSharp.git
4 changed files with 73 additions and 4 deletions
@ -0,0 +1,50 @@ |
|||||||
|
using System.Collections.Generic; |
||||||
|
using System.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using CppSharp.AST; |
||||||
|
using CppSharp.AST.Extensions; |
||||||
|
using CppSharp.Types; |
||||||
|
|
||||||
|
namespace CppSharp.Passes |
||||||
|
{ |
||||||
|
public class GenerateTemplatesCodePass : TranslationUnitPass |
||||||
|
{ |
||||||
|
public override bool VisitLibrary(ASTContext context) |
||||||
|
{ |
||||||
|
base.VisitLibrary(context); |
||||||
|
WriteTemplateInstantiations(); |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public override bool VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals) |
||||||
|
{ |
||||||
|
if (AlreadyVisited(template) || template.Template.Access == AccessSpecifier.Private) |
||||||
|
return false; |
||||||
|
|
||||||
|
if (template.Arguments.Select(a => a.Type.Type.Desugar()).All(t => t.IsAddress() && !t.GetFinalPointee().IsDependent)) |
||||||
|
{ |
||||||
|
var cppTypePrinter = new CppTypePrinter { PrintScopeKind = CppTypePrintScopeKind.Qualified }; |
||||||
|
templateInstantiations.Add(string.Format("{0}<{1}>", template.Template.Name, |
||||||
|
string.Join(", ", template.Arguments.Select(a => a.Type.Type.Visit(cppTypePrinter))))); |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private void WriteTemplateInstantiations() |
||||||
|
{ |
||||||
|
var cppBuilder = new StringBuilder(); |
||||||
|
foreach (var header in Driver.Options.Headers) |
||||||
|
cppBuilder.AppendFormat("#include <{0}>\n", header); |
||||||
|
foreach (var templateInstantiation in templateInstantiations) |
||||||
|
cppBuilder.AppendFormat("\ntemplate class {0};", templateInstantiation); |
||||||
|
var cpp = string.Format("{0}.cpp", Driver.Options.TemplatesLibraryName); |
||||||
|
Directory.CreateDirectory(Driver.Options.OutputDir); |
||||||
|
var path = Path.Combine(Driver.Options.OutputDir, cpp); |
||||||
|
File.WriteAllText(path, cppBuilder.ToString()); |
||||||
|
} |
||||||
|
|
||||||
|
private HashSet<string> templateInstantiations = new HashSet<string>(); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue