| 1 |
-- tolua: operator class |
| 2 |
-- Written by Waldemar Celes |
| 3 |
-- TeCGraf/PUC-Rio |
| 4 |
-- Jul 1998 |
| 5 |
-- $Id: $ |
| 6 |
|
| 7 |
-- This code is free software; you can redistribute it and/or modify it. |
| 8 |
-- The software provided hereunder is on an "as is" basis, and |
| 9 |
-- the author has no obligation to provide maintenance, support, updates, |
| 10 |
-- enhancements, or modifications. |
| 11 |
|
| 12 |
|
| 13 |
-- Operator class |
| 14 |
-- Represents an operator function or a class operator method. |
| 15 |
-- It stores the same fields as functions do plus: |
| 16 |
-- kind = set of character representing the operator (as it appers in C++ code) |
| 17 |
classOperator = { |
| 18 |
kind = '', |
| 19 |
} |
| 20 |
classOperator.__index = classOperator |
| 21 |
setmetatable(classOperator,classFunction) |
| 22 |
|
| 23 |
-- table to transform operator kind into the appropriate tag method name |
| 24 |
_TM = {['+'] = 'add', |
| 25 |
['-'] = 'sub', |
| 26 |
['*'] = 'mul', |
| 27 |
['/'] = 'div', |
| 28 |
['<'] = 'lt', |
| 29 |
['<='] = 'le', |
| 30 |
['=='] = 'eq', |
| 31 |
['[]'] = 'geti', |
| 32 |
['&[]'] = 'seti', |
| 33 |
--['->'] = 'flechita', |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
-- Print method |
| 38 |
function classOperator:print (ident,close) |
| 39 |
print(ident.."Operator{") |
| 40 |
print(ident.." kind = '"..self.kind.."',") |
| 41 |
print(ident.." mod = '"..self.mod.."',") |
| 42 |
print(ident.." type = '"..self.type.."',") |
| 43 |
print(ident.." ptr = '"..self.ptr.."',") |
| 44 |
print(ident.." name = '"..self.name.."',") |
| 45 |
print(ident.." const = '"..self.const.."',") |
| 46 |
print(ident.." cname = '"..self.cname.."',") |
| 47 |
print(ident.." lname = '"..self.lname.."',") |
| 48 |
print(ident.." args = {") |
| 49 |
local i=1 |
| 50 |
while self.args[i] do |
| 51 |
self.args[i]:print(ident.." ",",") |
| 52 |
i = i+1 |
| 53 |
end |
| 54 |
print(ident.." }") |
| 55 |
print(ident.."}"..close) |
| 56 |
end |
| 57 |
|
| 58 |
function classOperator:supcode_tmp() |
| 59 |
|
| 60 |
if not _TM[self.kind] then |
| 61 |
return classFunction.supcode(self) |
| 62 |
end |
| 63 |
|
| 64 |
-- no overload, no parameters, always inclass |
| 65 |
output("/* method:",self.name," of class ",self:inclass()," */") |
| 66 |
|
| 67 |
output("#ifndef TOLUA_DISABLE_"..self.cname) |
| 68 |
output("\nstatic int",self.cname,"(lua_State* tolua_S)") |
| 69 |
|
| 70 |
if overload < 0 then |
| 71 |
output('#ifndef TOLUA_RELEASE\n') |
| 72 |
end |
| 73 |
output(' tolua_Error tolua_err;') |
| 74 |
output(' if (\n') |
| 75 |
-- check self |
| 76 |
local is_func = get_is_function(self.parent.type) |
| 77 |
output(' !'..is_func..'(tolua_S,1,"'..self.parent.type..'",0,&tolua_err) ||\n') |
| 78 |
output(' !tolua_isnoobj(tolua_S,2,&tolua_err)\n )') |
| 79 |
output(' goto tolua_lerror;') |
| 80 |
|
| 81 |
output(' else\n') |
| 82 |
output('#endif\n') -- tolua_release |
| 83 |
output(' {') |
| 84 |
|
| 85 |
-- declare self |
| 86 |
output(' ',self.const,self.parent.type,'*','self = ') |
| 87 |
output('(',self.const,self.parent.type,'*) ') |
| 88 |
local to_func = get_to_func(self.parent.type) |
| 89 |
output(to_func,'(tolua_S,1,0);') |
| 90 |
|
| 91 |
-- check self |
| 92 |
output('#ifndef TOLUA_RELEASE\n') |
| 93 |
output(' if (!self) tolua_error(tolua_S,"'..output_error_hook("invalid \'self\' in function \'%s\'", self.name)..'",NULL);'); |
| 94 |
output('#endif\n') |
| 95 |
|
| 96 |
-- cast self |
| 97 |
output(' ',self.mod,self.type,self.ptr,'tolua_ret = ') |
| 98 |
output('(',self.mod,self.type,self.ptr,')(*self);') |
| 99 |
|
| 100 |
-- return value |
| 101 |
local t,ct = isbasic(self.type) |
| 102 |
if t then |
| 103 |
output(' tolua_push'..t..'(tolua_S,(',ct,')tolua_ret);') |
| 104 |
else |
| 105 |
t = self.type |
| 106 |
local push_func = get_push_function(t) |
| 107 |
new_t = string.gsub(t, "const%s+", "") |
| 108 |
if self.ptr == '' then |
| 109 |
output(' {') |
| 110 |
output('#ifdef __cplusplus\n') |
| 111 |
output(' void* tolua_obj = Mtolua_new((',new_t,')(tolua_ret));') |
| 112 |
output(' ',push_func,'(tolua_S,tolua_obj,"',t,'");') |
| 113 |
output(' tolua_register_gc(tolua_S,lua_gettop(tolua_S));') |
| 114 |
output('#else\n') |
| 115 |
output(' void* tolua_obj = tolua_copy(tolua_S,(void*)&tolua_ret,sizeof(',t,'));') |
| 116 |
output(' ',push_func,'(tolua_S,tolua_obj,"',t,'");') |
| 117 |
output(' tolua_register_gc(tolua_S,lua_gettop(tolua_S));') |
| 118 |
output('#endif\n') |
| 119 |
output(' }') |
| 120 |
elseif self.ptr == '&' then |
| 121 |
output(' ',push_func,'(tolua_S,(void*)&tolua_ret,"',t,'");') |
| 122 |
else |
| 123 |
if local_constructor then |
| 124 |
output(' ',push_func,'(tolua_S,(void *)tolua_ret,"',t,'");') |
| 125 |
output(' tolua_register_gc(tolua_S,lua_gettop(tolua_S));') |
| 126 |
else |
| 127 |
output(' ',push_func,'(tolua_S,(void*)tolua_ret,"',t,'");') |
| 128 |
end |
| 129 |
end |
| 130 |
end |
| 131 |
|
| 132 |
output(' }') |
| 133 |
output(' return 1;') |
| 134 |
|
| 135 |
output('#ifndef TOLUA_RELEASE\n') |
| 136 |
output('tolua_lerror:\n') |
| 137 |
output(' tolua_error(tolua_S,"'..output_error_hook("#ferror in function \'%s\'.", self.lname)..'",&tolua_err);') |
| 138 |
output(' return 0;') |
| 139 |
output('#endif\n') |
| 140 |
|
| 141 |
|
| 142 |
output('}') |
| 143 |
output('#endif //#ifndef TOLUA_DISABLE\n') |
| 144 |
output('\n') |
| 145 |
end |
| 146 |
|
| 147 |
-- Internal constructor |
| 148 |
function _Operator (t) |
| 149 |
setmetatable(t,classOperator) |
| 150 |
|
| 151 |
if t.const ~= 'const' and t.const ~= '' then |
| 152 |
error("#invalid 'const' specification") |
| 153 |
end |
| 154 |
|
| 155 |
append(t) |
| 156 |
if not t:inclass() then |
| 157 |
error("#operator can only be defined as class member") |
| 158 |
end |
| 159 |
|
| 160 |
--t.name = t.name .. "_" .. (_TM[t.kind] or t.kind) |
| 161 |
t.cname = t:cfuncname("tolua")..t:overload(t) |
| 162 |
t.name = "operator" .. t.kind -- set appropriate calling name |
| 163 |
return t |
| 164 |
end |
| 165 |
|
| 166 |
-- Constructor |
| 167 |
function Operator (d,k,a,c) |
| 168 |
|
| 169 |
local op_k = string.gsub(k, "^%s*", "") |
| 170 |
op_k = string.gsub(k, "%s*$", "") |
| 171 |
--if string.find(k, "^[%w_:%d<>%*%&]+$") then |
| 172 |
if d == "operator" and k ~= '' then |
| 173 |
|
| 174 |
d = k.." operator" |
| 175 |
elseif not _TM[op_k] then |
| 176 |
|
| 177 |
if flags['W'] then |
| 178 |
error("tolua: no support for operator" .. f.kind) |
| 179 |
else |
| 180 |
warning("No support for operator "..op_k..", ignoring") |
| 181 |
return nil |
| 182 |
end |
| 183 |
end |
| 184 |
|
| 185 |
local ref = '' |
| 186 |
local t = split_c_tokens(strsub(a,2,strlen(a)-1),',') -- eliminate braces |
| 187 |
local i=1 |
| 188 |
local l = {n=0} |
| 189 |
while t[i] do |
| 190 |
l.n = l.n+1 |
| 191 |
l[l.n] = Declaration(t[i],'var') |
| 192 |
i = i+1 |
| 193 |
end |
| 194 |
if k == '[]' then |
| 195 |
local _ |
| 196 |
_, _, ref = strfind(d,'(&)') |
| 197 |
d = gsub(d,'&','') |
| 198 |
elseif k=='&[]' then |
| 199 |
l.n = l.n+1 |
| 200 |
l[l.n] = Declaration(d,'var') |
| 201 |
l[l.n].name = 'tolua_value' |
| 202 |
end |
| 203 |
local f = Declaration(d,'func') |
| 204 |
if k == '[]' and (l[1]==nil or isbasic(l[1].type)~='number') then |
| 205 |
error('operator[] can only be defined for numeric index.') |
| 206 |
end |
| 207 |
f.args = l |
| 208 |
f.const = c |
| 209 |
f.kind = op_k |
| 210 |
f.lname = "."..(_TM[f.kind] or f.kind) |
| 211 |
if not _TM[f.kind] then |
| 212 |
f.cast_operator = true |
| 213 |
end |
| 214 |
if f.kind == '[]' and ref=='&' and f.const~='const' then |
| 215 |
Operator(d,'&'..k,a,c) -- create correspoding set operator |
| 216 |
end |
| 217 |
return _Operator(f) |
| 218 |
end |
| 219 |
|
| 220 |
|