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:
parent
b5806d2a1b
commit
c53147c1c4
7 changed files with 93 additions and 3 deletions
|
@ -34,6 +34,7 @@ namespace GtkSharp.Generation {
|
|||
private IDictionary<string, Property> props = new Dictionary<string, Property> ();
|
||||
private IDictionary<string, ObjectField> fields = new Dictionary<string, ObjectField> ();
|
||||
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> managed_interfaces = new List<string>();
|
||||
protected IList<Ctor> ctors = new List<Ctor>();
|
||||
|
@ -108,6 +109,11 @@ namespace GtkSharp.Generation {
|
|||
ctors.Add (new Ctor (member, this));
|
||||
break;
|
||||
|
||||
case "constant":
|
||||
name = member.GetAttribute ("name");
|
||||
constants.Add (name, new Constant (member));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -156,6 +162,14 @@ namespace GtkSharp.Generation {
|
|||
methods.Remove (method.Name);
|
||||
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) {
|
||||
if (!ctor.Validate (log))
|
||||
invalids.Add (ctor);
|
||||
|
@ -199,6 +213,7 @@ namespace GtkSharp.Generation {
|
|||
case "implements":
|
||||
case "constructor":
|
||||
case "disabledefaultconstructor":
|
||||
case "constant":
|
||||
return true;
|
||||
|
||||
default:
|
||||
|
@ -221,6 +236,12 @@ namespace GtkSharp.Generation {
|
|||
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)
|
||||
{
|
||||
foreach (XmlNode node in member.ChildNodes) {
|
||||
|
|
|
@ -77,9 +77,10 @@ namespace GtkSharp.Generation {
|
|||
sw.WriteLine (" {");
|
||||
sw.WriteLine ();
|
||||
|
||||
GenConstants (gen_info);
|
||||
GenProperties (gen_info, null);
|
||||
GenMethods (gen_info, null, null);
|
||||
|
||||
|
||||
sw.WriteLine ("#endregion");
|
||||
|
||||
sw.WriteLine ("\t}");
|
||||
|
|
64
generator/Constant.cs
Normal file
64
generator/Constant.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,6 +19,7 @@ sources = \
|
|||
CodeGenerator.cs \
|
||||
ConstFilenameGen.cs \
|
||||
ConstStringGen.cs \
|
||||
Constant.cs \
|
||||
Ctor.cs \
|
||||
DefaultSignalHandler.cs \
|
||||
EnumGen.cs \
|
||||
|
|
|
@ -195,9 +195,10 @@ namespace GtkSharp.Generation {
|
|||
GenSignals (gen_info, null);
|
||||
}
|
||||
|
||||
GenConstants (gen_info);
|
||||
GenClassMembers (gen_info, cs_parent);
|
||||
GenMethods (gen_info, null, null);
|
||||
|
||||
|
||||
if (interfaces.Count != 0) {
|
||||
var all_methods = new Dictionary<string, Method> ();
|
||||
foreach (Method m in Methods.Values) {
|
||||
|
|
|
@ -79,7 +79,8 @@ namespace GtkSharp.Generation {
|
|||
|
||||
sw.WriteLine (" {");
|
||||
sw.WriteLine ();
|
||||
|
||||
|
||||
GenConstants (gen_info);
|
||||
GenFields (gen_info);
|
||||
GenMethods (gen_info, null, null);
|
||||
GenCtors (gen_info);
|
||||
|
|
|
@ -91,6 +91,7 @@
|
|||
<Compile Include="Parameter.cs" />
|
||||
<Compile Include="ArrayParameter.cs" />
|
||||
<Compile Include="Options.cs" />
|
||||
<Compile Include="Constant.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="DESIGN" />
|
||||
|
|
Loading…
Add table
Reference in a new issue