Update BUILD
[lqt/mk.git] / generator / enums.lua
blob03249e39d5ec28c2d590c32176550646d2ab4363
1 module("enums", package.seeall)
3 local print_enum = fprint(assert(io.open(module_name.._src..module_name..'_enum.cpp', 'w')))
5 local function filter(enum)
6 local n = enum.xarg.name
7 if n~=string.lower(n) and not string.match(n, '_') then
8 return false
9 end
10 return true
11 end
13 local enum_list = {}
15 local function copy_enums(index)
16 for e in pairs(index) do
17 if e.label=='Enum'
18 and not string.match(e.xarg.fullname, '%b<>') then
19 if e.xarg.access=='public' and not filter(e) then
20 enum_list[e.xarg.fullname] = e
21 elseif e.xarg.access == 'protected' then
22 -- register it anyway
23 enum_list[e.xarg.fullname] = e
24 local c = fullnames[e.xarg.context]
25 assert(type(c) == "table" and c.label == "Class", "cannot find parent of enum "..e.xarg.fullname)
26 if not c.protected_enums then c.protected_enums = {} end
27 table.insert(c.protected_enums, e)
28 end
29 end
30 end
31 end
33 local function fill_enum_values()
34 for _,e in pairs(enum_list) do
35 local values = {}
36 for _, v in ipairs(e) do
37 if v.label=='Enumerator' then
38 table.insert(values, v)
39 end
40 end
41 e.values = values
42 end
43 end
45 function fill_enum_tables()
46 for _,e in pairs(enum_list) do
47 local table = 'lqt_Enum lqt_enum'..e.xarg.id..'[] = {\n'
48 for _,v in pairs(e.values) do
49 table = table .. ' { "' .. v.xarg.name
50 .. '", static_cast<int>('..v.xarg.fullname..') },\n'
51 end
52 table = table .. ' { 0, 0 }\n'
53 table = table .. '};\n'
54 e.enum_table = table
55 end
56 end
58 function fill_typesystem(types)
59 local etype = function(en)
60 return {
61 push = function(n)
62 return 'lqtL_pushenum(L, '..n..', "'..string.gsub(en, '::', '.')..'")', 1
63 end,
64 get = function(n)
65 return 'static_cast<'..en..'>'
66 ..'(lqtL_toenum(L, '..n..', "'..string.gsub(en, '::', '.')..'"))', 1
67 end,
68 test = function(n)
69 return 'lqtL_isenum(L, '..n..', "'..string.gsub(en, '::', '.')..'")', 1
70 end,
71 onstack = string.gsub(en, '::', '.')..',',
72 defect = 10, -- check these last
74 end
75 for _,e in pairs(enum_list) do
76 if not types[e.xarg.fullname] then
77 types[e.xarg.fullname] = etype(e.xarg.fullname)
78 else
79 --io.stderr:write(e.xarg.fullname, ': already present\n')
80 end
81 end
82 end
85 function print_enum_tables()
86 for _,e in pairs(enum_list) do
87 if e.xarg.access == 'public' then print_enum('static ' .. e.enum_table) end
88 end
89 return enums
90 end
92 function print_enum_creator(mod)
93 local out = 'static lqt_Enumlist lqt_enum_list[] = {\n'
94 for _,e in pairs(enum_list) do
95 if e.xarg.access == 'public' then
96 out = out..' { lqt_enum'..e.xarg.id..', "'..string.gsub(e.xarg.fullname, "::", ".")..'" },\n'
97 end
98 end
99 out = out..' { 0, 0 },\n};\n'
100 out = out .. 'void lqt_create_enums_'..mod..' (lua_State *L) {\n'
101 out = out .. ' lqtL_createenumlist(L, lqt_enum_list); return;\n}\n'
102 print_enum(out)
105 ---------------------------------------------------------------------
107 function preprocess(index)
108 copy_enums(index)
111 function process(index, types)
112 fill_enum_values()
113 fill_enum_tables()
114 fill_typesystem(types)
117 function output()
118 print_enum(output_includes)
119 print_enum_tables()
120 print_enum_creator(module_name)