From f26a2f3172da1984bc9f822285d25fa9bd83730c Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Tue, 19 Oct 2010 14:12:42 +0200 Subject: [PATCH] Support for Generic Type Parameters on methods --- .../src/cs2j/CLR/TranslationTemplate.cs | 65 +++++++++++++++---- .../antlr3/src/cs2jTemplateGen/Main.cs | 19 +++++- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs index dbff816..f249598 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs @@ -296,6 +296,9 @@ namespace RusticiSoftware.Translator.CLR // Method name public string Name { get; set; } + [XmlArrayItem("Name")] + public string[] TypeParams { get; set; } + // Return type public string Return { get; set; } @@ -303,13 +306,14 @@ namespace RusticiSoftware.Translator.CLR { } - public MethodRepTemplate (string retType, string methodName, List pars, string[] imps, string javaRep) : base(pars, imps, javaRep) + public MethodRepTemplate (string retType, string methodName, string[] tParams, List pars, string[] imps, string javaRep) : base(pars, imps, javaRep) { Name = methodName; + TypeParams = tParams; Return = retType; } - public MethodRepTemplate (string retType, string methodName, List pars) : this(retType, methodName, pars, null, null) + public MethodRepTemplate (string retType, string methodName, string[] tParams, List pars) : this(retType, methodName, tParams, pars, null, null) { } @@ -318,6 +322,15 @@ namespace RusticiSoftware.Translator.CLR { if (other == null) return false; + + if (TypeParams != other.TypeParams) { + if (TypeParams == null || other.TypeParams == null || TypeParams.Length != other.TypeParams.Length) + return false; + for (int i = 0; i < TypeParams.Length; i++) { + if (TypeParams[i] != other.TypeParams[i]) + return false; + } + } return Return == other.Return && Name == other.Name && base.Equals(other); } @@ -344,7 +357,14 @@ namespace RusticiSoftware.Translator.CLR public override int GetHashCode () { - return (Return ?? String.Empty).GetHashCode () ^ (Name ?? String.Empty).GetHashCode () ^ base.GetHashCode(); + int hashCode = 0; + if (TypeParams != null) { + foreach (string o in TypeParams) { + hashCode = hashCode ^ o.GetHashCode() ; + } + } + + return hashCode ^ (Return ?? String.Empty).GetHashCode () ^ (Name ?? String.Empty).GetHashCode () ^ base.GetHashCode(); } #endregion @@ -603,8 +623,8 @@ namespace RusticiSoftware.Translator.CLR [XmlElementAttribute("Name")] public string TypeName { get; set; } -// [XmlElementAttribute("Name")] -// public string[] TypeName { get; set; } + [XmlArrayItem("Name")] + public string[] TypeParams { get; set; } // Path to use when resolving types [XmlElementAttribute("Use")] @@ -622,9 +642,10 @@ namespace RusticiSoftware.Translator.CLR TypeName = typeName; } - protected TypeRepTemplate (string tName, UseRepTemplate[] usePath, string[] imports, string javaTemplate) : base(imports, javaTemplate) + protected TypeRepTemplate (string tName, string[] tParams, UseRepTemplate[] usePath, string[] imports, string javaTemplate) : base(imports, javaTemplate) { TypeName = tName; + TypeParams = tParams; Uses = usePath; } @@ -710,6 +731,14 @@ namespace RusticiSoftware.Translator.CLR return false; } } + if (TypeParams != other.TypeParams) { + if (TypeParams == null || other.TypeParams == null || TypeParams.Length != other.TypeParams.Length) + return false; + for (int i = 0; i < TypeParams.Length; i++) { + if (TypeParams[i] != other.TypeParams[i]) + return false; + } + } return TypeName == other.TypeName && base.Equals(other); } @@ -742,6 +771,11 @@ namespace RusticiSoftware.Translator.CLR hashCode ^= e.GetHashCode(); } } + if (TypeParams != null) { + foreach (string e in TypeParams) { + hashCode ^= e.GetHashCode(); + } + } return (Java ?? String.Empty).GetHashCode () ^ hashCode; } #endregion @@ -965,7 +999,8 @@ namespace RusticiSoftware.Translator.CLR { } - protected InterfaceRepTemplate (string tName, UseRepTemplate[] usePath, string[] inherits, List ms, List ps, List es, List ixs, string[] imps, string javaTemplate) : base(tName, usePath, imps, javaTemplate) + protected InterfaceRepTemplate (string tName, string[] tParams, UseRepTemplate[] usePath, string[] inherits, List ms, List ps, List es, List ixs, string[] imps, string javaTemplate) + : base(tName, tParams, usePath, imps, javaTemplate) { Inherits = inherits; _methods = ms; @@ -1130,15 +1165,17 @@ namespace RusticiSoftware.Translator.CLR { } - public ClassRepTemplate (string tName, UseRepTemplate[] usePath, string[] inherits, List cs, List ms, List ps, List fs, List es, List ixs, List cts, - string[] imports, string javaTemplate) : base(tName, usePath, inherits, ms, ps, es, ixs, imports, javaTemplate) + public ClassRepTemplate (string tName, string[] tParams, UseRepTemplate[] usePath, string[] inherits, List cs, List ms, List ps, List fs, List es, List ixs, List cts, + string[] imports, string javaTemplate) + : base(tName, tParams, usePath, inherits, ms, ps, es, ixs, imports, javaTemplate) { _constructors = cs; _fields = fs; _casts = cts; } - public ClassRepTemplate (string tName, UseRepTemplate[] usePath, string[] inherits, List cs, List ms, List ps, List fs, List es, List ixs, List cts) : base(tName, usePath, inherits, ms, ps, es, ixs, null, null) + public ClassRepTemplate (string tName, string[] tParams, UseRepTemplate[] usePath, string[] inherits, List cs, List ms, List ps, List fs, List es, List ixs, List cts) + : base(tName, tParams, usePath, inherits, ms, ps, es, ixs, null, null) { _constructors = cs; _fields = fs; @@ -1247,14 +1284,14 @@ namespace RusticiSoftware.Translator.CLR { } - public StructRepTemplate (string tName, UseRepTemplate[] usePath, string[] inherits, List cs, List ms, List ps, List fs, List es, List ixs, List cts, - string[] imports, string javaTemplate) : base(tName, usePath, inherits, cs, ms, ps, fs, es, ixs, cts, + public StructRepTemplate (string tName, string[] tParams, UseRepTemplate[] usePath, string[] inherits, List cs, List ms, List ps, List fs, List es, List ixs, List cts, + string[] imports, string javaTemplate) : base(tName, tParams, usePath, inherits, cs, ms, ps, fs, es, ixs, cts, imports, javaTemplate) { } - public StructRepTemplate (string tName, UseRepTemplate[] usePath, string[] inherits, List cs, List ms, List ps, List fs, List es, List ixs, List cts) : base(tName, usePath, inherits, cs, ms, ps, fs, es, ixs, cts, - null, null) + public StructRepTemplate (string tName, string[] tParams, UseRepTemplate[] usePath, string[] inherits, List cs, List ms, List ps, List fs, List es, List ixs, List cts) + : base(tName, tParams, usePath, inherits, cs, ms, ps, fs, es, ixs, cts, null, null) { } diff --git a/CSharpTranslator/antlr3/src/cs2jTemplateGen/Main.cs b/CSharpTranslator/antlr3/src/cs2jTemplateGen/Main.cs index 4de730d..874ac0a 100644 --- a/CSharpTranslator/antlr3/src/cs2jTemplateGen/Main.cs +++ b/CSharpTranslator/antlr3/src/cs2jTemplateGen/Main.cs @@ -76,7 +76,17 @@ namespace cs2j.Template.Utils private void buildInterface(InterfaceRepTemplate iface, Type t) { - iface.TypeName = TypeHelper.buildTypeName(t); + if (t.IsGenericType) { + iface.TypeName = t.GetGenericTypeDefinition().FullName; + string[] tParams = new string[t.GetGenericArguments().Length]; + for (int i = 0; i < t.GetGenericArguments().Length; i++) { + tParams[i] = t.GetGenericArguments()[i].Name; + } + iface.TypeParams = tParams; + } + else { + iface.TypeName = t.FullName; + } List bases = new List(); if (t.BaseType != null) @@ -92,6 +102,13 @@ namespace cs2j.Template.Utils MethodRepTemplate methRep = new MethodRepTemplate(); methRep.Name = m.Name; methRep.Return = TypeHelper.buildTypeName(m.ReturnType); + if (m.IsGenericMethod) { + string[] tParams = new string[m.GetGenericArguments().Length]; + for (int i = 0; i < m.GetGenericArguments().Length; i++) { + tParams[i] = m.GetGenericArguments()[i].Name; + } + methRep.TypeParams = tParams; + } buildParameters(methRep, m); iface.Methods.Add(methRep); }