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_const(self, type, *args, **kwargs):
49 raise NotImplementedError
51 def visit_struct(self, type, *args, **kwargs):
52 raise NotImplementedError
54 def visit_array(self, type, *args, **kwargs):
55 raise NotImplementedError
57 def visit_blob(self, type, *args, **kwargs):
58 raise NotImplementedError
60 def visit_enum(self, type, *args, **kwargs):
61 raise NotImplementedError
63 def visit_bitmask(self, type, *args, **kwargs):
64 raise NotImplementedError
66 def visit_pointer(self, type, *args, **kwargs):
67 raise NotImplementedError
69 def visit_alias(self, type, *args, **kwargs):
70 raise NotImplementedError
72 def visit_opaque(self, type, *args, **kwargs):
73 raise NotImplementedError
76 class Rebuilder(Visitor):
78 def visit_void(self, void):
81 def visit_literal(self, literal):
84 def visit_const(self, const):
85 return Const(const.type)
87 def visit_struct(self, struct):
88 members = [self.visit(member) for member in struct.members]
89 return Struct(struct.name, members)
91 def visit_array(self, array):
92 type = self.visit(array.type)
93 return Array(type, array.length)
95 def visit_blob(self, blob):
96 type = self.visit(blob.type)
97 return Blob(type, blob.size)
99 def visit_enum(self, enum):
102 def visit_bitmask(self, bitmask):
103 type = self.visit(bitmask.type)
104 return Bitmask(type, bitmask.values)
106 def visit_pointer(self, pointer):
107 type = self.visit(pointer.type)
110 def visit_alias(self, alias):
111 type = self.visit(alias.type)
112 return Alias(alias.expr, type)
114 def visit_opaque(self, opaque):
122 def __init__(self, expr, id = ''):
126 assert char.isalnum() or char in '_ '
128 id = id.replace(' ', '_')
132 id += str(Type.__seq)
134 assert id not in all_types
142 def visit(self, visitor, *args, **kwargs):
143 raise NotImplementedError
154 def dump(self, instance):
155 raise NotImplementedError
157 def wrap_instance(self, instance):
160 def unwrap_instance(self, instance):
167 Type.__init__(self, "void")
169 def visit(self, visitor, *args, **kwargs):
170 return visitor.visit_void(self, *args, **kwargs)
175 class Concrete(Type):
178 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
181 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
186 def _dump(self, instance):
187 raise NotImplementedError
189 def dump(self, instance):
190 print ' Dump%s(%s);' % (self.id, instance)
193 class Literal(Concrete):
195 def __init__(self, expr, format, base=10):
196 Concrete.__init__(self, expr)
199 def visit(self, visitor, *args, **kwargs):
200 return visitor.visit_literal(self, *args, **kwargs)
202 def _dump(self, instance):
203 print ' Log::Literal%s(%s);' % (self.format, instance)
208 def __init__(self, type):
210 if isinstance(type, Pointer):
211 expr = type.expr + " const"
213 expr = "const " + type.expr
215 Type.__init__(self, expr, 'C' + type.id)
219 def visit(self, visitor, *args, **kwargs):
220 return visitor.visit_const(self, *args, **kwargs)
222 def dump(self, instance):
223 self.type.dump(instance)
228 def __init__(self, type):
229 Type.__init__(self, type.expr + " *", 'P' + type.id)
232 def visit(self, visitor, *args, **kwargs):
233 return visitor.visit_pointer(self, *args, **kwargs)
235 def dump(self, instance):
236 print ' if(%s) {' % instance
237 print ' Log::BeginPointer("%s", (const void *)%s);' % (self.type, instance)
239 self.type.dump("*" + instance)
240 except NotImplementedError:
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 OutPointer(Pointer):
264 class Enum(Concrete):
266 def __init__(self, name, values):
267 Concrete.__init__(self, name)
270 def visit(self, visitor, *args, **kwargs):
271 return visitor.visit_enum(self, *args, **kwargs)
273 def _dump(self, instance):
274 print ' switch(%s) {' % instance
275 for value in self.values:
276 print ' case %s:' % value
277 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
280 print ' Log::LiteralSInt(%s);' % instance
285 def FakeEnum(type, values):
286 return Enum(type.expr, values)
289 class Bitmask(Concrete):
291 def __init__(self, type, values):
292 Concrete.__init__(self, type.expr)
296 def visit(self, visitor, *args, **kwargs):
297 return visitor.visit_bitmask(self, *args, **kwargs)
299 def _dump(self, instance):
300 print ' %s l_Value = %s;' % (self.type, instance)
301 print ' Log::BeginBitmask("%s");' % (self.type,)
302 for value in self.values:
303 print ' if((l_Value & %s) == %s) {' % (value, value)
304 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
305 print ' l_Value &= ~%s;' % value
307 print ' if(l_Value) {'
308 self.type.dump("l_Value");
310 print ' Log::EndBitmask();'
317 def __init__(self, type, length):
318 Type.__init__(self, type.expr + " *")
322 def visit(self, visitor, *args, **kwargs):
323 return visitor.visit_array(self, *args, **kwargs)
325 def dump(self, instance):
326 index = '__i' + self.type.id
327 print ' Log::BeginArray("%s", %s);' % (self.type, self.length)
328 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
329 print ' Log::BeginElement("%s");' % (self.type,)
330 self.type.dump('(%s)[%s]' % (instance, index))
331 print ' Log::EndElement();'
333 print ' Log::EndArray();'
335 def wrap_instance(self, instance):
336 self.type.wrap_instance("*" + instance)
338 def unwrap_instance(self, instance):
339 self.type.wrap_instance("*" + instance)
342 class OutArray(Array):
350 def __init__(self, type, size):
351 Type.__init__(self, type.expr + ' *')
355 def visit(self, visitor, *args, **kwargs):
356 return visitor.visit_blob(self, *args, **kwargs)
358 def dump(self, instance):
359 print ' Log::LiteralBlob(%s, %s);' % (instance, self.size)
362 class Struct(Concrete):
364 def __init__(self, name, members):
365 Concrete.__init__(self, name)
367 self.members = members
369 def visit(self, visitor, *args, **kwargs):
370 return visitor.visit_struct(self, *args, **kwargs)
372 def _dump(self, instance):
373 print ' Log::BeginStruct("%s");' % (self.name,)
374 for type, name in self.members:
375 print ' Log::BeginMember("%s", "%s");' % (type, name)
376 type.dump('(%s).%s' % (instance, name))
377 print ' Log::EndMember();'
378 print ' Log::EndStruct();'
383 def __init__(self, expr, type):
384 Type.__init__(self, expr)
387 def visit(self, visitor, *args, **kwargs):
388 return visitor.visit_alias(self, *args, **kwargs)
390 def dump(self, instance):
391 self.type.dump(instance)
396 def __init__(self, type):
397 Type.__init__(self, type.expr)
409 def dump(self, instance):
410 self.type.dump(instance)
412 def wrap_instance(self, instance):
413 self.type.wrap_instance(instance)
415 def unwrap_instance(self, instance):
416 self.type.unwrap_instance(instance)
421 def __init__(self, type, name, args, call = '__stdcall', fail = None):
428 def prototype(self, name=None):
435 s = self.call + ' ' + s
436 if name.startswith('*'):
438 s = self.type.expr + ' ' + s
441 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
447 def pointer_type(self):
448 return 'P' + self.name
450 def pointer_value(self):
451 return 'p' + self.name
454 ptype = self.pointer_type()
455 pvalue = self.pointer_value()
456 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
457 print 'static %s %s = NULL;' % (ptype, pvalue)
460 def get_true_pointer(self):
461 raise NotImplementedError
464 print ' ExitProcess(0);'
467 if self.fail is not None:
468 if self.type is Void:
469 assert self.fail == ''
472 assert self.fail != ''
473 print ' return %s;' % self.fail
478 pvalue = self.pointer_value()
479 print self.prototype() + ' {'
480 if self.type is Void:
483 print ' %s result;' % self.type
485 self.get_true_pointer()
486 print ' Log::BeginCall("%s");' % (self.name)
487 for type, name in self.args:
488 if not type.isoutput():
489 type.unwrap_instance(name)
490 print ' Log::BeginArg("%s", "%s");' % (type, name)
492 print ' Log::EndArg();'
493 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
494 for type, name in self.args:
496 print ' Log::BeginArg("%s", "%s");' % (type, name)
498 print ' Log::EndArg();'
499 type.wrap_instance(name)
500 if self.type is not Void:
501 print ' Log::BeginReturn("%s");' % self.type
502 self.type.dump("result")
503 print ' Log::EndReturn();'
504 self.type.wrap_instance('result')
505 print ' Log::EndCall();'
506 self.post_call_impl()
507 if self.type is not Void:
508 print ' return result;'
512 def post_call_impl(self):
516 class Interface(Type):
518 def __init__(self, name, base=None):
519 Type.__init__(self, name)
524 def itermethods(self):
525 if self.base is not None:
526 for method in self.base.itermethods():
528 for method in self.methods:
533 return "Wrap" + self.expr
535 def wrap_pre_decl(self):
536 print "class %s;" % self.wrap_name()
539 print "class %s : public %s " % (self.wrap_name(), self.name)
542 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
543 print " virtual ~%s();" % self.wrap_name()
545 for method in self.itermethods():
546 print " " + method.prototype() + ";"
549 print " %s * m_pInstance;" % (self.name,)
554 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
555 print ' m_pInstance = pInstance;'
558 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
561 for method in self.itermethods():
562 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
563 if method.type is Void:
566 print ' %s result;' % method.type
568 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
569 print ' Log::BeginArg("%s *", "this");' % self.name
570 print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
571 print ' Log::EndPointer();'
572 print ' Log::EndArg();'
573 for type, name in method.args:
574 if not type.isoutput():
575 type.unwrap_instance(name)
576 print ' Log::BeginArg("%s", "%s");' % (type, name)
578 print ' Log::EndArg();'
579 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
580 for type, name in method.args:
582 print ' Log::BeginArg("%s", "%s");' % (type, name)
584 print ' Log::EndArg();'
585 type.wrap_instance(name)
586 if method.type is not Void:
587 print ' Log::BeginReturn("%s");' % method.type
588 method.type.dump("result")
589 print ' Log::EndReturn();'
590 method.type.wrap_instance('result')
591 print ' Log::EndCall();'
592 if method.name == 'QueryInterface':
593 print ' if(*ppvObj == m_pInstance)'
594 print ' *ppvObj = this;'
595 if method.name == 'Release':
596 assert method.type is not Void
598 print ' delete this;'
599 if method.type is not Void:
600 print ' return result;'
606 class Method(Function):
608 def __init__(self, type, name, args):
609 Function.__init__(self, type, name, args, call = '__stdcall')
614 class WrapPointer(Pointer):
616 def __init__(self, type):
617 Pointer.__init__(self, type)
618 if type not in towrap:
621 def wrap_instance(self, instance):
622 print " if(%s)" % instance
623 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
625 def unwrap_instance(self, instance):
626 print " if(%s)" % instance
627 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
633 Type.__init__(self, "char *")
635 def visit(self, visitor, *args, **kwargs):
636 return visitor.visit_literal(self, *args, **kwargs)
638 def dump(self, instance):
639 print ' Log::LiteralString((const char *)%s);' % instance
647 Type.__init__(self, "void")
649 def visit(self, visitor, *args, **kwargs):
650 return visitor.visit_opaque(self, *args, **kwargs)
652 def dump(self, instance):
653 print ' Log::LiteralOpaque();'
655 Opaque = Pointer(_Opaque())
658 Bool = Literal("bool", "Bool")
659 SChar = Literal("signed char", "SInt")
660 UChar = Literal("unsigned char", "UInt")
661 Short = Literal("short", "SInt")
662 Int = Literal("int", "SInt")
663 Long = Literal("long", "SInt")
664 LongLong = Literal("long long", "SInt")
665 UShort = Literal("unsigned short", "UInt")
666 UInt = Literal("unsigned int", "UInt")
667 ULong = Literal("unsigned long", "UInt")
668 Float = Literal("float", "Float")
669 Double = Literal("double", "Float")
670 SizeT = Literal("size_t", "UInt")
671 WString = Literal("wchar_t *", "WString")
675 for type in all_types.itervalues():
678 for type in all_types.itervalues():