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
12 elseif lookup_table
[object
] then
13 return lookup_table
[object
]
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()
21 else new_table
[_copy(index
)] = _copy(value
) 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
])
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
52 o
[k
] = o
[k
]:gsub(orig
, new
)
53 elseif k
== 'member_template_parameters' then
56 elseif type(v
) == "table" then
57 template_repare(v
, orig
, new
)
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
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_')
76 ignore(name
, 'template not bound')
81 --- Append any created classes to the index
82 function finish(index
)
83 for f
in pairs(idindex_add
) do