1 #############################################################################
3 # Copyright 2008 Tungsten Graphics, Inc.
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.
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.
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/>.
18 #############################################################################
32 def __init__(self, expr, id = ''):
36 assert char.isalnum() or char in '_ '
38 id = id.replace(' ', '_')
44 assert id not in all_types
61 def dump(self, instance):
62 raise NotImplementedError
64 def wrap_instance(self, instance):
67 def unwrap_instance(self, instance):
74 Type.__init__(self, "void")
82 print 'void Dump%s(const %s &value);' % (self.id, self.expr)
85 print 'void Dump%s(const %s &value) {' % (self.id, self.expr)
90 def _dump(self, instance):
91 raise NotImplementedError
93 def dump(self, instance):
94 print ' Dump%s(%s);' % (self.id, instance)
97 class Intrinsic(Concrete):
99 def __init__(self, expr, format):
100 Concrete.__init__(self, expr)
103 def _dump(self, instance):
104 print ' Log::TextF("%s", %s);' % (self.format, instance)
109 def __init__(self, type):
111 if isinstance(type, Pointer):
112 expr = type.expr + " const"
114 expr = "const " + type.expr
116 Type.__init__(self, expr, 'C' + type.id)
120 def dump(self, instance):
121 self.type.dump(instance)
126 def __init__(self, type):
127 Type.__init__(self, type.expr + " *", 'P' + type.id)
130 def dump(self, instance):
131 print ' if(%s) {' % instance
132 print ' Log::BeginReference("%s", %s);' % (self.type, instance)
134 self.type.dump("*" + instance)
135 except NotImplementedError:
137 print ' Log::EndReference();'
140 print ' Log::Text("NULL");'
142 def wrap_instance(self, instance):
143 self.type.wrap_instance("*" + instance)
145 def unwrap_instance(self, instance):
146 self.type.wrap_instance("*" + instance)
149 def ConstPointer(type):
150 return Pointer(Const(type))
153 class OutPointer(Pointer):
159 class Enum(Concrete):
161 def __init__(self, name, values):
162 Concrete.__init__(self, name)
165 def _dump(self, instance):
166 print ' switch(%s) {' % instance
167 for value in self.values:
168 print ' case %s:' % value
169 print ' Log::Text("%s");' % value
172 print ' Log::TextF("%%i", %s);' % instance
177 class FakeEnum(Enum):
179 def __init__(self, type, values):
180 Enum.__init__(self, type.expr, values)
184 class Flags(Concrete):
186 def __init__(self, type, values):
187 Concrete.__init__(self, type.expr)
191 def _dump(self, instance):
192 print ' bool l_First = TRUE;'
193 print ' %s l_Value = %s;' % (self.type, instance)
194 for value in self.values:
195 print ' if((l_Value & %s) == %s) {' % (value, value)
196 print ' if(!l_First)'
197 print ' Log::Text(" | ");'
198 print ' Log::Text("%s");' % value
199 print ' l_Value &= ~%s;' % value
200 print ' l_First = FALSE;'
202 print ' if(l_Value || l_First) {'
203 print ' if(!l_First)'
204 print ' Log::Text(" | ");'
205 self.type.dump("l_Value");
209 class Struct(Concrete):
211 def __init__(self, name, members):
212 Concrete.__init__(self, name)
213 self.members = members
215 def _dump(self, instance):
216 for type, name in self.members:
217 print ' Log::BeginElement("%s", "%s");' % (type, name)
218 type.dump('(%s).%s' % (instance, name))
219 print ' Log::EndElement();'
224 def __init__(self, name, type):
225 Type.__init__(self, name)
228 def dump(self, instance):
229 self.type.dump(instance)
234 def __init__(self, type, name, args, call = '__stdcall', fail = None):
241 def prototype(self, name=None):
248 s = self.call + ' ' + s
249 if name.startswith('*'):
251 s = self.type.expr + ' ' + s
254 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
260 def pointer_type(self):
261 return 'P' + self.name
263 def pointer_value(self):
264 return 'p' + self.name
267 ptype = self.pointer_type()
268 pvalue = self.pointer_value()
269 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
270 print 'static %s %s = NULL;' % (ptype, pvalue)
273 def get_true_pointer(self):
274 raise NotImplementedError
277 if self.fail is not None:
278 if self.type is Void:
279 assert self.fail == ''
282 assert self.fail != ''
283 print ' return %s;' % self.fail
285 print ' ExitProcess(0);'
288 pvalue = self.pointer_value()
289 print self.prototype() + ' {'
290 if self.type is Void:
293 print ' %s result;' % self.type
295 self.get_true_pointer()
296 print ' Log::BeginCall("%s");' % (self.name)
297 for type, name in self.args:
298 if not type.isoutput():
299 type.unwrap_instance(name)
300 print ' Log::BeginArg("%s", "%s");' % (type, name)
302 print ' Log::EndArg();'
303 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
304 for type, name in self.args:
306 print ' Log::BeginArg("%s", "%s");' % (type, name)
308 print ' Log::EndArg();'
309 type.wrap_instance(name)
310 if self.type is not Void:
311 print ' Log::BeginReturn("%s");' % self.type
312 self.type.dump("result")
313 print ' Log::EndReturn();'
314 self.type.wrap_instance('result')
315 print ' Log::EndCall();'
316 self.post_call_impl()
317 if self.type is not Void:
318 print ' return result;'
322 def post_call_impl(self):
326 class Interface(Type):
328 def __init__(self, name, base=None):
329 Type.__init__(self, name)
334 def itermethods(self):
335 if self.base is not None:
336 for method in self.base.itermethods():
338 for method in self.methods:
343 return "Wrap" + self.expr
345 def wrap_pre_decl(self):
346 print "class %s;" % self.wrap_name()
349 print "class %s : public %s " % (self.wrap_name(), self.name)
352 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
353 print " virtual ~%s();" % self.wrap_name()
355 for method in self.itermethods():
356 print " " + method.prototype() + ";"
359 print " %s * m_pInstance;" % (self.name,)
364 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
365 print ' m_pInstance = pInstance;'
368 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
371 for method in self.itermethods():
372 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
373 if method.type is Void:
376 print ' %s result;' % method.type
378 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
379 print ' Log::BeginArg("%s *", "this");' % self.name
380 print ' Log::BeginReference("%s", m_pInstance);' % self.name
381 print ' Log::EndReference();'
382 print ' Log::EndArg();'
383 for type, name in method.args:
384 if not type.isoutput():
385 type.unwrap_instance(name)
386 print ' Log::BeginArg("%s", "%s");' % (type, name)
388 print ' Log::EndArg();'
389 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
390 for type, name in method.args:
392 print ' Log::BeginArg("%s", "%s");' % (type, name)
394 print ' Log::EndArg();'
395 type.wrap_instance(name)
396 if method.type is not Void:
397 print ' Log::BeginReturn("%s");' % method.type
398 method.type.dump("result")
399 print ' Log::EndReturn();'
400 method.type.wrap_instance('result')
401 print ' Log::EndCall();'
402 if method.name == 'QueryInterface':
403 print ' if(*ppvObj == m_pInstance)'
404 print ' *ppvObj = this;'
405 if method.name == 'Release':
406 assert method.type is not Void
408 print ' delete this;'
409 if method.type is not Void:
410 print ' return result;'
416 class Method(Function):
418 def __init__(self, type, name, args):
419 Function.__init__(self, type, name, args)
424 class WrapPointer(Pointer):
426 def __init__(self, type):
427 Pointer.__init__(self, type)
428 if type not in towrap:
431 def wrap_instance(self, instance):
432 print " if(%s)" % instance
433 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
435 def unwrap_instance(self, instance):
436 print " if(%s)" % instance
437 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
443 Type.__init__(self, "char *")
445 def dump(self, instance):
446 print ' Log::DumpString((const char *)%s);' % instance
450 class _WString(Type):
453 Type.__init__(self, "wchar_t *")
455 def dump(self, instance):
456 print ' Log::DumpWString(%s);' % instance
461 SChar = Intrinsic("signed char", "%i")
462 UChar = Intrinsic("unsigned char", "%u")
463 Short = Intrinsic("short", "%i")
464 Int = Intrinsic("int", "%i")
465 Long = Intrinsic("long", "%li")
466 UShort = Intrinsic("unsigned short", "%u")
467 UInt = Intrinsic("unsigned int", "%u")
468 ULong = Intrinsic("unsigned long", "%lu")
469 Float = Intrinsic("float", "%f")
470 Double = Intrinsic("double", "%f")
474 for type in all_types.itervalues():
477 for type in all_types.itervalues():