GtkSharp/generator/Method.cs
Mike Kestner 6d30cf0c3e 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
2002-05-23 23:43:25 +00:00

152 lines
3.3 KiB
C#

// 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++;
}
}
}