blob: 2057124c9edf9bd4610a0f423fd7e7f359fee71b [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:
José Fonseca5b6fb752012-04-14 14:56:45 +010044 if expr is not None:
45 tag = ''.join([c for c in expr if c.isalnum() or c in '_'])
46 else:
47 tag = 'anonynoums'
José Fonseca02c25002011-10-15 13:17:26 +010048 else:
49 for c in tag:
50 assert c.isalnum() or c in '_'
José Fonseca6fac5ae2010-11-29 16:09:13 +000051
José Fonseca02c25002011-10-15 13:17:26 +010052 # Ensure it is unique.
53 if tag in Type.__tags:
54 suffix = 1
55 while tag + str(suffix) in Type.__tags:
56 suffix += 1
57 tag += str(suffix)
58
59 assert tag not in Type.__tags
60 Type.__tags.add(tag)
61
62 self.tag = tag
José Fonseca6fac5ae2010-11-29 16:09:13 +000063
64 def __str__(self):
José Fonseca02c25002011-10-15 13:17:26 +010065 """Return the C/C++ type expression for this type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000066 return self.expr
67
68 def visit(self, visitor, *args, **kwargs):
69 raise NotImplementedError
70
71
72
73class _Void(Type):
José Fonseca02c25002011-10-15 13:17:26 +010074 """Singleton void type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000075
76 def __init__(self):
77 Type.__init__(self, "void")
78
79 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +000080 return visitor.visitVoid(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +000081
82Void = _Void()
83
84
85class Literal(Type):
José Fonseca2f2ea482011-10-15 15:10:06 +010086 """Class to describe literal types.
José Fonseca6fac5ae2010-11-29 16:09:13 +000087
José Fonseca2f2ea482011-10-15 15:10:06 +010088 Types which are not defined in terms of other types, such as integers and
89 floats."""
90
91 def __init__(self, expr, kind):
José Fonseca6fac5ae2010-11-29 16:09:13 +000092 Type.__init__(self, expr)
José Fonseca2f2ea482011-10-15 15:10:06 +010093 self.kind = kind
José Fonseca6fac5ae2010-11-29 16:09:13 +000094
95 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +000096 return visitor.visitLiteral(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +000097
98
99class Const(Type):
100
101 def __init__(self, type):
José Fonseca903c2ca2011-09-23 09:43:05 +0100102 # While "const foo" and "foo const" are synonymous, "const foo *" and
103 # "foo * const" are not quite the same, and some compilers do enforce
104 # strict const correctness.
105 if isinstance(type, String) or type is WString:
106 # For strings we never intend to say a const pointer to chars, but
107 # rather a point to const chars.
108 expr = "const " + type.expr
109 elif type.expr.startswith("const ") or '*' in type.expr:
José Fonseca6fac5ae2010-11-29 16:09:13 +0000110 expr = type.expr + " const"
111 else:
José Fonseca903c2ca2011-09-23 09:43:05 +0100112 # The most legible
José Fonseca6fac5ae2010-11-29 16:09:13 +0000113 expr = "const " + type.expr
114
José Fonseca02c25002011-10-15 13:17:26 +0100115 Type.__init__(self, expr, 'C' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000116
117 self.type = type
118
119 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000120 return visitor.visitConst(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000121
122
123class Pointer(Type):
124
125 def __init__(self, type):
José Fonseca02c25002011-10-15 13:17:26 +0100126 Type.__init__(self, type.expr + " *", 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000127 self.type = type
128
129 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000130 return visitor.visitPointer(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000131
132
José Fonseca59ee88e2012-01-15 14:24:10 +0000133class IntPointer(Type):
134 '''Integer encoded as a pointer.'''
135
136 def visit(self, visitor, *args, **kwargs):
137 return visitor.visitIntPointer(self, *args, **kwargs)
138
139
José Fonsecafbcf6832012-04-05 07:10:30 +0100140class ObjPointer(Type):
141 '''Pointer to an object.'''
142
143 def __init__(self, type):
144 Type.__init__(self, type.expr + " *", 'P' + type.tag)
145 self.type = type
146
147 def visit(self, visitor, *args, **kwargs):
148 return visitor.visitObjPointer(self, *args, **kwargs)
149
150
José Fonseca59ee88e2012-01-15 14:24:10 +0000151class LinearPointer(Type):
José Fonsecafbcf6832012-04-05 07:10:30 +0100152 '''Pointer to a linear range of memory.'''
José Fonseca59ee88e2012-01-15 14:24:10 +0000153
154 def __init__(self, type, size = None):
155 Type.__init__(self, type.expr + " *", 'P' + type.tag)
156 self.type = type
157 self.size = size
158
159 def visit(self, visitor, *args, **kwargs):
160 return visitor.visitLinearPointer(self, *args, **kwargs)
161
162
José Fonsecab89c5932012-04-01 22:47:11 +0200163class Reference(Type):
164 '''C++ references.'''
165
166 def __init__(self, type):
167 Type.__init__(self, type.expr + " &", 'R' + type.tag)
168 self.type = type
169
170 def visit(self, visitor, *args, **kwargs):
171 return visitor.visitReference(self, *args, **kwargs)
172
173
José Fonseca6fac5ae2010-11-29 16:09:13 +0000174class Handle(Type):
175
José Fonseca8a844ae2010-12-06 18:50:52 +0000176 def __init__(self, name, type, range=None, key=None):
José Fonseca02c25002011-10-15 13:17:26 +0100177 Type.__init__(self, type.expr, 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000178 self.name = name
179 self.type = type
180 self.range = range
José Fonseca8a844ae2010-12-06 18:50:52 +0000181 self.key = key
José Fonseca6fac5ae2010-11-29 16:09:13 +0000182
183 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000184 return visitor.visitHandle(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000185
186
187def ConstPointer(type):
188 return Pointer(Const(type))
189
190
191class Enum(Type):
192
José Fonseca02c25002011-10-15 13:17:26 +0100193 __id = 0
194
José Fonseca6fac5ae2010-11-29 16:09:13 +0000195 def __init__(self, name, values):
196 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100197
198 self.id = Enum.__id
199 Enum.__id += 1
200
José Fonseca6fac5ae2010-11-29 16:09:13 +0000201 self.values = list(values)
José Fonseca02c25002011-10-15 13:17:26 +0100202
José Fonseca6fac5ae2010-11-29 16:09:13 +0000203 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000204 return visitor.visitEnum(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000205
206
207def FakeEnum(type, values):
208 return Enum(type.expr, values)
209
210
211class Bitmask(Type):
212
José Fonseca02c25002011-10-15 13:17:26 +0100213 __id = 0
214
José Fonseca6fac5ae2010-11-29 16:09:13 +0000215 def __init__(self, type, values):
216 Type.__init__(self, type.expr)
José Fonseca02c25002011-10-15 13:17:26 +0100217
218 self.id = Bitmask.__id
219 Bitmask.__id += 1
220
José Fonseca6fac5ae2010-11-29 16:09:13 +0000221 self.type = type
222 self.values = values
223
224 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000225 return visitor.visitBitmask(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000226
227Flags = Bitmask
228
229
230class Array(Type):
231
232 def __init__(self, type, length):
233 Type.__init__(self, type.expr + " *")
234 self.type = type
235 self.length = length
236
237 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000238 return visitor.visitArray(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000239
240
241class Blob(Type):
242
243 def __init__(self, type, size):
244 Type.__init__(self, type.expr + ' *')
245 self.type = type
246 self.size = size
247
248 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000249 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000250
251
252class Struct(Type):
253
José Fonseca02c25002011-10-15 13:17:26 +0100254 __id = 0
255
José Fonseca6fac5ae2010-11-29 16:09:13 +0000256 def __init__(self, name, members):
257 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100258
259 self.id = Struct.__id
260 Struct.__id += 1
261
José Fonseca6fac5ae2010-11-29 16:09:13 +0000262 self.name = name
José Fonseca5b6fb752012-04-14 14:56:45 +0100263 self.members = []
264
265 # Eliminate anonymous unions
266 for type, name in members:
267 if name is not None:
268 self.members.append((type, name))
269 else:
270 assert isinstance(type, Union)
271 assert type.name is None
272 self.members.extend(type.members)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000273
274 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000275 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000276
277
José Fonseca5b6fb752012-04-14 14:56:45 +0100278class Union(Type):
279
280 __id = 0
281
282 def __init__(self, name, members):
283 Type.__init__(self, name)
284
285 self.id = Union.__id
286 Union.__id += 1
287
288 self.name = name
289 self.members = members
290
291
José Fonseca6fac5ae2010-11-29 16:09:13 +0000292class Alias(Type):
293
294 def __init__(self, expr, type):
295 Type.__init__(self, expr)
296 self.type = type
297
298 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000299 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000300
José Fonseca6fac5ae2010-11-29 16:09:13 +0000301class Arg:
302
José Fonseca9dd8f702012-04-07 10:42:50 +0100303 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000304 self.type = type
305 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100306 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000307 self.output = output
308 self.index = None
309
310 def __str__(self):
311 return '%s %s' % (self.type, self.name)
312
313
José Fonseca9dd8f702012-04-07 10:42:50 +0100314def In(type, name):
315 return Arg(type, name, input=True, output=False)
316
317def Out(type, name):
318 return Arg(type, name, input=False, output=True)
319
320def InOut(type, name):
321 return Arg(type, name, input=True, output=True)
322
323
José Fonseca6fac5ae2010-11-29 16:09:13 +0000324class Function:
325
José Fonseca46a48392011-10-14 11:34:27 +0100326 # 0-3 are reserved to memcpy, malloc, free, and realloc
327 __id = 4
José Fonseca6fac5ae2010-11-29 16:09:13 +0000328
José Fonsecabca0f412011-04-24 20:53:38 +0100329 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000330 self.id = Function.__id
331 Function.__id += 1
332
333 self.type = type
334 self.name = name
335
336 self.args = []
337 index = 0
338 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100339 if not isinstance(arg, Arg):
340 if isinstance(arg, tuple):
341 arg_type, arg_name = arg
342 else:
343 arg_type = arg
344 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000345 arg = Arg(arg_type, arg_name)
346 arg.index = index
347 index += 1
348 self.args.append(arg)
349
350 self.call = call
351 self.fail = fail
352 self.sideeffects = sideeffects
José Fonseca6fac5ae2010-11-29 16:09:13 +0000353
354 def prototype(self, name=None):
355 if name is not None:
356 name = name.strip()
357 else:
358 name = self.name
359 s = name
360 if self.call:
361 s = self.call + ' ' + s
362 if name.startswith('*'):
363 s = '(' + s + ')'
364 s = self.type.expr + ' ' + s
365 s += "("
366 if self.args:
367 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
368 else:
369 s += "void"
370 s += ")"
371 return s
372
José Fonseca568ecc22012-01-15 13:57:03 +0000373 def argNames(self):
374 return [arg.name for arg in self.args]
375
José Fonseca6fac5ae2010-11-29 16:09:13 +0000376
377def StdFunction(*args, **kwargs):
378 kwargs.setdefault('call', '__stdcall')
379 return Function(*args, **kwargs)
380
381
382def FunctionPointer(type, name, args, **kwargs):
383 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
384 return Opaque(name)
385
386
387class Interface(Type):
388
389 def __init__(self, name, base=None):
390 Type.__init__(self, name)
391 self.name = name
392 self.base = base
393 self.methods = []
394
395 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000396 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000397
José Fonseca54f304a2012-01-14 19:33:08 +0000398 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000399 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000400 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000401 yield method
402 for method in self.methods:
403 yield method
404 raise StopIteration
405
José Fonseca4220b1b2012-02-03 19:05:29 +0000406 def iterBaseMethods(self):
407 if self.base is not None:
408 for iface, method in self.base.iterBaseMethods():
409 yield iface, method
410 for method in self.methods:
411 yield self, method
412 raise StopIteration
413
José Fonseca6fac5ae2010-11-29 16:09:13 +0000414
415class Method(Function):
416
José Fonseca5b6fb752012-04-14 14:56:45 +0100417 def __init__(self, type, name, args, call = '__stdcall', const=False, sideeffects=True):
418 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000419 for index in range(len(self.args)):
420 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000421 self.const = const
422
423 def prototype(self, name=None):
424 s = Function.prototype(self, name)
425 if self.const:
426 s += ' const'
427 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000428
José Fonseca5b6fb752012-04-14 14:56:45 +0100429def StdMethod(*args, **kwargs):
430 kwargs.setdefault('call', '__stdcall')
431 return Method(*args, **kwargs)
432
José Fonseca6fac5ae2010-11-29 16:09:13 +0000433
José Fonseca6fac5ae2010-11-29 16:09:13 +0000434class String(Type):
435
José Fonseca280a1762012-01-31 15:10:13 +0000436 def __init__(self, expr = "char *", length = None, kind = 'String'):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000437 Type.__init__(self, expr)
438 self.length = length
José Fonseca280a1762012-01-31 15:10:13 +0000439 self.kind = kind
José Fonseca6fac5ae2010-11-29 16:09:13 +0000440
441 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000442 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000443
José Fonseca6fac5ae2010-11-29 16:09:13 +0000444
445class Opaque(Type):
446 '''Opaque pointer.'''
447
448 def __init__(self, expr):
449 Type.__init__(self, expr)
450
451 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000452 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000453
454
455def OpaquePointer(type, *args):
456 return Opaque(type.expr + ' *')
457
458def OpaqueArray(type, size):
459 return Opaque(type.expr + ' *')
460
461def OpaqueBlob(type, size):
462 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900463
José Fonseca501f2862010-11-19 20:41:18 +0000464
José Fonseca16d46dd2011-10-13 09:52:52 +0100465class Polymorphic(Type):
466
José Fonseca54f304a2012-01-14 19:33:08 +0000467 def __init__(self, defaultType, switchExpr, switchTypes):
468 Type.__init__(self, defaultType.expr)
469 self.defaultType = defaultType
470 self.switchExpr = switchExpr
471 self.switchTypes = switchTypes
José Fonseca16d46dd2011-10-13 09:52:52 +0100472
473 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000474 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100475
José Fonseca54f304a2012-01-14 19:33:08 +0000476 def iterSwitch(self):
José Fonseca46161112011-10-14 10:04:55 +0100477 cases = [['default']]
José Fonseca54f304a2012-01-14 19:33:08 +0000478 types = [self.defaultType]
José Fonseca46161112011-10-14 10:04:55 +0100479
José Fonseca54f304a2012-01-14 19:33:08 +0000480 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100481 case = 'case %s' % expr
482 try:
483 i = types.index(type)
484 except ValueError:
485 cases.append([case])
486 types.append(type)
487 else:
488 cases[i].append(case)
489
490 return zip(cases, types)
491
José Fonseca16d46dd2011-10-13 09:52:52 +0100492
José Fonseca501f2862010-11-19 20:41:18 +0000493class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000494 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000495
496 def visit(self, type, *args, **kwargs):
497 return type.visit(self, *args, **kwargs)
498
José Fonseca54f304a2012-01-14 19:33:08 +0000499 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000500 raise NotImplementedError
501
José Fonseca54f304a2012-01-14 19:33:08 +0000502 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000503 raise NotImplementedError
504
José Fonseca54f304a2012-01-14 19:33:08 +0000505 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000506 raise NotImplementedError
507
José Fonseca54f304a2012-01-14 19:33:08 +0000508 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000509 raise NotImplementedError
510
José Fonseca54f304a2012-01-14 19:33:08 +0000511 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000512 raise NotImplementedError
513
José Fonseca54f304a2012-01-14 19:33:08 +0000514 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000515 raise NotImplementedError
516
José Fonseca54f304a2012-01-14 19:33:08 +0000517 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000518 raise NotImplementedError
519
José Fonseca54f304a2012-01-14 19:33:08 +0000520 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000521 raise NotImplementedError
522
José Fonseca54f304a2012-01-14 19:33:08 +0000523 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000524 raise NotImplementedError
525
José Fonseca54f304a2012-01-14 19:33:08 +0000526 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000527 raise NotImplementedError
528
José Fonseca59ee88e2012-01-15 14:24:10 +0000529 def visitIntPointer(self, pointer, *args, **kwargs):
530 raise NotImplementedError
531
José Fonsecafbcf6832012-04-05 07:10:30 +0100532 def visitObjPointer(self, pointer, *args, **kwargs):
533 raise NotImplementedError
534
José Fonseca59ee88e2012-01-15 14:24:10 +0000535 def visitLinearPointer(self, pointer, *args, **kwargs):
536 raise NotImplementedError
537
José Fonsecab89c5932012-04-01 22:47:11 +0200538 def visitReference(self, reference, *args, **kwargs):
539 raise NotImplementedError
540
José Fonseca54f304a2012-01-14 19:33:08 +0000541 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000542 raise NotImplementedError
543
José Fonseca54f304a2012-01-14 19:33:08 +0000544 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000545 raise NotImplementedError
546
José Fonseca54f304a2012-01-14 19:33:08 +0000547 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000548 raise NotImplementedError
549
José Fonseca54f304a2012-01-14 19:33:08 +0000550 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000551 raise NotImplementedError
552
José Fonseca54f304a2012-01-14 19:33:08 +0000553 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100554 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000555 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100556
José Fonsecac356d6a2010-11-23 14:27:25 +0000557
558class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000559 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000560
561 def __init__(self):
562 self.__visited = set()
563
564 def visit(self, type, *args, **kwargs):
565 if type not in self.__visited:
566 self.__visited.add(type)
567 return type.visit(self, *args, **kwargs)
568 return None
569
José Fonseca501f2862010-11-19 20:41:18 +0000570
José Fonsecac9edb832010-11-20 09:03:10 +0000571class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000572 '''Visitor which rebuild types as it visits them.
573
574 By itself it is a no-op -- it is intended to be overwritten.
575 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000576
José Fonseca54f304a2012-01-14 19:33:08 +0000577 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000578 return void
579
José Fonseca54f304a2012-01-14 19:33:08 +0000580 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000581 return literal
582
José Fonseca54f304a2012-01-14 19:33:08 +0000583 def visitString(self, string):
José Fonseca2defc982010-11-22 16:59:10 +0000584 return string
585
José Fonseca54f304a2012-01-14 19:33:08 +0000586 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100587 const_type = self.visit(const.type)
588 if const_type is const.type:
589 return const
590 else:
591 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000592
José Fonseca54f304a2012-01-14 19:33:08 +0000593 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100594 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000595 return Struct(struct.name, members)
596
José Fonseca54f304a2012-01-14 19:33:08 +0000597 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000598 type = self.visit(array.type)
599 return Array(type, array.length)
600
José Fonseca54f304a2012-01-14 19:33:08 +0000601 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000602 type = self.visit(blob.type)
603 return Blob(type, blob.size)
604
José Fonseca54f304a2012-01-14 19:33:08 +0000605 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000606 return enum
607
José Fonseca54f304a2012-01-14 19:33:08 +0000608 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000609 type = self.visit(bitmask.type)
610 return Bitmask(type, bitmask.values)
611
José Fonseca54f304a2012-01-14 19:33:08 +0000612 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100613 pointer_type = self.visit(pointer.type)
614 if pointer_type is pointer.type:
615 return pointer
616 else:
617 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000618
José Fonseca59ee88e2012-01-15 14:24:10 +0000619 def visitIntPointer(self, pointer):
620 return pointer
621
José Fonsecafbcf6832012-04-05 07:10:30 +0100622 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100623 pointer_type = self.visit(pointer.type)
624 if pointer_type is pointer.type:
625 return pointer
626 else:
627 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100628
José Fonseca59ee88e2012-01-15 14:24:10 +0000629 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100630 pointer_type = self.visit(pointer.type)
631 if pointer_type is pointer.type:
632 return pointer
633 else:
634 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000635
José Fonsecab89c5932012-04-01 22:47:11 +0200636 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100637 reference_type = self.visit(reference.type)
638 if reference_type is reference.type:
639 return reference
640 else:
641 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200642
José Fonseca54f304a2012-01-14 19:33:08 +0000643 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100644 handle_type = self.visit(handle.type)
645 if handle_type is handle.type:
646 return handle
647 else:
648 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000649
José Fonseca54f304a2012-01-14 19:33:08 +0000650 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100651 alias_type = self.visit(alias.type)
652 if alias_type is alias.type:
653 return alias
654 else:
655 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000656
José Fonseca54f304a2012-01-14 19:33:08 +0000657 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000658 return opaque
659
José Fonseca7814edf2012-01-31 10:55:49 +0000660 def visitInterface(self, interface, *args, **kwargs):
661 return interface
662
José Fonseca54f304a2012-01-14 19:33:08 +0000663 def visitPolymorphic(self, polymorphic):
664 defaultType = self.visit(polymorphic.defaultType)
665 switchExpr = polymorphic.switchExpr
666 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
667 return Polymorphic(defaultType, switchExpr, switchTypes)
José Fonseca16d46dd2011-10-13 09:52:52 +0100668
José Fonsecac9edb832010-11-20 09:03:10 +0000669
José Fonsecae6a50bd2010-11-24 10:12:22 +0000670class Collector(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000671 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000672
673 def __init__(self):
674 self.__visited = set()
675 self.types = []
676
677 def visit(self, type):
678 if type in self.__visited:
679 return
680 self.__visited.add(type)
681 Visitor.visit(self, type)
682 self.types.append(type)
683
José Fonseca54f304a2012-01-14 19:33:08 +0000684 def visitVoid(self, literal):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000685 pass
686
José Fonseca54f304a2012-01-14 19:33:08 +0000687 def visitLiteral(self, literal):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000688 pass
689
José Fonseca54f304a2012-01-14 19:33:08 +0000690 def visitString(self, string):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000691 pass
692
José Fonseca54f304a2012-01-14 19:33:08 +0000693 def visitConst(self, const):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000694 self.visit(const.type)
695
José Fonseca54f304a2012-01-14 19:33:08 +0000696 def visitStruct(self, struct):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000697 for type, name in struct.members:
698 self.visit(type)
699
José Fonseca54f304a2012-01-14 19:33:08 +0000700 def visitArray(self, array):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000701 self.visit(array.type)
702
José Fonseca54f304a2012-01-14 19:33:08 +0000703 def visitBlob(self, array):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000704 pass
705
José Fonseca54f304a2012-01-14 19:33:08 +0000706 def visitEnum(self, enum):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000707 pass
708
José Fonseca54f304a2012-01-14 19:33:08 +0000709 def visitBitmask(self, bitmask):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000710 self.visit(bitmask.type)
711
José Fonseca54f304a2012-01-14 19:33:08 +0000712 def visitPointer(self, pointer):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000713 self.visit(pointer.type)
714
José Fonseca59ee88e2012-01-15 14:24:10 +0000715 def visitIntPointer(self, pointer):
716 pass
717
José Fonsecafbcf6832012-04-05 07:10:30 +0100718 def visitObjPointer(self, pointer):
719 self.visit(pointer.type)
720
José Fonseca59ee88e2012-01-15 14:24:10 +0000721 def visitLinearPointer(self, pointer):
722 self.visit(pointer.type)
723
José Fonsecab89c5932012-04-01 22:47:11 +0200724 def visitReference(self, reference):
725 self.visit(reference.type)
726
José Fonseca54f304a2012-01-14 19:33:08 +0000727 def visitHandle(self, handle):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000728 self.visit(handle.type)
729
José Fonseca54f304a2012-01-14 19:33:08 +0000730 def visitAlias(self, alias):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000731 self.visit(alias.type)
732
José Fonseca54f304a2012-01-14 19:33:08 +0000733 def visitOpaque(self, opaque):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000734 pass
735
José Fonseca54f304a2012-01-14 19:33:08 +0000736 def visitInterface(self, interface):
José Fonseca87d1cc62010-11-29 15:57:25 +0000737 if interface.base is not None:
738 self.visit(interface.base)
José Fonseca54f304a2012-01-14 19:33:08 +0000739 for method in interface.iterMethods():
José Fonseca87d1cc62010-11-29 15:57:25 +0000740 for arg in method.args:
741 self.visit(arg.type)
742 self.visit(method.type)
José Fonsecae6a50bd2010-11-24 10:12:22 +0000743
José Fonseca54f304a2012-01-14 19:33:08 +0000744 def visitPolymorphic(self, polymorphic):
745 self.visit(polymorphic.defaultType)
746 for expr, type in polymorphic.switchTypes:
José Fonseca16d46dd2011-10-13 09:52:52 +0100747 self.visit(type)
748
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000749
750class API:
José Fonseca9c4a2572012-01-13 23:21:10 +0000751 '''API abstraction.
752
753 Essentially, a collection of types, functions, and interfaces.
754 '''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000755
José Fonseca68ec4122011-02-20 11:25:25 +0000756 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000757 self.name = name
758 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000759 self.functions = []
760 self.interfaces = []
761
José Fonseca44703822012-01-31 10:48:58 +0000762 def getAllTypes(self):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000763 collector = Collector()
764 for function in self.functions:
765 for arg in function.args:
766 collector.visit(arg.type)
767 collector.visit(function.type)
768 for interface in self.interfaces:
769 collector.visit(interface)
José Fonseca54f304a2012-01-14 19:33:08 +0000770 for method in interface.iterMethods():
José Fonsecae6a50bd2010-11-24 10:12:22 +0000771 for arg in method.args:
772 collector.visit(arg.type)
773 collector.visit(method.type)
774 return collector.types
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000775
José Fonseca2ef6d5b2012-01-31 10:56:38 +0000776 def getAllInterfaces(self):
777 types = self.getAllTypes()
778 interfaces = [type for type in types if isinstance(type, Interface)]
779 for interface in self.interfaces:
780 if interface not in interfaces:
781 interfaces.append(interface)
782 return interfaces
783
José Fonseca54f304a2012-01-14 19:33:08 +0000784 def addFunction(self, function):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000785 self.functions.append(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000786
José Fonseca54f304a2012-01-14 19:33:08 +0000787 def addFunctions(self, functions):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000788 for function in functions:
José Fonseca54f304a2012-01-14 19:33:08 +0000789 self.addFunction(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000790
José Fonseca54f304a2012-01-14 19:33:08 +0000791 def addInterface(self, interface):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000792 self.interfaces.append(interface)
793
José Fonseca54f304a2012-01-14 19:33:08 +0000794 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000795 self.interfaces.extend(interfaces)
796
José Fonseca54f304a2012-01-14 19:33:08 +0000797 def addApi(self, api):
José Fonseca68ec4122011-02-20 11:25:25 +0000798 self.headers.extend(api.headers)
José Fonseca54f304a2012-01-14 19:33:08 +0000799 self.addFunctions(api.functions)
800 self.addInterfaces(api.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000801
José Fonsecaeccec3e2011-02-20 09:01:25 +0000802 def get_function_by_name(self, name):
803 for function in self.functions:
804 if function.name == name:
805 return function
806 return None
807
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000808
José Fonseca51c1ef82010-11-15 16:09:14 +0000809Bool = Literal("bool", "Bool")
810SChar = Literal("signed char", "SInt")
811UChar = Literal("unsigned char", "UInt")
812Short = Literal("short", "SInt")
813Int = Literal("int", "SInt")
814Long = Literal("long", "SInt")
815LongLong = Literal("long long", "SInt")
816UShort = Literal("unsigned short", "UInt")
817UInt = Literal("unsigned int", "UInt")
818ULong = Literal("unsigned long", "UInt")
José Fonseca28f034f2010-11-22 20:31:25 +0000819ULongLong = Literal("unsigned long long", "UInt")
José Fonseca51c1ef82010-11-15 16:09:14 +0000820Float = Literal("float", "Float")
José Fonseca9ff74442011-05-07 01:17:49 +0100821Double = Literal("double", "Double")
José Fonseca51c1ef82010-11-15 16:09:14 +0000822SizeT = Literal("size_t", "UInt")
José Fonseca280a1762012-01-31 15:10:13 +0000823
824# C string (i.e., zero terminated)
825CString = String()
826WString = String("wchar_t *", kind="WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900827
José Fonseca2250a0e2010-11-26 15:01:29 +0000828Int8 = Literal("int8_t", "SInt")
829UInt8 = Literal("uint8_t", "UInt")
830Int16 = Literal("int16_t", "SInt")
831UInt16 = Literal("uint16_t", "UInt")
832Int32 = Literal("int32_t", "SInt")
833UInt32 = Literal("uint32_t", "UInt")
834Int64 = Literal("int64_t", "SInt")
835UInt64 = Literal("uint64_t", "UInt")