codegen.lua 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. r[#r+1] = string.format("%s @returns %s", prefix, func.ret.comment[1])
  538. for i = 2,#func.ret.comment do
  539. r[#r+1] = string.format("%s %s", prefix, func.ret.comment[i])
  540. end
  541. return r
  542. end
  543. local function doxygen_func(r, func, prefix)
  544. if not func or not func.args or #func.args == 0 then
  545. return
  546. end
  547. r[#r+1] = prefix
  548. for _, arg in ipairs(func.args) do
  549. local inout
  550. if arg.out then
  551. inout = "out"
  552. elseif arg.inout then
  553. inout = "inout"
  554. else
  555. inout = "in"
  556. end
  557. local comment = string.format("%s @param[%s] %s", prefix, inout, arg.name)
  558. if arg.comment then
  559. r[#r+1] = comment .. " " .. arg.comment[1]
  560. for i = 2,#arg.comment do
  561. r[#r+1] = string.format("%s %s", prefix, arg.comment[i])
  562. end
  563. else
  564. r[#r+1] = comment
  565. end
  566. end
  567. doxygen_funcret(r, func, prefix)
  568. return r
  569. end
  570. function codegen.doxygen_type(doxygen, func, cname)
  571. if doxygen == nil then
  572. return
  573. end
  574. local result = {}
  575. for _, line in ipairs(doxygen) do
  576. result[#result+1] = "/// " .. line
  577. end
  578. doxygen_func(result, func, "///")
  579. if cname then
  580. result[#result+1] = "///"
  581. if type(cname) == "string" then
  582. result[#result+1] = string.format("/// @attention C99 equivalent is `%s`.", cname)
  583. else
  584. local names = {}
  585. for _, v in ipairs(cname) do
  586. names[#names+1] = "`" .. v .. "`"
  587. end
  588. result[#result+1] = string.format("/// @attention C99 equivalent are %s.", table.concat(names, ","))
  589. end
  590. end
  591. result[#result+1] = "///"
  592. return table.concat(result, "\n")
  593. end
  594. function codegen.doxygen_ctype(doxygen, func)
  595. if doxygen == nil then
  596. return
  597. end
  598. local result = {
  599. "/**",
  600. }
  601. for _, line in ipairs(doxygen) do
  602. result[#result+1] = " * " .. line
  603. end
  604. doxygen_func(result, func, " *")
  605. result[#result+1] = " *"
  606. result[#result+1] = " */"
  607. return table.concat(result, "\n")
  608. end
  609. local enum_temp = [[
  610. struct $NAME
  611. {
  612. $COMMENT
  613. enum Enum
  614. {
  615. $ITEMS
  616. Count
  617. };
  618. };
  619. ]]
  620. function codegen.gen_enum_define(enum)
  621. assert(type(enum.enum) == "table", "Not an enum")
  622. local items = {}
  623. for _, item in ipairs(enum.enum) do
  624. local text
  625. if not item.comment then
  626. text = item.name .. ","
  627. else
  628. local comment = table.concat(item.comment, " ")
  629. text = string.format("%s,%s //!< %s",
  630. item.name, namealign(item.name), comment)
  631. end
  632. items[#items+1] = text
  633. end
  634. local comment = ""
  635. if enum.comment then
  636. comment = "/// " .. enum.comment
  637. end
  638. local temp = {
  639. NAME = enum.typename,
  640. COMMENT = comment,
  641. ITEMS = table.concat(items, "\n\t\t"),
  642. }
  643. return (enum_temp:gsub("$(%u+)", temp))
  644. end
  645. local cenum_temp = [[
  646. typedef enum $NAME
  647. {
  648. $ITEMS
  649. $COUNT
  650. } $NAME_t;
  651. ]]
  652. function codegen.gen_enum_cdefine(enum)
  653. assert(type(enum.enum) == "table", "Not an enum")
  654. local cname = enum.cname:match "(.-)_t$"
  655. local uname = cname:upper()
  656. local items = {}
  657. for index , item in ipairs(enum.enum) do
  658. local comment = ""
  659. if item.comment then
  660. comment = table.concat(item.comment, " ")
  661. end
  662. local ename = item.cname
  663. if not ename then
  664. if enum.underscore then
  665. ename = camelcase_to_underscorecase(item.name)
  666. else
  667. ename = item.name
  668. end
  669. ename = ename:upper()
  670. end
  671. local name = uname .. "_" .. ename
  672. items[#items+1] = string.format("%s,%s /** (%2d) %s%s */",
  673. name,
  674. namealign(name, 40),
  675. index - 1,
  676. comment,
  677. namealign(comment, 30))
  678. end
  679. local temp = {
  680. NAME = cname,
  681. COUNT = uname .. "_COUNT",
  682. ITEMS = table.concat(items, "\n\t"),
  683. }
  684. return (cenum_temp:gsub("$(%u+)", temp))
  685. end
  686. local function flag_format(flag)
  687. if not flag.format then
  688. flag.format = "%0" .. (flag.bits // 4) .. "x"
  689. end
  690. end
  691. function codegen.gen_flag_cdefine(flag)
  692. assert(type(flag.flag) == "table", "Not a flag")
  693. flag_format(flag)
  694. local cname = "BGFX_" .. (flag.cname or to_underscorecase(flag.name):upper())
  695. local s = {}
  696. local shift = flag.shift
  697. for index, item in ipairs(flag.flag) do
  698. local name
  699. if item.cname then
  700. name = cname .. "_" .. item.cname
  701. else
  702. name = cname .. "_" .. to_underscorecase(item.name):upper()
  703. end
  704. local value = item.value
  705. -- combine flags
  706. if #item > 0 then
  707. if item.comment then
  708. for _, c in ipairs(item.comment) do
  709. s[#s+1] = "/// " .. c
  710. end
  711. end
  712. local sets = { "" }
  713. for _, v in ipairs(item) do
  714. sets[#sets+1] = cname .. "_" .. to_underscorecase(v):upper()
  715. end
  716. s[#s+1] = string.format("#define %s (0%s \\\n\t)\n", name, table.concat(sets, " \\\n\t| "))
  717. else
  718. local comment = ""
  719. if item.comment then
  720. if #item.comment > 1 then
  721. for _, c in ipairs(item.comment) do
  722. s[#s+1] = "/// " .. c
  723. end
  724. else
  725. comment = " //!< " .. item.comment[1]
  726. end
  727. end
  728. value = string.format(flag.format, value)
  729. local code = string.format("#define %s %sUINT%d_C(0x%s)%s",
  730. name, namealign(name, 35), flag.bits, value, comment)
  731. s[#s+1] = code
  732. end
  733. end
  734. local mask
  735. if flag.mask then
  736. mask = string.format(flag.format, flag.mask)
  737. mask = string.format("UINT%d_C(0x%s)", flag.bits, mask)
  738. end
  739. if shift then
  740. local name = cname .. "_SHIFT"
  741. local comment = flag.desc or ""
  742. local shift_align = tostring(shift)
  743. shift_align = shift_align .. namealign(shift_align, #mask)
  744. local comment = ""
  745. if flag.desc then
  746. comment = string.format(" //!< %s bit shift", flag.desc)
  747. end
  748. local code = string.format("#define %s %s%s%s", name, namealign(name, 35), shift_align, comment)
  749. s[#s+1] = code
  750. end
  751. if flag.range then
  752. local name = cname .. "_MASK"
  753. local comment = ""
  754. if flag.desc then
  755. comment = string.format(" //!< %s bit mask", flag.desc)
  756. end
  757. local code = string.format("#define %s %s%s%s", name, namealign(name, 35), mask, comment)
  758. s[#s+1] = code
  759. end
  760. if flag.helper then
  761. s[#s+1] = string.format(
  762. "#define %s(v) ( ( (uint%d_t)(v)<<%s )&%s)",
  763. cname,
  764. flag.bits,
  765. (cname .. "_SHIFT"),
  766. (cname .. "_MASK"))
  767. end
  768. s[#s+1] = ""
  769. return table.concat(s, "\n")
  770. end
  771. local function text_with_comments(items, item, cstyle, is_classmember)
  772. local name = item.name
  773. if item.array then
  774. if cstyle then
  775. name = name .. (item.carray or item.array)
  776. else
  777. name = name .. item.array
  778. end
  779. end
  780. local typename
  781. if cstyle then
  782. typename = item.ctype
  783. else
  784. typename = item.fulltype
  785. end
  786. if is_classmember then
  787. name = "m_" .. name
  788. end
  789. local text = string.format("%s%s %s;", typename, namealign(typename), name)
  790. if item.comment then
  791. if #item.comment > 1 then
  792. table.insert(items, "")
  793. if cstyle then
  794. table.insert(items, "/**")
  795. for _, c in ipairs(item.comment) do
  796. table.insert(items, " * " .. c)
  797. end
  798. table.insert(items, " */")
  799. else
  800. for _, c in ipairs(item.comment) do
  801. table.insert(items, "/// " .. c)
  802. end
  803. end
  804. else
  805. text = string.format(
  806. cstyle and "%s %s/** %s%s */" or "%s %s//!< %s",
  807. text, namealign(text, 40), item.comment[1], namealign(item.comment[1], 40))
  808. end
  809. end
  810. items[#items+1] = text
  811. end
  812. local struct_temp = [[
  813. struct $NAME
  814. {
  815. $METHODS
  816. $SUBSTRUCTS
  817. $ITEMS
  818. };
  819. ]]
  820. function codegen.gen_struct_define(struct, methods)
  821. assert(type(struct.struct) == "table", "Not a struct")
  822. local items = {}
  823. for _, item in ipairs(struct.struct) do
  824. text_with_comments(items, item, false, methods ~= nil and not struct.shortname)
  825. end
  826. local ctor = {}
  827. if struct.ctor then
  828. ctor[1] = struct.name .. "();"
  829. ctor[2] = ""
  830. end
  831. if methods then
  832. for _, m in ipairs(methods) do
  833. if m:sub(-1) ~= "\n" then
  834. m = m .. "\n"
  835. end
  836. for line in m:gmatch "(.-)\n" do
  837. ctor[#ctor+1] = line
  838. end
  839. ctor[#ctor+1] = ""
  840. end
  841. end
  842. local subs = {}
  843. if struct.substruct then
  844. for _, v in ipairs(struct.substruct) do
  845. local s = codegen.gen_struct_define(v)
  846. s = s:gsub("\n", "\n\t")
  847. subs[#subs+1] = s
  848. end
  849. end
  850. local temp = {
  851. NAME = struct.name,
  852. SUBSTRUCTS = lines(subs),
  853. ITEMS = table.concat(items, "\n\t"),
  854. METHODS = lines(ctor),
  855. }
  856. return remove_emptylines(struct_temp:gsub("$(%u+)", temp))
  857. end
  858. local cstruct_temp = [[
  859. typedef struct $NAME_s
  860. {
  861. $ITEMS
  862. } $NAME_t;
  863. ]]
  864. local cstruct_empty_temp = [[
  865. struct $NAME_s;
  866. typedef struct $NAME_s $NAME_t;
  867. ]]
  868. function codegen.gen_struct_cdefine(struct)
  869. assert(type(struct.struct) == "table", "Not a struct")
  870. local cname = struct.cname:match "(.-)_t$"
  871. local items = {}
  872. for _, item in ipairs(struct.struct) do
  873. text_with_comments(items, item, true)
  874. end
  875. local temp = {
  876. NAME = cname,
  877. ITEMS = table.concat(items, "\n\t"),
  878. }
  879. local codetemp = #struct.struct == 0 and cstruct_empty_temp or cstruct_temp
  880. return (codetemp:gsub("$(%u+)", temp))
  881. end
  882. local chandle_temp = [[
  883. typedef struct $NAME_s { uint16_t idx; } $NAME_t;
  884. ]]
  885. function codegen.gen_chandle(handle)
  886. assert(handle.handle, "Not a handle")
  887. return (chandle_temp:gsub("$(%u+)", { NAME = handle.cname:match "(.-)_t$" }))
  888. end
  889. local handle_temp = [[
  890. struct $NAME { uint16_t idx; };
  891. inline bool isValid($NAME _handle) { return bgfx::kInvalidHandle != _handle.idx; }
  892. ]]
  893. function codegen.gen_handle(handle)
  894. assert(handle.handle, "Not a handle")
  895. return (handle_temp:gsub("$(%u+)", { NAME = handle.name }))
  896. end
  897. local idl = require "idl"
  898. local doxygen = require "doxygen"
  899. local conversion
  900. local idlfile = {}
  901. function codegen.load(filename)
  902. assert(conversion == nil, "Don't call codegen.load() after codegen.idl()")
  903. assert(idlfile[filename] == nil, "Duplicate load " .. filename)
  904. local source = doxygen.load(filename)
  905. local f = assert(load(source, filename , "t", idl))
  906. f()
  907. idlfile[filename] = true
  908. end
  909. function codegen.idl(filename)
  910. if conversion == nil then
  911. if filename and not idlfile[filename] then
  912. codegen.load(filename)
  913. end
  914. assert(next(idlfile), "call codegen.load() first")
  915. conversion = true
  916. codegen.nameconversion(idl.types, idl.funcs)
  917. end
  918. return idl
  919. end
  920. return codegen