codegen.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. return {
  412. RET = func.ret.fulltype,
  413. CRET = func.ret.ctype,
  414. CFUNCNAME = func.cname,
  415. CFUNCNAMEUPPER = func.cname:upper(),
  416. CFUNCNAMECAML = underscorecase_to_camelcase(func.cname),
  417. FUNCNAME = func.name,
  418. CARGS = table.concat(cargs, ", "),
  419. CPPARGS = table.concat(args, ", "),
  420. ARGS = ARGS,
  421. CONVERSION = lines(conversion),
  422. CONVERSIONCTOC = lines(conversion_c2c),
  423. CONVERSIONCTOCPP = lines(conversion_c2cpp),
  424. PRERET = func.ret_prefix or "",
  425. PRERETCPPTOC = func.ret_prefix_cpp2c or "",
  426. CPPFUNC = cppfunc,
  427. CALLFUNCCTOC = callfunc_c2c,
  428. CALLARGSCTOCPP = table.concat(callargs_conversion, ", "),
  429. CALLARGSCPPTOC = table.concat(callargs_conversion_back, ", "),
  430. CALLARGS = table.concat(callargs, ", "),
  431. POSTRET = lines(func.ret_postfix),
  432. POSTRETCPPTOC = lines(func.ret_postfix_cpp2c),
  433. PRERETCTOC = preret_c2c,
  434. POSTRETCTOC = lines(postret_c2c),
  435. CLASSNAME = classname,
  436. CONST = func.const and " const" or "",
  437. }
  438. end
  439. local function apply_template(func, temp)
  440. func.codetemp = func.codetemp or codetemp(func)
  441. return (temp:gsub("$(%u+)", func.codetemp))
  442. end
  443. function codegen.apply_functemp(func, temp)
  444. return remove_emptylines(apply_template(func, temp))
  445. end
  446. function codegen.gen_funcptr(funcptr)
  447. return apply_template(funcptr, "typedef $RET (*$FUNCNAME)($ARGS);")
  448. end
  449. function codegen.gen_cfuncptr(funcptr)
  450. return apply_template(funcptr, "typedef $CRET (*$CFUNCNAME)($CARGS);")
  451. end
  452. local function doxygen_funcret(r, func, prefix)
  453. if not func or func.ret.fulltype == "void" or func.ret.comment == nil then
  454. return
  455. end
  456. r[#r+1] = prefix
  457. if type(func.ret.comment) == "string" then
  458. r[#r+1] = string.format("%s @returns %s", prefix, func.ret.comment)
  459. else
  460. r[#r+1] = string.format("%s @returns %s", prefix, func.ret.comment[1])
  461. for i = 2,#func.ret.comment do
  462. r[#r+1] = string.format("%s %s", prefix, func.ret.comment[i])
  463. end
  464. end
  465. return r
  466. end
  467. local function doxygen_func(r, func, prefix)
  468. if not func or not func.args or #func.args == 0 then
  469. return
  470. end
  471. r[#r+1] = prefix
  472. for _, arg in ipairs(func.args) do
  473. local inout
  474. if arg.out then
  475. inout = "out"
  476. elseif arg.inout then
  477. inout = "inout"
  478. else
  479. inout = "in"
  480. end
  481. local comment = string.format("%s @param[%s] %s", prefix, inout, arg.name)
  482. if arg.comment then
  483. if type(arg.comment) == "string" then
  484. r[#r+1] = comment .. " " .. arg.comment
  485. else
  486. r[#r+1] = comment .. " " .. arg.comment[1]
  487. for i = 2,#arg.comment do
  488. r[#r+1] = string.format("%s %s", prefix, arg.comment[i])
  489. end
  490. end
  491. else
  492. r[#r+1] = comment
  493. end
  494. end
  495. doxygen_funcret(r, func, prefix)
  496. return r
  497. end
  498. function codegen.doxygen_type(doxygen, func, cname)
  499. if doxygen == nil then
  500. return
  501. end
  502. local result = {}
  503. for _, line in ipairs(doxygen) do
  504. result[#result+1] = "/// " .. line
  505. end
  506. doxygen_func(result, func, "///")
  507. if cname then
  508. result[#result+1] = "///"
  509. if type(cname) == "string" then
  510. result[#result+1] = string.format("/// @attention C99 equivalent is `%s`.", cname)
  511. else
  512. local names = {}
  513. for _, v in ipairs(cname) do
  514. names[#names+1] = "`" .. v .. "`"
  515. end
  516. result[#result+1] = string.format("/// @attention C99 equivalent are %s.", table.concat(names, ","))
  517. end
  518. end
  519. result[#result+1] = "///"
  520. return table.concat(result, "\n")
  521. end
  522. function codegen.doxygen_ctype(doxygen, func)
  523. if doxygen == nil then
  524. return
  525. end
  526. local result = {
  527. "/**",
  528. }
  529. for _, line in ipairs(doxygen) do
  530. result[#result+1] = " * " .. line
  531. end
  532. doxygen_func(result, func, " *")
  533. result[#result+1] = " *"
  534. result[#result+1] = " */"
  535. return table.concat(result, "\n")
  536. end
  537. local enum_temp = [[
  538. struct $NAME
  539. {
  540. $COMMENT
  541. enum Enum
  542. {
  543. $ITEMS
  544. Count
  545. };
  546. };
  547. ]]
  548. function codegen.gen_enum_define(enum)
  549. assert(type(enum.enum) == "table", "Not an enum")
  550. local items = {}
  551. for _, item in ipairs(enum.enum) do
  552. local text
  553. if not item.comment then
  554. text = item.name .. ","
  555. else
  556. text = string.format("%s,%s //!< %s",
  557. item.name, namealign(item.name), item.comment)
  558. end
  559. items[#items+1] = text
  560. end
  561. local comment = ""
  562. if enum.comment then
  563. comment = "/// " .. enum.comment
  564. end
  565. local temp = {
  566. NAME = enum.typename,
  567. COMMENT = comment,
  568. ITEMS = table.concat(items, "\n\t\t"),
  569. }
  570. return (enum_temp:gsub("$(%u+)", temp))
  571. end
  572. local cenum_temp = [[
  573. typedef enum $NAME
  574. {
  575. $ITEMS
  576. $COUNT
  577. } $NAME_t;
  578. ]]
  579. function codegen.gen_enum_cdefine(enum)
  580. assert(type(enum.enum) == "table", "Not an enum")
  581. local cname = enum.cname:match "(.-)_t$"
  582. local uname = cname:upper()
  583. local items = {}
  584. for index , item in ipairs(enum.enum) do
  585. local comment = item.comment or ""
  586. local ename = item.cname
  587. if not ename then
  588. if enum.underscore then
  589. ename = camelcase_to_underscorecase(item.name)
  590. else
  591. ename = item.name
  592. end
  593. ename = ename:upper()
  594. end
  595. local name = uname .. "_" .. ename
  596. items[#items+1] = string.format("%s,%s /** (%2d) %s%s */",
  597. name,
  598. namealign(name, 40),
  599. index - 1,
  600. comment,
  601. namealign(comment, 30))
  602. end
  603. local temp = {
  604. NAME = cname,
  605. COUNT = uname .. "_COUNT",
  606. ITEMS = table.concat(items, "\n\t"),
  607. }
  608. return (cenum_temp:gsub("$(%u+)", temp))
  609. end
  610. local function text_with_comments(items, item, cstyle, is_classmember)
  611. local name = item.name
  612. if item.array then
  613. if cstyle then
  614. name = name .. (item.carray or item.array)
  615. else
  616. name = name .. item.array
  617. end
  618. end
  619. local typename
  620. if cstyle then
  621. typename = item.ctype
  622. else
  623. typename = item.fulltype
  624. end
  625. if is_classmember then
  626. name = "m_" .. name
  627. end
  628. local text = string.format("%s%s %s;", typename, namealign(typename), name)
  629. if item.comment then
  630. if type(item.comment) == "table" then
  631. table.insert(items, "")
  632. if cstyle then
  633. table.insert(items, "/**")
  634. for _, c in ipairs(item.comment) do
  635. table.insert(items, " * " .. c)
  636. end
  637. table.insert(items, " */")
  638. else
  639. for _, c in ipairs(item.comment) do
  640. table.insert(items, "/// " .. c)
  641. end
  642. end
  643. else
  644. text = string.format(
  645. cstyle and "%s %s/** %s%s */" or "%s %s//!< %s",
  646. text, namealign(text, 40), item.comment, namealign(item.comment, 40))
  647. end
  648. end
  649. items[#items+1] = text
  650. end
  651. local struct_temp = [[
  652. struct $NAME
  653. {
  654. $METHODS
  655. $SUBSTRUCTS
  656. $ITEMS
  657. };
  658. ]]
  659. function codegen.gen_struct_define(struct, methods)
  660. assert(type(struct.struct) == "table", "Not a struct")
  661. local items = {}
  662. for _, item in ipairs(struct.struct) do
  663. text_with_comments(items, item, false, methods ~= nil and not struct.shortname)
  664. end
  665. local ctor = {}
  666. if struct.ctor then
  667. ctor[1] = struct.name .. "();"
  668. ctor[2] = ""
  669. end
  670. if methods then
  671. for _, m in ipairs(methods) do
  672. if m:sub(-1) ~= "\n" then
  673. m = m .. "\n"
  674. end
  675. for line in m:gmatch "(.-)\n" do
  676. ctor[#ctor+1] = line
  677. end
  678. ctor[#ctor+1] = ""
  679. end
  680. end
  681. local subs = {}
  682. if struct.substruct then
  683. for _, v in ipairs(struct.substruct) do
  684. local s = codegen.gen_struct_define(v)
  685. s = s:gsub("\n", "\n\t")
  686. subs[#subs+1] = s
  687. end
  688. end
  689. local temp = {
  690. NAME = struct.name,
  691. SUBSTRUCTS = lines(subs),
  692. ITEMS = table.concat(items, "\n\t"),
  693. METHODS = lines(ctor),
  694. }
  695. return remove_emptylines(struct_temp:gsub("$(%u+)", temp))
  696. end
  697. local cstruct_temp = [[
  698. typedef struct $NAME_s
  699. {
  700. $ITEMS
  701. } $NAME_t;
  702. ]]
  703. local cstruct_empty_temp = [[
  704. struct $NAME_s;
  705. typedef struct $NAME_s $NAME_t;
  706. ]]
  707. function codegen.gen_struct_cdefine(struct)
  708. assert(type(struct.struct) == "table", "Not a struct")
  709. local cname = struct.cname:match "(.-)_t$"
  710. local items = {}
  711. for _, item in ipairs(struct.struct) do
  712. text_with_comments(items, item, true)
  713. end
  714. local temp = {
  715. NAME = cname,
  716. ITEMS = table.concat(items, "\n\t"),
  717. }
  718. local codetemp = #struct.struct == 0 and cstruct_empty_temp or cstruct_temp
  719. return (codetemp:gsub("$(%u+)", temp))
  720. end
  721. local chandle_temp = [[
  722. typedef struct $NAME_s { uint16_t idx; } $NAME_t;
  723. ]]
  724. function codegen.gen_chandle(handle)
  725. assert(handle.handle, "Not a handle")
  726. return (chandle_temp:gsub("$(%u+)", { NAME = handle.cname:match "(.-)_t$" }))
  727. end
  728. local handle_temp = [[
  729. struct $NAME { uint16_t idx; };
  730. inline bool isValid($NAME _handle) { return bgfx::kInvalidHandle != _handle.idx; }
  731. ]]
  732. function codegen.gen_handle(handle)
  733. assert(handle.handle, "Not a handle")
  734. return (handle_temp:gsub("$(%u+)", { NAME = handle.name }))
  735. end
  736. return codegen