Opentk/Source/Bind/Structures/Function.cs

171 lines
4.1 KiB
C#
Raw Normal View History

2007-08-01 09:27:57 +00:00
using System;
using System.Collections.Generic;
using System.Text;
2007-08-01 09:27:57 +00:00
namespace Bind.Structures
{
2007-08-01 09:27:57 +00:00
public class Function : Delegate
{
2007-08-01 09:27:57 +00:00
#region --- Constructors ---
public Function()
2007-08-01 09:27:57 +00:00
: base()
{
Body = new FunctionBody();
}
public Function(Function f)
2007-08-01 09:27:57 +00:00
: base(f)
{
this.Body = new FunctionBody(f.Body);
}
2007-08-01 09:27:57 +00:00
public Function(Delegate d)
: base(d)
{
2007-08-01 09:27:57 +00:00
this.Body = new FunctionBody();
}
#endregion
2007-08-01 09:27:57 +00:00
public override bool Unsafe
{
2007-08-01 09:27:57 +00:00
get
{
2007-08-01 09:27:57 +00:00
if (Settings.Compatibility == Settings.Legacy.Tao)
return false;
2007-08-01 09:27:57 +00:00
return base.Unsafe;
}
}
2007-08-01 09:27:57 +00:00
#region Function body
2007-08-01 09:27:57 +00:00
FunctionBody _body;
2007-08-01 09:27:57 +00:00
public FunctionBody Body
{
2007-08-01 09:27:57 +00:00
get { return _body; }
set { _body = value; }
}
#endregion
2007-08-01 09:27:57 +00:00
#region public override string ToString()
2007-08-01 09:27:57 +00:00
public override string ToString()
{
StringBuilder sb = new StringBuilder();
2007-08-01 09:27:57 +00:00
sb.Append(Unsafe ? "unsafe " : "");
sb.Append(ReturnType);
sb.Append(" ");
if (Settings.Compatibility == Settings.Legacy.Tao)
{
sb.Append("gl");
}
sb.Append(Name);
2007-08-01 09:27:57 +00:00
sb.Append(Parameters.ToString(true));
if (Body.Count > 0)
{
2007-08-01 09:27:57 +00:00
sb.AppendLine();
sb.Append(Body.ToString());
}
return sb.ToString();
}
#endregion
2007-08-01 09:27:57 +00:00
#region public Function GetCLSCompliantFunction(Dictionary<string, string> CSTypes)
2007-08-01 09:27:57 +00:00
public Function GetCLSCompliantFunction(Dictionary<string, string> CSTypes)
{
2007-08-01 09:27:57 +00:00
Function f = new Function(this);
2007-08-01 09:27:57 +00:00
for (int i = 0; i < f.Parameters.Count; i++)
{
f.Parameters[i].Type = f.Parameters[i].GetCLSCompliantType(CSTypes);
}
2007-08-01 09:27:57 +00:00
f.Body.Clear();
if (!f.NeedsWrapper)
{
2007-08-01 09:27:57 +00:00
f.Body.Add((f.ReturnType.Type != "void" ? "return " + this.CallString() : this.CallString()) + ";");
}
else
{
f.Body.AddRange(Function.GetBodyWithPins(this, CSTypes, true));
}
2007-08-01 09:27:57 +00:00
// The type system cannot differentiate between functions with the same parameters
// but different return types. Tough, only CLS-Compliant function in that case.
//f.ReturnType.Type = f.ReturnType.GetCLSCompliantType(CSTypes);
return f;
}
#endregion
}
#region class FunctionBody : List<string>
public class FunctionBody : List<string>
{
public FunctionBody()
{
}
public FunctionBody(FunctionBody fb)
{
foreach (string s in fb)
{
this.Add(s);
}
}
public override string ToString()
{
if (this.Count == 0)
return String.Empty;
StringBuilder sb = new StringBuilder(this.Count);
2007-08-01 09:27:57 +00:00
sb.AppendLine("{");
foreach (string s in this)
{
2007-08-01 09:27:57 +00:00
sb.AppendLine(" " + s);
}
2007-08-01 09:27:57 +00:00
sb.AppendLine("}");
return sb.ToString();
}
}
#endregion
2007-08-01 09:27:57 +00:00
class FunctionCollection : Dictionary<string, List<Function>>
{
public void Add(Function f)
{
if (!this.ContainsKey(f.Extension))
{
this.Add(f.Extension, new List<Function>());
this[f.Extension].Add(f);
}
else
{
this[f.Extension].Add(f);
}
}
public void AddRange(IEnumerable<Function> functions)
{
foreach (Function f in functions)
{
this.Add(f);
}
}
}
}