From 02fa6a31e6c453610593677f97724a958abf5300 Mon Sep 17 00:00:00 2001 From: Mike Kestner Date: Sat, 5 Jan 2002 12:45:55 +0000 Subject: [PATCH] 2002-01-05 Mike Kestner * generator/*.cs : Move into GtkSharp.Generation namespace. * generator/CodeGenerator.cs (Main): Add usage check. Add SymbolTable. * generator/EnumGen.cs (QualifiedName): New. (Generate): Add SymbolTable to signature. * generator/IGeneratable : Add QualifiedName prop and update Generate signature. * generator/Parser.cs : Switch from plain Hashtable to SymbolTable. (Parse): Replaces the Types property and returns a SymbolTable. * generator/StructBase.cs : New base class to derive struct and object types. Initial implementation of protected GenField method and ctor. * generator/StructGen.cs : New non-object struct type generatable. * generator/SymbolTable.cs : New. Manages complex types hash and a simple types hash. Will provide generic lookup interface. svn path=/trunk/gtk-sharp/; revision=1855 --- ChangeLog | 16 ++++++ generator/CodeGenerator.cs | 19 ++++--- generator/EnumGen.cs | 15 ++++-- generator/IGeneratable.cs | 8 +-- generator/Parser.cs | 53 +++++++++---------- generator/StructBase.cs | 37 +++++++++++++ generator/StructGen.cs | 106 +++++++++++++++++++++++++++++++++++++ generator/SymbolTable.cs | 41 ++++++++++++++ 8 files changed, 252 insertions(+), 43 deletions(-) create mode 100644 generator/StructBase.cs create mode 100644 generator/StructGen.cs create mode 100644 generator/SymbolTable.cs diff --git a/ChangeLog b/ChangeLog index d372fb312..933a78277 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2002-01-05 Mike Kestner + + * generator/*.cs : Move into GtkSharp.Generation namespace. + * generator/CodeGenerator.cs (Main): Add usage check. Add SymbolTable. + * generator/EnumGen.cs (QualifiedName): New. + (Generate): Add SymbolTable to signature. + * generator/IGeneratable : Add QualifiedName prop and update Generate + signature. + * generator/Parser.cs : Switch from plain Hashtable to SymbolTable. + (Parse): Replaces the Types property and returns a SymbolTable. + * generator/StructBase.cs : New base class to derive struct and object + types. Initial implementation of protected GenField method and ctor. + * generator/StructGen.cs : New non-object struct type generatable. + * generator/SymbolTable.cs : New. Manages complex types hash and a + simple types hash. Will provide generic lookup interface. + 2002-01-04 Mike Kestner * makefile : switch to the new generator. diff --git a/generator/CodeGenerator.cs b/generator/CodeGenerator.cs index 6db8c4093..b720ba91c 100644 --- a/generator/CodeGenerator.cs +++ b/generator/CodeGenerator.cs @@ -1,10 +1,10 @@ -// GtkSharp.CodeGenerator.cs - The main code generation engine. +// GtkSharp.Generation.CodeGenerator.cs - The main code generation engine. // // Author: Mike Kestner // // (c) 2001 Mike Kestner -namespace GtkSharp { +namespace GtkSharp.Generation { using System; using System.Collections; @@ -14,14 +14,19 @@ namespace GtkSharp { public static int Main (string[] args) { - Parser p = new Parser (args[0]); - Hashtable types = p.Types; - Console.WriteLine (types.Count); + if (args.Length != 1) { + Console.WriteLine ("Usage: codegen "); + return 0; + } - IDictionaryEnumerator de = types.GetEnumerator(); + Parser p = new Parser (args[0]); + SymbolTable table = p.Parse (); + Console.WriteLine (table.Count + " types parsed."); + + IDictionaryEnumerator de = table.GetEnumerator(); while (de.MoveNext()) { IGeneratable gen = (IGeneratable) de.Value; - gen.Generate (); + gen.Generate (table); } return 0; diff --git a/generator/EnumGen.cs b/generator/EnumGen.cs index 3c22f3e27..e8f0b1bfd 100644 --- a/generator/EnumGen.cs +++ b/generator/EnumGen.cs @@ -1,10 +1,10 @@ -// GtkSharp.EnumGen.cs - The Enumeration Generatable. +// GtkSharp.Generation.EnumGen.cs - The Enumeration Generatable. // // Author: Mike Kestner // // (c) 2001 Mike Kestner -namespace GtkSharp { +namespace GtkSharp.Generation { using System; using System.IO; @@ -35,6 +35,13 @@ namespace GtkSharp { } } + public String QualifiedName { + get + { + return ns + "." + elem.GetAttribute("cname"); + } + } + public String MarshalType { get { @@ -47,7 +54,7 @@ namespace GtkSharp { return "(int) " + var_name; } - public void Generate () + public void Generate (SymbolTable table) { String filename = "..\\" + ns.ToLower() + "\\generated\\" + Name + ".cs"; @@ -58,7 +65,7 @@ namespace GtkSharp { sw.WriteLine ("// 2001 Mike Kestner"); sw.WriteLine (); - sw.WriteLine ("namespace " + ns + "{"); + sw.WriteLine ("namespace " + ns + " {"); sw.WriteLine (); if (elem.GetAttribute("type") == "flags") { diff --git a/generator/IGeneratable.cs b/generator/IGeneratable.cs index 161934be0..42c1555df 100644 --- a/generator/IGeneratable.cs +++ b/generator/IGeneratable.cs @@ -1,10 +1,10 @@ -// GtkSharp.IGeneratable.cs - Interface to generate code for a type. +// GtkSharp.Generation.IGeneratable.cs - Interface to generate code for a type. // // Author: Mike Kestner // // (c) 2001 Mike Kestner -namespace GtkSharp { +namespace GtkSharp.Generation { using System; @@ -16,8 +16,10 @@ namespace GtkSharp { String Name {get;} + String QualifiedName {get;} + String CallByName (String var_name); - void Generate (); + void Generate (SymbolTable table); } } diff --git a/generator/Parser.cs b/generator/Parser.cs index 253402c00..c2555bf10 100644 --- a/generator/Parser.cs +++ b/generator/Parser.cs @@ -1,10 +1,10 @@ -// GtkSharp.Parser.cs - The XML Parsing engine. +// GtkSharp.Generation.Parser.cs - The XML Parsing engine. // // Author: Mike Kestner // // (c) 2001 Mike Kestner -namespace GtkSharp { +namespace GtkSharp.Generation { using System; using System.Collections; @@ -13,7 +13,7 @@ namespace GtkSharp { public class Parser { private XmlDocument doc; - private Hashtable types; + private SymbolTable table; public Parser (String filename) { @@ -30,33 +30,30 @@ namespace GtkSharp { } } - - public Hashtable Types { - get - { - if (types != null) return types; + public SymbolTable 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."); return null; - } - - types = new Hashtable (); - - foreach (XmlNode ns in root.ChildNodes) { - if (ns.Name != "namespace") { - continue; - } - - XmlElement elem = (XmlElement) ns; - ParseNamespace (elem); - } - - return types; } + + table = new SymbolTable (); + + foreach (XmlNode ns in root.ChildNodes) { + if (ns.Name != "namespace") { + continue; + } + + XmlElement elem = (XmlElement) ns; + ParseNamespace (elem); + } + + return table; } private void ParseNamespace (XmlElement ns) @@ -70,7 +67,7 @@ namespace GtkSharp { } XmlElement elem = (XmlElement) def; - + switch (def.Name) { case "alias": @@ -80,14 +77,14 @@ namespace GtkSharp { break; case "enum": - IGeneratable gen = new EnumGen (ns_name, elem); - types [gen.CName] = gen; + table.AddType (new EnumGen (ns_name, elem)); break; case "object": break; case "struct": + table.AddType (new StructGen (ns_name, elem)); break; default: @@ -95,8 +92,6 @@ namespace GtkSharp { break; } } - } - } } diff --git a/generator/StructBase.cs b/generator/StructBase.cs new file mode 100644 index 000000000..3ec663b29 --- /dev/null +++ b/generator/StructBase.cs @@ -0,0 +1,37 @@ +// GtkSharp.Generation.StructBase.cs - The Structure/Object Base Class. +// +// Author: Mike Kestner +// +// (c) 2001 Mike Kestner + +namespace GtkSharp.Generation { + + using System; + using System.IO; + using System.Xml; + + public class StructBase { + + protected String ns; + protected XmlElement elem; + + public StructBase (String ns, XmlElement elem) { + + this.ns = ns; + this.elem = elem; + } + + protected void GenField (XmlElement field, SymbolTable table, StreamWriter sw) + { + String c_type = field.GetAttribute("type"); + sw.Write ("\t\t" + table.GetCSType(c_type)); + if (field.HasAttribute("array_len")) { + sw.Write ("[]"); + } + + sw.WriteLine (" " + field.GetAttribute("cname") + ";"); + } + + } +} + diff --git a/generator/StructGen.cs b/generator/StructGen.cs new file mode 100644 index 000000000..38a32834e --- /dev/null +++ b/generator/StructGen.cs @@ -0,0 +1,106 @@ +// GtkSharp.Generation.StructGen.cs - The Structure Generatable. +// +// Author: Mike Kestner +// +// (c) 2001 Mike Kestner + +namespace GtkSharp.Generation { + + using System; + using System.IO; + using System.Xml; + + public class StructGen : StructBase, IGeneratable { + + public StructGen (String ns, XmlElement elem) : base (ns, elem) {} + + public String Name { + get + { + return elem.GetAttribute("name"); + } + } + + public String QualifiedName { + get + { + return ns + "." + elem.GetAttribute("name"); + } + } + + public String CName { + get + { + return elem.GetAttribute("cname"); + } + } + + public String MarshalType { + get + { + return "IntPtr"; + } + } + + public String CallByName (String var_name) + { + return var_name; + } + + public void Generate (SymbolTable table) + { + String filename = "..\\" + ns.ToLower() + "\\generated\\" + Name + ".cs"; + + FileStream stream = new FileStream (filename, FileMode.OpenOrCreate, 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 (); + + sw.WriteLine ("\tusing System;"); + sw.WriteLine ("\tusing System.Collections;"); + sw.WriteLine ("\tusing System.Runtime.InteropServices;"); + sw.WriteLine (); + + sw.WriteLine ("\t[StructLayout(LayoutKind.Sequential)]"); + sw.WriteLine ("\tpublic class " + Name + " {"); + sw.WriteLine (); + + foreach (XmlNode node in elem.ChildNodes) { + + XmlElement member = (XmlElement) node; + + switch (node.Name) { + case "field": + break; + + case "callback": + break; + + case "constructor": + break; + + case "method": + break; + + default: + Console.WriteLine ("Unexpected node"); + break; + } + + } + + sw.WriteLine ("\t}"); + sw.WriteLine (); + sw.WriteLine ("}"); + + sw.Flush(); + sw.Close(); + } + } +} + diff --git a/generator/SymbolTable.cs b/generator/SymbolTable.cs new file mode 100644 index 000000000..398b542d7 --- /dev/null +++ b/generator/SymbolTable.cs @@ -0,0 +1,41 @@ +// GtkSharp.Generation.SymbolTable.cs - The Symbol Table Class. +// +// Author: Mike Kestner +// +// (c) 2001 Mike Kestner + +namespace GtkSharp.Generation { + + using System; + using System.Collections; + + public class SymbolTable { + + private Hashtable complex_types = new Hashtable (); + private Hashtable simple_types; + + public void AddType (IGeneratable gen) + { + complex_types [gen.CName] = gen; + } + + public int Count { + get + { + return complex_types.Count; + } + } + + public IDictionaryEnumerator GetEnumerator () + { + return complex_types.GetEnumerator(); + } + + public String GetCSType (String c_type) + { + return ""; + } + + } +} +