Fix compilation on Mac OS X with Qt in custom directory
[lqt/mk.git] / generator / templates.lua
blob5a1bcfda7513f6b046a309ada87e6b3e3d90624c
1 module('templates', package.seeall)
3 -- TODO: maybe automate this?
4 local translate = dofile(template_file)
6 --- Creates a deep copy of an object.
7 local function deepcopy(object)
8 local lookup_table = {}
9 local function _copy(object)
10 if type(object) ~= "table" then
11 return object
12 elseif lookup_table[object] then
13 return lookup_table[object]
14 end
15 local new_table = {}
16 lookup_table[object] = new_table
17 for index, value in pairs(object) do
18 -- HACK: generate new ids for copied nodes
19 if index == "id" then new_table.id = next_id()
20 -- deep copy value
21 else new_table[_copy(index)] = _copy(value) end
22 end
23 return new_table
24 end
25 return _copy(object)
26 end
28 -- cannot modify idindex directly while traversing it ->
29 -- new methods from template classes are added here first and then
30 -- added to idindex after the traversal
31 local idindex_add = {}
34 --- Return true if an instance of templated class should be created.
35 function should_copy(class)
36 return translate[class.xarg.fullname] or
37 (translate[module_name] and translate[module_name][class.xarg.fullname])
38 end
40 --- Creates instantiated copies of template class.
41 -- New classes are created according to the 'translate' table as deep copies, and
42 -- are inserted into the 'ret' table.
43 function create(class, ret)
44 local temps = should_copy(class)
46 local replace_in = {name=true, context=true, fullname=true, member_of=true, member_of_class=true,
47 scope=true, type_base=true, type_name=true, return_type=true }
49 local function template_repare(o, orig, new)
50 for k,v in pairs(o) do
51 if replace_in[k] then
52 o[k] = o[k]:gsub(orig, new)
53 elseif k == 'member_template_parameters' then
54 -- ignore
55 o[k] = nil
56 elseif type(v) == "table" then
57 template_repare(v, orig, new)
58 end
59 end
60 if o.label and o.label:match'^Function' then
61 idindex_add[o] = true -- will be copied to index, so that later it can be picked up by copy_functions
62 end
63 end
65 local name = class.xarg.fullname
66 for _, t in ipairs(temps) do
67 local oclass, oparams = name:match('^(.+)<([^>]+)>$')
68 local tclass, tparams = t:match('^(.+)<([^>]+)>$')
69 if tclass == oclass then
70 -- TODO: handle multiple template parameters
71 local copy = deepcopy(class)
72 template_repare(copy, oparams, tparams)
73 copy.xarg.cname = copy.xarg.fullname:gsub('[<>*]', '_'):gsub('::', '_LQT_')
74 ret[copy] = true
75 else
76 ignore(name, 'template not bound')
77 end
78 end
79 end
81 --- Append any created classes to the index
82 function finish(index)
83 for f in pairs(idindex_add) do
84 index[f] = true
85 end
86 idindex_add = {}
87 end