bgfx/scripts/bindings-zig.lua

494 lines
12 KiB
Lua
Raw Normal View History

2022-04-04 04:42:43 +03:00
local codegen = require "codegen"
local idl = codegen.idl "bgfx.idl"
local zig_template = [[
2023-01-14 21:05:12 +03:00
// Copyright 2011-2023 Branimir Karadzic. All rights reserved.
2022-04-04 04:42:43 +03:00
// License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
//
// AUTO GENERATED! DO NOT EDIT!
//
const std = @import("std");
2022-04-08 16:41:00 +03:00
pub const ViewId = u16;
2022-04-04 04:42:43 +03:00
$types
$funcs
]]
local function isempty(s)
return s == nil or s == ''
end
local function hasPrefix(str, prefix)
return prefix == "" or str:sub(1, #prefix) == prefix
end
local function hasSuffix(str, suffix)
return suffix == "" or str:sub(- #suffix) == suffix
end
2022-05-23 04:43:03 +03:00
local enum = {}
2022-04-04 04:42:43 +03:00
2022-04-08 16:41:00 +03:00
local function convert_array(member)
if string.find(member.array, "::") then
return string.format("[%s]", enum[member.array])
else
return member.array
end
end
2022-05-23 04:43:03 +03:00
local function gisub(s, pat, repl, n)
pat = string.gsub(pat, '(%a)', function(v)
return '[' .. string.upper(v) .. string.lower(v) .. ']'
end)
if n then
return string.gsub(s, pat, repl, n)
else
return string.gsub(s, pat, repl)
end
end
2022-04-08 16:41:00 +03:00
local function convert_type_0(arg)
2022-04-04 04:42:43 +03:00
if hasPrefix(arg.ctype, "uint64_t") then
return arg.ctype:gsub("uint64_t", "u64")
elseif hasPrefix(arg.ctype, "int64_t") then
return arg.ctype:gsub("int64_t", "i64")
elseif hasPrefix(arg.ctype, "uint32_t") then
return arg.ctype:gsub("uint32_t", "u32")
elseif hasPrefix(arg.ctype, "int32_t") then
return arg.ctype:gsub("int32_t", "i32")
elseif hasPrefix(arg.ctype, "uint16_t") then
return arg.ctype:gsub("uint16_t", "u16")
elseif hasPrefix(arg.ctype, "uint8_t") then
return arg.ctype:gsub("uint8_t", "u8")
elseif hasPrefix(arg.ctype, "uintptr_t") then
return arg.ctype:gsub("uintptr_t", "usize")
elseif hasPrefix(arg.ctype, "float") then
return arg.ctype:gsub("float", "f32")
elseif arg.ctype == "const char*" then
2022-04-08 16:41:00 +03:00
return "[*c]const u8"
2022-04-04 04:42:43 +03:00
elseif hasPrefix(arg.ctype, "char") then
return arg.ctype:gsub("char", "u8")
elseif hasSuffix(arg.fulltype, "Handle") then
return arg.fulltype
elseif arg.ctype == "..." then
return "..."
2022-05-23 04:43:03 +03:00
elseif arg.ctype == "va_list" or arg.fulltype == "bx::AllocatorI*" or arg.fulltype == "CallbackI*" or arg.fulltype ==
"ReleaseFn" then
2022-04-04 04:42:43 +03:00
return "?*anyopaque"
end
return arg.fulltype
end
local function convert_type(arg)
local ctype = convert_type_0(arg)
ctype = ctype:gsub("::Enum", "")
ctype = ctype:gsub(" &", "*")
ctype = ctype:gsub("&", "*")
ctype = ctype:gsub("char", "u8")
ctype = ctype:gsub("float", "f32")
2022-04-08 16:41:00 +03:00
ctype = ctype:gsub("const void%*", "?*const anyopaque")
ctype = ctype:gsub("Encoder%*", "?*Encoder")
2022-04-04 04:42:43 +03:00
if hasSuffix(ctype, "void*") then
ctype = ctype:gsub("void%*", "?*anyopaque");
elseif hasSuffix(ctype, "*") then
ctype = "[*c]" .. ctype:gsub("*", "")
end
2022-04-08 16:41:00 +03:00
if arg.array ~= nil then
ctype = ctype:gsub("const ", "")
ctype = convert_array(arg) .. ctype
end
2022-04-04 04:42:43 +03:00
return ctype
end
local function convert_struct_type(arg)
2022-04-08 16:41:00 +03:00
return convert_type(arg)
2022-04-04 04:42:43 +03:00
end
local function convert_ret_type(arg)
return convert_type(arg)
end
2022-05-23 04:43:03 +03:00
local function wrap_simple_func(func, args, argNames)
local zigFunc = {}
local zigFuncTemplate = [[pub inline fn $func($params) $ret {
return $cfunc($args);
2022-05-23 04:43:03 +03:00
}]]
-- transform name to camelCase from snake_case
zigFunc.func = func.cname:gsub("_(.)", func.cname.upper)
-- make 2d/3d upper case 2D/3D
zigFunc.func = zigFunc.func:gsub("%dd", zigFunc.func.upper);
2022-05-23 04:43:03 +03:00
zigFunc.params = table.concat(args, ", ")
zigFunc.ret = convert_ret_type(func.ret)
zigFunc.cfunc = "bgfx_" .. func.cname
zigFunc.args = table.concat(argNames, ", ")
return zigFuncTemplate:gsub("$(%l+)", zigFunc)
end
local function wrap_method(func, type, args, argNames, indent)
local zigFunc = {}
local zigFuncTemplate = [[%spub inline fn $func($params) $ret {
%sreturn $cfunc($args);
2022-05-23 04:43:03 +03:00
%s}]]
zigFuncTemplate = string.format(zigFuncTemplate, indent, indent, indent);
-- transform name to camelCase from snake_case
zigFunc.func = func.cname:gsub("_(.)", func.cname.upper)
-- remove type from name
zigFunc.func = gisub(zigFunc.func, type, "");
-- make first letter lowercase
zigFunc.func = zigFunc.func:gsub("^%L", string.lower)
-- make 2d/3d upper case 2D/3D
zigFunc.func = zigFunc.func:gsub("%dd", zigFunc.func.upper);
2022-05-23 04:43:03 +03:00
zigFunc.params = table.concat(args, ", ")
zigFunc.ret = convert_ret_type(func.ret)
-- remove C API pointer [*c] for fluent interfaces
if zigFunc.ret == ("[*c]" .. type) then
zigFunc.ret = zigFunc.ret:gsub("%[%*c%]", "*")
end
zigFunc.cfunc = "bgfx_" .. func.cname
zigFunc.args = table.concat(argNames, ", ")
return zigFuncTemplate:gsub("$(%l+)", zigFunc)
end
2022-04-04 04:42:43 +03:00
local converter = {}
local yield = coroutine.yield
local gen = {}
2022-04-08 16:41:00 +03:00
local indent = ""
2022-04-04 04:42:43 +03:00
function gen.gen()
2022-05-23 04:43:03 +03:00
-- find the functions that have `this` first argument
-- these belong to a type (struct) and we need to add them when converting structures
local methods = {}
for _, func in ipairs(idl["funcs"]) do
if func.this ~= nil then
if methods[func.this_type.type] == nil then
methods[func.this_type.type] = {}
end
table.insert(methods[func.this_type.type], func)
end
end
2022-04-04 04:42:43 +03:00
local r = zig_template:gsub("$(%l+)", function(what)
local tmp = {}
for _, object in ipairs(idl[what]) do
local co = coroutine.create(converter[what])
local any
2022-05-23 04:43:03 +03:00
-- we're pretty confident there are no types that have the same name with a func
local funcs = methods[object.name]
2022-04-04 04:42:43 +03:00
while true do
2022-05-23 04:43:03 +03:00
local ok, v = coroutine.resume(co, {
obj = object,
funcs = funcs
})
2022-04-04 04:42:43 +03:00
assert(ok, debug.traceback(co, v))
if not v then
break
end
table.insert(tmp, v)
any = true
end
if any and tmp[#tmp] ~= "" then
table.insert(tmp, "")
end
end
return table.concat(tmp, "\n")
end)
return r
end
local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
for _, v in ipairs(combined) do
combined[v] = {}
end
local lastCombinedFlag
local function FlagBlock(typ)
local format = "0x%08x"
2022-04-08 16:41:00 +03:00
local enumType = "u32"
2022-04-04 04:42:43 +03:00
if typ.bits == 64 then
format = "0x%016x"
2022-04-08 16:41:00 +03:00
enumType = "u64"
2022-04-04 04:42:43 +03:00
elseif typ.bits == 16 then
format = "0x%04x"
2022-04-08 16:41:00 +03:00
enumType = "u16"
2022-04-04 04:42:43 +03:00
end
2022-04-08 16:41:00 +03:00
local name = typ.name .. "Flags"
yield("pub const " .. name .. " = " .. enumType .. ";")
2022-04-04 04:42:43 +03:00
for idx, flag in ipairs(typ.flag) do
if flag.comment ~= nil then
if idx ~= 1 then
yield("")
end
for _, comment in ipairs(flag.comment) do
2022-04-08 16:41:00 +03:00
yield("/// " .. comment)
2022-04-04 04:42:43 +03:00
end
end
local flagName = flag.name:gsub("_", "")
-- make 2d/3d upper case 2D/3D
flagName = flagName:gsub("%dd", flagName.upper);
2022-05-23 04:43:03 +03:00
yield("pub const " .. name .. "_" .. flagName .. ": " .. name .. string.rep(" ", 22 - #(flagName)) .. " = " ..
string.format(flag.format or format, flag.value) .. ";")
2022-04-04 04:42:43 +03:00
end
if typ.shift then
2022-05-23 04:43:03 +03:00
yield(" " .. "Shift" .. string.rep(" ", 22 - #("Shift")) .. " = " .. flag.shift)
2022-04-04 04:42:43 +03:00
end
-- generate Mask
if typ.mask then
2022-05-23 04:43:03 +03:00
yield(" " .. "Mask" .. string.rep(" ", 22 - #("Mask")) .. " = " .. string.format(format, flag.mask))
2022-04-04 04:42:43 +03:00
end
end
local function lastCombinedFlagBlock()
if lastCombinedFlag then
local typ = combined[lastCombinedFlag]
if typ then
FlagBlock(combined[lastCombinedFlag])
yield("")
end
lastCombinedFlag = nil
end
end
local function convert_struct_member(member)
2022-04-08 16:41:00 +03:00
return member.name .. ": " .. convert_struct_type(member)
2022-04-04 04:42:43 +03:00
end
local namespace = ""
2022-05-23 04:43:03 +03:00
function converter.types(params)
local typ = params.obj
local funcs = params.funcs
2022-04-04 04:42:43 +03:00
if typ.handle then
lastCombinedFlagBlock()
yield("pub const " .. typ.name .. " = extern struct {")
yield(" idx: c_ushort,")
yield("};")
elseif hasSuffix(typ.name, "::Enum") then
lastCombinedFlagBlock()
yield("pub const " .. typ.typename .. " = enum(c_int) {")
for idx, enum in ipairs(typ.enum) do
if enum.comment ~= nil then
if idx ~= 1 then
yield("")
end
for _, comment in ipairs(enum.comment) do
yield(" /// " .. comment)
end
end
yield(" " .. enum.name .. ",")
end
yield("");
yield(" Count")
yield("};")
enum["[" .. typ.typename .. "::Count]"] = #typ.enum
elseif typ.bits ~= nil then
local prefix, name = typ.name:match "(%u%l+)(.*)"
if prefix ~= lastCombinedFlag then
lastCombinedFlagBlock()
lastCombinedFlag = prefix
end
local combinedFlag = combined[prefix]
if combinedFlag then
combinedFlag.bits = typ.bits
combinedFlag.name = prefix
local flags = combinedFlag.flag or {}
combinedFlag.flag = flags
local lookup = combinedFlag.lookup or {}
combinedFlag.lookup = lookup
for _, flag in ipairs(typ.flag) do
local flagName = name .. flag.name:gsub("_", "")
local value = flag.value
if value == nil then
-- It's a combined flag
value = 0
for _, v in ipairs(flag) do
value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
end
end
lookup[flagName] = value
table.insert(flags, {
name = flagName,
value = value,
2022-05-23 04:43:03 +03:00
comment = flag.comment
2022-04-04 04:42:43 +03:00
})
end
if typ.shift then
table.insert(flags, {
name = name .. "Shift",
value = typ.shift,
format = "%d",
2022-05-23 04:43:03 +03:00
comment = typ.comment
2022-04-04 04:42:43 +03:00
})
end
if typ.mask then
-- generate Mask
table.insert(flags, {
name = name .. "Mask",
value = typ.mask,
2022-05-23 04:43:03 +03:00
comment = typ.comment
2022-04-04 04:42:43 +03:00
})
lookup[name .. "Mask"] = typ.mask
end
else
FlagBlock(typ)
end
elseif typ.struct ~= nil then
local skip = false
if typ.namespace ~= nil then
if namespace ~= typ.namespace then
yield("pub const " .. typ.namespace .. " = extern struct {")
namespace = typ.namespace
indent = " "
end
elseif namespace ~= "" then
indent = " "
namespace = ""
skip = true
end
if not skip then
2022-04-08 16:41:00 +03:00
if typ.name ~= "Encoder" then
yield(indent .. "pub const " .. typ.name .. " = extern struct {")
else
yield(indent .. "pub const " .. typ.name .. " = opaque {")
end
2022-04-04 04:42:43 +03:00
end
for _, member in ipairs(typ.struct) do
2022-04-08 16:41:00 +03:00
yield(indent .. indent .. convert_struct_member(member) .. ",")
2022-04-04 04:42:43 +03:00
end
2022-05-23 04:43:03 +03:00
if funcs ~= nil then
for _, func in ipairs(funcs) do
converter.funcs({
obj = func,
asMethod = true
})
end
end
2022-04-04 04:42:43 +03:00
yield(indent .. "};")
end
end
2022-05-23 04:43:03 +03:00
function converter.funcs(params)
local func = params.obj
2022-04-04 04:42:43 +03:00
if func.cpponly then
return
elseif func.cppinline and not func.conly then
return
2022-04-04 04:42:43 +03:00
end
-- skip for now, don't know how to handle variadic functions
2022-05-23 04:43:03 +03:00
if func.cname == "dbg_text_printf" or func.cname == "dbg_text_vprintf" then
return
end
local func_indent = (params.asMethod == true and indent .. indent or "")
2022-04-04 04:42:43 +03:00
if func.comments ~= nil then
for _, line in ipairs(func.comments) do
2022-05-23 04:43:03 +03:00
yield(func_indent .. "/// " .. line)
2022-04-04 04:42:43 +03:00
end
local hasParams = false
for _, arg in ipairs(func.args) do
if arg.comment ~= nil then
local comment = table.concat(arg.comment, " ")
2022-05-23 04:43:03 +03:00
yield(func_indent .. "/// <param name=\"" .. arg.name .. "\">" .. comment .. "</param>")
2022-04-04 04:42:43 +03:00
hasParams = true
end
end
end
local args = {}
2022-05-23 04:43:03 +03:00
local argNames = {}
2022-04-04 04:42:43 +03:00
if func.this ~= nil then
2022-04-08 16:41:00 +03:00
local ptr = "[*c]"
2022-05-23 04:43:03 +03:00
if params.asMethod == true then
ptr = "*"
end
-- print("Function " .. func.name .. " has this: " .. func.this_type.type)
2022-04-08 16:41:00 +03:00
if func.const ~= nil then
ptr = ptr .. "const "
end
if func.this_type.type == "Encoder" then
ptr = "?*"
end
args[1] = "self: " .. ptr .. func.this_type.type
2022-05-23 04:43:03 +03:00
argNames[1] = "self"
2022-04-04 04:42:43 +03:00
end
for _, arg in ipairs(func.args) do
2022-05-23 04:43:03 +03:00
local argName = arg.name
-- local argName = arg.name:gsub("_", "")
-- argName = argName:gsub("enum", "enumeration")
-- argName = argName:gsub("type_", '@"type"')
2022-04-04 04:42:43 +03:00
if not isempty(argName) then
2022-05-23 04:43:03 +03:00
table.insert(argNames, argName)
2022-04-04 04:42:43 +03:00
table.insert(args, argName .. ": " .. convert_type(arg))
else
table.insert(args, convert_type(arg))
end
end
2022-05-23 04:43:03 +03:00
if (params.asMethod == true) then
yield(wrap_method(func, func.this_type.type, args, argNames, func_indent))
else
if func.this == nil then
yield(wrap_simple_func(func, args, argNames))
end
yield(
"extern fn bgfx_" .. func.cname .. "(" .. table.concat(args, ", ") .. ") " .. convert_ret_type(func.ret) ..
";")
end
2022-04-04 04:42:43 +03:00
end
function gen.write(codes, outputfile)
local out = assert(io.open(outputfile, "wb"))
out:write(codes)
out:close()
print("Generating: " .. outputfile)
end
if (...) == nil then
2022-04-08 16:41:00 +03:00
-- run `lua bindings-zig.lua` in command line
2022-04-04 04:42:43 +03:00
print(gen.gen())
end
return gen