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_handle(self, handle):
102 self.visit(handle.type)
104 def visit_alias(self, alias):
105 self.visit(alias.type)
107 def visit_opaque(self, opaque):
110 def visit_interface(self, interface):
114 class DumpImplementer(base.Visitor):
115 '''Dump an instance.'''
117 def visit_literal(self, literal, instance):
118 print ' Log::Literal%s(%s);' % (literal.format, instance)
120 def visit_string(self, string, instance):
121 print ' Log::LiteralString((const char *)%s);' % instance
123 def visit_const(self, const, instance):
124 self.visit(const.type, instance)
126 def visit_struct(self, struct, instance):
127 print ' __traceStruct%s(%s);' % (struct.id, instance)
129 def visit_array(self, array, instance):
130 print ' if(%s) {' % instance
131 index = '__i' + array.type.id
132 print ' Log::BeginArray(%s);' % (array.length,)
133 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
134 print ' Log::BeginElement();'
135 self.visit(array.type, '(%s)[%s]' % (instance, index))
136 print ' Log::EndElement();'
138 print ' Log::EndArray();'
141 print ' Log::LiteralNull();'
143 def visit_blob(self, blob, instance):
144 print ' Log::LiteralBlob(%s, %s);' % (instance, blob.size)
146 def visit_enum(self, enum, instance):
147 print ' __traceEnum%s(%s);' % (enum.id, instance)
149 def visit_bitmask(self, bitmask, instance):
150 print ' __traceBitmask%s(%s);' % (bitmask.id, instance)
152 def visit_pointer(self, pointer, instance):
153 print ' if(%s) {' % instance
154 print ' Log::BeginPointer((const void *)%s);' % (instance,)
155 dump_instance(pointer.type, "*" + instance)
156 print ' Log::EndPointer();'
159 print ' Log::LiteralNull();'
161 def visit_handle(self, handle, instance):
162 self.visit(handle.type, instance)
164 def visit_alias(self, alias, instance):
165 self.visit(alias.type, instance)
167 def visit_opaque(self, opaque, instance):
168 print ' Log::LiteralOpaque((const void *)%s);' % instance
170 def visit_interface(self, interface, instance):
171 print ' Log::LiteralOpaque((const void *)%s);' % instance
174 dump_instance = DumpImplementer().visit
178 class Wrapper(base.Visitor):
179 '''Wrap an instance.'''
181 def visit_void(self, type, instance):
182 raise NotImplementedError
184 def visit_literal(self, type, instance):
187 def visit_string(self, type, instance):
190 def visit_const(self, type, instance):
193 def visit_struct(self, struct, instance):
194 for type, name in struct.members:
195 self.visit(type, "(%s).%s" % (instance, name))
197 def visit_array(self, array, instance):
198 # XXX: actually it is possible to return an array of pointers
201 def visit_blob(self, blob, instance):
204 def visit_enum(self, enum, instance):
207 def visit_bitmask(self, bitmask, instance):
210 def visit_pointer(self, pointer, instance):
211 self.visit(pointer.type, "*" + instance)
213 def visit_handle(self, handle, instance):
214 self.visit(handle.type, instance)
216 def visit_alias(self, alias, instance):
217 self.visit(alias.type, instance)
219 def visit_opaque(self, opaque, instance):
222 def visit_interface(self, interface, instance):
223 print " if(%s)" % instance
224 print " %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
227 class Unwrapper(Wrapper):
229 def visit_interface(self, interface, instance):
230 print " if(%s)" % instance
231 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance)
233 wrap_instance = Wrapper().visit
234 unwrap_instance = Unwrapper().visit
239 def trace_api(self, api):
243 for header in api.headers:
244 print '#include <%s>' % header
248 visitor = DumpDeclarator()
249 map(visitor.visit, api.types)
253 map(self.interface_wrap_name, api.interfaces)
254 map(self.interface_pre_decl, api.interfaces)
255 map(self.interface_decl, api.interfaces)
256 map(self.interface_wrap_impl, api.interfaces)
260 map(self.trace_function_decl, api.functions)
261 map(self.trace_function_impl, api.functions)
266 def header(self, api):
269 def footer(self, api):
272 def function_pointer_type(self, function):
273 return 'P' + function.name
275 def function_pointer_value(self, function):
276 return 'p' + function.name
278 def trace_function_decl(self, function):
279 ptype = self.function_pointer_type(function)
280 pvalue = self.function_pointer_value(function)
281 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
282 print 'static %s %s = NULL;' % (ptype, pvalue)
285 def trace_function_fail(self, function):
286 if function.fail is not None:
287 if function.type is base.Void:
288 assert function.fail == ''
291 assert function.fail != ''
292 print ' return %s;' % function.fail
294 print ' Log::Abort();'
296 def get_function_address(self, function):
297 raise NotImplementedError
299 def _get_true_pointer(self, function):
300 ptype = self.function_pointer_type(function)
301 pvalue = self.function_pointer_value(function)
302 print ' if(!%s) {' % (pvalue,)
303 print ' %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
304 print ' if(!%s)' % (pvalue,)
305 self.trace_function_fail(function)
308 def trace_function_impl(self, function):
309 pvalue = self.function_pointer_value(function)
310 print function.prototype() + ' {'
311 if function.type is base.Void:
314 print ' %s __result;' % function.type
315 result = '__result = '
316 self._get_true_pointer(function)
317 print ' Log::BeginCall("%s");' % (function.name)
318 for arg in function.args:
320 self.unwrap_arg(function, arg)
321 self.dump_arg(function, arg)
322 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
323 for arg in function.args:
325 self.dump_arg(function, arg)
326 self.wrap_arg(function, arg)
327 if function.type is not base.Void:
328 self.dump_ret(function, "__result")
329 print ' Log::EndCall();'
330 if function.type is not base.Void:
331 self.wrap_ret(function, "__result")
332 print ' return __result;'
336 def dump_arg(self, function, arg):
337 print ' Log::BeginArg("%s");' % (arg.name,)
338 dump_instance(arg.type, arg.name)
339 print ' Log::EndArg();'
341 def wrap_arg(self, function, arg):
342 wrap_instance(arg.type, arg.name)
344 def unwrap_arg(self, function, arg):
345 unwrap_instance(arg.type, arg.name)
347 def dump_ret(self, function, instance):
348 print ' Log::BeginReturn();'
349 dump_instance(function.type, instance)
350 print ' Log::EndReturn();'
352 def wrap_ret(self, function, instance):
353 wrap_instance(function.type, instance)
355 def unwrap_ret(self, function, instance):
356 unwrap_instance(function.type, instance)
358 def interface_wrap_name(self, interface):
359 return "Wrap" + interface.expr
361 def interface_pre_decl(self, interface):
362 print "class %s;" % interface.wrap_name()
364 def interface_decl(self, interface):
365 print "class %s : public %s " % (interface.wrap_name(), interface.name)
368 print " %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
369 print " virtual ~%s();" % interface.wrap_name()
371 for method in interface.itermethods():
372 print " " + method.prototype() + ";"
375 print " %s * m_pInstance;" % (interface.name,)
379 def interface_wrap_impl(self, interface):
380 print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name)
381 print ' m_pInstance = pInstance;'
384 print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
387 for method in interface.itermethods():
388 self.trace_method(interface, method)
391 def trace_method(self, interface, method):
392 print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
393 if method.type is Void:
396 print ' %s __result;' % method.type
397 result = '__result = '
398 print ' Log::BeginCall("%s");' % (interface.name + '::' + method.name)
399 print ' Log::BeginArg("this");'
400 print ' Log::LiteralOpaque((const void *)m_pInstance);'
401 print ' Log::EndArg();'
402 for arg in method.args:
404 self.unwrap_arg(method, arg)
405 self.dump_arg(method, arg)
406 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
407 for arg in method.args:
409 self.dump_arg(method, arg)
410 self.wrap_arg(method, arg)
411 if method.type is not Void:
412 print ' Log::BeginReturn("%s");' % method.type
413 dump_instance(method.type, "__result")
414 print ' Log::EndReturn();'
415 wrap_instance(method.type, '__result')
416 print ' Log::EndCall();'
417 if method.name == 'QueryInterface':
418 print ' if (*ppvObj == m_pInstance)'
419 print ' *ppvObj = this;'
420 if method.name == 'Release':
421 assert method.type is not Void
422 print ' if (!__result)'
423 print ' delete this;'
424 if method.type is not Void:
425 print ' return __result;'