add inline wrapper

This commit is contained in:
2026-06-09 18:39:33 +02:00
parent 3f37aebda3
commit b4c3946c85
7 changed files with 237 additions and 107 deletions
+48 -8
View File
@@ -8,7 +8,7 @@ using LibClang;
namespace Box2D.Setup;
class Box2DGenerator : Cpp2BeefGenerator, this(Span<char8*> args)
class Box2DGenerator : Cpp2BeefGenerator, this(Span<char8*> args, StreamWriter inlineWrapper)
{
protected override Span<char8*> Args => args;
protected override Flags Flags => .None;
@@ -74,16 +74,54 @@ class Box2DGenerator : Cpp2BeefGenerator, this(Span<char8*> args)
return .Continue;
}, &(this, default(void)));
str.Append('}');
return;
case .FunctionDecl:
if (Clang.Cursor_IsFunctionInlined(cursor) == 0) break;
let spelling = GetCursorSpelling!(cursor);
{
StringView signature = ScopeCXString!(Clang.GetCursorPrettyPrinted(cursor, printingPolicy));
Runtime.Assert(signature.StartsWith("static inline "));
signature.RemoveFromStart("static inline ".Length);
String wrapper = scope .(1024);
wrapper.Append('\n');
wrapper.Append(signature);
wrapper.Insert(wrapper.IndexOf(spelling), "beefb_");
if (signature.StartsWith("_Bool"))
wrapper.Replace(1...5, "bool");
wrapper.Append(" { ");
if (Clang.GetCursorResultType(cursor).kind != .Void)
wrapper.Append("return ");
wrapper.Append(spelling, "(");
let numArgs = Clang.Cursor_GetNumArguments(cursor);
for (let i < numArgs)
{
if (i > 0) wrapper.Append(", ");
let arg = Clang.Cursor_GetArgument(cursor, (.)i);
wrapper.Append(GetCursorSpelling!(arg));
}
wrapper.Append("); }");
inlineWrapper.Write(wrapper);
}
BeginCursor(cursor);
str.Append("[LinkName(\"beefb_", spelling, "\")] ");
AccessSpecifier(cursor);
str.Append("static extern ");
WriteTypeAndName(cursor, Clang.GetCursorResultType(cursor));
str.Append('(');
Method_Parameters(cursor);
str.Append(')');
str.Append(';');
return;
case .MacroDefinition:
let spelling = GetCursorSpelling!(cursor);
if (spelling == "B2_BREAKPOINT" || spelling == "B2_API" || spelling == "B2_INLINE" || spelling == "B2_ID_INLINE" || spelling == "B2_ZERO_INIT" || spelling == "B2_NULL_ID")
return;
fallthrough;
case .VarDecl when GetCursorSpelling!(cursor) == "b2_emptySimplexCache":
return;
default:
if (_ == .VarDecl && GetCursorSpelling!(cursor) == "b2_emptySimplexCache")
return;
base.HandleCursor(cursor);
}
base.HandleCursor(cursor);
}
List<String> functionNonPtrs = new .(8) ~ DeleteContainerAndItems!(_);
@@ -189,8 +227,6 @@ class Box2DGenerator : Cpp2BeefGenerator, this(Span<char8*> args)
}
base.WriteToken(token);
}
// There are functions you can override, but these are the most common
}
class Program
@@ -205,6 +241,7 @@ class Program
return 1;
}
StreamWriter inlineWrapper = scope .()..Create("../src/inline_wrapper.c");
{
StreamWriter all = scope .()..Create("b2_all.h");
for (let file in Directory.EnumerateFiles("../box2d/include/box2d"))
@@ -213,10 +250,13 @@ class Program
all.Write("#include <box2d/");
all.Write(filename);
all.Write(">\n");
inlineWrapper.Write("#include <box2d/");
inlineWrapper.Write(filename);
inlineWrapper.Write(">\n");
}
}
scope Box2DGenerator(char8*[?]("--language=c", "-I../box2d/include")).Generate("b2_all.h");
scope Box2DGenerator(char8*[?]("--language=c", "-I../box2d/include"), inlineWrapper).Generate("b2_all.h");
return 0;
}