]> git.cworth.org Git - apitrace/blob - trace.py
Less opaqueness.
[apitrace] / trace.py
1 ##########################################################################
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
29 import base
30
31
32 all_types = {}
33
34
35 class 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
101     def visit_handle(self, handle):
102         self.visit(handle.type)
103
104     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
114 class 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         if string.length is not None:
122             print '    Log::LiteralString((const char *)%s, %s);' % (instance, string.length)
123         else:
124             print '    Log::LiteralString((const char *)%s);' % instance
125
126     def visit_const(self, const, instance):
127         self.visit(const.type, instance)
128
129     def visit_struct(self, struct, instance):
130         print '    __traceStruct%s(%s);' % (struct.id, instance)
131
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();'
140         print '        }'
141         print '        Log::EndArray();'
142         print '    }'
143         print '    else'
144         print '        Log::LiteralNull();'
145
146     def visit_blob(self, blob, instance):
147         print '    Log::LiteralBlob(%s, %s);' % (instance, blob.size)
148
149     def visit_enum(self, enum, instance):
150         print '    __traceEnum%s(%s);' % (enum.id, instance)
151
152     def visit_bitmask(self, bitmask, instance):
153         print '    __traceBitmask%s(%s);' % (bitmask.id, instance)
154
155     def visit_pointer(self, pointer, instance):
156         print '    if(%s) {' % instance
157         print '        Log::BeginPointer((const void *)%s);' % (instance,)
158         dump_instance(pointer.type, "*" + instance)
159         print '        Log::EndPointer();'
160         print '    }'
161         print '    else'
162         print '        Log::LiteralNull();'
163
164     def visit_handle(self, handle, instance):
165         self.visit(handle.type, instance)
166
167     def visit_alias(self, alias, instance):
168         self.visit(alias.type, instance)
169
170     def visit_opaque(self, opaque, instance):
171         print '    Log::LiteralOpaque((const void *)%s);' % instance
172
173     def visit_interface(self, interface, instance):
174         print '    Log::LiteralOpaque((const void *)%s);' % instance
175
176
177 dump_instance = DumpImplementer().visit
178
179
180
181 class Wrapper(base.Visitor):
182     '''Wrap an instance.'''
183
184     def visit_void(self, type, instance):
185         raise NotImplementedError
186
187     def visit_literal(self, type, instance):
188         pass
189
190     def visit_string(self, type, instance):
191         pass
192
193     def visit_const(self, type, instance):
194         pass
195
196     def visit_struct(self, struct, instance):
197         for type, name in struct.members:
198             self.visit(type, "(%s).%s" % (instance, name))
199
200     def visit_array(self, array, instance):
201         # XXX: actually it is possible to return an array of pointers
202         pass
203
204     def visit_blob(self, blob, instance):
205         pass
206
207     def visit_enum(self, enum, instance):
208         pass
209
210     def visit_bitmask(self, bitmask, instance):
211         pass
212
213     def visit_pointer(self, pointer, instance):
214         self.visit(pointer.type, "*" + instance)
215
216     def visit_handle(self, handle, instance):
217         self.visit(handle.type, instance)
218
219     def visit_alias(self, alias, instance):
220         self.visit(alias.type, instance)
221
222     def visit_opaque(self, opaque, instance):
223         pass
224     
225     def visit_interface(self, interface, instance):
226         print "    if(%s)" % instance
227         print "        %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
228
229
230 class Unwrapper(Wrapper):
231
232     def visit_interface(self, interface, instance):
233         print "    if(%s)" % instance
234         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance)
235
236 wrap_instance = Wrapper().visit
237 unwrap_instance = Unwrapper().visit
238
239
240 class Tracer:
241
242     def trace_api(self, api):
243         self.header(api)
244
245         # Includes
246         for header in api.headers:
247             print header
248         print
249
250         # Type dumpers
251         types = api.all_types()
252         visitor = DumpDeclarator()
253         map(visitor.visit, types)
254         print
255
256         # Interfaces wrapers
257         map(self.interface_wrap_name, api.interfaces)
258         map(self.interface_pre_decl, api.interfaces)
259         map(self.interface_decl, api.interfaces)
260         map(self.interface_wrap_impl, api.interfaces)
261         print
262
263         # Function wrappers
264         map(self.trace_function_decl, api.functions)
265         map(self.trace_function_impl, api.functions)
266         print
267
268         self.footer(api)
269
270     def header(self, api):
271         pass
272
273     def footer(self, api):
274         pass
275
276     def function_pointer_type(self, function):
277         return 'P' + function.name
278
279     def function_pointer_value(self, function):
280         return 'p' + function.name
281
282     def trace_function_decl(self, function):
283         ptype = self.function_pointer_type(function)
284         pvalue = self.function_pointer_value(function)
285         print 'typedef ' + function.prototype('* %s' % ptype) + ';'
286         print 'static %s %s = NULL;' % (ptype, pvalue)
287         print
288
289     def trace_function_fail(self, function):
290         if function.fail is not None:
291             if function.type is base.Void:
292                 assert function.fail == ''
293                 print '            return;' 
294             else:
295                 assert function.fail != ''
296                 print '            return %s;' % function.fail
297         else:
298             print '            Log::Abort();'
299
300     def get_function_address(self, function):
301         raise NotImplementedError
302
303     def _get_true_pointer(self, function):
304         ptype = self.function_pointer_type(function)
305         pvalue = self.function_pointer_value(function)
306         print '    if(!%s) {' % (pvalue,)
307         print '        %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
308         print '        if(!%s)' % (pvalue,)
309         self.trace_function_fail(function)
310         print '    }'
311
312     def trace_function_impl(self, function):
313         pvalue = self.function_pointer_value(function)
314         print function.prototype() + ' {'
315         if function.type is base.Void:
316             result = ''
317         else:
318             print '    %s __result;' % function.type
319             result = '__result = '
320         self._get_true_pointer(function)
321         print '    Log::BeginCall("%s");' % (function.name)
322         for arg in function.args:
323             if not arg.output:
324                 self.unwrap_arg(function, arg)
325                 self.dump_arg(function, arg)
326         print '    %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
327         for arg in function.args:
328             if arg.output:
329                 self.dump_arg(function, arg)
330                 self.wrap_arg(function, arg)
331         if function.type is not base.Void:
332             self.dump_ret(function, "__result")
333         print '    Log::EndCall();'
334         if function.type is not base.Void:
335             self.wrap_ret(function, "__result")
336             print '    return __result;'
337         print '}'
338         print
339
340     def dump_arg(self, function, arg):
341         print '    Log::BeginArg("%s");' % (arg.name,)
342         dump_instance(arg.type, arg.name)
343         print '    Log::EndArg();'
344
345     def wrap_arg(self, function, arg):
346         wrap_instance(arg.type, arg.name)
347
348     def unwrap_arg(self, function, arg):
349         unwrap_instance(arg.type, arg.name)
350
351     def dump_ret(self, function, instance):
352         print '    Log::BeginReturn();'
353         dump_instance(function.type, instance)
354         print '    Log::EndReturn();'
355
356     def wrap_ret(self, function, instance):
357         wrap_instance(function.type, instance)
358
359     def unwrap_ret(self, function, instance):
360         unwrap_instance(function.type, instance)
361
362     def interface_wrap_name(self, interface):
363         return "Wrap" + interface.expr
364
365     def interface_pre_decl(self, interface):
366         print "class %s;" % interface.wrap_name()
367
368     def interface_decl(self, interface):
369         print "class %s : public %s " % (interface.wrap_name(), interface.name)
370         print "{"
371         print "public:"
372         print "    %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
373         print "    virtual ~%s();" % interface.wrap_name()
374         print
375         for method in interface.itermethods():
376             print "    " + method.prototype() + ";"
377         print
378         #print "private:"
379         print "    %s * m_pInstance;" % (interface.name,)
380         print "};"
381         print
382
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;'
386         print '}'
387         print
388         print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
389         print '}'
390         print
391         for method in interface.itermethods():
392             self.trace_method(interface, method)
393         print
394
395     def trace_method(self, interface, method):
396         print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
397         if method.type is Void:
398             result = ''
399         else:
400             print '    %s __result;' % method.type
401             result = '__result = '
402         print '    Log::BeginCall("%s");' % (interface.name + '::' + method.name)
403         print '    Log::BeginArg("this");'
404         print '    Log::LiteralOpaque((const void *)m_pInstance);'
405         print '    Log::EndArg();'
406         for arg in method.args:
407             if not arg.output:
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:
412             if arg.output:
413                 self.dump_arg(method, arg)
414                 self.wrap_arg(method, arg)
415         if method.type is not Void:
416             print '    Log::BeginReturn("%s");' % method.type
417             dump_instance(method.type, "__result")
418             print '    Log::EndReturn();'
419             wrap_instance(method.type, '__result')
420         print '    Log::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;'
430         print '}'
431         print
432
433