1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
-- flags
local disable_virtual_hooks = false
local enable_pure_virtual = true
local default_private_access = false
local access = {public = 0, protected = 1, private = 2}
function preparse_hook(p)
if default_private_access then
-- we need to make all structs 'public' by default
p.code = string.gsub(p.code, "(struct[^;]*{)", "%1\npublic:\n")
end
end
function parser_hook(s)
local container = classContainer.curr -- get the current container
if default_private_access then
if not container.curr_member_access and container.classtype == 'class' then
-- default access for classes is private
container.curr_member_access = access.private
end
end
-- try labels (public, private, etc)
do
local b,e,label = string.find(s, "^%s*(%w*)%s*:[^:]") -- we need to check for [^:], otherwise it would match 'namespace::type'
if b then
-- found a label, get the new access value from the global 'access' table
if access[label] then
container.curr_member_access = access[label]
end -- else ?
return strsub(s, e) -- normally we would use 'e+1', but we need to preserve the [^:]
end
end
local ret = nil
if disable_virtual_hooks then
return ret
end
local b,e,decl,arg = string.find(s, "^%s*virtual%s+([^%({~]+)(%b())")
local const
if b then
local ret = string.sub(s, e+1)
if string.find(ret, "^%s*const") then
const = "const"
ret = string.gsub(ret, "^%s*const", "")
end
local purev = false
if string.find(ret, "^%s*=%s*0") then
purev = true
ret = string.gsub(ret, "^%s*=%s*0", "")
end
ret = string.gsub(ret, "^%s*%b{}", "")
local func = Function(decl, arg, const)
func.pure_virtual = purev
--func.access = access
func.original_sig = decl
local curflags = classContainer.curr.flags
if not curflags.virtual_class then
curflags.virtual_class = VirtualClass()
end
curflags.virtual_class:add(func)
curflags.pure_virtual = curflags.pure_virtual or purev
return ret
end
return ret
end
-- class VirtualClass
classVirtualClass = {
classtype = 'class',
name = '',
base = '',
type = '',
btype = '',
ctype = '',
}
classVirtualClass.__index = classVirtualClass
setmetatable(classVirtualClass,classClass)
function classVirtualClass:add(f)
local parent = classContainer.curr
pop()
table.insert(self.methods, {f=f})
local name,sig
-- doble negative means positive
if f.name == 'new' and ((not self.flags.parent_object.flags.pure_virtual) or (enable_pure_virtual)) then
name = self.original_name
elseif f.name == 'delete' then
name = '~'..self.original_name
else
if f.access ~= 2 and (not f.pure_virtual) and f.name ~= 'new' and f.name ~= 'delete' then
name = f.mod.." "..f.type..f.ptr.." "..self.flags.parent_object.lname.."__"..f.name
end
end
if name then
sig = name..self:get_arg_list(f, true)..";\n"
push(self)
sig = preprocess(sig)
self:parse(sig)
pop()
end
push(parent)
end
function preprocess(sig)
sig = gsub(sig,"([^%w_])void%s*%*","%1_userdata ") -- substitute 'void*'
sig = gsub(sig,"([^%w_])void%s*%*","%1_userdata ") -- substitute 'void*'
sig = gsub(sig,"([^%w_])char%s*%*","%1_cstring ") -- substitute 'char*'
sig = gsub(sig,"([^%w_])lua_State%s*%*","%1_lstate ") -- substitute 'lua_State*'
return sig
end
function classVirtualClass:get_arg_list(f, decl)
local ret = ""
local sep = ""
local i=1
while f.args[i] do
local arg = f.args[i]
if decl then
local ptr
if arg.ret ~= '' then
ptr = arg.ret
else
ptr = arg.ptr
end
local def = ""
if arg.def and arg.def ~= "" then
def = " = "..arg.def
end
ret = ret..sep..arg.mod.." "..arg.type..ptr.." "..arg.name..def
else
ret = ret..sep..arg.name
end
sep = ","
i = i+1
end
return "("..ret..")"
end
function classVirtualClass:add_parent_virtual_methods(parent)
parent = parent or _global_classes[self.flags.parent_object.btype]
if not parent then return end
if parent.flags.virtual_class then
local vclass = parent.flags.virtual_class
for k,v in ipairs(vclass.methods) do
if v.f.name ~= 'new' and v.f.name ~= 'delete' and (not self:has_method(v.f)) then
table.insert(self.methods, {f=v.f})
end
end
end
parent = _global_classes[parent.btype]
if parent then
self:add_parent_virtual_methods(parent)
end
end
function classVirtualClass:has_method(f)
for k,v in pairs(self.methods) do
-- just match name for now
if v.f.name == f.name then
return true
end
end
return false
end
function classVirtualClass:add_constructors()
local i=1
while self.flags.parent_object[i] do
local v = self.flags.parent_object[i]
if getmetatable(v) == classFunction and (v.name == 'new' or v.name == 'delete') then
self:add(v)
end
i = i+1
end
end
--[[
function classVirtualClass:requirecollection(t)
self:add_constructors()
local req = classClass.requirecollection(self, t)
if req then
output('class ',self.name,";")
end
return req
end
--]]
function classVirtualClass:supcode()
-- pure virtual classes can have no default constructors on gcc 4
if self.flags.parent_object.flags.pure_virtual and not enable_pure_virtual then
output('#if (__GNUC__ == 4) || (__GNUC__ > 4 ) // I hope this works on Microsoft Visual studio .net server 2003 XP Compiler\n')
end
local ns
if self.prox.classtype == 'namespace' then
output('namespace ',self.prox.name, " {")
ns = true
end
output("class "..self.original_name.." : public "..self.btype..", public ToluaBase {")
output("public:\n")
self:add_parent_virtual_methods()
self:output_methods(self.btype)
self:output_parent_methods()
self:add_constructors()
-- no constructor for pure virtual classes
if (not self.flags.parent_object.flags.pure_virtual) or enable_pure_virtual then
self:output_constructors()
end
output("};\n\n")
if ns then
output("};")
end
classClass.supcode(self)
if self.flags.parent_object.flags.pure_virtual and not enable_pure_virtual then
output('#endif // __GNUC__ >= 4\n')
end
-- output collector for custom class if required
if self:requirecollection(_collect) and _collect[self.type] then
output('\n')
output('/* function to release collected object via destructor */')
output('#ifdef __cplusplus\n')
--for i,v in pairs(collect) do
i,v = self.type, _collect[self.type]
output('\nstatic int '..v..' (lua_State* tolua_S)')
output('{')
output(' '..i..'* self = ('..i..'*) tolua_tousertype(tolua_S,1,0);')
output(' delete self;')
output(' return 0;')
output('}')
--end
output('#endif\n\n')
end
end
function classVirtualClass:register(pre)
-- pure virtual classes can have no default constructors on gcc 4
if self.flags.parent_object.flags.pure_virtual and not enable_pure_virtual then
output('#if (__GNUC__ == 4) || (__GNUC__ > 4 )\n')
end
classClass.register(self, pre)
if self.flags.parent_object.flags.pure_virtual and not enable_pure_virtual then
output('#endif // __GNUC__ >= 4\n')
end
end
--function classVirtualClass:requirecollection(_c)
-- if self.flags.parent_object.flags.pure_virtual then
-- return false
-- end
-- return classClass.requirecollection(self, _c)
--end
function classVirtualClass:output_parent_methods()
for k,v in ipairs(self.methods) do
if v.f.access ~= 2 and (not v.f.pure_virtual) and v.f.name ~= 'new' and v.f.name ~= 'delete' then
local rettype = v.f.mod.." "..v.f.type..v.f.ptr.." "
local parent_name = rettype..self.btype.."__"..v.f.name
local par_list = self:get_arg_list(v.f, true)
local var_list = self:get_arg_list(v.f, false)
-- the parent's virtual function
output("\t"..parent_name..par_list.." {")
output("\t\treturn (",rettype,")"..self.btype.."::"..v.f.name..var_list..";")
output("\t};")
end
end
end
function classVirtualClass:output_methods(btype)
for k,v in ipairs(self.methods) do
if v.f.name ~= 'new' and v.f.name ~= 'delete' then
self:output_method(v.f, btype)
end
end
output("\n")
end
function classVirtualClass:output_constructors()
for k,v in ipairs(self.methods) do
if v.f.name == 'new' then
local par_list = self:get_arg_list(v.f, true)
local var_list = self:get_arg_list(v.f, false)
output("\t",self.original_name,par_list,":",self.btype,var_list,"{};")
end
end
end
function classVirtualClass:output_method(f, btype)
if f.access == 2 then -- private
return
end
local ptr
if f.ret ~= '' then
ptr = f.ret
else
ptr = f.ptr
end
local rettype = f.mod.." "..f.type..f.ptr.." "
local par_list = self:get_arg_list(f, true)
local var_list = self:get_arg_list(f, false)
if string.find(rettype, "%s*LuaQtGenericFlags%s*") then
_,_,rettype = string.find(f.original_sig, "^%s*([^%s]+)%s+")
end
-- the caller of the lua method
output("\t"..rettype.." "..f.name..par_list..f.const.." {")
local fn = f.cname
if f.access == 1 then
fn = "NULL"
end
output('\t\tif (push_method("',f.lname,'", ',fn,')) {')
--if f.type ~= 'void' then
-- output("\t\t\tint top = lua_gettop(lua_state)-1;")
--end
-- push the parameters
local argn = 0
for i,arg in ipairs(f.args) do
if arg.type ~= 'void' then
local t,ct = isbasic(arg.type)
if t and t ~= '' then
if arg.ret == "*" then
t = 'userdata'
ct = 'void*'
end
output("\t\t\ttolua_push"..t.."(lua_state, ("..ct..")"..arg.name..");");
else
local m = arg.ptr
if m and m~= "" then
if m == "*" then m = "" end
output("\t\t\ttolua_pushusertype(lua_state, (void*)"..m..arg.name..", \""..arg.type.."\");")
else
output("\t\t\tvoid* tolua_obj = (void*)new "..arg.type.."("..arg.name..");\n")
output('\t\t\ttolua_pushusertype_and_takeownership(lua_state, tolua_obj, "'..arg.type..'");\n')
end
end
argn = argn+1
end
end
-- call the function
output("\t\t\tToluaBase::dbcall(lua_state, ",argn+1,", ")
-- return value
if f.type ~= 'void' then
output("1);")
local t,ct = isbasic(f.type)
if t and t ~= '' then
--output("\t\t\treturn ("..rettype..")tolua_to"..t.."(lua_state, top, 0);")
output("\t\t\t",rettype,"tolua_ret = ("..rettype..")tolua_to"..t.."(lua_state, -1, 0);")
else
local mod = ""
if f.ptr ~= "*" then
mod = "*("..f.type.."*)"
end
--output("\t\t\treturn ("..rettype..")"..mod.."tolua_tousertype(lua_state, top, 0);")
output("\t\t\t",rettype,"tolua_ret = ("..rettype..")"..mod.."tolua_tousertype(lua_state, -1, 0);")
end
output("\t\t\tlua_pop(lua_state, 1);")
output("\t\t\treturn tolua_ret;")
else
output("0);")
end
-- handle non-implemeted function
output("\t\t} else {")
if f.pure_virtual then
output('\t\t\tif (lua_state)')
--output('\t\t\t\ttolua_error(lua_state, "pure-virtual method '..btype.."::"..f.name..' not implemented.", NULL);')
output('\t\t\t\tLOG("pure-virtual method '..btype.."::"..f.name..' not implemented.");')
output('\t\t\telse {')
output('\t\t\t\tLOG("pure-virtual method '..btype.."::"..f.name..' called with no lua_state. Aborting");')
output('\t\t\t\t::abort();')
output('\t\t\t};')
if( rettype == " std::string " ) then
output('\t\t\treturn "";')
else
output('\t\t\treturn (',rettype,')0;')
end
else
output('\t\t\treturn (',rettype,')',btype,'::',f.name,var_list,';')
end
output("\t\t};")
output("\t};")
end
function VirtualClass()
local parent = classContainer.curr
pop()
local name = "Lua__"..parent.original_name
local c = _Class(_Container{name=name, base=parent.name, extra_bases=nil})
setmetatable(c, classVirtualClass)
local ft = getnamespace(c.parent)..c.original_name
append_global_type(ft, c)
push(parent)
c.flags.parent_object = parent
c.methods = {}
push(c)
c:parse("\nvoid tolua__set_instance(_lstate L, lua_Object lo);\n")
pop()
return c
end
|