From 6d30cf0c3e653101c4c5314dce3189a9c97c571c Mon Sep 17 00:00:00 2001 From: Mike Kestner Date: Thu, 23 May 2002 23:43:25 +0000 Subject: [PATCH] refactoring to remove substantial code duplication thoughout the generator 2002-05-23 Mike Kestner * generator/BoxedGen.cs : Update for Static SymbolTable * generator/CallbackGen.cs : Use GenBase and Parameters classes * generator/CodeGenerator.cs : Update for Static SymbolTable * generator/Ctor.cs : code from StructBase using Parameters class * generator/EnumGen.cs : Use GenBase * generator/GenBase.cs : Abstract Stream Writer creation, stream boilerplate, and common *Name properties * generator/IGeneratable.cs : Update for Static SymbolTable * generator/InterfaceGen.cs : Use GenBase * generator/Method.cs : code from StructBase using Parameters class * generator/ObjectGen.cs : Major refactoring. Use GenBase. Build tables of Member generatables at construct time to facilitate future name collision resolution logic. * generator/Parameters.cs : new generatable to abstract duplicated parameter parsing logic. * generator/Parser.cs : Update for Static SymbolTable * generator/Property.cs : code from ObjectGen * generator/Signal.cs : code from ObjectGen * generator/SignalHandler.cs : Update for Static SymbolTable * generator/StructBase.cs : Update for Static SymbolTable * generator/StructGen.cs : Update for Static SymbolTable * generator/SymbolTable.cs : Make all methods and private members static. There is no reason to ever have multiple tables. svn path=/trunk/gtk-sharp/; revision=4895 --- ChangeLog | 26 +++ generator/BoxedGen.cs | 8 +- generator/CallbackGen.cs | 140 +++----------- generator/CodeGenerator.cs | 8 +- generator/Ctor.cs | 90 +++++++++ generator/EnumGen.cs | 63 +------ generator/GenBase.cs | 85 +++++++++ generator/IGeneratable.cs | 2 +- generator/InterfaceGen.cs | 74 ++------ generator/Method.cs | 151 +++++++++++++++ generator/ObjectGen.cs | 376 ++++++++++++++++--------------------- generator/Parameters.cs | 112 +++++++++++ generator/Parser.cs | 23 +-- generator/Property.cs | 101 ++++++++++ generator/Signal.cs | 76 ++++++++ generator/SignalHandler.cs | 16 +- generator/StructBase.cs | 42 ++--- generator/StructGen.cs | 4 +- generator/SymbolTable.cs | 47 +++-- 19 files changed, 932 insertions(+), 512 deletions(-) create mode 100644 generator/Ctor.cs create mode 100644 generator/GenBase.cs create mode 100644 generator/Method.cs create mode 100644 generator/Parameters.cs create mode 100644 generator/Property.cs create mode 100644 generator/Signal.cs diff --git a/ChangeLog b/ChangeLog index 281bc46a7..4d646a016 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,29 @@ +2002-05-23 Mike Kestner + + * generator/BoxedGen.cs : Update for Static SymbolTable + * generator/CallbackGen.cs : Use GenBase and Parameters classes + * generator/CodeGenerator.cs : Update for Static SymbolTable + * generator/Ctor.cs : code from StructBase using Parameters class + * generator/EnumGen.cs : Use GenBase + * generator/GenBase.cs : Abstract Stream Writer creation, stream + boilerplate, and common *Name properties + * generator/IGeneratable.cs : Update for Static SymbolTable + * generator/InterfaceGen.cs : Use GenBase + * generator/Method.cs : code from StructBase using Parameters class + * generator/ObjectGen.cs : Major refactoring. Use GenBase. Build + tables of Member generatables at construct time to facilitate + future name collision resolution logic. + * generator/Parameters.cs : new generatable to abstract duplicated + parameter parsing logic. + * generator/Parser.cs : Update for Static SymbolTable + * generator/Property.cs : code from ObjectGen + * generator/Signal.cs : code from ObjectGen + * generator/SignalHandler.cs : Update for Static SymbolTable + * generator/StructBase.cs : Update for Static SymbolTable + * generator/StructGen.cs : Update for Static SymbolTable + * generator/SymbolTable.cs : Make all methods and private members + static. There is no reason to ever have multiple tables. + 2002-05-13 Joe Shaw * sample/Makefile.in: Use -L compiler flags and specify all of the diff --git a/generator/BoxedGen.cs b/generator/BoxedGen.cs index 62dde9615..ecae75717 100644 --- a/generator/BoxedGen.cs +++ b/generator/BoxedGen.cs @@ -32,7 +32,7 @@ namespace GtkSharp.Generation { return "(" + QualifiedName + ") GLib.Boxed.FromNative(" + var + ")"; } - public void Generate (SymbolTable table) + public void Generate () { char sep = Path.DirectorySeparatorChar; string dir = ".." + sep + ns.ToLower() + sep + "generated"; @@ -72,7 +72,7 @@ namespace GtkSharp.Generation { switch (node.Name) { case "field": Statistics.IgnoreCount++; - // GenField(member, table, sw); + // GenField(member, sw); break; case "callback": @@ -80,13 +80,13 @@ namespace GtkSharp.Generation { break; case "constructor": - if (!GenCtor(member, table, sw, clash_map)) { + if (!GenCtor(member, sw, clash_map)) { Console.WriteLine(" in boxed " + CName); } break; case "method": - if (!GenMethod(member, table, sw)) { + if (!GenMethod(member, sw)) { Console.WriteLine(" in boxed " + CName); } break; diff --git a/generator/CallbackGen.cs b/generator/CallbackGen.cs index 083e73dd3..24bb0bda7 100644 --- a/generator/CallbackGen.cs +++ b/generator/CallbackGen.cs @@ -10,147 +10,67 @@ namespace GtkSharp.Generation { using System.IO; using System.Xml; - public class CallbackGen : IGeneratable { - - private String ns; - private XmlElement elem; - - public CallbackGen (String ns, XmlElement elem) { - - this.ns = ns; - this.elem = elem; + public class CallbackGen : GenBase, IGeneratable { + + private Parameters parms; + + public CallbackGen (String ns, XmlElement elem) : base (ns, elem) + { + if (elem ["parameters"] != null) + parms = new Parameters (elem ["parameters"]); } - - public String Name { - get - { - return elem.GetAttribute("name"); - } - } - - public String CName { - get - { - return elem.GetAttribute("cname"); - } - } - - public String QualifiedName { - get - { - return ns + "." + elem.GetAttribute("name"); - } - } - + public String MarshalType { get { return QualifiedName; } } - + public String CallByName (String var_name) { return var_name; } - + public String FromNative(String var) { return var; } - - public void Generate (SymbolTable table) + + public void Generate () { - XmlElement ret_elem = elem["return-type"]; + XmlElement ret_elem = Elem["return-type"]; if (ret_elem == null) { - Console.WriteLine("Missing return type in callback " + CName); + Console.WriteLine("No return type in callback " + CName); Statistics.ThrottledCount++; return; } - + string rettype = ret_elem.GetAttribute("type"); - string s_ret = table.GetCSType(rettype); + string s_ret = SymbolTable.GetCSType(rettype); if (s_ret == "") { Console.WriteLine("rettype: " + rettype + " in callback " + CName); Statistics.ThrottledCount++; return; } - - string parmstr = ""; - XmlNode params_elem = elem["parameters"]; - if (params_elem != null) { - - bool need_comma = false; - foreach (XmlNode node in params_elem.ChildNodes) { - if (node.Name != "parameter") { - continue; - } - - XmlElement param = (XmlElement) node; - string type = param.GetAttribute("type"); - string cs_type = table.GetCSType(type); - string name = param.GetAttribute("name"); - name = MangleName(name); - if ((cs_type == "") || (name == "")) { - Console.WriteLine("parmtype: " + type + " in callback " + CName); - Statistics.ThrottledCount++; - break; - } - - if (need_comma) - parmstr += ", "; - - parmstr += (cs_type + " " + name); - need_comma = true; - } + if ((parms != null) && !parms.Validate ()) { + Console.WriteLine(" in callback " + CName); + Statistics.ThrottledCount++; + return; } - - char sep = Path.DirectorySeparatorChar; - string dir = ".." + sep + ns.ToLower() + sep + "generated"; - if (!Directory.Exists(dir)) { - Directory.CreateDirectory(dir); - } - String filename = dir + sep + Name + ".cs"; - - FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write); - StreamWriter sw = new StreamWriter (stream); - - sw.WriteLine ("// Generated File. Do not modify."); - sw.WriteLine ("// 2001-2002 Mike Kestner"); - sw.WriteLine (); - - sw.WriteLine ("namespace " + ns + " {"); - sw.WriteLine (); - - sw.WriteLine ("\tpublic delegate " + s_ret + " " + Name + "(" + parmstr + ");"); - sw.WriteLine (); - sw.WriteLine ("}"); - - sw.Flush(); - sw.Close(); + StreamWriter sw = CreateWriter (); + + string sig = ""; + if (parms != null) + sig = parms.Signature; + + sw.WriteLine ("\tpublic delegate " + s_ret + " " + Name + "(" + sig + ");"); + + CloseWriter (sw); Statistics.CBCount++; } - - - public string MangleName(string name) - { - switch (name) { - case "string": - name = "str1ng"; - break; - case "event": - name = "evnt"; - break; - case "object": - name = "objekt"; - break; - default: - break; - } - return name; - } } } diff --git a/generator/CodeGenerator.cs b/generator/CodeGenerator.cs index b6fa087c3..5993da231 100644 --- a/generator/CodeGenerator.cs +++ b/generator/CodeGenerator.cs @@ -20,11 +20,11 @@ namespace GtkSharp.Generation { } Parser p = new Parser (args[0]); - SymbolTable table = p.Parse (); - Console.WriteLine (table.Count + " types parsed."); + p.Parse (); + Console.WriteLine (SymbolTable.Count + " types parsed."); - foreach (IGeneratable gen in table.Generatables) { - gen.Generate (table); + foreach (IGeneratable gen in SymbolTable.Generatables) { + gen.Generate (); } Statistics.Report(); diff --git a/generator/Ctor.cs b/generator/Ctor.cs new file mode 100644 index 000000000..a75836629 --- /dev/null +++ b/generator/Ctor.cs @@ -0,0 +1,90 @@ +// GtkSharp.Generation.Ctor.cs - The Constructor Generation Class. +// +// Author: Mike Kestner +// +// (c) 2001-2002 Mike Kestner + +namespace GtkSharp.Generation { + + using System; + using System.Collections; + using System.IO; + using System.Xml; + + public class Ctor { + + private string ns; + private XmlElement elem; + private Parameters parms; + + public Ctor (string ns, XmlElement elem) { + this.ns = ns; + this.elem = elem; + XmlElement parms_elem = elem ["parameters"]; + if (parms_elem != null) + parms = new Parameters (parms_elem); + } + + public bool Validate () + { + if ((parms != null) && !parms.Validate ()) { + Console.Write("ctor "); + Statistics.ThrottledCount++; + return false; + } + + return true; + } + + public void Generate (StreamWriter sw, Hashtable clash_map) + { + string sigtypes = ""; + string sig = "()"; + string call = "()"; + string isig = "();"; + if (parms != null) { + call = "(" + parms.CallString + ")"; + sig = "(" + parms.Signature + ")"; + isig = "(" + parms.ImportSig + ");"; + sigtypes = parms.SignatureTypes; + } + + bool clash = false; + if (clash_map.ContainsKey(sigtypes)) { + clash = true; + } else { + clash_map[sigtypes] = elem; + } + + string cname = elem.GetAttribute("cname"); + string name = ((XmlElement)elem.ParentNode).GetAttribute("name"); + + sw.WriteLine("\t\t[DllImport(\"" + SymbolTable.GetDllName(ns) + "\")]"); + sw.WriteLine("\t\tstatic extern IntPtr " + cname + isig); + sw.WriteLine(); + + if (clash) { + String mname = cname.Substring(cname.IndexOf("new")); + mname = mname.Substring(0,1).ToUpper() + mname.Substring(1); + int idx; + while ((idx = mname.IndexOf("_")) > 0) { + mname = mname.Substring(0, idx) + mname.Substring(idx+1, 1).ToUpper() + mname.Substring(idx+2); + } + + sw.WriteLine("\t\tpublic static " + name + " " + mname + sig); + sw.WriteLine("\t\t{"); + sw.WriteLine("\t\t\treturn new " + name + "(" + cname + call + ");"); + } else { + sw.WriteLine("\t\tpublic " + name + sig); + sw.WriteLine("\t\t{"); + sw.WriteLine("\t\t\tRaw = " + cname + call + ";"); + } + + sw.WriteLine("\t\t}"); + sw.WriteLine(); + + Statistics.CtorCount++; + } + } +} + diff --git a/generator/EnumGen.cs b/generator/EnumGen.cs index 45fbfae32..bd285e076 100644 --- a/generator/EnumGen.cs +++ b/generator/EnumGen.cs @@ -10,37 +10,9 @@ namespace GtkSharp.Generation { using System.IO; using System.Xml; - public class EnumGen : IGeneratable { + public class EnumGen : GenBase, IGeneratable { - private String ns; - private XmlElement elem; - - public EnumGen (String ns, XmlElement elem) { - - this.ns = ns; - this.elem = elem; - } - - public String Name { - get - { - return elem.GetAttribute("name"); - } - } - - public String CName { - get - { - return elem.GetAttribute("cname"); - } - } - - public String QualifiedName { - get - { - return ns + "." + elem.GetAttribute("name"); - } - } + public EnumGen (String ns, XmlElement elem) : base (ns, elem) {} public String MarshalType { get @@ -59,26 +31,11 @@ namespace GtkSharp.Generation { return "(" + QualifiedName + ")" + var; } - public void Generate (SymbolTable table) + public void Generate () { - char sep = Path.DirectorySeparatorChar; - string dir = ".." + sep + ns.ToLower() + sep + "generated"; - if (!Directory.Exists(dir)) { - Directory.CreateDirectory(dir); - } - String filename = dir + sep + Name + ".cs"; - - FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write); - StreamWriter sw = new StreamWriter (stream); - - sw.WriteLine ("// Generated File. Do not modify."); - sw.WriteLine ("// 2001 Mike Kestner"); - sw.WriteLine (); - - sw.WriteLine ("namespace " + ns + " {"); - sw.WriteLine (); - - if (elem.GetAttribute("type") == "flags") { + StreamWriter sw = CreateWriter (); + + if (Elem.GetAttribute("type") == "flags") { sw.WriteLine ("\tusing System;"); sw.WriteLine (); sw.WriteLine ("\t[Flags]"); @@ -87,7 +44,7 @@ namespace GtkSharp.Generation { sw.WriteLine ("\tpublic enum " + Name + " {"); sw.WriteLine (); - foreach (XmlNode node in elem.ChildNodes) { + foreach (XmlNode node in Elem.ChildNodes) { if (node.Name != "member") { continue; } @@ -102,11 +59,7 @@ namespace GtkSharp.Generation { } sw.WriteLine ("\t}"); - sw.WriteLine (); - sw.WriteLine ("}"); - - sw.Flush(); - sw.Close(); + CloseWriter (sw); Statistics.EnumCount++; } diff --git a/generator/GenBase.cs b/generator/GenBase.cs new file mode 100644 index 000000000..8c468888f --- /dev/null +++ b/generator/GenBase.cs @@ -0,0 +1,85 @@ +// GtkSharp.Generation.GenBase.cs - The Generatable base class. +// +// Author: Mike Kestner +// +// (c) 2001-2002 Mike Kestner + +namespace GtkSharp.Generation { + + using System; + using System.IO; + using System.Xml; + + public abstract class GenBase { + + private string ns; + private XmlElement elem; + + protected GenBase (string ns, XmlElement elem) + { + this.ns = ns; + this.elem = elem; + } + + public string CName { + get { + return elem.GetAttribute ("cname"); + } + } + + public XmlElement Elem { + get { + return elem; + } + } + + public string Name { + get { + return elem.GetAttribute ("name"); + } + } + + public string Namespace { + get { + return ns; + } + } + + public string QualifiedName { + get { + return ns + "." + Name; + } + } + + protected StreamWriter CreateWriter () + { + char sep = Path.DirectorySeparatorChar; + string dir = ".." + sep + ns.ToLower() + sep + "generated"; + if (!Directory.Exists(dir)) { + Directory.CreateDirectory(dir); + } + String filename = dir + sep + Name + ".cs"; + + FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write); + StreamWriter sw = new StreamWriter (stream); + + sw.WriteLine ("// Generated File. Do not modify."); + sw.WriteLine ("// 2001-2002 Mike Kestner"); + sw.WriteLine (); + sw.WriteLine ("namespace " + ns + " {"); + sw.WriteLine (); + + return sw; + } + + protected void CloseWriter (StreamWriter sw) + { + sw.WriteLine (); + sw.WriteLine ("}"); + sw.Flush(); + sw.Close(); + } + + } +} + diff --git a/generator/IGeneratable.cs b/generator/IGeneratable.cs index 1a583ba41..c5236f6ec 100644 --- a/generator/IGeneratable.cs +++ b/generator/IGeneratable.cs @@ -22,6 +22,6 @@ namespace GtkSharp.Generation { String FromNative (String var); - void Generate (SymbolTable table); + void Generate (); } } diff --git a/generator/InterfaceGen.cs b/generator/InterfaceGen.cs index 616afee70..1b6997fd0 100644 --- a/generator/InterfaceGen.cs +++ b/generator/InterfaceGen.cs @@ -2,7 +2,7 @@ // // Author: Mike Kestner // -// (c) 2001 Mike Kestner +// (c) 2001-2002 Mike Kestner namespace GtkSharp.Generation { @@ -10,45 +10,17 @@ namespace GtkSharp.Generation { using System.IO; using System.Xml; - public class InterfaceGen : IGeneratable { - - private String ns; - private XmlElement elem; - - public InterfaceGen (String ns, XmlElement elem) { - - this.ns = ns; - this.elem = elem; - } - - public String Name { - get - { - return elem.GetAttribute("name"); - } - } - - public String CName { - get - { - return elem.GetAttribute("cname"); - } - } - - public String QualifiedName { - get - { - return ns + "." + elem.GetAttribute("name"); - } - } - + public class InterfaceGen : GenBase, IGeneratable { + + public InterfaceGen (string ns, XmlElement elem) : base (ns, elem) {} + public String MarshalType { get { return ""; } } - + public String CallByName (String var_name) { return ""; @@ -59,44 +31,26 @@ namespace GtkSharp.Generation { return ""; } - public void Generate (SymbolTable table) + public void Generate () { - char sep = Path.DirectorySeparatorChar; - string dir = ".." + sep + ns.ToLower() + sep + "generated"; - if (!Directory.Exists(dir)) { - Directory.CreateDirectory(dir); - } - String filename = dir + sep + Name + ".cs"; - - FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write); - StreamWriter sw = new StreamWriter (stream); - - sw.WriteLine ("// Generated File. Do not modify."); - sw.WriteLine ("// 2001 Mike Kestner"); - sw.WriteLine (); - - sw.WriteLine ("namespace " + ns + " {"); - sw.WriteLine (); + StreamWriter sw = CreateWriter (); + sw.WriteLine ("\tusing System;"); sw.WriteLine (); - + sw.WriteLine ("\tpublic interface " + Name + " {"); sw.WriteLine (); - - foreach (XmlNode node in elem.ChildNodes) { + + foreach (XmlNode node in Elem.ChildNodes) { if (node.Name != "member") { continue; } //FIXME: Generate the methods. XmlElement member = (XmlElement) node; } - + sw.WriteLine ("\t}"); - sw.WriteLine (); - sw.WriteLine ("}"); - - sw.Flush(); - sw.Close(); + CloseWriter (sw); Statistics.IFaceCount++; } diff --git a/generator/Method.cs b/generator/Method.cs new file mode 100644 index 000000000..338c1980a --- /dev/null +++ b/generator/Method.cs @@ -0,0 +1,151 @@ +// GtkSharp.Generation.Method.cs - The Method Generatable. +// +// Author: Mike Kestner +// +// (c) 2001-2002 Mike Kestner + +namespace GtkSharp.Generation { + + using System; + using System.Collections; + using System.IO; + using System.Xml; + + public class Method { + + private string ns; + private XmlElement elem; + private Parameters parms; + + public Method (string ns, XmlElement elem) + { + this.ns = ns; + this.elem = elem; + if (elem["parameters"] != null) + parms = new Parameters (elem["parameters"]); + } + + public string Name { + get { + return elem.GetAttribute("name"); + } + set { + elem.SetAttribute("name", value); + } + } + + public Parameters Params { + get { + return parms; + } + } + + public override bool Equals (object o) + { + if (!(o is Method)) + return false; + + return (this == (Method) o); + } + + public static bool operator == (Method a, Method b) + { + if (a.Name != b.Name) + return false; + + if (a.Params == null) + return b.Params == null; + + if (b.Params == null) + return false; + + return (a.Params.SignatureTypes == b.Params.SignatureTypes); + } + + public static bool operator != (Method a, Method b) + { + if (a.Name == b.Name) + return false; + + if (a.Params == null) + return b.Params != null; + + if (b.Params == null) + return true; + + return (a.Params.SignatureTypes != b.Params.SignatureTypes); + } + + public bool Validate () + { + XmlElement ret_elem = elem["return-type"]; + if (ret_elem == null) { + Console.Write("Missing return type in method "); + Statistics.ThrottledCount++; + return false; + } + + string rettype = ret_elem.GetAttribute("type"); + string m_ret = SymbolTable.GetMarshalType(rettype); + string s_ret = SymbolTable.GetCSType(rettype); + if (m_ret == "" || s_ret == "") { + Console.Write("rettype: " + rettype + " method "); + Statistics.ThrottledCount++; + return false; + } + + if (Params == null) + return true; + + return Params.Validate (); + } + + public void Generate (StreamWriter sw) + { + string sig, isig, call; + + if (parms != null) { + sig = "(" + parms.Signature + ")"; + isig = "(IntPtr raw, " + parms.ImportSig + ");"; + call = "(Handle, " + parms.CallString + ")"; + } else { + sig = "()"; + isig = "(IntPtr raw);"; + call = "(Handle)"; + } + + string rettype = elem["return-type"].GetAttribute("type"); + string m_ret = SymbolTable.GetMarshalType(rettype); + string s_ret = SymbolTable.GetCSType(rettype); + string cname = elem.GetAttribute("cname"); + + if (cname[0] == '_') { + Statistics.ThrottledCount++; + return; + } + + sw.WriteLine("\t\t[DllImport(\"" + SymbolTable.GetDllName(ns) + + "\", CallingConvention=CallingConvention.Cdecl)]"); + sw.Write("\t\tstatic extern " + m_ret + " " + cname + isig); + sw.WriteLine(); + + sw.Write("\t\tpublic "); + if (elem.HasAttribute("new_flag")) + sw.Write("new "); + sw.WriteLine(s_ret + " " + Name + sig); + sw.WriteLine("\t\t{"); + sw.Write("\t\t\t"); + if (m_ret == "void") { + sw.WriteLine(cname + call + ";"); + } else { + sw.WriteLine("return " + SymbolTable.FromNative(rettype, cname + call) + ";"); + } + + sw.WriteLine("\t\t}"); + sw.WriteLine(); + + Statistics.MethodCount++; + } + } +} + diff --git a/generator/ObjectGen.cs b/generator/ObjectGen.cs index df329addd..0dba5c5b4 100644 --- a/generator/ObjectGen.cs +++ b/generator/ObjectGen.cs @@ -2,7 +2,7 @@ // // Author: Mike Kestner // -// (c) 2001 Mike Kestner +// (c) 2001-2002 Mike Kestner namespace GtkSharp.Generation { @@ -11,258 +11,208 @@ namespace GtkSharp.Generation { using System.IO; using System.Xml; - public class ObjectGen : StructBase, IGeneratable { - - public ObjectGen (String ns, XmlElement elem) : base (ns, elem) {} - - public String MarshalType { - get - { + public class ObjectGen : GenBase, IGeneratable { + + private ArrayList ctors = new ArrayList(); + private Hashtable props = new Hashtable(); + private Hashtable sigs = new Hashtable(); + private Hashtable methods = new Hashtable(); + + public ObjectGen (string ns, XmlElement elem) : base (ns, elem) + { + foreach (XmlNode node in elem.ChildNodes) { + + XmlElement member = (XmlElement) node; + + switch (node.Name) { + case "field": + case "callback": + Statistics.IgnoreCount++; + break; + + case "constructor": + ctors.Add (new Ctor (ns, member)); + break; + + case "method": + methods.Add (member.GetAttribute ("name"), new Method (ns, member)); + break; + + case "property": + props.Add (member.GetAttribute ("name"), new Property (member)); + break; + + case "signal": + sigs.Add (member.GetAttribute ("name"), new Signal (ns, member)); + break; + + default: + Console.WriteLine ("Unexpected node " + node.Name + " in " + CName); + break; + } + } + } + + public string MarshalType { + get { return "IntPtr"; } } - - public String CallByName (String var_name) + + public string CallByName (string var_name) { return var_name + ".Handle"; } - - public String FromNative(String var) + + public string FromNative(string var) { return "(" + QualifiedName + ") GLib.Object.GetObject(" + var + ")"; } - - public void Generate (SymbolTable table) - { - char sep = Path.DirectorySeparatorChar; - string dir = ".." + sep + ns.ToLower() + sep + "generated"; - if (!Directory.Exists(dir)) { - Directory.CreateDirectory(dir); + + private ObjectGen Parent { + get { + string parent = Elem.GetAttribute("parent"); + return SymbolTable.GetObjectGen(parent); } - String filename = dir + sep + Name + ".cs"; - - FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write); - StreamWriter sw = new StreamWriter (stream); - - sw.WriteLine ("// Generated File. Do not modify."); - sw.WriteLine ("// 2001-2002 Mike Kestner"); - sw.WriteLine (); - - sw.WriteLine ("namespace " + ns + " {"); - sw.WriteLine (); - + } + + public void Generate () + { + StreamWriter sw = CreateWriter (); + sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System.Collections;"); sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); - String parent = elem.GetAttribute("parent"); - String cs_parent = table.GetCSType(parent); sw.Write ("\tpublic class " + Name); - if (cs_parent == "") { - sw.WriteLine (" {"); - Console.WriteLine ("Object " + Name + " Unknown parent " + parent); - } else { - sw.WriteLine (" : " + cs_parent + " {"); - } + string cs_parent = SymbolTable.GetCSType(Elem.GetAttribute("parent")); + if (cs_parent != "") + sw.Write (" : " + cs_parent); + sw.WriteLine (" {"); sw.WriteLine (); - - sw.WriteLine("\t\tpublic " + Name + "(IntPtr raw) : base(raw) {}"); - sw.WriteLine(); - - Hashtable clash_map = new Hashtable(); - Hashtable props = new Hashtable(); - Hashtable sigs = new Hashtable(); - ArrayList methods = new ArrayList(); - bool first_sig = true; - - foreach (XmlNode node in elem.ChildNodes) { - - XmlElement member = (XmlElement) node; - switch (node.Name) { - case "field": - Statistics.IgnoreCount++; - break; - - case "callback": - Statistics.IgnoreCount++; - break; - - case "constructor": - if (!GenCtor(member, table, sw, clash_map)) { - Console.WriteLine("in object " + CName); - } - break; - - case "method": - methods.Add(member); - break; - - case "property": - String pname; - if (!GenProperty(member, table, sw, out pname)) { - Console.WriteLine("in object " + CName); - } - props.Add(pname, pname); - break; - - case "signal": - if (first_sig) { - first_sig = false; - sw.WriteLine("\t\tprivate Hashtable Signals = new Hashtable();"); - } - String sname; - if (!GenSignal(member, table, sw, out sname)) { - Console.WriteLine("in object " + CName); - } - sigs.Add(sname, sname); - break; - - default: - Console.WriteLine ("Unexpected node"); - break; - } - - } - - if (!clash_map.ContainsKey("")) { - sw.WriteLine("\t\tprotected " + Name + "() : base(){}"); - sw.WriteLine(); - } - - foreach (XmlElement member in methods) { - String mname = member.GetAttribute("name"); - if ((mname.StartsWith("Set") || mname.StartsWith("Get")) && - props.ContainsKey(mname.Substring(3))) { - continue; - } else if (sigs.ContainsKey(mname)) { - member.SetAttribute("name", "Emit" + mname); - } - - if (!GenMethod(member, table, sw)) { - Console.WriteLine("in object " + CName); - } - } + GenCtors (sw); + GenProperties (sw); + GenSignals (sw); + GenMethods (sw); - string custom = ".." + sep + ns.ToLower() + sep + Name + ".custom"; + char sep = Path.DirectorySeparatorChar; + string custom = ".." + sep + Namespace.ToLower() + sep + Name + ".custom"; if (File.Exists(custom)) { FileStream custstream = new FileStream (custom, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader (custstream); sw.WriteLine (sr.ReadToEnd ()); sr.Close (); } - + sw.WriteLine ("\t}"); - sw.WriteLine (); - sw.WriteLine ("}"); - - sw.Flush(); - sw.Close(); + + CloseWriter (sw); Statistics.ObjectCount++; } - public bool GenProperty (XmlElement prop, SymbolTable table, StreamWriter sw, out String name) + private bool Validate () { - String c_type = prop.GetAttribute("type"); - - char[] ast = {'*'}; - c_type = c_type.TrimEnd(ast); - string cs_type = table.GetCSType(c_type); - - XmlElement parent = (XmlElement) prop.ParentNode; - name = prop.GetAttribute("name"); - if (name == parent.GetAttribute("name")) { - name += "Prop"; - } - - string v_type = ""; - if (table.IsEnum(c_type)) { - v_type = "int"; - } else if (table.IsInterface(c_type)) { - // FIXME: Handle interface props properly. - Console.Write("Interface property detected "); - Statistics.ThrottledCount++; - return true; - } else if (table.IsObject(c_type)) { - v_type = "GLib.Object"; - } - - if (cs_type == "") { - Console.Write("Property has unknown Type {0} ", c_type); - Statistics.ThrottledCount++; + string parent = Elem.GetAttribute("parent"); + string cs_parent = SymbolTable.GetCSType(parent); + if (cs_parent == "") { + Console.WriteLine ("Object " + Name + " Unknown parent " + parent); return false; } - - if (prop.HasAttribute("construct-only") && !prop.HasAttribute("readable")) { - return true; - } - - sw.WriteLine("\t\tpublic " + cs_type + " " + name + " {"); - if (prop.HasAttribute("readable")) { - sw.WriteLine("\t\t\tget {"); - sw.WriteLine("\t\t\t\tGLib.Value val;"); - sw.WriteLine("\t\t\t\tGetProperty(\"" + prop.GetAttribute("cname") + "\", out val);"); - sw.Write("\t\t\t\treturn (" + cs_type + ") "); - if (v_type != "") { - sw.Write("(" + v_type + ") "); - } - sw.WriteLine("val;"); - sw.WriteLine("\t\t\t}"); - } - - if (prop.HasAttribute("writeable") && !prop.HasAttribute("construct-only")) { - sw.WriteLine("\t\t\tset {"); - sw.Write("\t\t\t\tSetProperty(\"" + prop.GetAttribute("cname") + "\", new GLib.Value("); - if (v_type != "") { - sw.Write("(" + v_type + ") "); - } - sw.WriteLine("value));"); - sw.WriteLine("\t\t\t}"); - } - - sw.WriteLine("\t\t}"); - sw.WriteLine(); - - Statistics.PropCount++; + + if (ctors != null) + foreach (Ctor ctor in ctors) + if (!ctor.Validate()) + return false; + + if (props != null) + foreach (Property prop in props.Values) + if (!prop.Validate()) + return false; + + if (sigs != null) + foreach (Signal sig in sigs.Values) + if (!sig.Validate()) + return false; + + if (methods != null) + foreach (Method method in methods.Values) + if (!method.Validate()) + return false; + return true; } - public bool GenSignal (XmlElement sig, SymbolTable table, StreamWriter sw, out String name) + private void GenCtors (StreamWriter sw) { - String cname = "\"" + sig.GetAttribute("cname") + "\""; - name = sig.GetAttribute("name"); + sw.WriteLine("\t\tpublic " + Name + "(IntPtr raw) : base(raw) {}"); + sw.WriteLine(); - String marsh = SignalHandler.GetName(sig, table); - if (marsh == "") { - Statistics.ThrottledCount++; - return false; + Hashtable clash_map = new Hashtable(); + + if (ctors != null) + foreach (Ctor ctor in ctors) { + if (ctor.Validate ()) + ctor.Generate (sw, clash_map); + else + Console.WriteLine(" in Object " + Name); + } + + if (!clash_map.ContainsKey("")) { + sw.WriteLine("\t\tprotected " + Name + "() : base(){}"); + sw.WriteLine(); } - - marsh = "GtkSharp." + marsh; - sw.WriteLine("\t\t/// " + name + " Event "); - sw.WriteLine("\t\t/// "); - // FIXME: Generate some signal docs - sw.WriteLine("\t\t/// "); - sw.WriteLine(); - sw.WriteLine("\t\tpublic event EventHandler " + name + " {"); - sw.WriteLine("\t\t\tadd {"); - sw.WriteLine("\t\t\t\tif (EventList[" + cname + "] == null)"); - sw.Write("\t\t\t\t\tSignals[" + cname + "] = new " + marsh); - sw.WriteLine("(this, Handle, " + cname + ", value);"); - sw.WriteLine("\t\t\t\tEventList.AddHandler(" + cname + ", value);"); - sw.WriteLine("\t\t\t}"); - sw.WriteLine("\t\t\tremove {"); - sw.WriteLine("\t\t\t\tEventList.RemoveHandler(" + cname + ", value);"); - sw.WriteLine("\t\t\t\tif (EventList[" + cname + "] == null)"); - sw.WriteLine("\t\t\t\t\tSignals.Remove(" + cname + ");"); - sw.WriteLine("\t\t\t}"); - sw.WriteLine("\t\t}"); - sw.WriteLine(); + } + + private void GenProperties (StreamWriter sw) + { + if (props == null) + return; + + foreach (Property prop in props.Values) { + if (prop.Validate ()) + prop.Generate (sw); + else + Console.WriteLine(" in Object " + Name); + } + } + + private void GenSignals (StreamWriter sw) + { + if (sigs == null) + return; + + sw.WriteLine("\t\tprivate Hashtable Signals = new Hashtable();"); - Statistics.SignalCount++; - return true; + foreach (Signal sig in sigs.Values) { + if (sig.Validate ()) + sig.Generate (sw); + else + Console.WriteLine(" in Object " + Name); + } + } + + private void GenMethods (StreamWriter sw) + { + if (methods == null) + return; + + foreach (Method method in methods.Values) { + string mname = method.Name; + if ((mname.StartsWith("Set") || mname.StartsWith("Get")) && + (props != null) && props.ContainsKey(mname.Substring(3))) { + continue; + } else if ((sigs != null) && sigs.ContainsKey(mname)) { + method.Name = "Emit" + mname; + } + + if (method.Validate ()) + method.Generate (sw); + else + Console.WriteLine(" in Object " + Name); + } } } } diff --git a/generator/Parameters.cs b/generator/Parameters.cs new file mode 100644 index 000000000..e36531047 --- /dev/null +++ b/generator/Parameters.cs @@ -0,0 +1,112 @@ +// GtkSharp.Generation.Parameters.cs - The Parameters Generation Class. +// +// Author: Mike Kestner +// +// (c) 2001-2002 Mike Kestner + +namespace GtkSharp.Generation { + + using System; + using System.Xml; + + public class Parameters { + + private XmlElement elem; + private string import_sig; + private string call_string; + private string signature; + private string signature_types; + + public Parameters (XmlElement elem) { + + this.elem = elem; + } + + public string CallString { + get { + return call_string; + } + } + + public string ImportSig { + get { + return import_sig; + } + } + + public string Signature { + get { + return signature; + } + } + + public string SignatureTypes { + get { + return signature_types; + } + } + + public bool Validate () + { + signature_types = signature = import_sig = call_string = ""; + bool need_sep = false; + + foreach (XmlNode parm in elem.ChildNodes) { + if (parm.Name != "parameter") { + continue; + } + + XmlElement p_elem = (XmlElement) parm; + string type = p_elem.GetAttribute("type"); + string cs_type = SymbolTable.GetCSType(type); + string m_type = SymbolTable.GetMarshalType(type); + string name = MangleName(p_elem.GetAttribute("name")); + string call_parm = SymbolTable.CallByName(type, name); + + if ((cs_type == "") || (name == "") || + (m_type == "") || (call_parm == "")) { + Console.Write("Name: " + name + " Type: " + type + " "); + return false; + } + + if (p_elem.HasAttribute("array")) { + cs_type += "[]"; + m_type += "[]"; + } + + if (need_sep) { + call_string += ", "; + signature += ", "; + import_sig += ", "; + signature_types += ":"; + } else { + need_sep = true; + } + + signature += (cs_type + " " + name); + signature_types += cs_type; + call_string += call_parm; + import_sig += (m_type + " " + name); + } + + return true; + } + + private string MangleName(string name) + { + switch (name) { + case "string": + return "str1ng"; + case "event": + return "evnt"; + case "object": + return "objekt"; + default: + break; + } + + return name; + } + } +} + diff --git a/generator/Parser.cs b/generator/Parser.cs index 6269a92ed..d8d4dd6be 100644 --- a/generator/Parser.cs +++ b/generator/Parser.cs @@ -13,7 +13,6 @@ namespace GtkSharp.Generation { public class Parser { private XmlDocument doc; - private SymbolTable table; public Parser (String filename) { @@ -31,19 +30,15 @@ namespace GtkSharp.Generation { } - public SymbolTable Parse () + public void Parse () { - if (table != null) return table; - XmlElement root = doc.DocumentElement; if ((root == null) || !root.HasChildNodes) { Console.WriteLine ("No Namespaces found."); - return null; + return; } - table = new SymbolTable (); - foreach (XmlNode ns in root.ChildNodes) { if (ns.Name != "namespace") { continue; @@ -52,8 +47,6 @@ namespace GtkSharp.Generation { XmlElement elem = (XmlElement) ns; ParseNamespace (elem); } - - return table; } private void ParseNamespace (XmlElement ns) @@ -74,27 +67,27 @@ namespace GtkSharp.Generation { break; case "boxed": - table.AddType (new BoxedGen (ns_name, elem)); + SymbolTable.AddType (new BoxedGen (ns_name, elem)); break; case "callback": - table.AddType (new CallbackGen (ns_name, elem)); + SymbolTable.AddType (new CallbackGen (ns_name, elem)); break; case "enum": - table.AddType (new EnumGen (ns_name, elem)); + SymbolTable.AddType (new EnumGen (ns_name, elem)); break; case "interface": - table.AddType (new InterfaceGen (ns_name, elem)); + SymbolTable.AddType (new InterfaceGen (ns_name, elem)); break; case "object": - table.AddType (new ObjectGen (ns_name, elem)); + SymbolTable.AddType (new ObjectGen (ns_name, elem)); break; case "struct": - table.AddType (new StructGen (ns_name, elem)); + SymbolTable.AddType (new StructGen (ns_name, elem)); break; default: diff --git a/generator/Property.cs b/generator/Property.cs new file mode 100644 index 000000000..259ce0020 --- /dev/null +++ b/generator/Property.cs @@ -0,0 +1,101 @@ +// GtkSharp.Generation.Property.cs - The Property Generatable. +// +// Author: Mike Kestner +// +// (c) 2001-2002 Mike Kestner + +namespace GtkSharp.Generation { + + using System; + using System.Collections; + using System.IO; + using System.Xml; + + public class Property { + + private XmlElement elem; + + public Property (XmlElement elem) + { + this.elem = elem; + } + + public bool Validate () + { + string c_type = elem.GetAttribute("type"); + string cs_type = SymbolTable.GetCSType(c_type); + + if (cs_type == "") { + Console.Write("Property has unknown Type {0} ", c_type); + Statistics.ThrottledCount++; + return false; + } + + if (SymbolTable.IsInterface(c_type)) { + // FIXME: Handle interface props properly. + Console.Write("Interface property detected "); + Statistics.ThrottledCount++; + return false; + } + + return true; + } + + public void Generate (StreamWriter sw) + { + string c_type = elem.GetAttribute("type"); + string cs_type = SymbolTable.GetCSType(c_type); + + XmlElement parent = (XmlElement) elem.ParentNode; + string name = elem.GetAttribute("name"); + if (name == parent.GetAttribute("name")) { + name += "Prop"; + } + + string v_type = ""; + if (SymbolTable.IsEnum(c_type)) { + v_type = "int"; + } else if (SymbolTable.IsInterface(c_type)) { + // FIXME: Handle interface props properly. + Console.Write("Interface property detected "); + Statistics.ThrottledCount++; + return; + } else if (SymbolTable.IsObject(c_type)) { + v_type = "GLib.Object"; + } + + if (elem.HasAttribute("construct-only") && !elem.HasAttribute("readable")) { + return; + } + + sw.WriteLine("\t\tpublic " + cs_type + " " + name + " {"); + if (elem.HasAttribute("readable")) { + sw.WriteLine("\t\t\tget {"); + sw.WriteLine("\t\t\t\tGLib.Value val;"); + sw.WriteLine("\t\t\t\tGetProperty(\"" + elem.GetAttribute("cname") + "\", out val);"); + sw.Write("\t\t\t\treturn (" + cs_type + ") "); + if (v_type != "") { + sw.Write("(" + v_type + ") "); + } + sw.WriteLine("val;"); + sw.WriteLine("\t\t\t}"); + } + + if (elem.HasAttribute("writeable") && !elem.HasAttribute("construct-only")) { + sw.WriteLine("\t\t\tset {"); + sw.Write("\t\t\t\tSetProperty(\"" + elem.GetAttribute("cname") + "\", new GLib.Value("); + if (v_type != "") { + sw.Write("(" + v_type + ") "); + } + sw.WriteLine("value));"); + sw.WriteLine("\t\t\t}"); + } + + sw.WriteLine("\t\t}"); + sw.WriteLine(); + + Statistics.PropCount++; + } + } +} + diff --git a/generator/Signal.cs b/generator/Signal.cs new file mode 100644 index 000000000..a72fc4183 --- /dev/null +++ b/generator/Signal.cs @@ -0,0 +1,76 @@ +// GtkSharp.Generation.Signal.cs - The Signal Generatable. +// +// Author: Mike Kestner +// +// (c) 2001-2002 Mike Kestner + +namespace GtkSharp.Generation { + + using System; + using System.Collections; + using System.IO; + using System.Xml; + + public class Signal { + + private string ns; + private string marsh; + private XmlElement elem; + + public Signal (string ns, XmlElement elem) + { + this.ns = ns; + this.elem = elem; + } + + public string Name { + get { + return elem.GetAttribute ("name"); + } + } + + public bool Validate () + { + marsh = SignalHandler.GetName(elem); + if ((Name == "") || (marsh == "")) { + Console.Write ("bad signal " + Name); + Statistics.ThrottledCount++; + return false; + } + + return true; + } + + public void Generate (StreamWriter sw) + { + string cname = "\"" + elem.GetAttribute("cname") + "\""; + marsh = "GtkSharp." + marsh; + + sw.WriteLine("\t\t/// " + Name + " Event "); + sw.WriteLine("\t\t/// "); + // FIXME: Generate some signal docs + sw.WriteLine("\t\t/// "); + sw.WriteLine(); + sw.Write("\t\tpublic "); + if (elem.HasAttribute("new_flag")) + sw.Write("new "); + sw.WriteLine("event EventHandler " + Name + " {"); + sw.WriteLine("\t\t\tadd {"); + sw.WriteLine("\t\t\t\tif (EventList[" + cname + "] == null)"); + sw.Write("\t\t\t\t\tSignals[" + cname + "] = new " + marsh); + sw.WriteLine("(this, Handle, " + cname + ", value);"); + sw.WriteLine("\t\t\t\tEventList.AddHandler(" + cname + ", value);"); + sw.WriteLine("\t\t\t}"); + sw.WriteLine("\t\t\tremove {"); + sw.WriteLine("\t\t\t\tEventList.RemoveHandler(" + cname + ", value);"); + sw.WriteLine("\t\t\t\tif (EventList[" + cname + "] == null)"); + sw.WriteLine("\t\t\t\t\tSignals.Remove(" + cname + ");"); + sw.WriteLine("\t\t\t}"); + sw.WriteLine("\t\t}"); + sw.WriteLine(); + + Statistics.SignalCount++; + } + } +} + diff --git a/generator/SignalHandler.cs b/generator/SignalHandler.cs index fa6608c61..b6dfdeea8 100644 --- a/generator/SignalHandler.cs +++ b/generator/SignalHandler.cs @@ -15,7 +15,7 @@ namespace GtkSharp.Generation { private static Hashtable handlers = new Hashtable (); - public static String GetName(XmlElement sig, SymbolTable table) + public static String GetName(XmlElement sig) { XmlElement ret_elem = sig["return-type"]; if (ret_elem == null) { @@ -29,8 +29,8 @@ namespace GtkSharp.Generation { return ""; } - String s_ret = table.GetCSType(retval); - String p_ret = table.GetMarshalType(retval); + String s_ret = SymbolTable.GetCSType(retval); + String p_ret = SymbolTable.GetMarshalType(retval); if ((s_ret == "") || (p_ret == "")) { Console.Write("Funky type: " + retval); return ""; @@ -38,7 +38,7 @@ namespace GtkSharp.Generation { String key = retval; String pinv = ""; - String name = table.GetName(retval); + String name = SymbolTable.GetName(retval); int pcnt = 0; ArrayList parms = new ArrayList(); @@ -54,7 +54,7 @@ namespace GtkSharp.Generation { XmlElement elem = (XmlElement) parm; String type = elem.GetAttribute("type"); - String ptype = table.GetMarshalType(type); + String ptype = SymbolTable.GetMarshalType(type); if (ptype == "") { Console.Write("Funky type: " + type); return ""; @@ -65,11 +65,11 @@ namespace GtkSharp.Generation { } pinv += (ptype + " arg" + pcnt); parms.Add(type); - if (table.IsObject(type)) { + if (SymbolTable.IsObject(type)) { name += "Object"; key += "Object"; } else { - name += table.GetName(type); + name += SymbolTable.GetName(type); key += type; } pcnt++; @@ -136,7 +136,7 @@ namespace GtkSharp.Generation { sw.WriteLine("\t\t\targs.Args = new object[" + (parms.Count-1) + "];"); } for (int idx=1; idx < parms.Count; idx++) { - if (table.IsObject((String)parms[idx])) { + if (SymbolTable.IsObject((String)parms[idx])) { sw.Write("\t\t\targs.Args[" + (idx-1) + "] "); sw.WriteLine("= GLib.Object.GetObject(arg" + idx + ");"); } else { diff --git a/generator/StructBase.cs b/generator/StructBase.cs index e6913d7f6..4b95ecb1d 100644 --- a/generator/StructBase.cs +++ b/generator/StructBase.cs @@ -45,7 +45,7 @@ namespace GtkSharp.Generation { } - protected bool GenCtor(XmlElement ctor, SymbolTable table, StreamWriter sw, Hashtable clash_map) + protected bool GenCtor(XmlElement ctor, StreamWriter sw, Hashtable clash_map) { String sig, isig, call, sigtypes; XmlElement parms = ctor["parameters"]; @@ -54,9 +54,9 @@ namespace GtkSharp.Generation { call = sig = "()"; isig = "();"; sigtypes = ""; - } else if (GetSignature(parms, table, out sig, out sigtypes) && - GetImportSig(parms, table, out isig) && - GetCallString(parms, table, out call)) { + } else if (GetSignature(parms, out sig, out sigtypes) && + GetImportSig(parms, out isig) && + GetCallString(parms, out call)) { sig = "(" + sig + ")"; isig = "(" + isig + ");"; call = "(" + call + ")"; @@ -75,7 +75,7 @@ namespace GtkSharp.Generation { String cname = ctor.GetAttribute("cname"); - sw.WriteLine("\t\t[DllImport(\"" + table.GetDllName(ns) + + sw.WriteLine("\t\t[DllImport(\"" + SymbolTable.GetDllName(ns) + "\", CallingConvention=CallingConvention.Cdecl)]"); sw.WriteLine("\t\tstatic extern IntPtr " + cname + isig); sw.WriteLine(); @@ -104,7 +104,7 @@ namespace GtkSharp.Generation { return true; } - protected bool GenField (XmlElement field, SymbolTable table, StreamWriter sw) + protected bool GenField (XmlElement field, StreamWriter sw) { String c_type; @@ -115,7 +115,7 @@ namespace GtkSharp.Generation { } char[] ast = {'*'}; c_type = c_type.TrimEnd(ast); - String cs_type = table.GetCSType(c_type); + String cs_type = SymbolTable.GetCSType(c_type); if (cs_type == "") { Console.WriteLine ("Field has unknown Type {0}", c_type); @@ -131,7 +131,7 @@ namespace GtkSharp.Generation { return true; } - protected bool GenMethod(XmlElement method, SymbolTable table, StreamWriter sw) + protected bool GenMethod(XmlElement method, StreamWriter sw) { String sig, isig, call, sigtypes; XmlElement parms = method["parameters"]; @@ -141,9 +141,9 @@ namespace GtkSharp.Generation { sig = "()"; isig = "(IntPtr raw);"; sigtypes = ""; - } else if (GetSignature(parms, table, out sig, out sigtypes) && - GetImportSig(parms, table, out isig) && - GetCallString(parms, table, out call)) { + } else if (GetSignature(parms, out sig, out sigtypes) && + GetImportSig(parms, out isig) && + GetCallString(parms, out call)) { sig = "(" + sig + ")"; isig = "(IntPtr raw, " + isig + ");"; call = "(Handle, " + call + ")"; @@ -162,8 +162,8 @@ namespace GtkSharp.Generation { String rettype = ret_elem.GetAttribute("type"); - String m_ret = table.GetMarshalType(rettype); - String s_ret = table.GetCSType(rettype); + String m_ret = SymbolTable.GetMarshalType(rettype); + String s_ret = SymbolTable.GetCSType(rettype); if (m_ret == "" || s_ret == "") { Console.Write("rettype: " + rettype + " method "); Statistics.ThrottledCount++; @@ -178,7 +178,7 @@ namespace GtkSharp.Generation { return true; } - sw.WriteLine("\t\t[DllImport(\"" + table.GetDllName(ns) + + sw.WriteLine("\t\t[DllImport(\"" + SymbolTable.GetDllName(ns) + "\", CallingConvention=CallingConvention.Cdecl)]"); sw.Write("\t\tstatic extern " + m_ret + " " + cname + isig); sw.WriteLine(); @@ -189,7 +189,7 @@ namespace GtkSharp.Generation { if (m_ret == "void") { sw.WriteLine(cname + call + ";"); } else { - sw.WriteLine("return " + table.FromNative(rettype, cname + call) + ";"); + sw.WriteLine("return " + SymbolTable.FromNative(rettype, cname + call) + ";"); } sw.WriteLine("\t\t}"); @@ -199,7 +199,7 @@ namespace GtkSharp.Generation { return true; } - private bool GetCallString(XmlElement parms, SymbolTable table, out String call) + private bool GetCallString(XmlElement parms, out String call) { call = ""; @@ -215,7 +215,7 @@ namespace GtkSharp.Generation { String type = elem.GetAttribute("type"); String name = elem.GetAttribute("name"); name = MangleName(name); - String call_parm = table.CallByName(type, name); + String call_parm = SymbolTable.CallByName(type, name); if (call_parm == "") { Console.Write("Name: " + name + " Type: " + type + " "); @@ -233,7 +233,7 @@ namespace GtkSharp.Generation { return true; } - private bool GetImportSig(XmlElement parms, SymbolTable table, out String isig) + private bool GetImportSig(XmlElement parms, out String isig) { isig = ""; @@ -246,7 +246,7 @@ namespace GtkSharp.Generation { XmlElement elem = (XmlElement) parm; String type = elem.GetAttribute("type"); - String m_type = table.GetMarshalType(type); + String m_type = SymbolTable.GetMarshalType(type); String name = elem.GetAttribute("name"); name = MangleName(name); @@ -270,7 +270,7 @@ namespace GtkSharp.Generation { return true; } - private bool GetSignature(XmlElement parms, SymbolTable table, out String sig, out String sigtypes) + private bool GetSignature(XmlElement parms, out String sig, out String sigtypes) { sigtypes = sig = ""; bool need_comma = false; @@ -282,7 +282,7 @@ namespace GtkSharp.Generation { XmlElement elem = (XmlElement) parm; String type = elem.GetAttribute("type"); - String cs_type = table.GetCSType(type); + String cs_type = SymbolTable.GetCSType(type); String name = elem.GetAttribute("name"); name = MangleName(name); diff --git a/generator/StructGen.cs b/generator/StructGen.cs index 18af9cc65..a635f3d41 100644 --- a/generator/StructGen.cs +++ b/generator/StructGen.cs @@ -31,7 +31,7 @@ namespace GtkSharp.Generation { return var; } - public void Generate (SymbolTable table) + public void Generate () { char sep = Path.DirectorySeparatorChar; string dir = ".." + sep + ns.ToLower() + sep + "generated"; @@ -66,7 +66,7 @@ namespace GtkSharp.Generation { switch (node.Name) { case "field": Statistics.IgnoreCount++; - // GenField(member, table, sw); + // GenField(member, sw); break; case "callback": diff --git a/generator/SymbolTable.cs b/generator/SymbolTable.cs index 64a9b32ea..5be0b422c 100644 --- a/generator/SymbolTable.cs +++ b/generator/SymbolTable.cs @@ -2,7 +2,7 @@ // // Author: Mike Kestner // -// (c) 2001 Mike Kestner +// (c) 2001-2002 Mike Kestner namespace GtkSharp.Generation { @@ -11,11 +11,11 @@ namespace GtkSharp.Generation { public class SymbolTable { - private Hashtable complex_types = new Hashtable (); - private Hashtable simple_types; - private Hashtable dlls; + private static Hashtable complex_types = new Hashtable (); + private static Hashtable simple_types; + private static Hashtable dlls; - public SymbolTable () + static SymbolTable () { simple_types = new Hashtable (); simple_types.Add ("void", "void"); @@ -72,32 +72,32 @@ namespace GtkSharp.Generation { dlls.Add("Gtk", "gtk-x11-2.0"); } - public void AddType (IGeneratable gen) + public static void AddType (IGeneratable gen) { complex_types [gen.CName] = gen; } - public int Count { + public static int Count { get { return complex_types.Count; } } - public IEnumerable Generatables { + public static IEnumerable Generatables { get { return complex_types.Values; } } - private string Trim(string type) + private static string Trim(string type) { string trim_type = type.TrimEnd('*'); if (trim_type.StartsWith("const-")) return trim_type.Substring(6); return trim_type; } - public string FromNative(string c_type, string val) + public static string FromNative(string c_type, string val) { c_type = Trim(c_type); if (simple_types.ContainsKey(c_type)) { @@ -110,7 +110,7 @@ namespace GtkSharp.Generation { } } - public string GetCSType(string c_type) + public static string GetCSType(string c_type) { c_type = Trim(c_type); if (simple_types.ContainsKey(c_type)) { @@ -123,7 +123,7 @@ namespace GtkSharp.Generation { } } - public string GetName(string c_type) + public static string GetName(string c_type) { c_type = Trim(c_type); if (simple_types.ContainsKey(c_type)) { @@ -142,12 +142,12 @@ namespace GtkSharp.Generation { } } - public string GetDllName(string ns) + public static string GetDllName(string ns) { return (string) dlls[ns]; } - public string GetMarshalType(string c_type) + public static string GetMarshalType(string c_type) { c_type = Trim(c_type); if (simple_types.ContainsKey(c_type)) { @@ -160,7 +160,7 @@ namespace GtkSharp.Generation { } } - public string CallByName(string c_type, string var_name) + public static string CallByName(string c_type, string var_name) { c_type = Trim(c_type); if (simple_types.ContainsKey(c_type)) { @@ -173,7 +173,7 @@ namespace GtkSharp.Generation { } } - public bool IsBoxed(string c_type) + public static bool IsBoxed(string c_type) { c_type = Trim(c_type); if (complex_types.ContainsKey(c_type)) { @@ -185,7 +185,7 @@ namespace GtkSharp.Generation { return false; } - public bool IsEnum(string c_type) + public static bool IsEnum(string c_type) { c_type = Trim(c_type); if (complex_types.ContainsKey(c_type)) { @@ -197,7 +197,7 @@ namespace GtkSharp.Generation { return false; } - public bool IsInterface(string c_type) + public static bool IsInterface(string c_type) { c_type = Trim(c_type); if (complex_types.ContainsKey(c_type)) { @@ -209,7 +209,16 @@ namespace GtkSharp.Generation { return false; } - public bool IsObject(string c_type) + public static ObjectGen GetObjectGen(string c_type) + { + c_type = Trim(c_type); + if (IsObject(c_type)) { + return (ObjectGen) complex_types[c_type]; + } + return null; + } + + public static bool IsObject(string c_type) { c_type = Trim(c_type); if (complex_types.ContainsKey(c_type)) {