blob: 5dd484515c697a84a6d4ce308e6912f7373666c5 [file] [log] [blame]
José Fonseca7ad40262009-09-30 17:17:12 +01001##########################################################################
José Fonseca95442442008-07-08 10:32:53 +09002#
José Fonseca6fac5ae2010-11-29 16:09:13 +00003# Copyright 2008-2010 VMware, Inc.
José Fonseca7ad40262009-09-30 17:17:12 +01004# 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
José Fonseca6fac5ae2010-11-29 16:09:13 +000032class Type:
José Fonseca02c25002011-10-15 13:17:26 +010033 """Base class for all types."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000034
José Fonseca02c25002011-10-15 13:17:26 +010035 __tags = set()
José Fonseca6fac5ae2010-11-29 16:09:13 +000036
José Fonseca02c25002011-10-15 13:17:26 +010037 def __init__(self, expr, tag = None):
José Fonseca6fac5ae2010-11-29 16:09:13 +000038 self.expr = expr
José Fonseca6fac5ae2010-11-29 16:09:13 +000039
José Fonseca02c25002011-10-15 13:17:26 +010040 # Generate a default tag, used when naming functions that will operate
41 # on this type, so it should preferrably be something representative of
42 # the type.
43 if tag is None:
44 tag = ''.join([c for c in expr if c.isalnum() or c in '_'])
45 else:
46 for c in tag:
47 assert c.isalnum() or c in '_'
José Fonseca6fac5ae2010-11-29 16:09:13 +000048
José Fonseca02c25002011-10-15 13:17:26 +010049 # Ensure it is unique.
50 if tag in Type.__tags:
51 suffix = 1
52 while tag + str(suffix) in Type.__tags:
53 suffix += 1
54 tag += str(suffix)
55
56 assert tag not in Type.__tags
57 Type.__tags.add(tag)
58
59 self.tag = tag
José Fonseca6fac5ae2010-11-29 16:09:13 +000060
61 def __str__(self):
José Fonseca02c25002011-10-15 13:17:26 +010062 """Return the C/C++ type expression for this type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000063 return self.expr
64
65 def visit(self, visitor, *args, **kwargs):
66 raise NotImplementedError
67
68
69
70class _Void(Type):
José Fonseca02c25002011-10-15 13:17:26 +010071 """Singleton void type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000072
73 def __init__(self):
74 Type.__init__(self, "void")
75
76 def visit(self, visitor, *args, **kwargs):
77 return visitor.visit_void(self, *args, **kwargs)
78
79Void = _Void()
80
81
82class Literal(Type):
José Fonseca2f2ea482011-10-15 15:10:06 +010083 """Class to describe literal types.
José Fonseca6fac5ae2010-11-29 16:09:13 +000084
José Fonseca2f2ea482011-10-15 15:10:06 +010085 Types which are not defined in terms of other types, such as integers and
86 floats."""
87
88 def __init__(self, expr, kind):
José Fonseca6fac5ae2010-11-29 16:09:13 +000089 Type.__init__(self, expr)
José Fonseca2f2ea482011-10-15 15:10:06 +010090 self.kind = kind
José Fonseca6fac5ae2010-11-29 16:09:13 +000091
92 def visit(self, visitor, *args, **kwargs):
93 return visitor.visit_literal(self, *args, **kwargs)
94
95
96class Const(Type):
97
98 def __init__(self, type):
José Fonseca903c2ca2011-09-23 09:43:05 +010099 # While "const foo" and "foo const" are synonymous, "const foo *" and
100 # "foo * const" are not quite the same, and some compilers do enforce
101 # strict const correctness.
102 if isinstance(type, String) or type is WString:
103 # For strings we never intend to say a const pointer to chars, but
104 # rather a point to const chars.
105 expr = "const " + type.expr
106 elif type.expr.startswith("const ") or '*' in type.expr:
José Fonseca6fac5ae2010-11-29 16:09:13 +0000107 expr = type.expr + " const"
108 else:
José Fonseca903c2ca2011-09-23 09:43:05 +0100109 # The most legible
José Fonseca6fac5ae2010-11-29 16:09:13 +0000110 expr = "const " + type.expr
111
José Fonseca02c25002011-10-15 13:17:26 +0100112 Type.__init__(self, expr, 'C' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000113
114 self.type = type
115
116 def visit(self, visitor, *args, **kwargs):
117 return visitor.visit_const(self, *args, **kwargs)
118
119
120class Pointer(Type):
121
122 def __init__(self, type):
José Fonseca02c25002011-10-15 13:17:26 +0100123 Type.__init__(self, type.expr + " *", 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000124 self.type = type
125
126 def visit(self, visitor, *args, **kwargs):
127 return visitor.visit_pointer(self, *args, **kwargs)
128
129
130class Handle(Type):
131
José Fonseca8a844ae2010-12-06 18:50:52 +0000132 def __init__(self, name, type, range=None, key=None):
José Fonseca02c25002011-10-15 13:17:26 +0100133 Type.__init__(self, type.expr, 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000134 self.name = name
135 self.type = type
136 self.range = range
José Fonseca8a844ae2010-12-06 18:50:52 +0000137 self.key = key
José Fonseca6fac5ae2010-11-29 16:09:13 +0000138
139 def visit(self, visitor, *args, **kwargs):
140 return visitor.visit_handle(self, *args, **kwargs)
141
142
143def ConstPointer(type):
144 return Pointer(Const(type))
145
146
147class Enum(Type):
148
José Fonseca02c25002011-10-15 13:17:26 +0100149 __id = 0
150
José Fonseca6fac5ae2010-11-29 16:09:13 +0000151 def __init__(self, name, values):
152 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100153
154 self.id = Enum.__id
155 Enum.__id += 1
156
José Fonseca6fac5ae2010-11-29 16:09:13 +0000157 self.values = list(values)
José Fonseca02c25002011-10-15 13:17:26 +0100158
José Fonseca6fac5ae2010-11-29 16:09:13 +0000159 def visit(self, visitor, *args, **kwargs):
160 return visitor.visit_enum(self, *args, **kwargs)
161
162
163def FakeEnum(type, values):
164 return Enum(type.expr, values)
165
166
167class Bitmask(Type):
168
José Fonseca02c25002011-10-15 13:17:26 +0100169 __id = 0
170
José Fonseca6fac5ae2010-11-29 16:09:13 +0000171 def __init__(self, type, values):
172 Type.__init__(self, type.expr)
José Fonseca02c25002011-10-15 13:17:26 +0100173
174 self.id = Bitmask.__id
175 Bitmask.__id += 1
176
José Fonseca6fac5ae2010-11-29 16:09:13 +0000177 self.type = type
178 self.values = values
179
180 def visit(self, visitor, *args, **kwargs):
181 return visitor.visit_bitmask(self, *args, **kwargs)
182
183Flags = Bitmask
184
185
186class Array(Type):
187
188 def __init__(self, type, length):
189 Type.__init__(self, type.expr + " *")
190 self.type = type
191 self.length = length
192
193 def visit(self, visitor, *args, **kwargs):
194 return visitor.visit_array(self, *args, **kwargs)
195
196
197class Blob(Type):
198
199 def __init__(self, type, size):
200 Type.__init__(self, type.expr + ' *')
201 self.type = type
202 self.size = size
203
204 def visit(self, visitor, *args, **kwargs):
205 return visitor.visit_blob(self, *args, **kwargs)
206
207
208class Struct(Type):
209
José Fonseca02c25002011-10-15 13:17:26 +0100210 __id = 0
211
José Fonseca6fac5ae2010-11-29 16:09:13 +0000212 def __init__(self, name, members):
213 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100214
215 self.id = Struct.__id
216 Struct.__id += 1
217
José Fonseca6fac5ae2010-11-29 16:09:13 +0000218 self.name = name
219 self.members = members
220
221 def visit(self, visitor, *args, **kwargs):
222 return visitor.visit_struct(self, *args, **kwargs)
223
224
225class Alias(Type):
226
227 def __init__(self, expr, type):
228 Type.__init__(self, expr)
229 self.type = type
230
231 def visit(self, visitor, *args, **kwargs):
232 return visitor.visit_alias(self, *args, **kwargs)
233
234
235def Out(type, name):
236 arg = Arg(type, name, output=True)
237 return arg
238
239
240class Arg:
241
242 def __init__(self, type, name, output=False):
243 self.type = type
244 self.name = name
245 self.output = output
246 self.index = None
247
248 def __str__(self):
249 return '%s %s' % (self.type, self.name)
250
251
252class Function:
253
José Fonseca46a48392011-10-14 11:34:27 +0100254 # 0-3 are reserved to memcpy, malloc, free, and realloc
255 __id = 4
José Fonseca6fac5ae2010-11-29 16:09:13 +0000256
José Fonsecabca0f412011-04-24 20:53:38 +0100257 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000258 self.id = Function.__id
259 Function.__id += 1
260
261 self.type = type
262 self.name = name
263
264 self.args = []
265 index = 0
266 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100267 if not isinstance(arg, Arg):
268 if isinstance(arg, tuple):
269 arg_type, arg_name = arg
270 else:
271 arg_type = arg
272 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000273 arg = Arg(arg_type, arg_name)
274 arg.index = index
275 index += 1
276 self.args.append(arg)
277
278 self.call = call
279 self.fail = fail
280 self.sideeffects = sideeffects
José Fonseca6fac5ae2010-11-29 16:09:13 +0000281
282 def prototype(self, name=None):
283 if name is not None:
284 name = name.strip()
285 else:
286 name = self.name
287 s = name
288 if self.call:
289 s = self.call + ' ' + s
290 if name.startswith('*'):
291 s = '(' + s + ')'
292 s = self.type.expr + ' ' + s
293 s += "("
294 if self.args:
295 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
296 else:
297 s += "void"
298 s += ")"
299 return s
300
301
302def StdFunction(*args, **kwargs):
303 kwargs.setdefault('call', '__stdcall')
304 return Function(*args, **kwargs)
305
306
307def FunctionPointer(type, name, args, **kwargs):
308 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
309 return Opaque(name)
310
311
312class Interface(Type):
313
314 def __init__(self, name, base=None):
315 Type.__init__(self, name)
316 self.name = name
317 self.base = base
318 self.methods = []
319
320 def visit(self, visitor, *args, **kwargs):
321 return visitor.visit_interface(self, *args, **kwargs)
322
323 def itermethods(self):
324 if self.base is not None:
325 for method in self.base.itermethods():
326 yield method
327 for method in self.methods:
328 yield method
329 raise StopIteration
330
331
332class Method(Function):
333
334 def __init__(self, type, name, args):
335 Function.__init__(self, type, name, args, call = '__stdcall')
336 for index in range(len(self.args)):
337 self.args[index].index = index + 1
338
339
José Fonseca6fac5ae2010-11-29 16:09:13 +0000340class String(Type):
341
342 def __init__(self, expr = "char *", length = None):
343 Type.__init__(self, expr)
344 self.length = length
345
346 def visit(self, visitor, *args, **kwargs):
347 return visitor.visit_string(self, *args, **kwargs)
348
349# C string (i.e., zero terminated)
350CString = String()
351
352
353class Opaque(Type):
354 '''Opaque pointer.'''
355
356 def __init__(self, expr):
357 Type.__init__(self, expr)
358
359 def visit(self, visitor, *args, **kwargs):
360 return visitor.visit_opaque(self, *args, **kwargs)
361
362
363def OpaquePointer(type, *args):
364 return Opaque(type.expr + ' *')
365
366def OpaqueArray(type, size):
367 return Opaque(type.expr + ' *')
368
369def OpaqueBlob(type, size):
370 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900371
José Fonseca501f2862010-11-19 20:41:18 +0000372
José Fonseca16d46dd2011-10-13 09:52:52 +0100373class Polymorphic(Type):
374
375 def __init__(self, default_type, switch_expr, switch_types):
376 Type.__init__(self, default_type.expr)
377 self.default_type = default_type
378 self.switch_expr = switch_expr
379 self.switch_types = switch_types
380
381 def visit(self, visitor, *args, **kwargs):
382 return visitor.visit_polymorphic(self, *args, **kwargs)
383
José Fonseca46161112011-10-14 10:04:55 +0100384 def iterswitch(self):
385 cases = [['default']]
386 types = [self.default_type]
387
388 for expr, type in self.switch_types:
389 case = 'case %s' % expr
390 try:
391 i = types.index(type)
392 except ValueError:
393 cases.append([case])
394 types.append(type)
395 else:
396 cases[i].append(case)
397
398 return zip(cases, types)
399
José Fonseca16d46dd2011-10-13 09:52:52 +0100400
José Fonseca501f2862010-11-19 20:41:18 +0000401class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000402 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000403
404 def visit(self, type, *args, **kwargs):
405 return type.visit(self, *args, **kwargs)
406
José Fonsecac356d6a2010-11-23 14:27:25 +0000407 def visit_void(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000408 raise NotImplementedError
409
José Fonsecac356d6a2010-11-23 14:27:25 +0000410 def visit_literal(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000411 raise NotImplementedError
412
José Fonsecac356d6a2010-11-23 14:27:25 +0000413 def visit_string(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000414 raise NotImplementedError
415
José Fonsecac356d6a2010-11-23 14:27:25 +0000416 def visit_const(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000417 raise NotImplementedError
418
José Fonsecac356d6a2010-11-23 14:27:25 +0000419 def visit_struct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000420 raise NotImplementedError
421
José Fonsecac356d6a2010-11-23 14:27:25 +0000422 def visit_array(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000423 raise NotImplementedError
424
José Fonsecac356d6a2010-11-23 14:27:25 +0000425 def visit_blob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000426 raise NotImplementedError
427
José Fonsecac356d6a2010-11-23 14:27:25 +0000428 def visit_enum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000429 raise NotImplementedError
430
José Fonsecac356d6a2010-11-23 14:27:25 +0000431 def visit_bitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000432 raise NotImplementedError
433
José Fonsecac356d6a2010-11-23 14:27:25 +0000434 def visit_pointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000435 raise NotImplementedError
436
José Fonseca50d78d82010-11-23 22:13:14 +0000437 def visit_handle(self, handle, *args, **kwargs):
438 raise NotImplementedError
439
José Fonsecac356d6a2010-11-23 14:27:25 +0000440 def visit_alias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000441 raise NotImplementedError
442
José Fonsecac356d6a2010-11-23 14:27:25 +0000443 def visit_opaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000444 raise NotImplementedError
445
José Fonsecac356d6a2010-11-23 14:27:25 +0000446 def visit_interface(self, interface, *args, **kwargs):
447 raise NotImplementedError
448
José Fonseca16d46dd2011-10-13 09:52:52 +0100449 def visit_polymorphic(self, polymorphic, *args, **kwargs):
450 raise NotImplementedError
451 #return self.visit(polymorphic.default_type, *args, **kwargs)
452
José Fonsecac356d6a2010-11-23 14:27:25 +0000453
454class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000455 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000456
457 def __init__(self):
458 self.__visited = set()
459
460 def visit(self, type, *args, **kwargs):
461 if type not in self.__visited:
462 self.__visited.add(type)
463 return type.visit(self, *args, **kwargs)
464 return None
465
José Fonseca501f2862010-11-19 20:41:18 +0000466
José Fonsecac9edb832010-11-20 09:03:10 +0000467class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000468 '''Visitor which rebuild types as it visits them.
469
470 By itself it is a no-op -- it is intended to be overwritten.
471 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000472
473 def visit_void(self, void):
474 return void
475
476 def visit_literal(self, literal):
477 return literal
478
José Fonseca2defc982010-11-22 16:59:10 +0000479 def visit_string(self, string):
480 return string
481
José Fonsecac9edb832010-11-20 09:03:10 +0000482 def visit_const(self, const):
483 return Const(const.type)
484
485 def visit_struct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100486 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000487 return Struct(struct.name, members)
488
489 def visit_array(self, array):
490 type = self.visit(array.type)
491 return Array(type, array.length)
492
José Fonseca885f2652010-11-20 11:22:25 +0000493 def visit_blob(self, blob):
494 type = self.visit(blob.type)
495 return Blob(type, blob.size)
496
José Fonsecac9edb832010-11-20 09:03:10 +0000497 def visit_enum(self, enum):
498 return enum
499
500 def visit_bitmask(self, bitmask):
501 type = self.visit(bitmask.type)
502 return Bitmask(type, bitmask.values)
503
504 def visit_pointer(self, pointer):
505 type = self.visit(pointer.type)
506 return Pointer(type)
507
José Fonseca50d78d82010-11-23 22:13:14 +0000508 def visit_handle(self, handle):
509 type = self.visit(handle.type)
José Fonseca8a844ae2010-12-06 18:50:52 +0000510 return Handle(handle.name, type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000511
José Fonsecac9edb832010-11-20 09:03:10 +0000512 def visit_alias(self, alias):
513 type = self.visit(alias.type)
514 return Alias(alias.expr, type)
515
516 def visit_opaque(self, opaque):
517 return opaque
518
José Fonseca16d46dd2011-10-13 09:52:52 +0100519 def visit_polymorphic(self, polymorphic):
520 default_type = self.visit(polymorphic.default_type)
521 switch_expr = polymorphic.switch_expr
522 switch_types = [(expr, self.visit(type)) for expr, type in polymorphic.switch_types]
523 return Polymorphic(default_type, switch_expr, switch_types)
524
José Fonsecac9edb832010-11-20 09:03:10 +0000525
José Fonsecae6a50bd2010-11-24 10:12:22 +0000526class Collector(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000527 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000528
529 def __init__(self):
530 self.__visited = set()
531 self.types = []
532
533 def visit(self, type):
534 if type in self.__visited:
535 return
536 self.__visited.add(type)
537 Visitor.visit(self, type)
538 self.types.append(type)
539
540 def visit_void(self, literal):
541 pass
542
543 def visit_literal(self, literal):
544 pass
545
546 def visit_string(self, string):
547 pass
548
549 def visit_const(self, const):
550 self.visit(const.type)
551
552 def visit_struct(self, struct):
553 for type, name in struct.members:
554 self.visit(type)
555
556 def visit_array(self, array):
557 self.visit(array.type)
558
559 def visit_blob(self, array):
560 pass
561
562 def visit_enum(self, enum):
563 pass
564
565 def visit_bitmask(self, bitmask):
566 self.visit(bitmask.type)
567
568 def visit_pointer(self, pointer):
569 self.visit(pointer.type)
570
571 def visit_handle(self, handle):
572 self.visit(handle.type)
573
574 def visit_alias(self, alias):
575 self.visit(alias.type)
576
577 def visit_opaque(self, opaque):
578 pass
579
580 def visit_interface(self, interface):
José Fonseca87d1cc62010-11-29 15:57:25 +0000581 if interface.base is not None:
582 self.visit(interface.base)
583 for method in interface.itermethods():
584 for arg in method.args:
585 self.visit(arg.type)
586 self.visit(method.type)
José Fonsecae6a50bd2010-11-24 10:12:22 +0000587
José Fonseca16d46dd2011-10-13 09:52:52 +0100588 def visit_polymorphic(self, polymorphic):
589 self.visit(polymorphic.default_type)
590 for expr, type in polymorphic.switch_types:
591 self.visit(type)
592
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000593
594class API:
José Fonseca9c4a2572012-01-13 23:21:10 +0000595 '''API abstraction.
596
597 Essentially, a collection of types, functions, and interfaces.
598 '''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000599
José Fonseca68ec4122011-02-20 11:25:25 +0000600 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000601 self.name = name
602 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000603 self.functions = []
604 self.interfaces = []
605
José Fonsecae6a50bd2010-11-24 10:12:22 +0000606 def all_types(self):
607 collector = Collector()
608 for function in self.functions:
609 for arg in function.args:
610 collector.visit(arg.type)
611 collector.visit(function.type)
612 for interface in self.interfaces:
613 collector.visit(interface)
José Fonseca87d1cc62010-11-29 15:57:25 +0000614 for method in interface.itermethods():
José Fonsecae6a50bd2010-11-24 10:12:22 +0000615 for arg in method.args:
616 collector.visit(arg.type)
617 collector.visit(method.type)
618 return collector.types
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000619
620 def add_function(self, function):
621 self.functions.append(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000622
623 def add_functions(self, functions):
624 for function in functions:
625 self.add_function(function)
626
627 def add_interface(self, interface):
628 self.interfaces.append(interface)
629
630 def add_interfaces(self, interfaces):
631 self.interfaces.extend(interfaces)
632
José Fonseca68ec4122011-02-20 11:25:25 +0000633 def add_api(self, api):
634 self.headers.extend(api.headers)
635 self.add_functions(api.functions)
636 self.add_interfaces(api.interfaces)
637
José Fonsecaeccec3e2011-02-20 09:01:25 +0000638 def get_function_by_name(self, name):
639 for function in self.functions:
640 if function.name == name:
641 return function
642 return None
643
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000644
José Fonseca51c1ef82010-11-15 16:09:14 +0000645Bool = Literal("bool", "Bool")
646SChar = Literal("signed char", "SInt")
647UChar = Literal("unsigned char", "UInt")
648Short = Literal("short", "SInt")
649Int = Literal("int", "SInt")
650Long = Literal("long", "SInt")
651LongLong = Literal("long long", "SInt")
652UShort = Literal("unsigned short", "UInt")
653UInt = Literal("unsigned int", "UInt")
654ULong = Literal("unsigned long", "UInt")
José Fonseca28f034f2010-11-22 20:31:25 +0000655ULongLong = Literal("unsigned long long", "UInt")
José Fonseca51c1ef82010-11-15 16:09:14 +0000656Float = Literal("float", "Float")
José Fonseca9ff74442011-05-07 01:17:49 +0100657Double = Literal("double", "Double")
José Fonseca51c1ef82010-11-15 16:09:14 +0000658SizeT = Literal("size_t", "UInt")
659WString = Literal("wchar_t *", "WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900660
José Fonseca2250a0e2010-11-26 15:01:29 +0000661Int8 = Literal("int8_t", "SInt")
662UInt8 = Literal("uint8_t", "UInt")
663Int16 = Literal("int16_t", "SInt")
664UInt16 = Literal("uint16_t", "UInt")
665Int32 = Literal("int32_t", "SInt")
666UInt32 = Literal("uint32_t", "UInt")
667Int64 = Literal("int64_t", "SInt")
668UInt64 = Literal("uint64_t", "UInt")