moved the generator to source root
[lqt.git] / entities.lua
blob6cd0a442fec2f14be4c9d2a101ca607f308c5627
1 #!/usr/bin/lua
3 local entities = {}
5 entities.is_function = function(f)
6 if type(f)~='table' or string.find(f.label, 'Function')~=1 then
7 return false
8 else
9 return true
10 end
11 end
12 local is_function = entities.is_function
15 entities.is_constructor = function(f)
16 assert(is_function(f), 'argument is not a function')
17 return (f.xarg.member_of_class and f.xarg.member_of_class~=''
18 and f.xarg.fullname==(f.xarg.member_of_class..'::'..f.xarg.name) -- this should be always true
19 and string.match(f.xarg.member_of_class, f.xarg.name..'$')) and '[constructor]'
20 end
21 local is_constructor = entities.is_constructor
23 entities.is_destructor = function(f)
24 assert(is_function(f), 'argument is not a function')
25 return f.xarg.name:sub(1,1)=='~' and '[destructor]'
26 end
27 local is_destructor = entities.is_destructor
29 entities.takes_this_pointer = function(f)
30 assert(is_function(f), 'argument is not a function')
31 if f.xarg.member_of_class and not (f.xarg.static=='1') and f.xarg.member_of_class~=''
32 and not is_constructor(f) then
33 return f.xarg.member_of_class .. '*;'
34 end
35 return false
36 end
37 local takes_this_pointer = entities.takes_this_pointer
39 entities.is_class = function(c)
40 if type(c)=='table' and c.label=='Class' then
41 return true
42 else
43 return false
44 end
45 end
46 local is_class = entities.is_class
48 entities.class_is_copy_constructible = function(c)
49 -- TODO: cache the response into the class itself (c.xarg.is_copy_constructible)
50 assert(is_class(c), 'this is NOT a class')
51 for _, m in ipairs(c) do
52 if is_function(m)
53 and is_constructor(m)
54 and #m==1
55 and m.xarg.access=='public'
56 and (m[1].xarg.type_name==c.xarg.fullname..' const&'
57 or m[1].xarg.type_name==c.xarg.fullname..'&'
58 or m[1].xarg.type_name==c.xarg.fullname) then
59 return true
60 end
61 end
62 return false
63 end
64 local class_is_copy_constructible = entities.class_is_copy_constructible
66 entities.class_is_default_constructible = function(c)
67 -- TODO: cache the response into the class itself (c.xarg.is_copy_constructible)
68 assert(is_class(c), 'this is NOT a class')
69 for _, m in ipairs(c) do
70 if is_function(m)
71 and is_constructor(m)
72 and #m==0
73 and m.xarg.access=='public' then
74 return true
75 end
76 end
77 return false
78 end
79 local class_is_default_constructible = entities.class_is_default_constructible
81 entities.default_constructor = function(t)
82 if t.xarg.type_name then
83 if t.xarg.type_name:match'%b[]$' then
84 return 'NULL'
85 elseif t.xarg.type_name:match'%(%*%)' then
86 return 'NULL'
87 elseif t.xarg.type_name:match'%&$' then
88 return nil
89 elseif t.xarg.indirections then
90 return 'NULL'
91 else
92 return 'static_cast< '..t.xarg.type_name..' >(0)'
93 end
94 else
95 if t.label=='Class' and class_is_default_constructible(t) then
96 return t.xarg.fullname..'()'
97 elseif t.label=='Class' then
98 return nil
99 else
100 return 'static_cast< '..t.xarg.type_name..' >(0)'
106 return entities