generator: Added constants to gapi

Unfortunately, gir marks all integers as gint regardless
of its size. We have to check if the value will really
fit into a int, that is why there is an automatic fallback
to long.
This commit is contained in:
Stephan Sundermann 2013-10-08 18:45:42 +02:00 committed by Andrés G. Aragoneses
parent b5806d2a1b
commit c53147c1c4
7 changed files with 93 additions and 3 deletions

View File

@ -34,6 +34,7 @@ namespace GtkSharp.Generation {
private IDictionary<string, Property> props = new Dictionary<string, Property> (); private IDictionary<string, Property> props = new Dictionary<string, Property> ();
private IDictionary<string, ObjectField> fields = new Dictionary<string, ObjectField> (); private IDictionary<string, ObjectField> fields = new Dictionary<string, ObjectField> ();
private IDictionary<string, Method> methods = new Dictionary<string, Method> (); private IDictionary<string, Method> methods = new Dictionary<string, Method> ();
private IDictionary<string, Constant> constants = new Dictionary<string, Constant>();
protected IList<string> interfaces = new List<string>(); protected IList<string> interfaces = new List<string>();
protected IList<string> managed_interfaces = new List<string>(); protected IList<string> managed_interfaces = new List<string>();
protected IList<Ctor> ctors = new List<Ctor>(); protected IList<Ctor> ctors = new List<Ctor>();
@ -108,6 +109,11 @@ namespace GtkSharp.Generation {
ctors.Add (new Ctor (member, this)); ctors.Add (new Ctor (member, this));
break; break;
case "constant":
name = member.GetAttribute ("name");
constants.Add (name, new Constant (member));
break;
default: default:
break; break;
} }
@ -156,6 +162,14 @@ namespace GtkSharp.Generation {
methods.Remove (method.Name); methods.Remove (method.Name);
invalids.Clear (); invalids.Clear ();
foreach (Constant con in constants.Values) {
if (!con.Validate (log))
invalids.Add (con);
}
foreach (Constant con in invalids)
constants.Remove (con.Name);
invalids.Clear ();
foreach (Ctor ctor in ctors) { foreach (Ctor ctor in ctors) {
if (!ctor.Validate (log)) if (!ctor.Validate (log))
invalids.Add (ctor); invalids.Add (ctor);
@ -199,6 +213,7 @@ namespace GtkSharp.Generation {
case "implements": case "implements":
case "constructor": case "constructor":
case "disabledefaultconstructor": case "disabledefaultconstructor":
case "constant":
return true; return true;
default: default:
@ -221,6 +236,12 @@ namespace GtkSharp.Generation {
field.Generate (gen_info, "\t\t"); field.Generate (gen_info, "\t\t");
} }
protected void GenConstants (GenerationInfo gen_info)
{
foreach (Constant con in constants.Values)
con.Generate (gen_info, "\t\t");
}
private void ParseImplements (XmlElement member) private void ParseImplements (XmlElement member)
{ {
foreach (XmlNode node in member.ChildNodes) { foreach (XmlNode node in member.ChildNodes) {

View File

@ -77,9 +77,10 @@ namespace GtkSharp.Generation {
sw.WriteLine (" {"); sw.WriteLine (" {");
sw.WriteLine (); sw.WriteLine ();
GenConstants (gen_info);
GenProperties (gen_info, null); GenProperties (gen_info, null);
GenMethods (gen_info, null, null); GenMethods (gen_info, null, null);
sw.WriteLine ("#endregion"); sw.WriteLine ("#endregion");
sw.WriteLine ("\t}"); sw.WriteLine ("\t}");

64
generator/Constant.cs Normal file
View File

@ -0,0 +1,64 @@
using System;
using System.Xml;
using System.IO;
namespace GtkSharp.Generation
{
public class Constant
{
private readonly XmlElement elem;
private readonly string name;
private readonly string value;
private readonly string ctype;
public Constant (XmlElement elem)
{
this.elem = elem;
this.name = elem.GetAttribute ("name");
this.value = elem.GetAttribute ("value");
this.ctype = elem.GetAttribute ("ctype");
}
public string Name {
get {
return this.name;
}
}
public string ConstType {
get {
if (IsString)
return "string";
// gir registers all integer values as gint even for numbers which do not fit into a gint
// if the number is too big for an int, try to fit it into a long
if (SymbolTable.Table.GetMarshalType (ctype) == "int" && value.Length < 20 && long.Parse (value) > Int32.MaxValue)
return "long";
return SymbolTable.Table.GetMarshalType (ctype);
}
}
public bool IsString {
get {
return (SymbolTable.Table.GetCSType (ctype) == "string");
}
}
public virtual bool Validate (LogWriter log)
{
if (ConstType == String.Empty) {
log.Warn ("{0} type is missing or wrong", Name);
return false;
}
if (SymbolTable.Table.GetMarshalType (ctype) == "int" && value.Length >= 20) {
return false;
}
return true;
}
public virtual void Generate (GenerationInfo gen_info, string indent)
{
StreamWriter sw = gen_info.Writer;
sw.WriteLine ("{0}public const {1} {2} = {3}{4}{3};", indent, ConstType, Name, IsString ? "\"": String.Empty, value);
}
}
}

View File

@ -19,6 +19,7 @@ sources = \
CodeGenerator.cs \ CodeGenerator.cs \
ConstFilenameGen.cs \ ConstFilenameGen.cs \
ConstStringGen.cs \ ConstStringGen.cs \
Constant.cs \
Ctor.cs \ Ctor.cs \
DefaultSignalHandler.cs \ DefaultSignalHandler.cs \
EnumGen.cs \ EnumGen.cs \

View File

@ -195,9 +195,10 @@ namespace GtkSharp.Generation {
GenSignals (gen_info, null); GenSignals (gen_info, null);
} }
GenConstants (gen_info);
GenClassMembers (gen_info, cs_parent); GenClassMembers (gen_info, cs_parent);
GenMethods (gen_info, null, null); GenMethods (gen_info, null, null);
if (interfaces.Count != 0) { if (interfaces.Count != 0) {
var all_methods = new Dictionary<string, Method> (); var all_methods = new Dictionary<string, Method> ();
foreach (Method m in Methods.Values) { foreach (Method m in Methods.Values) {

View File

@ -79,7 +79,8 @@ namespace GtkSharp.Generation {
sw.WriteLine (" {"); sw.WriteLine (" {");
sw.WriteLine (); sw.WriteLine ();
GenConstants (gen_info);
GenFields (gen_info); GenFields (gen_info);
GenMethods (gen_info, null, null); GenMethods (gen_info, null, null);
GenCtors (gen_info); GenCtors (gen_info);

View File

@ -91,6 +91,7 @@
<Compile Include="Parameter.cs" /> <Compile Include="Parameter.cs" />
<Compile Include="ArrayParameter.cs" /> <Compile Include="ArrayParameter.cs" />
<Compile Include="Options.cs" /> <Compile Include="Options.cs" />
<Compile Include="Constant.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="DESIGN" /> <None Include="DESIGN" />