Added Qt specific includes to make more bindings compile cleanly from the start
[lqt.git] / qt_generator.lua
blob48701c24270e16e4271d2f9731666dc0bbd094bb
1 #!/usr/bin/lua
3 xml = dofile'xml.lua'
4 B = dofile'binder.lua'
6 function NOINSTANCE(b, name)
7 b.types_from_stack[name] = function(i) error('cannot copy '..name) end
8 b.types_test[name] = function(i) error('cannot copy '..name) end
9 b.types_to_stack[name] = function(i) error('cannot copy '..name) end
11 b.types_from_stack[(name..' *')] = function(i) return '*static_cast<'..name..'**>(lqtL_toudata(L, '..tostring(i)..', "'..name..'"))' end
12 b.types_test[(name..' *')] = function(i) return 'lqtL_testudata(L, '..tostring(i)..', "'..name..'*")' end
13 b.types_to_stack[(name..' *')] = function(i) return 'lqtL_pushudata(L, '..tostring(i)..', "'..name..'*")' end
14 end
16 function cp_file(src, dst)
18 src = (type(src)=='string') and io.open(src, 'r') or src
19 check = (type(dst)=='string') and io.open(dst, 'r') or nil
20 if type(check)=='userdata' then
21 local a, b = src:read'*a', check:read'*a'
22 if a==b then check:close() src:close() return end
23 check:close()
24 end
25 end
26 src = (type(src)=='string') and io.open(src, 'r') or src
27 dst = (type(dst)=='string') and io.open(dst, 'w') or dst
28 local content = src:read('*a')
29 dst:write(content)
30 src:close()
31 dst:close()
32 end
34 function BINDQT(n)
35 n = tostring(n)
36 local h, c = B:make_namespace(n, n, 'QtCore', 'QtGui')
37 print(n..': writing definition file')
38 f = io.open('src/lqt_bind_'..n..'.cpp', 'w')
39 f:write(c)
40 f:close()
42 print(n..': writing prototypes file')
43 f = io.open('src/lqt_bind_'..n..'.hpp', 'w')
44 f:write(h)
45 f:close()
46 end
48 function init_qt(B)
49 B.filter = function (m)
50 local n = type(m)=='table' and type(m.attr)=='table' and m.attr.name
51 if n and string.match(n, "[_%w]*[xX]11[_%w]*$") then
52 return true, 'it is X11 specific'
53 end
54 if n and string.match(n, "[_%w]*_[_%w]*$") then
55 return true, 'it is meant to be internal'
56 end
57 return false
58 end
61 NOINSTANCE(B, 'QCoreApplication')
64 B.types_from_stack['const QString&'] = function(i) return 'QString::fromAscii(lua_tostring(L, '..tostring(i)..'), lua_objlen(L, '..tostring(i)..'))' end
65 B.types_test['const QString&'] = function(i) return '(lua_type(L, ' .. tostring(i) .. ')==LUA_TSTRING)' end
66 B.types_to_stack['const QString&'] = function(i) return 'lua_pushlstring(L, '..tostring(i)..'.toAscii().data(), '..tostring(i)..'.toAscii().size())' end
67 B.types_from_stack['QString'] = function(i) return 'QString::fromAscii(lua_tostring(L, '..tostring(i)..'), lua_objlen(L, '..tostring(i)..'))' end
68 B.types_test['QString'] = function(i) return '(lua_type(L, ' .. tostring(i) .. ')==LUA_TSTRING)' end
69 B.types_to_stack['QString'] = function(i) return 'lua_pushlstring(L, '..tostring(i)..'.toAscii().data(), '..tostring(i)..'.toAscii().size())' end
71 B.types_from_stack['QByteArray'] = function(i) return 'QByteArray(lua_tostring(L, '..tostring(i)..'), lua_objlen(L, '..tostring(i)..'))' end
72 B.types_test['QByteArray'] = function(i) return '(lua_type(L, ' .. tostring(i) .. ')==LUA_TSTRING)' end
73 B.types_to_stack['QByteArray'] = function(i) return 'lua_pushlstring(L, '..tostring(i)..'.data(), '..tostring(i)..'.size())' end
74 end
76 function make_tree (cl, tf)
77 f = io.open(tf..'.cpp', 'w')
78 for n in pairs(cl) do
79 f:write('#include <'..n..'>\n')
80 end
81 f:write'\nmain() {\n'
82 for n in pairs(cl) do
83 if not ({Qt=true})[n] then f:write(' '..n..' *'..string.lower(n)..';\n') end
84 end
85 f:write'}\n'
86 f:close()
87 os.execute('gccxml `pkg-config QtGui QtCore --cflags` -fxml='..tf..'.xml '..tf..'.cpp')
88 --os.execute'gccxml -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -fxml=auto.xml auto.cpp'
89 os.remove(tf..'.cpp')
90 end
92 function make_standard_qt(B, classlist)
93 print'copying common files'
94 cp_file('lqt_qt_utils.hpp', 'src/lqt_qt_utils.hpp')
95 cp_file('lqt_qt_utils.cpp', 'src/lqt_qt_utils.cpp')
96 cp_file('lqt_common.hpp', 'src/lqt_common.hpp')
97 cp_file('lqt_common.cpp', 'src/lqt_common.cpp')
100 local clist = {}
101 for s in string.gmatch(classlist, '([%u%l%d]+)') do
102 clist[s] = true
104 classlist = clist
107 local tmpfile='tmp/auto'
109 make_tree(classlist, tmpfile)
111 B:init(tmpfile..'.xml')
112 init_qt(B)
115 local clist = {}
116 for n in pairs(classlist) do
117 local c = B:find_name(n)
118 clist = B.set_union(clist, B:tree_of_bases(c))
120 classlist = B.set_union(classlist, clist)
123 for n in pairs(classlist) do
124 BINDQT(n)
128 function make_single_qt(B, class)
129 local classlist = { class }
131 local tmpfile='tmp/auto'
133 make_tree(classlist, tmpfile)
134 B:init(tmpfile..'.xml')
135 init_qt(B)
137 BINDQT(class)
140 function B:enum_push_body_plus_qt(id, c)
141 local enum = (type(id)=='string') and self:find_id(id) or id
142 local e_static = (self:find_id(enum.attr.context).tag == 'Class') and 'static ' or ''
143 local e_context = self:context_name(enum)
144 local e_name = 'lqt_pushenum_' .. enum.attr.name
145 local e_proto, e_def = '', ''
147 e_proto = e_proto .. ' ' .. e_static .. self.lua_proto(e_name) .. ';\n'
148 e_def = e_def .. self.lua_proto(c .. e_name) .. ' '
149 e_def = e_def .. '{\n'
150 e_def = e_def .. ' int enum_table = 0;\n'
151 e_def = e_def .. ' lua_getfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);\n'
152 e_def = e_def .. ' if (!lua_istable(L, -1)) {\n'
153 e_def = e_def .. ' lua_pop(L, 1);\n'
154 e_def = e_def .. ' lua_newtable(L);\n'
155 e_def = e_def .. ' lua_pushvalue(L, -1);\n'
156 e_def = e_def .. ' lua_setfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);\n'
157 e_def = e_def .. ' }\n'
159 e_def = e_def .. ' lua_newtable(L);\n'
160 e_def = e_def .. ' enum_table = lua_gettop(L);\n'
161 for i, e in ipairs(enum) do
162 if (type(e)=='table') and (e.tag=='EnumValue') then
163 e_def = e_def .. ' lua_pushstring(L, "' .. e.attr.name .. '");\n'
164 e_def = e_def .. ' lua_rawseti(L, enum_table, ' .. e.attr.init .. ');\n'
165 e_def = e_def .. ' lua_pushinteger(L, ' .. e.attr.init .. ');\n'
166 e_def = e_def .. ' lua_setfield(L, enum_table, "' .. e.attr.name .. '");\n'
169 e_def = e_def .. ' lua_pushcfunction(L, ' .. c .. e_name .. '_QFLAGS_CREATOR' .. ');\n'
170 e_def = e_def .. ' lua_setfield(L, enum_table, "QFlags");\n'
171 e_def = e_def .. ' lua_pushvalue(L, -1);\n'
172 e_def = e_def .. ' lua_setfield(L, -3, "' .. e_context .. enum.attr.name .. '");\n'
173 e_def = e_def .. ' lua_remove(L, -2);\n'
174 e_def = e_def .. ' return 1;\n'
175 e_def = e_def .. '}\n'
176 -- ######## QFLAGS SPECIFIC
177 e_proto = e_proto .. ' ' .. e_static .. self.lua_proto(e_name..'_QFLAGS_CREATOR') .. ';\n'
178 e_def = e_def .. self.lua_proto(c .. e_name .. '_QFLAGS_CREATOR') .. [[ {
179 int argn = lua_gettop(L);
180 int i = 0;
181 void *p = lua_newuserdata(L, sizeof(QFlags<]]..e_context..enum.attr.name..[[>*) + sizeof(QFlags<]]..e_context..enum.attr.name..[[>));
182 QFlags<]]..e_context..enum.attr.name..[[> *fl = static_cast<QFlags<]]..e_context..enum.attr.name..[[>*>( static_cast<void*>(&static_cast<QFlags<]]..e_context..enum.attr.name..[[>**>(p)[1]) );
183 *(void**)p = fl;
184 for (i=1;i<=argn;i++) {
185 *fl |= static_cast<]]..e_context..enum.attr.name..[[>(lqtL_toenum(L, i, "]]..e_context..enum.attr.name..[["));
187 if (luaL_newmetatable(L, "QFlags<]]..e_context..enum.attr.name..[[>*")) {
188 lua_pushstring(L, "QFlags<]]..e_context..enum.attr.name..[[>*");
189 lua_setfield(L, -2, "__qtype");
191 lua_setmetatable(L, -2);
192 return 1;
195 --print (e_def)
196 return e_proto, e_def, e_name
199 B.enum_push_body = B.enum_push_body_plus_qt
202 make_standard_qt(B,
204 QAbstractScrollArea
205 QAction
206 QApplication
207 QBoxLayout
208 QColor
209 QCoreApplication
210 QDialog
211 QEvent
212 QFileDialog
213 QFont
214 QFrame
215 QGraphicsItem
216 QGraphicsScene
217 QGraphicsView
218 QHBoxLayout
219 QIcon
220 QImage
221 QInputEvent
222 QKeyEvent
223 QKeySequence
224 QLabel
225 QLayout
226 QLayoutItem
227 QLineEdit
228 QMainWindow
229 QMenuBar
230 QMenu
231 QMessageBox
232 QObject
233 QPaintDevice
234 QShortcut
235 QSize
236 QSyntaxHighlighter
238 QTextBlockUserData
239 QTextCharFormat
240 QTextCursor
241 QTextDocument
242 QTextEdit
243 QTextFormat
244 QToolBar
245 QVBoxLayout
246 QWidget