Re-enable access to protected functions
[lqt/mk.git] / generator / operators.lua
blob36de353fdd9d76b756cece7a9b6d8775f1131a9c
1 module('operators', package.seeall)
3 local operatorTrans = {
4 ['<<'] = 'IN',
5 ['>>'] = 'OUT',
6 ['+='] = 'ADD',
7 ['-='] = 'SUB',
8 ['*='] = 'MUL',
9 ['/='] = 'DIV',
10 ['++'] = 'INC',
11 ['--'] = 'DEC',
12 ['+'] = '__add',
13 ['-'] = '__sub',
14 ['_'] = '__unm',
15 ['*'] = '__mul',
16 ['/'] = '__div',
17 ['=='] = '__eq',
20 local function is_helper_function(name)
21 local s1 = name:sub(1,1)
22 local s2 = name:sub(2,2)
23 return s1 == 'q' and s2 == s2:upper()
24 end
26 function fix_operators(index)
27 for f in pairs(index) do
28 if f.label == "Function" then
29 if f.xarg.name:match("^operator") and f.xarg.friend then
30 if f[1].xarg.type_base == f.xarg.member_of then
31 -- overloaded friend operator - its first argument is 'this',
32 -- needs to be removed
33 table.remove(f, 1)
34 table.remove(f.arguments, 1)
35 if f.xarg.name == 'operator-' and #f.arguments == 0 then
36 -- use '_' as a marker for unary minus
37 f.xarg.name = 'operator_'
38 end
39 else
40 -- operator in form: number OP class - do not bind these
41 f.ignore = true
42 end
43 elseif is_helper_function(f.xarg.name) then
44 f.ignore = true
45 end
46 end
47 end
48 end
50 function call_line(f)
51 local op = operators.get_operator(f.xarg.name)
52 if op == "*" and #f.arguments == 0 then
53 ignore(f.xarg.fullname, "pointer dereference operator", f.xarg.member_of_class)
54 return nil
55 elseif op == '_' then
56 -- unary minus
57 return '- *self', false
58 elseif op == '++' or op == '--' then
59 -- the ++ and -- operators don't line () at the end, like: *self ++()
60 if f.arguments[1] then
61 f.arguments[1] = nil
62 return op..' *self', false
63 else
64 return '*self '..op, false
65 end
66 else
67 return '*self '..op..'(', true
68 end
69 end
71 function get_operator(name)
72 return name:match('^operator(.+)$')
73 end
75 function is_operator(name)
76 return name:match('^operator.') and operatorTrans[get_operator(name)]
77 end
79 function rename_operator(name)
80 local trans = operatorTrans[get_operator(name)]
81 if is_operator(name) and trans then
82 return trans
83 end
84 return name
85 end