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
153 def dump(self, instance):
154 raise NotImplementedError
156 def wrap_instance(self, instance):
159 def unwrap_instance(self, instance):
166 Type.__init__(self, "void")
168 def visit(self, visitor, *args, **kwargs):
169 return visitor.visit_void(self, *args, **kwargs)
174 class Concrete(Type):
177 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
180 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
185 def _dump(self, instance):
186 raise NotImplementedError
188 def dump(self, instance):
189 print ' Dump%s(%s);' % (self.id, instance)
192 class Literal(Concrete):
194 def __init__(self, expr, format, base=10):
195 Concrete.__init__(self, expr)
198 def visit(self, visitor, *args, **kwargs):
199 return visitor.visit_literal(self, *args, **kwargs)
201 def _dump(self, instance):
202 print ' Log::Literal%s(%s);' % (self.format, instance)
207 def __init__(self, type):
209 if type.expr.startswith("const "):
210 expr = type.expr + " const"
212 expr = "const " + type.expr
214 Type.__init__(self, expr, 'C' + type.id)
218 def visit(self, visitor, *args, **kwargs):
219 return visitor.visit_const(self, *args, **kwargs)
221 def dump(self, instance):
222 self.type.dump(instance)
227 def __init__(self, type):
228 Type.__init__(self, type.expr + " *", 'P' + type.id)
231 def visit(self, visitor, *args, **kwargs):
232 return visitor.visit_pointer(self, *args, **kwargs)
234 def dump(self, instance):
235 print ' if(%s) {' % instance
236 print ' Log::BeginPointer("%s", (const void *)%s);' % (self.type, instance)
238 self.type.dump("*" + instance)
239 except NotImplementedError:
241 print ' Log::EndPointer();'
244 print ' Log::LiteralNull();'
246 def wrap_instance(self, instance):
247 self.type.wrap_instance("*" + instance)
249 def unwrap_instance(self, instance):
250 self.type.wrap_instance("*" + instance)
253 def ConstPointer(type):
254 return Pointer(Const(type))
257 def OutPointer(type):
258 return Out(Pointer(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)
339 def OutArray(type, length):
340 return Out(Array(type, length))
345 def __init__(self, type, size):
346 Type.__init__(self, type.expr + ' *')
350 def visit(self, visitor, *args, **kwargs):
351 return visitor.visit_blob(self, *args, **kwargs)
353 def dump(self, instance):
354 print ' Log::LiteralBlob(%s, %s);' % (instance, self.size)
357 class Struct(Concrete):
359 def __init__(self, name, members):
360 Concrete.__init__(self, name)
362 self.members = members
364 def visit(self, visitor, *args, **kwargs):
365 return visitor.visit_struct(self, *args, **kwargs)
367 def _dump(self, instance):
368 print ' Log::BeginStruct("%s");' % (self.name,)
369 for type, name in self.members:
370 print ' Log::BeginMember("%s", "%s");' % (type, name)
371 type.dump('(%s).%s' % (instance, name))
372 print ' Log::EndMember();'
373 print ' Log::EndStruct();'
378 def __init__(self, expr, type):
379 Type.__init__(self, expr)
382 def visit(self, visitor, *args, **kwargs):
383 return visitor.visit_alias(self, *args, **kwargs)
385 def dump(self, instance):
386 self.type.dump(instance)
396 def __init__(self, type, name, args, call = '__stdcall', fail = None):
403 def prototype(self, name=None):
410 s = self.call + ' ' + s
411 if name.startswith('*'):
413 s = self.type.expr + ' ' + s
416 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
422 def pointer_type(self):
423 return 'P' + self.name
425 def pointer_value(self):
426 return 'p' + self.name
429 ptype = self.pointer_type()
430 pvalue = self.pointer_value()
431 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
432 print 'static %s %s = NULL;' % (ptype, pvalue)
435 def get_true_pointer(self):
436 raise NotImplementedError
439 print ' ExitProcess(0);'
442 if self.fail is not None:
443 if self.type is Void:
444 assert self.fail == ''
447 assert self.fail != ''
448 print ' return %s;' % self.fail
453 pvalue = self.pointer_value()
454 print self.prototype() + ' {'
455 if self.type is Void:
458 print ' %s result;' % self.type
460 self.get_true_pointer()
461 print ' Log::BeginCall("%s");' % (self.name)
462 for type, name in self.args:
463 if not type.isoutput:
464 type.unwrap_instance(name)
465 print ' Log::BeginArg("%s", "%s");' % (type, name)
467 print ' Log::EndArg();'
468 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
469 for type, name in self.args:
471 print ' Log::BeginArg("%s", "%s");' % (type, name)
473 print ' Log::EndArg();'
474 type.wrap_instance(name)
475 if self.type is not Void:
476 print ' Log::BeginReturn("%s");' % self.type
477 self.type.dump("result")
478 print ' Log::EndReturn();'
479 self.type.wrap_instance('result')
480 print ' Log::EndCall();'
481 self.post_call_impl()
482 if self.type is not Void:
483 print ' return result;'
487 def post_call_impl(self):
491 class Interface(Type):
493 def __init__(self, name, base=None):
494 Type.__init__(self, name)
499 def itermethods(self):
500 if self.base is not None:
501 for method in self.base.itermethods():
503 for method in self.methods:
508 return "Wrap" + self.expr
510 def wrap_pre_decl(self):
511 print "class %s;" % self.wrap_name()
514 print "class %s : public %s " % (self.wrap_name(), self.name)
517 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
518 print " virtual ~%s();" % self.wrap_name()
520 for method in self.itermethods():
521 print " " + method.prototype() + ";"
524 print " %s * m_pInstance;" % (self.name,)
529 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
530 print ' m_pInstance = pInstance;'
533 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
536 for method in self.itermethods():
537 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
538 if method.type is Void:
541 print ' %s result;' % method.type
543 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
544 print ' Log::BeginArg("%s *", "this");' % self.name
545 print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
546 print ' Log::EndPointer();'
547 print ' Log::EndArg();'
548 for type, name in method.args:
549 if not type.isoutput:
550 type.unwrap_instance(name)
551 print ' Log::BeginArg("%s", "%s");' % (type, name)
553 print ' Log::EndArg();'
554 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
555 for type, name in method.args:
557 print ' Log::BeginArg("%s", "%s");' % (type, name)
559 print ' Log::EndArg();'
560 type.wrap_instance(name)
561 if method.type is not Void:
562 print ' Log::BeginReturn("%s");' % method.type
563 method.type.dump("result")
564 print ' Log::EndReturn();'
565 method.type.wrap_instance('result')
566 print ' Log::EndCall();'
567 if method.name == 'QueryInterface':
568 print ' if(*ppvObj == m_pInstance)'
569 print ' *ppvObj = this;'
570 if method.name == 'Release':
571 assert method.type is not Void
573 print ' delete this;'
574 if method.type is not Void:
575 print ' return result;'
581 class Method(Function):
583 def __init__(self, type, name, args):
584 Function.__init__(self, type, name, args, call = '__stdcall')
589 class WrapPointer(Pointer):
591 def __init__(self, type):
592 Pointer.__init__(self, type)
593 if type not in towrap:
596 def wrap_instance(self, instance):
597 print " if(%s)" % instance
598 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
600 def unwrap_instance(self, instance):
601 print " if(%s)" % instance
602 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
608 Type.__init__(self, "char *")
610 def visit(self, visitor, *args, **kwargs):
611 return visitor.visit_literal(self, *args, **kwargs)
613 def dump(self, instance):
614 print ' Log::LiteralString((const char *)%s);' % instance
620 '''Opaque pointer.'''
622 def __init__(self, expr):
623 Type.__init__(self, expr)
625 def visit(self, visitor, *args, **kwargs):
626 return visitor.visit_opaque(self, *args, **kwargs)
628 def dump(self, instance):
629 print ' Log::LiteralOpaque((const void *)%s);' % instance
632 def OpaquePointer(type):
633 return Opaque(type.expr + ' *')
636 Bool = Literal("bool", "Bool")
637 SChar = Literal("signed char", "SInt")
638 UChar = Literal("unsigned char", "UInt")
639 Short = Literal("short", "SInt")
640 Int = Literal("int", "SInt")
641 Long = Literal("long", "SInt")
642 LongLong = Literal("long long", "SInt")
643 UShort = Literal("unsigned short", "UInt")
644 UInt = Literal("unsigned int", "UInt")
645 ULong = Literal("unsigned long", "UInt")
646 Float = Literal("float", "Float")
647 Double = Literal("double", "Float")
648 SizeT = Literal("size_t", "UInt")
649 WString = Literal("wchar_t *", "WString")
653 for type in all_types.itervalues():
656 for type in all_types.itervalues():