blob: 57e9aa6eb1fcacda7aa77e976b6f1c06dbb9334c [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é Fonsecabd086342012-04-18 19:58:32 +0100404 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100405 for method in self.iterMethods():
406 if method.name == name:
407 return method
José Fonsecabd086342012-04-18 19:58:32 +0100408 return None
409
José Fonseca54f304a2012-01-14 19:33:08 +0000410 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000411 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000412 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000413 yield method
414 for method in self.methods:
415 yield method
416 raise StopIteration
417
José Fonseca143e9252012-04-15 09:31:18 +0100418 def iterBases(self):
419 iface = self
420 while iface is not None:
421 yield iface
422 iface = iface.base
423 raise StopIteration
424
José Fonseca4220b1b2012-02-03 19:05:29 +0000425 def iterBaseMethods(self):
426 if self.base is not None:
427 for iface, method in self.base.iterBaseMethods():
428 yield iface, method
429 for method in self.methods:
430 yield self, method
431 raise StopIteration
432
José Fonseca6fac5ae2010-11-29 16:09:13 +0000433
434class Method(Function):
435
José Fonseca5b6fb752012-04-14 14:56:45 +0100436 def __init__(self, type, name, args, call = '__stdcall', const=False, sideeffects=True):
437 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000438 for index in range(len(self.args)):
439 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000440 self.const = const
441
442 def prototype(self, name=None):
443 s = Function.prototype(self, name)
444 if self.const:
445 s += ' const'
446 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000447
José Fonsecabcb26b22012-04-15 08:42:25 +0100448
José Fonseca5b6fb752012-04-14 14:56:45 +0100449def StdMethod(*args, **kwargs):
450 kwargs.setdefault('call', '__stdcall')
451 return Method(*args, **kwargs)
452
José Fonseca6fac5ae2010-11-29 16:09:13 +0000453
José Fonseca6fac5ae2010-11-29 16:09:13 +0000454class String(Type):
455
José Fonseca280a1762012-01-31 15:10:13 +0000456 def __init__(self, expr = "char *", length = None, kind = 'String'):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000457 Type.__init__(self, expr)
458 self.length = length
José Fonseca280a1762012-01-31 15:10:13 +0000459 self.kind = kind
José Fonseca6fac5ae2010-11-29 16:09:13 +0000460
461 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000462 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000463
José Fonseca6fac5ae2010-11-29 16:09:13 +0000464
465class Opaque(Type):
466 '''Opaque pointer.'''
467
468 def __init__(self, expr):
469 Type.__init__(self, expr)
470
471 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000472 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000473
474
475def OpaquePointer(type, *args):
476 return Opaque(type.expr + ' *')
477
478def OpaqueArray(type, size):
479 return Opaque(type.expr + ' *')
480
481def OpaqueBlob(type, size):
482 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900483
José Fonseca501f2862010-11-19 20:41:18 +0000484
José Fonseca16d46dd2011-10-13 09:52:52 +0100485class Polymorphic(Type):
486
José Fonsecab95e3722012-04-16 14:01:15 +0100487 def __init__(self, switchExpr, switchTypes, defaultType, contextLess=True):
José Fonseca54f304a2012-01-14 19:33:08 +0000488 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000489 self.switchExpr = switchExpr
490 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100491 self.defaultType = defaultType
492 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100493
494 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000495 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100496
José Fonseca54f304a2012-01-14 19:33:08 +0000497 def iterSwitch(self):
José Fonseca46161112011-10-14 10:04:55 +0100498 cases = [['default']]
José Fonseca54f304a2012-01-14 19:33:08 +0000499 types = [self.defaultType]
José Fonseca46161112011-10-14 10:04:55 +0100500
José Fonseca54f304a2012-01-14 19:33:08 +0000501 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100502 case = 'case %s' % expr
503 try:
504 i = types.index(type)
505 except ValueError:
506 cases.append([case])
507 types.append(type)
508 else:
509 cases[i].append(case)
510
511 return zip(cases, types)
512
José Fonseca16d46dd2011-10-13 09:52:52 +0100513
José Fonsecab95e3722012-04-16 14:01:15 +0100514def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
515 enumValues = [expr for expr, type in switchTypes]
516 enum = Enum(enumName, enumValues)
517 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
518 return enum, polymorphic
519
520
José Fonseca501f2862010-11-19 20:41:18 +0000521class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000522 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000523
524 def visit(self, type, *args, **kwargs):
525 return type.visit(self, *args, **kwargs)
526
José Fonseca54f304a2012-01-14 19:33:08 +0000527 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000528 raise NotImplementedError
529
José Fonseca54f304a2012-01-14 19:33:08 +0000530 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000531 raise NotImplementedError
532
José Fonseca54f304a2012-01-14 19:33:08 +0000533 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000534 raise NotImplementedError
535
José Fonseca54f304a2012-01-14 19:33:08 +0000536 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000537 raise NotImplementedError
538
José Fonseca54f304a2012-01-14 19:33:08 +0000539 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000540 raise NotImplementedError
541
José Fonseca54f304a2012-01-14 19:33:08 +0000542 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000543 raise NotImplementedError
544
José Fonseca54f304a2012-01-14 19:33:08 +0000545 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000546 raise NotImplementedError
547
José Fonseca54f304a2012-01-14 19:33:08 +0000548 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000549 raise NotImplementedError
550
José Fonseca54f304a2012-01-14 19:33:08 +0000551 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000552 raise NotImplementedError
553
José Fonseca54f304a2012-01-14 19:33:08 +0000554 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000555 raise NotImplementedError
556
José Fonseca59ee88e2012-01-15 14:24:10 +0000557 def visitIntPointer(self, pointer, *args, **kwargs):
558 raise NotImplementedError
559
José Fonsecafbcf6832012-04-05 07:10:30 +0100560 def visitObjPointer(self, pointer, *args, **kwargs):
561 raise NotImplementedError
562
José Fonseca59ee88e2012-01-15 14:24:10 +0000563 def visitLinearPointer(self, pointer, *args, **kwargs):
564 raise NotImplementedError
565
José Fonsecab89c5932012-04-01 22:47:11 +0200566 def visitReference(self, reference, *args, **kwargs):
567 raise NotImplementedError
568
José Fonseca54f304a2012-01-14 19:33:08 +0000569 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000570 raise NotImplementedError
571
José Fonseca54f304a2012-01-14 19:33:08 +0000572 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000573 raise NotImplementedError
574
José Fonseca54f304a2012-01-14 19:33:08 +0000575 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000576 raise NotImplementedError
577
José Fonseca54f304a2012-01-14 19:33:08 +0000578 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000579 raise NotImplementedError
580
José Fonseca54f304a2012-01-14 19:33:08 +0000581 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100582 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000583 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100584
José Fonsecac356d6a2010-11-23 14:27:25 +0000585
586class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000587 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000588
589 def __init__(self):
590 self.__visited = set()
591
592 def visit(self, type, *args, **kwargs):
593 if type not in self.__visited:
594 self.__visited.add(type)
595 return type.visit(self, *args, **kwargs)
596 return None
597
José Fonseca501f2862010-11-19 20:41:18 +0000598
José Fonsecac9edb832010-11-20 09:03:10 +0000599class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000600 '''Visitor which rebuild types as it visits them.
601
602 By itself it is a no-op -- it is intended to be overwritten.
603 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000604
José Fonseca54f304a2012-01-14 19:33:08 +0000605 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000606 return void
607
José Fonseca54f304a2012-01-14 19:33:08 +0000608 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000609 return literal
610
José Fonseca54f304a2012-01-14 19:33:08 +0000611 def visitString(self, string):
José Fonseca2defc982010-11-22 16:59:10 +0000612 return string
613
José Fonseca54f304a2012-01-14 19:33:08 +0000614 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100615 const_type = self.visit(const.type)
616 if const_type is const.type:
617 return const
618 else:
619 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000620
José Fonseca54f304a2012-01-14 19:33:08 +0000621 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100622 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000623 return Struct(struct.name, members)
624
José Fonseca54f304a2012-01-14 19:33:08 +0000625 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000626 type = self.visit(array.type)
627 return Array(type, array.length)
628
José Fonseca54f304a2012-01-14 19:33:08 +0000629 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000630 type = self.visit(blob.type)
631 return Blob(type, blob.size)
632
José Fonseca54f304a2012-01-14 19:33:08 +0000633 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000634 return enum
635
José Fonseca54f304a2012-01-14 19:33:08 +0000636 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000637 type = self.visit(bitmask.type)
638 return Bitmask(type, bitmask.values)
639
José Fonseca54f304a2012-01-14 19:33:08 +0000640 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100641 pointer_type = self.visit(pointer.type)
642 if pointer_type is pointer.type:
643 return pointer
644 else:
645 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000646
José Fonseca59ee88e2012-01-15 14:24:10 +0000647 def visitIntPointer(self, pointer):
648 return pointer
649
José Fonsecafbcf6832012-04-05 07:10:30 +0100650 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100651 pointer_type = self.visit(pointer.type)
652 if pointer_type is pointer.type:
653 return pointer
654 else:
655 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100656
José Fonseca59ee88e2012-01-15 14:24:10 +0000657 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100658 pointer_type = self.visit(pointer.type)
659 if pointer_type is pointer.type:
660 return pointer
661 else:
662 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000663
José Fonsecab89c5932012-04-01 22:47:11 +0200664 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100665 reference_type = self.visit(reference.type)
666 if reference_type is reference.type:
667 return reference
668 else:
669 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200670
José Fonseca54f304a2012-01-14 19:33:08 +0000671 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100672 handle_type = self.visit(handle.type)
673 if handle_type is handle.type:
674 return handle
675 else:
676 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000677
José Fonseca54f304a2012-01-14 19:33:08 +0000678 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100679 alias_type = self.visit(alias.type)
680 if alias_type is alias.type:
681 return alias
682 else:
683 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000684
José Fonseca54f304a2012-01-14 19:33:08 +0000685 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000686 return opaque
687
José Fonseca7814edf2012-01-31 10:55:49 +0000688 def visitInterface(self, interface, *args, **kwargs):
689 return interface
690
José Fonseca54f304a2012-01-14 19:33:08 +0000691 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000692 switchExpr = polymorphic.switchExpr
693 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecab95e3722012-04-16 14:01:15 +0100694 defaultType = self.visit(polymorphic.defaultType)
695 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100696
José Fonsecac9edb832010-11-20 09:03:10 +0000697
José Fonseca0075f152012-04-14 20:25:52 +0100698class MutableRebuilder(Rebuilder):
699 '''Type visitor which derives a mutable type.'''
700
701 def visitConst(self, const):
702 # Strip out const qualifier
703 return const.type
704
705 def visitAlias(self, alias):
706 # Tear the alias on type changes
707 type = self.visit(alias.type)
708 if type is alias.type:
709 return alias
710 return type
711
712 def visitReference(self, reference):
713 # Strip out references
714 return reference.type
715
716
717class Traverser(Visitor):
718 '''Visitor which all types.'''
719
720 def visitVoid(self, void, *args, **kwargs):
721 pass
722
723 def visitLiteral(self, literal, *args, **kwargs):
724 pass
725
726 def visitString(self, string, *args, **kwargs):
727 pass
728
729 def visitConst(self, const, *args, **kwargs):
730 self.visit(const.type, *args, **kwargs)
731
732 def visitStruct(self, struct, *args, **kwargs):
733 for type, name in struct.members:
734 self.visit(type, *args, **kwargs)
735
736 def visitArray(self, array, *args, **kwargs):
737 self.visit(array.type, *args, **kwargs)
738
739 def visitBlob(self, array, *args, **kwargs):
740 pass
741
742 def visitEnum(self, enum, *args, **kwargs):
743 pass
744
745 def visitBitmask(self, bitmask, *args, **kwargs):
746 self.visit(bitmask.type, *args, **kwargs)
747
748 def visitPointer(self, pointer, *args, **kwargs):
749 self.visit(pointer.type, *args, **kwargs)
750
751 def visitIntPointer(self, pointer, *args, **kwargs):
752 pass
753
754 def visitObjPointer(self, pointer, *args, **kwargs):
755 self.visit(pointer.type, *args, **kwargs)
756
757 def visitLinearPointer(self, pointer, *args, **kwargs):
758 self.visit(pointer.type, *args, **kwargs)
759
760 def visitReference(self, reference, *args, **kwargs):
761 self.visit(reference.type, *args, **kwargs)
762
763 def visitHandle(self, handle, *args, **kwargs):
764 self.visit(handle.type, *args, **kwargs)
765
766 def visitAlias(self, alias, *args, **kwargs):
767 self.visit(alias.type, *args, **kwargs)
768
769 def visitOpaque(self, opaque, *args, **kwargs):
770 pass
771
772 def visitInterface(self, interface, *args, **kwargs):
773 if interface.base is not None:
774 self.visit(interface.base, *args, **kwargs)
775 for method in interface.iterMethods():
776 for arg in method.args:
777 self.visit(arg.type, *args, **kwargs)
778 self.visit(method.type, *args, **kwargs)
779
780 def visitPolymorphic(self, polymorphic, *args, **kwargs):
781 self.visit(polymorphic.defaultType, *args, **kwargs)
782 for expr, type in polymorphic.switchTypes:
783 self.visit(type, *args, **kwargs)
784
785
786class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000787 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000788
789 def __init__(self):
790 self.__visited = set()
791 self.types = []
792
793 def visit(self, type):
794 if type in self.__visited:
795 return
796 self.__visited.add(type)
797 Visitor.visit(self, type)
798 self.types.append(type)
799
José Fonseca16d46dd2011-10-13 09:52:52 +0100800
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000801
802class API:
José Fonseca9c4a2572012-01-13 23:21:10 +0000803 '''API abstraction.
804
805 Essentially, a collection of types, functions, and interfaces.
806 '''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000807
José Fonseca68ec4122011-02-20 11:25:25 +0000808 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000809 self.name = name
810 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000811 self.functions = []
812 self.interfaces = []
813
José Fonseca44703822012-01-31 10:48:58 +0000814 def getAllTypes(self):
José Fonsecae6a50bd2010-11-24 10:12:22 +0000815 collector = Collector()
816 for function in self.functions:
817 for arg in function.args:
818 collector.visit(arg.type)
819 collector.visit(function.type)
820 for interface in self.interfaces:
821 collector.visit(interface)
José Fonseca54f304a2012-01-14 19:33:08 +0000822 for method in interface.iterMethods():
José Fonsecae6a50bd2010-11-24 10:12:22 +0000823 for arg in method.args:
824 collector.visit(arg.type)
825 collector.visit(method.type)
826 return collector.types
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000827
José Fonseca2ef6d5b2012-01-31 10:56:38 +0000828 def getAllInterfaces(self):
829 types = self.getAllTypes()
830 interfaces = [type for type in types if isinstance(type, Interface)]
831 for interface in self.interfaces:
832 if interface not in interfaces:
833 interfaces.append(interface)
834 return interfaces
835
José Fonseca54f304a2012-01-14 19:33:08 +0000836 def addFunction(self, function):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000837 self.functions.append(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000838
José Fonseca54f304a2012-01-14 19:33:08 +0000839 def addFunctions(self, functions):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000840 for function in functions:
José Fonseca54f304a2012-01-14 19:33:08 +0000841 self.addFunction(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000842
José Fonseca54f304a2012-01-14 19:33:08 +0000843 def addInterface(self, interface):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000844 self.interfaces.append(interface)
845
José Fonseca54f304a2012-01-14 19:33:08 +0000846 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000847 self.interfaces.extend(interfaces)
848
José Fonseca54f304a2012-01-14 19:33:08 +0000849 def addApi(self, api):
José Fonseca68ec4122011-02-20 11:25:25 +0000850 self.headers.extend(api.headers)
José Fonseca54f304a2012-01-14 19:33:08 +0000851 self.addFunctions(api.functions)
852 self.addInterfaces(api.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000853
José Fonseca1b6c8752012-04-15 14:33:00 +0100854 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000855 for function in self.functions:
856 if function.name == name:
857 return function
858 return None
859
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000860
José Fonseca51c1ef82010-11-15 16:09:14 +0000861Bool = Literal("bool", "Bool")
862SChar = Literal("signed char", "SInt")
863UChar = Literal("unsigned char", "UInt")
864Short = Literal("short", "SInt")
865Int = Literal("int", "SInt")
866Long = Literal("long", "SInt")
867LongLong = Literal("long long", "SInt")
868UShort = Literal("unsigned short", "UInt")
869UInt = Literal("unsigned int", "UInt")
870ULong = Literal("unsigned long", "UInt")
José Fonseca28f034f2010-11-22 20:31:25 +0000871ULongLong = Literal("unsigned long long", "UInt")
José Fonseca51c1ef82010-11-15 16:09:14 +0000872Float = Literal("float", "Float")
José Fonseca9ff74442011-05-07 01:17:49 +0100873Double = Literal("double", "Double")
José Fonseca51c1ef82010-11-15 16:09:14 +0000874SizeT = Literal("size_t", "UInt")
José Fonseca280a1762012-01-31 15:10:13 +0000875
876# C string (i.e., zero terminated)
877CString = String()
878WString = String("wchar_t *", kind="WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900879
José Fonseca2250a0e2010-11-26 15:01:29 +0000880Int8 = Literal("int8_t", "SInt")
881UInt8 = Literal("uint8_t", "UInt")
882Int16 = Literal("int16_t", "SInt")
883UInt16 = Literal("uint16_t", "UInt")
884Int32 = Literal("int32_t", "SInt")
885UInt32 = Literal("uint32_t", "UInt")
886Int64 = Literal("int64_t", "SInt")
887UInt64 = Literal("uint64_t", "UInt")