1 ##########################################################################
3 # Copyright 2008-2010 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 ##########################################################################/
35 class DumpDeclarator(stdapi.OnceVisitor):
36 '''Declare helper functions to dump complex types.'''
38 def visit_void(self, literal):
41 def visit_literal(self, literal):
44 def visit_string(self, string):
47 def visit_const(self, const):
48 self.visit(const.type)
50 def visit_struct(self, struct):
51 for type, name in struct.members:
53 print 'static void __traceStruct%s(const %s &value) {' % (struct.id, struct.expr)
54 print ' Trace::BeginStruct(%u);' % len(struct.members)
55 for type, name in struct.members:
56 print ' Trace::BeginMember("%s");' % (name,)
57 dump_instance(type, 'value.%s' % (name,))
58 print ' Trace::EndMember();'
59 print ' Trace::EndStruct();'
63 def visit_array(self, array):
64 self.visit(array.type)
66 def visit_blob(self, array):
69 def visit_enum(self, enum):
70 print 'static void __traceEnum%s(const %s value) {' % (enum.id, enum.expr)
73 value = enum.values[i]
74 print ' static const Trace::EnumSig sig%u = {%u, "%s", %s};' % (i, enum.vid + i, value, value)
75 print ' const Trace::EnumSig *sig;'
76 print ' switch(value) {'
78 value = enum.values[i]
79 print ' case %s:' % value
80 print ' sig = &sig%u;' % i
83 print ' Trace::LiteralSInt(value);'
86 print ' Trace::LiteralEnum(sig);'
90 def visit_bitmask(self, bitmask):
91 print 'static const Trace::BitmaskVal __bitmask%s_vals[] = {' % (bitmask.id)
92 for value in bitmask.values:
93 print ' {"%s", %s},' % (value, value)
96 print 'static const Trace::BitmaskSig __bitmask%s_sig = {' % (bitmask.id)
97 print ' %u, %u, __bitmask%s_vals' % (int(bitmask.id), len(bitmask.values), bitmask.id)
101 def visit_pointer(self, pointer):
102 self.visit(pointer.type)
104 def visit_handle(self, handle):
105 self.visit(handle.type)
107 def visit_alias(self, alias):
108 self.visit(alias.type)
110 def visit_opaque(self, opaque):
113 def visit_interface(self, interface):
117 class DumpImplementer(stdapi.Visitor):
118 '''Dump an instance.'''
120 def visit_literal(self, literal, instance):
121 print ' Trace::Literal%s(%s);' % (literal.format, instance)
123 def visit_string(self, string, instance):
124 if string.length is not None:
125 print ' Trace::LiteralString((const char *)%s, %s);' % (instance, string.length)
127 print ' Trace::LiteralString((const char *)%s);' % instance
129 def visit_const(self, const, instance):
130 self.visit(const.type, instance)
132 def visit_struct(self, struct, instance):
133 print ' __traceStruct%s(%s);' % (struct.id, instance)
135 def visit_array(self, array, instance):
136 print ' if(%s) {' % instance
137 index = '__i' + array.type.id
138 print ' Trace::BeginArray(%s);' % (array.length,)
139 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
140 print ' Trace::BeginElement();'
141 self.visit(array.type, '(%s)[%s]' % (instance, index))
142 print ' Trace::EndElement();'
144 print ' Trace::EndArray();'
147 print ' Trace::LiteralNull();'
149 def visit_blob(self, blob, instance):
150 print ' Trace::LiteralBlob(%s, %s);' % (instance, blob.size)
152 def visit_enum(self, enum, instance):
153 print ' __traceEnum%s(%s);' % (enum.id, instance)
155 def visit_bitmask(self, bitmask, instance):
156 print ' Trace::LiteralBitmask(__bitmask%s_sig, %s);' % (bitmask.id, instance)
158 def visit_pointer(self, pointer, instance):
159 print ' if(%s) {' % instance
160 print ' Trace::BeginArray(1);'
161 print ' Trace::BeginElement();'
162 dump_instance(pointer.type, "*" + instance)
163 print ' Trace::EndElement();'
164 print ' Trace::EndArray();'
167 print ' Trace::LiteralNull();'
169 def visit_handle(self, handle, instance):
170 self.visit(handle.type, instance)
172 def visit_alias(self, alias, instance):
173 self.visit(alias.type, instance)
175 def visit_opaque(self, opaque, instance):
176 print ' Trace::LiteralOpaque((const void *)%s);' % instance
178 def visit_interface(self, interface, instance):
179 print ' Trace::LiteralOpaque((const void *)%s);' % instance
182 dump_instance = DumpImplementer().visit
186 class Wrapper(stdapi.Visitor):
187 '''Wrap an instance.'''
189 def visit_void(self, type, instance):
190 raise NotImplementedError
192 def visit_literal(self, type, instance):
195 def visit_string(self, type, instance):
198 def visit_const(self, type, instance):
201 def visit_struct(self, struct, instance):
202 for type, name in struct.members:
203 self.visit(type, "(%s).%s" % (instance, name))
205 def visit_array(self, array, instance):
206 # XXX: actually it is possible to return an array of pointers
209 def visit_blob(self, blob, instance):
212 def visit_enum(self, enum, instance):
215 def visit_bitmask(self, bitmask, instance):
218 def visit_pointer(self, pointer, instance):
219 self.visit(pointer.type, "*" + instance)
221 def visit_handle(self, handle, instance):
222 self.visit(handle.type, instance)
224 def visit_alias(self, alias, instance):
225 self.visit(alias.type, instance)
227 def visit_opaque(self, opaque, instance):
230 def visit_interface(self, interface, instance):
231 print " if(%s)" % instance
232 print " %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
235 class Unwrapper(Wrapper):
237 def visit_interface(self, interface, instance):
238 print " if(%s)" % instance
239 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance)
241 wrap_instance = Wrapper().visit
242 unwrap_instance = Unwrapper().visit
247 def trace_api(self, api):
251 for header in api.headers:
256 types = api.all_types()
257 visitor = DumpDeclarator()
258 map(visitor.visit, types)
262 map(self.interface_wrap_name, api.interfaces)
263 map(self.interface_pre_decl, api.interfaces)
264 map(self.interface_decl, api.interfaces)
265 map(self.interface_wrap_impl, api.interfaces)
269 map(self.trace_function_decl, api.functions)
270 map(self.trace_function_impl, api.functions)
275 def header(self, api):
278 def footer(self, api):
281 def function_pointer_type(self, function):
282 return 'P' + function.name
284 def function_pointer_value(self, function):
285 return 'p' + function.name
287 def trace_function_decl(self, function):
288 ptype = self.function_pointer_type(function)
289 pvalue = self.function_pointer_value(function)
290 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
291 print 'static %s %s = NULL;' % (ptype, pvalue)
293 print 'static const char * __%s_args[] = {' % (function.name)
294 for arg in function.args:
295 print ' "%s",' % (arg.name,)
298 print 'static const Trace::FunctionSig __%s_sig = {' % (function.name)
299 print ' %u, "%s", %u, __%s_args' % (int(function.id), function.name, len(function.args), function.name)
304 def trace_function_fail(self, function):
305 if function.fail is not None:
306 if function.type is stdapi.Void:
307 assert function.fail == ''
310 assert function.fail != ''
311 print ' return %s;' % function.fail
313 print ' Trace::Abort();'
315 def get_function_address(self, function):
316 raise NotImplementedError
318 def _get_true_pointer(self, function):
319 ptype = self.function_pointer_type(function)
320 pvalue = self.function_pointer_value(function)
321 print ' if(!%s) {' % (pvalue,)
322 print ' %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
323 print ' if(!%s)' % (pvalue,)
324 self.trace_function_fail(function)
327 def trace_function_impl(self, function):
328 pvalue = self.function_pointer_value(function)
329 print function.prototype() + ' {'
330 if function.type is stdapi.Void:
333 print ' %s __result;' % function.type
334 result = '__result = '
335 self._get_true_pointer(function)
336 print ' unsigned __call = Trace::BeginEnter(__%s_sig);' % (function.name)
337 for arg in function.args:
339 self.unwrap_arg(function, arg)
340 self.dump_arg(function, arg)
341 print ' Trace::EndEnter();'
342 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
343 print ' Trace::BeginLeave(__call);'
344 for arg in function.args:
346 self.dump_arg(function, arg)
347 self.wrap_arg(function, arg)
348 if function.type is not stdapi.Void:
349 self.dump_ret(function, "__result")
350 print ' Trace::EndLeave();'
351 if function.type is not stdapi.Void:
352 self.wrap_ret(function, "__result")
353 print ' return __result;'
357 def dump_arg(self, function, arg):
358 print ' Trace::BeginArg(%u);' % (arg.index,)
359 dump_instance(arg.type, arg.name)
360 print ' Trace::EndArg();'
362 def wrap_arg(self, function, arg):
363 wrap_instance(arg.type, arg.name)
365 def unwrap_arg(self, function, arg):
366 unwrap_instance(arg.type, arg.name)
368 def dump_ret(self, function, instance):
369 print ' Trace::BeginReturn();'
370 dump_instance(function.type, instance)
371 print ' Trace::EndReturn();'
373 def wrap_ret(self, function, instance):
374 wrap_instance(function.type, instance)
376 def unwrap_ret(self, function, instance):
377 unwrap_instance(function.type, instance)
379 def interface_wrap_name(self, interface):
380 return "Wrap" + interface.expr
382 def interface_pre_decl(self, interface):
383 print "class %s;" % interface.wrap_name()
385 def interface_decl(self, interface):
386 print "class %s : public %s " % (interface.wrap_name(), interface.name)
389 print " %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
390 print " virtual ~%s();" % interface.wrap_name()
392 for method in interface.itermethods():
393 print " " + method.prototype() + ";"
396 print " %s * m_pInstance;" % (interface.name,)
400 def interface_wrap_impl(self, interface):
401 print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name)
402 print ' m_pInstance = pInstance;'
405 print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
408 for method in interface.itermethods():
409 self.trace_method(interface, method)
412 def trace_method(self, interface, method):
413 print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
414 if method.type is Void:
417 print ' %s __result;' % method.type
418 result = '__result = '
419 print ' Trace::BeginCall("%s");' % (interface.name + '::' + method.name)
420 print ' Trace::BeginArg(0);'
421 print ' Trace::LiteralOpaque((const void *)m_pInstance);'
422 print ' Trace::EndArg();'
423 for arg in method.args:
425 self.unwrap_arg(method, arg)
426 self.dump_arg(method, arg)
427 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
428 for arg in method.args:
430 self.dump_arg(method, arg)
431 self.wrap_arg(method, arg)
432 if method.type is not Void:
433 print ' Trace::BeginReturn("%s");' % method.type
434 dump_instance(method.type, "__result")
435 print ' Trace::EndReturn();'
436 wrap_instance(method.type, '__result')
437 print ' Trace::EndCall();'
438 if method.name == 'QueryInterface':
439 print ' if (*ppvObj == m_pInstance)'
440 print ' *ppvObj = this;'
441 if method.name == 'Release':
442 assert method.type is not Void
443 print ' if (!__result)'
444 print ' delete this;'
445 if method.type is not Void:
446 print ' return __result;'