blob: ab2382fa5b9e43e712ed94d80860203d1ea1743e [file] [log] [blame]
José Fonseca7ad40262009-09-30 17:17:12 +01001##########################################################################
José Fonseca95442442008-07-08 10:32:53 +09002#
José Fonseca7ad40262009-09-30 17:17:12 +01003# Copyright 2008-2009 VMware, Inc.
4# All Rights Reserved.
José Fonseca95442442008-07-08 10:32:53 +09005#
José Fonseca7ad40262009-09-30 17:17:12 +01006# 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:
José Fonseca95442442008-07-08 10:32:53 +090012#
José Fonseca7ad40262009-09-30 17:17:12 +010013# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
José Fonseca95442442008-07-08 10:32:53 +090015#
José Fonseca7ad40262009-09-30 17:17:12 +010016# 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.
José Fonseca95442442008-07-08 10:32:53 +090023#
José Fonseca7ad40262009-09-30 17:17:12 +010024##########################################################################/
José Fonseca95442442008-07-08 10:32:53 +090025
José Fonsecad626cf42008-07-07 07:43:16 +090026"""C basic types"""
27
José Fonseca8a56d142008-07-09 12:18:08 +090028
29import debug
30
31
32all_types = {}
33
José Fonseca501f2862010-11-19 20:41:18 +000034
35class Visitor:
36
37 def visit(self, type, *args, **kwargs):
38 return type.visit(self, *args, **kwargs)
39
40 __call__ = visit
41
42 def visit_void(self, type, *args, **kwargs):
43 raise NotImplementedError
44
45 def visit_literal(self, type, *args, **kwargs):
46 raise NotImplementedError
47
48 def visit_const(self, type, *args, **kwargs):
49 raise NotImplementedError
50
51 def visit_struct(self, type, *args, **kwargs):
52 raise NotImplementedError
53
54 def visit_array(self, type, *args, **kwargs):
55 raise NotImplementedError
56
57 def visit_enum(self, type, *args, **kwargs):
58 raise NotImplementedError
59
60 def visit_bitmask(self, type, *args, **kwargs):
61 raise NotImplementedError
62
63 def visit_pointer(self, type, *args, **kwargs):
64 raise NotImplementedError
65
66 def visit_alias(self, type, *args, **kwargs):
67 raise NotImplementedError
68
69 def visit_opaque(self, type, *args, **kwargs):
70 raise NotImplementedError
71
72
José Fonsecad626cf42008-07-07 07:43:16 +090073class Type:
74
José Fonsecae753ce82009-07-22 18:13:52 +010075 __seq = 0
76
77 def __init__(self, expr, id = ''):
78 self.expr = expr
79
80 for char in id:
81 assert char.isalnum() or char in '_ '
82
83 id = id.replace(' ', '_')
84
85 if id in all_types:
86 Type.__seq += 1
87 id += str(Type.__seq)
88
89 assert id not in all_types
90 all_types[id] = self
91
92 self.id = id
José Fonsecad626cf42008-07-07 07:43:16 +090093
94 def __str__(self):
José Fonsecae753ce82009-07-22 18:13:52 +010095 return self.expr
José Fonsecae9e1d062009-04-13 13:26:29 +010096
José Fonseca501f2862010-11-19 20:41:18 +000097 def visit(self, visitor, *args, **kwargs):
98 raise NotImplementedError
99
José Fonsecad626cf42008-07-07 07:43:16 +0900100 def isoutput(self):
101 return False
102
José Fonseca8a56d142008-07-09 12:18:08 +0900103 def decl(self):
104 pass
105
106 def impl(self):
107 pass
108
José Fonsecaa83fb242008-07-07 16:55:52 +0900109 def dump(self, instance):
110 raise NotImplementedError
111
José Fonsecad626cf42008-07-07 07:43:16 +0900112 def wrap_instance(self, instance):
José Fonseca8a56d142008-07-09 12:18:08 +0900113 pass
José Fonsecad626cf42008-07-07 07:43:16 +0900114
José Fonseca27cd25d2008-07-07 13:44:00 +0900115 def unwrap_instance(self, instance):
116 pass
117
José Fonsecad626cf42008-07-07 07:43:16 +0900118
José Fonseca8a56d142008-07-09 12:18:08 +0900119class _Void(Type):
José Fonsecad626cf42008-07-07 07:43:16 +0900120
121 def __init__(self):
122 Type.__init__(self, "void")
123
José Fonseca501f2862010-11-19 20:41:18 +0000124 def visit(self, visitor, *args, **kwargs):
125 return visitor.visit_void(self, *args, **kwargs)
126
José Fonseca8a56d142008-07-09 12:18:08 +0900127Void = _Void()
José Fonsecad626cf42008-07-07 07:43:16 +0900128
129
José Fonseca8a56d142008-07-09 12:18:08 +0900130class Concrete(Type):
José Fonsecad626cf42008-07-07 07:43:16 +0900131
José Fonseca8a56d142008-07-09 12:18:08 +0900132 def decl(self):
José Fonseca622af962009-09-27 19:13:58 +0100133 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
José Fonseca8a56d142008-07-09 12:18:08 +0900134
135 def impl(self):
José Fonseca622af962009-09-27 19:13:58 +0100136 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
José Fonseca8a56d142008-07-09 12:18:08 +0900137 self._dump("value");
138 print '}'
139 print
140
141 def _dump(self, instance):
142 raise NotImplementedError
143
144 def dump(self, instance):
José Fonsecae753ce82009-07-22 18:13:52 +0100145 print ' Dump%s(%s);' % (self.id, instance)
José Fonseca8a56d142008-07-09 12:18:08 +0900146
147
José Fonseca51c1ef82010-11-15 16:09:14 +0000148class Literal(Concrete):
José Fonseca8a56d142008-07-09 12:18:08 +0900149
José Fonseca51c1ef82010-11-15 16:09:14 +0000150 def __init__(self, expr, format, base=10):
José Fonsecae753ce82009-07-22 18:13:52 +0100151 Concrete.__init__(self, expr)
José Fonsecad626cf42008-07-07 07:43:16 +0900152 self.format = format
153
José Fonseca501f2862010-11-19 20:41:18 +0000154 def visit(self, visitor, *args, **kwargs):
155 return visitor.visit_literal(self, *args, **kwargs)
156
José Fonseca8a56d142008-07-09 12:18:08 +0900157 def _dump(self, instance):
José Fonseca51c1ef82010-11-15 16:09:14 +0000158 print ' Log::Literal%s(%s);' % (self.format, instance)
José Fonsecaa83fb242008-07-07 16:55:52 +0900159
José Fonsecad626cf42008-07-07 07:43:16 +0900160
161class Const(Type):
162
163 def __init__(self, type):
José Fonsecae753ce82009-07-22 18:13:52 +0100164
165 if isinstance(type, Pointer):
166 expr = type.expr + " const"
167 else:
168 expr = "const " + type.expr
169
170 Type.__init__(self, expr, 'C' + type.id)
171
José Fonsecad626cf42008-07-07 07:43:16 +0900172 self.type = type
173
José Fonseca501f2862010-11-19 20:41:18 +0000174 def visit(self, visitor, *args, **kwargs):
175 return visitor.visit_const(self, *args, **kwargs)
176
José Fonsecaa83fb242008-07-07 16:55:52 +0900177 def dump(self, instance):
178 self.type.dump(instance)
179
José Fonsecad626cf42008-07-07 07:43:16 +0900180
181class Pointer(Type):
182
183 def __init__(self, type):
José Fonsecae753ce82009-07-22 18:13:52 +0100184 Type.__init__(self, type.expr + " *", 'P' + type.id)
José Fonsecad626cf42008-07-07 07:43:16 +0900185 self.type = type
186
José Fonseca501f2862010-11-19 20:41:18 +0000187 def visit(self, visitor, *args, **kwargs):
188 return visitor.visit_pointer(self, *args, **kwargs)
189
José Fonsecaa83fb242008-07-07 16:55:52 +0900190 def dump(self, instance):
José Fonseca4a9c40c2008-07-07 18:04:53 +0900191 print ' if(%s) {' % instance
José Fonsecac7337452010-11-15 22:10:53 +0000192 print ' Log::BeginPointer("%s", (const void *)%s);' % (self.type, instance)
José Fonsecaa83fb242008-07-07 16:55:52 +0900193 try:
José Fonsecaa83fb242008-07-07 16:55:52 +0900194 self.type.dump("*" + instance)
José Fonseca4a9c40c2008-07-07 18:04:53 +0900195 except NotImplementedError:
José Fonseca3bccbb12008-07-10 02:00:31 +0900196 pass
José Fonsecac7337452010-11-15 22:10:53 +0000197 print ' Log::EndPointer();'
José Fonseca4a9c40c2008-07-07 18:04:53 +0900198 print ' }'
199 print ' else'
José Fonseca51c1ef82010-11-15 16:09:14 +0000200 print ' Log::LiteralNull();'
José Fonsecad626cf42008-07-07 07:43:16 +0900201
202 def wrap_instance(self, instance):
203 self.type.wrap_instance("*" + instance)
204
José Fonseca27cd25d2008-07-07 13:44:00 +0900205 def unwrap_instance(self, instance):
206 self.type.wrap_instance("*" + instance)
207
José Fonsecad626cf42008-07-07 07:43:16 +0900208
José Fonsecab974caa2008-07-09 08:12:34 +0900209def ConstPointer(type):
210 return Pointer(Const(type))
211
212
José Fonsecaa83fb242008-07-07 16:55:52 +0900213class OutPointer(Pointer):
José Fonsecad626cf42008-07-07 07:43:16 +0900214
215 def isoutput(self):
216 return True
217
José Fonsecad626cf42008-07-07 07:43:16 +0900218
José Fonseca8a56d142008-07-09 12:18:08 +0900219class Enum(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900220
221 def __init__(self, name, values):
José Fonseca8a56d142008-07-09 12:18:08 +0900222 Concrete.__init__(self, name)
José Fonsecad626cf42008-07-07 07:43:16 +0900223 self.values = values
José Fonsecaa83fb242008-07-07 16:55:52 +0900224
José Fonseca501f2862010-11-19 20:41:18 +0000225 def visit(self, visitor, *args, **kwargs):
226 return visitor.visit_enum(self, *args, **kwargs)
227
José Fonseca8a56d142008-07-09 12:18:08 +0900228 def _dump(self, instance):
José Fonsecaa83fb242008-07-07 16:55:52 +0900229 print ' switch(%s) {' % instance
230 for value in self.values:
231 print ' case %s:' % value
José Fonseca7e329022010-11-19 17:05:18 +0000232 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
José Fonsecaa83fb242008-07-07 16:55:52 +0900233 print ' break;'
234 print ' default:'
José Fonseca51c1ef82010-11-15 16:09:14 +0000235 print ' Log::LiteralSInt(%s);' % instance
José Fonsecaa83fb242008-07-07 16:55:52 +0900236 print ' break;'
237 print ' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900238
239
José Fonseca501f2862010-11-19 20:41:18 +0000240def FakeEnum(type, values):
241 return Enum(type.expr, values)
José Fonseca6edf23c2009-05-04 10:20:52 +0100242
243
José Fonseca501f2862010-11-19 20:41:18 +0000244class Bitmask(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900245
246 def __init__(self, type, values):
José Fonsecae753ce82009-07-22 18:13:52 +0100247 Concrete.__init__(self, type.expr)
José Fonsecaec61f312008-07-09 02:16:43 +0900248 self.type = type
José Fonsecad626cf42008-07-07 07:43:16 +0900249 self.values = values
250
José Fonseca501f2862010-11-19 20:41:18 +0000251 def visit(self, visitor, *args, **kwargs):
252 return visitor.visit_bitmask(self, *args, **kwargs)
253
José Fonseca8a56d142008-07-09 12:18:08 +0900254 def _dump(self, instance):
255 print ' %s l_Value = %s;' % (self.type, instance)
José Fonseca51c1ef82010-11-15 16:09:14 +0000256 print ' Log::BeginBitmask("%s");' % (self.type,)
José Fonsecaec61f312008-07-09 02:16:43 +0900257 for value in self.values:
José Fonseca8a56d142008-07-09 12:18:08 +0900258 print ' if((l_Value & %s) == %s) {' % (value, value)
José Fonseca7e329022010-11-19 17:05:18 +0000259 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
José Fonseca8a56d142008-07-09 12:18:08 +0900260 print ' l_Value &= ~%s;' % value
261 print ' }'
José Fonseca51c1ef82010-11-15 16:09:14 +0000262 print ' if(l_Value) {'
José Fonsecaec61f312008-07-09 02:16:43 +0900263 self.type.dump("l_Value");
José Fonseca47e85e12009-05-04 11:05:11 +0100264 print ' }'
José Fonseca51c1ef82010-11-15 16:09:14 +0000265 print ' Log::EndBitmask();'
José Fonsecaec61f312008-07-09 02:16:43 +0900266
José Fonseca501f2862010-11-19 20:41:18 +0000267Flags = Bitmask
268
José Fonsecad626cf42008-07-07 07:43:16 +0900269
José Fonsecaccae31c2009-07-22 18:14:12 +0100270class Array(Type):
271
272 def __init__(self, type, length):
273 Type.__init__(self, type.expr + " *", 'P' + type.id)
274 self.type = type
275 self.length = length
276
José Fonseca501f2862010-11-19 20:41:18 +0000277 def visit(self, visitor, *args, **kwargs):
278 return visitor.visit_array(self, *args, **kwargs)
279
José Fonsecaccae31c2009-07-22 18:14:12 +0100280 def dump(self, instance):
281 index = '__i' + self.type.id
José Fonseca51c1ef82010-11-15 16:09:14 +0000282 print ' Log::BeginArray("%s", %s);' % (self.type, self.length)
José Fonseca4a5f33a2009-09-12 10:19:10 +0100283 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
José Fonsecaccae31c2009-07-22 18:14:12 +0100284 print ' Log::BeginElement("%s");' % (self.type,)
285 self.type.dump('(%s)[%s]' % (instance, index))
286 print ' Log::EndElement();'
287 print ' }'
José Fonseca51c1ef82010-11-15 16:09:14 +0000288 print ' Log::EndArray();'
José Fonsecaccae31c2009-07-22 18:14:12 +0100289
290 def wrap_instance(self, instance):
291 self.type.wrap_instance("*" + instance)
292
293 def unwrap_instance(self, instance):
294 self.type.wrap_instance("*" + instance)
295
296
José Fonseca83c9ac82010-01-28 14:45:36 +0000297class OutArray(Array):
298
299 def isoutput(self):
300 return True
301
302
José Fonseca8a56d142008-07-09 12:18:08 +0900303class Struct(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900304
305 def __init__(self, name, members):
José Fonseca8a56d142008-07-09 12:18:08 +0900306 Concrete.__init__(self, name)
José Fonseca51c1ef82010-11-15 16:09:14 +0000307 self.name = name
José Fonsecad626cf42008-07-07 07:43:16 +0900308 self.members = members
309
José Fonseca501f2862010-11-19 20:41:18 +0000310 def visit(self, visitor, *args, **kwargs):
311 return visitor.visit_struct(self, *args, **kwargs)
312
José Fonseca8a56d142008-07-09 12:18:08 +0900313 def _dump(self, instance):
José Fonseca51c1ef82010-11-15 16:09:14 +0000314 print ' Log::BeginStruct("%s");' % (self.name,)
José Fonsecaa83fb242008-07-07 16:55:52 +0900315 for type, name in self.members:
José Fonseca51c1ef82010-11-15 16:09:14 +0000316 print ' Log::BeginMember("%s", "%s");' % (type, name)
José Fonsecaa83fb242008-07-07 16:55:52 +0900317 type.dump('(%s).%s' % (instance, name))
José Fonseca51c1ef82010-11-15 16:09:14 +0000318 print ' Log::EndMember();'
319 print ' Log::EndStruct();'
José Fonsecaa83fb242008-07-07 16:55:52 +0900320
José Fonsecad626cf42008-07-07 07:43:16 +0900321
322class Alias(Type):
323
324 def __init__(self, name, type):
325 Type.__init__(self, name)
326 self.type = type
327
José Fonseca501f2862010-11-19 20:41:18 +0000328 def visit(self, visitor, *args, **kwargs):
329 return visitor.visit_alias(self, *args, **kwargs)
330
José Fonsecaa83fb242008-07-07 16:55:52 +0900331 def dump(self, instance):
332 self.type.dump(instance)
333
José Fonsecad626cf42008-07-07 07:43:16 +0900334
José Fonseca83c9ac82010-01-28 14:45:36 +0000335class Out(Type):
336
337 def __init__(self, type):
338 Type.__init__(self, type.expr)
339 self.type = type
340
341 def isoutput(self):
342 return True
343
344 def decl(self):
345 self.type.decl()
346
347 def impl(self):
348 self.type.impl()
349
350 def dump(self, instance):
351 self.type.dump(instance)
352
353 def wrap_instance(self, instance):
354 self.type.wrap_instance(instance)
355
356 def unwrap_instance(self, instance):
357 self.type.unwrap_instance(instance)
358
359
José Fonsecad626cf42008-07-07 07:43:16 +0900360class Function:
361
José Fonseca290c28c2009-04-23 15:20:29 +0100362 def __init__(self, type, name, args, call = '__stdcall', fail = None):
José Fonsecad626cf42008-07-07 07:43:16 +0900363 self.type = type
364 self.name = name
365 self.args = args
366 self.call = call
José Fonseca290c28c2009-04-23 15:20:29 +0100367 self.fail = fail
José Fonsecad626cf42008-07-07 07:43:16 +0900368
369 def prototype(self, name=None):
370 if name is not None:
371 name = name.strip()
372 else:
373 name = self.name
374 s = name
375 if self.call:
376 s = self.call + ' ' + s
377 if name.startswith('*'):
378 s = '(' + s + ')'
José Fonsecae753ce82009-07-22 18:13:52 +0100379 s = self.type.expr + ' ' + s
José Fonsecad626cf42008-07-07 07:43:16 +0900380 s += "("
381 if self.args:
382 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
383 else:
384 s += "void"
385 s += ")"
386 return s
387
José Fonseca3c2c9292009-05-04 12:16:30 +0100388 def pointer_type(self):
389 return 'P' + self.name
390
391 def pointer_value(self):
392 return 'p' + self.name
393
394 def wrap_decl(self):
395 ptype = self.pointer_type()
396 pvalue = self.pointer_value()
397 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
398 print 'static %s %s = NULL;' % (ptype, pvalue)
399 print
400
401 def get_true_pointer(self):
402 raise NotImplementedError
403
José Fonseca83178a02010-11-14 00:35:05 +0000404 def exit_impl(self):
405 print ' ExitProcess(0);'
406
José Fonseca3c2c9292009-05-04 12:16:30 +0100407 def fail_impl(self):
408 if self.fail is not None:
José Fonseca243772e2009-06-25 13:56:57 +0100409 if self.type is Void:
410 assert self.fail == ''
411 print ' return;'
412 else:
413 assert self.fail != ''
414 print ' return %s;' % self.fail
José Fonseca3c2c9292009-05-04 12:16:30 +0100415 else:
José Fonseca83178a02010-11-14 00:35:05 +0000416 self.exit_impl()
José Fonseca3c2c9292009-05-04 12:16:30 +0100417
418 def wrap_impl(self):
419 pvalue = self.pointer_value()
420 print self.prototype() + ' {'
421 if self.type is Void:
422 result = ''
423 else:
424 print ' %s result;' % self.type
425 result = 'result = '
426 self.get_true_pointer()
427 print ' Log::BeginCall("%s");' % (self.name)
428 for type, name in self.args:
429 if not type.isoutput():
430 type.unwrap_instance(name)
431 print ' Log::BeginArg("%s", "%s");' % (type, name)
432 type.dump(name)
433 print ' Log::EndArg();'
434 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
435 for type, name in self.args:
436 if type.isoutput():
437 print ' Log::BeginArg("%s", "%s");' % (type, name)
438 type.dump(name)
439 print ' Log::EndArg();'
440 type.wrap_instance(name)
441 if self.type is not Void:
442 print ' Log::BeginReturn("%s");' % self.type
443 self.type.dump("result")
444 print ' Log::EndReturn();'
445 self.type.wrap_instance('result')
446 print ' Log::EndCall();'
José Fonsecac77023d2009-05-04 12:53:50 +0100447 self.post_call_impl()
José Fonseca3c2c9292009-05-04 12:16:30 +0100448 if self.type is not Void:
449 print ' return result;'
450 print '}'
451 print
452
José Fonsecac77023d2009-05-04 12:53:50 +0100453 def post_call_impl(self):
454 pass
455
José Fonsecad626cf42008-07-07 07:43:16 +0900456
457class Interface(Type):
458
459 def __init__(self, name, base=None):
460 Type.__init__(self, name)
José Fonsecae753ce82009-07-22 18:13:52 +0100461 self.name = name
José Fonsecad626cf42008-07-07 07:43:16 +0900462 self.base = base
463 self.methods = []
464
465 def itermethods(self):
466 if self.base is not None:
467 for method in self.base.itermethods():
468 yield method
469 for method in self.methods:
470 yield method
471 raise StopIteration
472
473 def wrap_name(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100474 return "Wrap" + self.expr
José Fonsecad626cf42008-07-07 07:43:16 +0900475
476 def wrap_pre_decl(self):
477 print "class %s;" % self.wrap_name()
478
479 def wrap_decl(self):
480 print "class %s : public %s " % (self.wrap_name(), self.name)
481 print "{"
482 print "public:"
483 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
484 print " virtual ~%s();" % self.wrap_name()
485 print
486 for method in self.itermethods():
487 print " " + method.prototype() + ";"
488 print
José Fonseca27cd25d2008-07-07 13:44:00 +0900489 #print "private:"
José Fonsecad626cf42008-07-07 07:43:16 +0900490 print " %s * m_pInstance;" % (self.name,)
491 print "};"
492 print
493
494 def wrap_impl(self):
495 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
José Fonsecad626cf42008-07-07 07:43:16 +0900496 print ' m_pInstance = pInstance;'
497 print '}'
498 print
499 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
José Fonsecad626cf42008-07-07 07:43:16 +0900500 print '}'
501 print
502 for method in self.itermethods():
503 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
504 if method.type is Void:
505 result = ''
506 else:
507 print ' %s result;' % method.type
508 result = 'result = '
José Fonseca22aec832008-07-09 09:38:45 +0900509 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900510 print ' Log::BeginArg("%s *", "this");' % self.name
José Fonsecac7337452010-11-15 22:10:53 +0000511 print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
512 print ' Log::EndPointer();'
José Fonseca3bccbb12008-07-10 02:00:31 +0900513 print ' Log::EndArg();'
José Fonseca27cd25d2008-07-07 13:44:00 +0900514 for type, name in method.args:
515 if not type.isoutput():
516 type.unwrap_instance(name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900517 print ' Log::BeginArg("%s", "%s");' % (type, name)
José Fonsecaa83fb242008-07-07 16:55:52 +0900518 type.dump(name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900519 print ' Log::EndArg();'
José Fonsecad626cf42008-07-07 07:43:16 +0900520 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
521 for type, name in method.args:
522 if type.isoutput():
José Fonseca3bccbb12008-07-10 02:00:31 +0900523 print ' Log::BeginArg("%s", "%s");' % (type, name)
José Fonseca22aa6882008-07-07 17:33:30 +0900524 type.dump(name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900525 print ' Log::EndArg();'
José Fonsecad626cf42008-07-07 07:43:16 +0900526 type.wrap_instance(name)
527 if method.type is not Void:
José Fonseca22aec832008-07-09 09:38:45 +0900528 print ' Log::BeginReturn("%s");' % method.type
José Fonseca4a9c40c2008-07-07 18:04:53 +0900529 method.type.dump("result")
José Fonseca22aec832008-07-09 09:38:45 +0900530 print ' Log::EndReturn();'
José Fonsecad626cf42008-07-07 07:43:16 +0900531 method.type.wrap_instance('result')
José Fonseca22aec832008-07-09 09:38:45 +0900532 print ' Log::EndCall();'
José Fonsecad626cf42008-07-07 07:43:16 +0900533 if method.name == 'QueryInterface':
534 print ' if(*ppvObj == m_pInstance)'
535 print ' *ppvObj = this;'
536 if method.name == 'Release':
537 assert method.type is not Void
538 print ' if(!result)'
539 print ' delete this;'
540 if method.type is not Void:
541 print ' return result;'
542 print '}'
543 print
544 print
545
546
547class Method(Function):
548
549 def __init__(self, type, name, args):
José Fonseca51c1ef82010-11-15 16:09:14 +0000550 Function.__init__(self, type, name, args, call = '__stdcall')
José Fonsecad626cf42008-07-07 07:43:16 +0900551
552
553towrap = []
554
555class WrapPointer(Pointer):
556
557 def __init__(self, type):
558 Pointer.__init__(self, type)
559 if type not in towrap:
560 towrap.append(type)
561
562 def wrap_instance(self, instance):
563 print " if(%s)" % instance
564 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
565
José Fonseca27cd25d2008-07-07 13:44:00 +0900566 def unwrap_instance(self, instance):
567 print " if(%s)" % instance
568 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
569
José Fonseca8a56d142008-07-09 12:18:08 +0900570
571class _String(Type):
572
573 def __init__(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100574 Type.__init__(self, "char *")
José Fonseca8a56d142008-07-09 12:18:08 +0900575
José Fonseca501f2862010-11-19 20:41:18 +0000576 def visit(self, visitor, *args, **kwargs):
577 return visitor.visit_literal(self, *args, **kwargs)
578
José Fonseca8a56d142008-07-09 12:18:08 +0900579 def dump(self, instance):
José Fonseca51c1ef82010-11-15 16:09:14 +0000580 print ' Log::LiteralString((const char *)%s);' % instance
José Fonseca8a56d142008-07-09 12:18:08 +0900581
582String = _String()
583
José Fonseca51c1ef82010-11-15 16:09:14 +0000584
585class _Opaque(Type):
José Fonsecae54e4112009-06-25 13:56:18 +0100586
587 def __init__(self):
José Fonsecac7337452010-11-15 22:10:53 +0000588 Type.__init__(self, "void")
José Fonsecae54e4112009-06-25 13:56:18 +0100589
José Fonseca501f2862010-11-19 20:41:18 +0000590 def visit(self, visitor, *args, **kwargs):
591 return visitor.visit_opaque(self, *args, **kwargs)
592
José Fonsecae54e4112009-06-25 13:56:18 +0100593 def dump(self, instance):
José Fonsecac7337452010-11-15 22:10:53 +0000594 print ' Log::LiteralOpaque();'
José Fonsecae54e4112009-06-25 13:56:18 +0100595
José Fonsecac7337452010-11-15 22:10:53 +0000596Opaque = Pointer(_Opaque())
José Fonsecae54e4112009-06-25 13:56:18 +0100597
José Fonseca8a56d142008-07-09 12:18:08 +0900598
José Fonseca51c1ef82010-11-15 16:09:14 +0000599Bool = Literal("bool", "Bool")
600SChar = Literal("signed char", "SInt")
601UChar = Literal("unsigned char", "UInt")
602Short = Literal("short", "SInt")
603Int = Literal("int", "SInt")
604Long = Literal("long", "SInt")
605LongLong = Literal("long long", "SInt")
606UShort = Literal("unsigned short", "UInt")
607UInt = Literal("unsigned int", "UInt")
608ULong = Literal("unsigned long", "UInt")
609Float = Literal("float", "Float")
610Double = Literal("double", "Float")
611SizeT = Literal("size_t", "UInt")
612WString = Literal("wchar_t *", "WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900613
614
615def wrap():
José Fonseca8a56d142008-07-09 12:18:08 +0900616 for type in all_types.itervalues():
617 type.decl()
618 print
619 for type in all_types.itervalues():
620 type.impl()
621 print
José Fonsecad626cf42008-07-07 07:43:16 +0900622 for type in towrap:
623 type.wrap_pre_decl()
624 print
625 for type in towrap:
626 type.wrap_decl()
627 print
628 for type in towrap:
629 type.wrap_impl()
630 print