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 return "const " + str(self.type)
125 def __init__(self, type):
126 Type.__init__(self, 'P' + type.name)
130 return str(self.type) + " *"
132 def dump(self, instance):
133 print ' if(%s) {' % instance
134 print ' Log::BeginReference("%s", %s);' % (self.type, instance)
136 self.type.dump("*" + instance)
137 except NotImplementedError:
139 print ' Log::EndReference();'
142 print ' Log::Text("NULL");'
144 def wrap_instance(self, instance):
145 self.type.wrap_instance("*" + instance)
147 def unwrap_instance(self, instance):
148 self.type.wrap_instance("*" + instance)
151 def ConstPointer(type):
152 return Pointer(Const(type))
155 class OutPointer(Pointer):
161 class Enum(Concrete):
163 def __init__(self, name, values):
164 Concrete.__init__(self, name)
167 def _dump(self, instance):
168 print ' switch(%s) {' % instance
169 for value in self.values:
170 print ' case %s:' % value
171 print ' Log::Text("%s");' % value
174 print ' Log::TextF("%%i", %s);' % instance
179 class FakeEnum(Enum):
183 def __init__(self, type, values):
185 Enum.__init__(self, type.name + str(FakeEnum.__seq), values)
189 return str(self.type)
192 class Flags(Concrete):
196 def __init__(self, type, values):
198 Concrete.__init__(self, type.name + str(Flags.__seq))
203 return str(self.type)
205 def _dump(self, instance):
206 print ' bool l_First = TRUE;'
207 print ' %s l_Value = %s;' % (self.type, instance)
208 for value in self.values:
209 print ' if((l_Value & %s) == %s) {' % (value, value)
210 print ' if(!l_First)'
211 print ' Log::Text(" | ");'
212 print ' Log::Text("%s");' % value
213 print ' l_Value &= ~%s;' % value
214 print ' l_First = FALSE;'
216 print ' if(l_Value || l_First) {'
217 print ' if(!l_First)'
218 print ' Log::Text(" | ");'
219 self.type.dump("l_Value");
223 class Struct(Concrete):
225 def __init__(self, name, members):
226 Concrete.__init__(self, name)
227 self.members = members
229 def _dump(self, instance):
230 for type, name in self.members:
231 print ' Log::BeginElement("%s", "%s");' % (type, name)
232 type.dump('(%s).%s' % (instance, name))
233 print ' Log::EndElement();'
238 def __init__(self, name, type):
239 Type.__init__(self, name)
242 def dump(self, instance):
243 self.type.dump(instance)
248 def __init__(self, type, name, args, call = '__stdcall', fail = None):
255 def prototype(self, name=None):
262 s = self.call + ' ' + s
263 if name.startswith('*'):
265 s = str(self.type) + ' ' + s
268 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
274 def pointer_type(self):
275 return 'P' + self.name
277 def pointer_value(self):
278 return 'p' + self.name
281 ptype = self.pointer_type()
282 pvalue = self.pointer_value()
283 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
284 print 'static %s %s = NULL;' % (ptype, pvalue)
287 def get_true_pointer(self):
288 raise NotImplementedError
291 if self.fail is not None:
292 assert self.type is not Void
293 print ' return %s;' % self.fail
295 print ' ExitProcess(0);'
298 pvalue = self.pointer_value()
299 print self.prototype() + ' {'
300 if self.type is Void:
303 print ' %s result;' % self.type
305 self.get_true_pointer()
306 print ' Log::BeginCall("%s");' % (self.name)
307 for type, name in self.args:
308 if not type.isoutput():
309 type.unwrap_instance(name)
310 print ' Log::BeginArg("%s", "%s");' % (type, name)
312 print ' Log::EndArg();'
313 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
314 for type, name in self.args:
316 print ' Log::BeginArg("%s", "%s");' % (type, name)
318 print ' Log::EndArg();'
319 type.wrap_instance(name)
320 if self.type is not Void:
321 print ' Log::BeginReturn("%s");' % self.type
322 self.type.dump("result")
323 print ' Log::EndReturn();'
324 self.type.wrap_instance('result')
325 print ' Log::EndCall();'
326 if self.type is not Void:
327 print ' return result;'
332 class Interface(Type):
334 def __init__(self, name, base=None):
335 Type.__init__(self, name)
339 def itermethods(self):
340 if self.base is not None:
341 for method in self.base.itermethods():
343 for method in self.methods:
348 return "Wrap" + self.name
350 def wrap_pre_decl(self):
351 print "class %s;" % self.wrap_name()
354 print "class %s : public %s " % (self.wrap_name(), self.name)
357 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
358 print " virtual ~%s();" % self.wrap_name()
360 for method in self.itermethods():
361 print " " + method.prototype() + ";"
364 print " %s * m_pInstance;" % (self.name,)
369 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
370 print ' m_pInstance = pInstance;'
373 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
376 for method in self.itermethods():
377 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
378 if method.type is Void:
381 print ' %s result;' % method.type
383 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
384 print ' Log::BeginArg("%s *", "this");' % self.name
385 print ' Log::BeginReference("%s", m_pInstance);' % self.name
386 print ' Log::EndReference();'
387 print ' Log::EndArg();'
388 for type, name in method.args:
389 if not type.isoutput():
390 type.unwrap_instance(name)
391 print ' Log::BeginArg("%s", "%s");' % (type, name)
393 print ' Log::EndArg();'
394 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
395 for type, name in method.args:
397 print ' Log::BeginArg("%s", "%s");' % (type, name)
399 print ' Log::EndArg();'
400 type.wrap_instance(name)
401 if method.type is not Void:
402 print ' Log::BeginReturn("%s");' % method.type
403 method.type.dump("result")
404 print ' Log::EndReturn();'
405 method.type.wrap_instance('result')
406 print ' Log::EndCall();'
407 if method.name == 'QueryInterface':
408 print ' if(*ppvObj == m_pInstance)'
409 print ' *ppvObj = this;'
410 if method.name == 'Release':
411 assert method.type is not Void
413 print ' delete this;'
414 if method.type is not Void:
415 print ' return result;'
421 class Method(Function):
423 def __init__(self, type, name, args):
424 Function.__init__(self, type, name, args)
429 class WrapPointer(Pointer):
431 def __init__(self, type):
432 Pointer.__init__(self, type)
433 if type not in towrap:
436 def wrap_instance(self, instance):
437 print " if(%s)" % instance
438 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
440 def unwrap_instance(self, instance):
441 print " if(%s)" % instance
442 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
448 Type.__init__(self, "String")
453 def dump(self, instance):
454 print ' Log::DumpString((const char *)%s);' % instance
459 SChar = Intrinsic("signed char", "%i")
460 UChar = Intrinsic("unsigned char", "%u")
461 Short = Intrinsic("short", "%i")
462 Int = Intrinsic("int", "%i")
463 Long = Intrinsic("long", "%li")
464 UShort = Intrinsic("unsigned short", "%u")
465 UInt = Intrinsic("unsigned int", "%u")
466 ULong = Intrinsic("unsigned long", "%lu")
467 Float = Intrinsic("float", "%f")
468 Double = Intrinsic("double", "%f")
472 for type in all_types.itervalues():
475 for type in all_types.itervalues():