From 5ae7f84b4afd861ca41b5c211c2a413599f4e04a Mon Sep 17 00:00:00 2001 From: Mauro Iazzi Date: Mon, 28 Apr 2008 17:37:12 +0200 Subject: [PATCH] moved code completed getting types from stack initial code for calling simple functions --- new/entities.lua | 27 ------------ new/generator.lua | 127 +++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 102 insertions(+), 52 deletions(-) diff --git a/new/entities.lua b/new/entities.lua index 2f98c84..8ac3ca4 100644 --- a/new/entities.lua +++ b/new/entities.lua @@ -60,33 +60,6 @@ entities.class_is_copy_constructible = function(c) end local class_is_copy_constructible = entities.class_is_copy_constructible -entities.arguments_on_stack = function(f) - assert(is_function(f), 'argument is not a function') - local args_on_stack = '' - --print('=====', f.xarg.fullname) - for _,a in ipairs(f) do - --local st, err = pcall(type_on_stack, a) - --if not st then table.foreach(a, print) end - --assert(st, err) - local err = type_on_stack(a) - args_on_stack = args_on_stack .. err - end - if takes_this_pointer(f) then - args_on_stack = f.xarg.member_of .. '*;' .. args_on_stack - end - return args_on_stack -end -local arguments_on_stack = entities.arguments_on_stack - --- TODO: must wait for a way to specify pushing base types -entities.calling_code = function(f) - assert(is_function(f), 'argument is not a function') - local ret, indent = '', ' ' - for _,a in ipairs(f) do - ret = ret .. indent .. '\n' - end -end -local calling_code = entities.calling_code diff --git a/new/generator.lua b/new/generator.lua index f5e6286..092422a 100644 --- a/new/generator.lua +++ b/new/generator.lua @@ -29,11 +29,11 @@ local base_types = dofile'types.lua' do local t = {} - for _, v in pairs(xmlstream.byid) do + for _, v in pairs(xmlstream.byid) do if v.xarg.fullname then local o = t[v.xarg.fullname] or {} table.insert(o, v) t[v.xarg.fullname] = o - end + end end get_from_fullname = function(n) return t[n] end @@ -104,13 +104,30 @@ local get_class = function(fullname) fullname .. ' *>(LqtGetClassType(L, '..tostring(j)..', "' .. fullname .. '*"));' end end +local get_constref = function(fullname) + return function(i,j) + j = j or -i + return fullname .. ' const& arg' .. tostring(i) .. ' = *static_cast< ' .. + fullname .. ' *>(LqtGetClassType(L, '..tostring(j)..', "' .. fullname .. '*"));' + end +end +local get_ref = function(fullname) + return function(i,j) + j = j or -i + return fullname .. '& arg' .. tostring(i) .. ' = *static_cast< ' .. + fullname .. ' *>(LqtGetClassType(L, '..tostring(j)..', "' .. fullname .. '*"));' + end +end type_properties = function(t) local typename = type(t)=='string' and t or t.xarg.type_name + if rawget(base_types, typename) then local ret = rawget(base_types, typename) return ret.on_stack, ret.get end + + -- not a base type if type(t)=='string' or t.xarg.type_base==typename then local identifier = get_from_fullname(typename) assert(identifier and #identifier==1, 'cannot resolve base type: '..typename) @@ -122,32 +139,38 @@ type_properties = function(t) else error('unknown identifier type: '..identifier.label) end - else - if t.xarg.array then - error'I cannot manipulate arrays' - elseif string.match(typename, '%(%*%)') then - -- function pointer type - -- FIXME: the XML description does not contain this info - error'I cannot manipulate function pointers' - elseif t.xarg.indirections then - if t.xarg.indirections=='1' then - local b = assert(get_from_fullname(t.xarg.type_base), 'unknown type base')[1] - if b.label=='Class' then - return t.xarg.type_base..'*;' - else - error('I cannot manipulate pointers to '..t.xarg.type_base) - end + elseif t.xarg.array then + error'I cannot manipulate arrays' + elseif string.match(typename, '%(%*%)') then + -- function pointer type + -- FIXME: the XML description does not contain this info + error'I cannot manipulate function pointers' + elseif t.xarg.indirections then + if t.xarg.indirections=='1' then + local b = get_from_fullname(t.xarg.type_base) + b = assert(b, 'unknown base type '..t.xarg.type_base)[1] + if b.label=='Class' then + -- TODO: check if other modifiers are in place? + return t.xarg.type_base..'*;', get_pointer(t.xarg.type_base) + else + error('I cannot manipulate pointers to '..t.xarg.type_base) end - error'I cannot manipulate double pointers' - else - -- this is any combination of constant, volatile and reference - -- we ignore this info and treat this as normal value - return type_on_stack(t.xarg.type_base) end + error'I cannot manipulate double pointers' + else + -- this is any combination of constant, volatile and reference + local ret_get = nil + if typename==(t.xarg.type_base..' const&') then + ret_get = get_constref(t.xarg.type_base) + elseif typename==(t.xarg.type_base..'&') then + ret_get = get_ref(t.xarg.type_base) + end + assert(ret_get, 'cannot get non-base type '..typename..' from stack') + return type_properties(t.xarg.type_base), ret_get end end -return_type = function(f) +entities.return_type = function(f) assert_function(f) if entities.is_destructor(f) then return nil @@ -157,14 +180,34 @@ return_type = function(f) f.xarg.type_name = f.xarg.type_base..'*' f.xarg.indirections='1' return f + elseif f.xarg.type_name=='' or f.xarg.type_name=='void' then + return nil else return f end end + +local arguments_on_stack = function(f) + assert_function(f) + local args_on_stack = '' + --print('=====', f.xarg.fullname) + for _,a in ipairs(f) do + --local st, err = pcall(type_on_stack, a) + --if not st then table.foreach(a, print) end + --assert(st, err) + local err = type_properties(a) + args_on_stack = args_on_stack .. err + end + if entities.takes_this_pointer(f) then + args_on_stack = f.xarg.member_of .. '*;' .. args_on_stack + end + return args_on_stack +end + function_description = function(f) assert_function(f) - local args_on_stack = entities.arguments_on_stack(f) + local args_on_stack = arguments_on_stack(f) return f.xarg.type_name .. ' ' .. f.xarg.fullname .. ' (' .. args_on_stack .. ')'.. (f.xarg.static=='1' and ' [static]' or '').. (f.xarg.virtual=='1' and ' [virtual]' or '').. @@ -173,10 +216,44 @@ function_description = function(f) ' [in ' .. tostring(f.xarg.member_of) .. ']' end +-- TODO: must wait for a way to specify pushing base types +local calling_code = function(f) + assert_function(f) + local ret, indent = '', ' ' + local n = 0 + for _,a in ipairs(f) do if a.label=='Argument' then + n = n + 1 + local d, g, p = type_properties(a) + ret = ret .. indent .. g(n) .. '\n' + end end + if entities.is_constructor(f) then + elseif entities.is_constructor(f) then + elseif entities.takes_this_pointer(f) then + else + local args = '' + for i = 1,n do + args = args .. (i > 1 and ', arg' or 'arg') .. tostring(i) + end + args = '('..args..')'; + local ret_type = entities.return_type(f) + ret_type = ret_type and ret_type.xarg.type_name or nil + local call_line = (ret_type and (ret_type..' ret = ') or '') + call_line = call_line .. f.xarg.fullname .. args + ret = ret .. indent .. call_line .. ';\n' + end + return ret +end + for _, v in pairs(xmlstream.byid) do if string.find(v.label, 'Function')==1 then local status, err = pcall(function_description, v) - io[status and 'stdout' or 'stderr']:write((status and '' or v.xarg.fullname..': ')..err..'\n') + --io[status and 'stdout' or 'stderr']:write((status and '' or v.xarg.fullname..': ')..err..'\n') + if status then + local s, e = pcall(calling_code, v) + io[s and 'stdout' or 'stderr']:write((s and '' + or ('error calling '..v.xarg.fullname..': '))..e..(s and '' or '\n')) + end + --io[status and 'stdout' or 'stderr']:write((status and '' or v.xarg.fullname..': ')..err..'\n') end end --table.foreach(name_list, print) -- 2.11.4.GIT