2010-12-02 22:36:05 +01:00
|
|
|
|
#region License
|
|
|
|
|
//
|
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2006 - 2010 the Open Toolkit library.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
//
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml.XPath;
|
|
|
|
|
using Bind.Structures;
|
|
|
|
|
|
|
|
|
|
namespace Bind
|
|
|
|
|
{
|
|
|
|
|
using Delegate = Bind.Structures.Delegate;
|
|
|
|
|
using Enum = Bind.Structures.Enum;
|
|
|
|
|
|
|
|
|
|
class XmlSpecReader : ISpecReader
|
|
|
|
|
{
|
2010-12-03 10:43:08 +01:00
|
|
|
|
#region Public Members
|
|
|
|
|
|
|
|
|
|
public DelegateCollection ReadDelegates(XPathNavigator specs)
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
|
|
|
|
DelegateCollection delegates = new DelegateCollection();
|
|
|
|
|
|
2010-12-03 10:43:08 +01:00
|
|
|
|
foreach (XPathNavigator node in specs.SelectChildren("function", String.Empty))
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 10:43:08 +01:00
|
|
|
|
var name = node.GetAttribute("name", String.Empty);
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-03 10:43:08 +01:00
|
|
|
|
// Check whether we are adding to an existing delegate or creating a new one.
|
|
|
|
|
Delegate d = null;
|
|
|
|
|
if (delegates.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
d = delegates[name];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
d = new Delegate();
|
|
|
|
|
d.Name = name;
|
|
|
|
|
d.Version = node.GetAttribute("version", String.Empty);
|
|
|
|
|
d.Category = node.GetAttribute("category", String.Empty);
|
|
|
|
|
d.DeprecatedVersion = node.GetAttribute("deprecated", String.Empty);
|
|
|
|
|
d.Deprecated = !String.IsNullOrEmpty(d.DeprecatedVersion);
|
|
|
|
|
}
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-03 10:43:08 +01:00
|
|
|
|
foreach (XPathNavigator param in node.SelectChildren(XPathNodeType.Element))
|
|
|
|
|
{
|
|
|
|
|
switch (param.Name)
|
|
|
|
|
{
|
|
|
|
|
case "returns":
|
|
|
|
|
d.ReturnType.CurrentType = param.GetAttribute("type", String.Empty);
|
|
|
|
|
break;
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-03 10:43:08 +01:00
|
|
|
|
case "param":
|
|
|
|
|
Parameter p = new Parameter();
|
|
|
|
|
p.CurrentType = param.GetAttribute("type", String.Empty);
|
|
|
|
|
p.Name = param.GetAttribute("name", String.Empty);
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-03 10:43:08 +01:00
|
|
|
|
string element_count = param.GetAttribute("elementcount", String.Empty);
|
|
|
|
|
if (String.IsNullOrEmpty(element_count))
|
|
|
|
|
element_count = param.GetAttribute("count", String.Empty);
|
|
|
|
|
if (!String.IsNullOrEmpty(element_count))
|
|
|
|
|
p.ElementCount = Int32.Parse(element_count);
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-03 10:43:08 +01:00
|
|
|
|
p.Flow = Parameter.GetFlowDirection(param.GetAttribute("flow", String.Empty));
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-03 10:43:08 +01:00
|
|
|
|
d.Parameters.Add(p);
|
|
|
|
|
break;
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-03 10:43:08 +01:00
|
|
|
|
|
|
|
|
|
delegates.Add(d);
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return delegates;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-03 10:43:08 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ISpecReader Members
|
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
public DelegateCollection ReadDelegates(string file)
|
2010-12-03 10:43:08 +01:00
|
|
|
|
{
|
2010-12-06 15:34:16 +01:00
|
|
|
|
var specs = new XPathDocument(file);
|
2010-12-06 13:29:18 +01:00
|
|
|
|
var delegates = ReadDelegates(specs.CreateNavigator().SelectSingleNode("/signatures/add"));
|
|
|
|
|
return delegates;
|
2010-12-03 10:43:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
public Dictionary<string, string> ReadTypeMap(string file)
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-06 15:34:16 +01:00
|
|
|
|
using (var sr = new StreamReader(file))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Reading opengl types.");
|
|
|
|
|
Dictionary<string, string> GLTypes = new Dictionary<string, string>();
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
if (sr == null)
|
|
|
|
|
return GLTypes;
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
string line = sr.ReadLine();
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
if (String.IsNullOrEmpty(line) || line.StartsWith("#"))
|
|
|
|
|
continue;
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
string[] words = line.Split(" ,*\t".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
if (words[0].ToLower() == "void")
|
|
|
|
|
{
|
|
|
|
|
// Special case for "void" -> "". We make it "void" -> "void"
|
|
|
|
|
GLTypes.Add(words[0], "void");
|
|
|
|
|
}
|
|
|
|
|
else if (words[0] == "VoidPointer" || words[0] == "ConstVoidPointer")
|
|
|
|
|
{
|
|
|
|
|
// "(Const)VoidPointer" -> "void*"
|
|
|
|
|
GLTypes.Add(words[0], "void*");
|
|
|
|
|
}
|
|
|
|
|
else if (words[0] == "CharPointer" || words[0] == "charPointerARB")
|
|
|
|
|
{
|
|
|
|
|
// The typematching logic cannot handle pointers to pointers, e.g. CharPointer* -> char** -> string* -> string[].
|
|
|
|
|
// Hence we give it a push.
|
|
|
|
|
// Note: When both CurrentType == "String" and Pointer == true, the typematching is hardcoded to use
|
|
|
|
|
// String[] or StringBuilder[].
|
|
|
|
|
GLTypes.Add(words[0], "String");
|
|
|
|
|
}
|
|
|
|
|
/*else if (words[0].Contains("Pointer"))
|
|
|
|
|
{
|
|
|
|
|
GLTypes.Add(words[0], words[1].Replace("Pointer", "*"));
|
|
|
|
|
}*/
|
|
|
|
|
else if (words[1].Contains("GLvoid"))
|
|
|
|
|
{
|
|
|
|
|
GLTypes.Add(words[0], "void");
|
|
|
|
|
}
|
|
|
|
|
else if (words[1] == "const" && words[2] == "GLubyte")
|
|
|
|
|
{
|
|
|
|
|
GLTypes.Add(words[0], "String");
|
|
|
|
|
}
|
|
|
|
|
else if (words[1] == "struct")
|
|
|
|
|
{
|
|
|
|
|
GLTypes.Add(words[0], words[2]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GLTypes.Add(words[0], words[1]);
|
|
|
|
|
}
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
2010-12-06 15:34:16 +01:00
|
|
|
|
while (!sr.EndOfStream);
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
return GLTypes;
|
|
|
|
|
}
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
public Dictionary<string, string> ReadCSTypeMap(string file)
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-06 15:34:16 +01:00
|
|
|
|
using (var sr = new StreamReader(file))
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-06 15:34:16 +01:00
|
|
|
|
Dictionary<string, string> CSTypes = new Dictionary<string, string>();
|
|
|
|
|
Console.WriteLine("Reading C# types.");
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
while (!sr.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
string line = sr.ReadLine();
|
|
|
|
|
if (String.IsNullOrEmpty(line) || line.StartsWith("#"))
|
|
|
|
|
continue;
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
string[] words = line.Split(" ,\t".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (words.Length < 2)
|
|
|
|
|
continue;
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
if (((Settings.Compatibility & Settings.Legacy.NoBoolParameters) != Settings.Legacy.None) && words[1] == "bool")
|
|
|
|
|
words[1] = "Int32";
|
|
|
|
|
|
|
|
|
|
CSTypes.Add(words[0], words[1]);
|
|
|
|
|
}
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
return CSTypes;
|
|
|
|
|
}
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 15:34:16 +01:00
|
|
|
|
public EnumCollection ReadEnums(string file)
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
|
|
|
|
// First, read all enum definitions from spec and override file.
|
|
|
|
|
// Afterwards, read all token/enum overrides from overrides file.
|
|
|
|
|
// Every single enum is merged into
|
|
|
|
|
|
|
|
|
|
EnumCollection enums = new EnumCollection();
|
2010-12-06 15:34:16 +01:00
|
|
|
|
var specs = new XPathDocument(file);
|
2010-12-03 12:22:11 +01:00
|
|
|
|
foreach (XPathNavigator nav in specs.CreateNavigator().Select("/signatures/add"))
|
|
|
|
|
{
|
|
|
|
|
var new_enums = ReadEnums(nav);
|
|
|
|
|
Utilities.Merge(enums, new_enums);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return enums;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EnumCollection ReadEnums(XPathNavigator nav)
|
|
|
|
|
{
|
|
|
|
|
EnumCollection enums = new EnumCollection();
|
|
|
|
|
Enum all = new Enum() { Name = Settings.CompleteEnumName };
|
|
|
|
|
|
|
|
|
|
if (nav != null)
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 12:22:11 +01:00
|
|
|
|
foreach (XPathNavigator node in nav.SelectChildren("enum", String.Empty))
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 12:22:11 +01:00
|
|
|
|
Enum e = new Enum()
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 12:22:11 +01:00
|
|
|
|
Name = node.GetAttribute("name", String.Empty),
|
|
|
|
|
Type = node.GetAttribute("type", String.Empty)
|
|
|
|
|
};
|
|
|
|
|
if (String.IsNullOrEmpty(e.Name))
|
|
|
|
|
throw new InvalidOperationException(String.Format("Empty name for enum element {0}", node.ToString()));
|
|
|
|
|
|
|
|
|
|
foreach (XPathNavigator param in node.SelectChildren(XPathNodeType.Element))
|
|
|
|
|
{
|
|
|
|
|
Constant c = null;
|
|
|
|
|
switch (param.Name)
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 12:22:11 +01:00
|
|
|
|
case "token":
|
|
|
|
|
c = new Constant
|
|
|
|
|
{
|
|
|
|
|
Name = param.GetAttribute("name", String.Empty),
|
|
|
|
|
Value = param.GetAttribute("value", String.Empty)
|
|
|
|
|
};
|
|
|
|
|
break;
|
2010-12-02 22:36:05 +01:00
|
|
|
|
|
2010-12-03 12:22:11 +01:00
|
|
|
|
case "use":
|
|
|
|
|
c = new Constant
|
|
|
|
|
{
|
|
|
|
|
Name = param.GetAttribute("token", String.Empty),
|
|
|
|
|
Reference = param.GetAttribute("enum", String.Empty),
|
|
|
|
|
Value = param.GetAttribute("token", String.Empty),
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
Utilities.Merge(all, c);
|
|
|
|
|
try
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 12:22:11 +01:00
|
|
|
|
if (!e.ConstantCollection.ContainsKey(c.Name))
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 12:22:11 +01:00
|
|
|
|
e.ConstantCollection.Add(c.Name, c);
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
2010-12-03 12:22:11 +01:00
|
|
|
|
else if (e.ConstantCollection[c.Name].Value != c.Value)
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 12:22:11 +01:00
|
|
|
|
var existing = e.ConstantCollection[c.Name];
|
|
|
|
|
if (existing.Reference != null && c.Reference == null)
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 12:22:11 +01:00
|
|
|
|
e.ConstantCollection[c.Name] = c;
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
2010-12-03 12:22:11 +01:00
|
|
|
|
else if (existing.Reference == null && c.Reference != null)
|
|
|
|
|
{ } // Keep existing
|
|
|
|
|
else
|
2010-12-02 22:36:05 +01:00
|
|
|
|
{
|
2010-12-03 12:22:11 +01:00
|
|
|
|
Console.WriteLine("[Warning] Conflicting token {0}.{1} with value {2} != {3}",
|
|
|
|
|
e.Name, c.Name, e.ConstantCollection[c.Name].Value, c.Value);
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-03 12:22:11 +01:00
|
|
|
|
catch (ArgumentException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[Warning] Failed to add constant {0} to enum {1}: {2}", c.Name, e.Name, ex.Message);
|
|
|
|
|
}
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
2010-12-03 12:22:11 +01:00
|
|
|
|
|
|
|
|
|
Utilities.Merge(enums, e);
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utilities.Merge(enums, all);
|
|
|
|
|
return enums;
|
|
|
|
|
}
|
2010-12-03 10:43:08 +01:00
|
|
|
|
|
|
|
|
|
#endregion
|
2010-12-02 22:36:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|