Ryujinx-GtkSharp/generator/Ctor.cs

224 lines
7.0 KiB
C#
Raw Normal View History

// GtkSharp.Generation.Ctor.cs - The Constructor Generation Class.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// Copyright (c) 2001-2003 Mike Kestner
// Copyright (c) 2004 Novell, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
namespace GtkSharp.Generation {
using System;
using System.Collections;
using System.IO;
using System.Xml;
public class Ctor {
private string libname;
private XmlElement elem;
private Parameters parms;
private Signature sig = null;
private ImportSignature isig = null;
private MethodBody body = null;
private bool preferred;
private string clashName = null;
private ClassBase container_type;
2002-07-30 Rachel Hestilow <hestilow@ximian.com> * generator/ClassBase.cs: Change hasDefaultConstructor to protected, adjust now that it is an attr and not a subnode. Also add virtual property AssignToName (for ctors). * generator/Ctor.cs: Add property ForceStatic. (Generate): Optimize return code a bit for the static case. * generator/Method.cs: Assign to a "raw_ret" pointer before calling FromNativeReturn. * generator/Parameters.cs: Change "out ref" to "out", not "ref". * generator/Property.cs: Fix to work correctly with all object and struct types (mostly just some if-cases added). * generator/SignalHandler.cs: Remove args_type and argfields (unused). (Generate): Initialize struct if necessary. * generator/StructBase.cs: Massive reworking to support methods, ctors, etc. * generator/SymbolTable.cs: Add GdkAtom and gconstpointer simple types. * glib/Boxed.cs: Accept both IntPtr and object ctors. Add access for both. * glib/Opaque.cs: Fix copy/pasted copyright notice, remove data and event fields. Fix docs. * glib/Value.cs: Work correctly with boxed properties. * gnome/Modules.cs: Use new struct ctors. * gnome/Program.custom: Remove Get, this is being generated now. * parser/Gdk.metadata: Fix the drawable classes to inherit correctly. * parser/Metadata.pm: Change per-class attributes to actually be attributes. * parser/Gtk.metadata: Add a dummy attribute value for disabledefaultctor. * parser/gapi2xml.pl: Add hacks for the (broken) Drawable and Bitmap typedefs. * sample/test/TestColorSelection.cs: Display color string in hex format, update to use IsNull instead of == null, and size dialog to look pretty. * sample/Size.cs: Added. svn path=/trunk/gtk-sharp/; revision=6264
2002-07-31 01:02:12 +02:00
private bool force_static;
private bool needs_chaining = false;
public bool Preferred {
get { return preferred; }
set { preferred = value; }
}
2002-07-30 Rachel Hestilow <hestilow@ximian.com> * generator/ClassBase.cs: Change hasDefaultConstructor to protected, adjust now that it is an attr and not a subnode. Also add virtual property AssignToName (for ctors). * generator/Ctor.cs: Add property ForceStatic. (Generate): Optimize return code a bit for the static case. * generator/Method.cs: Assign to a "raw_ret" pointer before calling FromNativeReturn. * generator/Parameters.cs: Change "out ref" to "out", not "ref". * generator/Property.cs: Fix to work correctly with all object and struct types (mostly just some if-cases added). * generator/SignalHandler.cs: Remove args_type and argfields (unused). (Generate): Initialize struct if necessary. * generator/StructBase.cs: Massive reworking to support methods, ctors, etc. * generator/SymbolTable.cs: Add GdkAtom and gconstpointer simple types. * glib/Boxed.cs: Accept both IntPtr and object ctors. Add access for both. * glib/Opaque.cs: Fix copy/pasted copyright notice, remove data and event fields. Fix docs. * glib/Value.cs: Work correctly with boxed properties. * gnome/Modules.cs: Use new struct ctors. * gnome/Program.custom: Remove Get, this is being generated now. * parser/Gdk.metadata: Fix the drawable classes to inherit correctly. * parser/Metadata.pm: Change per-class attributes to actually be attributes. * parser/Gtk.metadata: Add a dummy attribute value for disabledefaultctor. * parser/gapi2xml.pl: Add hacks for the (broken) Drawable and Bitmap typedefs. * sample/test/TestColorSelection.cs: Display color string in hex format, update to use IsNull instead of == null, and size dialog to look pretty. * sample/Size.cs: Added. svn path=/trunk/gtk-sharp/; revision=6264
2002-07-31 01:02:12 +02:00
public bool ForceStatic {
get { return force_static; }
set { force_static = value; }
}
public Parameters Params {
get { return parms; }
}
public Ctor (string libname, XmlElement elem, ClassBase container_type) {
this.libname = libname;
this.elem = elem;
this.container_type = container_type;
XmlElement parms_elem = elem ["parameters"];
if (parms_elem != null)
parms = new Parameters (parms_elem, container_type.NS);
if (elem.HasAttribute ("preferred"))
preferred = true;
if (container_type is ObjectGen)
needs_chaining = true;
}
public bool Validate ()
{
2002-06-21 Rachel Hestilow <hestilow@ximian.com> * generator/ClassBase.cs: New base class for classes and interfaces. * generator/InterfaceGen.cs: Inherit from ClassBase, generate declarations. * generator/ObjectGen.cs: Move half of this into ClassBase. * generator/Method.cs: Turn all applicable Get/Set functions into .NET accessors. Remove redundant == overload and move into Equals, as it was confusing "!= null". * generator/Parameters.cs: Alter signature creation to accept "is_set" option, add support for variable arguments. Add properties "Count", "IsVarArgs", "VAType". * generator/Ctor.cs: Fixup for changes in Parameters (indenting, signature creation). * generator/Signal.cs: Support generating declarations. * generator/SymbolTable: Change GetObjectGen to GetClassGen. * glib/IWrapper.cs: Move "Handle" declaration to here, so both classes and interfaces can benefit from it. * glib/Object.cs: Inherit from IWrapper.cs * parser/Metadata.pm: Support attribute changes on constructors, methods, signals, and paramater lists. * parser/gapi2xml.pl: Parse init funcs for interfaces. Ignore "_" functions here. * parser/gapi_pp.pl: Remove boxed_type_register check, as it will be caught in the init funcs. * parser/Atk.metadata: Added. * parser/Gtk.metadata: Add all needed signal/method collision renames. Rename GtkEditable.Editable accessors to IsEditable, as .NET does not like accessors with the same name as their declaring type. Tag TreeStore constructor as varargs. * samples/ButtonApp.cs: s/EmitAdd/Add. * samples/Menu.cs: s/EmitAdd/Add, s/Activate/Activated. svn path=/trunk/gtk-sharp/; revision=5394
2002-06-21 19:15:19 +02:00
if (parms != null) {
if (!parms.Validate ()) {
Console.Write("in ctor ");
2002-06-21 Rachel Hestilow <hestilow@ximian.com> * generator/ClassBase.cs: New base class for classes and interfaces. * generator/InterfaceGen.cs: Inherit from ClassBase, generate declarations. * generator/ObjectGen.cs: Move half of this into ClassBase. * generator/Method.cs: Turn all applicable Get/Set functions into .NET accessors. Remove redundant == overload and move into Equals, as it was confusing "!= null". * generator/Parameters.cs: Alter signature creation to accept "is_set" option, add support for variable arguments. Add properties "Count", "IsVarArgs", "VAType". * generator/Ctor.cs: Fixup for changes in Parameters (indenting, signature creation). * generator/Signal.cs: Support generating declarations. * generator/SymbolTable: Change GetObjectGen to GetClassGen. * glib/IWrapper.cs: Move "Handle" declaration to here, so both classes and interfaces can benefit from it. * glib/Object.cs: Inherit from IWrapper.cs * parser/Metadata.pm: Support attribute changes on constructors, methods, signals, and paramater lists. * parser/gapi2xml.pl: Parse init funcs for interfaces. Ignore "_" functions here. * parser/gapi_pp.pl: Remove boxed_type_register check, as it will be caught in the init funcs. * parser/Atk.metadata: Added. * parser/Gtk.metadata: Add all needed signal/method collision renames. Rename GtkEditable.Editable accessors to IsEditable, as .NET does not like accessors with the same name as their declaring type. Tag TreeStore constructor as varargs. * samples/ButtonApp.cs: s/EmitAdd/Add. * samples/Menu.cs: s/EmitAdd/Add, s/Activate/Activated. svn path=/trunk/gtk-sharp/; revision=5394
2002-06-21 19:15:19 +02:00
Statistics.ThrottledCount++;
return false;
}
}
sig = new Signature (parms);
isig = new ImportSignature (parms, container_type.NS);
body = new MethodBody (parms, container_type.NS);
return true;
}
public void InitClashMap (Hashtable clash_map)
{
if (clash_map.ContainsKey (sig.Types)) {
int num = (int) clash_map[sig.Types];
clash_map[sig.Types] = ++num;
}
else
clash_map[sig.Types] = 0;
}
public void Initialize (Hashtable clash_map)
{
int clashes = (int) clash_map[sig.Types];
string cname = elem.GetAttribute("cname");
2002-07-30 Rachel Hestilow <hestilow@ximian.com> * generator/ClassBase.cs: Change hasDefaultConstructor to protected, adjust now that it is an attr and not a subnode. Also add virtual property AssignToName (for ctors). * generator/Ctor.cs: Add property ForceStatic. (Generate): Optimize return code a bit for the static case. * generator/Method.cs: Assign to a "raw_ret" pointer before calling FromNativeReturn. * generator/Parameters.cs: Change "out ref" to "out", not "ref". * generator/Property.cs: Fix to work correctly with all object and struct types (mostly just some if-cases added). * generator/SignalHandler.cs: Remove args_type and argfields (unused). (Generate): Initialize struct if necessary. * generator/StructBase.cs: Massive reworking to support methods, ctors, etc. * generator/SymbolTable.cs: Add GdkAtom and gconstpointer simple types. * glib/Boxed.cs: Accept both IntPtr and object ctors. Add access for both. * glib/Opaque.cs: Fix copy/pasted copyright notice, remove data and event fields. Fix docs. * glib/Value.cs: Work correctly with boxed properties. * gnome/Modules.cs: Use new struct ctors. * gnome/Program.custom: Remove Get, this is being generated now. * parser/Gdk.metadata: Fix the drawable classes to inherit correctly. * parser/Metadata.pm: Change per-class attributes to actually be attributes. * parser/Gtk.metadata: Add a dummy attribute value for disabledefaultctor. * parser/gapi2xml.pl: Add hacks for the (broken) Drawable and Bitmap typedefs. * sample/test/TestColorSelection.cs: Display color string in hex format, update to use IsNull instead of == null, and size dialog to look pretty. * sample/Size.cs: Added. svn path=/trunk/gtk-sharp/; revision=6264
2002-07-31 01:02:12 +02:00
if (force_static || (clashes > 0 && !Preferred)) {
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);
}
clashName = mname + "(" + sig.ToString () + ")";
}
}
2003-10-05 02:20:17 +02:00
public void Generate (GenerationInfo gen_info)
{
2003-10-05 02:20:17 +02:00
StreamWriter sw = gen_info.Writer;
string cname = elem.GetAttribute("cname");
string name = ((XmlElement)elem.ParentNode).GetAttribute("name");
string safety;
if (body.ThrowsException)
safety = "unsafe ";
else
safety = "";
SymbolTable table = SymbolTable.Table;
sw.WriteLine("\t\t[DllImport(\"" + libname + "\")]");
sw.WriteLine("\t\tstatic extern " + safety + "IntPtr " + cname + "(" + isig.ToString () + ");");
sw.WriteLine();
if (clashName != null) {
string modifiers = "";
Ctor dup = null;
ObjectGen parent = (ObjectGen) container_type.Parent;
while (dup == null && parent != null) {
foreach (Ctor c in parent.Ctors) {
if (c.clashName == clashName) {
dup = c;
modifiers = "new ";
break;
}
}
parent = (ObjectGen) parent.Parent;
}
sw.WriteLine("\t\tpublic static " + safety + modifiers + name + " " + clashName);
sw.WriteLine("\t\t{");
2002-07-30 Rachel Hestilow <hestilow@ximian.com> * generator/ClassBase.cs: Change hasDefaultConstructor to protected, adjust now that it is an attr and not a subnode. Also add virtual property AssignToName (for ctors). * generator/Ctor.cs: Add property ForceStatic. (Generate): Optimize return code a bit for the static case. * generator/Method.cs: Assign to a "raw_ret" pointer before calling FromNativeReturn. * generator/Parameters.cs: Change "out ref" to "out", not "ref". * generator/Property.cs: Fix to work correctly with all object and struct types (mostly just some if-cases added). * generator/SignalHandler.cs: Remove args_type and argfields (unused). (Generate): Initialize struct if necessary. * generator/StructBase.cs: Massive reworking to support methods, ctors, etc. * generator/SymbolTable.cs: Add GdkAtom and gconstpointer simple types. * glib/Boxed.cs: Accept both IntPtr and object ctors. Add access for both. * glib/Opaque.cs: Fix copy/pasted copyright notice, remove data and event fields. Fix docs. * glib/Value.cs: Work correctly with boxed properties. * gnome/Modules.cs: Use new struct ctors. * gnome/Program.custom: Remove Get, this is being generated now. * parser/Gdk.metadata: Fix the drawable classes to inherit correctly. * parser/Metadata.pm: Change per-class attributes to actually be attributes. * parser/Gtk.metadata: Add a dummy attribute value for disabledefaultctor. * parser/gapi2xml.pl: Add hacks for the (broken) Drawable and Bitmap typedefs. * sample/test/TestColorSelection.cs: Display color string in hex format, update to use IsNull instead of == null, and size dialog to look pretty. * sample/Size.cs: Added. svn path=/trunk/gtk-sharp/; revision=6264
2002-07-31 01:02:12 +02:00
body.Initialize(gen_info, false, false, "");
2002-07-30 Rachel Hestilow <hestilow@ximian.com> * generator/ClassBase.cs: Change hasDefaultConstructor to protected, adjust now that it is an attr and not a subnode. Also add virtual property AssignToName (for ctors). * generator/Ctor.cs: Add property ForceStatic. (Generate): Optimize return code a bit for the static case. * generator/Method.cs: Assign to a "raw_ret" pointer before calling FromNativeReturn. * generator/Parameters.cs: Change "out ref" to "out", not "ref". * generator/Property.cs: Fix to work correctly with all object and struct types (mostly just some if-cases added). * generator/SignalHandler.cs: Remove args_type and argfields (unused). (Generate): Initialize struct if necessary. * generator/StructBase.cs: Massive reworking to support methods, ctors, etc. * generator/SymbolTable.cs: Add GdkAtom and gconstpointer simple types. * glib/Boxed.cs: Accept both IntPtr and object ctors. Add access for both. * glib/Opaque.cs: Fix copy/pasted copyright notice, remove data and event fields. Fix docs. * glib/Value.cs: Work correctly with boxed properties. * gnome/Modules.cs: Use new struct ctors. * gnome/Program.custom: Remove Get, this is being generated now. * parser/Gdk.metadata: Fix the drawable classes to inherit correctly. * parser/Metadata.pm: Change per-class attributes to actually be attributes. * parser/Gtk.metadata: Add a dummy attribute value for disabledefaultctor. * parser/gapi2xml.pl: Add hacks for the (broken) Drawable and Bitmap typedefs. * sample/test/TestColorSelection.cs: Display color string in hex format, update to use IsNull instead of == null, and size dialog to look pretty. * sample/Size.cs: Added. svn path=/trunk/gtk-sharp/; revision=6264
2002-07-31 01:02:12 +02:00
sw.Write("\t\t\treturn ");
if (container_type is StructBase)
sw.Write ("{0}.New (", name);
else
sw.Write ("new {0} (", name);
sw.WriteLine (cname + "(" + body.GetCallString (false) + "));");
} else {
sw.WriteLine("\t\tpublic {0}{1} ({2}) {3}", safety, name, sig.ToString(), needs_chaining ? ": base (IntPtr.Zero)" : "");
sw.WriteLine("\t\t{");
2002-07-30 Rachel Hestilow <hestilow@ximian.com> * generator/ClassBase.cs: Change hasDefaultConstructor to protected, adjust now that it is an attr and not a subnode. Also add virtual property AssignToName (for ctors). * generator/Ctor.cs: Add property ForceStatic. (Generate): Optimize return code a bit for the static case. * generator/Method.cs: Assign to a "raw_ret" pointer before calling FromNativeReturn. * generator/Parameters.cs: Change "out ref" to "out", not "ref". * generator/Property.cs: Fix to work correctly with all object and struct types (mostly just some if-cases added). * generator/SignalHandler.cs: Remove args_type and argfields (unused). (Generate): Initialize struct if necessary. * generator/StructBase.cs: Massive reworking to support methods, ctors, etc. * generator/SymbolTable.cs: Add GdkAtom and gconstpointer simple types. * glib/Boxed.cs: Accept both IntPtr and object ctors. Add access for both. * glib/Opaque.cs: Fix copy/pasted copyright notice, remove data and event fields. Fix docs. * glib/Value.cs: Work correctly with boxed properties. * gnome/Modules.cs: Use new struct ctors. * gnome/Program.custom: Remove Get, this is being generated now. * parser/Gdk.metadata: Fix the drawable classes to inherit correctly. * parser/Metadata.pm: Change per-class attributes to actually be attributes. * parser/Gtk.metadata: Add a dummy attribute value for disabledefaultctor. * parser/gapi2xml.pl: Add hacks for the (broken) Drawable and Bitmap typedefs. * sample/test/TestColorSelection.cs: Display color string in hex format, update to use IsNull instead of == null, and size dialog to look pretty. * sample/Size.cs: Added. svn path=/trunk/gtk-sharp/; revision=6264
2002-07-31 01:02:12 +02:00
if (needs_chaining) {
sw.WriteLine ("\t\t\tif (GetType () != typeof (" + name + ")) {");
if (Params == null || Params.Count == 0) {
sw.WriteLine ("\t\t\t\tCreateNativeObject (new string [0], new GLib.Value[0]);");
sw.WriteLine ("\t\t\t\treturn;");
} else {
ArrayList names = new ArrayList ();
ArrayList values = new ArrayList ();
for (int i = 0; i < Params.Count; i++) {
Parameter p = Params[i];
if (container_type.GetPropertyRecursively (p.StudlyName) != null) {
names.Add (p.Name);
values.Add (p.Name);
} else if (p.PropertyName != String.Empty) {
names.Add (p.PropertyName);
values.Add (p.Name);
}
}
if (names.Count == Params.Count) {
sw.WriteLine ("\t\t\t\tArrayList vals = new ArrayList();");
sw.WriteLine ("\t\t\t\tArrayList names = new ArrayList();");
for (int i = 0; i < names.Count; i++) {
Parameter p = Params [i];
string indent = "\t\t\t\t";
if (p.NullOk && p.Generatable is ObjectGen) {
sw.WriteLine (indent + "if (" + p.Name + " != null) {");
indent += "\t";
}
sw.WriteLine (indent + "names.Add (\"" + names [i] + "\");");
sw.Write (indent + "vals.Add (");
if (table.IsEnum (p.CType))
sw.WriteLine ("new GLib.Value (this, \"" + names[i] + "\", new GLib.EnumWrapper ((int)" + values[i] + ", " + (table.IsEnumFlags (p.CType) ? "true" : "false") + ")));");
else
sw.WriteLine ("new GLib.Value (" + values[i] + "));");
if (p.NullOk && p.Generatable is ObjectGen)
sw.WriteLine ("\t\t\t\t}");
}
sw.WriteLine ("\t\t\t\tCreateNativeObject ((string[])names.ToArray (typeof (string)), (GLib.Value[])vals.ToArray (typeof (GLib.Value)));");
sw.WriteLine ("\t\t\t\treturn;");
} else
sw.WriteLine ("\t\t\t\tthrow new InvalidOperationException (\"Can't override this constructor.\");");
}
sw.WriteLine ("\t\t\t}");
}
body.Initialize(gen_info, false, false, "");
sw.WriteLine("\t\t\t{0} = {1}({2});", container_type.AssignToName, cname, body.GetCallString (false));
body.HandleException (sw, "");
}
sw.WriteLine("\t\t}");
sw.WriteLine();
Statistics.CtorCount++;
}
}
}