88 lines
2.5 KiB
Beef
88 lines
2.5 KiB
Beef
|
namespace Caa;
|
||
|
|
||
|
using System;
|
||
|
using System.Collections;
|
||
|
using libclang_beef;
|
||
|
|
||
|
class Binding
|
||
|
{
|
||
|
public static List<BindingFunction> Function = new .() ~ DeleteContainerAndItems!(_);
|
||
|
public static List<BindingEnum> Enums = new .() ~ DeleteContainerAndItems!(_);
|
||
|
public static List<BindingStruct> Structs = new .() ~ DeleteContainerAndItems!(_);
|
||
|
public static BindingOptions Options;
|
||
|
|
||
|
public static void Bind(ref String[] pArgs, BindingOptions pOptions)
|
||
|
{
|
||
|
CXIndex index = clang_createIndex(0, 0);
|
||
|
CXTranslationUnit* unit;
|
||
|
clang_parseTranslationUnit2(
|
||
|
index,
|
||
|
pArgs[0], //TODO: This is a hack, allegedly it would be fine if we just input the file as an arg but that doesnt seem to work
|
||
|
(.)&pArgs, (.)pArgs.Count, null, 0,
|
||
|
(.)CXTranslationUnit_Flags.CXTranslationUnit_None,
|
||
|
out unit
|
||
|
);
|
||
|
//TODO: Error Handling
|
||
|
|
||
|
CXCursor cursor = clang_getTranslationUnitCursor(*unit);
|
||
|
clang_visitChildren(
|
||
|
cursor,
|
||
|
(cursor, parent, client_data) => {
|
||
|
if(clang_getCursorKind(cursor) == .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);
|
||
|
}
|
||
|
else if(clang_getCursorKind(cursor) == .CXCursor_StructDecl)
|
||
|
{
|
||
|
var name = clang_getCursorSpelling(cursor);
|
||
|
BindingStruct strct = new .(name.text);
|
||
|
Structs.Add(strct);
|
||
|
|
||
|
}
|
||
|
else if(clang_getCursorKind(cursor) == .CXCursor_FieldDecl)
|
||
|
{
|
||
|
if(Structs.Count > 0)
|
||
|
Structs[Structs.Count-1].Fields.Add(new .(clang_getCursorSpelling(cursor).text, clang_getCursorType(cursor)));
|
||
|
}
|
||
|
|
||
|
|
||
|
return CXChildVisitResult.CXChildVisit_Recurse;
|
||
|
}, null );
|
||
|
|
||
|
|
||
|
clang_disposeTranslationUnit(*unit);
|
||
|
clang_disposeIndex(index);
|
||
|
}
|
||
|
|
||
|
///Generate the actual file strings and try to write them according to the input parameter and the options
|
||
|
public static Result<void> Generate(StringView pOutputDir = "")
|
||
|
{
|
||
|
if(pOutputDir == "")
|
||
|
return .Err;
|
||
|
|
||
|
String toWrite = new .();
|
||
|
defer delete toWrite;
|
||
|
|
||
|
for(var i in Function)
|
||
|
i.ToBeef(toWrite);
|
||
|
for(var i in Structs)
|
||
|
i.ToBeef(toWrite);
|
||
|
for(var i in Enums)
|
||
|
i.ToBeef(toWrite);
|
||
|
|
||
|
return System.IO.File.WriteAllText(pOutputDir, toWrite);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|