blob: 2f2b1d0bd10e041e01c6c3a63c366b94957609e6 [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é Fonsecac9edb832010-11-20 09:03:10 +000073class Rebuilder(Visitor):
74
75 def visit_void(self, void):
76 return void
77
78 def visit_literal(self, literal):
79 return literal
80
81 def visit_const(self, const):
82 return Const(const.type)
83
84 def visit_struct(self, struct):
85 members = [self.visit(member) for member in struct.members]
86 return Struct(struct.name, members)
87
88 def visit_array(self, array):
89 type = self.visit(array.type)
90 return Array(type, array.length)
91
92 def visit_enum(self, enum):
93 return enum
94
95 def visit_bitmask(self, bitmask):
96 type = self.visit(bitmask.type)
97 return Bitmask(type, bitmask.values)
98
99 def visit_pointer(self, pointer):
100 type = self.visit(pointer.type)
101 return Pointer(type)
102
103 def visit_alias(self, alias):
104 type = self.visit(alias.type)
105 return Alias(alias.expr, type)
106
107 def visit_opaque(self, opaque):
108 return opaque
109
110
José Fonsecad626cf42008-07-07 07:43:16 +0900111class Type:
112
José Fonsecae753ce82009-07-22 18:13:52 +0100113 __seq = 0
114
115 def __init__(self, expr, id = ''):
116 self.expr = expr
117
118 for char in id:
119 assert char.isalnum() or char in '_ '
120
121 id = id.replace(' ', '_')
122
123 if id in all_types:
124 Type.__seq += 1
125 id += str(Type.__seq)
126
127 assert id not in all_types
128 all_types[id] = self
129
130 self.id = id
José Fonsecad626cf42008-07-07 07:43:16 +0900131
132 def __str__(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100133 return self.expr
José Fonsecae9e1d062009-04-13 13:26:29 +0100134
José Fonseca501f2862010-11-19 20:41:18 +0000135 def visit(self, visitor, *args, **kwargs):
136 raise NotImplementedError
137
José Fonsecad626cf42008-07-07 07:43:16 +0900138 def isoutput(self):
139 return False
140
José Fonseca8a56d142008-07-09 12:18:08 +0900141 def decl(self):
142 pass
143
144 def impl(self):
145 pass
146
José Fonsecaa83fb242008-07-07 16:55:52 +0900147 def dump(self, instance):
148 raise NotImplementedError
149
José Fonsecad626cf42008-07-07 07:43:16 +0900150 def wrap_instance(self, instance):
José Fonseca8a56d142008-07-09 12:18:08 +0900151 pass
José Fonsecad626cf42008-07-07 07:43:16 +0900152
José Fonseca27cd25d2008-07-07 13:44:00 +0900153 def unwrap_instance(self, instance):
154 pass
155
José Fonsecad626cf42008-07-07 07:43:16 +0900156
José Fonseca8a56d142008-07-09 12:18:08 +0900157class _Void(Type):
José Fonsecad626cf42008-07-07 07:43:16 +0900158
159 def __init__(self):
160 Type.__init__(self, "void")
161
José Fonseca501f2862010-11-19 20:41:18 +0000162 def visit(self, visitor, *args, **kwargs):
163 return visitor.visit_void(self, *args, **kwargs)
164
José Fonseca8a56d142008-07-09 12:18:08 +0900165Void = _Void()
José Fonsecad626cf42008-07-07 07:43:16 +0900166
167
José Fonseca8a56d142008-07-09 12:18:08 +0900168class Concrete(Type):
José Fonsecad626cf42008-07-07 07:43:16 +0900169
José Fonseca8a56d142008-07-09 12:18:08 +0900170 def decl(self):
José Fonseca622af962009-09-27 19:13:58 +0100171 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
José Fonseca8a56d142008-07-09 12:18:08 +0900172
173 def impl(self):
José Fonseca622af962009-09-27 19:13:58 +0100174 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
José Fonseca8a56d142008-07-09 12:18:08 +0900175 self._dump("value");
176 print '}'
177 print
178
179 def _dump(self, instance):
180 raise NotImplementedError
181
182 def dump(self, instance):
José Fonsecae753ce82009-07-22 18:13:52 +0100183 print ' Dump%s(%s);' % (self.id, instance)
José Fonseca8a56d142008-07-09 12:18:08 +0900184
185
José Fonseca51c1ef82010-11-15 16:09:14 +0000186class Literal(Concrete):
José Fonseca8a56d142008-07-09 12:18:08 +0900187
José Fonseca51c1ef82010-11-15 16:09:14 +0000188 def __init__(self, expr, format, base=10):
José Fonsecae753ce82009-07-22 18:13:52 +0100189 Concrete.__init__(self, expr)
José Fonsecad626cf42008-07-07 07:43:16 +0900190 self.format = format
191
José Fonseca501f2862010-11-19 20:41:18 +0000192 def visit(self, visitor, *args, **kwargs):
193 return visitor.visit_literal(self, *args, **kwargs)
194
José Fonseca8a56d142008-07-09 12:18:08 +0900195 def _dump(self, instance):
José Fonseca51c1ef82010-11-15 16:09:14 +0000196 print ' Log::Literal%s(%s);' % (self.format, instance)
José Fonsecaa83fb242008-07-07 16:55:52 +0900197
José Fonsecad626cf42008-07-07 07:43:16 +0900198
199class Const(Type):
200
201 def __init__(self, type):
José Fonsecae753ce82009-07-22 18:13:52 +0100202
203 if isinstance(type, Pointer):
204 expr = type.expr + " const"
205 else:
206 expr = "const " + type.expr
207
208 Type.__init__(self, expr, 'C' + type.id)
209
José Fonsecad626cf42008-07-07 07:43:16 +0900210 self.type = type
211
José Fonseca501f2862010-11-19 20:41:18 +0000212 def visit(self, visitor, *args, **kwargs):
213 return visitor.visit_const(self, *args, **kwargs)
214
José Fonsecaa83fb242008-07-07 16:55:52 +0900215 def dump(self, instance):
216 self.type.dump(instance)
217
José Fonsecad626cf42008-07-07 07:43:16 +0900218
219class Pointer(Type):
220
221 def __init__(self, type):
José Fonsecae753ce82009-07-22 18:13:52 +0100222 Type.__init__(self, type.expr + " *", 'P' + type.id)
José Fonsecad626cf42008-07-07 07:43:16 +0900223 self.type = type
224
José Fonseca501f2862010-11-19 20:41:18 +0000225 def visit(self, visitor, *args, **kwargs):
226 return visitor.visit_pointer(self, *args, **kwargs)
227
José Fonsecaa83fb242008-07-07 16:55:52 +0900228 def dump(self, instance):
José Fonseca4a9c40c2008-07-07 18:04:53 +0900229 print ' if(%s) {' % instance
José Fonsecac7337452010-11-15 22:10:53 +0000230 print ' Log::BeginPointer("%s", (const void *)%s);' % (self.type, instance)
José Fonsecaa83fb242008-07-07 16:55:52 +0900231 try:
José Fonsecaa83fb242008-07-07 16:55:52 +0900232 self.type.dump("*" + instance)
José Fonseca4a9c40c2008-07-07 18:04:53 +0900233 except NotImplementedError:
José Fonseca3bccbb12008-07-10 02:00:31 +0900234 pass
José Fonsecac7337452010-11-15 22:10:53 +0000235 print ' Log::EndPointer();'
José Fonseca4a9c40c2008-07-07 18:04:53 +0900236 print ' }'
237 print ' else'
José Fonseca51c1ef82010-11-15 16:09:14 +0000238 print ' Log::LiteralNull();'
José Fonsecad626cf42008-07-07 07:43:16 +0900239
240 def wrap_instance(self, instance):
241 self.type.wrap_instance("*" + instance)
242
José Fonseca27cd25d2008-07-07 13:44:00 +0900243 def unwrap_instance(self, instance):
244 self.type.wrap_instance("*" + instance)
245
José Fonsecad626cf42008-07-07 07:43:16 +0900246
José Fonsecab974caa2008-07-09 08:12:34 +0900247def ConstPointer(type):
248 return Pointer(Const(type))
249
250
José Fonsecaa83fb242008-07-07 16:55:52 +0900251class OutPointer(Pointer):
José Fonsecad626cf42008-07-07 07:43:16 +0900252
253 def isoutput(self):
254 return True
255
José Fonsecad626cf42008-07-07 07:43:16 +0900256
José Fonseca8a56d142008-07-09 12:18:08 +0900257class Enum(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900258
259 def __init__(self, name, values):
José Fonseca8a56d142008-07-09 12:18:08 +0900260 Concrete.__init__(self, name)
José Fonsecad626cf42008-07-07 07:43:16 +0900261 self.values = values
José Fonsecaa83fb242008-07-07 16:55:52 +0900262
José Fonseca501f2862010-11-19 20:41:18 +0000263 def visit(self, visitor, *args, **kwargs):
264 return visitor.visit_enum(self, *args, **kwargs)
265
José Fonseca8a56d142008-07-09 12:18:08 +0900266 def _dump(self, instance):
José Fonsecaa83fb242008-07-07 16:55:52 +0900267 print ' switch(%s) {' % instance
268 for value in self.values:
269 print ' case %s:' % value
José Fonseca7e329022010-11-19 17:05:18 +0000270 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
José Fonsecaa83fb242008-07-07 16:55:52 +0900271 print ' break;'
272 print ' default:'
José Fonseca51c1ef82010-11-15 16:09:14 +0000273 print ' Log::LiteralSInt(%s);' % instance
José Fonsecaa83fb242008-07-07 16:55:52 +0900274 print ' break;'
275 print ' }'
José Fonsecad626cf42008-07-07 07:43:16 +0900276
277
José Fonseca501f2862010-11-19 20:41:18 +0000278def FakeEnum(type, values):
279 return Enum(type.expr, values)
José Fonseca6edf23c2009-05-04 10:20:52 +0100280
281
José Fonseca501f2862010-11-19 20:41:18 +0000282class Bitmask(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900283
284 def __init__(self, type, values):
José Fonsecae753ce82009-07-22 18:13:52 +0100285 Concrete.__init__(self, type.expr)
José Fonsecaec61f312008-07-09 02:16:43 +0900286 self.type = type
José Fonsecad626cf42008-07-07 07:43:16 +0900287 self.values = values
288
José Fonseca501f2862010-11-19 20:41:18 +0000289 def visit(self, visitor, *args, **kwargs):
290 return visitor.visit_bitmask(self, *args, **kwargs)
291
José Fonseca8a56d142008-07-09 12:18:08 +0900292 def _dump(self, instance):
293 print ' %s l_Value = %s;' % (self.type, instance)
José Fonseca51c1ef82010-11-15 16:09:14 +0000294 print ' Log::BeginBitmask("%s");' % (self.type,)
José Fonsecaec61f312008-07-09 02:16:43 +0900295 for value in self.values:
José Fonseca8a56d142008-07-09 12:18:08 +0900296 print ' if((l_Value & %s) == %s) {' % (value, value)
José Fonseca7e329022010-11-19 17:05:18 +0000297 print ' Log::LiteralNamedConstant("%s", %s);' % (value, value)
José Fonseca8a56d142008-07-09 12:18:08 +0900298 print ' l_Value &= ~%s;' % value
299 print ' }'
José Fonseca51c1ef82010-11-15 16:09:14 +0000300 print ' if(l_Value) {'
José Fonsecaec61f312008-07-09 02:16:43 +0900301 self.type.dump("l_Value");
José Fonseca47e85e12009-05-04 11:05:11 +0100302 print ' }'
José Fonseca51c1ef82010-11-15 16:09:14 +0000303 print ' Log::EndBitmask();'
José Fonsecaec61f312008-07-09 02:16:43 +0900304
José Fonseca501f2862010-11-19 20:41:18 +0000305Flags = Bitmask
306
José Fonsecad626cf42008-07-07 07:43:16 +0900307
José Fonsecaccae31c2009-07-22 18:14:12 +0100308class Array(Type):
309
310 def __init__(self, type, length):
311 Type.__init__(self, type.expr + " *", 'P' + type.id)
312 self.type = type
313 self.length = length
314
José Fonseca501f2862010-11-19 20:41:18 +0000315 def visit(self, visitor, *args, **kwargs):
316 return visitor.visit_array(self, *args, **kwargs)
317
José Fonsecaccae31c2009-07-22 18:14:12 +0100318 def dump(self, instance):
319 index = '__i' + self.type.id
José Fonseca51c1ef82010-11-15 16:09:14 +0000320 print ' Log::BeginArray("%s", %s);' % (self.type, self.length)
José Fonseca4a5f33a2009-09-12 10:19:10 +0100321 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
José Fonsecaccae31c2009-07-22 18:14:12 +0100322 print ' Log::BeginElement("%s");' % (self.type,)
323 self.type.dump('(%s)[%s]' % (instance, index))
324 print ' Log::EndElement();'
325 print ' }'
José Fonseca51c1ef82010-11-15 16:09:14 +0000326 print ' Log::EndArray();'
José Fonsecaccae31c2009-07-22 18:14:12 +0100327
328 def wrap_instance(self, instance):
329 self.type.wrap_instance("*" + instance)
330
331 def unwrap_instance(self, instance):
332 self.type.wrap_instance("*" + instance)
333
334
José Fonseca83c9ac82010-01-28 14:45:36 +0000335class OutArray(Array):
336
337 def isoutput(self):
338 return True
339
340
José Fonseca8a56d142008-07-09 12:18:08 +0900341class Struct(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900342
343 def __init__(self, name, members):
José Fonseca8a56d142008-07-09 12:18:08 +0900344 Concrete.__init__(self, name)
José Fonseca51c1ef82010-11-15 16:09:14 +0000345 self.name = name
José Fonsecad626cf42008-07-07 07:43:16 +0900346 self.members = members
347
José Fonseca501f2862010-11-19 20:41:18 +0000348 def visit(self, visitor, *args, **kwargs):
349 return visitor.visit_struct(self, *args, **kwargs)
350
José Fonseca8a56d142008-07-09 12:18:08 +0900351 def _dump(self, instance):
José Fonseca51c1ef82010-11-15 16:09:14 +0000352 print ' Log::BeginStruct("%s");' % (self.name,)
José Fonsecaa83fb242008-07-07 16:55:52 +0900353 for type, name in self.members:
José Fonseca51c1ef82010-11-15 16:09:14 +0000354 print ' Log::BeginMember("%s", "%s");' % (type, name)
José Fonsecaa83fb242008-07-07 16:55:52 +0900355 type.dump('(%s).%s' % (instance, name))
José Fonseca51c1ef82010-11-15 16:09:14 +0000356 print ' Log::EndMember();'
357 print ' Log::EndStruct();'
José Fonsecaa83fb242008-07-07 16:55:52 +0900358
José Fonsecad626cf42008-07-07 07:43:16 +0900359
360class Alias(Type):
361
José Fonsecac9edb832010-11-20 09:03:10 +0000362 def __init__(self, expr, type):
363 Type.__init__(self, expr)
José Fonsecad626cf42008-07-07 07:43:16 +0900364 self.type = type
365
José Fonseca501f2862010-11-19 20:41:18 +0000366 def visit(self, visitor, *args, **kwargs):
367 return visitor.visit_alias(self, *args, **kwargs)
368
José Fonsecaa83fb242008-07-07 16:55:52 +0900369 def dump(self, instance):
370 self.type.dump(instance)
371
José Fonsecad626cf42008-07-07 07:43:16 +0900372
José Fonseca83c9ac82010-01-28 14:45:36 +0000373class Out(Type):
374
375 def __init__(self, type):
376 Type.__init__(self, type.expr)
377 self.type = type
378
379 def isoutput(self):
380 return True
381
382 def decl(self):
383 self.type.decl()
384
385 def impl(self):
386 self.type.impl()
387
388 def dump(self, instance):
389 self.type.dump(instance)
390
391 def wrap_instance(self, instance):
392 self.type.wrap_instance(instance)
393
394 def unwrap_instance(self, instance):
395 self.type.unwrap_instance(instance)
396
397
José Fonsecad626cf42008-07-07 07:43:16 +0900398class Function:
399
José Fonseca290c28c2009-04-23 15:20:29 +0100400 def __init__(self, type, name, args, call = '__stdcall', fail = None):
José Fonsecad626cf42008-07-07 07:43:16 +0900401 self.type = type
402 self.name = name
403 self.args = args
404 self.call = call
José Fonseca290c28c2009-04-23 15:20:29 +0100405 self.fail = fail
José Fonsecad626cf42008-07-07 07:43:16 +0900406
407 def prototype(self, name=None):
408 if name is not None:
409 name = name.strip()
410 else:
411 name = self.name
412 s = name
413 if self.call:
414 s = self.call + ' ' + s
415 if name.startswith('*'):
416 s = '(' + s + ')'
José Fonsecae753ce82009-07-22 18:13:52 +0100417 s = self.type.expr + ' ' + s
José Fonsecad626cf42008-07-07 07:43:16 +0900418 s += "("
419 if self.args:
420 s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
421 else:
422 s += "void"
423 s += ")"
424 return s
425
José Fonseca3c2c9292009-05-04 12:16:30 +0100426 def pointer_type(self):
427 return 'P' + self.name
428
429 def pointer_value(self):
430 return 'p' + self.name
431
432 def wrap_decl(self):
433 ptype = self.pointer_type()
434 pvalue = self.pointer_value()
435 print 'typedef ' + self.prototype('* %s' % ptype) + ';'
436 print 'static %s %s = NULL;' % (ptype, pvalue)
437 print
438
439 def get_true_pointer(self):
440 raise NotImplementedError
441
José Fonseca83178a02010-11-14 00:35:05 +0000442 def exit_impl(self):
443 print ' ExitProcess(0);'
444
José Fonseca3c2c9292009-05-04 12:16:30 +0100445 def fail_impl(self):
446 if self.fail is not None:
José Fonseca243772e2009-06-25 13:56:57 +0100447 if self.type is Void:
448 assert self.fail == ''
449 print ' return;'
450 else:
451 assert self.fail != ''
452 print ' return %s;' % self.fail
José Fonseca3c2c9292009-05-04 12:16:30 +0100453 else:
José Fonseca83178a02010-11-14 00:35:05 +0000454 self.exit_impl()
José Fonseca3c2c9292009-05-04 12:16:30 +0100455
456 def wrap_impl(self):
457 pvalue = self.pointer_value()
458 print self.prototype() + ' {'
459 if self.type is Void:
460 result = ''
461 else:
462 print ' %s result;' % self.type
463 result = 'result = '
464 self.get_true_pointer()
465 print ' Log::BeginCall("%s");' % (self.name)
466 for type, name in self.args:
467 if not type.isoutput():
468 type.unwrap_instance(name)
469 print ' Log::BeginArg("%s", "%s");' % (type, name)
470 type.dump(name)
471 print ' Log::EndArg();'
472 print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
473 for type, name in self.args:
474 if type.isoutput():
475 print ' Log::BeginArg("%s", "%s");' % (type, name)
476 type.dump(name)
477 print ' Log::EndArg();'
478 type.wrap_instance(name)
479 if self.type is not Void:
480 print ' Log::BeginReturn("%s");' % self.type
481 self.type.dump("result")
482 print ' Log::EndReturn();'
483 self.type.wrap_instance('result')
484 print ' Log::EndCall();'
José Fonsecac77023d2009-05-04 12:53:50 +0100485 self.post_call_impl()
José Fonseca3c2c9292009-05-04 12:16:30 +0100486 if self.type is not Void:
487 print ' return result;'
488 print '}'
489 print
490
José Fonsecac77023d2009-05-04 12:53:50 +0100491 def post_call_impl(self):
492 pass
493
José Fonsecad626cf42008-07-07 07:43:16 +0900494
495class Interface(Type):
496
497 def __init__(self, name, base=None):
498 Type.__init__(self, name)
José Fonsecae753ce82009-07-22 18:13:52 +0100499 self.name = name
José Fonsecad626cf42008-07-07 07:43:16 +0900500 self.base = base
501 self.methods = []
502
503 def itermethods(self):
504 if self.base is not None:
505 for method in self.base.itermethods():
506 yield method
507 for method in self.methods:
508 yield method
509 raise StopIteration
510
511 def wrap_name(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100512 return "Wrap" + self.expr
José Fonsecad626cf42008-07-07 07:43:16 +0900513
514 def wrap_pre_decl(self):
515 print "class %s;" % self.wrap_name()
516
517 def wrap_decl(self):
518 print "class %s : public %s " % (self.wrap_name(), self.name)
519 print "{"
520 print "public:"
521 print " %s(%s * pInstance);" % (self.wrap_name(), self.name)
522 print " virtual ~%s();" % self.wrap_name()
523 print
524 for method in self.itermethods():
525 print " " + method.prototype() + ";"
526 print
José Fonseca27cd25d2008-07-07 13:44:00 +0900527 #print "private:"
José Fonsecad626cf42008-07-07 07:43:16 +0900528 print " %s * m_pInstance;" % (self.name,)
529 print "};"
530 print
531
532 def wrap_impl(self):
533 print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
José Fonsecad626cf42008-07-07 07:43:16 +0900534 print ' m_pInstance = pInstance;'
535 print '}'
536 print
537 print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
José Fonsecad626cf42008-07-07 07:43:16 +0900538 print '}'
539 print
540 for method in self.itermethods():
541 print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
542 if method.type is Void:
543 result = ''
544 else:
545 print ' %s result;' % method.type
546 result = 'result = '
José Fonseca22aec832008-07-09 09:38:45 +0900547 print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900548 print ' Log::BeginArg("%s *", "this");' % self.name
José Fonsecac7337452010-11-15 22:10:53 +0000549 print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
550 print ' Log::EndPointer();'
José Fonseca3bccbb12008-07-10 02:00:31 +0900551 print ' Log::EndArg();'
José Fonseca27cd25d2008-07-07 13:44:00 +0900552 for type, name in method.args:
553 if not type.isoutput():
554 type.unwrap_instance(name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900555 print ' Log::BeginArg("%s", "%s");' % (type, name)
José Fonsecaa83fb242008-07-07 16:55:52 +0900556 type.dump(name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900557 print ' Log::EndArg();'
José Fonsecad626cf42008-07-07 07:43:16 +0900558 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
559 for type, name in method.args:
560 if type.isoutput():
José Fonseca3bccbb12008-07-10 02:00:31 +0900561 print ' Log::BeginArg("%s", "%s");' % (type, name)
José Fonseca22aa6882008-07-07 17:33:30 +0900562 type.dump(name)
José Fonseca3bccbb12008-07-10 02:00:31 +0900563 print ' Log::EndArg();'
José Fonsecad626cf42008-07-07 07:43:16 +0900564 type.wrap_instance(name)
565 if method.type is not Void:
José Fonseca22aec832008-07-09 09:38:45 +0900566 print ' Log::BeginReturn("%s");' % method.type
José Fonseca4a9c40c2008-07-07 18:04:53 +0900567 method.type.dump("result")
José Fonseca22aec832008-07-09 09:38:45 +0900568 print ' Log::EndReturn();'
José Fonsecad626cf42008-07-07 07:43:16 +0900569 method.type.wrap_instance('result')
José Fonseca22aec832008-07-09 09:38:45 +0900570 print ' Log::EndCall();'
José Fonsecad626cf42008-07-07 07:43:16 +0900571 if method.name == 'QueryInterface':
572 print ' if(*ppvObj == m_pInstance)'
573 print ' *ppvObj = this;'
574 if method.name == 'Release':
575 assert method.type is not Void
576 print ' if(!result)'
577 print ' delete this;'
578 if method.type is not Void:
579 print ' return result;'
580 print '}'
581 print
582 print
583
584
585class Method(Function):
586
587 def __init__(self, type, name, args):
José Fonseca51c1ef82010-11-15 16:09:14 +0000588 Function.__init__(self, type, name, args, call = '__stdcall')
José Fonsecad626cf42008-07-07 07:43:16 +0900589
590
591towrap = []
592
593class WrapPointer(Pointer):
594
595 def __init__(self, type):
596 Pointer.__init__(self, type)
597 if type not in towrap:
598 towrap.append(type)
599
600 def wrap_instance(self, instance):
601 print " if(%s)" % instance
602 print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
603
José Fonseca27cd25d2008-07-07 13:44:00 +0900604 def unwrap_instance(self, instance):
605 print " if(%s)" % instance
606 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
607
José Fonseca8a56d142008-07-09 12:18:08 +0900608
609class _String(Type):
610
611 def __init__(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100612 Type.__init__(self, "char *")
José Fonseca8a56d142008-07-09 12:18:08 +0900613
José Fonseca501f2862010-11-19 20:41:18 +0000614 def visit(self, visitor, *args, **kwargs):
615 return visitor.visit_literal(self, *args, **kwargs)
616
José Fonseca8a56d142008-07-09 12:18:08 +0900617 def dump(self, instance):
José Fonseca51c1ef82010-11-15 16:09:14 +0000618 print ' Log::LiteralString((const char *)%s);' % instance
José Fonseca8a56d142008-07-09 12:18:08 +0900619
620String = _String()
621
José Fonseca51c1ef82010-11-15 16:09:14 +0000622
623class _Opaque(Type):
José Fonsecae54e4112009-06-25 13:56:18 +0100624
625 def __init__(self):
José Fonsecac7337452010-11-15 22:10:53 +0000626 Type.__init__(self, "void")
José Fonsecae54e4112009-06-25 13:56:18 +0100627
José Fonseca501f2862010-11-19 20:41:18 +0000628 def visit(self, visitor, *args, **kwargs):
629 return visitor.visit_opaque(self, *args, **kwargs)
630
José Fonsecae54e4112009-06-25 13:56:18 +0100631 def dump(self, instance):
José Fonsecac7337452010-11-15 22:10:53 +0000632 print ' Log::LiteralOpaque();'
José Fonsecae54e4112009-06-25 13:56:18 +0100633
José Fonsecac7337452010-11-15 22:10:53 +0000634Opaque = Pointer(_Opaque())
José Fonsecae54e4112009-06-25 13:56:18 +0100635
José Fonseca8a56d142008-07-09 12:18:08 +0900636
José Fonseca51c1ef82010-11-15 16:09:14 +0000637Bool = Literal("bool", "Bool")
638SChar = Literal("signed char", "SInt")
639UChar = Literal("unsigned char", "UInt")
640Short = Literal("short", "SInt")
641Int = Literal("int", "SInt")
642Long = Literal("long", "SInt")
643LongLong = Literal("long long", "SInt")
644UShort = Literal("unsigned short", "UInt")
645UInt = Literal("unsigned int", "UInt")
646ULong = Literal("unsigned long", "UInt")
647Float = Literal("float", "Float")
648Double = Literal("double", "Float")
649SizeT = Literal("size_t", "UInt")
650WString = Literal("wchar_t *", "WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900651
652
653def wrap():
José Fonseca8a56d142008-07-09 12:18:08 +0900654 for type in all_types.itervalues():
655 type.decl()
656 print
657 for type in all_types.itervalues():
658 type.impl()
659 print
José Fonsecad626cf42008-07-07 07:43:16 +0900660 for type in towrap:
661 type.wrap_pre_decl()
662 print
663 for type in towrap:
664 type.wrap_decl()
665 print
666 for type in towrap:
667 type.wrap_impl()
668 print