From f738d00738e08aed4cd1b94a3071cd941c5f6428 Mon Sep 17 00:00:00 2001 From: marcos henrich Date: Fri, 19 Jul 2013 17:57:50 +0100 Subject: [PATCH 01/10] Parser now initializes ClassTemplate's parameters --- src/Parser/Parser.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index 89c9b0f6..89daffee 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -499,6 +499,17 @@ CppSharp::AST::ClassTemplate^ Parser::WalkClassTemplate(clang::ClassTemplateDecl auto Class = WalkRecordCXX(TD->getTemplatedDecl()); CppSharp::AST::ClassTemplate^ CT = gcnew CppSharp::AST::ClassTemplate(Class); + auto TPL = TD->getTemplateParameters(); + for(auto it = TPL->begin(); it != TPL->end(); ++it) + { + auto ND = *it; + + auto TP = CppSharp::AST::TemplateParameter(); + TP.Name = clix::marshalString(ND->getNameAsString()); + + CT->Parameters->Add(TP); + } + return CT; } From 10b4cde90744dfe28f30d504e27769709e52bb70 Mon Sep 17 00:00:00 2001 From: marcos henrich Date: Fri, 19 Jul 2013 18:01:23 +0100 Subject: [PATCH 02/10] Added support to specify with TypeMaps, required includes and forward references of generic types --- .../CLI/CLIForwardReferencePrinter.cs | 44 +++++++++++++++---- .../Generators/CLI/CLIHeadersTemplate.cs | 4 +- src/Generator/Types/TypeMap.cs | 4 ++ src/Generator/Types/Types.cs | 13 ++++++ 4 files changed, 55 insertions(+), 10 deletions(-) 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 @@ public class CLIForwardReferencePrinter : IDeclVisitor 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 @@ public bool VisitClassDecl(Class @class) Refs.Add(new CLIForwardReference() { Declaration = @class, - Namespace = currentTypeReference.Namespace, + Namespace = CurrentTypeReference.Namespace, Text = string.Format("value struct {0};", @class.Name) }); @@ -75,7 +78,7 @@ public bool VisitClassDecl(Class @class) Refs.Add(new CLIForwardReference() { Declaration = @class, - Namespace = currentTypeReference.Namespace, + Namespace = CurrentTypeReference.Namespace, Text = string.Format("ref class {0};", @class.Name) }); @@ -153,7 +156,7 @@ public bool VisitEnumDecl(Enumeration @enum) Refs.Add(new CLIForwardReference() { Declaration = @enum, - Namespace = currentTypeReference.Namespace, + Namespace = CurrentTypeReference.Namespace, Text = string.Format("enum struct {0};", @enum.Name) }); @@ -163,7 +166,7 @@ public bool VisitEnumDecl(Enumeration @enum) 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 @@ public bool VisitVariableDecl(Variable variable) 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 @@ public void GenerateIncludeForwardRefs() { 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 @@ public void GenerateForwardRefs(Namespace @namespace) { 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 @@ public virtual string CLISignature(CLITypePrinterContext ctx) 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 @@ public override bool VisitFieldDecl(Field field) 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); + } + } } From 8e34a026b91dd6c5ca3854fee14896f056b27fa7 Mon Sep 17 00:00:00 2001 From: marcos henrich Date: Mon, 22 Jul 2013 02:21:07 +0100 Subject: [PATCH 03/10] Changed property Declaration.Name to virtual. Now ClassTemplate shares Name with its TemplatedClass. --- src/AST/Declaration.cs | 4 ++-- src/AST/Template.cs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/AST/Declaration.cs b/src/AST/Declaration.cs index f794b39e..c715d347 100644 --- a/src/AST/Declaration.cs +++ b/src/AST/Declaration.cs @@ -44,7 +44,7 @@ public abstract class Declaration : INamedDecl private string name; // Name of the declaration. - public string Name + public virtual string Name { get { return name; } set @@ -67,7 +67,7 @@ public string QualifiedName } // Name of the declaration. - public string OriginalName; + public virtual string OriginalName { get; set;} public string QualifiedOriginalName { diff --git a/src/AST/Template.cs b/src/AST/Template.cs index dbce1df7..637400a0 100644 --- a/src/AST/Template.cs +++ b/src/AST/Template.cs @@ -41,6 +41,40 @@ public override T Visit(IDeclVisitor visitor) { return visitor.VisitClassTemplateDecl(this); } + + public override string Name + { + get + { + if(TemplatedDecl != null) + return TemplatedClass.Name; + return base.Name; + } + set + { + if(TemplatedDecl != null) + TemplatedClass.Name = value; + else + base.Name = value; + } + } + + public override string OriginalName + { + get + { + if(TemplatedDecl != null) + return TemplatedClass.OriginalName; + return base.OriginalName; + } + set + { + if(TemplatedDecl != null) + TemplatedClass.OriginalName = value; + else + base.OriginalName = value; + } + } } public class ClassTemplateSpecialization : Class From 4875addfb4f61fb21da7fbcafde844524c651906 Mon Sep 17 00:00:00 2001 From: marcos henrich Date: Mon, 22 Jul 2013 02:27:25 +0100 Subject: [PATCH 04/10] Added synthetized check on block generation of ObjectOverridesPass.cs so already existing methods are not modified. --- src/Generator/Passes/ObjectOverridesPass.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Generator/Passes/ObjectOverridesPass.cs b/src/Generator/Passes/ObjectOverridesPass.cs index fb55cf5f..bf3e5f71 100644 --- a/src/Generator/Passes/ObjectOverridesPass.cs +++ b/src/Generator/Passes/ObjectOverridesPass.cs @@ -16,12 +16,16 @@ void OnUnitGenerated(GeneratorOutput output) foreach (var block in template.FindBlocks(CLIBlockKind.MethodBody)) { var method = block.Declaration as Method; + if (!method.IsSynthetized) + continue; + switch (method.Name) { case "GetHashCode": block.Write("return (int)NativePtr;"); break; case "Equals": + block.WriteLine("if (!object) return false;"); block.Write("return Instance == safe_cast({0})->Instance;", method.Parameters[0].Name); From 32428155e3bd911d83bbb28899eefd10ad134997 Mon Sep 17 00:00:00 2001 From: marcos henrich Date: Mon, 22 Jul 2013 02:32:55 +0100 Subject: [PATCH 05/10] Fixed enum type print. --- src/Generator/Types/CppTypePrinter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generator/Types/CppTypePrinter.cs b/src/Generator/Types/CppTypePrinter.cs index a9f9a92f..15a9aed9 100644 --- a/src/Generator/Types/CppTypePrinter.cs +++ b/src/Generator/Types/CppTypePrinter.cs @@ -222,7 +222,7 @@ public string VisitTypedefDecl(TypedefDecl typedef) public string VisitEnumDecl(Enumeration @enum) { - return @enum.Name; + return string.Format("::{0}", @enum.QualifiedOriginalName); } public string VisitVariableDecl(Variable variable) From e168e84d6aea450daa6b20b53258524102fcca3a Mon Sep 17 00:00:00 2001 From: marcos henrich Date: Mon, 22 Jul 2013 02:35:58 +0100 Subject: [PATCH 06/10] Resolved ambiguity of System.Type and CppSharp.AST.Type --- src/Generator/Generators/CLI/CLITypePrinter.cs | 3 ++- src/Generator/Generators/CSharp/CSharpGenerator.cs | 2 +- src/Generator/Generators/CSharp/CSharpTextTemplate.cs | 3 ++- src/Generator/Generators/CSharp/CSharpTypePrinter.cs | 3 ++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Generator/Generators/CLI/CLITypePrinter.cs b/src/Generator/Generators/CLI/CLITypePrinter.cs index ca98abdd..83f5ecba 100644 --- a/src/Generator/Generators/CLI/CLITypePrinter.cs +++ b/src/Generator/Generators/CLI/CLITypePrinter.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using CppSharp.AST; using CppSharp.Types; +using Type = CppSharp.AST.Type; namespace CppSharp.Generators.CLI { @@ -340,7 +341,7 @@ public string VisitProperty(Property property) throw new NotImplementedException(); } - public string ToString(AST.Type type) + public string ToString(Type type) { return type.Visit(this); } diff --git a/src/Generator/Generators/CSharp/CSharpGenerator.cs b/src/Generator/Generators/CSharp/CSharpGenerator.cs index 2e429eee..30d75eea 100644 --- a/src/Generator/Generators/CSharp/CSharpGenerator.cs +++ b/src/Generator/Generators/CSharp/CSharpGenerator.cs @@ -13,7 +13,7 @@ public class CSharpGenerator : Generator public CSharpGenerator(Driver driver) : base(driver) { typePrinter = new CSharpTypePrinter(driver.TypeDatabase, driver.Library); - AST.Type.TypePrinterDelegate += type => type.Visit(typePrinter).Type; + CppSharp.AST.Type.TypePrinterDelegate += type => type.Visit(typePrinter).Type; } public override List