codegen.lua 19 KB

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