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.
This commit is contained in:
Bertrand Lorentz 2012-11-04 16:19:05 +01:00
parent 608a760d45
commit 82a957bc9d
14 changed files with 110 additions and 89 deletions

View File

@ -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<string> ();
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 ();
}
}

View File

@ -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<string> interfaces = new List<string>();
protected IList<string> managed_interfaces = new List<string>();
protected IList<Ctor> ctors = new List<Ctor>();
private bool ctors_initted = false;
private Hashtable clash_map;
private Dictionary<string, Ctor> 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<string, bool> 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<Ctor> 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<Ctor>();
clash_map = new Dictionary<string, Ctor>();
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))

View File

@ -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<IGeneratable> ();
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<IGeneratable> ();
foreach (IGeneratable gen in gens) {
if (!gen.Validate ())
invalids.Add (gen);

View File

@ -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<string> ();
var values = new List<string> ();
for (int i = 0; i < Parameters.Count; i++) {
Parameter p = Parameters[i];
if (container_type.GetPropertyRecursively (p.StudlyName) != null) {

View File

@ -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<string> members = new List<string> ();
public EnumGen (XmlElement ns, XmlElement elem) : base (ns, elem)
{

View File

@ -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<Method> ();
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<string, InterfaceVM> ();
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

View File

@ -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<ClassField> class_fields = new List<ClassField> ();
// The default handlers of these signals need to be overridden with g_signal_override_class_closure
protected ArrayList virtual_methods = new ArrayList ();
protected IList<GObjectVM> virtual_methods = new List<GObjectVM> ();
// 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<VirtualMethod> hidden_vms = new List<VirtualMethod> ();
protected IList<InterfaceVM> interface_vms = new List<InterfaceVM> ();
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);
}

View File

@ -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<string> custom_attrs = new List<string> ();
private IList<XmlElement> strings = new List<XmlElement> ();
private IDictionary<string, ChildProperty> childprops = new Dictionary<string, ChildProperty> ();
private static IDictionary<string, DirectoryInfo> dirs = new Dictionary<string, DirectoryInfo> ();
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<string> ();
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<string, string> objects;
public DirectoryInfo (string assembly_name) {
public DirectoryInfo (string assembly_name)
{
this.assembly_name = assembly_name;
objects = new Hashtable ();
objects = new Dictionary<string, string> ();
}
}
@ -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<string, Method> ();
foreach (Method m in Methods.Values) {
all_methods[m.Name] = m;
Hashtable collisions = new Hashtable ();
}
var collisions = new Dictionary<string, bool> ();
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<string, string> 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}");

View File

@ -29,7 +29,7 @@ namespace GtkSharp.Generation {
public class Parameters : IEnumerable<Parameter> {
List<Parameter> param_list = new List<Parameter> ();
IList<Parameter> param_list = new List<Parameter> ();
XmlElement elem;
bool first_is_instance;

View File

@ -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<IGeneratable> ();
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<IGeneratable> ParseNamespace (XmlElement ns)
{
ArrayList result = new ArrayList ();
var result = new List<IGeneratable> ();
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);

View File

@ -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<Parameter> parms = new List<Parameter> ();
public Signature (Parameters parms)
{

View File

@ -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<StructField> fields = new List<StructField> ();
bool need_read_native = false;
protected StructBase (XmlElement ns, XmlElement elem) : base (ns, elem)

View File

@ -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<string, IGeneratable> types = new Dictionary<string, IGeneratable> ();
public static SymbolTable Table {
get {
@ -168,7 +168,7 @@ namespace GtkSharp.Generation {
}
}
public IEnumerable Generatables {
public IEnumerable<IGeneratable> 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)

View File

@ -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<Parameter> parms = new List<Parameter> ();
public VMSignature (Parameters parms)
{