codegen.lua 20 KB

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