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 #############################################################################
30 def __init__(self, name):
37 return self.name.replace(' ', '_')
48 def dump(self, instance):
49 raise NotImplementedError
51 def wrap_instance(self, instance):
54 def unwrap_instance(self, instance):
61 Type.__init__(self, "void")
68 def __init__(self, name):
70 assert char.isalnum() or char in '_ '
72 Type.__init__(self, name)
74 assert self.name not in all_types
75 if self.name not in all_types:
76 all_types[self.name] = self
79 print 'void Dump%s(const %s &value);' % (self.identifier(), str(self))
82 print 'void Dump%s(const %s &value) {' % (self.identifier(), str(self))
87 def _dump(self, instance):
88 raise NotImplementedError
90 def dump(self, instance):
91 print ' Dump%s(%s);' % (self.identifier(), instance)
94 class Intrinsic(Concrete):
96 def __init__(self, expr, format, name = None):
99 Concrete.__init__(self, name)
103 def _dump(self, instance):
104 print ' Log::TextF("%s", %s);' % (self.format, instance)
112 def __init__(self, type):
113 Type.__init__(self, 'C' + type.name)
116 def dump(self, instance):
117 self.type.dump(instance)
120 if isinstance(self.type, Pointer):
121 return str(self.type) + " const"
123 return "const " + str(self.type)
128 def __init__(self, type):
129 Type.__init__(self, 'P' + type.name)
133 return str(self.type) + " *"
135 def dump(self, instance):
136 print ' if(%s) {' % instance
137 print ' Log::BeginReference("%s", %s);' % (self.type, instance)
139 self.type.dump("*" + instance)
140 except NotImplementedError:
142 print ' Log::EndReference();'
145 print ' Log::Text("NULL");'
147 def wrap_instance(self, instance):
148 self.type.wrap_instance("*" + instance)
150 def unwrap_instance(self, instance):
151 self.type.wrap_instance("*" + instance)
154 def ConstPointer(type):
155 return Pointer(Const(type))
158 class OutPointer(Pointer):
164 class Enum(Concrete):
166 def __init__(self, name, values):
167 Concrete.__init__(self, name)
170 def _dump(self, instance):
171 print ' switch(%s) {' % instance
172 for value in self.values:
173 print ' case %s:' % value
174 print ' Log::Text("%s");' % value
177 print ' Log::TextF("%%i", %s);' % instance
182 class FakeEnum(Enum):
186 def __init__(self, type, values):
188 Enum.__init__(self, type.name + str(FakeEnum.__seq), values)
192 return str(self.type)
195 class Flags(Concrete):
199 def __init__(self, type, values):
201 Concrete.__init__(self, type.name + str(Flags.__seq))
206 return str(self.type)
208 def _dump(self, instance):
209 print ' bool l_First = TRUE;'
210 print ' %s l_Value = %s;' % (self.type, instance)
211 for value in self.values:
212 print ' if((l_Value & %s) == %s) {' % (value, value)
213 print ' if(!l_First)'
214 print ' Log::Text(" | ");'
215 print ' Log::Text("%s");' % value
216 print ' l_Value &= ~%s;' % value
217 print ' l_First = FALSE;'
219 print ' if(l_Value || l_First) {'
220 print ' if(!l_First)'
221 print ' Log::Text(" | ");'
222 self.type.dump("l_Value");
226 class Struct(Concrete):
228 def __init__(self, name, members):
229 Concrete.__init__(self, name)
230 self.members = members
232 def _dump(self, instance):
233 for type, name in self.members:
234 print ' Log::BeginElement("%s", "%s");' % (type, name)
235 type.dump('(%s).%s' % (instance, name))
236 print ' Log::EndElement();'
241 def __init__(self, name, type):
242 Type.__init__(self, name)
245 def dump(self, instance):
246 self.type.dump(instance)
251 def __init__(self, type, name, args, call = '__stdcall', fail = None):
258 def prototype(self, name=None):
265 s = self.call + ' ' + s
266 if name.startswith('*'):
268 s = str(self.type) + ' ' + s
271 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
277 def pointer_type(self):
278 return 'P' + self.name
280 def pointer_value(self):
281 return 'p' + self.name
284 ptype = self.pointer_type()
285 pvalue = self.pointer_value()
286 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
287 print 'static %s %s = NULL;' % (ptype, pvalue)
290 def get_true_pointer(self):
291 raise NotImplementedError
294 if self.fail is not None:
295 if self.type is Void:
296 assert self.fail == ''
299 assert self.fail != ''
300 print ' return %s;' % self.fail
302 print ' ExitProcess(0);'
305 pvalue = self.pointer_value()
306 print self.prototype() + ' {'
307 if self.type is Void:
310 print ' %s result;' % self.type
312 self.get_true_pointer()
313 print ' Log::BeginCall("%s");' % (self.name)
314 for type, name in self.args:
315 if not type.isoutput():
316 type.unwrap_instance(name)
317 print ' Log::BeginArg("%s", "%s");' % (type, name)
319 print ' Log::EndArg();'
320 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
321 for type, name in self.args:
323 print ' Log::BeginArg("%s", "%s");' % (type, name)
325 print ' Log::EndArg();'
326 type.wrap_instance(name)
327 if self.type is not Void:
328 print ' Log::BeginReturn("%s");' % self.type
329 self.type.dump("result")
330 print ' Log::EndReturn();'
331 self.type.wrap_instance('result')
332 print ' Log::EndCall();'
333 self.post_call_impl()
334 if self.type is not Void:
335 print ' return result;'
339 def post_call_impl(self):
343 class Interface(Type):
345 def __init__(self, name, base=None):
346 Type.__init__(self, name)
350 def itermethods(self):
351 if self.base is not None:
352 for method in self.base.itermethods():
354 for method in self.methods:
359 return "Wrap" + self.name
361 def wrap_pre_decl(self):
362 print "class %s;" % self.wrap_name()
365 print "class %s : public %s " % (self.wrap_name(), self.name)
368 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
369 print " virtual ~%s();" % self.wrap_name()
371 for method in self.itermethods():
372 print " " + method.prototype() + ";"
375 print " %s * m_pInstance;" % (self.name,)
380 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
381 print ' m_pInstance = pInstance;'
384 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
387 for method in self.itermethods():
388 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
389 if method.type is Void:
392 print ' %s result;' % method.type
394 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
395 print ' Log::BeginArg("%s *", "this");' % self.name
396 print ' Log::BeginReference("%s", m_pInstance);' % self.name
397 print ' Log::EndReference();'
398 print ' Log::EndArg();'
399 for type, name in method.args:
400 if not type.isoutput():
401 type.unwrap_instance(name)
402 print ' Log::BeginArg("%s", "%s");' % (type, name)
404 print ' Log::EndArg();'
405 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
406 for type, name in method.args:
408 print ' Log::BeginArg("%s", "%s");' % (type, name)
410 print ' Log::EndArg();'
411 type.wrap_instance(name)
412 if method.type is not Void:
413 print ' Log::BeginReturn("%s");' % method.type
414 method.type.dump("result")
415 print ' Log::EndReturn();'
416 method.type.wrap_instance('result')
417 print ' Log::EndCall();'
418 if method.name == 'QueryInterface':
419 print ' if(*ppvObj == m_pInstance)'
420 print ' *ppvObj = this;'
421 if method.name == 'Release':
422 assert method.type is not Void
424 print ' delete this;'
425 if method.type is not Void:
426 print ' return result;'
432 class Method(Function):
434 def __init__(self, type, name, args):
435 Function.__init__(self, type, name, args)
440 class WrapPointer(Pointer):
442 def __init__(self, type):
443 Pointer.__init__(self, type)
444 if type not in towrap:
447 def wrap_instance(self, instance):
448 print " if(%s)" % instance
449 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
451 def unwrap_instance(self, instance):
452 print " if(%s)" % instance
453 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
459 Type.__init__(self, "String")
464 def dump(self, instance):
465 print ' Log::DumpString((const char *)%s);' % instance
469 class _WString(Type):
472 Type.__init__(self, "WString")
477 def dump(self, instance):
478 print ' Log::DumpWString(%s);' % instance
483 SChar = Intrinsic("signed char", "%i")
484 UChar = Intrinsic("unsigned char", "%u")
485 Short = Intrinsic("short", "%i")
486 Int = Intrinsic("int", "%i")
487 Long = Intrinsic("long", "%li")
488 UShort = Intrinsic("unsigned short", "%u")
489 UInt = Intrinsic("unsigned int", "%u")
490 ULong = Intrinsic("unsigned long", "%lu")
491 Float = Intrinsic("float", "%f")
492 Double = Intrinsic("double", "%f")
496 for type in all_types.itervalues():
499 for type in all_types.itervalues():