blob: c7d18afae33239307fbda307556e8577bb99ec96 [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
José Fonseca885f2652010-11-20 11:22:25 +000057 def visit_blob(self, type, *args, **kwargs):
58 raise NotImplementedError
59
José Fonseca501f2862010-11-19 20:41:18 +000060 def visit_enum(self, type, *args, **kwargs):
61 raise NotImplementedError
62
63 def visit_bitmask(self, type, *args, **kwargs):
64 raise NotImplementedError
65
66 def visit_pointer(self, type, *args, **kwargs):
67 raise NotImplementedError
68
69 def visit_alias(self, type, *args, **kwargs):
70 raise NotImplementedError
71
72 def visit_opaque(self, type, *args, **kwargs):
73 raise NotImplementedError
74
75
José Fonsecac9edb832010-11-20 09:03:10 +000076class Rebuilder(Visitor):
77
78 def visit_void(self, void):
79 return void
80
81 def visit_literal(self, literal):
82 return literal
83
84 def visit_const(self, const):
85 return Const(const.type)
86
87 def visit_struct(self, struct):
88 members = [self.visit(member) for member in struct.members]
89 return Struct(struct.name, members)
90
91 def visit_array(self, array):
92 type = self.visit(array.type)
93 return Array(type, array.length)
94
José Fonseca885f2652010-11-20 11:22:25 +000095 def visit_blob(self, blob):
96 type = self.visit(blob.type)
97 return Blob(type, blob.size)
98
José Fonsecac9edb832010-11-20 09:03:10 +000099 def visit_enum(self, enum):
100 return enum
101
102 def visit_bitmask(self, bitmask):
103 type = self.visit(bitmask.type)
104 return Bitmask(type, bitmask.values)
105
106 def visit_pointer(self, pointer):
107 type = self.visit(pointer.type)
108 return Pointer(type)
109
110 def visit_alias(self, alias):
111 type = self.visit(alias.type)
112 return Alias(alias.expr, type)
113
114 def visit_opaque(self, opaque):
115 return opaque
116
117
José Fonsecad626cf42008-07-07 07:43:16 +0900118class Type:
119
José Fonsecae753ce82009-07-22 18:13:52 +0100120 __seq = 0
121
122 def __init__(self, expr, id = ''):
123 self.expr = expr
124
125 for char in id:
126 assert char.isalnum() or char in '_ '
127
128 id = id.replace(' ', '_')
129
130 if id in all_types:
131 Type.__seq += 1
132 id += str(Type.__seq)
133
134 assert id not in all_types
135 all_types[id] = self
136
137 self.id = id
José Fonsecad626cf42008-07-07 07:43:16 +0900138
139 def __str__(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100140 return self.expr
José Fonsecae9e1d062009-04-13 13:26:29 +0100141
José Fonseca501f2862010-11-19 20:41:18 +0000142 def visit(self, visitor, *args, **kwargs):
143 raise NotImplementedError
144
José Fonseca8a56d142008-07-09 12:18:08 +0900145 def decl(self):
146 pass
147
148 def impl(self):
149 pass
150
José Fonsecaa83fb242008-07-07 16:55:52 +0900151 def dump(self, instance):
152 raise NotImplementedError
153
José Fonsecad626cf42008-07-07 07:43:16 +0900154 def wrap_instance(self, instance):
José Fonseca8a56d142008-07-09 12:18:08 +0900155 pass
José Fonsecad626cf42008-07-07 07:43:16 +0900156
José Fonseca27cd25d2008-07-07 13:44:00 +0900157 def unwrap_instance(self, instance):
158 pass
159
José Fonsecad626cf42008-07-07 07:43:16 +0900160
José Fonseca8a56d142008-07-09 12:18:08 +0900161class _Void(Type):
José Fonsecad626cf42008-07-07 07:43:16 +0900162
163 def __init__(self):
164 Type.__init__(self, "void")
165
José Fonseca501f2862010-11-19 20:41:18 +0000166 def visit(self, visitor, *args, **kwargs):
167 return visitor.visit_void(self, *args, **kwargs)
168
José Fonseca8a56d142008-07-09 12:18:08 +0900169Void = _Void()
José Fonsecad626cf42008-07-07 07:43:16 +0900170
171
José Fonseca8a56d142008-07-09 12:18:08 +0900172class Concrete(Type):
José Fonsecad626cf42008-07-07 07:43:16 +0900173
José Fonseca8a56d142008-07-09 12:18:08 +0900174 def decl(self):
José Fonseca622af962009-09-27 19:13:58 +0100175 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
José Fonseca8a56d142008-07-09 12:18:08 +0900176
177 def impl(self):
José Fonseca622af962009-09-27 19:13:58 +0100178 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
José Fonseca8a56d142008-07-09 12:18:08 +0900179 self._dump("value");
180 print '}'
181 print
182
183 def _dump(self, instance):
184 raise NotImplementedError
185
186 def dump(self, instance):
José Fonsecae753ce82009-07-22 18:13:52 +0100187 print ' Dump%s(%s);' % (self.id, instance)
José Fonseca8a56d142008-07-09 12:18:08 +0900188
189
José Fonseca51c1ef82010-11-15 16:09:14 +0000190class Literal(Concrete):
José Fonseca8a56d142008-07-09 12:18:08 +0900191
José Fonseca51c1ef82010-11-15 16:09:14 +0000192 def __init__(self, expr, format, base=10):
José Fonsecae753ce82009-07-22 18:13:52 +0100193 Concrete.__init__(self, expr)
José Fonsecad626cf42008-07-07 07:43:16 +0900194 self.format = format
195
José Fonseca501f2862010-11-19 20:41:18 +0000196 def visit(self, visitor, *args, **kwargs):
197 return visitor.visit_literal(self, *args, **kwargs)
198
José Fonseca8a56d142008-07-09 12:18:08 +0900199 def _dump(self, instance):
José Fonseca51c1ef82010-11-15 16:09:14 +0000200 print ' Log::Literal%s(%s);' % (self.format, instance)
José Fonsecaa83fb242008-07-07 16:55:52 +0900201
José Fonsecad626cf42008-07-07 07:43:16 +0900202
203class Const(Type):
204
205 def __init__(self, type):
José Fonsecae753ce82009-07-22 18:13:52 +0100206
José Fonsecaf6592d72010-11-21 12:44:41 +0000207 if type.expr.startswith("const "):
José Fonsecae753ce82009-07-22 18:13:52 +0100208 expr = type.expr + " const"
209 else:
210 expr = "const " + type.expr
211
212 Type.__init__(self, expr, 'C' + type.id)
213
José Fonsecad626cf42008-07-07 07:43:16 +0900214 self.type = type
215
José Fonseca501f2862010-11-19 20:41:18 +0000216 def visit(self, visitor, *args, **kwargs):
217 return visitor.visit_const(self, *args, **kwargs)
218
José Fonsecaa83fb242008-07-07 16:55:52 +0900219 def dump(self, instance):
220 self.type.dump(instance)
221
José Fonsecad626cf42008-07-07 07:43:16 +0900222
223class Pointer(Type):
224
225 def __init__(self, type):
José Fonsecae753ce82009-07-22 18:13:52 +0100226 Type.__init__(self, type.expr + " *", 'P' + type.id)
José Fonsecad626cf42008-07-07 07:43:16 +0900227 self.type = type
228
José Fonseca501f2862010-11-19 20:41:18 +0000229 def visit(self, visitor, *args, **kwargs):
230 return visitor.visit_pointer(self, *args, **kwargs)
231
José Fonsecaa83fb242008-07-07 16:55:52 +0900232 def dump(self, instance):
José Fonseca4a9c40c2008-07-07 18:04:53 +0900233 print ' if(%s) {' % instance
José Fonsecac7337452010-11-15 22:10:53 +0000234 print ' Log::BeginPointer("%s", (const void *)%s);' % (self.type, instance)
José Fonsecaa83fb242008-07-07 16:55:52 +0900235 try:
José Fonsecaa83fb242008-07-07 16:55:52 +0900236 self.type.dump("*" + instance)
José Fonseca4a9c40c2008-07-07 18:04:53 +0900237 except NotImplementedError:
José Fonseca3bccbb12008-07-10 02:00:31 +0900238 pass
José Fonsecac7337452010-11-15 22:10:53 +0000239 print ' Log::EndPointer();'
José Fonseca4a9c40c2008-07-07 18:04:53 +0900240 print ' }'
241 print ' else'
José Fonseca51c1ef82010-11-15 16:09:14 +0000242 print ' Log::LiteralNull();'
José Fonsecad626cf42008-07-07 07:43:16 +0900243
244 def wrap_instance(self, instance):
245 self.type.wrap_instance("*" + instance)
246
José Fonseca27cd25d2008-07-07 13:44:00 +0900247 def unwrap_instance(self, instance):
248 self.type.wrap_instance("*" + instance)
249
José Fonsecad626cf42008-07-07 07:43:16 +0900250
José Fonsecab974caa2008-07-09 08:12:34 +0900251def ConstPointer(type):
252 return Pointer(Const(type))
253
254
José Fonseca8a56d142008-07-09 12:18:08 +0900255class Enum(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900256
257 def __init__(self, name, values):
José Fonseca8a56d142008-07-09 12:18:08 +0900258 Concrete.__init__(self, name)
José Fonsecad626cf42008-07-07 07:43:16 +0900259 self.values = values
José Fonsecaa83fb242008-07-07 16:55:52 +0900260
José Fonseca501f2862010-11-19 20:41:18 +0000261 def visit(self, visitor, *args, **kwargs):
262 return visitor.visit_enum(self, *args, **kwargs)
263
José Fonseca8a56d142008-07-09 12:18:08 +0900264 def _dump(self, instance):
José Fonsecaa83fb242008-07-07 16:55:52 +0900265 print ' switch(%s) {' % instance
266 for value in self.values:
267 print ' case %s:' % value
José Fonseca7e329022010-11-19 17:05:18 +0000268 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
José Fonsecaa83fb242008-07-07 16:55:52 +0900269 print ' break;'
270 print ' default:'
José Fonseca51c1ef82010-11-15 16:09:14 +0000271 print ' Log::LiteralSInt(%s);' % instance
José Fonsecaa83fb242008-07-07 16:55:52 +0900272 print ' break;'
273 print ' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900274
275
José Fonseca501f2862010-11-19 20:41:18 +0000276def FakeEnum(type, values):
277 return Enum(type.expr, values)
José Fonseca6edf23c2009-05-04 10:20:52 +0100278
279
José Fonseca501f2862010-11-19 20:41:18 +0000280class Bitmask(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900281
282 def __init__(self, type, values):
José Fonsecae753ce82009-07-22 18:13:52 +0100283 Concrete.__init__(self, type.expr)
José Fonsecaec61f312008-07-09 02:16:43 +0900284 self.type = type
José Fonsecad626cf42008-07-07 07:43:16 +0900285 self.values = values
286
José Fonseca501f2862010-11-19 20:41:18 +0000287 def visit(self, visitor, *args, **kwargs):
288 return visitor.visit_bitmask(self, *args, **kwargs)
289
José Fonseca8a56d142008-07-09 12:18:08 +0900290 def _dump(self, instance):
291 print ' %s l_Value = %s;' % (self.type, instance)
José Fonseca51c1ef82010-11-15 16:09:14 +0000292 print ' Log::BeginBitmask("%s");' % (self.type,)
José Fonsecaec61f312008-07-09 02:16:43 +0900293 for value in self.values:
José Fonseca8a56d142008-07-09 12:18:08 +0900294 print ' if((l_Value & %s) == %s) {' % (value, value)
José Fonseca7e329022010-11-19 17:05:18 +0000295 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
José Fonseca8a56d142008-07-09 12:18:08 +0900296 print ' l_Value &= ~%s;' % value
297 print ' }'
José Fonseca51c1ef82010-11-15 16:09:14 +0000298 print ' if(l_Value) {'
José Fonsecaec61f312008-07-09 02:16:43 +0900299 self.type.dump("l_Value");
José Fonseca47e85e12009-05-04 11:05:11 +0100300 print ' }'
José Fonseca51c1ef82010-11-15 16:09:14 +0000301 print ' Log::EndBitmask();'
José Fonsecaec61f312008-07-09 02:16:43 +0900302
José Fonseca501f2862010-11-19 20:41:18 +0000303Flags = Bitmask
304
José Fonsecad626cf42008-07-07 07:43:16 +0900305
José Fonsecaccae31c2009-07-22 18:14:12 +0100306class Array(Type):
307
308 def __init__(self, type, length):
José Fonseca885f2652010-11-20 11:22:25 +0000309 Type.__init__(self, type.expr + " *")
José Fonsecaccae31c2009-07-22 18:14:12 +0100310 self.type = type
311 self.length = length
312
José Fonseca501f2862010-11-19 20:41:18 +0000313 def visit(self, visitor, *args, **kwargs):
314 return visitor.visit_array(self, *args, **kwargs)
315
José Fonsecaccae31c2009-07-22 18:14:12 +0100316 def dump(self, instance):
317 index = '__i' + self.type.id
José Fonseca51c1ef82010-11-15 16:09:14 +0000318 print ' Log::BeginArray("%s", %s);' % (self.type, self.length)
José Fonseca4a5f33a2009-09-12 10:19:10 +0100319 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
José Fonsecaccae31c2009-07-22 18:14:12 +0100320 print ' Log::BeginElement("%s");' % (self.type,)
321 self.type.dump('(%s)[%s]' % (instance, index))
322 print ' Log::EndElement();'
323 print ' }'
José Fonseca51c1ef82010-11-15 16:09:14 +0000324 print ' Log::EndArray();'
José Fonsecaccae31c2009-07-22 18:14:12 +0100325
326 def wrap_instance(self, instance):
327 self.type.wrap_instance("*" + instance)
328
329 def unwrap_instance(self, instance):
330 self.type.wrap_instance("*" + instance)
331
332
José Fonseca885f2652010-11-20 11:22:25 +0000333class Blob(Type):
334
335 def __init__(self, type, size):
336 Type.__init__(self, type.expr + ' *')
337 self.type = type
338 self.size = size
339
340 def visit(self, visitor, *args, **kwargs):
341 return visitor.visit_blob(self, *args, **kwargs)
342
343 def dump(self, instance):
344 print ' Log::LiteralBlob(%s, %s);' % (instance, self.size)
345
346
José Fonseca8a56d142008-07-09 12:18:08 +0900347class Struct(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900348
349 def __init__(self, name, members):
José Fonseca8a56d142008-07-09 12:18:08 +0900350 Concrete.__init__(self, name)
José Fonseca51c1ef82010-11-15 16:09:14 +0000351 self.name = name
José Fonsecad626cf42008-07-07 07:43:16 +0900352 self.members = members
353
José Fonseca501f2862010-11-19 20:41:18 +0000354 def visit(self, visitor, *args, **kwargs):
355 return visitor.visit_struct(self, *args, **kwargs)
356
José Fonseca8a56d142008-07-09 12:18:08 +0900357 def _dump(self, instance):
José Fonseca51c1ef82010-11-15 16:09:14 +0000358 print ' Log::BeginStruct("%s");' % (self.name,)
José Fonsecaa83fb242008-07-07 16:55:52 +0900359 for type, name in self.members:
José Fonseca51c1ef82010-11-15 16:09:14 +0000360 print ' Log::BeginMember("%s", "%s");' % (type, name)
José Fonsecaa83fb242008-07-07 16:55:52 +0900361 type.dump('(%s).%s' % (instance, name))
José Fonseca51c1ef82010-11-15 16:09:14 +0000362 print ' Log::EndMember();'
363 print ' Log::EndStruct();'
José Fonsecaa83fb242008-07-07 16:55:52 +0900364
José Fonsecad626cf42008-07-07 07:43:16 +0900365
366class Alias(Type):
367
José Fonsecac9edb832010-11-20 09:03:10 +0000368 def __init__(self, expr, type):
369 Type.__init__(self, expr)
José Fonsecad626cf42008-07-07 07:43:16 +0900370 self.type = type
371
José Fonseca501f2862010-11-19 20:41:18 +0000372 def visit(self, visitor, *args, **kwargs):
373 return visitor.visit_alias(self, *args, **kwargs)
374
José Fonsecaa83fb242008-07-07 16:55:52 +0900375 def dump(self, instance):
376 self.type.dump(instance)
377
José Fonsecad626cf42008-07-07 07:43:16 +0900378
José Fonsecac9096f02010-11-22 13:02:26 +0000379def Out(type, name):
380 arg = Arg(type, name, output=True)
381 return arg
382
383
384class Arg:
385
386 def __init__(self, type, name, output=False):
387 self.type = type
388 self.name = name
389 self.output = output
390
391 def __str__(self):
392 return '%s %s' % (self.type, self.name)
José Fonseca83c9ac82010-01-28 14:45:36 +0000393
394
José Fonsecad626cf42008-07-07 07:43:16 +0900395class Function:
396
José Fonseca290c28c2009-04-23 15:20:29 +0100397 def __init__(self, type, name, args, call = '__stdcall', fail = None):
José Fonsecad626cf42008-07-07 07:43:16 +0900398 self.type = type
399 self.name = name
José Fonsecac9096f02010-11-22 13:02:26 +0000400
401 self.args = []
402 for arg in args:
403 if isinstance(arg, tuple):
404 arg_type, arg_name = arg
405 arg = Arg(arg_type, arg_name)
406 self.args.append(arg)
407
José Fonsecad626cf42008-07-07 07:43:16 +0900408 self.call = call
José Fonseca290c28c2009-04-23 15:20:29 +0100409 self.fail = fail
José Fonsecad626cf42008-07-07 07:43:16 +0900410
411 def prototype(self, name=None):
412 if name is not None:
413 name = name.strip()
414 else:
415 name = self.name
416 s = name
417 if self.call:
418 s = self.call + ' ' + s
419 if name.startswith('*'):
420 s = '(' + s + ')'
José Fonsecae753ce82009-07-22 18:13:52 +0100421 s = self.type.expr + ' ' + s
José Fonsecad626cf42008-07-07 07:43:16 +0900422 s += "("
423 if self.args:
José Fonsecac9096f02010-11-22 13:02:26 +0000424 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
José Fonsecad626cf42008-07-07 07:43:16 +0900425 else:
426 s += "void"
427 s += ")"
428 return s
429
José Fonseca3c2c9292009-05-04 12:16:30 +0100430 def pointer_type(self):
431 return 'P' + self.name
432
433 def pointer_value(self):
434 return 'p' + self.name
435
436 def wrap_decl(self):
437 ptype = self.pointer_type()
438 pvalue = self.pointer_value()
439 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
440 print 'static %s %s = NULL;' % (ptype, pvalue)
441 print
442
443 def get_true_pointer(self):
444 raise NotImplementedError
445
José Fonseca83178a02010-11-14 00:35:05 +0000446 def exit_impl(self):
447 print ' ExitProcess(0);'
448
José Fonseca3c2c9292009-05-04 12:16:30 +0100449 def fail_impl(self):
450 if self.fail is not None:
José Fonseca243772e2009-06-25 13:56:57 +0100451 if self.type is Void:
452 assert self.fail == ''
453 print ' return;'
454 else:
455 assert self.fail != ''
456 print ' return %s;' % self.fail
José Fonseca3c2c9292009-05-04 12:16:30 +0100457 else:
José Fonseca83178a02010-11-14 00:35:05 +0000458 self.exit_impl()
José Fonseca3c2c9292009-05-04 12:16:30 +0100459
460 def wrap_impl(self):
461 pvalue = self.pointer_value()
462 print self.prototype() + ' {'
463 if self.type is Void:
464 result = ''
465 else:
466 print ' %s result;' % self.type
467 result = 'result = '
468 self.get_true_pointer()
469 print ' Log::BeginCall("%s");' % (self.name)
José Fonsecac9096f02010-11-22 13:02:26 +0000470 for arg in self.args:
471 if not arg.output:
472 arg.type.unwrap_instance(arg.name)
473 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
474 arg.type.dump(arg.name)
José Fonseca3c2c9292009-05-04 12:16:30 +0100475 print ' Log::EndArg();'
José Fonsecac9096f02010-11-22 13:02:26 +0000476 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in self.args]))
477 for arg in self.args:
478 if arg.output:
479 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
480 arg.type.dump(arg.name)
José Fonseca3c2c9292009-05-04 12:16:30 +0100481 print ' Log::EndArg();'
José Fonsecac9096f02010-11-22 13:02:26 +0000482 arg.type.wrap_instance(arg.name)
José Fonseca3c2c9292009-05-04 12:16:30 +0100483 if self.type is not Void:
484 print ' Log::BeginReturn("%s");' % self.type
485 self.type.dump("result")
486 print ' Log::EndReturn();'
487 self.type.wrap_instance('result')
488 print ' Log::EndCall();'
José Fonsecac77023d2009-05-04 12:53:50 +0100489 self.post_call_impl()
José Fonseca3c2c9292009-05-04 12:16:30 +0100490 if self.type is not Void:
491 print ' return result;'
492 print '}'
493 print
494
José Fonsecac77023d2009-05-04 12:53:50 +0100495 def post_call_impl(self):
496 pass
497
José Fonsecad626cf42008-07-07 07:43:16 +0900498
499class Interface(Type):
500
501 def __init__(self, name, base=None):
502 Type.__init__(self, name)
José Fonsecae753ce82009-07-22 18:13:52 +0100503 self.name = name
José Fonsecad626cf42008-07-07 07:43:16 +0900504 self.base = base
505 self.methods = []
506
507 def itermethods(self):
508 if self.base is not None:
509 for method in self.base.itermethods():
510 yield method
511 for method in self.methods:
512 yield method
513 raise StopIteration
514
515 def wrap_name(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100516 return "Wrap" + self.expr
José Fonsecad626cf42008-07-07 07:43:16 +0900517
518 def wrap_pre_decl(self):
519 print "class %s;" % self.wrap_name()
520
521 def wrap_decl(self):
522 print "class %s : public %s " % (self.wrap_name(), self.name)
523 print "{"
524 print "public:"
525 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
526 print " virtual ~%s();" % self.wrap_name()
527 print
528 for method in self.itermethods():
529 print " " + method.prototype() + ";"
530 print
José Fonseca27cd25d2008-07-07 13:44:00 +0900531 #print "private:"
José Fonsecad626cf42008-07-07 07:43:16 +0900532 print " %s * m_pInstance;" % (self.name,)
533 print "};"
534 print
535
536 def wrap_impl(self):
537 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
José Fonsecad626cf42008-07-07 07:43:16 +0900538 print ' m_pInstance = pInstance;'
539 print '}'
540 print
541 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
José Fonsecad626cf42008-07-07 07:43:16 +0900542 print '}'
543 print
544 for method in self.itermethods():
545 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
546 if method.type is Void:
547 result = ''
548 else:
549 print ' %s result;' % method.type
550 result = 'result = '
José Fonseca22aec832008-07-09 09:38:45 +0900551 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900552 print ' Log::BeginArg("%s *", "this");' % self.name
José Fonsecac7337452010-11-15 22:10:53 +0000553 print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
554 print ' Log::EndPointer();'
José Fonseca3bccbb12008-07-10 02:00:31 +0900555 print ' Log::EndArg();'
José Fonsecac9096f02010-11-22 13:02:26 +0000556 for arg in method.args:
557 if not arg.output:
558 arg.type.unwrap_instance(arg.name)
559 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
560 arg.type.dump(arg.name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900561 print ' Log::EndArg();'
José Fonsecac9096f02010-11-22 13:02:26 +0000562 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
563 for arg in method.args:
564 if arg.output:
565 print ' Log::BeginArg("%s", "%s");' % (arg.type, arg.name)
566 arg.type.dump(arg.name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900567 print ' Log::EndArg();'
José Fonsecac9096f02010-11-22 13:02:26 +0000568 arg.type.wrap_instance(arg.name)
José Fonsecad626cf42008-07-07 07:43:16 +0900569 if method.type is not Void:
José Fonseca22aec832008-07-09 09:38:45 +0900570 print ' Log::BeginReturn("%s");' % method.type
José Fonseca4a9c40c2008-07-07 18:04:53 +0900571 method.type.dump("result")
José Fonseca22aec832008-07-09 09:38:45 +0900572 print ' Log::EndReturn();'
José Fonsecad626cf42008-07-07 07:43:16 +0900573 method.type.wrap_instance('result')
José Fonseca22aec832008-07-09 09:38:45 +0900574 print ' Log::EndCall();'
José Fonsecad626cf42008-07-07 07:43:16 +0900575 if method.name == 'QueryInterface':
576 print ' if(*ppvObj == m_pInstance)'
577 print ' *ppvObj = this;'
578 if method.name == 'Release':
579 assert method.type is not Void
580 print ' if(!result)'
581 print ' delete this;'
582 if method.type is not Void:
583 print ' return result;'
584 print '}'
585 print
586 print
587
588
589class Method(Function):
590
591 def __init__(self, type, name, args):
José Fonseca51c1ef82010-11-15 16:09:14 +0000592 Function.__init__(self, type, name, args, call = '__stdcall')
José Fonsecad626cf42008-07-07 07:43:16 +0900593
594
595towrap = []
596
597class WrapPointer(Pointer):
598
599 def __init__(self, type):
600 Pointer.__init__(self, type)
601 if type not in towrap:
602 towrap.append(type)
603
604 def wrap_instance(self, instance):
605 print " if(%s)" % instance
606 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
607
José Fonseca27cd25d2008-07-07 13:44:00 +0900608 def unwrap_instance(self, instance):
609 print " if(%s)" % instance
610 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
611
José Fonseca8a56d142008-07-09 12:18:08 +0900612
613class _String(Type):
614
615 def __init__(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100616 Type.__init__(self, "char *")
José Fonseca8a56d142008-07-09 12:18:08 +0900617
José Fonseca501f2862010-11-19 20:41:18 +0000618 def visit(self, visitor, *args, **kwargs):
619 return visitor.visit_literal(self, *args, **kwargs)
620
José Fonseca8a56d142008-07-09 12:18:08 +0900621 def dump(self, instance):
José Fonseca51c1ef82010-11-15 16:09:14 +0000622 print ' Log::LiteralString((const char *)%s);' % instance
José Fonseca8a56d142008-07-09 12:18:08 +0900623
624String = _String()
625
José Fonseca51c1ef82010-11-15 16:09:14 +0000626
José Fonsecaf6592d72010-11-21 12:44:41 +0000627class Opaque(Type):
628 '''Opaque pointer.'''
José Fonsecae54e4112009-06-25 13:56:18 +0100629
José Fonsecaf6592d72010-11-21 12:44:41 +0000630 def __init__(self, expr):
631 Type.__init__(self, expr)
José Fonsecae54e4112009-06-25 13:56:18 +0100632
José Fonseca501f2862010-11-19 20:41:18 +0000633 def visit(self, visitor, *args, **kwargs):
634 return visitor.visit_opaque(self, *args, **kwargs)
635
José Fonsecae54e4112009-06-25 13:56:18 +0100636 def dump(self, instance):
José Fonsecaf6592d72010-11-21 12:44:41 +0000637 print ' Log::LiteralOpaque((const void *)%s);' % instance
José Fonsecae54e4112009-06-25 13:56:18 +0100638
José Fonsecaf6592d72010-11-21 12:44:41 +0000639
640def OpaquePointer(type):
641 return Opaque(type.expr + ' *')
José Fonsecae54e4112009-06-25 13:56:18 +0100642
José Fonseca8a56d142008-07-09 12:18:08 +0900643
José Fonseca51c1ef82010-11-15 16:09:14 +0000644Bool = Literal("bool", "Bool")
645SChar = Literal("signed char", "SInt")
646UChar = Literal("unsigned char", "UInt")
647Short = Literal("short", "SInt")
648Int = Literal("int", "SInt")
649Long = Literal("long", "SInt")
650LongLong = Literal("long long", "SInt")
651UShort = Literal("unsigned short", "UInt")
652UInt = Literal("unsigned int", "UInt")
653ULong = Literal("unsigned long", "UInt")
654Float = Literal("float", "Float")
655Double = Literal("double", "Float")
656SizeT = Literal("size_t", "UInt")
657WString = Literal("wchar_t *", "WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900658
659
660def wrap():
José Fonseca8a56d142008-07-09 12:18:08 +0900661 for type in all_types.itervalues():
662 type.decl()
663 print
664 for type in all_types.itervalues():
665 type.impl()
666 print
José Fonsecad626cf42008-07-07 07:43:16 +0900667 for type in towrap:
668 type.wrap_pre_decl()
669 print
670 for type in towrap:
671 type.wrap_decl()
672 print
673 for type in towrap:
674 type.wrap_impl()
675 print