diff --git a/src/Generator/Generators/CLI/CLIForwardReferencePrinter.cs b/src/Generator/Generators/CLI/CLIForwardReferencePrinter.cs index 5b767234..61fd5123 100644 --- a/src/Generator/Generators/CLI/CLIForwardReferencePrinter.cs +++ b/src/Generator/Generators/CLI/CLIForwardReferencePrinter.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using CppSharp.AST; +using CppSharp.Types; namespace CppSharp.Generators.CLI { @@ -17,20 +18,22 @@ namespace CppSharp.Generators.CLI public readonly IList Includes; public readonly IList Refs; private readonly TypeRefsVisitor TypeRefs; - private TypeReference currentTypeReference; + public TypeReference CurrentTypeReference; + private readonly ITypeMapDatabase TypeMapDatabase; - public CLIForwardReferencePrinter(TypeRefsVisitor typeRefs) + public CLIForwardReferencePrinter(TypeRefsVisitor typeRefs, ITypeMapDatabase typeMapDatabase) { Includes = new List(); Refs = new List(); TypeRefs = typeRefs; + TypeMapDatabase = typeMapDatabase; } public void Process() { foreach (var typeRef in TypeRefs.References) { - currentTypeReference = typeRef; + CurrentTypeReference = typeRef; typeRef.Declaration.Visit(this); } @@ -65,7 +68,7 @@ namespace CppSharp.Generators.CLI Refs.Add(new CLIForwardReference() { Declaration = @class, - Namespace = currentTypeReference.Namespace, + Namespace = CurrentTypeReference.Namespace, Text = string.Format("value struct {0};", @class.Name) }); @@ -75,7 +78,7 @@ namespace CppSharp.Generators.CLI Refs.Add(new CLIForwardReference() { Declaration = @class, - Namespace = currentTypeReference.Namespace, + Namespace = CurrentTypeReference.Namespace, Text = string.Format("ref class {0};", @class.Name) }); @@ -153,7 +156,7 @@ namespace CppSharp.Generators.CLI Refs.Add(new CLIForwardReference() { Declaration = @enum, - Namespace = currentTypeReference.Namespace, + Namespace = CurrentTypeReference.Namespace, Text = string.Format("enum struct {0};", @enum.Name) }); @@ -163,7 +166,7 @@ namespace CppSharp.Generators.CLI Refs.Add(new CLIForwardReference() { Declaration = @enum, - Namespace = currentTypeReference.Namespace, + Namespace = CurrentTypeReference.Namespace, Text = string.Format("enum struct {0} : {1};", @enum.Name, @enum.Type) }); return true; @@ -176,7 +179,32 @@ namespace CppSharp.Generators.CLI public bool VisitClassTemplateDecl(ClassTemplate template) { - throw new NotImplementedException(); + var decl = template.TemplatedDecl; + + TypeMap typeMap = null; + if (TypeMapDatabase.FindTypeMap(decl, out typeMap)) + { + typeMap.Declaration = decl; + typeMap.CLIForwardReference(this); + return true; + } + + if(decl.Ignore) + return true; + + Includes.Add(GetHeaderFromDecl(template)); + + var @params = string.Empty; + for(var i = 0; i < template.Parameters.Count; i++) + @params = string.Format("{0}{1}typename {2}", @params, (i>0)? ", " : "", template.Parameters[i].Name); + + Refs.Add(new CLIForwardReference() + { + Declaration = template, + Namespace = CurrentTypeReference.Namespace, + Text = string.Format("generic<{0}> ref class {1};", @params, template.TemplatedClass.QualifiedName) + }); + return true; } public bool VisitFunctionTemplateDecl(FunctionTemplate template) diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index 02faded7..4ea8e214 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -48,7 +48,7 @@ namespace CppSharp.Generators.CLI { var typeRefs = TranslationUnit.TypeReferences as TypeRefsVisitor; - var forwardRefsPrinter = new CLIForwardReferencePrinter(typeRefs); + var forwardRefsPrinter = new CLIForwardReferencePrinter(typeRefs, Driver.TypeDatabase); forwardRefsPrinter.Process(); var includes = new SortedSet(StringComparer.InvariantCulture); @@ -75,7 +75,7 @@ namespace CppSharp.Generators.CLI { var typeRefs = TranslationUnit.TypeReferences as TypeRefsVisitor; - var forwardRefsPrinter = new CLIForwardReferencePrinter(typeRefs); + var forwardRefsPrinter = new CLIForwardReferencePrinter(typeRefs, Driver.TypeDatabase); forwardRefsPrinter.Process(); // Use a set to remove duplicate entries. diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 5f63b6ba..512bedca 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -65,6 +65,10 @@ namespace CppSharp.Types throw new NotImplementedException(); } + public virtual void CLIForwardReference(CLIForwardReferencePrinter ctx) + { + } + public virtual void CLIMarshalToNative(MarshalContext ctx) { throw new NotImplementedException(); diff --git a/src/Generator/Types/Types.cs b/src/Generator/Types/Types.cs index 38f0de57..5d79ed1b 100644 --- a/src/Generator/Types/Types.cs +++ b/src/Generator/Types/Types.cs @@ -250,5 +250,18 @@ namespace CppSharp return decl.Type.Visit(this); } + + public override bool VisitTagType(TagType tag, TypeQualifiers quals) + { + Collect(tag.Declaration); + return true; + } + + public override bool VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals) + { + Collect(template.Template); + return base.VisitTemplateSpecializationType(template, quals); + } + } }