blob: a866f2a6c8012393c8510299aa7571bc0d8d47e1 [file] [log] [blame]
José Fonseca8fbdd3a2010-11-23 20:55:07 +00001##########################################################################
2#
3# Copyright 2008-2010 VMware, Inc.
4# All Rights Reserved.
5#
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:
12#
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15#
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
22# THE SOFTWARE.
23#
24##########################################################################/
25
26"""C basic types"""
27
28
29import base
30
31
32all_types = {}
33
34
35class DumpDeclarator(base.OnceVisitor):
36 '''Declare helper functions to dump complex types.'''
37
38 def visit_void(self, literal):
39 pass
40
41 def visit_literal(self, literal):
42 pass
43
44 def visit_string(self, string):
45 pass
46
47 def visit_const(self, const):
48 self.visit(const.type)
49
50 def visit_struct(self, struct):
51 for type, name in struct.members:
52 self.visit(type)
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();'
60 print '}'
61 print
62
63 def visit_array(self, array):
64 self.visit(array.type)
65
66 def visit_blob(self, array):
67 pass
68
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)
75 print ' break;'
76 print ' default:'
77 print ' Log::LiteralSInt(value);'
78 print ' break;'
79 print ' }'
80 print '}'
81 print
82
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
90 print ' }'
91 print ' if(value) {'
92 dump_instance(bitmask.type, "value");
93 print ' }'
94 print ' Log::EndBitmask();'
95 print '}'
96 print
97
98 def visit_pointer(self, pointer):
99 self.visit(pointer.type)
100
José Fonseca50d78d82010-11-23 22:13:14 +0000101 def visit_handle(self, handle):
102 self.visit(handle.type)
103
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000104 def visit_alias(self, alias):
105 self.visit(alias.type)
106
107 def visit_opaque(self, opaque):
108 pass
109
110 def visit_interface(self, interface):
111 pass
112
113
114class DumpImplementer(base.Visitor):
115 '''Dump an instance.'''
116
117 def visit_literal(self, literal, instance):
118 print ' Log::Literal%s(%s);' % (literal.format, instance)
119
120 def visit_string(self, string, instance):
121 print ' Log::LiteralString((const char *)%s);' % instance
122
123 def visit_const(self, const, instance):
124 self.visit(const.type, instance)
125
126 def visit_struct(self, struct, instance):
127 print ' __traceStruct%s(%s);' % (struct.id, instance)
128
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();'
137 print ' }'
138 print ' Log::EndArray();'
139 print ' }'
140 print ' else'
141 print ' Log::LiteralNull();'
142
143 def visit_blob(self, blob, instance):
144 print ' Log::LiteralBlob(%s, %s);' % (instance, blob.size)
145
146 def visit_enum(self, enum, instance):
147 print ' __traceEnum%s(%s);' % (enum.id, instance)
148
149 def visit_bitmask(self, bitmask, instance):
150 print ' __traceBitmask%s(%s);' % (bitmask.id, instance)
151
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();'
157 print ' }'
158 print ' else'
159 print ' Log::LiteralNull();'
160
José Fonseca50d78d82010-11-23 22:13:14 +0000161 def visit_handle(self, handle, instance):
162 self.visit(handle.type, instance)
163
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000164 def visit_alias(self, alias, instance):
165 self.visit(alias.type, instance)
166
167 def visit_opaque(self, opaque, instance):
168 print ' Log::LiteralOpaque((const void *)%s);' % instance
169
170 def visit_interface(self, interface, instance):
171 print ' Log::LiteralOpaque((const void *)%s);' % instance
172
173
174dump_instance = DumpImplementer().visit
175
176
177
178class Wrapper(base.Visitor):
179 '''Wrap an instance.'''
180
181 def visit_void(self, type, instance):
182 raise NotImplementedError
183
184 def visit_literal(self, type, instance):
185 pass
186
187 def visit_string(self, type, instance):
188 pass
189
190 def visit_const(self, type, instance):
191 pass
192
193 def visit_struct(self, struct, instance):
194 for type, name in struct.members:
195 self.visit(type, "(%s).%s" % (instance, name))
196
197 def visit_array(self, array, instance):
198 # XXX: actually it is possible to return an array of pointers
199 pass
200
201 def visit_blob(self, blob, instance):
202 pass
203
204 def visit_enum(self, enum, instance):
205 pass
206
207 def visit_bitmask(self, bitmask, instance):
208 pass
209
210 def visit_pointer(self, pointer, instance):
211 self.visit(pointer.type, "*" + instance)
212
José Fonseca50d78d82010-11-23 22:13:14 +0000213 def visit_handle(self, handle, instance):
214 self.visit(handle.type, instance)
215
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000216 def visit_alias(self, alias, instance):
217 self.visit(alias.type, instance)
218
219 def visit_opaque(self, opaque, instance):
220 pass
221
222 def visit_interface(self, interface, instance):
223 print " if(%s)" % instance
224 print " %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
225
226
227class Unwrapper(Wrapper):
228
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)
232
233wrap_instance = Wrapper().visit
234unwrap_instance = Unwrapper().visit
235
236
237class Tracer:
238
239 def trace_api(self, api):
240 self.header(api)
241
242 # Includes
243 for header in api.headers:
244 print '#include <%s>' % header
245 print
246
247 # Type dumpers
248 visitor = DumpDeclarator()
249 map(visitor.visit, api.types)
250 print
251
252 # Interfaces wrapers
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)
257 print
258
259 # Function wrappers
260 map(self.trace_function_decl, api.functions)
261 map(self.trace_function_impl, api.functions)
262 print
263
264 self.footer(api)
265
266 def header(self, api):
267 pass
268
269 def footer(self, api):
270 pass
271
272 def function_pointer_type(self, function):
273 return 'P' + function.name
274
275 def function_pointer_value(self, function):
276 return 'p' + function.name
277
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)
283 print
284
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 == ''
289 print ' return;'
290 else:
291 assert function.fail != ''
292 print ' return %s;' % function.fail
293 else:
294 print ' Log::Abort();'
295
296 def get_function_address(self, function):
297 raise NotImplementedError
298
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)
306 print ' }'
307
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:
312 result = ''
313 else:
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:
319 if not arg.output:
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:
324 if arg.output:
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;'
333 print '}'
334 print
335
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();'
340
341 def wrap_arg(self, function, arg):
342 wrap_instance(arg.type, arg.name)
343
344 def unwrap_arg(self, function, arg):
345 unwrap_instance(arg.type, arg.name)
346
347 def dump_ret(self, function, instance):
348 print ' Log::BeginReturn();'
349 dump_instance(function.type, instance)
350 print ' Log::EndReturn();'
351
352 def wrap_ret(self, function, instance):
353 wrap_instance(function.type, instance)
354
355 def unwrap_ret(self, function, instance):
356 unwrap_instance(function.type, instance)
357
358 def interface_wrap_name(self, interface):
359 return "Wrap" + interface.expr
360
361 def interface_pre_decl(self, interface):
362 print "class %s;" % interface.wrap_name()
363
364 def interface_decl(self, interface):
365 print "class %s : public %s " % (interface.wrap_name(), interface.name)
366 print "{"
367 print "public:"
368 print " %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
369 print " virtual ~%s();" % interface.wrap_name()
370 print
371 for method in interface.itermethods():
372 print " " + method.prototype() + ";"
373 print
374 #print "private:"
375 print " %s * m_pInstance;" % (interface.name,)
376 print "};"
377 print
378
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;'
382 print '}'
383 print
384 print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
385 print '}'
386 print
387 for method in interface.itermethods():
388 self.trace_method(interface, method)
389 print
390
391 def trace_method(self, interface, method):
392 print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
393 if method.type is Void:
394 result = ''
395 else:
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:
403 if not arg.output:
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:
408 if arg.output:
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;'
426 print '}'
427 print
428
429