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
77 def __init__(self, expr, id = ''):
81 assert char.isalnum() or char in '_ '
83 id = id.replace(' ', '_')
89 assert id not in all_types
97 def visit(self, visitor, *args, **kwargs):
98 raise NotImplementedError
109 def dump(self, instance):
110 raise NotImplementedError
112 def wrap_instance(self, instance):
115 def unwrap_instance(self, instance):
122 Type.__init__(self, "void")
124 def visit(self, visitor, *args, **kwargs):
125 return visitor.visit_void(self, *args, **kwargs)
130 class Concrete(Type):
133 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
136 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
141 def _dump(self, instance):
142 raise NotImplementedError
144 def dump(self, instance):
145 print ' Dump%s(%s);' % (self.id, instance)
148 class Literal(Concrete):
150 def __init__(self, expr, format, base=10):
151 Concrete.__init__(self, expr)
154 def visit(self, visitor, *args, **kwargs):
155 return visitor.visit_literal(self, *args, **kwargs)
157 def _dump(self, instance):
158 print ' Log::Literal%s(%s);' % (self.format, instance)
163 def __init__(self, type):
165 if isinstance(type, Pointer):
166 expr = type.expr + " const"
168 expr = "const " + type.expr
170 Type.__init__(self, expr, 'C' + type.id)
174 def visit(self, visitor, *args, **kwargs):
175 return visitor.visit_const(self, *args, **kwargs)
177 def dump(self, instance):
178 self.type.dump(instance)
183 def __init__(self, type):
184 Type.__init__(self, type.expr + " *", 'P' + type.id)
187 def visit(self, visitor, *args, **kwargs):
188 return visitor.visit_pointer(self, *args, **kwargs)
190 def dump(self, instance):
191 print ' if(%s) {' % instance
192 print ' Log::BeginPointer("%s", (const void *)%s);' % (self.type, instance)
194 self.type.dump("*" + instance)
195 except NotImplementedError:
197 print ' Log::EndPointer();'
200 print ' Log::LiteralNull();'
202 def wrap_instance(self, instance):
203 self.type.wrap_instance("*" + instance)
205 def unwrap_instance(self, instance):
206 self.type.wrap_instance("*" + instance)
209 def ConstPointer(type):
210 return Pointer(Const(type))
213 class OutPointer(Pointer):
219 class Enum(Concrete):
221 def __init__(self, name, values):
222 Concrete.__init__(self, name)
225 def visit(self, visitor, *args, **kwargs):
226 return visitor.visit_enum(self, *args, **kwargs)
228 def _dump(self, instance):
229 print ' switch(%s) {' % instance
230 for value in self.values:
231 print ' case %s:' % value
232 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
235 print ' Log::LiteralSInt(%s);' % instance
240 def FakeEnum(type, values):
241 return Enum(type.expr, values)
244 class Bitmask(Concrete):
246 def __init__(self, type, values):
247 Concrete.__init__(self, type.expr)
251 def visit(self, visitor, *args, **kwargs):
252 return visitor.visit_bitmask(self, *args, **kwargs)
254 def _dump(self, instance):
255 print ' %s l_Value = %s;' % (self.type, instance)
256 print ' Log::BeginBitmask("%s");' % (self.type,)
257 for value in self.values:
258 print ' if((l_Value & %s) == %s) {' % (value, value)
259 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
260 print ' l_Value &= ~%s;' % value
262 print ' if(l_Value) {'
263 self.type.dump("l_Value");
265 print ' Log::EndBitmask();'
272 def __init__(self, type, length):
273 Type.__init__(self, type.expr + " *", 'P' + type.id)
277 def visit(self, visitor, *args, **kwargs):
278 return visitor.visit_array(self, *args, **kwargs)
280 def dump(self, instance):
281 index = '__i' + self.type.id
282 print ' Log::BeginArray("%s", %s);' % (self.type, self.length)
283 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
284 print ' Log::BeginElement("%s");' % (self.type,)
285 self.type.dump('(%s)[%s]' % (instance, index))
286 print ' Log::EndElement();'
288 print ' Log::EndArray();'
290 def wrap_instance(self, instance):
291 self.type.wrap_instance("*" + instance)
293 def unwrap_instance(self, instance):
294 self.type.wrap_instance("*" + instance)
297 class OutArray(Array):
303 class Struct(Concrete):
305 def __init__(self, name, members):
306 Concrete.__init__(self, name)
308 self.members = members
310 def visit(self, visitor, *args, **kwargs):
311 return visitor.visit_struct(self, *args, **kwargs)
313 def _dump(self, instance):
314 print ' Log::BeginStruct("%s");' % (self.name,)
315 for type, name in self.members:
316 print ' Log::BeginMember("%s", "%s");' % (type, name)
317 type.dump('(%s).%s' % (instance, name))
318 print ' Log::EndMember();'
319 print ' Log::EndStruct();'
324 def __init__(self, name, type):
325 Type.__init__(self, name)
328 def visit(self, visitor, *args, **kwargs):
329 return visitor.visit_alias(self, *args, **kwargs)
331 def dump(self, instance):
332 self.type.dump(instance)
337 def __init__(self, type):
338 Type.__init__(self, type.expr)
350 def dump(self, instance):
351 self.type.dump(instance)
353 def wrap_instance(self, instance):
354 self.type.wrap_instance(instance)
356 def unwrap_instance(self, instance):
357 self.type.unwrap_instance(instance)
362 def __init__(self, type, name, args, call = '__stdcall', fail = None):
369 def prototype(self, name=None):
376 s = self.call + ' ' + s
377 if name.startswith('*'):
379 s = self.type.expr + ' ' + s
382 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
388 def pointer_type(self):
389 return 'P' + self.name
391 def pointer_value(self):
392 return 'p' + self.name
395 ptype = self.pointer_type()
396 pvalue = self.pointer_value()
397 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
398 print 'static %s %s = NULL;' % (ptype, pvalue)
401 def get_true_pointer(self):
402 raise NotImplementedError
405 print ' ExitProcess(0);'
408 if self.fail is not None:
409 if self.type is Void:
410 assert self.fail == ''
413 assert self.fail != ''
414 print ' return %s;' % self.fail
419 pvalue = self.pointer_value()
420 print self.prototype() + ' {'
421 if self.type is Void:
424 print ' %s result;' % self.type
426 self.get_true_pointer()
427 print ' Log::BeginCall("%s");' % (self.name)
428 for type, name in self.args:
429 if not type.isoutput():
430 type.unwrap_instance(name)
431 print ' Log::BeginArg("%s", "%s");' % (type, name)
433 print ' Log::EndArg();'
434 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
435 for type, name in self.args:
437 print ' Log::BeginArg("%s", "%s");' % (type, name)
439 print ' Log::EndArg();'
440 type.wrap_instance(name)
441 if self.type is not Void:
442 print ' Log::BeginReturn("%s");' % self.type
443 self.type.dump("result")
444 print ' Log::EndReturn();'
445 self.type.wrap_instance('result')
446 print ' Log::EndCall();'
447 self.post_call_impl()
448 if self.type is not Void:
449 print ' return result;'
453 def post_call_impl(self):
457 class Interface(Type):
459 def __init__(self, name, base=None):
460 Type.__init__(self, name)
465 def itermethods(self):
466 if self.base is not None:
467 for method in self.base.itermethods():
469 for method in self.methods:
474 return "Wrap" + self.expr
476 def wrap_pre_decl(self):
477 print "class %s;" % self.wrap_name()
480 print "class %s : public %s " % (self.wrap_name(), self.name)
483 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
484 print " virtual ~%s();" % self.wrap_name()
486 for method in self.itermethods():
487 print " " + method.prototype() + ";"
490 print " %s * m_pInstance;" % (self.name,)
495 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
496 print ' m_pInstance = pInstance;'
499 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
502 for method in self.itermethods():
503 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
504 if method.type is Void:
507 print ' %s result;' % method.type
509 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
510 print ' Log::BeginArg("%s *", "this");' % self.name
511 print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
512 print ' Log::EndPointer();'
513 print ' Log::EndArg();'
514 for type, name in method.args:
515 if not type.isoutput():
516 type.unwrap_instance(name)
517 print ' Log::BeginArg("%s", "%s");' % (type, name)
519 print ' Log::EndArg();'
520 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
521 for type, name in method.args:
523 print ' Log::BeginArg("%s", "%s");' % (type, name)
525 print ' Log::EndArg();'
526 type.wrap_instance(name)
527 if method.type is not Void:
528 print ' Log::BeginReturn("%s");' % method.type
529 method.type.dump("result")
530 print ' Log::EndReturn();'
531 method.type.wrap_instance('result')
532 print ' Log::EndCall();'
533 if method.name == 'QueryInterface':
534 print ' if(*ppvObj == m_pInstance)'
535 print ' *ppvObj = this;'
536 if method.name == 'Release':
537 assert method.type is not Void
539 print ' delete this;'
540 if method.type is not Void:
541 print ' return result;'
547 class Method(Function):
549 def __init__(self, type, name, args):
550 Function.__init__(self, type, name, args, call = '__stdcall')
555 class WrapPointer(Pointer):
557 def __init__(self, type):
558 Pointer.__init__(self, type)
559 if type not in towrap:
562 def wrap_instance(self, instance):
563 print " if(%s)" % instance
564 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
566 def unwrap_instance(self, instance):
567 print " if(%s)" % instance
568 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
574 Type.__init__(self, "char *")
576 def visit(self, visitor, *args, **kwargs):
577 return visitor.visit_literal(self, *args, **kwargs)
579 def dump(self, instance):
580 print ' Log::LiteralString((const char *)%s);' % instance
588 Type.__init__(self, "void")
590 def visit(self, visitor, *args, **kwargs):
591 return visitor.visit_opaque(self, *args, **kwargs)
593 def dump(self, instance):
594 print ' Log::LiteralOpaque();'
596 Opaque = Pointer(_Opaque())
599 Bool = Literal("bool", "Bool")
600 SChar = Literal("signed char", "SInt")
601 UChar = Literal("unsigned char", "UInt")
602 Short = Literal("short", "SInt")
603 Int = Literal("int", "SInt")
604 Long = Literal("long", "SInt")
605 LongLong = Literal("long long", "SInt")
606 UShort = Literal("unsigned short", "UInt")
607 UInt = Literal("unsigned int", "UInt")
608 ULong = Literal("unsigned long", "UInt")
609 Float = Literal("float", "Float")
610 Double = Literal("double", "Float")
611 SizeT = Literal("size_t", "UInt")
612 WString = Literal("wchar_t *", "WString")
616 for type in all_types.itervalues():
619 for type in all_types.itervalues():