José Fonseca | 9544244 | 2008-07-08 10:32:53 +0900 | [diff] [blame] | 1 | ############################################################################# |
| 2 | # |
| 3 | # Copyright 2008 Jose Fonseca |
| 4 | # |
| 5 | # This program is free software: you can redistribute it and/or modify it |
| 6 | # under the terms of the GNU Lesser General Public License as published |
| 7 | # by the Free Software Foundation, either version 3 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU Lesser General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU Lesser General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | # |
| 18 | ############################################################################# |
| 19 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 20 | """C basic types""" |
| 21 | |
| 22 | class Type: |
| 23 | |
| 24 | def __init__(self, name): |
| 25 | self.name = name |
| 26 | |
| 27 | def __str__(self): |
| 28 | return self.name |
| 29 | |
| 30 | def isoutput(self): |
| 31 | return False |
| 32 | |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 33 | def dump(self, instance): |
| 34 | raise NotImplementedError |
| 35 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 36 | def wrap_instance(self, instance): |
| 37 | pass |
| 38 | |
José Fonseca | 27cd25d | 2008-07-07 13:44:00 +0900 | [diff] [blame] | 39 | def unwrap_instance(self, instance): |
| 40 | pass |
| 41 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 42 | |
| 43 | class Void(Type): |
| 44 | |
| 45 | def __init__(self): |
| 46 | Type.__init__(self, "void") |
| 47 | |
| 48 | Void = Void() |
| 49 | |
| 50 | |
| 51 | class Intrinsic(Type): |
| 52 | |
| 53 | def __init__(self, name, format): |
| 54 | Type.__init__(self, name) |
| 55 | self.format = format |
| 56 | |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 57 | def dump(self, instance): |
| 58 | print ' g_pLog->TextF("%s", %s);' % (self.format, instance) |
| 59 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 60 | |
| 61 | class Const(Type): |
| 62 | |
| 63 | def __init__(self, type): |
| 64 | Type.__init__(self, 'C' + type.name) |
| 65 | self.type = type |
| 66 | |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 67 | def dump(self, instance): |
| 68 | self.type.dump(instance) |
| 69 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 70 | def __str__(self): |
| 71 | return "const " + str(self.type) |
| 72 | |
| 73 | |
| 74 | class Pointer(Type): |
| 75 | |
| 76 | def __init__(self, type): |
| 77 | Type.__init__(self, 'P' + type.name) |
| 78 | self.type = type |
| 79 | |
| 80 | def __str__(self): |
| 81 | return str(self.type) + " *" |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 82 | |
| 83 | def dump(self, instance): |
José Fonseca | 4a9c40c | 2008-07-07 18:04:53 +0900 | [diff] [blame] | 84 | print ' if(%s) {' % instance |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 85 | try: |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 86 | self.type.dump("*" + instance) |
José Fonseca | 4a9c40c | 2008-07-07 18:04:53 +0900 | [diff] [blame] | 87 | except NotImplementedError: |
| 88 | print ' g_pLog->TextF("%%p", %s);' % instance |
| 89 | print ' }' |
| 90 | print ' else' |
| 91 | print ' g_pLog->Text("NULL");' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 92 | |
| 93 | def wrap_instance(self, instance): |
| 94 | self.type.wrap_instance("*" + instance) |
| 95 | |
José Fonseca | 27cd25d | 2008-07-07 13:44:00 +0900 | [diff] [blame] | 96 | def unwrap_instance(self, instance): |
| 97 | self.type.wrap_instance("*" + instance) |
| 98 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 99 | |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 100 | class OutPointer(Pointer): |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 101 | |
| 102 | def isoutput(self): |
| 103 | return True |
| 104 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 105 | |
| 106 | class Enum(Type): |
| 107 | |
| 108 | def __init__(self, name, values): |
| 109 | Type.__init__(self, name) |
| 110 | self.values = values |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 111 | |
| 112 | def dump(self, instance): |
| 113 | print ' switch(%s) {' % instance |
| 114 | for value in self.values: |
| 115 | print ' case %s:' % value |
| 116 | print ' g_pLog->Text("%s");' % value |
| 117 | print ' break;' |
| 118 | print ' default:' |
| 119 | print ' g_pLog->TextF("%%i", %s);' % instance |
| 120 | print ' break;' |
| 121 | print ' }' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 122 | |
| 123 | |
| 124 | class Flags(Type): |
| 125 | |
| 126 | def __init__(self, type, values): |
| 127 | Type.__init__(self, type.name) |
| 128 | self.values = values |
| 129 | |
| 130 | |
| 131 | class Struct(Type): |
| 132 | |
| 133 | def __init__(self, name, members): |
| 134 | Type.__init__(self, name) |
| 135 | self.members = members |
| 136 | |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 137 | def dump(self, instance): |
| 138 | print ' g_pLog->Text("{");' |
| 139 | first = True |
| 140 | for type, name in self.members: |
| 141 | if first: |
| 142 | first = False |
| 143 | else: |
| 144 | print ' g_pLog->Text(", ");' |
| 145 | type.dump('(%s).%s' % (instance, name)) |
| 146 | print ' g_pLog->Text("}");' |
| 147 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 148 | |
| 149 | class Alias(Type): |
| 150 | |
| 151 | def __init__(self, name, type): |
| 152 | Type.__init__(self, name) |
| 153 | self.type = type |
| 154 | |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 155 | def dump(self, instance): |
| 156 | self.type.dump(instance) |
| 157 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 158 | |
| 159 | class Function: |
| 160 | |
| 161 | def __init__(self, type, name, args, call = '__stdcall'): |
| 162 | self.type = type |
| 163 | self.name = name |
| 164 | self.args = args |
| 165 | self.call = call |
| 166 | |
| 167 | def prototype(self, name=None): |
| 168 | if name is not None: |
| 169 | name = name.strip() |
| 170 | else: |
| 171 | name = self.name |
| 172 | s = name |
| 173 | if self.call: |
| 174 | s = self.call + ' ' + s |
| 175 | if name.startswith('*'): |
| 176 | s = '(' + s + ')' |
| 177 | s = str(self.type) + ' ' + s |
| 178 | s += "(" |
| 179 | if self.args: |
| 180 | s += ", ".join(["%s %s" % (type, name) for type, name in self.args]) |
| 181 | else: |
| 182 | s += "void" |
| 183 | s += ")" |
| 184 | return s |
| 185 | |
| 186 | |
| 187 | class Interface(Type): |
| 188 | |
| 189 | def __init__(self, name, base=None): |
| 190 | Type.__init__(self, name) |
| 191 | self.base = base |
| 192 | self.methods = [] |
| 193 | |
| 194 | def itermethods(self): |
| 195 | if self.base is not None: |
| 196 | for method in self.base.itermethods(): |
| 197 | yield method |
| 198 | for method in self.methods: |
| 199 | yield method |
| 200 | raise StopIteration |
| 201 | |
| 202 | def wrap_name(self): |
| 203 | return "Wrap" + self.name |
| 204 | |
| 205 | def wrap_pre_decl(self): |
| 206 | print "class %s;" % self.wrap_name() |
| 207 | |
| 208 | def wrap_decl(self): |
| 209 | print "class %s : public %s " % (self.wrap_name(), self.name) |
| 210 | print "{" |
| 211 | print "public:" |
| 212 | print " %s(%s * pInstance);" % (self.wrap_name(), self.name) |
| 213 | print " virtual ~%s();" % self.wrap_name() |
| 214 | print |
| 215 | for method in self.itermethods(): |
| 216 | print " " + method.prototype() + ";" |
| 217 | print |
José Fonseca | 27cd25d | 2008-07-07 13:44:00 +0900 | [diff] [blame] | 218 | #print "private:" |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 219 | print " %s * m_pInstance;" % (self.name,) |
| 220 | print "};" |
| 221 | print |
| 222 | |
| 223 | def wrap_impl(self): |
| 224 | print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name) |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 225 | print ' m_pInstance = pInstance;' |
| 226 | print '}' |
| 227 | print |
| 228 | print '%s::~%s() {' % (self.wrap_name(), self.wrap_name()) |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 229 | print '}' |
| 230 | print |
| 231 | for method in self.itermethods(): |
| 232 | print method.prototype(self.wrap_name() + '::' + method.name) + ' {' |
| 233 | if method.type is Void: |
| 234 | result = '' |
| 235 | else: |
| 236 | print ' %s result;' % method.type |
| 237 | result = 'result = ' |
José Fonseca | d760589 | 2008-07-07 13:09:31 +0900 | [diff] [blame] | 238 | print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name) |
José Fonseca | 4a9c40c | 2008-07-07 18:04:53 +0900 | [diff] [blame] | 239 | print ' g_pLog->BeginParam("this", "%s *");' % self.name |
| 240 | print ' g_pLog->TextF("%p", m_pInstance);' |
| 241 | print ' g_pLog->EndParam();' |
José Fonseca | 27cd25d | 2008-07-07 13:44:00 +0900 | [diff] [blame] | 242 | for type, name in method.args: |
| 243 | if not type.isoutput(): |
| 244 | type.unwrap_instance(name) |
José Fonseca | a83fb24 | 2008-07-07 16:55:52 +0900 | [diff] [blame] | 245 | print ' g_pLog->BeginParam("%s", "%s");' % (name, type) |
| 246 | type.dump(name) |
| 247 | print ' g_pLog->EndParam();' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 248 | print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args])) |
| 249 | for type, name in method.args: |
| 250 | if type.isoutput(): |
José Fonseca | 22aa688 | 2008-07-07 17:33:30 +0900 | [diff] [blame] | 251 | print ' g_pLog->BeginParam("%s", "%s");' % (name, type) |
| 252 | type.dump(name) |
| 253 | print ' g_pLog->EndParam();' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 254 | type.wrap_instance(name) |
| 255 | if method.type is not Void: |
José Fonseca | 22aa688 | 2008-07-07 17:33:30 +0900 | [diff] [blame] | 256 | print ' g_pLog->BeginReturn("%s");' % method.type |
José Fonseca | 4a9c40c | 2008-07-07 18:04:53 +0900 | [diff] [blame] | 257 | method.type.dump("result") |
José Fonseca | 22aa688 | 2008-07-07 17:33:30 +0900 | [diff] [blame] | 258 | print ' g_pLog->EndReturn();' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 259 | method.type.wrap_instance('result') |
José Fonseca | 22aa688 | 2008-07-07 17:33:30 +0900 | [diff] [blame] | 260 | print ' g_pLog->EndCall();' |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 261 | if method.name == 'QueryInterface': |
| 262 | print ' if(*ppvObj == m_pInstance)' |
| 263 | print ' *ppvObj = this;' |
| 264 | if method.name == 'Release': |
| 265 | assert method.type is not Void |
| 266 | print ' if(!result)' |
| 267 | print ' delete this;' |
| 268 | if method.type is not Void: |
| 269 | print ' return result;' |
| 270 | print '}' |
| 271 | print |
| 272 | print |
| 273 | |
| 274 | |
| 275 | class Method(Function): |
| 276 | |
| 277 | def __init__(self, type, name, args): |
| 278 | Function.__init__(self, type, name, args) |
| 279 | |
| 280 | |
| 281 | towrap = [] |
| 282 | |
| 283 | class WrapPointer(Pointer): |
| 284 | |
| 285 | def __init__(self, type): |
| 286 | Pointer.__init__(self, type) |
| 287 | if type not in towrap: |
| 288 | towrap.append(type) |
| 289 | |
| 290 | def wrap_instance(self, instance): |
| 291 | print " if(%s)" % instance |
| 292 | print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance) |
| 293 | |
José Fonseca | 27cd25d | 2008-07-07 13:44:00 +0900 | [diff] [blame] | 294 | def unwrap_instance(self, instance): |
| 295 | print " if(%s)" % instance |
| 296 | print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance) |
| 297 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 298 | String = Intrinsic("char *", "%s") |
| 299 | Int = Intrinsic("int", "%i") |
| 300 | Long = Intrinsic("long", "%li") |
| 301 | Float = Intrinsic("float", "%f") |
| 302 | |
| 303 | |
| 304 | def wrap(): |
| 305 | for type in towrap: |
| 306 | type.wrap_pre_decl() |
| 307 | print |
| 308 | for type in towrap: |
| 309 | type.wrap_decl() |
| 310 | print |
| 311 | for type in towrap: |
| 312 | type.wrap_impl() |
| 313 | print |