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 ##########################################################################/
37 def visit(self, type, *args, **kwargs):
38 return type.visit(self, *args, **kwargs)
42 def visit_void(self, type, *args, **kwargs):
43 raise NotImplementedError
45 def visit_literal(self, type, *args, **kwargs):
46 raise NotImplementedError
48 def visit_string(self, type, *args, **kwargs):
49 raise NotImplementedError
51 def visit_const(self, type, *args, **kwargs):
52 raise NotImplementedError
54 def visit_struct(self, type, *args, **kwargs):
55 raise NotImplementedError
57 def visit_array(self, type, *args, **kwargs):
58 raise NotImplementedError
60 def visit_blob(self, type, *args, **kwargs):
61 raise NotImplementedError
63 def visit_enum(self, type, *args, **kwargs):
64 raise NotImplementedError
66 def visit_bitmask(self, type, *args, **kwargs):
67 raise NotImplementedError
69 def visit_pointer(self, type, *args, **kwargs):
70 raise NotImplementedError
72 def visit_alias(self, type, *args, **kwargs):
73 raise NotImplementedError
75 def visit_opaque(self, type, *args, **kwargs):
76 raise NotImplementedError
79 class Rebuilder(Visitor):
81 def visit_void(self, void):
84 def visit_literal(self, literal):
87 def visit_string(self, string):
90 def visit_const(self, const):
91 return Const(const.type)
93 def visit_struct(self, struct):
94 members = [self.visit(member) for member in struct.members]
95 return Struct(struct.name, members)
97 def visit_array(self, array):
98 type = self.visit(array.type)
99 return Array(type, array.length)
101 def visit_blob(self, blob):
102 type = self.visit(blob.type)
103 return Blob(type, blob.size)
105 def visit_enum(self, enum):
108 def visit_bitmask(self, bitmask):
109 type = self.visit(bitmask.type)
110 return Bitmask(type, bitmask.values)
112 def visit_pointer(self, pointer):
113 type = self.visit(pointer.type)
116 def visit_alias(self, alias):
117 type = self.visit(alias.type)
118 return Alias(alias.expr, type)
120 def visit_opaque(self, opaque):
128 def __init__(self, expr, id = ''):
132 assert char.isalnum() or char in '_ '
134 id = id.replace(' ', '_')
138 id += str(Type.__seq)
140 assert id not in all_types
148 def visit(self, visitor, *args, **kwargs):
149 raise NotImplementedError
157 def dump(self, instance):
158 raise NotImplementedError
160 def wrap_instance(self, instance):
163 def unwrap_instance(self, instance):
170 Type.__init__(self, "void")
172 def visit(self, visitor, *args, **kwargs):
173 return visitor.visit_void(self, *args, **kwargs)
178 class Concrete(Type):
181 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
184 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
189 def _dump(self, instance):
190 raise NotImplementedError
192 def dump(self, instance):
193 print ' Dump%s(%s);' % (self.id, instance)
196 class Literal(Concrete):
198 def __init__(self, expr, format, base=10):
199 Concrete.__init__(self, expr)
202 def visit(self, visitor, *args, **kwargs):
203 return visitor.visit_literal(self, *args, **kwargs)
205 def _dump(self, instance):
206 print ' Log::Literal%s(%s);' % (self.format, instance)
211 def __init__(self, type):
213 if type.expr.startswith("const "):
214 expr = type.expr + " const"
216 expr = "const " + type.expr
218 Type.__init__(self, expr, 'C' + type.id)
222 def visit(self, visitor, *args, **kwargs):
223 return visitor.visit_const(self, *args, **kwargs)
225 def dump(self, instance):
226 self.type.dump(instance)
231 def __init__(self, type):
232 Type.__init__(self, type.expr + " *", 'P' + type.id)
235 def visit(self, visitor, *args, **kwargs):
236 return visitor.visit_pointer(self, *args, **kwargs)
238 def dump(self, instance):
239 print ' if(%s) {' % instance
240 print ' Log::BeginPointer("%s", (const void *)%s);' % (self.type, instance)
241 self.type.dump("*" + instance)
242 print ' Log::EndPointer();'
245 print ' Log::LiteralNull();'
247 def wrap_instance(self, instance):
248 self.type.wrap_instance("*" + instance)
250 def unwrap_instance(self, instance):
251 self.type.wrap_instance("*" + instance)
254 def ConstPointer(type):
255 return Pointer(Const(type))
258 class Enum(Concrete):
260 def __init__(self, name, values):
261 Concrete.__init__(self, name)
264 def visit(self, visitor, *args, **kwargs):
265 return visitor.visit_enum(self, *args, **kwargs)
267 def _dump(self, instance):
268 print ' switch(%s) {' % instance
269 for value in self.values:
270 print ' case %s:' % value
271 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
274 print ' Log::LiteralSInt(%s);' % instance
279 def FakeEnum(type, values):
280 return Enum(type.expr, values)
283 class Bitmask(Concrete):
285 def __init__(self, type, values):
286 Concrete.__init__(self, type.expr)
290 def visit(self, visitor, *args, **kwargs):
291 return visitor.visit_bitmask(self, *args, **kwargs)
293 def _dump(self, instance):
294 print ' %s l_Value = %s;' % (self.type, instance)
295 print ' Log::BeginBitmask("%s");' % (self.type,)
296 for value in self.values:
297 print ' if((l_Value & %s) == %s) {' % (value, value)
298 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
299 print ' l_Value &= ~%s;' % value
301 print ' if(l_Value) {'
302 self.type.dump("l_Value");
304 print ' Log::EndBitmask();'
311 def __init__(self, type, length):
312 Type.__init__(self, type.expr + " *")
316 def visit(self, visitor, *args, **kwargs):
317 return visitor.visit_array(self, *args, **kwargs)
319 def dump(self, instance):
320 print ' if(%s) {' % instance
321 index = '__i' + self.type.id
322 print ' Log::BeginArray("%s", %s);' % (self.type, self.length)
323 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
324 print ' Log::BeginElement("%s");' % (self.type,)
325 self.type.dump('(%s)[%s]' % (instance, index))
326 print ' Log::EndElement();'
328 print ' Log::EndArray();'
331 print ' Log::LiteralNull();'
333 def wrap_instance(self, instance):
334 self.type.wrap_instance("*" + instance)
336 def unwrap_instance(self, instance):
337 self.type.wrap_instance("*" + instance)
342 def __init__(self, type, size):
343 Type.__init__(self, type.expr + ' *')
347 def visit(self, visitor, *args, **kwargs):
348 return visitor.visit_blob(self, *args, **kwargs)
350 def dump(self, instance):
351 print ' Log::LiteralBlob(%s, %s);' % (instance, self.size)
354 class Struct(Concrete):
356 def __init__(self, name, members):
357 Concrete.__init__(self, name)
359 self.members = members
361 def visit(self, visitor, *args, **kwargs):
362 return visitor.visit_struct(self, *args, **kwargs)
364 def _dump(self, instance):
365 print ' Log::BeginStruct("%s");' % (self.name,)
366 for type, name in self.members:
367 print ' Log::BeginMember("%s", "%s");' % (type, name)
368 type.dump('(%s).%s' % (instance, name))
369 print ' Log::EndMember();'
370 print ' Log::EndStruct();'
375 def __init__(self, expr, type):
376 Type.__init__(self, expr)
379 def visit(self, visitor, *args, **kwargs):
380 return visitor.visit_alias(self, *args, **kwargs)
382 def dump(self, instance):
383 self.type.dump(instance)
387 arg = Arg(type, name, output=True)
393 def __init__(self, type, name, output=False):
399 return '%s %s' % (self.type, self.name)
404 def __init__(self, type, name, args, call = '__stdcall', fail = None, sideeffects=True):
410 if isinstance(arg, tuple):
411 arg_type, arg_name = arg
412 arg = Arg(arg_type, arg_name)
413 self.args.append(arg)
417 self.sideeffects = sideeffects
419 def prototype(self, name=None):
426 s = self.call + ' ' + s
427 if name.startswith('*'):
429 s = self.type.expr + ' ' + s
432 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
438 def pointer_type(self):
439 return 'P' + self.name
441 def pointer_value(self):
442 return 'p' + self.name
445 ptype = self.pointer_type()
446 pvalue = self.pointer_value()
447 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
448 print 'static %s %s = NULL;' % (ptype, pvalue)
451 def get_true_pointer(self):
452 raise NotImplementedError
455 print ' Log::Abort();'
458 if self.fail is not None:
459 if self.type is Void:
460 assert self.fail == ''
463 assert self.fail != ''
464 print ' return %s;' % self.fail
469 pvalue = self.pointer_value()
470 print self.prototype() + ' {'
471 if self.type is Void:
474 print ' %s result;' % self.type
476 self.get_true_pointer()
477 print ' Log::BeginCall("%s");' % (self.name)
478 for arg in self.args:
480 arg.type.unwrap_instance(arg.name)
481 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
482 arg.type.dump(arg.name)
483 print ' Log::EndArg();'
484 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in self.args]))
485 for arg in self.args:
487 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
488 arg.type.dump(arg.name)
489 print ' Log::EndArg();'
490 arg.type.wrap_instance(arg.name)
491 if self.type is not Void:
492 print ' Log::BeginReturn("%s");' % self.type
493 self.type.dump("result")
494 print ' Log::EndReturn();'
495 self.type.wrap_instance('result')
496 print ' Log::EndCall();'
497 self.post_call_impl()
498 if self.type is not Void:
499 print ' return result;'
503 def post_call_impl(self):
507 def FunctionPointer(type, name, args, **kwargs):
512 class Interface(Type):
514 def __init__(self, name, base=None):
515 Type.__init__(self, name)
520 def dump(self, instance):
521 print ' Log::LiteralOpaque((const void *)%s);' % instance
523 def itermethods(self):
524 if self.base is not None:
525 for method in self.base.itermethods():
527 for method in self.methods:
532 return "Wrap" + self.expr
534 def wrap_pre_decl(self):
535 print "class %s;" % self.wrap_name()
538 print "class %s : public %s " % (self.wrap_name(), self.name)
541 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
542 print " virtual ~%s();" % self.wrap_name()
544 for method in self.itermethods():
545 print " " + method.prototype() + ";"
548 print " %s * m_pInstance;" % (self.name,)
553 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
554 print ' m_pInstance = pInstance;'
557 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
560 for method in self.itermethods():
561 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
562 if method.type is Void:
565 print ' %s result;' % method.type
567 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
568 print ' Log::BeginArg("%s *", "this");' % self.name
569 print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
570 print ' Log::EndPointer();'
571 print ' Log::EndArg();'
572 for arg in method.args:
574 arg.type.unwrap_instance(arg.name)
575 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
576 arg.type.dump(arg.name)
577 print ' Log::EndArg();'
578 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
579 for arg in method.args:
581 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
582 arg.type.dump(arg.name)
583 print ' Log::EndArg();'
584 arg.type.wrap_instance(arg.name)
585 if method.type is not Void:
586 print ' Log::BeginReturn("%s");' % method.type
587 method.type.dump("result")
588 print ' Log::EndReturn();'
589 method.type.wrap_instance('result')
590 print ' Log::EndCall();'
591 if method.name == 'QueryInterface':
592 print ' if(*ppvObj == m_pInstance)'
593 print ' *ppvObj = this;'
594 if method.name == 'Release':
595 assert method.type is not Void
597 print ' delete this;'
598 if method.type is not Void:
599 print ' return result;'
605 class Method(Function):
607 def __init__(self, type, name, args):
608 Function.__init__(self, type, name, args, call = '__stdcall')
613 class WrapPointer(Pointer):
615 def __init__(self, type):
616 Pointer.__init__(self, type)
617 if type not in towrap:
620 def wrap_instance(self, instance):
621 print " if(%s)" % instance
622 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
624 def unwrap_instance(self, instance):
625 print " if(%s)" % instance
626 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
632 Type.__init__(self, "char *")
634 def visit(self, visitor, *args, **kwargs):
635 return visitor.visit_string(self, *args, **kwargs)
637 def dump(self, instance):
638 print ' Log::LiteralString((const char *)%s);' % instance
644 '''Opaque pointer.'''
646 def __init__(self, expr):
647 Type.__init__(self, expr)
649 def visit(self, visitor, *args, **kwargs):
650 return visitor.visit_opaque(self, *args, **kwargs)
652 def dump(self, instance):
653 print ' Log::LiteralOpaque((const void *)%s);' % instance
656 def OpaquePointer(type):
657 return Opaque(type.expr + ' *')
660 Bool = Literal("bool", "Bool")
661 SChar = Literal("signed char", "SInt")
662 UChar = Literal("unsigned char", "UInt")
663 Short = Literal("short", "SInt")
664 Int = Literal("int", "SInt")
665 Long = Literal("long", "SInt")
666 LongLong = Literal("long long", "SInt")
667 UShort = Literal("unsigned short", "UInt")
668 UInt = Literal("unsigned int", "UInt")
669 ULong = Literal("unsigned long", "UInt")
670 ULongLong = Literal("unsigned long long", "UInt")
671 Float = Literal("float", "Float")
672 Double = Literal("double", "Float")
673 SizeT = Literal("size_t", "UInt")
674 WString = Literal("wchar_t *", "WString")
678 for type in all_types.itervalues():
681 for type in all_types.itervalues():