mirror of https://github.com/mono/CppSharp.git
2 changed files with 0 additions and 419 deletions
@ -1,158 +0,0 @@
@@ -1,158 +0,0 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using Cxxi.Generators; |
||||
using Cxxi.Types; |
||||
|
||||
namespace Cxxi.Templates.CSharp |
||||
{ |
||||
class CSharpTypePrinter : ITypePrinter |
||||
{ |
||||
public Library Library { get; set; } |
||||
|
||||
public CSharpTypePrinter() |
||||
{ |
||||
} |
||||
|
||||
public string VisitTagType(TagType tag, TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
|
||||
public string VisitArrayType(ArrayType array, TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
|
||||
// C# only supports fixed arrays in unsafe sections
|
||||
// and they are constrained to a set of built-in types.
|
||||
|
||||
//return string.Format("{0}[]", Type);
|
||||
} |
||||
|
||||
public string VisitFunctionType(FunctionType function, TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
|
||||
//string args = string.Empty;
|
||||
|
||||
//if (Arguments.Count > 0)
|
||||
// args = ToArgumentString(hasNames: false);
|
||||
|
||||
//if (ReturnType.IsPrimitiveType(PrimitiveType.Void))
|
||||
//{
|
||||
// if (!string.IsNullOrEmpty(args))
|
||||
// args = string.Format("<{0}>", args);
|
||||
// return string.Format("Action{0}", args);
|
||||
//}
|
||||
|
||||
//if (!string.IsNullOrEmpty(args))
|
||||
// args = string.Format(", {0}", args);
|
||||
|
||||
//return string.Format("Func<{0}{1}>",
|
||||
// ReturnType.ToCSharp(), args);
|
||||
} |
||||
|
||||
public string VisitPointerType(PointerType pointer, TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
|
||||
//if (Pointee is FunctionType)
|
||||
//{
|
||||
// var function = Pointee as FunctionType;
|
||||
// return function.ToCSharp();
|
||||
//}
|
||||
|
||||
//if (Pointee is TagType)
|
||||
// return Pointee.ToCSharp();
|
||||
|
||||
//return "IntPtr";
|
||||
|
||||
//return string.Format("{0}{1}",
|
||||
// Pointee.ToCSharp(), ConvertModifierToString(Modifier));
|
||||
} |
||||
|
||||
public string VisitMemberPointerType(MemberPointerType member, |
||||
TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
|
||||
public string VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
|
||||
public string VisitTypedefType(TypedefType typedef, TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
|
||||
public string VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
|
||||
public string VisitPrimitiveType(PrimitiveType type, TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
|
||||
public string VisitDeclaration(Declaration decl, TypeQualifiers quals) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
|
||||
public string GetArgumentsString(FunctionType function, bool hasNames) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
|
||||
public string GetArgumentString(Parameter arg, bool hasName) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
|
||||
//if (hasName && !string.IsNullOrEmpty(Name))
|
||||
// return string.Format("{0} {1}", Type.ToCSharp(), Name);
|
||||
//else
|
||||
// return Type.ToCSharp();
|
||||
} |
||||
|
||||
public string ToDelegateString(FunctionType function) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
} |
||||
|
||||
public class CSharpModule : TextTemplate |
||||
{ |
||||
// from https://github.com/mono/mono/blob/master/mcs/class/System/Microsoft.CSharp/CSharpCodeGenerator.cs
|
||||
private static string[] keywords = new string[] |
||||
{ |
||||
"abstract","event","new","struct","as","explicit","null","switch","base","extern", |
||||
"this","false","operator","throw","break","finally","out","true", |
||||
"fixed","override","try","case","params","typeof","catch","for", |
||||
"private","foreach","protected","checked","goto","public", |
||||
"unchecked","class","if","readonly","unsafe","const","implicit","ref", |
||||
"continue","in","return","using","virtual","default", |
||||
"interface","sealed","volatile","delegate","internal","do","is", |
||||
"sizeof","while","lock","stackalloc","else","static","enum", |
||||
"namespace", |
||||
"object","bool","byte","float","uint","char","ulong","ushort", |
||||
"decimal","int","sbyte","short","double","long","string","void", |
||||
"partial", "yield", "where" |
||||
}; |
||||
|
||||
public static string SafeIdentifier(string proposedName) |
||||
{ |
||||
proposedName = new string(((IEnumerable<char>)proposedName).Select(c => char.IsLetterOrDigit(c) ? c : '_').ToArray()); |
||||
return keywords.Contains(proposedName) ? "@" + proposedName : proposedName; |
||||
} |
||||
|
||||
public override string FileExtension { get { return "cs"; } } |
||||
|
||||
protected override void Generate() |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
} |
||||
} |
@ -1,261 +0,0 @@
@@ -1,261 +0,0 @@
|
||||
<#@ template debug="true" language="C#" inherits="Cxxi.Templates.TextTemplate" #> |
||||
<#@ output extension=".cs" #> |
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
namespace <#= SafeIdentifier(Library.Name) #> |
||||
{ |
||||
<# |
||||
GenerateDeclarations(); |
||||
#> |
||||
} |
||||
|
||||
<#+ |
||||
public void GenerateDeclarations() |
||||
{ |
||||
PushIndent(DefaultIndent); |
||||
|
||||
bool NeedsNewline = false; |
||||
|
||||
// Generate all the enum declarations for the module. |
||||
for(int i = 0; i < Module.Enums.Count; ++i) |
||||
{ |
||||
var E = Module.Enums[i]; |
||||
if (E.Ignore) continue; |
||||
|
||||
GenerateEnum(E); |
||||
NeedsNewline = true; |
||||
if (i < Module.Enums.Count - 1) |
||||
WriteLine(""); |
||||
} |
||||
|
||||
if (NeedsNewline) |
||||
WriteLine(""); |
||||
|
||||
NeedsNewline = false; |
||||
|
||||
// Generate all the typedef declarations for the module. |
||||
for(int i = 0; i < Module.Typedefs.Count; ++i) |
||||
{ |
||||
var T = Module.Typedefs[i]; |
||||
if (T.Ignore) continue; |
||||
|
||||
GenerateTypedef(T); |
||||
NeedsNewline = true; |
||||
|
||||
if (i < Module.Typedefs.Count - 1) |
||||
WriteLine(""); |
||||
} |
||||
|
||||
if (NeedsNewline) |
||||
WriteLine(""); |
||||
|
||||
NeedsNewline = false; |
||||
|
||||
// Generate all the struct/class declarations for the module. |
||||
for(int i = 0; i < Module.Classes.Count; ++i) |
||||
{ |
||||
var C = Module.Classes[i]; |
||||
if (C.Ignore) continue; |
||||
|
||||
GenerateClass(C); |
||||
NeedsNewline = true; |
||||
if (i < Module.Classes.Count - 1) |
||||
WriteLine(""); |
||||
} |
||||
|
||||
if (NeedsNewline) |
||||
WriteLine(""); |
||||
|
||||
if (Module.HasFunctions) |
||||
{ |
||||
WriteLine("public partial class " + SafeIdentifier(Library.Name)); |
||||
WriteLine("{"); |
||||
PushIndent(DefaultIndent); |
||||
} |
||||
|
||||
// Generate all the function declarations for the module. |
||||
foreach(var E in Module.Functions) |
||||
{ |
||||
GenerateFunction(E); |
||||
} |
||||
|
||||
if (Module.HasFunctions) |
||||
{ |
||||
PopIndent(); |
||||
WriteLine("}"); |
||||
} |
||||
|
||||
PopIndent(); |
||||
} |
||||
#> |
||||
|
||||
<#+ |
||||
public void GenerateDeclarationCommon(Declaration T) |
||||
{ |
||||
GenerateSummary(T.BriefComment); |
||||
GenerateDebug(T); |
||||
} |
||||
#> |
||||
|
||||
<#+ |
||||
public void GenerateClass(Class C) |
||||
{ |
||||
if(C.Ignore) return; |
||||
GenerateDeclarationCommon(C); |
||||
|
||||
if (C.IsUnion) |
||||
WriteLine("[StructLayout(LayoutKind.Explicit)]"); |
||||
Write("public unsafe "); |
||||
|
||||
if (C.IsAbstract) |
||||
Write("abstract "); |
||||
|
||||
Write("class {0}", SafeIdentifier(C.Name)); |
||||
|
||||
if (C.HasBase) |
||||
Write(" : {0}", SafeIdentifier(C.Bases[0].Class.Name)); |
||||
|
||||
WriteLine(String.Empty); |
||||
WriteLine("{"); |
||||
|
||||
if (!C.IsOpaque) |
||||
{ |
||||
PushIndent(DefaultIndent); |
||||
foreach(var F in C.Fields) |
||||
{ |
||||
if (F.Ignore == true) continue; |
||||
|
||||
GenerateDeclarationCommon(F); |
||||
if (C.IsUnion) |
||||
WriteLine("[FieldOffset({0})]", F.Offset); |
||||
WriteLine("public {0} {1};", F.Type, SafeIdentifier(F.Name)); |
||||
} |
||||
PopIndent(); |
||||
} |
||||
|
||||
WriteLine("}"); |
||||
} |
||||
#> |
||||
|
||||
<#+ |
||||
public void GenerateTypedef(TypedefDecl T) |
||||
{ |
||||
if(T.Ignore) return; |
||||
GenerateDeclarationCommon(T); |
||||
|
||||
FunctionType func; |
||||
TagType tag; |
||||
|
||||
if (T.Type.IsPointerToPrimitiveType(PrimitiveType.Void) |
||||
|| T.Type.IsPointerTo<TagType>(out tag)) |
||||
{ |
||||
WriteLine("public class " + SafeIdentifier(T.Name) + @" { }"); |
||||
WriteLine(""); |
||||
} |
||||
else if(T.Type.IsPointerTo<FunctionType>(out func)) |
||||
{ |
||||
//WriteLine("public {0};", |
||||
// string.Format(func.ToDelegateString(), SafeIdentifier(T.Name))); |
||||
} |
||||
else if (T.Type.IsEnumType()) |
||||
{ |
||||
// Already handled in the parser. |
||||
} |
||||
else |
||||
{ |
||||
Console.WriteLine("Unhandled typedef type: {0}", T); |
||||
} |
||||
} |
||||
#> |
||||
|
||||
<#+ |
||||
public void GenerateFunction(Function F) |
||||
{ |
||||
if(F.Ignore) return; |
||||
GenerateDeclarationCommon(F); |
||||
#> |
||||
[DllImport("<#= SafeIdentifier(Library.Native) #>.dll", |
||||
CallingConvention = CallingConvention.<#= F.ToCSharpCallConv() #>, |
||||
EntryPoint="<#= F.Mangled #>")] |
||||
public unsafe static extern <#= F.ReturnType #> <#= SafeIdentifier(F.Name) #>(<#+ |
||||
for(int i = 0; i < F.Parameters.Count; ++i) |
||||
{ |
||||
var P = F.Parameters[i]; |
||||
Write("{0} {1}", P.Type, SafeIdentifier(P.Name)); |
||||
if (i < F.Parameters.Count - 1) |
||||
Write(", "); |
||||
} |
||||
#>); |
||||
<#+ |
||||
WriteLine(""); |
||||
} |
||||
#> |
||||
|
||||
<#+ |
||||
public void GenerateDebug(Declaration decl) |
||||
{ |
||||
if(Options.OutputDebug && !String.IsNullOrWhiteSpace(decl.DebugText)) |
||||
WriteLine("// DEBUG: " + decl.DebugText); |
||||
} |
||||
#> |
||||
|
||||
<#+ |
||||
public void GenerateSummary(string Comment) |
||||
{ |
||||
if(String.IsNullOrWhiteSpace(Comment)) |
||||
return; |
||||
#> |
||||
/// <summary> |
||||
/// <#= Comment #> |
||||
/// </summary> |
||||
<#+ |
||||
} |
||||
#> |
||||
|
||||
<#+ |
||||
public void GenerateInlineSummary(string Comment) |
||||
{ |
||||
if(String.IsNullOrWhiteSpace(Comment)) |
||||
return; |
||||
#> |
||||
/// <summary> <#= Comment #> </summary> |
||||
<#+ |
||||
} |
||||
#> |
||||
|
||||
<#+ |
||||
public void GenerateEnum(Enumeration E) |
||||
{ |
||||
if(E.Ignore) return; |
||||
GenerateDeclarationCommon(E); |
||||
|
||||
if(E.Modifiers.HasFlag(Enumeration.EnumModifiers.Flags)) |
||||
WriteLine("[Flags]"); |
||||
#> |
||||
public enum <#= SafeIdentifier(E.Name) #> |
||||
<#+ |
||||
if(E.BuiltinType.Type != PrimitiveType.Int32) |
||||
WriteLine(" : {0}", E.BuiltinType.Type); |
||||
WriteLine("{"); |
||||
|
||||
PushIndent(DefaultIndent); |
||||
for(int i = 0; i < E.Items.Count; ++i) |
||||
{ |
||||
var I = E.Items[i]; |
||||
GenerateInlineSummary(I.Comment); |
||||
if (I.ExplicitValue) |
||||
Write(String.Format("{0} = {1}", SafeIdentifier(I.Name), I.Value)); |
||||
else |
||||
Write(String.Format("{0}", SafeIdentifier(I.Name))); |
||||
|
||||
if (i < E.Items.Count - 1) |
||||
WriteLine(","); |
||||
} |
||||
PopIndent(); |
||||
WriteLine(""); |
||||
#> |
||||
} |
||||
<#+ |
||||
} |
||||
#> |
Loading…
Reference in new issue