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)
71 print ' switch(value) {'
72 for value in enum.values:
73 print ' case %s:' % value
74 print ' Trace::LiteralNamedConstant("%s", %s);' % (value, value)
77 print ' Trace::LiteralSInt(value);'
83 def visit_bitmask(self, bitmask):
84 print 'static const Trace::BitmaskVal __bitmask%s_vals[] = {' % (bitmask.id)
85 for value in bitmask.values:
86 print ' {"%s", %s},' % (value, value)
89 print 'static const Trace::BitmaskSig __bitmask%s_sig = {' % (bitmask.id)
90 print ' %u, %u, __bitmask%s_vals' % (int(bitmask.id), len(bitmask.values), bitmask.id)
94 def visit_pointer(self, pointer):
95 self.visit(pointer.type)
97 def visit_handle(self, handle):
98 self.visit(handle.type)
100 def visit_alias(self, alias):
101 self.visit(alias.type)
103 def visit_opaque(self, opaque):
106 def visit_interface(self, interface):
110 class DumpImplementer(stdapi.Visitor):
111 '''Dump an instance.'''
113 def visit_literal(self, literal, instance):
114 print ' Trace::Literal%s(%s);' % (literal.format, instance)
116 def visit_string(self, string, instance):
117 if string.length is not None:
118 print ' Trace::LiteralString((const char *)%s, %s);' % (instance, string.length)
120 print ' Trace::LiteralString((const char *)%s);' % instance
122 def visit_const(self, const, instance):
123 self.visit(const.type, instance)
125 def visit_struct(self, struct, instance):
126 print ' __traceStruct%s(%s);' % (struct.id, instance)
128 def visit_array(self, array, instance):
129 print ' if(%s) {' % instance
130 index = '__i' + array.type.id
131 print ' Trace::BeginArray(%s);' % (array.length,)
132 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
133 print ' Trace::BeginElement();'
134 self.visit(array.type, '(%s)[%s]' % (instance, index))
135 print ' Trace::EndElement();'
137 print ' Trace::EndArray();'
140 print ' Trace::LiteralNull();'
142 def visit_blob(self, blob, instance):
143 print ' Trace::LiteralBlob(%s, %s);' % (instance, blob.size)
145 def visit_enum(self, enum, instance):
146 print ' __traceEnum%s(%s);' % (enum.id, instance)
148 def visit_bitmask(self, bitmask, instance):
149 print ' Trace::LiteralBitmask(__bitmask%s_sig, %s);' % (bitmask.id, instance)
151 def visit_pointer(self, pointer, instance):
152 print ' if(%s) {' % instance
153 print ' Trace::BeginArray(1);'
154 print ' Trace::BeginElement();'
155 dump_instance(pointer.type, "*" + instance)
156 print ' Trace::EndElement();'
157 print ' Trace::EndArray();'
160 print ' Trace::LiteralNull();'
162 def visit_handle(self, handle, instance):
163 self.visit(handle.type, instance)
165 def visit_alias(self, alias, instance):
166 self.visit(alias.type, instance)
168 def visit_opaque(self, opaque, instance):
169 print ' Trace::LiteralOpaque((const void *)%s);' % instance
171 def visit_interface(self, interface, instance):
172 print ' Trace::LiteralOpaque((const void *)%s);' % instance
175 dump_instance = DumpImplementer().visit
179 class Wrapper(stdapi.Visitor):
180 '''Wrap an instance.'''
182 def visit_void(self, type, instance):
183 raise NotImplementedError
185 def visit_literal(self, type, instance):
188 def visit_string(self, type, instance):
191 def visit_const(self, type, instance):
194 def visit_struct(self, struct, instance):
195 for type, name in struct.members:
196 self.visit(type, "(%s).%s" % (instance, name))
198 def visit_array(self, array, instance):
199 # XXX: actually it is possible to return an array of pointers
202 def visit_blob(self, blob, instance):
205 def visit_enum(self, enum, instance):
208 def visit_bitmask(self, bitmask, instance):
211 def visit_pointer(self, pointer, instance):
212 self.visit(pointer.type, "*" + instance)
214 def visit_handle(self, handle, instance):
215 self.visit(handle.type, instance)
217 def visit_alias(self, alias, instance):
218 self.visit(alias.type, instance)
220 def visit_opaque(self, opaque, instance):
223 def visit_interface(self, interface, instance):
224 print " if(%s)" % instance
225 print " %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
228 class Unwrapper(Wrapper):
230 def visit_interface(self, interface, instance):
231 print " if(%s)" % instance
232 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance)
234 wrap_instance = Wrapper().visit
235 unwrap_instance = Unwrapper().visit
240 def trace_api(self, api):
244 for header in api.headers:
249 types = api.all_types()
250 visitor = DumpDeclarator()
251 map(visitor.visit, types)
255 map(self.interface_wrap_name, api.interfaces)
256 map(self.interface_pre_decl, api.interfaces)
257 map(self.interface_decl, api.interfaces)
258 map(self.interface_wrap_impl, api.interfaces)
262 map(self.trace_function_decl, api.functions)
263 map(self.trace_function_impl, api.functions)
268 def header(self, api):
271 def footer(self, api):
274 def function_pointer_type(self, function):
275 return 'P' + function.name
277 def function_pointer_value(self, function):
278 return 'p' + function.name
280 def trace_function_decl(self, function):
281 ptype = self.function_pointer_type(function)
282 pvalue = self.function_pointer_value(function)
283 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
284 print 'static %s %s = NULL;' % (ptype, pvalue)
287 def trace_function_fail(self, function):
288 if function.fail is not None:
289 if function.type is stdapi.Void:
290 assert function.fail == ''
293 assert function.fail != ''
294 print ' return %s;' % function.fail
296 print ' Trace::Abort();'
298 def get_function_address(self, function):
299 raise NotImplementedError
301 def _get_true_pointer(self, function):
302 ptype = self.function_pointer_type(function)
303 pvalue = self.function_pointer_value(function)
304 print ' if(!%s) {' % (pvalue,)
305 print ' %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
306 print ' if(!%s)' % (pvalue,)
307 self.trace_function_fail(function)
310 def trace_function_impl(self, function):
311 pvalue = self.function_pointer_value(function)
312 print function.prototype() + ' {'
313 if function.type is stdapi.Void:
316 print ' %s __result;' % function.type
317 result = '__result = '
318 self._get_true_pointer(function)
319 print ' unsigned __call = Trace::BeginEnter("%s");' % (function.name)
320 for arg in function.args:
322 self.unwrap_arg(function, arg)
323 self.dump_arg(function, arg)
324 print ' Trace::EndEnter();'
325 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
326 print ' Trace::BeginLeave(__call);'
327 for arg in function.args:
329 self.dump_arg(function, arg)
330 self.wrap_arg(function, arg)
331 if function.type is not stdapi.Void:
332 self.dump_ret(function, "__result")
333 print ' Trace::EndLeave();'
334 if function.type is not stdapi.Void:
335 self.wrap_ret(function, "__result")
336 print ' return __result;'
340 def dump_arg(self, function, arg):
341 print ' Trace::BeginArg(%u, "%s");' % (arg.index, arg.name,)
342 dump_instance(arg.type, arg.name)
343 print ' Trace::EndArg();'
345 def wrap_arg(self, function, arg):
346 wrap_instance(arg.type, arg.name)
348 def unwrap_arg(self, function, arg):
349 unwrap_instance(arg.type, arg.name)
351 def dump_ret(self, function, instance):
352 print ' Trace::BeginReturn();'
353 dump_instance(function.type, instance)
354 print ' Trace::EndReturn();'
356 def wrap_ret(self, function, instance):
357 wrap_instance(function.type, instance)
359 def unwrap_ret(self, function, instance):
360 unwrap_instance(function.type, instance)
362 def interface_wrap_name(self, interface):
363 return "Wrap" + interface.expr
365 def interface_pre_decl(self, interface):
366 print "class %s;" % interface.wrap_name()
368 def interface_decl(self, interface):
369 print "class %s : public %s " % (interface.wrap_name(), interface.name)
372 print " %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
373 print " virtual ~%s();" % interface.wrap_name()
375 for method in interface.itermethods():
376 print " " + method.prototype() + ";"
379 print " %s * m_pInstance;" % (interface.name,)
383 def interface_wrap_impl(self, interface):
384 print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name)
385 print ' m_pInstance = pInstance;'
388 print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
391 for method in interface.itermethods():
392 self.trace_method(interface, method)
395 def trace_method(self, interface, method):
396 print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
397 if method.type is Void:
400 print ' %s __result;' % method.type
401 result = '__result = '
402 print ' Trace::BeginCall("%s");' % (interface.name + '::' + method.name)
403 print ' Trace::BeginArg(0, "this");'
404 print ' Trace::LiteralOpaque((const void *)m_pInstance);'
405 print ' Trace::EndArg();'
406 for arg in method.args:
408 self.unwrap_arg(method, arg)
409 self.dump_arg(method, arg)
410 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
411 for arg in method.args:
413 self.dump_arg(method, arg)
414 self.wrap_arg(method, arg)
415 if method.type is not Void:
416 print ' Trace::BeginReturn("%s");' % method.type
417 dump_instance(method.type, "__result")
418 print ' Trace::EndReturn();'
419 wrap_instance(method.type, '__result')
420 print ' Trace::EndCall();'
421 if method.name == 'QueryInterface':
422 print ' if (*ppvObj == m_pInstance)'
423 print ' *ppvObj = this;'
424 if method.name == 'Release':
425 assert method.type is not Void
426 print ' if (!__result)'
427 print ' delete this;'
428 if method.type is not Void:
429 print ' return __result;'