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_enum(self, type, *args, **kwargs):
58 raise NotImplementedError
60 def visit_bitmask(self, type, *args, **kwargs):
61 raise NotImplementedError
63 def visit_pointer(self, type, *args, **kwargs):
64 raise NotImplementedError
66 def visit_alias(self, type, *args, **kwargs):
67 raise NotImplementedError
69 def visit_opaque(self, type, *args, **kwargs):
70 raise NotImplementedError
73 class Rebuilder(Visitor):
75 def visit_void(self, void):
78 def visit_literal(self, literal):
81 def visit_const(self, const):
82 return Const(const.type)
84 def visit_struct(self, struct):
85 members = [self.visit(member) for member in struct.members]
86 return Struct(struct.name, members)
88 def visit_array(self, array):
89 type = self.visit(array.type)
90 return Array(type, array.length)
92 def visit_enum(self, enum):
95 def visit_bitmask(self, bitmask):
96 type = self.visit(bitmask.type)
97 return Bitmask(type, bitmask.values)
99 def visit_pointer(self, pointer):
100 type = self.visit(pointer.type)
103 def visit_alias(self, alias):
104 type = self.visit(alias.type)
105 return Alias(alias.expr, type)
107 def visit_opaque(self, opaque):
115 def __init__(self, expr, id = ''):
119 assert char.isalnum() or char in '_ '
121 id = id.replace(' ', '_')
125 id += str(Type.__seq)
127 assert id not in all_types
135 def visit(self, visitor, *args, **kwargs):
136 raise NotImplementedError
147 def dump(self, instance):
148 raise NotImplementedError
150 def wrap_instance(self, instance):
153 def unwrap_instance(self, instance):
160 Type.__init__(self, "void")
162 def visit(self, visitor, *args, **kwargs):
163 return visitor.visit_void(self, *args, **kwargs)
168 class Concrete(Type):
171 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
174 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
179 def _dump(self, instance):
180 raise NotImplementedError
182 def dump(self, instance):
183 print ' Dump%s(%s);' % (self.id, instance)
186 class Literal(Concrete):
188 def __init__(self, expr, format, base=10):
189 Concrete.__init__(self, expr)
192 def visit(self, visitor, *args, **kwargs):
193 return visitor.visit_literal(self, *args, **kwargs)
195 def _dump(self, instance):
196 print ' Log::Literal%s(%s);' % (self.format, instance)
201 def __init__(self, type):
203 if isinstance(type, Pointer):
204 expr = type.expr + " const"
206 expr = "const " + type.expr
208 Type.__init__(self, expr, 'C' + type.id)
212 def visit(self, visitor, *args, **kwargs):
213 return visitor.visit_const(self, *args, **kwargs)
215 def dump(self, instance):
216 self.type.dump(instance)
221 def __init__(self, type):
222 Type.__init__(self, type.expr + " *", 'P' + type.id)
225 def visit(self, visitor, *args, **kwargs):
226 return visitor.visit_pointer(self, *args, **kwargs)
228 def dump(self, instance):
229 print ' if(%s) {' % instance
230 print ' Log::BeginPointer("%s", (const void *)%s);' % (self.type, instance)
232 self.type.dump("*" + instance)
233 except NotImplementedError:
235 print ' Log::EndPointer();'
238 print ' Log::LiteralNull();'
240 def wrap_instance(self, instance):
241 self.type.wrap_instance("*" + instance)
243 def unwrap_instance(self, instance):
244 self.type.wrap_instance("*" + instance)
247 def ConstPointer(type):
248 return Pointer(Const(type))
251 class OutPointer(Pointer):
257 class Enum(Concrete):
259 def __init__(self, name, values):
260 Concrete.__init__(self, name)
263 def visit(self, visitor, *args, **kwargs):
264 return visitor.visit_enum(self, *args, **kwargs)
266 def _dump(self, instance):
267 print ' switch(%s) {' % instance
268 for value in self.values:
269 print ' case %s:' % value
270 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
273 print ' Log::LiteralSInt(%s);' % instance
278 def FakeEnum(type, values):
279 return Enum(type.expr, values)
282 class Bitmask(Concrete):
284 def __init__(self, type, values):
285 Concrete.__init__(self, type.expr)
289 def visit(self, visitor, *args, **kwargs):
290 return visitor.visit_bitmask(self, *args, **kwargs)
292 def _dump(self, instance):
293 print ' %s l_Value = %s;' % (self.type, instance)
294 print ' Log::BeginBitmask("%s");' % (self.type,)
295 for value in self.values:
296 print ' if((l_Value & %s) == %s) {' % (value, value)
297 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
298 print ' l_Value &= ~%s;' % value
300 print ' if(l_Value) {'
301 self.type.dump("l_Value");
303 print ' Log::EndBitmask();'
310 def __init__(self, type, length):
311 Type.__init__(self, type.expr + " *", 'P' + type.id)
315 def visit(self, visitor, *args, **kwargs):
316 return visitor.visit_array(self, *args, **kwargs)
318 def dump(self, instance):
319 index = '__i' + self.type.id
320 print ' Log::BeginArray("%s", %s);' % (self.type, self.length)
321 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
322 print ' Log::BeginElement("%s");' % (self.type,)
323 self.type.dump('(%s)[%s]' % (instance, index))
324 print ' Log::EndElement();'
326 print ' Log::EndArray();'
328 def wrap_instance(self, instance):
329 self.type.wrap_instance("*" + instance)
331 def unwrap_instance(self, instance):
332 self.type.wrap_instance("*" + instance)
335 class OutArray(Array):
341 class Struct(Concrete):
343 def __init__(self, name, members):
344 Concrete.__init__(self, name)
346 self.members = members
348 def visit(self, visitor, *args, **kwargs):
349 return visitor.visit_struct(self, *args, **kwargs)
351 def _dump(self, instance):
352 print ' Log::BeginStruct("%s");' % (self.name,)
353 for type, name in self.members:
354 print ' Log::BeginMember("%s", "%s");' % (type, name)
355 type.dump('(%s).%s' % (instance, name))
356 print ' Log::EndMember();'
357 print ' Log::EndStruct();'
362 def __init__(self, expr, type):
363 Type.__init__(self, expr)
366 def visit(self, visitor, *args, **kwargs):
367 return visitor.visit_alias(self, *args, **kwargs)
369 def dump(self, instance):
370 self.type.dump(instance)
375 def __init__(self, type):
376 Type.__init__(self, type.expr)
388 def dump(self, instance):
389 self.type.dump(instance)
391 def wrap_instance(self, instance):
392 self.type.wrap_instance(instance)
394 def unwrap_instance(self, instance):
395 self.type.unwrap_instance(instance)
400 def __init__(self, type, name, args, call = '__stdcall', fail = None):
407 def prototype(self, name=None):
414 s = self.call + ' ' + s
415 if name.startswith('*'):
417 s = self.type.expr + ' ' + s
420 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
426 def pointer_type(self):
427 return 'P' + self.name
429 def pointer_value(self):
430 return 'p' + self.name
433 ptype = self.pointer_type()
434 pvalue = self.pointer_value()
435 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
436 print 'static %s %s = NULL;' % (ptype, pvalue)
439 def get_true_pointer(self):
440 raise NotImplementedError
443 print ' ExitProcess(0);'
446 if self.fail is not None:
447 if self.type is Void:
448 assert self.fail == ''
451 assert self.fail != ''
452 print ' return %s;' % self.fail
457 pvalue = self.pointer_value()
458 print self.prototype() + ' {'
459 if self.type is Void:
462 print ' %s result;' % self.type
464 self.get_true_pointer()
465 print ' Log::BeginCall("%s");' % (self.name)
466 for type, name in self.args:
467 if not type.isoutput():
468 type.unwrap_instance(name)
469 print ' Log::BeginArg("%s", "%s");' % (type, name)
471 print ' Log::EndArg();'
472 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
473 for type, name in self.args:
475 print ' Log::BeginArg("%s", "%s");' % (type, name)
477 print ' Log::EndArg();'
478 type.wrap_instance(name)
479 if self.type is not Void:
480 print ' Log::BeginReturn("%s");' % self.type
481 self.type.dump("result")
482 print ' Log::EndReturn();'
483 self.type.wrap_instance('result')
484 print ' Log::EndCall();'
485 self.post_call_impl()
486 if self.type is not Void:
487 print ' return result;'
491 def post_call_impl(self):
495 class Interface(Type):
497 def __init__(self, name, base=None):
498 Type.__init__(self, name)
503 def itermethods(self):
504 if self.base is not None:
505 for method in self.base.itermethods():
507 for method in self.methods:
512 return "Wrap" + self.expr
514 def wrap_pre_decl(self):
515 print "class %s;" % self.wrap_name()
518 print "class %s : public %s " % (self.wrap_name(), self.name)
521 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
522 print " virtual ~%s();" % self.wrap_name()
524 for method in self.itermethods():
525 print " " + method.prototype() + ";"
528 print " %s * m_pInstance;" % (self.name,)
533 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
534 print ' m_pInstance = pInstance;'
537 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
540 for method in self.itermethods():
541 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
542 if method.type is Void:
545 print ' %s result;' % method.type
547 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
548 print ' Log::BeginArg("%s *", "this");' % self.name
549 print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
550 print ' Log::EndPointer();'
551 print ' Log::EndArg();'
552 for type, name in method.args:
553 if not type.isoutput():
554 type.unwrap_instance(name)
555 print ' Log::BeginArg("%s", "%s");' % (type, name)
557 print ' Log::EndArg();'
558 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
559 for type, name in method.args:
561 print ' Log::BeginArg("%s", "%s");' % (type, name)
563 print ' Log::EndArg();'
564 type.wrap_instance(name)
565 if method.type is not Void:
566 print ' Log::BeginReturn("%s");' % method.type
567 method.type.dump("result")
568 print ' Log::EndReturn();'
569 method.type.wrap_instance('result')
570 print ' Log::EndCall();'
571 if method.name == 'QueryInterface':
572 print ' if(*ppvObj == m_pInstance)'
573 print ' *ppvObj = this;'
574 if method.name == 'Release':
575 assert method.type is not Void
577 print ' delete this;'
578 if method.type is not Void:
579 print ' return result;'
585 class Method(Function):
587 def __init__(self, type, name, args):
588 Function.__init__(self, type, name, args, call = '__stdcall')
593 class WrapPointer(Pointer):
595 def __init__(self, type):
596 Pointer.__init__(self, type)
597 if type not in towrap:
600 def wrap_instance(self, instance):
601 print " if(%s)" % instance
602 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
604 def unwrap_instance(self, instance):
605 print " if(%s)" % instance
606 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
612 Type.__init__(self, "char *")
614 def visit(self, visitor, *args, **kwargs):
615 return visitor.visit_literal(self, *args, **kwargs)
617 def dump(self, instance):
618 print ' Log::LiteralString((const char *)%s);' % instance
626 Type.__init__(self, "void")
628 def visit(self, visitor, *args, **kwargs):
629 return visitor.visit_opaque(self, *args, **kwargs)
631 def dump(self, instance):
632 print ' Log::LiteralOpaque();'
634 Opaque = Pointer(_Opaque())
637 Bool = Literal("bool", "Bool")
638 SChar = Literal("signed char", "SInt")
639 UChar = Literal("unsigned char", "UInt")
640 Short = Literal("short", "SInt")
641 Int = Literal("int", "SInt")
642 Long = Literal("long", "SInt")
643 LongLong = Literal("long long", "SInt")
644 UShort = Literal("unsigned short", "UInt")
645 UInt = Literal("unsigned int", "UInt")
646 ULong = Literal("unsigned long", "UInt")
647 Float = Literal("float", "Float")
648 Double = Literal("double", "Float")
649 SizeT = Literal("size_t", "UInt")
650 WString = Literal("wchar_t *", "WString")
654 for type in all_types.itervalues():
657 for type in all_types.itervalues():