2004-01-19 05:30:05 +00:00
|
|
|
// GtkSharp.Generation.Field.cs - The Field generation Class.
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@ximian.com>
|
|
|
|
//
|
2004-06-25 16:35:15 +00:00
|
|
|
// 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.
|
|
|
|
|
2004-01-19 05:30:05 +00:00
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
public class Field {
|
|
|
|
|
|
|
|
public static int bitfields;
|
|
|
|
|
|
|
|
XmlElement elem;
|
|
|
|
|
|
|
|
public Field (XmlElement elem)
|
|
|
|
{
|
|
|
|
this.elem = elem;
|
|
|
|
}
|
|
|
|
|
2004-02-10 16:04:50 +00:00
|
|
|
public string Access {
|
|
|
|
get {
|
|
|
|
return elem.HasAttribute ("access") ? elem.GetAttribute ("access") : "public";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-19 17:05:53 +00:00
|
|
|
public int ArrayLength {
|
|
|
|
get {
|
|
|
|
if (!IsArray)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int result;
|
|
|
|
try {
|
|
|
|
result = Int32.Parse (elem.GetAttribute("array_len"));
|
|
|
|
} catch (Exception e) {
|
|
|
|
Console.Write ("Non-numeric array_len: " + elem.GetAttribute("array_len"));
|
|
|
|
Console.WriteLine (" warning: array field {0} incorrectly generated", Name);
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-19 05:30:05 +00:00
|
|
|
public string CSType {
|
|
|
|
get {
|
|
|
|
string type = SymbolTable.Table.GetCSType (CType);
|
|
|
|
if (IsArray)
|
2004-01-19 17:05:53 +00:00
|
|
|
type += "[]";
|
2004-01-19 05:30:05 +00:00
|
|
|
else if (IsBit)
|
|
|
|
type = "uint";
|
|
|
|
else if ((IsPointer || SymbolTable.Table.IsOpaque (CType)) && type != "string")
|
|
|
|
type = "IntPtr";
|
|
|
|
else if (SymbolTable.Table.IsCallback (CType))
|
|
|
|
type = "IntPtr";
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string CType {
|
|
|
|
get {
|
|
|
|
return elem.GetAttribute ("type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-02 20:28:42 +00:00
|
|
|
public bool Hidden {
|
|
|
|
get {
|
|
|
|
return elem.HasAttribute("hidden");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-19 05:30:05 +00:00
|
|
|
public bool IsArray {
|
|
|
|
get {
|
|
|
|
return elem.HasAttribute("array_len");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsBit {
|
|
|
|
get {
|
|
|
|
return (elem.HasAttribute("bits") && (elem.GetAttribute("bits") == "1"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsPadding {
|
|
|
|
get {
|
|
|
|
string c_name = elem.GetAttribute ("cname");
|
2004-01-21 20:47:41 +00:00
|
|
|
return (c_name.StartsWith ("dummy") || c_name.StartsWith ("padding"));
|
2004-01-19 05:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsPointer {
|
|
|
|
get {
|
|
|
|
return (CType.EndsWith ("*") || CType.EndsWith ("pointer"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Name {
|
|
|
|
get {
|
|
|
|
string result = "";
|
|
|
|
if ((IsPointer || SymbolTable.Table.IsOpaque (CType)) && CSType != "string")
|
|
|
|
result = "_";
|
|
|
|
|
|
|
|
if (IsBit)
|
|
|
|
result = String.Format ("_bitfield{0}", bitfields++);
|
|
|
|
else
|
|
|
|
result += SymbolTable.Table.MangleName (elem.GetAttribute ("cname"));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-21 20:47:41 +00:00
|
|
|
public string StudlyName {
|
2004-01-19 05:30:05 +00:00
|
|
|
get {
|
2004-12-17 20:29:54 +00:00
|
|
|
string studly = elem.GetAttribute ("name");
|
|
|
|
if (studly != "")
|
|
|
|
return studly;
|
|
|
|
|
|
|
|
// FIXME: this is backward compatibility for API files
|
|
|
|
// output by older versions of the parser. It can go
|
|
|
|
// away at some point.
|
|
|
|
string name = elem.GetAttribute ("cname");
|
|
|
|
string[] segs = name.Split('_');
|
|
|
|
foreach (string s in segs) {
|
|
|
|
if (s.Trim () == "")
|
|
|
|
continue;
|
|
|
|
studly += (s.Substring(0,1).ToUpper() + s.Substring(1));
|
|
|
|
}
|
|
|
|
return studly;
|
2004-01-19 05:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Generate (StreamWriter sw)
|
|
|
|
{
|
2004-06-02 20:28:42 +00:00
|
|
|
if (Hidden)
|
|
|
|
return true;
|
|
|
|
|
2004-01-19 05:30:05 +00:00
|
|
|
if (CSType == "") {
|
|
|
|
Console.WriteLine ("Field has unknown Type {0}", CType);
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
|
2004-01-19 17:05:53 +00:00
|
|
|
if (IsArray)
|
|
|
|
sw.WriteLine ("\t\t[MarshalAs (UnmanagedType.ByValArray, SizeConst=" + ArrayLength + ")]");
|
2004-01-19 05:30:05 +00:00
|
|
|
|
|
|
|
string wrapped = table.GetCSType (CType);
|
|
|
|
string wrapped_name = SymbolTable.Table.MangleName (elem.GetAttribute ("cname"));
|
2005-01-05 20:54:39 +00:00
|
|
|
IGeneratable gen = table [CType];
|
|
|
|
|
2004-01-21 20:47:41 +00:00
|
|
|
if (IsArray) {
|
2004-12-16 23:22:07 +00:00
|
|
|
sw.WriteLine ("\t\t{0} {1} {2};", Access, CSType, StudlyName);
|
2004-01-21 20:47:41 +00:00
|
|
|
} else if (IsPadding) {
|
|
|
|
sw.WriteLine ("\t\tprivate {0} {1};", CSType, Name);
|
|
|
|
} else if (IsBit) {
|
|
|
|
// FIXME
|
|
|
|
sw.WriteLine ("\t\tprivate {0} {1};", CSType, Name);
|
|
|
|
} else if (table.IsCallback (CType)) {
|
|
|
|
// FIXME
|
|
|
|
sw.WriteLine ("\t\tprivate {0} {1};", CSType, Name);
|
2005-01-05 20:54:39 +00:00
|
|
|
} else if (gen is LPGen || gen is LPUGen) {
|
|
|
|
sw.WriteLine ("\t\tprivate " + gen.MarshalType + " " + Name + ";");
|
|
|
|
sw.WriteLine ("\t\tpublic " + CSType + " " + StudlyName + " {");
|
|
|
|
sw.WriteLine ("\t\t\tget {");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn " + gen.FromNative (Name) + ";");
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
|
|
|
sw.WriteLine ("\t\t\tset {");
|
|
|
|
sw.WriteLine ("\t\t\t\t" + Name + " = " + gen.CallByName ("value") + ";");
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
|
|
|
sw.WriteLine ("\t\t}");
|
2004-12-16 23:22:07 +00:00
|
|
|
} else if (table.IsObject (CType) || table.IsOpaque (CType)) {
|
2004-01-21 20:47:41 +00:00
|
|
|
sw.WriteLine ("\t\tprivate {0} {1};", CSType, Name);
|
2004-12-16 23:22:07 +00:00
|
|
|
|
|
|
|
if (Access != "private") {
|
|
|
|
sw.WriteLine ("\t\t" + Access + " " + wrapped + " " + wrapped_name + " {");
|
|
|
|
sw.WriteLine ("\t\t\tget { ");
|
|
|
|
sw.WriteLine ("\t\t\t\t" + wrapped + " ret = " + table.FromNativeReturn(CType, Name) + ";");
|
|
|
|
if (table.IsOpaque (CType))
|
|
|
|
sw.WriteLine ("\t\t\t\tif (ret == null) ret = new " + wrapped + "(" + Name + ");");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn ret;");
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\t\tset { " + Name + " = " + table.CallByName (CType, "value") + "; }");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
}
|
2004-01-19 05:30:05 +00:00
|
|
|
} else if (IsPointer && (table.IsStruct (CType) || table.IsBoxed (CType))) {
|
2004-01-21 20:47:41 +00:00
|
|
|
sw.WriteLine ("\t\tprivate {0} {1};", CSType, Name);
|
2004-01-19 05:30:05 +00:00
|
|
|
sw.WriteLine ();
|
2004-12-16 23:22:07 +00:00
|
|
|
if (Access != "private") {
|
|
|
|
sw.WriteLine ("\t\t" + Access + " " + wrapped + " " + wrapped_name + " {");
|
|
|
|
sw.WriteLine ("\t\t\tget { return " + table.FromNativeReturn (CType, Name) + "; }");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
}
|
2004-01-21 20:47:41 +00:00
|
|
|
} else if (IsPointer && CSType != "string") {
|
|
|
|
// FIXME: probably some fields here which should be visible.
|
|
|
|
sw.WriteLine ("\t\tprivate {0} {1};", CSType, Name);
|
2004-02-10 16:04:50 +00:00
|
|
|
} else if (Access != "public") {
|
|
|
|
sw.WriteLine ("\t\t{0} {1} {2};", Access, CSType, Name);
|
2004-01-21 20:47:41 +00:00
|
|
|
} else {
|
2004-02-10 16:04:50 +00:00
|
|
|
sw.WriteLine ("\t\tpublic {0} {1};", CSType, StudlyName);
|
2004-01-19 05:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|