2009-02-22 10:43:35 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2013-10-26 23:25:29 +00:00
|
|
|
|
using System.Linq;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2009-08-17 12:28:22 +00:00
|
|
|
|
using Bind.Structures;
|
2009-08-21 20:28:14 +00:00
|
|
|
|
using Delegate=Bind.Structures.Delegate;
|
|
|
|
|
using Enum=Bind.Structures.Enum;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
namespace Bind
|
|
|
|
|
{
|
|
|
|
|
#region WrapperTypes enum
|
|
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
|
public enum WrapperTypes
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// No wrapper needed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
None = 0,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function takes bool parameter - C uses Int for bools, so we have to marshal.
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
BoolParameter = 1 << 0,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function takes generic parameters - add ref/out generic and generic overloads.
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
GenericParameter = 1 << 1,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function takes arrays as parameters - add ref/out and ([Out]) array overloads.
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
ArrayParameter = 1 << 2,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function with bitmask parameters. Bitmask parameters map to UInt, but since we can only use signed
|
|
|
|
|
/// types (for CLS compliance), we must add the unchecked keyword.
|
|
|
|
|
/// Usually found in bitmasks
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
UncheckedParameter = 1 << 3,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function that takes (in/ref/out) a naked pointer as a parameter - we pass an IntPtr.
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
PointerParameter = 1 << 4,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function that takes a reference to a struct.
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
ReferenceParameter = 1 << 5,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function returns string - needs manual marshalling through IntPtr to prevent the managed GC
|
|
|
|
|
/// from freeing memory allocated on the unmanaged side (e.g. glGetString).
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
StringReturnType = 1 << 6,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function returns a void pointer - maps to IntPtr, and the user has to manually marshal the type.
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
GenericReturnType = 1 << 7,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function returns a typed pointer - we have to copy the data to an array to protect it from the GC.
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
ArrayReturnType = 1 << 8,
|
2013-11-09 18:24:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function normally returns a value via an out parameter.
|
|
|
|
|
/// This overload returns a single item directly.
|
2013-11-10 17:29:53 +00:00
|
|
|
|
/// e.g. void GetIntegerv(enum pname, out int value) => int GetInteger(enum pname)
|
2013-11-09 18:24:43 +00:00
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
ConvenienceReturnType = 1 << 9,
|
2013-11-09 18:24:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function normally returns an array via an out parameter.
|
|
|
|
|
/// This overload returns a single item directly.
|
2013-11-10 17:29:53 +00:00
|
|
|
|
/// e.g. void GenBuffers(int count, int[] ids) => int GenBuffer()
|
2013-11-09 18:24:43 +00:00
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
ConvenienceArrayReturnType = 1 << 10,
|
2013-11-10 17:29:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function normally takes an array in parameter.
|
|
|
|
|
/// This overload takes a single item directly.
|
|
|
|
|
/// e.g. void DeleteBuffers(int count, int[] ids) => DeleteBuffer(int id)
|
|
|
|
|
/// </summary>
|
2013-12-03 08:09:19 +00:00
|
|
|
|
ConvenienceArrayType = 1 << 11,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function takes a String or StringBuilder parameter
|
|
|
|
|
/// </summary>
|
|
|
|
|
StringParameter = 1 << 12,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function takes a String[] parameter
|
|
|
|
|
/// </summary>
|
|
|
|
|
StringArrayParameter = 1 << 13,
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2013-11-01 08:03:36 +00:00
|
|
|
|
static class Utilities
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
public static readonly char[] Separators = { ' ', '\n', ',', '(', ')', ';', '#' };
|
2013-10-26 23:25:29 +00:00
|
|
|
|
public static Regex Extensions { get; private set; }
|
|
|
|
|
public static Regex Acronyms { get; private set; }
|
|
|
|
|
//public static readonly Regex Extensions = new Regex(
|
|
|
|
|
// "ARB|EXT|ATIX|ATI|AMDX|AMD|NV|NVX|SUNX|SUN|SGIS|SGIX|SGI|MESAX|MESA|3DFX|IBM|GREMEDY|HP|INTEL|PGI|INGR|APPLE|OML|I3D|ARM|ANGLE|OES|QCOM|VIV|IMG",
|
|
|
|
|
// RegexOptions.Compiled);
|
|
|
|
|
//public static readonly Regex Acronyms = new Regex(Extensions.ToString() + "|EGL|3TC|DXT|ES|GL|CL|RGBA|BGRA|RGB|BGR|ETC",
|
|
|
|
|
// RegexOptions.Compiled);
|
|
|
|
|
|
|
|
|
|
public static void InitExtensions(IEnumerable<string> extensions)
|
|
|
|
|
{
|
|
|
|
|
var acronyms = new string[]
|
|
|
|
|
{
|
|
|
|
|
"EGL", "ES", "GL", "CL",
|
|
|
|
|
"RGBA", "BGRA", "RGB", "BGR",
|
|
|
|
|
"SRGB", "YCBCR",
|
|
|
|
|
"3TC", "DXT", "BPTC", "RGTC",
|
|
|
|
|
"3DC", "ATC", "ETC",
|
|
|
|
|
"ANGLE", "MESAX", "MESA",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Extensions = new Regex(
|
|
|
|
|
String.Join("|", extensions.ToArray()),
|
|
|
|
|
RegexOptions.Compiled);
|
|
|
|
|
|
|
|
|
|
Acronyms = new Regex(
|
|
|
|
|
String.Join("|", extensions.Concat(acronyms).ToArray()),
|
|
|
|
|
RegexOptions.Compiled);
|
|
|
|
|
}
|
2011-12-05 15:16:11 +00:00
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
#region internal StreamReader OpenSpecFile(string file)
|
|
|
|
|
|
|
|
|
|
internal static StreamReader OpenSpecFile(string folder, string file)
|
|
|
|
|
{
|
2009-08-11 15:33:17 +00:00
|
|
|
|
if (String.IsNullOrEmpty(folder) || String.IsNullOrEmpty(file))
|
|
|
|
|
return null;
|
|
|
|
|
|
2009-03-08 18:08:35 +00:00
|
|
|
|
Console.WriteLine(folder);
|
|
|
|
|
Console.WriteLine(file);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
string path = Path.Combine(folder, file);
|
2009-03-08 18:08:35 +00:00
|
|
|
|
Console.WriteLine(path);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
return new StreamReader(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2010-12-05 11:24:05 +00:00
|
|
|
|
#region Keywords
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2013-11-01 08:03:36 +00:00
|
|
|
|
public static List<string> Keywords(GeneratorLanguage language)
|
2010-12-05 11:24:05 +00:00
|
|
|
|
{
|
2013-11-01 08:03:36 +00:00
|
|
|
|
switch (language)
|
2010-12-05 11:24:05 +00:00
|
|
|
|
{
|
2013-11-01 08:03:36 +00:00
|
|
|
|
case GeneratorLanguage.CSharp: return CSharpKeywords;
|
|
|
|
|
case GeneratorLanguage.Cpp: return CppKeywords;
|
|
|
|
|
case GeneratorLanguage.Java: return JavaKeywords;
|
|
|
|
|
default: throw new NotImplementedException();
|
2010-12-05 11:24:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly List<string> CSharpKeywords = new List<string>(
|
2009-02-22 10:43:35 +00:00
|
|
|
|
new string[]
|
|
|
|
|
{
|
|
|
|
|
"abstract", "event", "new", "struct",
|
|
|
|
|
"as", "explicit", "null", "switch",
|
|
|
|
|
"base", "extern", "object", "this",
|
|
|
|
|
"bool", "false", "operator", "throw",
|
|
|
|
|
"break", "finally", "out", "true",
|
|
|
|
|
"byte", "fixed", "override", "try",
|
|
|
|
|
"case", "float", "params", "typeof",
|
|
|
|
|
"catch", "for", "private", "uint",
|
|
|
|
|
"char", "foreach", "protected", "ulong",
|
|
|
|
|
"checked", "goto", "public", "unchecked",
|
|
|
|
|
"class", "if", "readonly", "unsafe",
|
|
|
|
|
"const", "implicit", "ref", "ushort",
|
|
|
|
|
"continue", "in", "return", "using",
|
|
|
|
|
"decimal", "int", "sbyte", "virtual",
|
|
|
|
|
"default", "interface", "sealed", "volatile",
|
|
|
|
|
"delegate", "internal", "short", "void",
|
|
|
|
|
"do", "is", "sizeof", "while",
|
|
|
|
|
"double", "lock", "stackalloc",
|
|
|
|
|
"else", "long", "static",
|
|
|
|
|
"enum", "namespace", "string"
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2011-12-02 11:27:04 +00:00
|
|
|
|
static readonly List<string> JavaKeywords = new List<string>(
|
|
|
|
|
new string[]
|
|
|
|
|
{
|
|
|
|
|
"abstract", "continue", "for", "new", "switch",
|
|
|
|
|
"assert", "default", "goto", "package", "synchronized",
|
|
|
|
|
"boolean", "do", "if", "private", "this",
|
|
|
|
|
"break", "double", "implements", "protected", "throw",
|
|
|
|
|
"byte", "else", "import", "public", "throws",
|
|
|
|
|
"case", "enum", "instanceof", "return", "transient",
|
|
|
|
|
"catch", "extends", "int", "short", "try",
|
|
|
|
|
"char", "final", "interface", "static", "void",
|
|
|
|
|
"class", "finally", "long", "strictfp", "volatile",
|
2011-12-05 12:26:54 +00:00
|
|
|
|
"const", "float", "native", "super", "while",
|
|
|
|
|
"callback"
|
2011-12-02 11:27:04 +00:00
|
|
|
|
});
|
|
|
|
|
|
2010-12-05 11:24:05 +00:00
|
|
|
|
static readonly List<string> CppKeywords = new List<string>();
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2010-12-03 09:45:20 +00:00
|
|
|
|
#region Merge
|
|
|
|
|
|
|
|
|
|
// Merges the specified enum collections.
|
|
|
|
|
internal static void Merge(EnumCollection enums, EnumCollection new_enums)
|
|
|
|
|
{
|
|
|
|
|
foreach (var e in new_enums)
|
|
|
|
|
{
|
|
|
|
|
Merge(enums, e.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Merges the given enum into the enum list. If an enum of the same name exists,
|
|
|
|
|
/// it merges their respective constants.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="enums"></param>
|
|
|
|
|
/// <param name="t"></param>
|
2009-08-21 20:28:14 +00:00
|
|
|
|
internal static void Merge(EnumCollection enums, Enum t)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
if (!enums.ContainsKey(t.Name))
|
|
|
|
|
{
|
|
|
|
|
enums.Add(t.Name, t);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-08-21 20:28:14 +00:00
|
|
|
|
Enum e = enums[t.Name];
|
|
|
|
|
foreach (Constant c in t.ConstantCollection.Values)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
Merge(e, c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Places a new constant in the specified enum, if it doesn't already exist.
|
|
|
|
|
/// The existing constant is replaced iff the new has a numeric value and the old
|
|
|
|
|
/// has a reference value (eg 0x5 is preferred over AttribMask.Foo)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <param name="t"></param>
|
|
|
|
|
/// <returns></returns>
|
2009-08-21 20:28:14 +00:00
|
|
|
|
internal static Enum Merge(Enum s, Constant t)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
|
|
|
|
if (!s.ConstantCollection.ContainsKey(t.Name))
|
|
|
|
|
{
|
|
|
|
|
s.ConstantCollection.Add(t.Name, t);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Tried to add a constant that already exists. If one constant
|
|
|
|
|
// is like: 'Foo = 0x5' and the other like: 'Foo = Bar.Foo', then
|
|
|
|
|
// keep the first one.
|
2013-10-25 06:57:33 +00:00
|
|
|
|
if (!String.IsNullOrEmpty(s.ConstantCollection[t.Name].Reference))
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2013-11-03 11:16:07 +00:00
|
|
|
|
s.ConstantCollection[t.Name] = t;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region internal static string GetGL2Extension(string name)
|
|
|
|
|
|
|
|
|
|
internal static string GetGL2Extension(string name)
|
|
|
|
|
{
|
2010-12-03 11:22:44 +00:00
|
|
|
|
var match = Extensions.Match(name);
|
|
|
|
|
if (match.Success)
|
|
|
|
|
{
|
|
|
|
|
string ext = match.Value;
|
|
|
|
|
if (ext.Length > 2)
|
|
|
|
|
{
|
|
|
|
|
ext = ext[0] + ext.Substring(1).ToLower();
|
|
|
|
|
}
|
|
|
|
|
return ext;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region private static bool IsGL2Extension(string function)
|
|
|
|
|
|
|
|
|
|
private static bool IsGL2Extension(string function)
|
|
|
|
|
{
|
2009-07-31 09:24:49 +00:00
|
|
|
|
return !String.IsNullOrEmpty(GetGL2Extension(function));
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region internal static string StripGL2Extension(string p)
|
|
|
|
|
|
|
|
|
|
internal static string StripGL2Extension(string p)
|
|
|
|
|
{
|
|
|
|
|
return p.Substring(0, p.Length - GetGL2Extension(p).Length);
|
|
|
|
|
}
|
2009-08-01 14:42:54 +00:00
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|