From 82a957bc9d3d1fcc44c715a1020fe2afd16c7f4a Mon Sep 17 00:00:00 2001 From: Bertrand Lorentz Date: Sun, 4 Nov 2012 16:19:05 +0100 Subject: [PATCH] generator: Switch a lot of collections to their generic counterpart The additional type information makes the code a bit more safe and readable. At least one bug is fixed by this: in ObjectGen, an invalid child property could still be generated, as it was not removed from the hash table. This should cause no real change to the generated code, except maybe in the order of some members. --- generator/ArrayParameter.cs | 6 ++-- generator/ClassBase.cs | 25 +++++++------- generator/CodeGenerator.cs | 6 ++-- generator/Ctor.cs | 6 ++-- generator/EnumGen.cs | 4 +-- generator/InterfaceGen.cs | 16 ++++----- generator/ObjectBase.cs | 20 +++++++----- generator/ObjectGen.cs | 65 +++++++++++++++++++++---------------- generator/Parameters.cs | 2 +- generator/Parser.cs | 22 +++++++++---- generator/Signature.cs | 4 +-- generator/StructBase.cs | 4 +-- generator/SymbolTable.cs | 15 +++++---- generator/VMSignature.cs | 4 +-- 14 files changed, 110 insertions(+), 89 deletions(-) diff --git a/generator/ArrayParameter.cs b/generator/ArrayParameter.cs index f8965bdc3..9b4f8034d 100644 --- a/generator/ArrayParameter.cs +++ b/generator/ArrayParameter.cs @@ -23,7 +23,7 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.Xml; public class ArrayParameter : Parameter { @@ -55,7 +55,7 @@ namespace GtkSharp.Generation { if (CSType == MarshalType) return new string [0]; - ArrayList result = new ArrayList (); + var result = new List (); result.Add (String.Format ("int cnt_{0} = {0} == null ? 0 : {0}.Length;", CallName)); result.Add (String.Format ("{0}[] native_{1} = new {0} [cnt_{1}" + (NullTerminated ? " + 1" : "") + "];", MarshalType.TrimEnd('[', ']'), CallName)); result.Add (String.Format ("for (int i = 0; i < cnt_{0}; i++)", CallName)); @@ -67,7 +67,7 @@ namespace GtkSharp.Generation { if (NullTerminated) result.Add (String.Format ("native_{0} [cnt_{0}] = IntPtr.Zero;", CallName)); - return (string[]) result.ToArray (typeof (string)); + return result.ToArray (); } } diff --git a/generator/ClassBase.cs b/generator/ClassBase.cs index ca79e486e..4ce6aa86b 100644 --- a/generator/ClassBase.cs +++ b/generator/ClassBase.cs @@ -26,6 +26,7 @@ namespace GtkSharp.Generation { using System; using System.Collections; + using System.Collections.Generic; using System.IO; using System.Xml; @@ -33,12 +34,12 @@ namespace GtkSharp.Generation { protected Hashtable props = new Hashtable(); protected Hashtable fields = new Hashtable(); protected Hashtable methods = new Hashtable(); - protected ArrayList interfaces = new ArrayList(); - protected ArrayList managed_interfaces = new ArrayList(); - protected ArrayList ctors = new ArrayList(); + protected IList interfaces = new List(); + protected IList managed_interfaces = new List(); + protected IList ctors = new List(); private bool ctors_initted = false; - private Hashtable clash_map; + private Dictionary clash_map; private bool deprecated = false; private bool isabstract = false; @@ -240,8 +241,8 @@ namespace GtkSharp.Generation { (fields != null) && fields.ContainsKey(mname.Substring(3)))); } - public void GenMethods (GenerationInfo gen_info, Hashtable collisions, ClassBase implementor) - { + public void GenMethods (GenerationInfo gen_info, IDictionary collisions, ClassBase implementor) + { if (methods == null) return; @@ -250,7 +251,7 @@ namespace GtkSharp.Generation { continue; string oname = null, oprotection = null; - if (collisions != null && collisions.Contains (method.Name)) { + if (collisions != null && collisions.ContainsKey (method.Name)) { oname = method.Name; oprotection = method.Protection; method.Name = QualifiedName + "." + method.Name; @@ -332,7 +333,7 @@ namespace GtkSharp.Generation { return false; } - public ArrayList Ctors { get { return ctors; } } + public IList Ctors { get { return ctors; } } bool HasStaticCtor (string name) { @@ -354,12 +355,12 @@ namespace GtkSharp.Generation { if (Parent != null) Parent.InitializeCtors (); - ArrayList valid_ctors = new ArrayList(); - clash_map = new Hashtable(); + var valid_ctors = new List(); + clash_map = new Dictionary(); foreach (Ctor ctor in ctors) { - if (clash_map.Contains (ctor.Signature.Types)) { - Ctor clash = clash_map [ctor.Signature.Types] as Ctor; + if (clash_map.ContainsKey (ctor.Signature.Types)) { + Ctor clash = clash_map [ctor.Signature.Types]; Ctor alter = ctor.Preferred ? clash : ctor; alter.IsStatic = true; if (Parent != null && Parent.HasStaticCtor (alter.StaticName)) diff --git a/generator/CodeGenerator.cs b/generator/CodeGenerator.cs index 05c38e988..e3d05b56f 100644 --- a/generator/CodeGenerator.cs +++ b/generator/CodeGenerator.cs @@ -23,7 +23,7 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.Xml; public class CodeGenerator { @@ -43,7 +43,7 @@ namespace GtkSharp.Generation { string gluelib_name = ""; SymbolTable table = SymbolTable.Table; - ArrayList gens = new ArrayList (); + var gens = new List (); foreach (string arg in args) { if (arg.StartsWith ("--customdir=")) { Console.WriteLine ("Using .custom files is not supported anymore, use partial classes instead."); @@ -90,7 +90,7 @@ namespace GtkSharp.Generation { // Now that everything is loaded, validate all the to-be- // generated generatables and then remove the invalid ones. - ArrayList invalids = new ArrayList (); + var invalids = new List (); foreach (IGeneratable gen in gens) { if (!gen.Validate ()) invalids.Add (gen); diff --git a/generator/Ctor.cs b/generator/Ctor.cs index 5dccb7454..3fe0c842c 100644 --- a/generator/Ctor.cs +++ b/generator/Ctor.cs @@ -23,7 +23,7 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.IO; using System.Xml; @@ -109,8 +109,8 @@ namespace GtkSharp.Generation { sw.WriteLine ("\t\t\t\tCreateNativeObject (new string [0], new GLib.Value[0]);"); sw.WriteLine ("\t\t\t\treturn;"); } else { - ArrayList names = new ArrayList (); - ArrayList values = new ArrayList (); + var names = new List (); + var values = new List (); for (int i = 0; i < Parameters.Count; i++) { Parameter p = Parameters[i]; if (container_type.GetPropertyRecursively (p.StudlyName) != null) { diff --git a/generator/EnumGen.cs b/generator/EnumGen.cs index 0d00575fb..94e97c770 100644 --- a/generator/EnumGen.cs +++ b/generator/EnumGen.cs @@ -22,7 +22,7 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.IO; using System.Xml; using System.Text.RegularExpressions; @@ -30,7 +30,7 @@ namespace GtkSharp.Generation { public class EnumGen : GenBase { string enum_type = String.Empty; - ArrayList members = new ArrayList (); + IList members = new List (); public EnumGen (XmlElement ns, XmlElement elem) : base (ns, elem) { diff --git a/generator/InterfaceGen.cs b/generator/InterfaceGen.cs index f0d7e811e..1f8c27e80 100644 --- a/generator/InterfaceGen.cs +++ b/generator/InterfaceGen.cs @@ -23,7 +23,7 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.IO; using System.Xml; @@ -74,7 +74,7 @@ namespace GtkSharp.Generation { return false; LogWriter log = new LogWriter (QualifiedName); - ArrayList invalids = new ArrayList (); + var invalids = new List (); foreach (Method method in methods.Values) { if (!method.Validate (log)) invalids.Add (method); @@ -264,7 +264,7 @@ namespace GtkSharp.Generation { Method temp = methods ["GetType"] as Method; if (temp != null) methods.Remove ("GetType"); - GenMethods (gen_info, new Hashtable (), this); + GenMethods (gen_info, null, this); if (temp != null) methods ["GetType"] = temp; @@ -287,20 +287,20 @@ namespace GtkSharp.Generation { string access = IsInternal ? "internal" : "public"; sw.WriteLine ("\t" + access + " partial interface " + Name + "Implementor : GLib.IWrapper {"); sw.WriteLine (); - Hashtable vm_table = new Hashtable (); + var vm_table = new Dictionary (); foreach (InterfaceVM vm in interface_vms) { vm_table [vm.Name] = vm; } foreach (InterfaceVM vm in interface_vms) { - if (vm_table [vm.Name] == null) + if (!vm_table.ContainsKey (vm.Name)) { continue; - else if (!vm.Validate (new LogWriter (QualifiedName))) { + } else if (!vm.Validate (new LogWriter (QualifiedName))) { vm_table.Remove (vm.Name); continue; } else if (vm.IsGetter || vm.IsSetter) { string cmp_name = (vm.IsGetter ? "Set" : "Get") + vm.Name.Substring (3); - InterfaceVM cmp = vm_table [cmp_name] as InterfaceVM; - if (cmp != null && (cmp.IsGetter || cmp.IsSetter)) { + InterfaceVM cmp = null; + if (vm_table.TryGetValue (cmp_name, out cmp) && (cmp.IsGetter || cmp.IsSetter)) { if (vm.IsSetter) cmp.GenerateDeclaration (sw, vm); else diff --git a/generator/ObjectBase.cs b/generator/ObjectBase.cs index 3ddf333fd..e39a3f663 100644 --- a/generator/ObjectBase.cs +++ b/generator/ObjectBase.cs @@ -24,6 +24,7 @@ namespace GtkSharp.Generation { using System; using System.Collections; + using System.Collections.Generic; using System.IO; using System.Xml; @@ -32,12 +33,12 @@ namespace GtkSharp.Generation { protected string class_struct_name = null; bool class_fields_valid; // false if the class structure contains a bitfield or fields of unknown types ArrayList class_members = new ArrayList (); - protected ArrayList class_fields = new ArrayList (); + protected IList class_fields = new List (); // The default handlers of these signals need to be overridden with g_signal_override_class_closure - protected ArrayList virtual_methods = new ArrayList (); + protected IList virtual_methods = new List (); // virtual methods that are generated as an IntPtr in the class struct - protected ArrayList hidden_vms = new ArrayList (); - protected ArrayList interface_vms = new ArrayList (); + protected IList hidden_vms = new List (); + protected IList interface_vms = new List (); protected Hashtable sigs = new Hashtable(); protected ObjectBase (XmlElement ns, XmlElement elem, bool is_interface) : base (ns, elem) @@ -133,10 +134,11 @@ namespace GtkSharp.Generation { if (vm_elem.GetAttributeAsBoolean ("padding") || vm_elem.GetAttributeAsBoolean ("hidden")) hidden_vms.Add (vm); else { - if (vm is GObjectVM) - virtual_methods.Add (vm); - else - interface_vms.Add (vm); + if (vm is GObjectVM) { + virtual_methods.Add ((GObjectVM)vm); + } else { + interface_vms.Add ((InterfaceVM)vm); + } } if (vm.CName != "") class_members.Add (vm); @@ -273,7 +275,7 @@ namespace GtkSharp.Generation { if (!vm.Validate (log)) invalids.Add (vm); - foreach (VirtualMethod invalid_vm in invalids) { + foreach (GObjectVM invalid_vm in invalids) { virtual_methods.Remove (invalid_vm); hidden_vms.Add (invalid_vm); } diff --git a/generator/ObjectGen.cs b/generator/ObjectGen.cs index 238b4c5ca..b316325ca 100644 --- a/generator/ObjectGen.cs +++ b/generator/ObjectGen.cs @@ -23,23 +23,26 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; public class ObjectGen : ObjectBase { - private ArrayList custom_attrs = new ArrayList(); - private ArrayList strings = new ArrayList(); - private Hashtable childprops = new Hashtable(); - private static Hashtable dirs = new Hashtable (); + private IList custom_attrs = new List (); + private IList strings = new List (); + private IDictionary childprops = new Dictionary (); + private static IDictionary dirs = new Dictionary (); public ObjectGen (XmlElement ns, XmlElement elem) : base (ns, elem, false) { foreach (XmlNode node in elem.ChildNodes) { - if (!(node is XmlElement)) continue; - XmlElement member = (XmlElement) node; + XmlElement member = node as XmlElement; + if (member == null) { + continue; + } + if (member.GetAttributeAsBoolean ("hidden")) continue; @@ -53,7 +56,7 @@ namespace GtkSharp.Generation { break; case "static-string": - strings.Add (node); + strings.Add (member); break; case "childprop": @@ -80,14 +83,14 @@ namespace GtkSharp.Generation { { LogWriter log = new LogWriter (QualifiedName); - ArrayList invalids = new ArrayList (); + var invalids = new List (); - foreach (ChildProperty prop in childprops.Values) { - if (!prop.Validate (log)) - invalids.Add (prop); + foreach (string prop_name in childprops.Keys) { + if (!childprops [prop_name].Validate (log)) + invalids.Add (prop_name); } - foreach (ChildProperty prop in invalids) - childprops.Remove (prop); + foreach (string prop_name in invalids) + childprops.Remove (prop_name); return base.Validate (); } @@ -100,11 +103,12 @@ namespace GtkSharp.Generation { private class DirectoryInfo { public string assembly_name; - public Hashtable objects; + public IDictionary objects; - public DirectoryInfo (string assembly_name) { + public DirectoryInfo (string assembly_name) + { this.assembly_name = assembly_name; - objects = new Hashtable (); + objects = new Dictionary (); } } @@ -113,7 +117,7 @@ namespace GtkSharp.Generation { DirectoryInfo result; if (dirs.ContainsKey (dir)) { - result = dirs [dir] as DirectoryInfo; + result = dirs [dir]; if (result.assembly_name != assembly_name) { Console.WriteLine ("Can't put multiple assemblies in one directory."); return null; @@ -195,10 +199,11 @@ namespace GtkSharp.Generation { GenMethods (gen_info, null, null); if (interfaces.Count != 0) { - Hashtable all_methods = new Hashtable (); - foreach (Method m in Methods.Values) + var all_methods = new Dictionary (); + foreach (Method m in Methods.Values) { all_methods[m.Name] = m; - Hashtable collisions = new Hashtable (); + } + var collisions = new Dictionary (); foreach (string iface in interfaces) { ClassBase igen = table.GetClassGen (iface); foreach (Method m in igen.Methods.Values) { @@ -208,7 +213,8 @@ namespace GtkSharp.Generation { continue; } } - Method collision = all_methods[m.Name] as Method; + Method collision = null; + all_methods.TryGetValue (m.Name, out collision); if (collision != null && collision.Signature.Types == m.Signature.Types) collisions[m.Name] = true; else @@ -351,12 +357,14 @@ namespace GtkSharp.Generation { throw new ArgumentException ("cname doesn't follow the NamespaceType capitalization style: " + cname); } - private static bool NeedsMap (Hashtable objs, string assembly_name) + private static bool NeedsMap (IDictionary objs, string assembly_name) { - foreach (string key in objs.Keys) - if (GetExpected (key) != ((string) objs[key])) + foreach (string key in objs.Keys) { + if (GetExpected (key) != objs[key]) { return true; - + } + } + return false; } @@ -375,7 +383,7 @@ namespace GtkSharp.Generation { { foreach (string dir in dirs.Keys) { - DirectoryInfo di = dirs[dir] as DirectoryInfo; + DirectoryInfo di = dirs[dir]; if (!NeedsMap (di.objects, di.assembly_name)) continue; @@ -404,8 +412,9 @@ namespace GtkSharp.Generation { sw.WriteLine ("\t\t\tinitialized = true;"); foreach (string key in dir_info.objects.Keys) { - if (GetExpected(key) != ((string) dir_info.objects[key])) + if (GetExpected(key) != dir_info.objects[key]) { sw.WriteLine ("\t\t\tGLib.GType.Register ({0}.GType, typeof ({0}));", dir_info.objects [key]); + } } sw.WriteLine ("\t\t}"); diff --git a/generator/Parameters.cs b/generator/Parameters.cs index 61726d809..ad31a1b1c 100644 --- a/generator/Parameters.cs +++ b/generator/Parameters.cs @@ -29,7 +29,7 @@ namespace GtkSharp.Generation { public class Parameters : IEnumerable { - List param_list = new List (); + IList param_list = new List (); XmlElement elem; bool first_is_instance; diff --git a/generator/Parser.cs b/generator/Parser.cs index 610193736..2f0ba6cd2 100644 --- a/generator/Parser.cs +++ b/generator/Parser.cs @@ -23,7 +23,7 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.IO; using System.Xml; @@ -74,7 +74,7 @@ namespace GtkSharp.Generation { if (parser_version > curr_parser_version) Console.WriteLine ("WARNING: The input file {0} was created by a parser that was released after this version of the generator. Consider updating the code generator if you experience problems.", filename); - ArrayList gens = new ArrayList (); + var gens = new List (); foreach (XmlNode child in root.ChildNodes) { XmlElement elem = child as XmlElement; @@ -94,12 +94,12 @@ namespace GtkSharp.Generation { } } - return (IGeneratable[]) gens.ToArray (typeof (IGeneratable)); + return gens.ToArray (); } - private ArrayList ParseNamespace (XmlElement ns) + private IList ParseNamespace (XmlElement ns) { - ArrayList result = new ArrayList (); + var result = new List (); foreach (XmlNode def in ns.ChildNodes) { @@ -121,7 +121,11 @@ namespace GtkSharp.Generation { result.Add (new AliasGen (aname, atype)); break; case "boxed": - result.Add (is_opaque ? new OpaqueGen (ns, elem) as object : new BoxedGen (ns, elem) as object); + if (is_opaque) { + result.Add (new OpaqueGen (ns, elem)); + } else { + result.Add (new BoxedGen (ns, elem)); + } break; case "callback": result.Add (new CallbackGen (ns, elem)); @@ -139,7 +143,11 @@ namespace GtkSharp.Generation { result.Add (new ClassGen (ns, elem)); break; case "struct": - result.Add (is_opaque ? new OpaqueGen (ns, elem) as object : new StructGen (ns, elem) as object); + if (is_opaque) { + result.Add (new OpaqueGen (ns, elem)); + } else { + result.Add (new StructGen (ns, elem)); + } break; default: Console.WriteLine ("Parser::ParseNamespace - Unexpected node: " + def.Name); diff --git a/generator/Signature.cs b/generator/Signature.cs index 8e7f7ee3e..80808ddd3 100644 --- a/generator/Signature.cs +++ b/generator/Signature.cs @@ -22,12 +22,12 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.Xml; public class Signature { - private ArrayList parms = new ArrayList (); + private IList parms = new List (); public Signature (Parameters parms) { diff --git a/generator/StructBase.cs b/generator/StructBase.cs index b0ac8bfbc..a01535a97 100644 --- a/generator/StructBase.cs +++ b/generator/StructBase.cs @@ -22,7 +22,7 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; @@ -30,7 +30,7 @@ namespace GtkSharp.Generation { public abstract class StructBase : ClassBase, IManualMarshaler { - new ArrayList fields = new ArrayList (); + new IList fields = new List (); bool need_read_native = false; protected StructBase (XmlElement ns, XmlElement elem) : base (ns, elem) diff --git a/generator/SymbolTable.cs b/generator/SymbolTable.cs index afc7e0190..dca21ebec 100644 --- a/generator/SymbolTable.cs +++ b/generator/SymbolTable.cs @@ -23,13 +23,13 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; public class SymbolTable { static SymbolTable table = null; - Hashtable types = new Hashtable (); + IDictionary types = new Dictionary (); public static SymbolTable Table { get { @@ -168,7 +168,7 @@ namespace GtkSharp.Generation { } } - public IEnumerable Generatables { + public IEnumerable Generatables { get { return types.Values; } @@ -176,7 +176,7 @@ namespace GtkSharp.Generation { public IGeneratable this [string ctype] { get { - return DeAlias (ctype) as IGeneratable; + return DeAlias (ctype); } } @@ -208,16 +208,17 @@ namespace GtkSharp.Generation { return trim_type; } - private object DeAlias (string type) + private IGeneratable DeAlias (string type) { type = Trim (type); - while (types [type] is AliasGen) { + IGeneratable cur_type = null; + while (types.TryGetValue (type, out cur_type) && cur_type is AliasGen) { IGeneratable igen = types [type] as AliasGen; types [type] = types [igen.Name]; type = igen.Name; } - return types [type]; + return cur_type; } public string FromNative(string c_type, string val) diff --git a/generator/VMSignature.cs b/generator/VMSignature.cs index 70ffd60ad..d2dbc7ce4 100644 --- a/generator/VMSignature.cs +++ b/generator/VMSignature.cs @@ -22,12 +22,12 @@ namespace GtkSharp.Generation { using System; - using System.Collections; + using System.Collections.Generic; using System.Xml; public class VMSignature { - private ArrayList parms = new ArrayList (); + private IList parms = new List (); public VMSignature (Parameters parms) {