summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/scripts/bindings-zig.lua
blob: ad385ecd3a37394107f9e08ef197fd46b5991aed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
local codegen = require "codegen"
local idl = codegen.idl "bgfx.idl"

local zig_template = [[
// Copyright 2011-2022 Branimir Karadzic. All rights reserved.
// License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE


//
// AUTO GENERATED! DO NOT EDIT!
//


const std = @import("std");

pub const ViewId = u16;

$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

local enum = {}

local function convert_array(member)
	if string.find(member.array, "::") then
		return string.format("[%s]", enum[member.array])
	else
		return member.array
	end
end

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

local function convert_type_0(arg)
	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
		return "[*c]const u8"
	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 "..."
	elseif arg.ctype == "va_list" or arg.fulltype == "bx::AllocatorI*" or arg.fulltype == "CallbackI*" or arg.fulltype ==
		"ReleaseFn" then
		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")
	ctype = ctype:gsub("const void%*", "?*const anyopaque")
	ctype = ctype:gsub("Encoder%*", "?*Encoder")

	if hasSuffix(ctype, "void*") then
		ctype = ctype:gsub("void%*", "?*anyopaque");
	elseif hasSuffix(ctype, "*") then
		ctype = "[*c]" .. ctype:gsub("*", "")
	end

	if arg.array ~= nil then
		ctype = ctype:gsub("const ", "")
		ctype = convert_array(arg) .. ctype
	end

	return ctype
end

local function convert_struct_type(arg)
	return convert_type(arg)
end

local function convert_ret_type(arg)
	return convert_type(arg)
end

local function wrap_simple_func(func, args, argNames)
	local zigFunc = {}
	local zigFuncTemplate = [[pub inline fn $func($params) $ret {
    return $cfunc($args);
}]]

	-- 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);
	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);
%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);
	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

local converter = {}
local yield = coroutine.yield
local gen = {}

local indent = ""

function gen.gen()
	-- 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

	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
			-- we're pretty confident there are no types that have the same name with a func
			local funcs = methods[object.name]
			while true do
				local ok, v = coroutine.resume(co, {
					obj = object,
					funcs = funcs
				})
				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"
	local enumType = "u32"
	if typ.bits == 64 then
		format = "0x%016x"
		enumType = "u64"
	elseif typ.bits == 16 then
		format = "0x%04x"
		enumType = "u16"
	end

	local name = typ.name .. "Flags"
	yield("pub const " .. name .. " = " .. enumType .. ";")

	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
				yield("/// " .. comment)
			end
		end

		local flagName = flag.name:gsub("_", "")
		-- make 2d/3d upper case 2D/3D
		flagName = flagName:gsub("%dd", flagName.upper);
		yield("pub const " .. name .. "_" .. flagName .. ": " .. name .. string.rep(" ", 22 - #(flagName)) .. " = " ..
			string.format(flag.format or format, flag.value) .. ";")
	end

	if typ.shift then
		yield("    " .. "Shift" .. string.rep(" ", 22 - #("Shift")) .. " = " .. flag.shift)
	end

	-- generate Mask
	if typ.mask then
		yield("    " .. "Mask" .. string.rep(" ", 22 - #("Mask")) .. " = " .. string.format(format, flag.mask))
	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)
	return member.name .. ": " .. convert_struct_type(member)
end

local namespace = ""

function converter.types(params)
	local typ = params.obj
	local funcs = params.funcs
	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,
					comment = flag.comment
				})
			end

			if typ.shift then
				table.insert(flags, {
					name = name .. "Shift",
					value = typ.shift,
					format = "%d",
					comment = typ.comment
				})
			end

			if typ.mask then
				-- generate Mask
				table.insert(flags, {
					name = name .. "Mask",
					value = typ.mask,
					comment = typ.comment
				})
				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
			if typ.name ~= "Encoder" then
				yield(indent .. "pub const " .. typ.name .. " = extern struct {")
			else
				yield(indent .. "pub const " .. typ.name .. " = opaque {")
			end
		end

		for _, member in ipairs(typ.struct) do
			yield(indent .. indent .. convert_struct_member(member) .. ",")
		end
		if funcs ~= nil then
			for _, func in ipairs(funcs) do
				converter.funcs({
					obj = func,
					asMethod = true
				})
			end
		end

		yield(indent .. "};")
	end
end

function converter.funcs(params)
	local func = params.obj
	if func.cpponly then
		return
	end

	-- skip for now, don't know how to handle variadic functions
	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 "")

	if func.comments ~= nil then
		for _, line in ipairs(func.comments) do
			yield(func_indent .. "/// " .. line)
		end

		local hasParams = false

		for _, arg in ipairs(func.args) do
			if arg.comment ~= nil then
				local comment = table.concat(arg.comment, " ")

				yield(func_indent .. "/// <param name=\"" .. arg.name .. "\">" .. comment .. "</param>")

				hasParams = true
			end
		end
	end

	local args = {}
	local argNames = {}

	if func.this ~= nil then
		local ptr = "[*c]"
		if params.asMethod == true then
			ptr = "*"
		end
		-- print("Function " .. func.name .. " has this: " .. func.this_type.type)
		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
		argNames[1] = "self"
	end
	for _, arg in ipairs(func.args) do
		local argName = arg.name
		-- local argName = arg.name:gsub("_", "")
		-- argName = argName:gsub("enum", "enumeration")
		-- argName = argName:gsub("type_", '@"type"')
		if not isempty(argName) then
			table.insert(argNames, argName)
			table.insert(args, argName .. ": " .. convert_type(arg))
		else
			table.insert(args, convert_type(arg))
		end
	end

	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
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
	-- run `lua bindings-zig.lua` in command line
	print(gen.gen())
end

return gen