blob: 9b5c6b6aff461da03d0673c32207cf96b37ebf1d [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
101 def visit_alias(self, alias):
102 self.visit(alias.type)
103
104 def visit_opaque(self, opaque):
105 pass
106
107 def visit_interface(self, interface):
108 pass
109
110
111class DumpImplementer(base.Visitor):
112 '''Dump an instance.'''
113
114 def visit_literal(self, literal, instance):
115 print ' Log::Literal%s(%s);' % (literal.format, instance)
116
117 def visit_string(self, string, instance):
118 print ' Log::LiteralString((const char *)%s);' % instance
119
120 def visit_const(self, const, instance):
121 self.visit(const.type, instance)
122
123 def visit_struct(self, struct, instance):
124 print ' __traceStruct%s(%s);' % (struct.id, instance)
125
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();'
134 print ' }'
135 print ' Log::EndArray();'
136 print ' }'
137 print ' else'
138 print ' Log::LiteralNull();'
139
140 def visit_blob(self, blob, instance):
141 print ' Log::LiteralBlob(%s, %s);' % (instance, blob.size)
142
143 def visit_enum(self, enum, instance):
144 print ' __traceEnum%s(%s);' % (enum.id, instance)
145
146 def visit_bitmask(self, bitmask, instance):
147 print ' __traceBitmask%s(%s);' % (bitmask.id, instance)
148
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();'
154 print ' }'
155 print ' else'
156 print ' Log::LiteralNull();'
157
158 def visit_alias(self, alias, instance):
159 self.visit(alias.type, instance)
160
161 def visit_opaque(self, opaque, instance):
162 print ' Log::LiteralOpaque((const void *)%s);' % instance
163
164 def visit_interface(self, interface, instance):
165 print ' Log::LiteralOpaque((const void *)%s);' % instance
166
167
168dump_instance = DumpImplementer().visit
169
170
171
172class Wrapper(base.Visitor):
173 '''Wrap an instance.'''
174
175 def visit_void(self, type, instance):
176 raise NotImplementedError
177
178 def visit_literal(self, type, instance):
179 pass
180
181 def visit_string(self, type, instance):
182 pass
183
184 def visit_const(self, type, instance):
185 pass
186
187 def visit_struct(self, struct, instance):
188 for type, name in struct.members:
189 self.visit(type, "(%s).%s" % (instance, name))
190
191 def visit_array(self, array, instance):
192 # XXX: actually it is possible to return an array of pointers
193 pass
194
195 def visit_blob(self, blob, instance):
196 pass
197
198 def visit_enum(self, enum, instance):
199 pass
200
201 def visit_bitmask(self, bitmask, instance):
202 pass
203
204 def visit_pointer(self, pointer, instance):
205 self.visit(pointer.type, "*" + instance)
206
207 def visit_alias(self, alias, instance):
208 self.visit(alias.type, instance)
209
210 def visit_opaque(self, opaque, instance):
211 pass
212
213 def visit_interface(self, interface, instance):
214 print " if(%s)" % instance
215 print " %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
216
217
218class Unwrapper(Wrapper):
219
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)
223
224wrap_instance = Wrapper().visit
225unwrap_instance = Unwrapper().visit
226
227
228class Tracer:
229
230 def trace_api(self, api):
231 self.header(api)
232
233 # Includes
234 for header in api.headers:
235 print '#include <%s>' % header
236 print
237
238 # Type dumpers
239 visitor = DumpDeclarator()
240 map(visitor.visit, api.types)
241 print
242
243 # Interfaces wrapers
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)
248 print
249
250 # Function wrappers
251 map(self.trace_function_decl, api.functions)
252 map(self.trace_function_impl, api.functions)
253 print
254
255 self.footer(api)
256
257 def header(self, api):
258 pass
259
260 def footer(self, api):
261 pass
262
263 def function_pointer_type(self, function):
264 return 'P' + function.name
265
266 def function_pointer_value(self, function):
267 return 'p' + function.name
268
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)
274 print
275
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 == ''
280 print ' return;'
281 else:
282 assert function.fail != ''
283 print ' return %s;' % function.fail
284 else:
285 print ' Log::Abort();'
286
287 def get_function_address(self, function):
288 raise NotImplementedError
289
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)
297 print ' }'
298
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:
303 result = ''
304 else:
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:
310 if not arg.output:
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:
315 if arg.output:
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;'
324 print '}'
325 print
326
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();'
331
332 def wrap_arg(self, function, arg):
333 wrap_instance(arg.type, arg.name)
334
335 def unwrap_arg(self, function, arg):
336 unwrap_instance(arg.type, arg.name)
337
338 def dump_ret(self, function, instance):
339 print ' Log::BeginReturn();'
340 dump_instance(function.type, instance)
341 print ' Log::EndReturn();'
342
343 def wrap_ret(self, function, instance):
344 wrap_instance(function.type, instance)
345
346 def unwrap_ret(self, function, instance):
347 unwrap_instance(function.type, instance)
348
349 def interface_wrap_name(self, interface):
350 return "Wrap" + interface.expr
351
352 def interface_pre_decl(self, interface):
353 print "class %s;" % interface.wrap_name()
354
355 def interface_decl(self, interface):
356 print "class %s : public %s " % (interface.wrap_name(), interface.name)
357 print "{"
358 print "public:"
359 print " %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
360 print " virtual ~%s();" % interface.wrap_name()
361 print
362 for method in interface.itermethods():
363 print " " + method.prototype() + ";"
364 print
365 #print "private:"
366 print " %s * m_pInstance;" % (interface.name,)
367 print "};"
368 print
369
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;'
373 print '}'
374 print
375 print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
376 print '}'
377 print
378 for method in interface.itermethods():
379 self.trace_method(interface, method)
380 print
381
382 def trace_method(self, interface, method):
383 print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
384 if method.type is Void:
385 result = ''
386 else:
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:
394 if not arg.output:
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:
399 if arg.output:
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;'
417 print '}'
418 print
419
420