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(%u);' % len(struct.members)
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 if string.length is not None:
122 print ' Log::LiteralString((const char *)%s, %s);' % (instance, string.length)
124 print ' Log::LiteralString((const char *)%s);' % instance
126 def visit_const(self, const, instance):
127 self.visit(const.type, instance)
129 def visit_struct(self, struct, instance):
130 print ' __traceStruct%s(%s);' % (struct.id, instance)
132 def visit_array(self, array, instance):
133 print ' if(%s) {' % instance
134 index = '__i' + array.type.id
135 print ' Log::BeginArray(%s);' % (array.length,)
136 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
137 print ' Log::BeginElement();'
138 self.visit(array.type, '(%s)[%s]' % (instance, index))
139 print ' Log::EndElement();'
141 print ' Log::EndArray();'
144 print ' Log::LiteralNull();'
146 def visit_blob(self, blob, instance):
147 print ' Log::LiteralBlob(%s, %s);' % (instance, blob.size)
149 def visit_enum(self, enum, instance):
150 print ' __traceEnum%s(%s);' % (enum.id, instance)
152 def visit_bitmask(self, bitmask, instance):
153 print ' __traceBitmask%s(%s);' % (bitmask.id, instance)
155 def visit_pointer(self, pointer, instance):
156 print ' if(%s) {' % instance
157 print ' Log::BeginArray(1);'
158 print ' Log::BeginElement();'
159 dump_instance(pointer.type, "*" + instance)
160 print ' Log::EndElement();'
161 print ' Log::EndArray();'
164 print ' Log::LiteralNull();'
166 def visit_handle(self, handle, instance):
167 self.visit(handle.type, instance)
169 def visit_alias(self, alias, instance):
170 self.visit(alias.type, instance)
172 def visit_opaque(self, opaque, instance):
173 print ' Log::LiteralOpaque((const void *)%s);' % instance
175 def visit_interface(self, interface, instance):
176 print ' Log::LiteralOpaque((const void *)%s);' % instance
179 dump_instance = DumpImplementer().visit
183 class Wrapper(base.Visitor):
184 '''Wrap an instance.'''
186 def visit_void(self, type, instance):
187 raise NotImplementedError
189 def visit_literal(self, type, instance):
192 def visit_string(self, type, instance):
195 def visit_const(self, type, instance):
198 def visit_struct(self, struct, instance):
199 for type, name in struct.members:
200 self.visit(type, "(%s).%s" % (instance, name))
202 def visit_array(self, array, instance):
203 # XXX: actually it is possible to return an array of pointers
206 def visit_blob(self, blob, instance):
209 def visit_enum(self, enum, instance):
212 def visit_bitmask(self, bitmask, instance):
215 def visit_pointer(self, pointer, instance):
216 self.visit(pointer.type, "*" + instance)
218 def visit_handle(self, handle, instance):
219 self.visit(handle.type, instance)
221 def visit_alias(self, alias, instance):
222 self.visit(alias.type, instance)
224 def visit_opaque(self, opaque, instance):
227 def visit_interface(self, interface, instance):
228 print " if(%s)" % instance
229 print " %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
232 class Unwrapper(Wrapper):
234 def visit_interface(self, interface, instance):
235 print " if(%s)" % instance
236 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance)
238 wrap_instance = Wrapper().visit
239 unwrap_instance = Unwrapper().visit
244 def trace_api(self, api):
248 for header in api.headers:
253 types = api.all_types()
254 visitor = DumpDeclarator()
255 map(visitor.visit, types)
259 map(self.interface_wrap_name, api.interfaces)
260 map(self.interface_pre_decl, api.interfaces)
261 map(self.interface_decl, api.interfaces)
262 map(self.interface_wrap_impl, api.interfaces)
266 map(self.trace_function_decl, api.functions)
267 map(self.trace_function_impl, api.functions)
272 def header(self, api):
275 def footer(self, api):
278 def function_pointer_type(self, function):
279 return 'P' + function.name
281 def function_pointer_value(self, function):
282 return 'p' + function.name
284 def trace_function_decl(self, function):
285 ptype = self.function_pointer_type(function)
286 pvalue = self.function_pointer_value(function)
287 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
288 print 'static %s %s = NULL;' % (ptype, pvalue)
291 def trace_function_fail(self, function):
292 if function.fail is not None:
293 if function.type is base.Void:
294 assert function.fail == ''
297 assert function.fail != ''
298 print ' return %s;' % function.fail
300 print ' Log::Abort();'
302 def get_function_address(self, function):
303 raise NotImplementedError
305 def _get_true_pointer(self, function):
306 ptype = self.function_pointer_type(function)
307 pvalue = self.function_pointer_value(function)
308 print ' if(!%s) {' % (pvalue,)
309 print ' %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
310 print ' if(!%s)' % (pvalue,)
311 self.trace_function_fail(function)
314 def trace_function_impl(self, function):
315 pvalue = self.function_pointer_value(function)
316 print function.prototype() + ' {'
317 if function.type is base.Void:
320 print ' %s __result;' % function.type
321 result = '__result = '
322 self._get_true_pointer(function)
323 print ' Log::BeginCall("%s");' % (function.name)
324 for arg in function.args:
326 self.unwrap_arg(function, arg)
327 self.dump_arg(function, arg)
328 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
329 for arg in function.args:
331 self.dump_arg(function, arg)
332 self.wrap_arg(function, arg)
333 if function.type is not base.Void:
334 self.dump_ret(function, "__result")
335 print ' Log::EndCall();'
336 if function.type is not base.Void:
337 self.wrap_ret(function, "__result")
338 print ' return __result;'
342 def dump_arg(self, function, arg):
343 print ' Log::BeginArg(%u, "%s");' % (arg.index, arg.name,)
344 dump_instance(arg.type, arg.name)
345 print ' Log::EndArg();'
347 def wrap_arg(self, function, arg):
348 wrap_instance(arg.type, arg.name)
350 def unwrap_arg(self, function, arg):
351 unwrap_instance(arg.type, arg.name)
353 def dump_ret(self, function, instance):
354 print ' Log::BeginReturn();'
355 dump_instance(function.type, instance)
356 print ' Log::EndReturn();'
358 def wrap_ret(self, function, instance):
359 wrap_instance(function.type, instance)
361 def unwrap_ret(self, function, instance):
362 unwrap_instance(function.type, instance)
364 def interface_wrap_name(self, interface):
365 return "Wrap" + interface.expr
367 def interface_pre_decl(self, interface):
368 print "class %s;" % interface.wrap_name()
370 def interface_decl(self, interface):
371 print "class %s : public %s " % (interface.wrap_name(), interface.name)
374 print " %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
375 print " virtual ~%s();" % interface.wrap_name()
377 for method in interface.itermethods():
378 print " " + method.prototype() + ";"
381 print " %s * m_pInstance;" % (interface.name,)
385 def interface_wrap_impl(self, interface):
386 print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name)
387 print ' m_pInstance = pInstance;'
390 print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
393 for method in interface.itermethods():
394 self.trace_method(interface, method)
397 def trace_method(self, interface, method):
398 print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
399 if method.type is Void:
402 print ' %s __result;' % method.type
403 result = '__result = '
404 print ' Log::BeginCall("%s");' % (interface.name + '::' + method.name)
405 print ' Log::BeginArg(0, "this");'
406 print ' Log::LiteralOpaque((const void *)m_pInstance);'
407 print ' Log::EndArg();'
408 for arg in method.args:
410 self.unwrap_arg(method, arg)
411 self.dump_arg(method, arg)
412 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
413 for arg in method.args:
415 self.dump_arg(method, arg)
416 self.wrap_arg(method, arg)
417 if method.type is not Void:
418 print ' Log::BeginReturn("%s");' % method.type
419 dump_instance(method.type, "__result")
420 print ' Log::EndReturn();'
421 wrap_instance(method.type, '__result')
422 print ' Log::EndCall();'
423 if method.name == 'QueryInterface':
424 print ' if (*ppvObj == m_pInstance)'
425 print ' *ppvObj = this;'
426 if method.name == 'Release':
427 assert method.type is not Void
428 print ' if (!__result)'
429 print ' delete this;'
430 if method.type is not Void:
431 print ' return __result;'