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");
211 def __init__(self, type, length):
212 Type.__init__(self, type.expr + " *", 'P' + type.id)
216 def dump(self, instance):
217 index = '__i' + self.type.id
218 print ' for (int %s; %s < %s; ++%s) {' % (index, index, self.length, index)
219 print ' Log::BeginElement("%s");' % (self.type,)
220 self.type.dump('(%s)[%s]' % (instance, index))
221 print ' Log::EndElement();'
224 def wrap_instance(self, instance):
225 self.type.wrap_instance("*" + instance)
227 def unwrap_instance(self, instance):
228 self.type.wrap_instance("*" + instance)
231 class Struct(Concrete):
233 def __init__(self, name, members):
234 Concrete.__init__(self, name)
235 self.members = members
237 def _dump(self, instance):
238 for type, name in self.members:
239 print ' Log::BeginElement("%s", "%s");' % (type, name)
240 type.dump('(%s).%s' % (instance, name))
241 print ' Log::EndElement();'
246 def __init__(self, name, type):
247 Type.__init__(self, name)
250 def dump(self, instance):
251 self.type.dump(instance)
256 def __init__(self, type, name, args, call = '__stdcall', fail = None):
263 def prototype(self, name=None):
270 s = self.call + ' ' + s
271 if name.startswith('*'):
273 s = self.type.expr + ' ' + s
276 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
282 def pointer_type(self):
283 return 'P' + self.name
285 def pointer_value(self):
286 return 'p' + self.name
289 ptype = self.pointer_type()
290 pvalue = self.pointer_value()
291 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
292 print 'static %s %s = NULL;' % (ptype, pvalue)
295 def get_true_pointer(self):
296 raise NotImplementedError
299 if self.fail is not None:
300 if self.type is Void:
301 assert self.fail == ''
304 assert self.fail != ''
305 print ' return %s;' % self.fail
307 print ' ExitProcess(0);'
310 pvalue = self.pointer_value()
311 print self.prototype() + ' {'
312 if self.type is Void:
315 print ' %s result;' % self.type
317 self.get_true_pointer()
318 print ' Log::BeginCall("%s");' % (self.name)
319 for type, name in self.args:
320 if not type.isoutput():
321 type.unwrap_instance(name)
322 print ' Log::BeginArg("%s", "%s");' % (type, name)
324 print ' Log::EndArg();'
325 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
326 for type, name in self.args:
328 print ' Log::BeginArg("%s", "%s");' % (type, name)
330 print ' Log::EndArg();'
331 type.wrap_instance(name)
332 if self.type is not Void:
333 print ' Log::BeginReturn("%s");' % self.type
334 self.type.dump("result")
335 print ' Log::EndReturn();'
336 self.type.wrap_instance('result')
337 print ' Log::EndCall();'
338 self.post_call_impl()
339 if self.type is not Void:
340 print ' return result;'
344 def post_call_impl(self):
348 class Interface(Type):
350 def __init__(self, name, base=None):
351 Type.__init__(self, name)
356 def itermethods(self):
357 if self.base is not None:
358 for method in self.base.itermethods():
360 for method in self.methods:
365 return "Wrap" + self.expr
367 def wrap_pre_decl(self):
368 print "class %s;" % self.wrap_name()
371 print "class %s : public %s " % (self.wrap_name(), self.name)
374 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
375 print " virtual ~%s();" % self.wrap_name()
377 for method in self.itermethods():
378 print " " + method.prototype() + ";"
381 print " %s * m_pInstance;" % (self.name,)
386 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
387 print ' m_pInstance = pInstance;'
390 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
393 for method in self.itermethods():
394 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
395 if method.type is Void:
398 print ' %s result;' % method.type
400 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
401 print ' Log::BeginArg("%s *", "this");' % self.name
402 print ' Log::BeginReference("%s", m_pInstance);' % self.name
403 print ' Log::EndReference();'
404 print ' Log::EndArg();'
405 for type, name in method.args:
406 if not type.isoutput():
407 type.unwrap_instance(name)
408 print ' Log::BeginArg("%s", "%s");' % (type, name)
410 print ' Log::EndArg();'
411 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
412 for type, name in method.args:
414 print ' Log::BeginArg("%s", "%s");' % (type, name)
416 print ' Log::EndArg();'
417 type.wrap_instance(name)
418 if method.type is not Void:
419 print ' Log::BeginReturn("%s");' % method.type
420 method.type.dump("result")
421 print ' Log::EndReturn();'
422 method.type.wrap_instance('result')
423 print ' Log::EndCall();'
424 if method.name == 'QueryInterface':
425 print ' if(*ppvObj == m_pInstance)'
426 print ' *ppvObj = this;'
427 if method.name == 'Release':
428 assert method.type is not Void
430 print ' delete this;'
431 if method.type is not Void:
432 print ' return result;'
438 class Method(Function):
440 def __init__(self, type, name, args):
441 Function.__init__(self, type, name, args)
446 class WrapPointer(Pointer):
448 def __init__(self, type):
449 Pointer.__init__(self, type)
450 if type not in towrap:
453 def wrap_instance(self, instance):
454 print " if(%s)" % instance
455 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
457 def unwrap_instance(self, instance):
458 print " if(%s)" % instance
459 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
465 Type.__init__(self, "char *")
467 def dump(self, instance):
468 print ' Log::DumpString((const char *)%s);' % instance
472 class _WString(Type):
475 Type.__init__(self, "wchar_t *")
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():