codegen.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. -- Copyright 2019 云风 https://github.com/cloudwu . All rights reserved.
  2. -- License (the same with bgfx) : https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  3. local codegen = {}
  4. local NAMEALIGN = 20
  5. local function namealign(name, align)
  6. align = align or NAMEALIGN
  7. return string.rep(" ", align - #name)
  8. end
  9. local function camelcase_to_underscorecase(name)
  10. local tmp = {}
  11. for v in name:gmatch "[%u%d]+%l*" do
  12. tmp[#tmp+1] = v:lower()
  13. end
  14. return table.concat(tmp, "_")
  15. end
  16. local function to_underscorecase(name)
  17. local tmp = {}
  18. for v in name:gmatch "[_%u][%l%d]*" do
  19. if v:byte() == 95 then -- '_'
  20. v = v:sub(2) -- remove _
  21. end
  22. tmp[#tmp+1] = v
  23. end
  24. return table.concat(tmp, "_")
  25. end
  26. local function underscorecase_to_camelcase(name)
  27. local tmp = {}
  28. for v in name:gmatch "[^_]+" do
  29. tmp[#tmp+1] = v:sub(1,1):upper() .. v:sub(2)
  30. end
  31. return table.concat(tmp)
  32. end
  33. local function convert_funcname(name)
  34. name = name:gsub("^%l", string.upper) -- Change to upper CamlCase
  35. return camelcase_to_underscorecase(name)
  36. end
  37. local function convert_arg(all_types, arg, namespace)
  38. local fulltype, array = arg.fulltype:match "(.-)%s*(%[%s*[%d%a_:]*%s*%])"
  39. if array then
  40. arg.fulltype = fulltype
  41. arg.array = array
  42. local enum, value = array:match "%[%s*([%a%d]+)::([%a%d]+)%]"
  43. if enum then
  44. local typedef = all_types[ enum .. "::Enum" ]
  45. if typedef == nil then
  46. error ("Unknown Enum " .. enum)
  47. end
  48. arg.carray = "[BGFX_" .. camelcase_to_underscorecase(enum):upper() .. "_" .. value:upper() .. "]"
  49. end
  50. end
  51. local t, postfix = arg.fulltype:match "(%a[%a%d_:]*)%s*([*&]+)%s*$"
  52. if t then
  53. arg.type = t
  54. if postfix == "&" then
  55. arg.ref = true
  56. end
  57. else
  58. local prefix, t = arg.fulltype:match "^%s*(%a+)%s+(%S+)"
  59. if prefix then
  60. arg.type = t
  61. else
  62. arg.type = arg.fulltype
  63. end
  64. end
  65. local ctype
  66. local substruct = namespace.substruct
  67. if substruct then
  68. ctype = substruct[arg.type]
  69. end
  70. if not ctype then
  71. ctype = all_types[arg.type]
  72. end
  73. if not ctype then
  74. error ("Undefined type " .. arg.fulltype .. " in " .. namespace.name)
  75. end
  76. arg.ctype = arg.fulltype:gsub(arg.type, ctype.cname):gsub("&", "*")
  77. if ctype.cname ~= arg.type then
  78. arg.cpptype = arg.fulltype:gsub(arg.type, "bgfx::"..arg.type)
  79. else
  80. arg.cpptype = arg.fulltype
  81. end
  82. if arg.ref then
  83. arg.ptype = arg.cpptype:gsub("&", "*")
  84. end
  85. end
  86. local function alternative_name(name)
  87. if name:sub(1,1) == "_" then
  88. return name:sub(2)
  89. else
  90. return name .. "_"
  91. end
  92. end
  93. local function gen_arg_conversion(all_types, arg)
  94. if arg.ctype == arg.fulltype then
  95. -- do not need conversion
  96. return
  97. end
  98. local ctype = all_types[arg.type]
  99. if ctype.handle and arg.type == arg.fulltype then
  100. local aname = alternative_name(arg.name)
  101. arg.aname = aname .. ".cpp"
  102. arg.aname_cpp2c = aname .. ".c"
  103. arg.conversion = string.format(
  104. "union { %s c; bgfx::%s cpp; } %s = { %s };" ,
  105. ctype.cname, arg.type, aname, arg.name)
  106. arg.conversion_back = string.format(
  107. "union { bgfx::%s cpp; %s c; } %s = { %s };" ,
  108. arg.type, ctype.cname, aname, arg.name)
  109. elseif arg.ref then
  110. if ctype.cname == arg.type then
  111. arg.aname = "*" .. arg.name
  112. arg.aname_cpp2c = "&" .. arg.name
  113. elseif arg.out and ctype.enum then
  114. local aname = alternative_name(arg.name)
  115. local cpptype = arg.cpptype:match "(.-)%s*&" -- remove &
  116. local c99type = arg.ctype:match "(.-)%s*%*" -- remove *
  117. arg.aname = aname
  118. arg.aname_cpp2c = "&" .. aname
  119. arg.conversion = string.format("%s %s;", cpptype, aname)
  120. arg.conversion_back = string.format("%s %s;", c99type, aname);
  121. arg.out_conversion = string.format("*%s = (%s)%s;", arg.name, ctype.cname, aname)
  122. arg.out_conversion_back = string.format("%s = (%s)%s;", arg.name, c99type, aname)
  123. else
  124. arg.aname = alternative_name(arg.name)
  125. arg.aname_cpp2c = string.format("(%s)&%s" , arg.ctype , arg.name)
  126. arg.conversion = string.format(
  127. "%s %s = *(%s)%s;",
  128. arg.cpptype, arg.aname, arg.ptype, arg.name)
  129. end
  130. else
  131. local cpptype = arg.cpptype
  132. local ctype = arg.ctype
  133. if arg.array then
  134. cpptype = cpptype .. "*"
  135. ctype = ctype .. "*"
  136. end
  137. arg.aname = string.format(
  138. "(%s)%s",
  139. cpptype, arg.name)
  140. arg.aname_cpp2c = string.format(
  141. "(%s)%s",
  142. ctype, arg.name)
  143. end
  144. end
  145. local function gen_ret_conversion(all_types, func)
  146. local postfix = { func.vararg and "va_end(argList);" }
  147. local postfix_cpp2c = { postfix[1] }
  148. func.ret_postfix = postfix
  149. func.ret_postfix_cpp2c = postfix_cpp2c
  150. for _, arg in ipairs(func.args) do
  151. if arg.out_conversion then
  152. postfix[#postfix+1] = arg.out_conversion
  153. postfix_cpp2c[#postfix_cpp2c+1] = arg.out_conversion_back
  154. end
  155. end
  156. local ctype = all_types[func.ret.type]
  157. if ctype.handle then
  158. func.ret_conversion = string.format(
  159. "union { %s c; bgfx::%s cpp; } handle_ret;" ,
  160. ctype.cname, ctype.name)
  161. func.ret_conversion_cpp2c = string.format(
  162. "union { bgfx::%s cpp; %s c; } handle_ret;" ,
  163. ctype.name, ctype.cname)
  164. func.ret_prefix = "handle_ret.cpp = "
  165. func.ret_prefix_cpp2c = "handle_ret.c = "
  166. postfix[#postfix+1] = "return handle_ret.c;"
  167. postfix_cpp2c[#postfix_cpp2c+1] = "return handle_ret.cpp;"
  168. elseif func.ret.fulltype ~= "void" then
  169. local ctype_conversion = ""
  170. local conversion_back = ""
  171. if ctype.name ~= ctype.cname then
  172. if func.ret.ref then
  173. ctype_conversion = "(" .. func.ret.ctype .. ")&"
  174. conversion_back = "*(" .. func.ret.ptype .. ")"
  175. else
  176. ctype_conversion = "(" .. func.ret.ctype .. ")"
  177. conversion_back = "(" .. func.ret.cpptype .. ")"
  178. end
  179. end
  180. if #postfix > 0 then
  181. func.ret_prefix = string.format("%s retValue = %s", func.ret.ctype , ctype_conversion)
  182. func.ret_prefix_cpp2c = string.format("%s retValue = %s", func.ret.cpptype , conversion_back)
  183. local ret = "return retValue;"
  184. postfix[#postfix+1] = ret
  185. postfix_cpp2c[#postfix_cpp2c+1] = ret
  186. else
  187. func.ret_prefix = string.format("return %s", ctype_conversion)
  188. func.ret_prefix_cpp2c = string.format("return %s", conversion_back)
  189. end
  190. end
  191. end
  192. local function convert_vararg(v)
  193. if v.vararg then
  194. local args = v.args
  195. local vararg = {
  196. name = "",
  197. fulltype = "...",
  198. type = "...",
  199. ctype = "...",
  200. aname = "argList",
  201. conversion = string.format(
  202. "va_list argList;\n\tva_start(argList, %s);",
  203. args[#args].name),
  204. }
  205. args[#args + 1] = vararg
  206. v.alter_name = v.vararg
  207. end
  208. end
  209. function codegen.nameconversion(all_types, all_funcs)
  210. for _,v in ipairs(all_types) do
  211. local name = v.name
  212. local cname = v.cname
  213. if cname == nil then
  214. if name:match "^%u" then
  215. cname = camelcase_to_underscorecase(name)
  216. elseif not v.flag then
  217. v.cname = name
  218. end
  219. end
  220. if cname and not v.flag then
  221. if v.namespace then
  222. cname = camelcase_to_underscorecase(v.namespace) .. "_" .. cname
  223. end
  224. v.cname = "bgfx_".. cname .. "_t"
  225. end
  226. if v.enum then
  227. v.typename = v.name
  228. v.name = v.name .. "::Enum"
  229. end
  230. end
  231. -- make index
  232. for _,v in ipairs(all_types) do
  233. if not v.namespace then
  234. if all_types[v.name] then
  235. error ("Duplicate type " .. v.name)
  236. elseif not v.flag then
  237. all_types[v.name] = v
  238. end
  239. end
  240. end
  241. -- make sub struct index
  242. for _,v in ipairs(all_types) do
  243. if v.namespace then
  244. local super = all_types[v.namespace]
  245. if not super then
  246. error ("Define " .. v.namespace .. " first")
  247. end
  248. local substruct = super.substruct
  249. if not substruct then
  250. substruct = {}
  251. super.substruct = substruct
  252. end
  253. if substruct[v.name] then
  254. error ( "Duplicate sub struct " .. v.name .. " in " .. v.namespace)
  255. end
  256. substruct[#substruct+1] = v
  257. substruct[v.name] = v
  258. end
  259. end
  260. for _,v in ipairs(all_types) do
  261. if v.struct then
  262. for _, item in ipairs(v.struct) do
  263. convert_arg(all_types, item, v)
  264. end
  265. elseif v.args then
  266. -- funcptr
  267. for _, arg in ipairs(v.args) do
  268. convert_arg(all_types, arg, v)
  269. end
  270. convert_vararg(v)
  271. convert_arg(all_types, v.ret, v)
  272. end
  273. end
  274. local funcs = {}
  275. local funcs_conly = {}
  276. local funcs_alter = {}
  277. for _,v in ipairs(all_funcs) do
  278. if v.cname == nil then
  279. v.cname = convert_funcname(v.name)
  280. end
  281. if v.class then
  282. v.cname = convert_funcname(v.class) .. "_" .. v.cname
  283. local classtype = all_types[v.class]
  284. if classtype then
  285. local methods = classtype.methods
  286. if not methods then
  287. methods = {}
  288. classtype.methods = methods
  289. end
  290. methods[#methods+1] = v
  291. end
  292. elseif not v.conly then
  293. funcs[v.name] = v
  294. end
  295. if v.conly then
  296. table.insert(funcs_conly, v)
  297. end
  298. for _, arg in ipairs(v.args) do
  299. convert_arg(all_types, arg, v)
  300. gen_arg_conversion(all_types, arg)
  301. end
  302. convert_vararg(v)
  303. if v.alter_name then
  304. funcs_alter[#funcs_alter+1] = v
  305. end
  306. convert_arg(all_types, v.ret, v)
  307. gen_ret_conversion(all_types, v)
  308. local namespace = v.class
  309. if namespace then
  310. local classname = namespace
  311. if v.const then
  312. classname = "const " .. classname
  313. end
  314. local classtype = { fulltype = classname .. "*" }
  315. convert_arg(all_types, classtype, v)
  316. v.this = classtype.ctype .. " _this"
  317. v.this_conversion = string.format( "%s This = (%s)_this;", classtype.cpptype, classtype.cpptype)
  318. v.this_to_c = string.format("(%s)this", classtype.ctype)
  319. end
  320. end
  321. for _, v in ipairs(funcs_conly) do
  322. local func = funcs[v.name]
  323. if func then
  324. func.multicfunc = func.multicfunc or { func.cname }
  325. table.insert(func.multicfunc, v.cname)
  326. end
  327. end
  328. for _, v in ipairs(funcs_alter) do
  329. local func = funcs[v.alter_name]
  330. v.alter_cname = func.cname
  331. end
  332. end
  333. local function lines(tbl)
  334. if not tbl or #tbl == 0 then
  335. return "//EMPTYLINE"
  336. else
  337. return table.concat(tbl, "\n\t")
  338. end
  339. end
  340. local function remove_emptylines(txt)
  341. return (txt:gsub("\t//EMPTYLINE\n", ""))
  342. end
  343. local function codetemp(func)
  344. local conversion = {}
  345. local conversion_c2cpp = {}
  346. local args = {}
  347. local cargs = {}
  348. local callargs_conversion = {}
  349. local callargs_conversion_back = {}
  350. local callargs = {}
  351. local cppfunc
  352. local classname
  353. if func.class then
  354. -- It's a member function
  355. cargs[1] = func.this
  356. conversion[1] = func.this_conversion
  357. cppfunc = "This->" .. func.name
  358. callargs[1] = "_this"
  359. callargs_conversion_back[1] = func.this_to_c
  360. classname = func.class .. "::"
  361. else
  362. cppfunc = "bgfx::" .. tostring(func.alter_name or func.name)
  363. classname = ""
  364. end
  365. for _, arg in ipairs(func.args) do
  366. conversion[#conversion+1] = arg.conversion
  367. conversion_c2cpp[#conversion_c2cpp+1] = arg.conversion_back
  368. local cname = arg.ctype .. " " .. arg.name
  369. if arg.array then
  370. cname = cname .. (arg.carray or arg.array)
  371. end
  372. local name = arg.fulltype .. " " .. arg.name
  373. if arg.array then
  374. name = name .. arg.array
  375. end
  376. if arg.default ~= nil then
  377. name = name .. " = " .. tostring(arg.default)
  378. end
  379. cargs[#cargs+1] = cname
  380. args[#args+1] = name
  381. callargs_conversion[#callargs_conversion+1] = arg.aname or arg.name
  382. callargs_conversion_back[#callargs_conversion_back+1] = arg.aname_cpp2c or arg.name
  383. callargs[#callargs+1] = arg.name
  384. end
  385. conversion[#conversion+1] = func.ret_conversion
  386. conversion_c2cpp[#conversion_c2cpp+1] = func.ret_conversion_cpp2c
  387. local ARGS
  388. local args_n = #args
  389. if args_n == 0 then
  390. ARGS = ""
  391. elseif args_n == 1 then
  392. ARGS = args[1]
  393. else
  394. ARGS = "\n\t " .. table.concat(args, "\n\t, ") .. "\n\t"
  395. end
  396. local preret_c2c
  397. local postret_c2c = {}
  398. local conversion_c2c = {}
  399. local callfunc_c2c
  400. if func.vararg then
  401. postret_c2c[1] = "va_end(argList);"
  402. local vararg = func.args[#func.args]
  403. callargs[#callargs] = vararg.aname
  404. callargs_conversion_back[#callargs_conversion_back] = vararg.aname
  405. conversion_c2c[1] = vararg.conversion
  406. conversion_c2cpp[1] = vararg.conversion
  407. if func.ret.fulltype == "void" then
  408. preret_c2c = ""
  409. else
  410. preret_c2c = func.ret.ctype .. " retValue = "
  411. postret_c2c[#postret_c2c+1] = "return retValue;"
  412. end
  413. callfunc_c2c = func.alter_cname or func.cname
  414. else
  415. if func.ret.fulltype == "void" then
  416. preret_c2c = ""
  417. else
  418. preret_c2c = "return "
  419. end
  420. callfunc_c2c = func.cname
  421. end
  422. outCargs = table.concat(cargs, ", ")
  423. if outCargs == "" then
  424. outCargs = "void"
  425. end
  426. return {
  427. RET = func.ret.fulltype,
  428. CRET = func.ret.ctype,
  429. CFUNCNAME = func.cname,
  430. CFUNCNAMEUPPER = func.cname:upper(),
  431. CFUNCNAMECAML = underscorecase_to_camelcase(func.cname),
  432. FUNCNAME = func.name,
  433. CARGS = outCargs,
  434. CPPARGS = table.concat(args, ", "),
  435. ARGS = ARGS,
  436. CONVERSION = lines(conversion),
  437. CONVERSIONCTOC = lines(conversion_c2c),
  438. CONVERSIONCTOCPP = lines(conversion_c2cpp),
  439. PRERET = func.ret_prefix or "",
  440. PRERETCPPTOC = func.ret_prefix_cpp2c or "",
  441. CPPFUNC = cppfunc,
  442. CALLFUNCCTOC = callfunc_c2c,
  443. CALLARGSCTOCPP = table.concat(callargs_conversion, ", "),
  444. CALLARGSCPPTOC = table.concat(callargs_conversion_back, ", "),
  445. CALLARGS = table.concat(callargs, ", "),
  446. POSTRET = lines(func.ret_postfix),
  447. POSTRETCPPTOC = lines(func.ret_postfix_cpp2c),
  448. PRERETCTOC = preret_c2c,
  449. POSTRETCTOC = lines(postret_c2c),
  450. CLASSNAME = classname,
  451. CONST = func.const and " const" or "",
  452. }
  453. end
  454. local function apply_template(func, temp)
  455. func.codetemp = func.codetemp or codetemp(func)
  456. return (temp:gsub("$(%u+)", func.codetemp))
  457. end
  458. function codegen.apply_functemp(func, temp)
  459. return remove_emptylines(apply_template(func, temp))
  460. end
  461. function codegen.gen_funcptr(funcptr)
  462. return apply_template(funcptr, "typedef $RET (*$FUNCNAME)($ARGS);")
  463. end
  464. function codegen.gen_cfuncptr(funcptr)
  465. return apply_template(funcptr, "typedef $CRET (*$CFUNCNAME)($CARGS);")
  466. end
  467. local function doxygen_funcret(r, func, prefix)
  468. if not func or func.ret.fulltype == "void" or func.ret.comment == nil then
  469. return
  470. end
  471. r[#r+1] = prefix
  472. if type(func.ret.comment) == "string" then
  473. r[#r+1] = string.format("%s @returns %s", prefix, func.ret.comment)
  474. else
  475. r[#r+1] = string.format("%s @returns %s", prefix, func.ret.comment[1])
  476. for i = 2,#func.ret.comment do
  477. r[#r+1] = string.format("%s %s", prefix, func.ret.comment[i])
  478. end
  479. end
  480. return r
  481. end
  482. local function doxygen_func(r, func, prefix)
  483. if not func or not func.args or #func.args == 0 then
  484. return
  485. end
  486. r[#r+1] = prefix
  487. for _, arg in ipairs(func.args) do
  488. local inout
  489. if arg.out then
  490. inout = "out"
  491. elseif arg.inout then
  492. inout = "inout"
  493. else
  494. inout = "in"
  495. end
  496. local comment = string.format("%s @param[%s] %s", prefix, inout, arg.name)
  497. if arg.comment then
  498. if type(arg.comment) == "string" then
  499. r[#r+1] = comment .. " " .. arg.comment
  500. else
  501. r[#r+1] = comment .. " " .. arg.comment[1]
  502. for i = 2,#arg.comment do
  503. r[#r+1] = string.format("%s %s", prefix, arg.comment[i])
  504. end
  505. end
  506. else
  507. r[#r+1] = comment
  508. end
  509. end
  510. doxygen_funcret(r, func, prefix)
  511. return r
  512. end
  513. function codegen.doxygen_type(doxygen, func, cname)
  514. if doxygen == nil then
  515. return
  516. end
  517. local result = {}
  518. for _, line in ipairs(doxygen) do
  519. result[#result+1] = "/// " .. line
  520. end
  521. doxygen_func(result, func, "///")
  522. if cname then
  523. result[#result+1] = "///"
  524. if type(cname) == "string" then
  525. result[#result+1] = string.format("/// @attention C99 equivalent is `%s`.", cname)
  526. else
  527. local names = {}
  528. for _, v in ipairs(cname) do
  529. names[#names+1] = "`" .. v .. "`"
  530. end
  531. result[#result+1] = string.format("/// @attention C99 equivalent are %s.", table.concat(names, ","))
  532. end
  533. end
  534. result[#result+1] = "///"
  535. return table.concat(result, "\n")
  536. end
  537. function codegen.doxygen_ctype(doxygen, func)
  538. if doxygen == nil then
  539. return
  540. end
  541. local result = {
  542. "/**",
  543. }
  544. for _, line in ipairs(doxygen) do
  545. result[#result+1] = " * " .. line
  546. end
  547. doxygen_func(result, func, " *")
  548. result[#result+1] = " *"
  549. result[#result+1] = " */"
  550. return table.concat(result, "\n")
  551. end
  552. local enum_temp = [[
  553. struct $NAME
  554. {
  555. $COMMENT
  556. enum Enum
  557. {
  558. $ITEMS
  559. Count
  560. };
  561. };
  562. ]]
  563. function codegen.gen_enum_define(enum)
  564. assert(type(enum.enum) == "table", "Not an enum")
  565. local items = {}
  566. for _, item in ipairs(enum.enum) do
  567. local text
  568. if not item.comment then
  569. text = item.name .. ","
  570. else
  571. text = string.format("%s,%s //!< %s",
  572. item.name, namealign(item.name), item.comment)
  573. end
  574. items[#items+1] = text
  575. end
  576. local comment = ""
  577. if enum.comment then
  578. comment = "/// " .. enum.comment
  579. end
  580. local temp = {
  581. NAME = enum.typename,
  582. COMMENT = comment,
  583. ITEMS = table.concat(items, "\n\t\t"),
  584. }
  585. return (enum_temp:gsub("$(%u+)", temp))
  586. end
  587. local cenum_temp = [[
  588. typedef enum $NAME
  589. {
  590. $ITEMS
  591. $COUNT
  592. } $NAME_t;
  593. ]]
  594. function codegen.gen_enum_cdefine(enum)
  595. assert(type(enum.enum) == "table", "Not an enum")
  596. local cname = enum.cname:match "(.-)_t$"
  597. local uname = cname:upper()
  598. local items = {}
  599. for index , item in ipairs(enum.enum) do
  600. local comment = item.comment or ""
  601. local ename = item.cname
  602. if not ename then
  603. if enum.underscore then
  604. ename = camelcase_to_underscorecase(item.name)
  605. else
  606. ename = item.name
  607. end
  608. ename = ename:upper()
  609. end
  610. local name = uname .. "_" .. ename
  611. items[#items+1] = string.format("%s,%s /** (%2d) %s%s */",
  612. name,
  613. namealign(name, 40),
  614. index - 1,
  615. comment,
  616. namealign(comment, 30))
  617. end
  618. local temp = {
  619. NAME = cname,
  620. COUNT = uname .. "_COUNT",
  621. ITEMS = table.concat(items, "\n\t"),
  622. }
  623. return (cenum_temp:gsub("$(%u+)", temp))
  624. end
  625. local function flag_format(flag)
  626. if not flag.format then
  627. flag.format = "%0" .. (flag.bits // 4) .. "x"
  628. end
  629. end
  630. function codegen.gen_flag_cdefine(flag)
  631. assert(type(flag.flag) == "table", "Not a flag")
  632. flag_format(flag)
  633. local cname = "BGFX_" .. (flag.cname or to_underscorecase(flag.name):upper())
  634. local s = {}
  635. local shift = flag.shift
  636. local base = flag.base or 0
  637. local cap = 1 << (flag.range or 0)
  638. for index, item in ipairs(flag.flag) do
  639. local name
  640. if item.cname then
  641. name = cname .. "_" .. item.cname
  642. else
  643. name = cname .. "_" .. to_underscorecase(item.name):upper()
  644. end
  645. local value = item.value
  646. if flag.const then
  647. -- use value directly
  648. elseif shift then
  649. if value then
  650. if value > 0 then
  651. value = value - 1
  652. end
  653. else
  654. value = index + base - 1
  655. end
  656. if value >= cap then
  657. error (string.format("Out of range for %s (%d/%d)", name, value, cap))
  658. end
  659. value = value << shift
  660. elseif #item == 0 then
  661. if value then
  662. if value > 0 then
  663. value = 1 << (value - 1)
  664. end
  665. else
  666. local s = index + base - 2
  667. if s >= 0 then
  668. value = 1 << s
  669. else
  670. value = 0
  671. end
  672. end
  673. end
  674. if value == nil then
  675. if item.comment then
  676. if type(item.comment) == "table" then
  677. for _, c in ipairs(item.comment) do
  678. s[#s+1] = "/// " .. c
  679. end
  680. else
  681. s[#s+1] = "/// " .. item.comment
  682. end
  683. end
  684. local sets = { "" }
  685. for _, v in ipairs(item) do
  686. sets[#sets+1] = cname .. "_" .. to_underscorecase(v):upper()
  687. end
  688. s[#s+1] = string.format("#define %s (0%s \\\n\t)\n", name, table.concat(sets, " \\\n\t| "))
  689. else
  690. local comment = ""
  691. if item.comment then
  692. comment = " //!< " .. item.comment
  693. end
  694. value = string.format(flag.format, value)
  695. local code = string.format("#define %s %sUINT%d_C(0x%s)%s",
  696. name, namealign(name, 35), flag.bits, value, comment)
  697. s[#s+1] = code
  698. end
  699. end
  700. local mask
  701. if flag.range then
  702. if flag.range == 64 then
  703. mask = "ffffffffffffffff"
  704. else
  705. mask = string.format(flag.format, ((1 << flag.range) - 1) << shift)
  706. end
  707. mask = string.format("UINT%d_C(0x%s)", flag.bits, mask)
  708. end
  709. if shift then
  710. local name = cname .. "_SHIFT"
  711. local comment = flag.desc or ""
  712. local shift_align = tostring(shift)
  713. shift_align = shift_align .. namealign(shift_align, #mask)
  714. local comment = ""
  715. if flag.desc then
  716. comment = string.format(" //!< %s bit shift", flag.desc)
  717. end
  718. local code = string.format("#define %s %s%s%s", name, namealign(name, 35), shift_align, comment)
  719. s[#s+1] = code
  720. end
  721. if flag.range then
  722. local name = cname .. "_MASK"
  723. local comment = ""
  724. if flag.desc then
  725. comment = string.format(" //!< %s bit mask", flag.desc)
  726. end
  727. local code = string.format("#define %s %s%s%s", name, namealign(name, 35), mask, comment)
  728. s[#s+1] = code
  729. end
  730. if flag.helper then
  731. s[#s+1] = string.format(
  732. "#define %s(v) ( ( (uint%d_t)(v)<<%s )&%s)",
  733. cname,
  734. flag.bits,
  735. (cname .. "_SHIFT"),
  736. (cname .. "_MASK"))
  737. end
  738. s[#s+1] = ""
  739. return table.concat(s, "\n")
  740. end
  741. local function text_with_comments(items, item, cstyle, is_classmember)
  742. local name = item.name
  743. if item.array then
  744. if cstyle then
  745. name = name .. (item.carray or item.array)
  746. else
  747. name = name .. item.array
  748. end
  749. end
  750. local typename
  751. if cstyle then
  752. typename = item.ctype
  753. else
  754. typename = item.fulltype
  755. end
  756. if is_classmember then
  757. name = "m_" .. name
  758. end
  759. local text = string.format("%s%s %s;", typename, namealign(typename), name)
  760. if item.comment then
  761. if type(item.comment) == "table" then
  762. table.insert(items, "")
  763. if cstyle then
  764. table.insert(items, "/**")
  765. for _, c in ipairs(item.comment) do
  766. table.insert(items, " * " .. c)
  767. end
  768. table.insert(items, " */")
  769. else
  770. for _, c in ipairs(item.comment) do
  771. table.insert(items, "/// " .. c)
  772. end
  773. end
  774. else
  775. text = string.format(
  776. cstyle and "%s %s/** %s%s */" or "%s %s//!< %s",
  777. text, namealign(text, 40), item.comment, namealign(item.comment, 40))
  778. end
  779. end
  780. items[#items+1] = text
  781. end
  782. local struct_temp = [[
  783. struct $NAME
  784. {
  785. $METHODS
  786. $SUBSTRUCTS
  787. $ITEMS
  788. };
  789. ]]
  790. function codegen.gen_struct_define(struct, methods)
  791. assert(type(struct.struct) == "table", "Not a struct")
  792. local items = {}
  793. for _, item in ipairs(struct.struct) do
  794. text_with_comments(items, item, false, methods ~= nil and not struct.shortname)
  795. end
  796. local ctor = {}
  797. if struct.ctor then
  798. ctor[1] = struct.name .. "();"
  799. ctor[2] = ""
  800. end
  801. if methods then
  802. for _, m in ipairs(methods) do
  803. if m:sub(-1) ~= "\n" then
  804. m = m .. "\n"
  805. end
  806. for line in m:gmatch "(.-)\n" do
  807. ctor[#ctor+1] = line
  808. end
  809. ctor[#ctor+1] = ""
  810. end
  811. end
  812. local subs = {}
  813. if struct.substruct then
  814. for _, v in ipairs(struct.substruct) do
  815. local s = codegen.gen_struct_define(v)
  816. s = s:gsub("\n", "\n\t")
  817. subs[#subs+1] = s
  818. end
  819. end
  820. local temp = {
  821. NAME = struct.name,
  822. SUBSTRUCTS = lines(subs),
  823. ITEMS = table.concat(items, "\n\t"),
  824. METHODS = lines(ctor),
  825. }
  826. return remove_emptylines(struct_temp:gsub("$(%u+)", temp))
  827. end
  828. local cstruct_temp = [[
  829. typedef struct $NAME_s
  830. {
  831. $ITEMS
  832. } $NAME_t;
  833. ]]
  834. local cstruct_empty_temp = [[
  835. struct $NAME_s;
  836. typedef struct $NAME_s $NAME_t;
  837. ]]
  838. function codegen.gen_struct_cdefine(struct)
  839. assert(type(struct.struct) == "table", "Not a struct")
  840. local cname = struct.cname:match "(.-)_t$"
  841. local items = {}
  842. for _, item in ipairs(struct.struct) do
  843. text_with_comments(items, item, true)
  844. end
  845. local temp = {
  846. NAME = cname,
  847. ITEMS = table.concat(items, "\n\t"),
  848. }
  849. local codetemp = #struct.struct == 0 and cstruct_empty_temp or cstruct_temp
  850. return (codetemp:gsub("$(%u+)", temp))
  851. end
  852. local chandle_temp = [[
  853. typedef struct $NAME_s { uint16_t idx; } $NAME_t;
  854. ]]
  855. function codegen.gen_chandle(handle)
  856. assert(handle.handle, "Not a handle")
  857. return (chandle_temp:gsub("$(%u+)", { NAME = handle.cname:match "(.-)_t$" }))
  858. end
  859. local handle_temp = [[
  860. struct $NAME { uint16_t idx; };
  861. inline bool isValid($NAME _handle) { return bgfx::kInvalidHandle != _handle.idx; }
  862. ]]
  863. function codegen.gen_handle(handle)
  864. assert(handle.handle, "Not a handle")
  865. return (handle_temp:gsub("$(%u+)", { NAME = handle.name }))
  866. end
  867. return codegen