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(base.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 ' Log::BeginStruct("%s");' % struct.name
55 for type, name in struct.members:
56 print ' Log::BeginMember("%s");' % (name,)
57 dump_instance(type, 'value.%s' % (name,))
58 print ' Log::EndMember();'
59 print ' Log::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 ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
77 print ' Log::LiteralSInt(value);'
83 def visit_bitmask(self, bitmask):
84 print 'static void __traceBitmask%s(%s value) {' % (bitmask.id, bitmask.type)
85 print ' Log::BeginBitmask();'
86 for value in bitmask.values:
87 print ' if((value & %s) == %s) {' % (value, value)
88 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
89 print ' value &= ~%s;' % value
92 dump_instance(bitmask.type, "value");
94 print ' Log::EndBitmask();'
98 def visit_pointer(self, pointer):
99 self.visit(pointer.type)
101 def visit_alias(self, alias):
102 self.visit(alias.type)
104 def visit_opaque(self, opaque):
107 def visit_interface(self, interface):
111 class DumpImplementer(base.Visitor):
112 '''Dump an instance.'''
114 def visit_literal(self, literal, instance):
115 print ' Log::Literal%s(%s);' % (literal.format, instance)
117 def visit_string(self, string, instance):
118 print ' Log::LiteralString((const char *)%s);' % instance
120 def visit_const(self, const, instance):
121 self.visit(const.type, instance)
123 def visit_struct(self, struct, instance):
124 print ' __traceStruct%s(%s);' % (struct.id, instance)
126 def visit_array(self, array, instance):
127 print ' if(%s) {' % instance
128 index = '__i' + array.type.id
129 print ' Log::BeginArray(%s);' % (array.length,)
130 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
131 print ' Log::BeginElement();'
132 self.visit(array.type, '(%s)[%s]' % (instance, index))
133 print ' Log::EndElement();'
135 print ' Log::EndArray();'
138 print ' Log::LiteralNull();'
140 def visit_blob(self, blob, instance):
141 print ' Log::LiteralBlob(%s, %s);' % (instance, blob.size)
143 def visit_enum(self, enum, instance):
144 print ' __traceEnum%s(%s);' % (enum.id, instance)
146 def visit_bitmask(self, bitmask, instance):
147 print ' __traceBitmask%s(%s);' % (bitmask.id, instance)
149 def visit_pointer(self, pointer, instance):
150 print ' if(%s) {' % instance
151 print ' Log::BeginPointer((const void *)%s);' % (instance,)
152 dump_instance(pointer.type, "*" + instance)
153 print ' Log::EndPointer();'
156 print ' Log::LiteralNull();'
158 def visit_alias(self, alias, instance):
159 self.visit(alias.type, instance)
161 def visit_opaque(self, opaque, instance):
162 print ' Log::LiteralOpaque((const void *)%s);' % instance
164 def visit_interface(self, interface, instance):
165 print ' Log::LiteralOpaque((const void *)%s);' % instance
168 dump_instance = DumpImplementer().visit
172 class Wrapper(base.Visitor):
173 '''Wrap an instance.'''
175 def visit_void(self, type, instance):
176 raise NotImplementedError
178 def visit_literal(self, type, instance):
181 def visit_string(self, type, instance):
184 def visit_const(self, type, instance):
187 def visit_struct(self, struct, instance):
188 for type, name in struct.members:
189 self.visit(type, "(%s).%s" % (instance, name))
191 def visit_array(self, array, instance):
192 # XXX: actually it is possible to return an array of pointers
195 def visit_blob(self, blob, instance):
198 def visit_enum(self, enum, instance):
201 def visit_bitmask(self, bitmask, instance):
204 def visit_pointer(self, pointer, instance):
205 self.visit(pointer.type, "*" + instance)
207 def visit_alias(self, alias, instance):
208 self.visit(alias.type, instance)
210 def visit_opaque(self, opaque, instance):
213 def visit_interface(self, interface, instance):
214 print " if(%s)" % instance
215 print " %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
218 class Unwrapper(Wrapper):
220 def visit_interface(self, interface, instance):
221 print " if(%s)" % instance
222 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance)
224 wrap_instance = Wrapper().visit
225 unwrap_instance = Unwrapper().visit
230 def trace_api(self, api):
234 for header in api.headers:
235 print '#include <%s>' % header
239 visitor = DumpDeclarator()
240 map(visitor.visit, api.types)
244 map(self.interface_wrap_name, api.interfaces)
245 map(self.interface_pre_decl, api.interfaces)
246 map(self.interface_decl, api.interfaces)
247 map(self.interface_wrap_impl, api.interfaces)
251 map(self.trace_function_decl, api.functions)
252 map(self.trace_function_impl, api.functions)
257 def header(self, api):
260 def footer(self, api):
263 def function_pointer_type(self, function):
264 return 'P' + function.name
266 def function_pointer_value(self, function):
267 return 'p' + function.name
269 def trace_function_decl(self, function):
270 ptype = self.function_pointer_type(function)
271 pvalue = self.function_pointer_value(function)
272 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
273 print 'static %s %s = NULL;' % (ptype, pvalue)
276 def trace_function_fail(self, function):
277 if function.fail is not None:
278 if function.type is base.Void:
279 assert function.fail == ''
282 assert function.fail != ''
283 print ' return %s;' % function.fail
285 print ' Log::Abort();'
287 def get_function_address(self, function):
288 raise NotImplementedError
290 def _get_true_pointer(self, function):
291 ptype = self.function_pointer_type(function)
292 pvalue = self.function_pointer_value(function)
293 print ' if(!%s) {' % (pvalue,)
294 print ' %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
295 print ' if(!%s)' % (pvalue,)
296 self.trace_function_fail(function)
299 def trace_function_impl(self, function):
300 pvalue = self.function_pointer_value(function)
301 print function.prototype() + ' {'
302 if function.type is base.Void:
305 print ' %s __result;' % function.type
306 result = '__result = '
307 self._get_true_pointer(function)
308 print ' Log::BeginCall("%s");' % (function.name)
309 for arg in function.args:
311 self.unwrap_arg(function, arg)
312 self.dump_arg(function, arg)
313 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
314 for arg in function.args:
316 self.dump_arg(function, arg)
317 self.wrap_arg(function, arg)
318 if function.type is not base.Void:
319 self.dump_ret(function, "__result")
320 print ' Log::EndCall();'
321 if function.type is not base.Void:
322 self.wrap_ret(function, "__result")
323 print ' return __result;'
327 def dump_arg(self, function, arg):
328 print ' Log::BeginArg("%s");' % (arg.name,)
329 dump_instance(arg.type, arg.name)
330 print ' Log::EndArg();'
332 def wrap_arg(self, function, arg):
333 wrap_instance(arg.type, arg.name)
335 def unwrap_arg(self, function, arg):
336 unwrap_instance(arg.type, arg.name)
338 def dump_ret(self, function, instance):
339 print ' Log::BeginReturn();'
340 dump_instance(function.type, instance)
341 print ' Log::EndReturn();'
343 def wrap_ret(self, function, instance):
344 wrap_instance(function.type, instance)
346 def unwrap_ret(self, function, instance):
347 unwrap_instance(function.type, instance)
349 def interface_wrap_name(self, interface):
350 return "Wrap" + interface.expr
352 def interface_pre_decl(self, interface):
353 print "class %s;" % interface.wrap_name()
355 def interface_decl(self, interface):
356 print "class %s : public %s " % (interface.wrap_name(), interface.name)
359 print " %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
360 print " virtual ~%s();" % interface.wrap_name()
362 for method in interface.itermethods():
363 print " " + method.prototype() + ";"
366 print " %s * m_pInstance;" % (interface.name,)
370 def interface_wrap_impl(self, interface):
371 print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name)
372 print ' m_pInstance = pInstance;'
375 print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
378 for method in interface.itermethods():
379 self.trace_method(interface, method)
382 def trace_method(self, interface, method):
383 print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
384 if method.type is Void:
387 print ' %s __result;' % method.type
388 result = '__result = '
389 print ' Log::BeginCall("%s");' % (interface.name + '::' + method.name)
390 print ' Log::BeginArg("this");'
391 print ' Log::LiteralOpaque((const void *)m_pInstance);'
392 print ' Log::EndArg();'
393 for arg in method.args:
395 self.unwrap_arg(method, arg)
396 self.dump_arg(method, arg)
397 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
398 for arg in method.args:
400 self.dump_arg(method, arg)
401 self.wrap_arg(method, arg)
402 if method.type is not Void:
403 print ' Log::BeginReturn("%s");' % method.type
404 dump_instance(method.type, "__result")
405 print ' Log::EndReturn();'
406 wrap_instance(method.type, '__result')
407 print ' Log::EndCall();'
408 if method.name == 'QueryInterface':
409 print ' if (*ppvObj == m_pInstance)'
410 print ' *ppvObj = this;'
411 if method.name == 'Release':
412 assert method.type is not Void
413 print ' if (!__result)'
414 print ' delete this;'
415 if method.type is not Void:
416 print ' return __result;'