1 ##########################################################################
3 # Copyright 2008-2009 VMware, Inc.
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 ##########################################################################/
38 def __init__(self, expr, id = ''):
42 assert char.isalnum() or char in '_ '
44 id = id.replace(' ', '_')
50 assert id not in all_types
67 def dump(self, instance):
68 raise NotImplementedError
70 def wrap_instance(self, instance):
73 def unwrap_instance(self, instance):
80 Type.__init__(self, "void")
88 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
91 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
96 def _dump(self, instance):
97 raise NotImplementedError
99 def dump(self, instance):
100 print ' Dump%s(%s);' % (self.id, instance)
103 class Intrinsic(Concrete):
105 def __init__(self, expr, format):
106 Concrete.__init__(self, expr)
109 def _dump(self, instance):
110 print ' Log::TextF("%s", %s);' % (self.format, instance)
115 def __init__(self, type):
117 if isinstance(type, Pointer):
118 expr = type.expr + " const"
120 expr = "const " + type.expr
122 Type.__init__(self, expr, 'C' + type.id)
126 def dump(self, instance):
127 self.type.dump(instance)
132 def __init__(self, type):
133 Type.__init__(self, type.expr + " *", 'P' + type.id)
136 def dump(self, instance):
137 print ' if(%s) {' % instance
138 print ' Log::BeginReference("%s", %s);' % (self.type, instance)
140 self.type.dump("*" + instance)
141 except NotImplementedError:
143 print ' Log::EndReference();'
146 print ' Log::Text("NULL");'
148 def wrap_instance(self, instance):
149 self.type.wrap_instance("*" + instance)
151 def unwrap_instance(self, instance):
152 self.type.wrap_instance("*" + instance)
155 def ConstPointer(type):
156 return Pointer(Const(type))
159 class OutPointer(Pointer):
165 class Enum(Concrete):
167 def __init__(self, name, values):
168 Concrete.__init__(self, name)
171 def _dump(self, instance):
172 print ' switch(%s) {' % instance
173 for value in self.values:
174 print ' case %s:' % value
175 print ' Log::Text("%s");' % value
178 print ' Log::TextF("%%i", %s);' % instance
183 class FakeEnum(Enum):
185 def __init__(self, type, values):
186 Enum.__init__(self, type.expr, values)
190 class Flags(Concrete):
192 def __init__(self, type, values):
193 Concrete.__init__(self, type.expr)
197 def _dump(self, instance):
198 print ' bool l_First = TRUE;'
199 print ' %s l_Value = %s;' % (self.type, instance)
200 for value in self.values:
201 print ' if((l_Value & %s) == %s) {' % (value, value)
202 print ' if(!l_First)'
203 print ' Log::Text(" | ");'
204 print ' Log::Text("%s");' % value
205 print ' l_Value &= ~%s;' % value
206 print ' l_First = FALSE;'
208 print ' if(l_Value || l_First) {'
209 print ' if(!l_First)'
210 print ' Log::Text(" | ");'
211 self.type.dump("l_Value");
217 def __init__(self, type, length):
218 Type.__init__(self, type.expr + " *", 'P' + type.id)
222 def dump(self, instance):
223 index = '__i' + self.type.id
224 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
225 print ' Log::BeginElement("%s");' % (self.type,)
226 self.type.dump('(%s)[%s]' % (instance, index))
227 print ' Log::EndElement();'
230 def wrap_instance(self, instance):
231 self.type.wrap_instance("*" + instance)
233 def unwrap_instance(self, instance):
234 self.type.wrap_instance("*" + instance)
237 class OutArray(Array):
243 class Struct(Concrete):
245 def __init__(self, name, members):
246 Concrete.__init__(self, name)
247 self.members = members
249 def _dump(self, instance):
250 for type, name in self.members:
251 print ' Log::BeginElement("%s", "%s");' % (type, name)
252 type.dump('(%s).%s' % (instance, name))
253 print ' Log::EndElement();'
258 def __init__(self, name, type):
259 Type.__init__(self, name)
262 def dump(self, instance):
263 self.type.dump(instance)
268 def __init__(self, type):
269 Type.__init__(self, type.expr)
281 def dump(self, instance):
282 self.type.dump(instance)
284 def wrap_instance(self, instance):
285 self.type.wrap_instance(instance)
287 def unwrap_instance(self, instance):
288 self.type.unwrap_instance(instance)
293 def __init__(self, type, name, args, call = '__stdcall', fail = None):
300 def prototype(self, name=None):
307 s = self.call + ' ' + s
308 if name.startswith('*'):
310 s = self.type.expr + ' ' + s
313 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
319 def pointer_type(self):
320 return 'P' + self.name
322 def pointer_value(self):
323 return 'p' + self.name
326 ptype = self.pointer_type()
327 pvalue = self.pointer_value()
328 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
329 print 'static %s %s = NULL;' % (ptype, pvalue)
332 def get_true_pointer(self):
333 raise NotImplementedError
336 if self.fail is not None:
337 if self.type is Void:
338 assert self.fail == ''
341 assert self.fail != ''
342 print ' return %s;' % self.fail
344 print ' ExitProcess(0);'
347 pvalue = self.pointer_value()
348 print self.prototype() + ' {'
349 if self.type is Void:
352 print ' %s result;' % self.type
354 self.get_true_pointer()
355 print ' Log::BeginCall("%s");' % (self.name)
356 for type, name in self.args:
357 if not type.isoutput():
358 type.unwrap_instance(name)
359 print ' Log::BeginArg("%s", "%s");' % (type, name)
361 print ' Log::EndArg();'
362 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
363 for type, name in self.args:
365 print ' Log::BeginArg("%s", "%s");' % (type, name)
367 print ' Log::EndArg();'
368 type.wrap_instance(name)
369 if self.type is not Void:
370 print ' Log::BeginReturn("%s");' % self.type
371 self.type.dump("result")
372 print ' Log::EndReturn();'
373 self.type.wrap_instance('result')
374 print ' Log::EndCall();'
375 self.post_call_impl()
376 if self.type is not Void:
377 print ' return result;'
381 def post_call_impl(self):
385 class Interface(Type):
387 def __init__(self, name, base=None):
388 Type.__init__(self, name)
393 def itermethods(self):
394 if self.base is not None:
395 for method in self.base.itermethods():
397 for method in self.methods:
402 return "Wrap" + self.expr
404 def wrap_pre_decl(self):
405 print "class %s;" % self.wrap_name()
408 print "class %s : public %s " % (self.wrap_name(), self.name)
411 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
412 print " virtual ~%s();" % self.wrap_name()
414 for method in self.itermethods():
415 print " " + method.prototype() + ";"
418 print " %s * m_pInstance;" % (self.name,)
423 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
424 print ' m_pInstance = pInstance;'
427 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
430 for method in self.itermethods():
431 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
432 if method.type is Void:
435 print ' %s result;' % method.type
437 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
438 print ' Log::BeginArg("%s *", "this");' % self.name
439 print ' Log::BeginReference("%s", m_pInstance);' % self.name
440 print ' Log::EndReference();'
441 print ' Log::EndArg();'
442 for type, name in method.args:
443 if not type.isoutput():
444 type.unwrap_instance(name)
445 print ' Log::BeginArg("%s", "%s");' % (type, name)
447 print ' Log::EndArg();'
448 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
449 for type, name in method.args:
451 print ' Log::BeginArg("%s", "%s");' % (type, name)
453 print ' Log::EndArg();'
454 type.wrap_instance(name)
455 if method.type is not Void:
456 print ' Log::BeginReturn("%s");' % method.type
457 method.type.dump("result")
458 print ' Log::EndReturn();'
459 method.type.wrap_instance('result')
460 print ' Log::EndCall();'
461 if method.name == 'QueryInterface':
462 print ' if(*ppvObj == m_pInstance)'
463 print ' *ppvObj = this;'
464 if method.name == 'Release':
465 assert method.type is not Void
467 print ' delete this;'
468 if method.type is not Void:
469 print ' return result;'
475 class Method(Function):
477 def __init__(self, type, name, args):
478 Function.__init__(self, type, name, args)
483 class WrapPointer(Pointer):
485 def __init__(self, type):
486 Pointer.__init__(self, type)
487 if type not in towrap:
490 def wrap_instance(self, instance):
491 print " if(%s)" % instance
492 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
494 def unwrap_instance(self, instance):
495 print " if(%s)" % instance
496 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
502 Type.__init__(self, "char *")
504 def dump(self, instance):
505 print ' Log::DumpString((const char *)%s);' % instance
509 class _WString(Type):
512 Type.__init__(self, "wchar_t *")
514 def dump(self, instance):
515 print ' Log::DumpWString(%s);' % instance
520 SChar = Intrinsic("signed char", "%i")
521 UChar = Intrinsic("unsigned char", "%u")
522 Short = Intrinsic("short", "%i")
523 Int = Intrinsic("int", "%i")
524 Long = Intrinsic("long", "%li")
525 UShort = Intrinsic("unsigned short", "%u")
526 UInt = Intrinsic("unsigned int", "%u")
527 ULong = Intrinsic("unsigned long", "%lu")
528 Float = Intrinsic("float", "%f")
529 Double = Intrinsic("double", "%f")
530 SizeT = Intrinsic("size_t", "%lu")
534 for type in all_types.itervalues():
537 for type in all_types.itervalues():