codegen.lua 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. local function calc_flag_values(flag)
  210. local shift = flag.shift
  211. local base = flag.base or 0
  212. local cap = 1 << (flag.range or 0)
  213. if flag.range then
  214. if flag.range == 64 then
  215. flag.mask = 0xffffffffffffffff
  216. else
  217. flag.mask = ((1 << flag.range) - 1) << shift
  218. end
  219. end
  220. local values = {}
  221. for index, item in ipairs(flag.flag) do
  222. local value = item.value
  223. if flag.const then
  224. -- use value directly
  225. elseif shift then
  226. if value then
  227. if value > 0 then
  228. value = value - 1
  229. end
  230. else
  231. value = index + base - 1
  232. end
  233. if value >= cap then
  234. error (string.format("Out of range for %s.%s (%d/%d)", flag.name, item.name, value, cap))
  235. end
  236. value = value << shift
  237. elseif #item == 0 then
  238. if value then
  239. if value > 0 then
  240. value = 1 << (value - 1)
  241. end
  242. else
  243. local s = index + base - 2
  244. if s >= 0 then
  245. value = 1 << s
  246. else
  247. value = 0
  248. end
  249. end
  250. end
  251. if not value then
  252. -- It's a combine flags
  253. value = 0
  254. for _, name in ipairs(item) do
  255. local v = values[name]
  256. if v then
  257. value = value | v
  258. else
  259. -- todo : it's a undefined flag
  260. value = nil
  261. break
  262. end
  263. end
  264. end
  265. item.value = value
  266. values[item.name] = value
  267. end
  268. end
  269. function codegen.nameconversion(all_types, all_funcs)
  270. for _,v in ipairs(all_types) do
  271. local name = v.name
  272. local cname = v.cname
  273. if cname == nil then
  274. if name:match "^%u" then
  275. cname = camelcase_to_underscorecase(name)
  276. elseif not v.flag then
  277. v.cname = name
  278. end
  279. end
  280. if cname and not v.flag then
  281. if v.namespace then
  282. cname = camelcase_to_underscorecase(v.namespace) .. "_" .. cname
  283. end
  284. v.cname = "bgfx_".. cname .. "_t"
  285. end
  286. if v.enum then
  287. v.typename = v.name
  288. v.name = v.name .. "::Enum"
  289. end
  290. if v.flag then
  291. calc_flag_values(v)
  292. end
  293. end
  294. -- make index
  295. for _,v in ipairs(all_types) do
  296. if not v.namespace then
  297. if all_types[v.name] then
  298. error ("Duplicate type " .. v.name)
  299. elseif not v.flag then
  300. all_types[v.name] = v
  301. end
  302. end
  303. end
  304. -- make sub struct index
  305. for _,v in ipairs(all_types) do
  306. if v.namespace then
  307. local super = all_types[v.namespace]
  308. if not super then
  309. error ("Define " .. v.namespace .. " first")
  310. end
  311. local substruct = super.substruct
  312. if not substruct then
  313. substruct = {}
  314. super.substruct = substruct
  315. end
  316. if substruct[v.name] then
  317. error ( "Duplicate sub struct " .. v.name .. " in " .. v.namespace)
  318. end
  319. v.parent_class = super
  320. substruct[#substruct+1] = v
  321. substruct[v.name] = v
  322. end
  323. end
  324. for _,v in ipairs(all_types) do
  325. if v.struct then
  326. for _, item in ipairs(v.struct) do
  327. convert_arg(all_types, item, v)
  328. end
  329. elseif v.args then
  330. -- funcptr
  331. for _, arg in ipairs(v.args) do
  332. convert_arg(all_types, arg, v)
  333. end
  334. convert_vararg(v)
  335. convert_arg(all_types, v.ret, v)
  336. end
  337. end
  338. local funcs = {}
  339. local funcs_conly = {}
  340. local funcs_alter = {}
  341. for _,v in ipairs(all_funcs) do
  342. if v.cname == nil then
  343. v.cname = convert_funcname(v.name)
  344. end
  345. if v.class then
  346. v.cname = convert_funcname(v.class) .. "_" .. v.cname
  347. local classtype = all_types[v.class]
  348. if classtype then
  349. local methods = classtype.methods
  350. if not methods then
  351. methods = {}
  352. classtype.methods = methods
  353. end
  354. methods[#methods+1] = v
  355. end
  356. elseif not v.conly then
  357. funcs[v.name] = v
  358. end
  359. if v.conly then
  360. table.insert(funcs_conly, v)
  361. end
  362. for _, arg in ipairs(v.args) do
  363. convert_arg(all_types, arg, v)
  364. gen_arg_conversion(all_types, arg)
  365. end
  366. convert_vararg(v)
  367. if v.alter_name then
  368. funcs_alter[#funcs_alter+1] = v
  369. end
  370. convert_arg(all_types, v.ret, v)
  371. gen_ret_conversion(all_types, v)
  372. local namespace = v.class
  373. if namespace then
  374. local classname = namespace
  375. if v.const then
  376. classname = "const " .. classname
  377. end
  378. local classtype = { fulltype = classname .. "*" }
  379. convert_arg(all_types, classtype, v)
  380. v.this = classtype.ctype
  381. v.this_type = classtype
  382. v.this_conversion = string.format( "%s This = (%s)_this;", classtype.cpptype, classtype.cpptype)
  383. v.this_to_c = string.format("(%s)this", classtype.ctype)
  384. end
  385. end
  386. for _, v in ipairs(funcs_conly) do
  387. local func = funcs[v.name]
  388. if func then
  389. func.multicfunc = func.multicfunc or { func.cname }
  390. table.insert(func.multicfunc, v.cname)
  391. end
  392. end
  393. for _, v in ipairs(funcs_alter) do
  394. local func = funcs[v.alter_name]
  395. v.alter_cname = func.cname
  396. end
  397. end
  398. local function lines(tbl)
  399. if not tbl or #tbl == 0 then
  400. return "//EMPTYLINE"
  401. else
  402. return table.concat(tbl, "\n\t")
  403. end
  404. end
  405. local function remove_emptylines(txt)
  406. return (txt:gsub("\t//EMPTYLINE\n", ""))
  407. end
  408. local function codetemp(func)
  409. local conversion = {}
  410. local conversion_c2cpp = {}
  411. local args = {}
  412. local cargs = {}
  413. local callargs_conversion = {}
  414. local callargs_conversion_back = {}
  415. local callargs = {}
  416. local cppfunc
  417. local classname
  418. if func.class then
  419. -- It's a member function
  420. cargs[1] = func.this .. " _this"
  421. conversion[1] = func.this_conversion
  422. cppfunc = "This->" .. func.name
  423. callargs[1] = "_this"
  424. callargs_conversion_back[1] = func.this_to_c
  425. classname = func.class .. "::"
  426. else
  427. cppfunc = "bgfx::" .. tostring(func.alter_name or func.name)
  428. classname = ""
  429. end
  430. for _, arg in ipairs(func.args) do
  431. conversion[#conversion+1] = arg.conversion
  432. conversion_c2cpp[#conversion_c2cpp+1] = arg.conversion_back
  433. local cname = arg.ctype .. " " .. arg.name
  434. if arg.array then
  435. cname = cname .. (arg.carray or arg.array)
  436. end
  437. local name = arg.fulltype .. " " .. arg.name
  438. if arg.array then
  439. name = name .. arg.array
  440. end
  441. if arg.default ~= nil then
  442. name = name .. " = " .. tostring(arg.default)
  443. end
  444. cargs[#cargs+1] = cname
  445. args[#args+1] = name
  446. callargs_conversion[#callargs_conversion+1] = arg.aname or arg.name
  447. callargs_conversion_back[#callargs_conversion_back+1] = arg.aname_cpp2c or arg.name
  448. callargs[#callargs+1] = arg.name
  449. end
  450. conversion[#conversion+1] = func.ret_conversion
  451. conversion_c2cpp[#conversion_c2cpp+1] = func.ret_conversion_cpp2c
  452. local ARGS
  453. local args_n = #args
  454. if args_n == 0 then
  455. ARGS = ""
  456. elseif args_n == 1 then
  457. ARGS = args[1]
  458. else
  459. ARGS = "\n\t " .. table.concat(args, "\n\t, ") .. "\n\t"
  460. end
  461. local preret_c2c
  462. local postret_c2c = {}
  463. local conversion_c2c = {}
  464. local callfunc_c2c
  465. if func.vararg then
  466. postret_c2c[1] = "va_end(argList);"
  467. local vararg = func.args[#func.args]
  468. callargs[#callargs] = vararg.aname
  469. callargs_conversion_back[#callargs_conversion_back] = vararg.aname
  470. conversion_c2c[1] = vararg.conversion
  471. conversion_c2cpp[1] = vararg.conversion
  472. if func.ret.fulltype == "void" then
  473. preret_c2c = ""
  474. else
  475. preret_c2c = func.ret.ctype .. " retValue = "
  476. postret_c2c[#postret_c2c+1] = "return retValue;"
  477. end
  478. callfunc_c2c = func.alter_cname or func.cname
  479. else
  480. if func.ret.fulltype == "void" then
  481. preret_c2c = ""
  482. else
  483. preret_c2c = "return "
  484. end
  485. callfunc_c2c = func.cname
  486. end
  487. outCargs = table.concat(cargs, ", ")
  488. if outCargs == "" then
  489. outCargs = "void"
  490. end
  491. return {
  492. RET = func.ret.fulltype,
  493. CRET = func.ret.ctype,
  494. CFUNCNAME = func.cname,
  495. CFUNCNAMEUPPER = func.cname:upper(),
  496. CFUNCNAMECAML = underscorecase_to_camelcase(func.cname),
  497. FUNCNAME = func.name,
  498. CARGS = outCargs,
  499. CPPARGS = table.concat(args, ", "),
  500. ARGS = ARGS,
  501. CONVERSION = lines(conversion),
  502. CONVERSIONCTOC = lines(conversion_c2c),
  503. CONVERSIONCTOCPP = lines(conversion_c2cpp),
  504. PRERET = func.ret_prefix or "",
  505. PRERETCPPTOC = func.ret_prefix_cpp2c or "",
  506. CPPFUNC = cppfunc,
  507. CALLFUNCCTOC = callfunc_c2c,
  508. CALLARGSCTOCPP = table.concat(callargs_conversion, ", "),
  509. CALLARGSCPPTOC = table.concat(callargs_conversion_back, ", "),
  510. CALLARGS = table.concat(callargs, ", "),
  511. POSTRET = lines(func.ret_postfix),
  512. POSTRETCPPTOC = lines(func.ret_postfix_cpp2c),
  513. PRERETCTOC = preret_c2c,
  514. POSTRETCTOC = lines(postret_c2c),
  515. CLASSNAME = classname,
  516. CONST = func.const and " const" or "",
  517. }
  518. end
  519. local function apply_template(func, temp)
  520. func.codetemp = func.codetemp or codetemp(func)
  521. return (temp:gsub("$(%u+)", func.codetemp))
  522. end
  523. function codegen.apply_functemp(func, temp)
  524. return remove_emptylines(apply_template(func, temp))
  525. end
  526. function codegen.gen_funcptr(funcptr)
  527. return apply_template(funcptr, "typedef $RET (*$FUNCNAME)($ARGS);")
  528. end
  529. function codegen.gen_cfuncptr(funcptr)
  530. return apply_template(funcptr, "typedef $CRET (*$CFUNCNAME)($CARGS);")
  531. end
  532. local function doxygen_funcret(r, func, prefix)
  533. if not func or func.ret.fulltype == "void" or func.ret.comment == nil then
  534. return
  535. end
  536. r[#r+1] = prefix
  537. if type(func.ret.comment) == "string" then
  538. r[#r+1] = string.format("%s @returns %s", prefix, func.ret.comment)
  539. else
  540. r[#r+1] = string.format("%s @returns %s", prefix, func.ret.comment[1])
  541. for i = 2,#func.ret.comment do
  542. r[#r+1] = string.format("%s %s", prefix, func.ret.comment[i])
  543. end
  544. end
  545. return r
  546. end
  547. local function doxygen_func(r, func, prefix)
  548. if not func or not func.args or #func.args == 0 then
  549. return
  550. end
  551. r[#r+1] = prefix
  552. for _, arg in ipairs(func.args) do
  553. local inout
  554. if arg.out then
  555. inout = "out"
  556. elseif arg.inout then
  557. inout = "inout"
  558. else
  559. inout = "in"
  560. end
  561. local comment = string.format("%s @param[%s] %s", prefix, inout, arg.name)
  562. if arg.comment then
  563. if type(arg.comment) == "string" then
  564. r[#r+1] = comment .. " " .. arg.comment
  565. else
  566. r[#r+1] = comment .. " " .. arg.comment[1]
  567. for i = 2,#arg.comment do
  568. r[#r+1] = string.format("%s %s", prefix, arg.comment[i])
  569. end
  570. end
  571. else
  572. r[#r+1] = comment
  573. end
  574. end
  575. doxygen_funcret(r, func, prefix)
  576. return r
  577. end
  578. function codegen.doxygen_type(doxygen, func, cname)
  579. if doxygen == nil then
  580. return
  581. end
  582. local result = {}
  583. for _, line in ipairs(doxygen) do
  584. result[#result+1] = "/// " .. line
  585. end
  586. doxygen_func(result, func, "///")
  587. if cname then
  588. result[#result+1] = "///"
  589. if type(cname) == "string" then
  590. result[#result+1] = string.format("/// @attention C99 equivalent is `%s`.", cname)
  591. else
  592. local names = {}
  593. for _, v in ipairs(cname) do
  594. names[#names+1] = "`" .. v .. "`"
  595. end
  596. result[#result+1] = string.format("/// @attention C99 equivalent are %s.", table.concat(names, ","))
  597. end
  598. end
  599. result[#result+1] = "///"
  600. return table.concat(result, "\n")
  601. end
  602. function codegen.doxygen_ctype(doxygen, func)
  603. if doxygen == nil then
  604. return
  605. end
  606. local result = {
  607. "/**",
  608. }
  609. for _, line in ipairs(doxygen) do
  610. result[#result+1] = " * " .. line
  611. end
  612. doxygen_func(result, func, " *")
  613. result[#result+1] = " *"
  614. result[#result+1] = " */"
  615. return table.concat(result, "\n")
  616. end
  617. local enum_temp = [[
  618. struct $NAME
  619. {
  620. $COMMENT
  621. enum Enum
  622. {
  623. $ITEMS
  624. Count
  625. };
  626. };
  627. ]]
  628. function codegen.gen_enum_define(enum)
  629. assert(type(enum.enum) == "table", "Not an enum")
  630. local items = {}
  631. for _, item in ipairs(enum.enum) do
  632. local text
  633. if not item.comment then
  634. text = item.name .. ","
  635. else
  636. text = string.format("%s,%s //!< %s",
  637. item.name, namealign(item.name), item.comment)
  638. end
  639. items[#items+1] = text
  640. end
  641. local comment = ""
  642. if enum.comment then
  643. comment = "/// " .. enum.comment
  644. end
  645. local temp = {
  646. NAME = enum.typename,
  647. COMMENT = comment,
  648. ITEMS = table.concat(items, "\n\t\t"),
  649. }
  650. return (enum_temp:gsub("$(%u+)", temp))
  651. end
  652. local cenum_temp = [[
  653. typedef enum $NAME
  654. {
  655. $ITEMS
  656. $COUNT
  657. } $NAME_t;
  658. ]]
  659. function codegen.gen_enum_cdefine(enum)
  660. assert(type(enum.enum) == "table", "Not an enum")
  661. local cname = enum.cname:match "(.-)_t$"
  662. local uname = cname:upper()
  663. local items = {}
  664. for index , item in ipairs(enum.enum) do
  665. local comment = item.comment or ""
  666. local ename = item.cname
  667. if not ename then
  668. if enum.underscore then
  669. ename = camelcase_to_underscorecase(item.name)
  670. else
  671. ename = item.name
  672. end
  673. ename = ename:upper()
  674. end
  675. local name = uname .. "_" .. ename
  676. items[#items+1] = string.format("%s,%s /** (%2d) %s%s */",
  677. name,
  678. namealign(name, 40),
  679. index - 1,
  680. comment,
  681. namealign(comment, 30))
  682. end
  683. local temp = {
  684. NAME = cname,
  685. COUNT = uname .. "_COUNT",
  686. ITEMS = table.concat(items, "\n\t"),
  687. }
  688. return (cenum_temp:gsub("$(%u+)", temp))
  689. end
  690. local function flag_format(flag)
  691. if not flag.format then
  692. flag.format = "%0" .. (flag.bits // 4) .. "x"
  693. end
  694. end
  695. function codegen.gen_flag_cdefine(flag)
  696. assert(type(flag.flag) == "table", "Not a flag")
  697. flag_format(flag)
  698. local cname = "BGFX_" .. (flag.cname or to_underscorecase(flag.name):upper())
  699. local s = {}
  700. local shift = flag.shift
  701. for index, item in ipairs(flag.flag) do
  702. local name
  703. if item.cname then
  704. name = cname .. "_" .. item.cname
  705. else
  706. name = cname .. "_" .. to_underscorecase(item.name):upper()
  707. end
  708. local value = item.value
  709. -- combine flags
  710. if #item > 0 then
  711. if item.comment then
  712. if type(item.comment) == "table" then
  713. for _, c in ipairs(item.comment) do
  714. s[#s+1] = "/// " .. c
  715. end
  716. else
  717. s[#s+1] = "/// " .. item.comment
  718. end
  719. end
  720. local sets = { "" }
  721. for _, v in ipairs(item) do
  722. sets[#sets+1] = cname .. "_" .. to_underscorecase(v):upper()
  723. end
  724. s[#s+1] = string.format("#define %s (0%s \\\n\t)\n", name, table.concat(sets, " \\\n\t| "))
  725. else
  726. local comment = ""
  727. if item.comment then
  728. comment = " //!< " .. item.comment
  729. end
  730. value = string.format(flag.format, value)
  731. local code = string.format("#define %s %sUINT%d_C(0x%s)%s",
  732. name, namealign(name, 35), flag.bits, value, comment)
  733. s[#s+1] = code
  734. end
  735. end
  736. local mask
  737. if flag.mask then
  738. mask = string.format(flag.format, flag.mask)
  739. mask = string.format("UINT%d_C(0x%s)", flag.bits, mask)
  740. end
  741. if shift then
  742. local name = cname .. "_SHIFT"
  743. local comment = flag.desc or ""
  744. local shift_align = tostring(shift)
  745. shift_align = shift_align .. namealign(shift_align, #mask)
  746. local comment = ""
  747. if flag.desc then
  748. comment = string.format(" //!< %s bit shift", flag.desc)
  749. end
  750. local code = string.format("#define %s %s%s%s", name, namealign(name, 35), shift_align, comment)
  751. s[#s+1] = code
  752. end
  753. if flag.range then
  754. local name = cname .. "_MASK"
  755. local comment = ""
  756. if flag.desc then
  757. comment = string.format(" //!< %s bit mask", flag.desc)
  758. end
  759. local code = string.format("#define %s %s%s%s", name, namealign(name, 35), mask, comment)
  760. s[#s+1] = code
  761. end
  762. if flag.helper then
  763. s[#s+1] = string.format(
  764. "#define %s(v) ( ( (uint%d_t)(v)<<%s )&%s)",
  765. cname,
  766. flag.bits,
  767. (cname .. "_SHIFT"),
  768. (cname .. "_MASK"))
  769. end
  770. s[#s+1] = ""
  771. return table.concat(s, "\n")
  772. end
  773. local function text_with_comments(items, item, cstyle, is_classmember)
  774. local name = item.name
  775. if item.array then
  776. if cstyle then
  777. name = name .. (item.carray or item.array)
  778. else
  779. name = name .. item.array
  780. end
  781. end
  782. local typename
  783. if cstyle then
  784. typename = item.ctype
  785. else
  786. typename = item.fulltype
  787. end
  788. if is_classmember then
  789. name = "m_" .. name
  790. end
  791. local text = string.format("%s%s %s;", typename, namealign(typename), name)
  792. if item.comment then
  793. if type(item.comment) == "table" then
  794. table.insert(items, "")
  795. if cstyle then
  796. table.insert(items, "/**")
  797. for _, c in ipairs(item.comment) do
  798. table.insert(items, " * " .. c)
  799. end
  800. table.insert(items, " */")
  801. else
  802. for _, c in ipairs(item.comment) do
  803. table.insert(items, "/// " .. c)
  804. end
  805. end
  806. else
  807. text = string.format(
  808. cstyle and "%s %s/** %s%s */" or "%s %s//!< %s",
  809. text, namealign(text, 40), item.comment, namealign(item.comment, 40))
  810. end
  811. end
  812. items[#items+1] = text
  813. end
  814. local struct_temp = [[
  815. struct $NAME
  816. {
  817. $METHODS
  818. $SUBSTRUCTS
  819. $ITEMS
  820. };
  821. ]]
  822. function codegen.gen_struct_define(struct, methods)
  823. assert(type(struct.struct) == "table", "Not a struct")
  824. local items = {}
  825. for _, item in ipairs(struct.struct) do
  826. text_with_comments(items, item, false, methods ~= nil and not struct.shortname)
  827. end
  828. local ctor = {}
  829. if struct.ctor then
  830. ctor[1] = struct.name .. "();"
  831. ctor[2] = ""
  832. end
  833. if methods then
  834. for _, m in ipairs(methods) do
  835. if m:sub(-1) ~= "\n" then
  836. m = m .. "\n"
  837. end
  838. for line in m:gmatch "(.-)\n" do
  839. ctor[#ctor+1] = line
  840. end
  841. ctor[#ctor+1] = ""
  842. end
  843. end
  844. local subs = {}
  845. if struct.substruct then
  846. for _, v in ipairs(struct.substruct) do
  847. local s = codegen.gen_struct_define(v)
  848. s = s:gsub("\n", "\n\t")
  849. subs[#subs+1] = s
  850. end
  851. end
  852. local temp = {
  853. NAME = struct.name,
  854. SUBSTRUCTS = lines(subs),
  855. ITEMS = table.concat(items, "\n\t"),
  856. METHODS = lines(ctor),
  857. }
  858. return remove_emptylines(struct_temp:gsub("$(%u+)", temp))
  859. end
  860. local cstruct_temp = [[
  861. typedef struct $NAME_s
  862. {
  863. $ITEMS
  864. } $NAME_t;
  865. ]]
  866. local cstruct_empty_temp = [[
  867. struct $NAME_s;
  868. typedef struct $NAME_s $NAME_t;
  869. ]]
  870. function codegen.gen_struct_cdefine(struct)
  871. assert(type(struct.struct) == "table", "Not a struct")
  872. local cname = struct.cname:match "(.-)_t$"
  873. local items = {}
  874. for _, item in ipairs(struct.struct) do
  875. text_with_comments(items, item, true)
  876. end
  877. local temp = {
  878. NAME = cname,
  879. ITEMS = table.concat(items, "\n\t"),
  880. }
  881. local codetemp = #struct.struct == 0 and cstruct_empty_temp or cstruct_temp
  882. return (codetemp:gsub("$(%u+)", temp))
  883. end
  884. local chandle_temp = [[
  885. typedef struct $NAME_s { uint16_t idx; } $NAME_t;
  886. ]]
  887. function codegen.gen_chandle(handle)
  888. assert(handle.handle, "Not a handle")
  889. return (chandle_temp:gsub("$(%u+)", { NAME = handle.cname:match "(.-)_t$" }))
  890. end
  891. local handle_temp = [[
  892. struct $NAME { uint16_t idx; };
  893. inline bool isValid($NAME _handle) { return bgfx::kInvalidHandle != _handle.idx; }
  894. ]]
  895. function codegen.gen_handle(handle)
  896. assert(handle.handle, "Not a handle")
  897. return (handle_temp:gsub("$(%u+)", { NAME = handle.name }))
  898. end
  899. local idl = require "idl"
  900. local doxygen = require "doxygen"
  901. local conversion
  902. local idlfile = {}
  903. function codegen.load(filename)
  904. assert(conversion == nil, "Don't call codegen.load() after codegen.idl()")
  905. assert(idlfile[filename] == nil, "Duplicate load " .. filename)
  906. local source = doxygen.load(filename)
  907. local f = assert(load(source, filename , "t", idl))
  908. f()
  909. idlfile[filename] = true
  910. end
  911. function codegen.idl(filename)
  912. if conversion == nil then
  913. if filename and not idlfile[filename] then
  914. codegen.load(filename)
  915. end
  916. assert(next(idlfile), "call codegen.load() first")
  917. conversion = true
  918. codegen.nameconversion(idl.types, idl.funcs)
  919. end
  920. return idl
  921. end
  922. return codegen