blob: 2e4e5d86c2b89c52a798735e7040dd439c3aa0d3 [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
José Fonseca0075f152012-04-14 20:25:52 +010071 def mutable(self):
72 '''Return a mutable version of this type.
73
74 Convenience wrapper around MutableRebuilder.'''
75 visitor = MutableRebuilder()
76 return visitor.visit(self)
José Fonseca6fac5ae2010-11-29 16:09:13 +000077
78
79class _Void(Type):
José Fonseca02c25002011-10-15 13:17:26 +010080 """Singleton void type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000081
82 def __init__(self):
83 Type.__init__(self, "void")
84
85 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +000086 return visitor.visitVoid(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +000087
88Void = _Void()
89
90
91class Literal(Type):
José Fonseca2f2ea482011-10-15 15:10:06 +010092 """Class to describe literal types.
José Fonseca6fac5ae2010-11-29 16:09:13 +000093
José Fonseca2f2ea482011-10-15 15:10:06 +010094 Types which are not defined in terms of other types, such as integers and
95 floats."""
96
97 def __init__(self, expr, kind):
José Fonseca6fac5ae2010-11-29 16:09:13 +000098 Type.__init__(self, expr)
José Fonseca2f2ea482011-10-15 15:10:06 +010099 self.kind = kind
José Fonseca6fac5ae2010-11-29 16:09:13 +0000100
101 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000102 return visitor.visitLiteral(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000103
104
105class Const(Type):
106
107 def __init__(self, type):
José Fonseca903c2ca2011-09-23 09:43:05 +0100108 # While "const foo" and "foo const" are synonymous, "const foo *" and
109 # "foo * const" are not quite the same, and some compilers do enforce
110 # strict const correctness.
111 if isinstance(type, String) or type is WString:
112 # For strings we never intend to say a const pointer to chars, but
113 # rather a point to const chars.
114 expr = "const " + type.expr
115 elif type.expr.startswith("const ") or '*' in type.expr:
José Fonseca6fac5ae2010-11-29 16:09:13 +0000116 expr = type.expr + " const"
117 else:
José Fonseca903c2ca2011-09-23 09:43:05 +0100118 # The most legible
José Fonseca6fac5ae2010-11-29 16:09:13 +0000119 expr = "const " + type.expr
120
José Fonseca02c25002011-10-15 13:17:26 +0100121 Type.__init__(self, expr, 'C' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000122
123 self.type = type
124
125 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000126 return visitor.visitConst(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000127
128
129class Pointer(Type):
130
131 def __init__(self, type):
José Fonseca02c25002011-10-15 13:17:26 +0100132 Type.__init__(self, type.expr + " *", 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000133 self.type = type
134
135 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000136 return visitor.visitPointer(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000137
138
José Fonseca59ee88e2012-01-15 14:24:10 +0000139class IntPointer(Type):
140 '''Integer encoded as a pointer.'''
141
142 def visit(self, visitor, *args, **kwargs):
143 return visitor.visitIntPointer(self, *args, **kwargs)
144
145
José Fonsecafbcf6832012-04-05 07:10:30 +0100146class ObjPointer(Type):
147 '''Pointer to an object.'''
148
149 def __init__(self, type):
150 Type.__init__(self, type.expr + " *", 'P' + type.tag)
151 self.type = type
152
153 def visit(self, visitor, *args, **kwargs):
154 return visitor.visitObjPointer(self, *args, **kwargs)
155
156
José Fonseca59ee88e2012-01-15 14:24:10 +0000157class LinearPointer(Type):
José Fonsecafbcf6832012-04-05 07:10:30 +0100158 '''Pointer to a linear range of memory.'''
José Fonseca59ee88e2012-01-15 14:24:10 +0000159
160 def __init__(self, type, size = None):
161 Type.__init__(self, type.expr + " *", 'P' + type.tag)
162 self.type = type
163 self.size = size
164
165 def visit(self, visitor, *args, **kwargs):
166 return visitor.visitLinearPointer(self, *args, **kwargs)
167
168
José Fonsecab89c5932012-04-01 22:47:11 +0200169class Reference(Type):
170 '''C++ references.'''
171
172 def __init__(self, type):
173 Type.__init__(self, type.expr + " &", 'R' + type.tag)
174 self.type = type
175
176 def visit(self, visitor, *args, **kwargs):
177 return visitor.visitReference(self, *args, **kwargs)
178
179
José Fonseca6fac5ae2010-11-29 16:09:13 +0000180class Handle(Type):
181
José Fonseca8a844ae2010-12-06 18:50:52 +0000182 def __init__(self, name, type, range=None, key=None):
José Fonseca02c25002011-10-15 13:17:26 +0100183 Type.__init__(self, type.expr, 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000184 self.name = name
185 self.type = type
186 self.range = range
José Fonseca8a844ae2010-12-06 18:50:52 +0000187 self.key = key
José Fonseca6fac5ae2010-11-29 16:09:13 +0000188
189 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000190 return visitor.visitHandle(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000191
192
193def ConstPointer(type):
194 return Pointer(Const(type))
195
196
197class Enum(Type):
198
José Fonseca02c25002011-10-15 13:17:26 +0100199 __id = 0
200
José Fonseca6fac5ae2010-11-29 16:09:13 +0000201 def __init__(self, name, values):
202 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100203
204 self.id = Enum.__id
205 Enum.__id += 1
206
José Fonseca6fac5ae2010-11-29 16:09:13 +0000207 self.values = list(values)
José Fonseca02c25002011-10-15 13:17:26 +0100208
José Fonseca6fac5ae2010-11-29 16:09:13 +0000209 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000210 return visitor.visitEnum(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000211
212
213def FakeEnum(type, values):
214 return Enum(type.expr, values)
215
216
217class Bitmask(Type):
218
José Fonseca02c25002011-10-15 13:17:26 +0100219 __id = 0
220
José Fonseca6fac5ae2010-11-29 16:09:13 +0000221 def __init__(self, type, values):
222 Type.__init__(self, type.expr)
José Fonseca02c25002011-10-15 13:17:26 +0100223
224 self.id = Bitmask.__id
225 Bitmask.__id += 1
226
José Fonseca6fac5ae2010-11-29 16:09:13 +0000227 self.type = type
228 self.values = values
229
230 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000231 return visitor.visitBitmask(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000232
233Flags = Bitmask
234
235
236class Array(Type):
237
238 def __init__(self, type, length):
239 Type.__init__(self, type.expr + " *")
240 self.type = type
241 self.length = length
242
243 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000244 return visitor.visitArray(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000245
246
247class Blob(Type):
248
249 def __init__(self, type, size):
250 Type.__init__(self, type.expr + ' *')
251 self.type = type
252 self.size = size
253
254 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000255 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000256
257
258class Struct(Type):
259
José Fonseca02c25002011-10-15 13:17:26 +0100260 __id = 0
261
José Fonseca6fac5ae2010-11-29 16:09:13 +0000262 def __init__(self, name, members):
263 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100264
265 self.id = Struct.__id
266 Struct.__id += 1
267
José Fonseca6fac5ae2010-11-29 16:09:13 +0000268 self.name = name
José Fonseca5b6fb752012-04-14 14:56:45 +0100269 self.members = []
270
271 # Eliminate anonymous unions
272 for type, name in members:
273 if name is not None:
274 self.members.append((type, name))
275 else:
276 assert isinstance(type, Union)
277 assert type.name is None
278 self.members.extend(type.members)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000279
280 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000281 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000282
283
José Fonseca5b6fb752012-04-14 14:56:45 +0100284class Union(Type):
285
286 __id = 0
287
288 def __init__(self, name, members):
289 Type.__init__(self, name)
290
291 self.id = Union.__id
292 Union.__id += 1
293
294 self.name = name
295 self.members = members
296
297
José Fonseca6fac5ae2010-11-29 16:09:13 +0000298class Alias(Type):
299
300 def __init__(self, expr, type):
301 Type.__init__(self, expr)
302 self.type = type
303
304 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000305 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000306
José Fonseca6fac5ae2010-11-29 16:09:13 +0000307class Arg:
308
José Fonseca9dd8f702012-04-07 10:42:50 +0100309 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000310 self.type = type
311 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100312 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000313 self.output = output
314 self.index = None
315
316 def __str__(self):
317 return '%s %s' % (self.type, self.name)
318
319
José Fonseca9dd8f702012-04-07 10:42:50 +0100320def In(type, name):
321 return Arg(type, name, input=True, output=False)
322
323def Out(type, name):
324 return Arg(type, name, input=False, output=True)
325
326def InOut(type, name):
327 return Arg(type, name, input=True, output=True)
328
329
José Fonseca6fac5ae2010-11-29 16:09:13 +0000330class Function:
331
José Fonseca46a48392011-10-14 11:34:27 +0100332 # 0-3 are reserved to memcpy, malloc, free, and realloc
333 __id = 4
José Fonseca6fac5ae2010-11-29 16:09:13 +0000334
José Fonsecabca0f412011-04-24 20:53:38 +0100335 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000336 self.id = Function.__id
337 Function.__id += 1
338
339 self.type = type
340 self.name = name
341
342 self.args = []
343 index = 0
344 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100345 if not isinstance(arg, Arg):
346 if isinstance(arg, tuple):
347 arg_type, arg_name = arg
348 else:
349 arg_type = arg
350 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000351 arg = Arg(arg_type, arg_name)
352 arg.index = index
353 index += 1
354 self.args.append(arg)
355
356 self.call = call
357 self.fail = fail
358 self.sideeffects = sideeffects
José Fonseca6fac5ae2010-11-29 16:09:13 +0000359
360 def prototype(self, name=None):
361 if name is not None:
362 name = name.strip()
363 else:
364 name = self.name
365 s = name
366 if self.call:
367 s = self.call + ' ' + s
368 if name.startswith('*'):
369 s = '(' + s + ')'
370 s = self.type.expr + ' ' + s
371 s += "("
372 if self.args:
373 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
374 else:
375 s += "void"
376 s += ")"
377 return s
378
José Fonseca568ecc22012-01-15 13:57:03 +0000379 def argNames(self):
380 return [arg.name for arg in self.args]
381
José Fonseca6fac5ae2010-11-29 16:09:13 +0000382
383def StdFunction(*args, **kwargs):
384 kwargs.setdefault('call', '__stdcall')
385 return Function(*args, **kwargs)
386
387
388def FunctionPointer(type, name, args, **kwargs):
389 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
390 return Opaque(name)
391
392
393class Interface(Type):
394
395 def __init__(self, name, base=None):
396 Type.__init__(self, name)
397 self.name = name
398 self.base = base
399 self.methods = []
400
401 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000402 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000403
José Fonseca54f304a2012-01-14 19:33:08 +0000404 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000405 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000406 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000407 yield method
408 for method in self.methods:
409 yield method
410 raise StopIteration
411
José Fonseca4220b1b2012-02-03 19:05:29 +0000412 def iterBaseMethods(self):
413 if self.base is not None:
414 for iface, method in self.base.iterBaseMethods():
415 yield iface, method
416 for method in self.methods:
417 yield self, method
418 raise StopIteration
419
José Fonseca6fac5ae2010-11-29 16:09:13 +0000420
421class Method(Function):
422
José Fonseca5b6fb752012-04-14 14:56:45 +0100423 def __init__(self, type, name, args, call = '__stdcall', const=False, sideeffects=True):
424 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000425 for index in range(len(self.args)):
426 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000427 self.const = const
428
429 def prototype(self, name=None):
430 s = Function.prototype(self, name)
431 if self.const:
432 s += ' const'
433 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000434
José Fonseca5b6fb752012-04-14 14:56:45 +0100435def StdMethod(*args, **kwargs):
436 kwargs.setdefault('call', '__stdcall')
437 return Method(*args, **kwargs)
438
José Fonseca6fac5ae2010-11-29 16:09:13 +0000439
José Fonseca6fac5ae2010-11-29 16:09:13 +0000440class String(Type):
441
José Fonseca280a1762012-01-31 15:10:13 +0000442 def __init__(self, expr = "char *", length = None, kind = 'String'):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000443 Type.__init__(self, expr)
444 self.length = length
José Fonseca280a1762012-01-31 15:10:13 +0000445 self.kind = kind
José Fonseca6fac5ae2010-11-29 16:09:13 +0000446
447 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000448 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000449
José Fonseca6fac5ae2010-11-29 16:09:13 +0000450
451class Opaque(Type):
452 '''Opaque pointer.'''
453
454 def __init__(self, expr):
455 Type.__init__(self, expr)
456
457 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000458 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000459
460
461def OpaquePointer(type, *args):
462 return Opaque(type.expr + ' *')
463
464def OpaqueArray(type, size):
465 return Opaque(type.expr + ' *')
466
467def OpaqueBlob(type, size):
468 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900469
José Fonseca501f2862010-11-19 20:41:18 +0000470
José Fonseca16d46dd2011-10-13 09:52:52 +0100471class Polymorphic(Type):
472
José Fonseca54f304a2012-01-14 19:33:08 +0000473 def __init__(self, defaultType, switchExpr, switchTypes):
474 Type.__init__(self, defaultType.expr)
475 self.defaultType = defaultType
476 self.switchExpr = switchExpr
477 self.switchTypes = switchTypes
José Fonseca16d46dd2011-10-13 09:52:52 +0100478
479 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000480 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100481
José Fonseca54f304a2012-01-14 19:33:08 +0000482 def iterSwitch(self):
José Fonseca46161112011-10-14 10:04:55 +0100483 cases = [['default']]
José Fonseca54f304a2012-01-14 19:33:08 +0000484 types = [self.defaultType]
José Fonseca46161112011-10-14 10:04:55 +0100485
José Fonseca54f304a2012-01-14 19:33:08 +0000486 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100487 case = 'case %s' % expr
488 try:
489 i = types.index(type)
490 except ValueError:
491 cases.append([case])
492 types.append(type)
493 else:
494 cases[i].append(case)
495
496 return zip(cases, types)
497
José Fonseca16d46dd2011-10-13 09:52:52 +0100498
José Fonseca501f2862010-11-19 20:41:18 +0000499class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000500 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000501
502 def visit(self, type, *args, **kwargs):
503 return type.visit(self, *args, **kwargs)
504
José Fonseca54f304a2012-01-14 19:33:08 +0000505 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000506 raise NotImplementedError
507
José Fonseca54f304a2012-01-14 19:33:08 +0000508 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000509 raise NotImplementedError
510
José Fonseca54f304a2012-01-14 19:33:08 +0000511 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000512 raise NotImplementedError
513
José Fonseca54f304a2012-01-14 19:33:08 +0000514 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000515 raise NotImplementedError
516
José Fonseca54f304a2012-01-14 19:33:08 +0000517 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000518 raise NotImplementedError
519
José Fonseca54f304a2012-01-14 19:33:08 +0000520 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000521 raise NotImplementedError
522
José Fonseca54f304a2012-01-14 19:33:08 +0000523 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000524 raise NotImplementedError
525
José Fonseca54f304a2012-01-14 19:33:08 +0000526 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000527 raise NotImplementedError
528
José Fonseca54f304a2012-01-14 19:33:08 +0000529 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000530 raise NotImplementedError
531
José Fonseca54f304a2012-01-14 19:33:08 +0000532 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000533 raise NotImplementedError
534
José Fonseca59ee88e2012-01-15 14:24:10 +0000535 def visitIntPointer(self, pointer, *args, **kwargs):
536 raise NotImplementedError
537
José Fonsecafbcf6832012-04-05 07:10:30 +0100538 def visitObjPointer(self, pointer, *args, **kwargs):
539 raise NotImplementedError
540
José Fonseca59ee88e2012-01-15 14:24:10 +0000541 def visitLinearPointer(self, pointer, *args, **kwargs):
542 raise NotImplementedError
543
José Fonsecab89c5932012-04-01 22:47:11 +0200544 def visitReference(self, reference, *args, **kwargs):
545 raise NotImplementedError
546
José Fonseca54f304a2012-01-14 19:33:08 +0000547 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000548 raise NotImplementedError
549
José Fonseca54f304a2012-01-14 19:33:08 +0000550 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000551 raise NotImplementedError
552
José Fonseca54f304a2012-01-14 19:33:08 +0000553 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000554 raise NotImplementedError
555
José Fonseca54f304a2012-01-14 19:33:08 +0000556 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000557 raise NotImplementedError
558
José Fonseca54f304a2012-01-14 19:33:08 +0000559 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100560 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000561 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100562
José Fonsecac356d6a2010-11-23 14:27:25 +0000563
564class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000565 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000566
567 def __init__(self):
568 self.__visited = set()
569
570 def visit(self, type, *args, **kwargs):
571 if type not in self.__visited:
572 self.__visited.add(type)
573 return type.visit(self, *args, **kwargs)
574 return None
575
José Fonseca501f2862010-11-19 20:41:18 +0000576
José Fonsecac9edb832010-11-20 09:03:10 +0000577class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000578 '''Visitor which rebuild types as it visits them.
579
580 By itself it is a no-op -- it is intended to be overwritten.
581 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000582
José Fonseca54f304a2012-01-14 19:33:08 +0000583 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000584 return void
585
José Fonseca54f304a2012-01-14 19:33:08 +0000586 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000587 return literal
588
José Fonseca54f304a2012-01-14 19:33:08 +0000589 def visitString(self, string):
José Fonseca2defc982010-11-22 16:59:10 +0000590 return string
591
José Fonseca54f304a2012-01-14 19:33:08 +0000592 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100593 const_type = self.visit(const.type)
594 if const_type is const.type:
595 return const
596 else:
597 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000598
José Fonseca54f304a2012-01-14 19:33:08 +0000599 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100600 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000601 return Struct(struct.name, members)
602
José Fonseca54f304a2012-01-14 19:33:08 +0000603 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000604 type = self.visit(array.type)
605 return Array(type, array.length)
606
José Fonseca54f304a2012-01-14 19:33:08 +0000607 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000608 type = self.visit(blob.type)
609 return Blob(type, blob.size)
610
José Fonseca54f304a2012-01-14 19:33:08 +0000611 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000612 return enum
613
José Fonseca54f304a2012-01-14 19:33:08 +0000614 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000615 type = self.visit(bitmask.type)
616 return Bitmask(type, bitmask.values)
617
José Fonseca54f304a2012-01-14 19:33:08 +0000618 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100619 pointer_type = self.visit(pointer.type)
620 if pointer_type is pointer.type:
621 return pointer
622 else:
623 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000624
José Fonseca59ee88e2012-01-15 14:24:10 +0000625 def visitIntPointer(self, pointer):
626 return pointer
627
José Fonsecafbcf6832012-04-05 07:10:30 +0100628 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100629 pointer_type = self.visit(pointer.type)
630 if pointer_type is pointer.type:
631 return pointer
632 else:
633 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100634
José Fonseca59ee88e2012-01-15 14:24:10 +0000635 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100636 pointer_type = self.visit(pointer.type)
637 if pointer_type is pointer.type:
638 return pointer
639 else:
640 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000641
José Fonsecab89c5932012-04-01 22:47:11 +0200642 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100643 reference_type = self.visit(reference.type)
644 if reference_type is reference.type:
645 return reference
646 else:
647 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200648
José Fonseca54f304a2012-01-14 19:33:08 +0000649 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100650 handle_type = self.visit(handle.type)
651 if handle_type is handle.type:
652 return handle
653 else:
654 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000655
José Fonseca54f304a2012-01-14 19:33:08 +0000656 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100657 alias_type = self.visit(alias.type)
658 if alias_type is alias.type:
659 return alias
660 else:
661 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000662
José Fonseca54f304a2012-01-14 19:33:08 +0000663 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000664 return opaque
665
José Fonseca7814edf2012-01-31 10:55:49 +0000666 def visitInterface(self, interface, *args, **kwargs):
667 return interface
668
José Fonseca54f304a2012-01-14 19:33:08 +0000669 def visitPolymorphic(self, polymorphic):
670 defaultType = self.visit(polymorphic.defaultType)
671 switchExpr = polymorphic.switchExpr
672 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
673 return Polymorphic(defaultType, switchExpr, switchTypes)
José Fonseca16d46dd2011-10-13 09:52:52 +0100674
José Fonsecac9edb832010-11-20 09:03:10 +0000675
José Fonseca0075f152012-04-14 20:25:52 +0100676class MutableRebuilder(Rebuilder):
677 '''Type visitor which derives a mutable type.'''
678
679 def visitConst(self, const):
680 # Strip out const qualifier
681 return const.type
682
683 def visitAlias(self, alias):
684 # Tear the alias on type changes
685 type = self.visit(alias.type)
686 if type is alias.type:
687 return alias
688 return type
689
690 def visitReference(self, reference):
691 # Strip out references
692 return reference.type
693
694
695class Traverser(Visitor):
696 '''Visitor which all types.'''
697
698 def visitVoid(self, void, *args, **kwargs):
699 pass
700
701 def visitLiteral(self, literal, *args, **kwargs):
702 pass
703
704 def visitString(self, string, *args, **kwargs):
705 pass
706
707 def visitConst(self, const, *args, **kwargs):
708 self.visit(const.type, *args, **kwargs)
709
710 def visitStruct(self, struct, *args, **kwargs):
711 for type, name in struct.members:
712 self.visit(type, *args, **kwargs)
713
714 def visitArray(self, array, *args, **kwargs):
715 self.visit(array.type, *args, **kwargs)
716
717 def visitBlob(self, array, *args, **kwargs):
718 pass
719
720 def visitEnum(self, enum, *args, **kwargs):
721 pass
722
723 def visitBitmask(self, bitmask, *args, **kwargs):
724 self.visit(bitmask.type, *args, **kwargs)
725
726 def visitPointer(self, pointer, *args, **kwargs):
727 self.visit(pointer.type, *args, **kwargs)
728
729 def visitIntPointer(self, pointer, *args, **kwargs):
730 pass
731
732 def visitObjPointer(self, pointer, *args, **kwargs):
733 self.visit(pointer.type, *args, **kwargs)
734
735 def visitLinearPointer(self, pointer, *args, **kwargs):
736 self.visit(pointer.type, *args, **kwargs)
737
738 def visitReference(self, reference, *args, **kwargs):
739 self.visit(reference.type, *args, **kwargs)
740
741 def visitHandle(self, handle, *args, **kwargs):
742 self.visit(handle.type, *args, **kwargs)
743
744 def visitAlias(self, alias, *args, **kwargs):
745 self.visit(alias.type, *args, **kwargs)
746
747 def visitOpaque(self, opaque, *args, **kwargs):
748 pass
749
750 def visitInterface(self, interface, *args, **kwargs):
751 if interface.base is not None:
752 self.visit(interface.base, *args, **kwargs)
753 for method in interface.iterMethods():
754 for arg in method.args:
755 self.visit(arg.type, *args, **kwargs)
756 self.visit(method.type, *args, **kwargs)
757
758 def visitPolymorphic(self, polymorphic, *args, **kwargs):
759 self.visit(polymorphic.defaultType, *args, **kwargs)
760 for expr, type in polymorphic.switchTypes:
761 self.visit(type, *args, **kwargs)
762
763
764class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000765 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000766
767 def __init__(self):
768 self.__visited = set()
769 self.types = []
770
771 def visit(self, type):
772 if type in self.__visited:
773 return
774 self.__visited.add(type)
775 Visitor.visit(self, type)
776 self.types.append(type)
777
José Fonseca16d46dd2011-10-13 09:52:52 +0100778
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000779
780class API:
José Fonseca9c4a2572012-01-13 23:21:10 +0000781 '''API abstraction.
782
783 Essentially, a collection of types, functions, and interfaces.
784 '''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000785
José Fonseca68ec4122011-02-20 11:25:25 +0000786 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000787 self.name = name
788 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000789 self.functions = []
790 self.interfaces = []
791
José Fonseca44703822012-01-31 10:48:58 +0000792 def getAllTypes(self):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000793 collector = Collector()
794 for function in self.functions:
795 for arg in function.args:
796 collector.visit(arg.type)
797 collector.visit(function.type)
798 for interface in self.interfaces:
799 collector.visit(interface)
José Fonseca54f304a2012-01-14 19:33:08 +0000800 for method in interface.iterMethods():
José Fonsecae6a50bd2010-11-24 10:12:22 +0000801 for arg in method.args:
802 collector.visit(arg.type)
803 collector.visit(method.type)
804 return collector.types
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000805
José Fonseca2ef6d5b2012-01-31 10:56:38 +0000806 def getAllInterfaces(self):
807 types = self.getAllTypes()
808 interfaces = [type for type in types if isinstance(type, Interface)]
809 for interface in self.interfaces:
810 if interface not in interfaces:
811 interfaces.append(interface)
812 return interfaces
813
José Fonseca54f304a2012-01-14 19:33:08 +0000814 def addFunction(self, function):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000815 self.functions.append(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000816
José Fonseca54f304a2012-01-14 19:33:08 +0000817 def addFunctions(self, functions):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000818 for function in functions:
José Fonseca54f304a2012-01-14 19:33:08 +0000819 self.addFunction(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000820
José Fonseca54f304a2012-01-14 19:33:08 +0000821 def addInterface(self, interface):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000822 self.interfaces.append(interface)
823
José Fonseca54f304a2012-01-14 19:33:08 +0000824 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000825 self.interfaces.extend(interfaces)
826
José Fonseca54f304a2012-01-14 19:33:08 +0000827 def addApi(self, api):
José Fonseca68ec4122011-02-20 11:25:25 +0000828 self.headers.extend(api.headers)
José Fonseca54f304a2012-01-14 19:33:08 +0000829 self.addFunctions(api.functions)
830 self.addInterfaces(api.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000831
José Fonsecaeccec3e2011-02-20 09:01:25 +0000832 def get_function_by_name(self, name):
833 for function in self.functions:
834 if function.name == name:
835 return function
836 return None
837
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000838
José Fonseca51c1ef82010-11-15 16:09:14 +0000839Bool = Literal("bool", "Bool")
840SChar = Literal("signed char", "SInt")
841UChar = Literal("unsigned char", "UInt")
842Short = Literal("short", "SInt")
843Int = Literal("int", "SInt")
844Long = Literal("long", "SInt")
845LongLong = Literal("long long", "SInt")
846UShort = Literal("unsigned short", "UInt")
847UInt = Literal("unsigned int", "UInt")
848ULong = Literal("unsigned long", "UInt")
José Fonseca28f034f2010-11-22 20:31:25 +0000849ULongLong = Literal("unsigned long long", "UInt")
José Fonseca51c1ef82010-11-15 16:09:14 +0000850Float = Literal("float", "Float")
José Fonseca9ff74442011-05-07 01:17:49 +0100851Double = Literal("double", "Double")
José Fonseca51c1ef82010-11-15 16:09:14 +0000852SizeT = Literal("size_t", "UInt")
José Fonseca280a1762012-01-31 15:10:13 +0000853
854# C string (i.e., zero terminated)
855CString = String()
856WString = String("wchar_t *", kind="WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900857
José Fonseca2250a0e2010-11-26 15:01:29 +0000858Int8 = Literal("int8_t", "SInt")
859UInt8 = Literal("uint8_t", "UInt")
860Int16 = Literal("int16_t", "SInt")
861UInt16 = Literal("uint16_t", "UInt")
862Int32 = Literal("int32_t", "SInt")
863UInt32 = Literal("uint32_t", "UInt")
864Int64 = Literal("int64_t", "SInt")
865UInt64 = Literal("uint64_t", "UInt")