examine types appearing in the given file
[lqt.git] / new / generator.lua
blob19492806301c4da47e7d81b3ff384eed518d060a
1 #!/usr/bin/lua
3 local my = {
4 readfile = function(fn) local f = assert(io.open(fn)) local s = f:read'*a' f:close() return s end
8 local filename = ...
9 local path = string.match(arg[0], '(.*/)[^%/]+') or ''
10 local xmlstream = dofile(path..'xml.lua')(my.readfile(filename))
11 local code = xmlstream[1]
13 local decompound = function(n)
14 -- test function pointer
15 local r, a = string.match(n, '(.-) %(%*%) (%b())')
16 if r and a then
17 -- only single arguments are supported
18 return 'function', r, string.match(a, '%(([^,]*)%))')
19 end
20 return nil
21 end
24 local base_types = dofile'types.lua'
26 -- this only works on resolved types (no typedefs) and no compound types
27 local types_desc = setmetatable({}, {
28 __index = function(t, k)
29 -- exclude base types
30 if rawget(base_types, k) then
31 t[k] = rawget(base_types, k)
32 return rawget(base_types, k)
33 end
34 -- exclude templates
35 if string.match(k, '[<>]') then
36 return nil -- explicitly won't support templates yet
37 end
39 -- traverse namespace tree
40 local space = code
41 local iter = string.gmatch(k, '[^:]+')
42 for n in iter do if space.byname and space.byname[n] then
43 space = space.byname[n]
44 if type(space)=='table' and space.label=='TypeAlias' then
45 print(space.xarg.fullname, k)
46 error'you should resolve aliases before calling this function'
47 end -- is an alias?
48 else -- this name is not in this space
49 -- this is probably a template argument (at least in Qt) so we do not care for now
50 do return nil end
51 error(tostring(k)..' '..tostring(space.fullname)..' '..tostring(n) )
52 end end
54 -- make use of final result
55 if type(space)~='table' then
56 return nil
57 else
58 t[k] = space
59 end
60 return t[k]
61 end,
64 local types_name = setmetatable({}, {
65 __index = function(t, k)
66 -- exclude base types
67 if rawget(base_types, k) then
68 t[k] = k
69 return k
70 end
71 -- exclude templates
72 if string.match(k, '<') then
73 return nil -- explicitly won't support templates yet
74 end
76 -- traverse namespace tree
77 local space = code
78 local iter = string.gmatch(k, '[^:]+')
79 for n in iter do if space.byname and space.byname[n] then
80 space = space.byname[n]
81 if type(space)=='table' and space.label=='TypeAlias' then
82 print('^^^^^^^^', k)
83 local alias = space.xarg.type_name
84 if space.xarg.type_base~=alias then
85 -- if it is not a pure object name, it should not have members
86 if iter() then
87 error'compound type shouldn\'t have members'
88 else
89 local sub = t[space.xarg.type_base]
90 local ret = sub and string.gsub(alias, space.xarg.type_base, sub) or nil
91 print('++++', ret)
92 t[k] = ret
93 return ret
94 end
95 else -- alias to compound type?
96 for i in iter do alias = alias..'::'..i end -- reconstruct full name
97 --print ('----', k, 'is alias for', n)
98 local ret = t[alias]
99 if ret then t[k] = ret end
100 print ('----', k, 'is alias for', alias, 'and is', ret)
101 return ret
102 end -- alias to compound type?
103 end -- is an alias?
104 else
105 t[k] = nil
106 return nil
107 end end
109 -- make use of final result
110 if type(space)~='table' then
111 t[k] = nil
112 else
113 t[k] = space.xarg.fullname
115 return t[k]
116 end,
120 local cache = {}
121 for _, v in pairs(xmlstream.byid) do
122 if v.xarg.type_base then
123 if not string.match(v.xarg.context, '[<,]%s*'..v.xarg.type_base..'%s*[>,]') then
124 local __ = types_desc[v.xarg.type_base]
125 else
128 --if v.xarg.scope~=v.xarg.context..'::' then print(v.label, v.xarg.id, v.xarg.type_name, v.xarg.scope, v.xarg.context) end
129 --cache[v.xarg.context] = true
130 --print(pushtype(v)(v.xarg.name), ' // '.._..': '..v.label..' : '..(v.xarg.type_name or ''))
131 --assert(type_name(v.xarg.type_base, v.xarg.type_constant, v.xarg.type_volatile, v.xarg.type_reference, v.xarg.indirections or 0)==v.xarg.type_name)
133 --table.foreach(cache, print)
135 local t = {}
136 --table.foreach(types_desc, function(i, j) t[j] = true end)
137 table.foreach(types_desc, print)