bindings-zig.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. local codegen = require "codegen"
  2. local idl = codegen.idl "bgfx.idl"
  3. local zig_template = [[
  4. // Copyright 2011-2022 Branimir Karadzic. All rights reserved.
  5. // License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  6. //
  7. // AUTO GENERATED! DO NOT EDIT!
  8. //
  9. const std = @import("std");
  10. pub const ViewId = u16;
  11. $types
  12. $funcs
  13. ]]
  14. local function isempty(s)
  15. return s == nil or s == ''
  16. end
  17. local function hasPrefix(str, prefix)
  18. return prefix == "" or str:sub(1, #prefix) == prefix
  19. end
  20. local function hasSuffix(str, suffix)
  21. return suffix == "" or str:sub(- #suffix) == suffix
  22. end
  23. local enum = { }
  24. local function convert_array(member)
  25. if string.find(member.array, "::") then
  26. return string.format("[%s]", enum[member.array])
  27. else
  28. return member.array
  29. end
  30. end
  31. local function convert_type_0(arg)
  32. if hasPrefix(arg.ctype, "uint64_t") then
  33. return arg.ctype:gsub("uint64_t", "u64")
  34. elseif hasPrefix(arg.ctype, "int64_t") then
  35. return arg.ctype:gsub("int64_t", "i64")
  36. elseif hasPrefix(arg.ctype, "uint32_t") then
  37. return arg.ctype:gsub("uint32_t", "u32")
  38. elseif hasPrefix(arg.ctype, "int32_t") then
  39. return arg.ctype:gsub("int32_t", "i32")
  40. elseif hasPrefix(arg.ctype, "uint16_t") then
  41. return arg.ctype:gsub("uint16_t", "u16")
  42. elseif hasPrefix(arg.ctype, "uint8_t") then
  43. return arg.ctype:gsub("uint8_t", "u8")
  44. elseif hasPrefix(arg.ctype, "uintptr_t") then
  45. return arg.ctype:gsub("uintptr_t", "usize")
  46. elseif hasPrefix(arg.ctype, "float") then
  47. return arg.ctype:gsub("float", "f32")
  48. elseif arg.ctype == "const char*" then
  49. return "[*c]const u8"
  50. elseif hasPrefix(arg.ctype, "char") then
  51. return arg.ctype:gsub("char", "u8")
  52. elseif hasSuffix(arg.fulltype, "Handle") then
  53. return arg.fulltype
  54. elseif arg.ctype == "..." then
  55. return "..."
  56. elseif arg.ctype == "va_list"
  57. or arg.fulltype == "bx::AllocatorI*"
  58. or arg.fulltype == "CallbackI*"
  59. or arg.fulltype == "ReleaseFn" then
  60. return "?*anyopaque"
  61. end
  62. return arg.fulltype
  63. end
  64. local function convert_type(arg)
  65. local ctype = convert_type_0(arg)
  66. ctype = ctype:gsub("::Enum", "")
  67. ctype = ctype:gsub(" &", "*")
  68. ctype = ctype:gsub("&", "*")
  69. ctype = ctype:gsub("char", "u8")
  70. ctype = ctype:gsub("float", "f32")
  71. ctype = ctype:gsub("const void%*", "?*const anyopaque")
  72. ctype = ctype:gsub("Encoder%*", "?*Encoder")
  73. if hasSuffix(ctype, "void*") then
  74. ctype = ctype:gsub("void%*", "?*anyopaque");
  75. elseif hasSuffix(ctype, "*") then
  76. ctype = "[*c]" .. ctype:gsub("*", "")
  77. end
  78. if arg.array ~= nil then
  79. ctype = ctype:gsub("const ", "")
  80. ctype = convert_array(arg) .. ctype
  81. end
  82. return ctype
  83. end
  84. local function convert_struct_type(arg)
  85. return convert_type(arg)
  86. end
  87. local function convert_ret_type(arg)
  88. return convert_type(arg)
  89. end
  90. local converter = {}
  91. local yield = coroutine.yield
  92. local gen = {}
  93. local indent = ""
  94. function gen.gen()
  95. local r = zig_template:gsub("$(%l+)", function(what)
  96. local tmp = {}
  97. for _, object in ipairs(idl[what]) do
  98. local co = coroutine.create(converter[what])
  99. local any
  100. while true do
  101. local ok, v = coroutine.resume(co, object)
  102. assert(ok, debug.traceback(co, v))
  103. if not v then
  104. break
  105. end
  106. table.insert(tmp, v)
  107. any = true
  108. end
  109. if any and tmp[#tmp] ~= "" then
  110. table.insert(tmp, "")
  111. end
  112. end
  113. return table.concat(tmp, "\n")
  114. end)
  115. return r
  116. end
  117. local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
  118. for _, v in ipairs(combined) do
  119. combined[v] = {}
  120. end
  121. local lastCombinedFlag
  122. local function FlagBlock(typ)
  123. local format = "0x%08x"
  124. local enumType = "u32"
  125. if typ.bits == 64 then
  126. format = "0x%016x"
  127. enumType = "u64"
  128. elseif typ.bits == 16 then
  129. format = "0x%04x"
  130. enumType = "u16"
  131. end
  132. local name = typ.name .. "Flags"
  133. yield("pub const " .. name .. " = " .. enumType .. ";")
  134. for idx, flag in ipairs(typ.flag) do
  135. if flag.comment ~= nil then
  136. if idx ~= 1 then
  137. yield("")
  138. end
  139. for _, comment in ipairs(flag.comment) do
  140. yield("/// " .. comment)
  141. end
  142. end
  143. local flagName = flag.name:gsub("_", "")
  144. yield("pub const " .. name .. "_"
  145. .. flagName
  146. .. ": "
  147. .. name
  148. .. string.rep(" ", 22 - #(flagName))
  149. .. " = "
  150. .. string.format(flag.format or format, flag.value)
  151. .. ";"
  152. )
  153. end
  154. if typ.shift then
  155. yield(" "
  156. .. "Shift"
  157. .. string.rep(" ", 22 - #("Shift"))
  158. .. " = "
  159. .. flag.shift
  160. )
  161. end
  162. -- generate Mask
  163. if typ.mask then
  164. yield(" "
  165. .. "Mask"
  166. .. string.rep(" ", 22 - #("Mask"))
  167. .. " = "
  168. .. string.format(format, flag.mask)
  169. )
  170. end
  171. end
  172. local function lastCombinedFlagBlock()
  173. if lastCombinedFlag then
  174. local typ = combined[lastCombinedFlag]
  175. if typ then
  176. FlagBlock(combined[lastCombinedFlag])
  177. yield("")
  178. end
  179. lastCombinedFlag = nil
  180. end
  181. end
  182. local function convert_struct_member(member)
  183. return member.name .. ": " .. convert_struct_type(member)
  184. end
  185. local namespace = ""
  186. function converter.types(typ)
  187. if typ.handle then
  188. lastCombinedFlagBlock()
  189. yield("pub const " .. typ.name .. " = extern struct {")
  190. yield(" idx: c_ushort,")
  191. yield("};")
  192. elseif hasSuffix(typ.name, "::Enum") then
  193. lastCombinedFlagBlock()
  194. yield("pub const " .. typ.typename .. " = enum(c_int) {")
  195. for idx, enum in ipairs(typ.enum) do
  196. if enum.comment ~= nil then
  197. if idx ~= 1 then
  198. yield("")
  199. end
  200. for _, comment in ipairs(enum.comment) do
  201. yield(" /// " .. comment)
  202. end
  203. end
  204. yield(" " .. enum.name .. ",")
  205. end
  206. yield("");
  207. yield(" Count")
  208. yield("};")
  209. enum["[" .. typ.typename .. "::Count]"] = #typ.enum
  210. elseif typ.bits ~= nil then
  211. local prefix, name = typ.name:match "(%u%l+)(.*)"
  212. if prefix ~= lastCombinedFlag then
  213. lastCombinedFlagBlock()
  214. lastCombinedFlag = prefix
  215. end
  216. local combinedFlag = combined[prefix]
  217. if combinedFlag then
  218. combinedFlag.bits = typ.bits
  219. combinedFlag.name = prefix
  220. local flags = combinedFlag.flag or {}
  221. combinedFlag.flag = flags
  222. local lookup = combinedFlag.lookup or {}
  223. combinedFlag.lookup = lookup
  224. for _, flag in ipairs(typ.flag) do
  225. local flagName = name .. flag.name:gsub("_", "")
  226. local value = flag.value
  227. if value == nil then
  228. -- It's a combined flag
  229. value = 0
  230. for _, v in ipairs(flag) do
  231. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  232. end
  233. end
  234. lookup[flagName] = value
  235. table.insert(flags, {
  236. name = flagName,
  237. value = value,
  238. comment = flag.comment,
  239. })
  240. end
  241. if typ.shift then
  242. table.insert(flags, {
  243. name = name .. "Shift",
  244. value = typ.shift,
  245. format = "%d",
  246. comment = typ.comment,
  247. })
  248. end
  249. if typ.mask then
  250. -- generate Mask
  251. table.insert(flags, {
  252. name = name .. "Mask",
  253. value = typ.mask,
  254. comment = typ.comment,
  255. })
  256. lookup[name .. "Mask"] = typ.mask
  257. end
  258. else
  259. FlagBlock(typ)
  260. end
  261. elseif typ.struct ~= nil then
  262. local skip = false
  263. if typ.namespace ~= nil then
  264. if namespace ~= typ.namespace then
  265. yield("pub const " .. typ.namespace .. " = extern struct {")
  266. namespace = typ.namespace
  267. indent = " "
  268. end
  269. elseif namespace ~= "" then
  270. indent = " "
  271. namespace = ""
  272. skip = true
  273. end
  274. if not skip then
  275. if typ.name ~= "Encoder" then
  276. yield(indent .. "pub const " .. typ.name .. " = extern struct {")
  277. else
  278. yield(indent .. "pub const " .. typ.name .. " = opaque {")
  279. end
  280. end
  281. for _, member in ipairs(typ.struct) do
  282. yield(indent .. indent .. convert_struct_member(member) .. ",")
  283. end
  284. yield(indent .. "};")
  285. end
  286. end
  287. function converter.funcs(func)
  288. if func.cpponly then
  289. return
  290. end
  291. if func.comments ~= nil then
  292. for _, line in ipairs(func.comments) do
  293. yield("/// " .. line)
  294. end
  295. local hasParams = false
  296. for _, arg in ipairs(func.args) do
  297. if arg.comment ~= nil then
  298. local comment = table.concat(arg.comment, " ")
  299. yield("/// <param name=\""
  300. .. arg.name
  301. .. "\">"
  302. .. comment
  303. .. "</param>"
  304. )
  305. hasParams = true
  306. end
  307. end
  308. end
  309. local args = {}
  310. if func.this ~= nil then
  311. local ptr = "[*c]"
  312. if func.const ~= nil then
  313. ptr = ptr .. "const "
  314. end
  315. if func.this_type.type == "Encoder" then
  316. ptr = "?*"
  317. end
  318. args[1] = "self: " .. ptr .. func.this_type.type
  319. end
  320. for _, arg in ipairs(func.args) do
  321. local argName = arg.name:gsub("_", "")
  322. argName = argName:gsub("enum", "enumeration")
  323. if not isempty(argName) then
  324. table.insert(args, argName .. ": " .. convert_type(arg))
  325. else
  326. table.insert(args, convert_type(arg))
  327. end
  328. end
  329. yield("pub extern fn bgfx_" .. func.cname .. "(" .. table.concat(args, ", ") .. ") " .. convert_ret_type(func.ret) .. ";")
  330. end
  331. function gen.write(codes, outputfile)
  332. local out = assert(io.open(outputfile, "wb"))
  333. out:write(codes)
  334. out:close()
  335. print("Generating: " .. outputfile)
  336. end
  337. if (...) == nil then
  338. -- run `lua bindings-zig.lua` in command line
  339. print(gen.gen())
  340. end
  341. return gen