fixing struct bug

This commit is contained in:
Booklordofthedings 2024-05-24 12:09:18 +02:00
parent 46566f5244
commit abf1f52643
3 changed files with 54 additions and 19 deletions

View file

@ -10,10 +10,10 @@ class Binding
public static List<BindingEnum> Enums = new .() ~ DeleteContainerAndItems!(_); public static List<BindingEnum> Enums = new .() ~ DeleteContainerAndItems!(_);
public static List<BindingStruct> Structs = new .() ~ DeleteContainerAndItems!(_); public static List<BindingStruct> Structs = new .() ~ DeleteContainerAndItems!(_);
public static BindingOptions Options = new .() ~ delete _; public static BindingOptions Options = new .() ~ delete _;
public static Dictionary<CXCursorKind, function void(CXCursor)> CursorHandlers = new .() { public static Dictionary<CXCursorKind, function void(CXCursor*)> CursorHandlers = new .() {
(.CXCursor_FunctionDecl, => HandleFunctionDecl), (.CXCursor_FunctionDecl, => HandleFunctionDecl),
(.CXCursor_StructDecl, => HandleStructDecl), (.CXCursor_StructDecl, => HandleStructDecl),
//(.CXCursor_, => HandleFieldDecl), (.CXCursor_FieldDecl, => HandleFieldDecl),
(.CXCursor_EnumDecl, => HandleEnumDecl), (.CXCursor_EnumDecl, => HandleEnumDecl),
(.CXCursor_EnumConstantDecl, => HandleEnumConstDecl), (.CXCursor_EnumConstantDecl, => HandleEnumConstDecl),
(.CXCursor_ParmDecl, => DoNothing), (.CXCursor_ParmDecl, => DoNothing),
@ -40,10 +40,46 @@ class Binding
cursor, cursor,
(cursor, parent, client_data) => { (cursor, parent, client_data) => {
switch(clang_getCursorKind(cursor))
{
case .CXCursor_FunctionDecl:
var name = clang_getCursorSpelling(cursor);
BindingFunction func = new .(name.text, clang_getCursorResultType(cursor));
var count = clang_Cursor_getNumArguments(cursor);
for(int i < count)
{
var arg = clang_Cursor_getArgument(cursor, (.)i);
var argType = clang_getCursorType(arg);
var argName = clang_getCursorSpelling(arg);
func.Args.Add(new .(argName.text, argType));
}
Function.Add(func);
case .CXCursor_StructDecl:
var name = clang_getCursorSpelling(cursor);
BindingStruct strct = new .(name.text);
Structs.Add(strct);
case .CXCursor_FieldDecl:
if(Structs.Count > 0)
Structs[Structs.Count-1].Fields.Add(new .(clang_getCursorSpelling(cursor).text, clang_getCursorType(cursor)));
case .CXCursor_EnumDecl:
var name = clang_getCursorSpelling(cursor);
BindingEnum enm = new .(name.text);
Enums.Add(enm);
case .CXCursor_EnumConstantDecl:
if(Enums.Count > 0)
Enums[Enums.Count-1].Entries.Add(new .(clang_getCursorSpelling(cursor).text, (.)clang_getEnumConstantDeclValue(cursor)));
case .CXCursor_ParmDecl:
DoNothing(&cursor);
default:
Console.WriteLine(scope $"{clang_getCursorKind(cursor)} {clang_getCursorSpelling(cursor).text}");
}
/*
if(CursorHandlers.ContainsKey(clang_getCursorKind(cursor))) if(CursorHandlers.ContainsKey(clang_getCursorKind(cursor)))
CursorHandlers[clang_getCursorKind(cursor)](cursor); CursorHandlers[clang_getCursorKind(cursor)](&cursor);
else else
Console.WriteLine(scope $"{clang_getCursorKind(cursor)} {clang_getCursorSpelling(cursor).text}"); Console.WriteLine(scope $"{clang_getCursorKind(cursor)} {clang_getCursorSpelling(cursor).text}");
*/
return CXChildVisitResult.CXChildVisit_Recurse; return CXChildVisitResult.CXChildVisit_Recurse;
@ -54,15 +90,15 @@ class Binding
clang_disposeIndex(index); clang_disposeIndex(index);
} }
public static void HandleFunctionDecl(CXCursor cursor) public static void HandleFunctionDecl(CXCursor* cursor)
{ {
var name = clang_getCursorSpelling(cursor); var name = clang_getCursorSpelling(*cursor);
BindingFunction func = new .(name.text, clang_getCursorResultType(cursor)); BindingFunction func = new .(name.text, clang_getCursorResultType(*cursor));
var count = clang_Cursor_getNumArguments(cursor); var count = clang_Cursor_getNumArguments(*cursor);
for(int i < count) for(int i < count)
{ {
var arg = clang_Cursor_getArgument(cursor, (.)i); var arg = clang_Cursor_getArgument(*cursor, (.)i);
var argType = clang_getCursorType(arg); var argType = clang_getCursorType(arg);
var argName = clang_getCursorSpelling(arg); var argName = clang_getCursorSpelling(arg);
func.Args.Add(new .(argName.text, argType)); func.Args.Add(new .(argName.text, argType));
@ -70,35 +106,35 @@ class Binding
Function.Add(func); Function.Add(func);
} }
public static void HandleStructDecl(CXCursor cursor) public static void HandleStructDecl(CXCursor* cursor)
{ {
var name = clang_getCursorSpelling(cursor); var name = clang_getCursorSpelling(*cursor);
BindingStruct strct = new .(name.text); BindingStruct strct = new .(name.text);
Structs.Add(strct); Structs.Add(strct);
} }
public static void HandleFieldDecl(CXCursor cursor) public static void HandleFieldDecl(CXCursor* cursor)
{ {
if(Structs.Count > 0) if(Structs.Count > 0)
Structs[Structs.Count-1].Fields.Add(new .(clang_getCursorSpelling(cursor).text, clang_getCursorType(cursor))); Structs[Structs.Count-1].Fields.Add(new .(clang_getCursorSpelling(*cursor).text, clang_getCursorType(*cursor)));
} }
public static void HandleEnumDecl(CXCursor cursor) public static void HandleEnumDecl(CXCursor* cursor)
{ {
var name = clang_getCursorSpelling(cursor); var name = clang_getCursorSpelling(*cursor);
BindingEnum enm = new .(name.text); BindingEnum enm = new .(name.text);
Enums.Add(enm); Enums.Add(enm);
} }
public static void HandleEnumConstDecl(CXCursor cursor) public static void HandleEnumConstDecl(CXCursor* cursor)
{ {
if(Enums.Count > 0) if(Enums.Count > 0)
{ {
Enums[Enums.Count-1].Entries.Add(new .(clang_getCursorSpelling(cursor).text, (.)clang_getEnumConstantDeclValue(cursor))); Enums[Enums.Count-1].Entries.Add(new .(clang_getCursorSpelling(*cursor).text, (.)clang_getEnumConstantDeclValue(*cursor)));
} }
} }
private static void DoNothing(CXCursor cursor) {} private static void DoNothing(CXCursor* cursor) {}
///Generate the actual file strings and try to write them according to the input parameter and the options ///Generate the actual file strings and try to write them according to the input parameter and the options

View file

@ -8,7 +8,7 @@ class BindingOptions
This handles the C way of namespacing This handles the C way of namespacing
If something has the namespace infront of it we remove it and incase of functions, generate the linkname property If something has the namespace infront of it we remove it and incase of functions, generate the linkname property
*/ */
private String _Namespace = new .("") ~ delete _; private String _Namespace = new .("doifjdsoifjodsfjjfoijfdsf") ~ delete _;
public String Namespace public String Namespace
{ {
get => _Namespace; get => _Namespace;

View file

@ -6,7 +6,6 @@ class Program
{ {
public static int Main(String[] args) public static int Main(String[] args)
{ {
Binding.Options.Namespace = "sqlite3_";
Console.WriteLine(""" Console.WriteLine("""
Caa - Generate a Beef binding for c headers Caa - Generate a Beef binding for c headers
Version 1.0.0 Version 1.0.0