refactoring to remove substantial code duplication thoughout the generator

2002-05-23  Mike Kestner <mkestner@speakeasy.net>

	* 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
This commit is contained in:
Mike Kestner 2002-05-23 23:43:25 +00:00
parent e5db28deea
commit 6d30cf0c3e
19 changed files with 932 additions and 512 deletions

View file

@ -1,3 +1,29 @@
2002-05-23 Mike Kestner <mkestner@speakeasy.net>
* 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 <joe@assbarn.com> 2002-05-13 Joe Shaw <joe@assbarn.com>
* sample/Makefile.in: Use -L compiler flags and specify all of the * sample/Makefile.in: Use -L compiler flags and specify all of the

View file

@ -32,7 +32,7 @@ namespace GtkSharp.Generation {
return "(" + QualifiedName + ") GLib.Boxed.FromNative(" + var + ")"; return "(" + QualifiedName + ") GLib.Boxed.FromNative(" + var + ")";
} }
public void Generate (SymbolTable table) public void Generate ()
{ {
char sep = Path.DirectorySeparatorChar; char sep = Path.DirectorySeparatorChar;
string dir = ".." + sep + ns.ToLower() + sep + "generated"; string dir = ".." + sep + ns.ToLower() + sep + "generated";
@ -72,7 +72,7 @@ namespace GtkSharp.Generation {
switch (node.Name) { switch (node.Name) {
case "field": case "field":
Statistics.IgnoreCount++; Statistics.IgnoreCount++;
// GenField(member, table, sw); // GenField(member, sw);
break; break;
case "callback": case "callback":
@ -80,13 +80,13 @@ namespace GtkSharp.Generation {
break; break;
case "constructor": case "constructor":
if (!GenCtor(member, table, sw, clash_map)) { if (!GenCtor(member, sw, clash_map)) {
Console.WriteLine(" in boxed " + CName); Console.WriteLine(" in boxed " + CName);
} }
break; break;
case "method": case "method":
if (!GenMethod(member, table, sw)) { if (!GenMethod(member, sw)) {
Console.WriteLine(" in boxed " + CName); Console.WriteLine(" in boxed " + CName);
} }
break; break;

View file

@ -10,147 +10,67 @@ namespace GtkSharp.Generation {
using System.IO; using System.IO;
using System.Xml; using System.Xml;
public class CallbackGen : IGeneratable { public class CallbackGen : GenBase, IGeneratable {
private String ns; private Parameters parms;
private XmlElement elem;
public CallbackGen (String ns, XmlElement elem) : base (ns, elem)
public CallbackGen (String ns, XmlElement elem) { {
if (elem ["parameters"] != null)
this.ns = ns; parms = new Parameters (elem ["parameters"]);
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 String MarshalType { public String MarshalType {
get get
{ {
return QualifiedName; return QualifiedName;
} }
} }
public String CallByName (String var_name) public String CallByName (String var_name)
{ {
return var_name; return var_name;
} }
public String FromNative(String var) public String FromNative(String var)
{ {
return 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) { if (ret_elem == null) {
Console.WriteLine("Missing return type in callback " + CName); Console.WriteLine("No return type in callback " + CName);
Statistics.ThrottledCount++; Statistics.ThrottledCount++;
return; return;
} }
string rettype = ret_elem.GetAttribute("type"); string rettype = ret_elem.GetAttribute("type");
string s_ret = table.GetCSType(rettype); string s_ret = SymbolTable.GetCSType(rettype);
if (s_ret == "") { if (s_ret == "") {
Console.WriteLine("rettype: " + rettype + " in callback " + CName); Console.WriteLine("rettype: " + rettype + " in callback " + CName);
Statistics.ThrottledCount++; Statistics.ThrottledCount++;
return; 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 == "")) { if ((parms != null) && !parms.Validate ()) {
Console.WriteLine("parmtype: " + type + " in callback " + CName); Console.WriteLine(" in callback " + CName);
Statistics.ThrottledCount++; Statistics.ThrottledCount++;
break; return;
}
if (need_comma)
parmstr += ", ";
parmstr += (cs_type + " " + name);
need_comma = true;
}
} }
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 ("// <c> 2001-2002 Mike Kestner");
sw.WriteLine ();
sw.WriteLine ("namespace " + ns + " {");
sw.WriteLine ();
sw.WriteLine ("\tpublic delegate " + s_ret + " " + Name + "(" + parmstr + ");");
sw.WriteLine (); StreamWriter sw = CreateWriter ();
sw.WriteLine ("}");
string sig = "";
sw.Flush(); if (parms != null)
sw.Close(); sig = parms.Signature;
sw.WriteLine ("\tpublic delegate " + s_ret + " " + Name + "(" + sig + ");");
CloseWriter (sw);
Statistics.CBCount++; 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;
}
} }
} }

View file

@ -20,11 +20,11 @@ namespace GtkSharp.Generation {
} }
Parser p = new Parser (args[0]); Parser p = new Parser (args[0]);
SymbolTable table = p.Parse (); p.Parse ();
Console.WriteLine (table.Count + " types parsed."); Console.WriteLine (SymbolTable.Count + " types parsed.");
foreach (IGeneratable gen in table.Generatables) { foreach (IGeneratable gen in SymbolTable.Generatables) {
gen.Generate (table); gen.Generate ();
} }
Statistics.Report(); Statistics.Report();

90
generator/Ctor.cs Normal file
View file

@ -0,0 +1,90 @@
// GtkSharp.Generation.Ctor.cs - The Constructor Generation Class.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (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++;
}
}
}

View file

@ -10,37 +10,9 @@ namespace GtkSharp.Generation {
using System.IO; using System.IO;
using System.Xml; using System.Xml;
public class EnumGen : IGeneratable { public class EnumGen : GenBase, IGeneratable {
private String ns; public EnumGen (String ns, XmlElement elem) : base (ns, elem) {}
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 String MarshalType { public String MarshalType {
get get
@ -59,26 +31,11 @@ namespace GtkSharp.Generation {
return "(" + QualifiedName + ")" + var; return "(" + QualifiedName + ")" + var;
} }
public void Generate (SymbolTable table) public void Generate ()
{ {
char sep = Path.DirectorySeparatorChar; StreamWriter sw = CreateWriter ();
string dir = ".." + sep + ns.ToLower() + sep + "generated";
if (!Directory.Exists(dir)) { if (Elem.GetAttribute("type") == "flags") {
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 ("// <c> 2001 Mike Kestner");
sw.WriteLine ();
sw.WriteLine ("namespace " + ns + " {");
sw.WriteLine ();
if (elem.GetAttribute("type") == "flags") {
sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System;");
sw.WriteLine (); sw.WriteLine ();
sw.WriteLine ("\t[Flags]"); sw.WriteLine ("\t[Flags]");
@ -87,7 +44,7 @@ namespace GtkSharp.Generation {
sw.WriteLine ("\tpublic enum " + Name + " {"); sw.WriteLine ("\tpublic enum " + Name + " {");
sw.WriteLine (); sw.WriteLine ();
foreach (XmlNode node in elem.ChildNodes) { foreach (XmlNode node in Elem.ChildNodes) {
if (node.Name != "member") { if (node.Name != "member") {
continue; continue;
} }
@ -102,11 +59,7 @@ namespace GtkSharp.Generation {
} }
sw.WriteLine ("\t}"); sw.WriteLine ("\t}");
sw.WriteLine (); CloseWriter (sw);
sw.WriteLine ("}");
sw.Flush();
sw.Close();
Statistics.EnumCount++; Statistics.EnumCount++;
} }

85
generator/GenBase.cs Normal file
View file

@ -0,0 +1,85 @@
// GtkSharp.Generation.GenBase.cs - The Generatable base class.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (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 ("// <c> 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();
}
}
}

View file

@ -22,6 +22,6 @@ namespace GtkSharp.Generation {
String FromNative (String var); String FromNative (String var);
void Generate (SymbolTable table); void Generate ();
} }
} }

View file

@ -2,7 +2,7 @@
// //
// Author: Mike Kestner <mkestner@speakeasy.net> // Author: Mike Kestner <mkestner@speakeasy.net>
// //
// (c) 2001 Mike Kestner // (c) 2001-2002 Mike Kestner
namespace GtkSharp.Generation { namespace GtkSharp.Generation {
@ -10,45 +10,17 @@ namespace GtkSharp.Generation {
using System.IO; using System.IO;
using System.Xml; using System.Xml;
public class InterfaceGen : IGeneratable { public class InterfaceGen : GenBase, IGeneratable {
private String ns; public InterfaceGen (string ns, XmlElement elem) : base (ns, elem) {}
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 String MarshalType { public String MarshalType {
get get
{ {
return ""; return "";
} }
} }
public String CallByName (String var_name) public String CallByName (String var_name)
{ {
return ""; return "";
@ -59,44 +31,26 @@ namespace GtkSharp.Generation {
return ""; return "";
} }
public void Generate (SymbolTable table) public void Generate ()
{ {
char sep = Path.DirectorySeparatorChar; StreamWriter sw = CreateWriter ();
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 ("// <c> 2001 Mike Kestner");
sw.WriteLine ();
sw.WriteLine ("namespace " + ns + " {");
sw.WriteLine ();
sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System;");
sw.WriteLine (); sw.WriteLine ();
sw.WriteLine ("\tpublic interface " + Name + " {"); sw.WriteLine ("\tpublic interface " + Name + " {");
sw.WriteLine (); sw.WriteLine ();
foreach (XmlNode node in elem.ChildNodes) { foreach (XmlNode node in Elem.ChildNodes) {
if (node.Name != "member") { if (node.Name != "member") {
continue; continue;
} }
//FIXME: Generate the methods. //FIXME: Generate the methods.
XmlElement member = (XmlElement) node; XmlElement member = (XmlElement) node;
} }
sw.WriteLine ("\t}"); sw.WriteLine ("\t}");
sw.WriteLine (); CloseWriter (sw);
sw.WriteLine ("}");
sw.Flush();
sw.Close();
Statistics.IFaceCount++; Statistics.IFaceCount++;
} }

151
generator/Method.cs Normal file
View file

@ -0,0 +1,151 @@
// GtkSharp.Generation.Method.cs - The Method Generatable.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (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++;
}
}
}

View file

@ -2,7 +2,7 @@
// //
// Author: Mike Kestner <mkestner@speakeasy.net> // Author: Mike Kestner <mkestner@speakeasy.net>
// //
// (c) 2001 Mike Kestner // (c) 2001-2002 Mike Kestner
namespace GtkSharp.Generation { namespace GtkSharp.Generation {
@ -11,258 +11,208 @@ namespace GtkSharp.Generation {
using System.IO; using System.IO;
using System.Xml; using System.Xml;
public class ObjectGen : StructBase, IGeneratable { public class ObjectGen : GenBase, IGeneratable {
public ObjectGen (String ns, XmlElement elem) : base (ns, elem) {} private ArrayList ctors = new ArrayList();
private Hashtable props = new Hashtable();
public String MarshalType { private Hashtable sigs = new Hashtable();
get 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"; return "IntPtr";
} }
} }
public String CallByName (String var_name) public string CallByName (string var_name)
{ {
return var_name + ".Handle"; return var_name + ".Handle";
} }
public String FromNative(String var) public string FromNative(string var)
{ {
return "(" + QualifiedName + ") GLib.Object.GetObject(" + var + ")"; return "(" + QualifiedName + ") GLib.Object.GetObject(" + var + ")";
} }
public void Generate (SymbolTable table) private ObjectGen Parent {
{ get {
char sep = Path.DirectorySeparatorChar; string parent = Elem.GetAttribute("parent");
string dir = ".." + sep + ns.ToLower() + sep + "generated"; return SymbolTable.GetObjectGen(parent);
if (!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
} }
String filename = dir + sep + Name + ".cs"; }
FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write); public void Generate ()
StreamWriter sw = new StreamWriter (stream); {
StreamWriter sw = CreateWriter ();
sw.WriteLine ("// Generated File. Do not modify.");
sw.WriteLine ("// <c> 2001-2002 Mike Kestner");
sw.WriteLine ();
sw.WriteLine ("namespace " + ns + " {");
sw.WriteLine ();
sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System;");
sw.WriteLine ("\tusing System.Collections;"); sw.WriteLine ("\tusing System.Collections;");
sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine ("\tusing System.Runtime.InteropServices;");
sw.WriteLine (); sw.WriteLine ();
String parent = elem.GetAttribute("parent");
String cs_parent = table.GetCSType(parent);
sw.Write ("\tpublic class " + Name); sw.Write ("\tpublic class " + Name);
if (cs_parent == "") { string cs_parent = SymbolTable.GetCSType(Elem.GetAttribute("parent"));
sw.WriteLine (" {"); if (cs_parent != "")
Console.WriteLine ("Object " + Name + " Unknown parent " + parent); sw.Write (" : " + cs_parent);
} else { sw.WriteLine (" {");
sw.WriteLine (" : " + 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) { GenCtors (sw);
case "field": GenProperties (sw);
Statistics.IgnoreCount++; GenSignals (sw);
break; GenMethods (sw);
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);
}
}
string custom = ".." + sep + ns.ToLower() + sep + Name + ".custom"; char sep = Path.DirectorySeparatorChar;
string custom = ".." + sep + Namespace.ToLower() + sep + Name + ".custom";
if (File.Exists(custom)) { if (File.Exists(custom)) {
FileStream custstream = new FileStream (custom, FileMode.Open, FileAccess.Read); FileStream custstream = new FileStream (custom, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader (custstream); StreamReader sr = new StreamReader (custstream);
sw.WriteLine (sr.ReadToEnd ()); sw.WriteLine (sr.ReadToEnd ());
sr.Close (); sr.Close ();
} }
sw.WriteLine ("\t}"); sw.WriteLine ("\t}");
sw.WriteLine ();
sw.WriteLine ("}"); CloseWriter (sw);
sw.Flush();
sw.Close();
Statistics.ObjectCount++; Statistics.ObjectCount++;
} }
public bool GenProperty (XmlElement prop, SymbolTable table, StreamWriter sw, out String name) private bool Validate ()
{ {
String c_type = prop.GetAttribute("type"); string parent = Elem.GetAttribute("parent");
string cs_parent = SymbolTable.GetCSType(parent);
char[] ast = {'*'}; if (cs_parent == "") {
c_type = c_type.TrimEnd(ast); Console.WriteLine ("Object " + Name + " Unknown parent " + parent);
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++;
return false; return false;
} }
if (prop.HasAttribute("construct-only") && !prop.HasAttribute("readable")) { if (ctors != null)
return true; foreach (Ctor ctor in ctors)
} if (!ctor.Validate())
return false;
sw.WriteLine("\t\tpublic " + cs_type + " " + name + " {");
if (prop.HasAttribute("readable")) { if (props != null)
sw.WriteLine("\t\t\tget {"); foreach (Property prop in props.Values)
sw.WriteLine("\t\t\t\tGLib.Value val;"); if (!prop.Validate())
sw.WriteLine("\t\t\t\tGetProperty(\"" + prop.GetAttribute("cname") + "\", out val);"); return false;
sw.Write("\t\t\t\treturn (" + cs_type + ") ");
if (v_type != "") { if (sigs != null)
sw.Write("(" + v_type + ") "); foreach (Signal sig in sigs.Values)
} if (!sig.Validate())
sw.WriteLine("val;"); return false;
sw.WriteLine("\t\t\t}");
} if (methods != null)
foreach (Method method in methods.Values)
if (prop.HasAttribute("writeable") && !prop.HasAttribute("construct-only")) { if (!method.Validate())
sw.WriteLine("\t\t\tset {"); return false;
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++;
return true; return true;
} }
public bool GenSignal (XmlElement sig, SymbolTable table, StreamWriter sw, out String name) private void GenCtors (StreamWriter sw)
{ {
String cname = "\"" + sig.GetAttribute("cname") + "\""; sw.WriteLine("\t\tpublic " + Name + "(IntPtr raw) : base(raw) {}");
name = sig.GetAttribute("name"); sw.WriteLine();
String marsh = SignalHandler.GetName(sig, table); Hashtable clash_map = new Hashtable();
if (marsh == "") {
Statistics.ThrottledCount++; if (ctors != null)
return false; 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/// <summary> " + name + " Event </summary>"); }
sw.WriteLine("\t\t/// <remarks>");
// FIXME: Generate some signal docs private void GenProperties (StreamWriter sw)
sw.WriteLine("\t\t/// </remarks>"); {
sw.WriteLine(); if (props == null)
sw.WriteLine("\t\tpublic event EventHandler " + name + " {"); return;
sw.WriteLine("\t\t\tadd {");
sw.WriteLine("\t\t\t\tif (EventList[" + cname + "] == null)"); foreach (Property prop in props.Values) {
sw.Write("\t\t\t\t\tSignals[" + cname + "] = new " + marsh); if (prop.Validate ())
sw.WriteLine("(this, Handle, " + cname + ", value);"); prop.Generate (sw);
sw.WriteLine("\t\t\t\tEventList.AddHandler(" + cname + ", value);"); else
sw.WriteLine("\t\t\t}"); Console.WriteLine(" in Object " + Name);
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 + ");"); private void GenSignals (StreamWriter sw)
sw.WriteLine("\t\t\t}"); {
sw.WriteLine("\t\t}"); if (sigs == null)
sw.WriteLine(); return;
sw.WriteLine("\t\tprivate Hashtable Signals = new Hashtable();");
Statistics.SignalCount++; foreach (Signal sig in sigs.Values) {
return true; 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);
}
} }
} }
} }

112
generator/Parameters.cs Normal file
View file

@ -0,0 +1,112 @@
// GtkSharp.Generation.Parameters.cs - The Parameters Generation Class.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (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;
}
}
}

View file

@ -13,7 +13,6 @@ namespace GtkSharp.Generation {
public class Parser { public class Parser {
private XmlDocument doc; private XmlDocument doc;
private SymbolTable table;
public Parser (String filename) 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; XmlElement root = doc.DocumentElement;
if ((root == null) || !root.HasChildNodes) { if ((root == null) || !root.HasChildNodes) {
Console.WriteLine ("No Namespaces found."); Console.WriteLine ("No Namespaces found.");
return null; return;
} }
table = new SymbolTable ();
foreach (XmlNode ns in root.ChildNodes) { foreach (XmlNode ns in root.ChildNodes) {
if (ns.Name != "namespace") { if (ns.Name != "namespace") {
continue; continue;
@ -52,8 +47,6 @@ namespace GtkSharp.Generation {
XmlElement elem = (XmlElement) ns; XmlElement elem = (XmlElement) ns;
ParseNamespace (elem); ParseNamespace (elem);
} }
return table;
} }
private void ParseNamespace (XmlElement ns) private void ParseNamespace (XmlElement ns)
@ -74,27 +67,27 @@ namespace GtkSharp.Generation {
break; break;
case "boxed": case "boxed":
table.AddType (new BoxedGen (ns_name, elem)); SymbolTable.AddType (new BoxedGen (ns_name, elem));
break; break;
case "callback": case "callback":
table.AddType (new CallbackGen (ns_name, elem)); SymbolTable.AddType (new CallbackGen (ns_name, elem));
break; break;
case "enum": case "enum":
table.AddType (new EnumGen (ns_name, elem)); SymbolTable.AddType (new EnumGen (ns_name, elem));
break; break;
case "interface": case "interface":
table.AddType (new InterfaceGen (ns_name, elem)); SymbolTable.AddType (new InterfaceGen (ns_name, elem));
break; break;
case "object": case "object":
table.AddType (new ObjectGen (ns_name, elem)); SymbolTable.AddType (new ObjectGen (ns_name, elem));
break; break;
case "struct": case "struct":
table.AddType (new StructGen (ns_name, elem)); SymbolTable.AddType (new StructGen (ns_name, elem));
break; break;
default: default:

101
generator/Property.cs Normal file
View file

@ -0,0 +1,101 @@
// GtkSharp.Generation.Property.cs - The Property Generatable.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (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++;
}
}
}

76
generator/Signal.cs Normal file
View file

@ -0,0 +1,76 @@
// GtkSharp.Generation.Signal.cs - The Signal Generatable.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (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/// <summary> " + Name + " Event </summary>");
sw.WriteLine("\t\t/// <remarks>");
// FIXME: Generate some signal docs
sw.WriteLine("\t\t/// </remarks>");
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++;
}
}
}

View file

@ -15,7 +15,7 @@ namespace GtkSharp.Generation {
private static Hashtable handlers = new Hashtable (); 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"]; XmlElement ret_elem = sig["return-type"];
if (ret_elem == null) { if (ret_elem == null) {
@ -29,8 +29,8 @@ namespace GtkSharp.Generation {
return ""; return "";
} }
String s_ret = table.GetCSType(retval); String s_ret = SymbolTable.GetCSType(retval);
String p_ret = table.GetMarshalType(retval); String p_ret = SymbolTable.GetMarshalType(retval);
if ((s_ret == "") || (p_ret == "")) { if ((s_ret == "") || (p_ret == "")) {
Console.Write("Funky type: " + retval); Console.Write("Funky type: " + retval);
return ""; return "";
@ -38,7 +38,7 @@ namespace GtkSharp.Generation {
String key = retval; String key = retval;
String pinv = ""; String pinv = "";
String name = table.GetName(retval); String name = SymbolTable.GetName(retval);
int pcnt = 0; int pcnt = 0;
ArrayList parms = new ArrayList(); ArrayList parms = new ArrayList();
@ -54,7 +54,7 @@ namespace GtkSharp.Generation {
XmlElement elem = (XmlElement) parm; XmlElement elem = (XmlElement) parm;
String type = elem.GetAttribute("type"); String type = elem.GetAttribute("type");
String ptype = table.GetMarshalType(type); String ptype = SymbolTable.GetMarshalType(type);
if (ptype == "") { if (ptype == "") {
Console.Write("Funky type: " + type); Console.Write("Funky type: " + type);
return ""; return "";
@ -65,11 +65,11 @@ namespace GtkSharp.Generation {
} }
pinv += (ptype + " arg" + pcnt); pinv += (ptype + " arg" + pcnt);
parms.Add(type); parms.Add(type);
if (table.IsObject(type)) { if (SymbolTable.IsObject(type)) {
name += "Object"; name += "Object";
key += "Object"; key += "Object";
} else { } else {
name += table.GetName(type); name += SymbolTable.GetName(type);
key += type; key += type;
} }
pcnt++; pcnt++;
@ -136,7 +136,7 @@ namespace GtkSharp.Generation {
sw.WriteLine("\t\t\targs.Args = new object[" + (parms.Count-1) + "];"); sw.WriteLine("\t\t\targs.Args = new object[" + (parms.Count-1) + "];");
} }
for (int idx=1; idx < parms.Count; idx++) { 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.Write("\t\t\targs.Args[" + (idx-1) + "] ");
sw.WriteLine("= GLib.Object.GetObject(arg" + idx + ");"); sw.WriteLine("= GLib.Object.GetObject(arg" + idx + ");");
} else { } else {

View file

@ -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; String sig, isig, call, sigtypes;
XmlElement parms = ctor["parameters"]; XmlElement parms = ctor["parameters"];
@ -54,9 +54,9 @@ namespace GtkSharp.Generation {
call = sig = "()"; call = sig = "()";
isig = "();"; isig = "();";
sigtypes = ""; sigtypes = "";
} else if (GetSignature(parms, table, out sig, out sigtypes) && } else if (GetSignature(parms, out sig, out sigtypes) &&
GetImportSig(parms, table, out isig) && GetImportSig(parms, out isig) &&
GetCallString(parms, table, out call)) { GetCallString(parms, out call)) {
sig = "(" + sig + ")"; sig = "(" + sig + ")";
isig = "(" + isig + ");"; isig = "(" + isig + ");";
call = "(" + call + ")"; call = "(" + call + ")";
@ -75,7 +75,7 @@ namespace GtkSharp.Generation {
String cname = ctor.GetAttribute("cname"); String cname = ctor.GetAttribute("cname");
sw.WriteLine("\t\t[DllImport(\"" + table.GetDllName(ns) + sw.WriteLine("\t\t[DllImport(\"" + SymbolTable.GetDllName(ns) +
"\", CallingConvention=CallingConvention.Cdecl)]"); "\", CallingConvention=CallingConvention.Cdecl)]");
sw.WriteLine("\t\tstatic extern IntPtr " + cname + isig); sw.WriteLine("\t\tstatic extern IntPtr " + cname + isig);
sw.WriteLine(); sw.WriteLine();
@ -104,7 +104,7 @@ namespace GtkSharp.Generation {
return true; return true;
} }
protected bool GenField (XmlElement field, SymbolTable table, StreamWriter sw) protected bool GenField (XmlElement field, StreamWriter sw)
{ {
String c_type; String c_type;
@ -115,7 +115,7 @@ namespace GtkSharp.Generation {
} }
char[] ast = {'*'}; char[] ast = {'*'};
c_type = c_type.TrimEnd(ast); c_type = c_type.TrimEnd(ast);
String cs_type = table.GetCSType(c_type); String cs_type = SymbolTable.GetCSType(c_type);
if (cs_type == "") { if (cs_type == "") {
Console.WriteLine ("Field has unknown Type {0}", c_type); Console.WriteLine ("Field has unknown Type {0}", c_type);
@ -131,7 +131,7 @@ namespace GtkSharp.Generation {
return true; return true;
} }
protected bool GenMethod(XmlElement method, SymbolTable table, StreamWriter sw) protected bool GenMethod(XmlElement method, StreamWriter sw)
{ {
String sig, isig, call, sigtypes; String sig, isig, call, sigtypes;
XmlElement parms = method["parameters"]; XmlElement parms = method["parameters"];
@ -141,9 +141,9 @@ namespace GtkSharp.Generation {
sig = "()"; sig = "()";
isig = "(IntPtr raw);"; isig = "(IntPtr raw);";
sigtypes = ""; sigtypes = "";
} else if (GetSignature(parms, table, out sig, out sigtypes) && } else if (GetSignature(parms, out sig, out sigtypes) &&
GetImportSig(parms, table, out isig) && GetImportSig(parms, out isig) &&
GetCallString(parms, table, out call)) { GetCallString(parms, out call)) {
sig = "(" + sig + ")"; sig = "(" + sig + ")";
isig = "(IntPtr raw, " + isig + ");"; isig = "(IntPtr raw, " + isig + ");";
call = "(Handle, " + call + ")"; call = "(Handle, " + call + ")";
@ -162,8 +162,8 @@ namespace GtkSharp.Generation {
String rettype = ret_elem.GetAttribute("type"); String rettype = ret_elem.GetAttribute("type");
String m_ret = table.GetMarshalType(rettype); String m_ret = SymbolTable.GetMarshalType(rettype);
String s_ret = table.GetCSType(rettype); String s_ret = SymbolTable.GetCSType(rettype);
if (m_ret == "" || s_ret == "") { if (m_ret == "" || s_ret == "") {
Console.Write("rettype: " + rettype + " method "); Console.Write("rettype: " + rettype + " method ");
Statistics.ThrottledCount++; Statistics.ThrottledCount++;
@ -178,7 +178,7 @@ namespace GtkSharp.Generation {
return true; return true;
} }
sw.WriteLine("\t\t[DllImport(\"" + table.GetDllName(ns) + sw.WriteLine("\t\t[DllImport(\"" + SymbolTable.GetDllName(ns) +
"\", CallingConvention=CallingConvention.Cdecl)]"); "\", CallingConvention=CallingConvention.Cdecl)]");
sw.Write("\t\tstatic extern " + m_ret + " " + cname + isig); sw.Write("\t\tstatic extern " + m_ret + " " + cname + isig);
sw.WriteLine(); sw.WriteLine();
@ -189,7 +189,7 @@ namespace GtkSharp.Generation {
if (m_ret == "void") { if (m_ret == "void") {
sw.WriteLine(cname + call + ";"); sw.WriteLine(cname + call + ";");
} else { } else {
sw.WriteLine("return " + table.FromNative(rettype, cname + call) + ";"); sw.WriteLine("return " + SymbolTable.FromNative(rettype, cname + call) + ";");
} }
sw.WriteLine("\t\t}"); sw.WriteLine("\t\t}");
@ -199,7 +199,7 @@ namespace GtkSharp.Generation {
return true; return true;
} }
private bool GetCallString(XmlElement parms, SymbolTable table, out String call) private bool GetCallString(XmlElement parms, out String call)
{ {
call = ""; call = "";
@ -215,7 +215,7 @@ namespace GtkSharp.Generation {
String type = elem.GetAttribute("type"); String type = elem.GetAttribute("type");
String name = elem.GetAttribute("name"); String name = elem.GetAttribute("name");
name = MangleName(name); name = MangleName(name);
String call_parm = table.CallByName(type, name); String call_parm = SymbolTable.CallByName(type, name);
if (call_parm == "") { if (call_parm == "") {
Console.Write("Name: " + name + " Type: " + type + " "); Console.Write("Name: " + name + " Type: " + type + " ");
@ -233,7 +233,7 @@ namespace GtkSharp.Generation {
return true; return true;
} }
private bool GetImportSig(XmlElement parms, SymbolTable table, out String isig) private bool GetImportSig(XmlElement parms, out String isig)
{ {
isig = ""; isig = "";
@ -246,7 +246,7 @@ namespace GtkSharp.Generation {
XmlElement elem = (XmlElement) parm; XmlElement elem = (XmlElement) parm;
String type = elem.GetAttribute("type"); String type = elem.GetAttribute("type");
String m_type = table.GetMarshalType(type); String m_type = SymbolTable.GetMarshalType(type);
String name = elem.GetAttribute("name"); String name = elem.GetAttribute("name");
name = MangleName(name); name = MangleName(name);
@ -270,7 +270,7 @@ namespace GtkSharp.Generation {
return true; 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 = ""; sigtypes = sig = "";
bool need_comma = false; bool need_comma = false;
@ -282,7 +282,7 @@ namespace GtkSharp.Generation {
XmlElement elem = (XmlElement) parm; XmlElement elem = (XmlElement) parm;
String type = elem.GetAttribute("type"); String type = elem.GetAttribute("type");
String cs_type = table.GetCSType(type); String cs_type = SymbolTable.GetCSType(type);
String name = elem.GetAttribute("name"); String name = elem.GetAttribute("name");
name = MangleName(name); name = MangleName(name);

View file

@ -31,7 +31,7 @@ namespace GtkSharp.Generation {
return var; return var;
} }
public void Generate (SymbolTable table) public void Generate ()
{ {
char sep = Path.DirectorySeparatorChar; char sep = Path.DirectorySeparatorChar;
string dir = ".." + sep + ns.ToLower() + sep + "generated"; string dir = ".." + sep + ns.ToLower() + sep + "generated";
@ -66,7 +66,7 @@ namespace GtkSharp.Generation {
switch (node.Name) { switch (node.Name) {
case "field": case "field":
Statistics.IgnoreCount++; Statistics.IgnoreCount++;
// GenField(member, table, sw); // GenField(member, sw);
break; break;
case "callback": case "callback":

View file

@ -2,7 +2,7 @@
// //
// Author: Mike Kestner <mkestner@speakeasy.net> // Author: Mike Kestner <mkestner@speakeasy.net>
// //
// (c) 2001 Mike Kestner // (c) 2001-2002 Mike Kestner
namespace GtkSharp.Generation { namespace GtkSharp.Generation {
@ -11,11 +11,11 @@ namespace GtkSharp.Generation {
public class SymbolTable { public class SymbolTable {
private Hashtable complex_types = new Hashtable (); private static Hashtable complex_types = new Hashtable ();
private Hashtable simple_types; private static Hashtable simple_types;
private Hashtable dlls; private static Hashtable dlls;
public SymbolTable () static SymbolTable ()
{ {
simple_types = new Hashtable (); simple_types = new Hashtable ();
simple_types.Add ("void", "void"); simple_types.Add ("void", "void");
@ -72,32 +72,32 @@ namespace GtkSharp.Generation {
dlls.Add("Gtk", "gtk-x11-2.0"); dlls.Add("Gtk", "gtk-x11-2.0");
} }
public void AddType (IGeneratable gen) public static void AddType (IGeneratable gen)
{ {
complex_types [gen.CName] = gen; complex_types [gen.CName] = gen;
} }
public int Count { public static int Count {
get get
{ {
return complex_types.Count; return complex_types.Count;
} }
} }
public IEnumerable Generatables { public static IEnumerable Generatables {
get { get {
return complex_types.Values; return complex_types.Values;
} }
} }
private string Trim(string type) private static string Trim(string type)
{ {
string trim_type = type.TrimEnd('*'); string trim_type = type.TrimEnd('*');
if (trim_type.StartsWith("const-")) return trim_type.Substring(6); if (trim_type.StartsWith("const-")) return trim_type.Substring(6);
return trim_type; 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); c_type = Trim(c_type);
if (simple_types.ContainsKey(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); c_type = Trim(c_type);
if (simple_types.ContainsKey(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); c_type = Trim(c_type);
if (simple_types.ContainsKey(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]; return (string) dlls[ns];
} }
public string GetMarshalType(string c_type) public static string GetMarshalType(string c_type)
{ {
c_type = Trim(c_type); c_type = Trim(c_type);
if (simple_types.ContainsKey(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); c_type = Trim(c_type);
if (simple_types.ContainsKey(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); c_type = Trim(c_type);
if (complex_types.ContainsKey(c_type)) { if (complex_types.ContainsKey(c_type)) {
@ -185,7 +185,7 @@ namespace GtkSharp.Generation {
return false; return false;
} }
public bool IsEnum(string c_type) public static bool IsEnum(string c_type)
{ {
c_type = Trim(c_type); c_type = Trim(c_type);
if (complex_types.ContainsKey(c_type)) { if (complex_types.ContainsKey(c_type)) {
@ -197,7 +197,7 @@ namespace GtkSharp.Generation {
return false; return false;
} }
public bool IsInterface(string c_type) public static bool IsInterface(string c_type)
{ {
c_type = Trim(c_type); c_type = Trim(c_type);
if (complex_types.ContainsKey(c_type)) { if (complex_types.ContainsKey(c_type)) {
@ -209,7 +209,16 @@ namespace GtkSharp.Generation {
return false; 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); c_type = Trim(c_type);
if (complex_types.ContainsKey(c_type)) { if (complex_types.ContainsKey(c_type)) {