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)
242 self.type.dump("*" + instance)
243 except NotImplementedError:
245 print ' Log::EndPointer();'
248 print ' Log::LiteralNull();'
250 def wrap_instance(self, instance):
251 self.type.wrap_instance("*" + instance)
253 def unwrap_instance(self, instance):
254 self.type.wrap_instance("*" + instance)
257 def ConstPointer(type):
258 return Pointer(Const(type))
261 class Enum(Concrete):
263 def __init__(self, name, values):
264 Concrete.__init__(self, name)
267 def visit(self, visitor, *args, **kwargs):
268 return visitor.visit_enum(self, *args, **kwargs)
270 def _dump(self, instance):
271 print ' switch(%s) {' % instance
272 for value in self.values:
273 print ' case %s:' % value
274 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
277 print ' Log::LiteralSInt(%s);' % instance
282 def FakeEnum(type, values):
283 return Enum(type.expr, values)
286 class Bitmask(Concrete):
288 def __init__(self, type, values):
289 Concrete.__init__(self, type.expr)
293 def visit(self, visitor, *args, **kwargs):
294 return visitor.visit_bitmask(self, *args, **kwargs)
296 def _dump(self, instance):
297 print ' %s l_Value = %s;' % (self.type, instance)
298 print ' Log::BeginBitmask("%s");' % (self.type,)
299 for value in self.values:
300 print ' if((l_Value & %s) == %s) {' % (value, value)
301 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
302 print ' l_Value &= ~%s;' % value
304 print ' if(l_Value) {'
305 self.type.dump("l_Value");
307 print ' Log::EndBitmask();'
314 def __init__(self, type, length):
315 Type.__init__(self, type.expr + " *")
319 def visit(self, visitor, *args, **kwargs):
320 return visitor.visit_array(self, *args, **kwargs)
322 def dump(self, instance):
323 index = '__i' + self.type.id
324 print ' Log::BeginArray("%s", %s);' % (self.type, self.length)
325 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
326 print ' Log::BeginElement("%s");' % (self.type,)
327 self.type.dump('(%s)[%s]' % (instance, index))
328 print ' Log::EndElement();'
330 print ' Log::EndArray();'
332 def wrap_instance(self, instance):
333 self.type.wrap_instance("*" + instance)
335 def unwrap_instance(self, instance):
336 self.type.wrap_instance("*" + instance)
341 def __init__(self, type, size):
342 Type.__init__(self, type.expr + ' *')
346 def visit(self, visitor, *args, **kwargs):
347 return visitor.visit_blob(self, *args, **kwargs)
349 def dump(self, instance):
350 print ' Log::LiteralBlob(%s, %s);' % (instance, self.size)
353 class Struct(Concrete):
355 def __init__(self, name, members):
356 Concrete.__init__(self, name)
358 self.members = members
360 def visit(self, visitor, *args, **kwargs):
361 return visitor.visit_struct(self, *args, **kwargs)
363 def _dump(self, instance):
364 print ' Log::BeginStruct("%s");' % (self.name,)
365 for type, name in self.members:
366 print ' Log::BeginMember("%s", "%s");' % (type, name)
367 type.dump('(%s).%s' % (instance, name))
368 print ' Log::EndMember();'
369 print ' Log::EndStruct();'
374 def __init__(self, expr, type):
375 Type.__init__(self, expr)
378 def visit(self, visitor, *args, **kwargs):
379 return visitor.visit_alias(self, *args, **kwargs)
381 def dump(self, instance):
382 self.type.dump(instance)
386 arg = Arg(type, name, output=True)
392 def __init__(self, type, name, output=False):
398 return '%s %s' % (self.type, self.name)
403 def __init__(self, type, name, args, call = '__stdcall', fail = None, sideeffects=True):
409 if isinstance(arg, tuple):
410 arg_type, arg_name = arg
411 arg = Arg(arg_type, arg_name)
412 self.args.append(arg)
416 self.sideeffects = sideeffects
418 def prototype(self, name=None):
425 s = self.call + ' ' + s
426 if name.startswith('*'):
428 s = self.type.expr + ' ' + s
431 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
437 def pointer_type(self):
438 return 'P' + self.name
440 def pointer_value(self):
441 return 'p' + self.name
444 ptype = self.pointer_type()
445 pvalue = self.pointer_value()
446 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
447 print 'static %s %s = NULL;' % (ptype, pvalue)
450 def get_true_pointer(self):
451 raise NotImplementedError
454 print ' Log::Abort();'
457 if self.fail is not None:
458 if self.type is Void:
459 assert self.fail == ''
462 assert self.fail != ''
463 print ' return %s;' % self.fail
468 pvalue = self.pointer_value()
469 print self.prototype() + ' {'
470 if self.type is Void:
473 print ' %s result;' % self.type
475 self.get_true_pointer()
476 print ' Log::BeginCall("%s");' % (self.name)
477 for arg in self.args:
479 arg.type.unwrap_instance(arg.name)
480 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
481 arg.type.dump(arg.name)
482 print ' Log::EndArg();'
483 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in self.args]))
484 for arg in self.args:
486 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
487 arg.type.dump(arg.name)
488 print ' Log::EndArg();'
489 arg.type.wrap_instance(arg.name)
490 if self.type is not Void:
491 print ' Log::BeginReturn("%s");' % self.type
492 self.type.dump("result")
493 print ' Log::EndReturn();'
494 self.type.wrap_instance('result')
495 print ' Log::EndCall();'
496 self.post_call_impl()
497 if self.type is not Void:
498 print ' return result;'
502 def post_call_impl(self):
506 class Interface(Type):
508 def __init__(self, name, base=None):
509 Type.__init__(self, name)
514 def itermethods(self):
515 if self.base is not None:
516 for method in self.base.itermethods():
518 for method in self.methods:
523 return "Wrap" + self.expr
525 def wrap_pre_decl(self):
526 print "class %s;" % self.wrap_name()
529 print "class %s : public %s " % (self.wrap_name(), self.name)
532 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
533 print " virtual ~%s();" % self.wrap_name()
535 for method in self.itermethods():
536 print " " + method.prototype() + ";"
539 print " %s * m_pInstance;" % (self.name,)
544 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
545 print ' m_pInstance = pInstance;'
548 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
551 for method in self.itermethods():
552 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
553 if method.type is Void:
556 print ' %s result;' % method.type
558 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
559 print ' Log::BeginArg("%s *", "this");' % self.name
560 print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
561 print ' Log::EndPointer();'
562 print ' Log::EndArg();'
563 for arg in method.args:
565 arg.type.unwrap_instance(arg.name)
566 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
567 arg.type.dump(arg.name)
568 print ' Log::EndArg();'
569 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
570 for arg in method.args:
572 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
573 arg.type.dump(arg.name)
574 print ' Log::EndArg();'
575 arg.type.wrap_instance(arg.name)
576 if method.type is not Void:
577 print ' Log::BeginReturn("%s");' % method.type
578 method.type.dump("result")
579 print ' Log::EndReturn();'
580 method.type.wrap_instance('result')
581 print ' Log::EndCall();'
582 if method.name == 'QueryInterface':
583 print ' if(*ppvObj == m_pInstance)'
584 print ' *ppvObj = this;'
585 if method.name == 'Release':
586 assert method.type is not Void
588 print ' delete this;'
589 if method.type is not Void:
590 print ' return result;'
596 class Method(Function):
598 def __init__(self, type, name, args):
599 Function.__init__(self, type, name, args, call = '__stdcall')
604 class WrapPointer(Pointer):
606 def __init__(self, type):
607 Pointer.__init__(self, type)
608 if type not in towrap:
611 def wrap_instance(self, instance):
612 print " if(%s)" % instance
613 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
615 def unwrap_instance(self, instance):
616 print " if(%s)" % instance
617 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
623 Type.__init__(self, "char *")
625 def visit(self, visitor, *args, **kwargs):
626 return visitor.visit_string(self, *args, **kwargs)
628 def dump(self, instance):
629 print ' Log::LiteralString((const char *)%s);' % instance
635 '''Opaque pointer.'''
637 def __init__(self, expr):
638 Type.__init__(self, expr)
640 def visit(self, visitor, *args, **kwargs):
641 return visitor.visit_opaque(self, *args, **kwargs)
643 def dump(self, instance):
644 print ' Log::LiteralOpaque((const void *)%s);' % instance
647 def OpaquePointer(type):
648 return Opaque(type.expr + ' *')
651 Bool = Literal("bool", "Bool")
652 SChar = Literal("signed char", "SInt")
653 UChar = Literal("unsigned char", "UInt")
654 Short = Literal("short", "SInt")
655 Int = Literal("int", "SInt")
656 Long = Literal("long", "SInt")
657 LongLong = Literal("long long", "SInt")
658 UShort = Literal("unsigned short", "UInt")
659 UInt = Literal("unsigned int", "UInt")
660 ULong = Literal("unsigned long", "UInt")
661 Float = Literal("float", "Float")
662 Double = Literal("double", "Float")
663 SizeT = Literal("size_t", "UInt")
664 WString = Literal("wchar_t *", "WString")
668 for type in all_types.itervalues():
671 for type in all_types.itervalues():