blob: f36c4e2e58276521be54ec0c9b104bd3d6ed241b [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
Jose Fonsecafdcd30b2016-02-01 14:13:40 +000029import sys
30
José Fonseca8a56d142008-07-09 12:18:08 +090031import debug
32
33
José Fonseca6fac5ae2010-11-29 16:09:13 +000034class Type:
José Fonseca02c25002011-10-15 13:17:26 +010035 """Base class for all types."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000036
José Fonseca02c25002011-10-15 13:17:26 +010037 __tags = set()
José Fonseca6fac5ae2010-11-29 16:09:13 +000038
José Fonseca02c25002011-10-15 13:17:26 +010039 def __init__(self, expr, tag = None):
José Fonseca6fac5ae2010-11-29 16:09:13 +000040 self.expr = expr
José Fonseca6fac5ae2010-11-29 16:09:13 +000041
José Fonseca02c25002011-10-15 13:17:26 +010042 # Generate a default tag, used when naming functions that will operate
43 # on this type, so it should preferrably be something representative of
44 # the type.
45 if tag is None:
José Fonseca5b6fb752012-04-14 14:56:45 +010046 if expr is not None:
47 tag = ''.join([c for c in expr if c.isalnum() or c in '_'])
48 else:
49 tag = 'anonynoums'
José Fonseca02c25002011-10-15 13:17:26 +010050 else:
51 for c in tag:
52 assert c.isalnum() or c in '_'
José Fonseca6fac5ae2010-11-29 16:09:13 +000053
José Fonseca02c25002011-10-15 13:17:26 +010054 # Ensure it is unique.
55 if tag in Type.__tags:
56 suffix = 1
57 while tag + str(suffix) in Type.__tags:
58 suffix += 1
59 tag += str(suffix)
60
61 assert tag not in Type.__tags
62 Type.__tags.add(tag)
63
64 self.tag = tag
José Fonseca6fac5ae2010-11-29 16:09:13 +000065
66 def __str__(self):
José Fonseca02c25002011-10-15 13:17:26 +010067 """Return the C/C++ type expression for this type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000068 return self.expr
69
70 def visit(self, visitor, *args, **kwargs):
71 raise NotImplementedError
72
José Fonseca0075f152012-04-14 20:25:52 +010073 def mutable(self):
74 '''Return a mutable version of this type.
75
76 Convenience wrapper around MutableRebuilder.'''
77 visitor = MutableRebuilder()
78 return visitor.visit(self)
José Fonseca6fac5ae2010-11-29 16:09:13 +000079
José Fonseca13b93432014-11-18 20:21:23 +000080 def depends(self, other):
81 '''Whether this type depends on another.'''
82
83 collector = Collector()
84 collector.visit(self)
85 return other in collector.types
86
José Fonseca6fac5ae2010-11-29 16:09:13 +000087
88class _Void(Type):
José Fonseca02c25002011-10-15 13:17:26 +010089 """Singleton void type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000090
91 def __init__(self):
92 Type.__init__(self, "void")
93
94 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +000095 return visitor.visitVoid(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +000096
97Void = _Void()
98
99
100class Literal(Type):
José Fonseca2f2ea482011-10-15 15:10:06 +0100101 """Class to describe literal types.
José Fonseca6fac5ae2010-11-29 16:09:13 +0000102
José Fonseca2f2ea482011-10-15 15:10:06 +0100103 Types which are not defined in terms of other types, such as integers and
104 floats."""
105
106 def __init__(self, expr, kind):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000107 Type.__init__(self, expr)
José Fonseca2f2ea482011-10-15 15:10:06 +0100108 self.kind = kind
José Fonseca6fac5ae2010-11-29 16:09:13 +0000109
110 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000111 return visitor.visitLiteral(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000112
113
José Fonsecabcfc81b2012-08-07 21:07:22 +0100114Bool = Literal("bool", "Bool")
115SChar = Literal("signed char", "SInt")
116UChar = Literal("unsigned char", "UInt")
117Short = Literal("short", "SInt")
118Int = Literal("int", "SInt")
119Long = Literal("long", "SInt")
120LongLong = Literal("long long", "SInt")
121UShort = Literal("unsigned short", "UInt")
122UInt = Literal("unsigned int", "UInt")
123ULong = Literal("unsigned long", "UInt")
124ULongLong = Literal("unsigned long long", "UInt")
125Float = Literal("float", "Float")
126Double = Literal("double", "Double")
127SizeT = Literal("size_t", "UInt")
128
129Char = Literal("char", "SInt")
130WChar = Literal("wchar_t", "SInt")
131
132Int8 = Literal("int8_t", "SInt")
133UInt8 = Literal("uint8_t", "UInt")
134Int16 = Literal("int16_t", "SInt")
135UInt16 = Literal("uint16_t", "UInt")
136Int32 = Literal("int32_t", "SInt")
137UInt32 = Literal("uint32_t", "UInt")
138Int64 = Literal("int64_t", "SInt")
139UInt64 = Literal("uint64_t", "UInt")
140
José Fonsecacb9d2e02012-10-19 14:51:48 +0100141IntPtr = Literal("intptr_t", "SInt")
142UIntPtr = Literal("uintptr_t", "UInt")
José Fonsecabcfc81b2012-08-07 21:07:22 +0100143
José Fonseca6fac5ae2010-11-29 16:09:13 +0000144class Const(Type):
145
146 def __init__(self, type):
José Fonseca903c2ca2011-09-23 09:43:05 +0100147 # While "const foo" and "foo const" are synonymous, "const foo *" and
148 # "foo * const" are not quite the same, and some compilers do enforce
149 # strict const correctness.
José Fonsecabcfc81b2012-08-07 21:07:22 +0100150 if type.expr.startswith("const ") or '*' in type.expr:
José Fonseca6fac5ae2010-11-29 16:09:13 +0000151 expr = type.expr + " const"
152 else:
José Fonseca903c2ca2011-09-23 09:43:05 +0100153 # The most legible
José Fonseca6fac5ae2010-11-29 16:09:13 +0000154 expr = "const " + type.expr
155
José Fonseca02c25002011-10-15 13:17:26 +0100156 Type.__init__(self, expr, 'C' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000157
158 self.type = type
159
160 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000161 return visitor.visitConst(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000162
163
164class Pointer(Type):
165
166 def __init__(self, type):
José Fonseca02c25002011-10-15 13:17:26 +0100167 Type.__init__(self, type.expr + " *", 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000168 self.type = type
169
170 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000171 return visitor.visitPointer(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000172
173
José Fonseca59ee88e2012-01-15 14:24:10 +0000174class IntPointer(Type):
175 '''Integer encoded as a pointer.'''
176
177 def visit(self, visitor, *args, **kwargs):
178 return visitor.visitIntPointer(self, *args, **kwargs)
179
180
José Fonsecafbcf6832012-04-05 07:10:30 +0100181class ObjPointer(Type):
182 '''Pointer to an object.'''
183
184 def __init__(self, type):
185 Type.__init__(self, type.expr + " *", 'P' + type.tag)
186 self.type = type
187
188 def visit(self, visitor, *args, **kwargs):
189 return visitor.visitObjPointer(self, *args, **kwargs)
190
191
José Fonseca59ee88e2012-01-15 14:24:10 +0000192class LinearPointer(Type):
José Fonsecafbcf6832012-04-05 07:10:30 +0100193 '''Pointer to a linear range of memory.'''
José Fonseca59ee88e2012-01-15 14:24:10 +0000194
195 def __init__(self, type, size = None):
196 Type.__init__(self, type.expr + " *", 'P' + type.tag)
197 self.type = type
198 self.size = size
199
200 def visit(self, visitor, *args, **kwargs):
201 return visitor.visitLinearPointer(self, *args, **kwargs)
202
203
José Fonsecab89c5932012-04-01 22:47:11 +0200204class Reference(Type):
205 '''C++ references.'''
206
207 def __init__(self, type):
208 Type.__init__(self, type.expr + " &", 'R' + type.tag)
209 self.type = type
210
211 def visit(self, visitor, *args, **kwargs):
212 return visitor.visitReference(self, *args, **kwargs)
213
214
José Fonseca6fac5ae2010-11-29 16:09:13 +0000215class Handle(Type):
216
José Fonseca8a844ae2010-12-06 18:50:52 +0000217 def __init__(self, name, type, range=None, key=None):
José Fonseca02c25002011-10-15 13:17:26 +0100218 Type.__init__(self, type.expr, 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000219 self.name = name
220 self.type = type
221 self.range = range
José Fonseca8a844ae2010-12-06 18:50:52 +0000222 self.key = key
José Fonseca6fac5ae2010-11-29 16:09:13 +0000223
224 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000225 return visitor.visitHandle(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000226
227
228def ConstPointer(type):
229 return Pointer(Const(type))
230
231
232class Enum(Type):
233
José Fonseca02c25002011-10-15 13:17:26 +0100234 __id = 0
235
José Fonseca6fac5ae2010-11-29 16:09:13 +0000236 def __init__(self, name, values):
237 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100238
239 self.id = Enum.__id
240 Enum.__id += 1
241
José Fonseca6fac5ae2010-11-29 16:09:13 +0000242 self.values = list(values)
José Fonseca02c25002011-10-15 13:17:26 +0100243
José Fonseca6fac5ae2010-11-29 16:09:13 +0000244 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000245 return visitor.visitEnum(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000246
247
248def FakeEnum(type, values):
249 return Enum(type.expr, values)
250
251
252class Bitmask(Type):
253
José Fonseca02c25002011-10-15 13:17:26 +0100254 __id = 0
255
José Fonseca6fac5ae2010-11-29 16:09:13 +0000256 def __init__(self, type, values):
257 Type.__init__(self, type.expr)
José Fonseca02c25002011-10-15 13:17:26 +0100258
259 self.id = Bitmask.__id
260 Bitmask.__id += 1
261
José Fonseca6fac5ae2010-11-29 16:09:13 +0000262 self.type = type
263 self.values = values
264
265 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000266 return visitor.visitBitmask(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000267
268Flags = Bitmask
269
270
Jose Fonsecaf92ea422016-05-18 16:16:18 +0100271def EnumFlags(name, values):
272 return Flags(Alias(name, UInt), values)
273
274
José Fonseca6fac5ae2010-11-29 16:09:13 +0000275class Array(Type):
276
Jose Fonsecaa9b61382015-06-10 22:04:45 +0100277 def __init__(self, type_, length):
278 Type.__init__(self, type_.expr + " *")
279 self.type = type_
José Fonseca6fac5ae2010-11-29 16:09:13 +0000280 self.length = length
Jose Fonsecaa9b61382015-06-10 22:04:45 +0100281 if not isinstance(length, int):
282 assert isinstance(length, basestring)
283 # Check if length is actually a valid constant expression
284 try:
285 eval(length, {}, {})
286 except:
287 pass
288 else:
289 raise ValueError("length %r should be an integer" % length)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000290
291 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000292 return visitor.visitArray(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000293
294
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200295class AttribArray(Type):
296
José Fonseca48a92b92013-07-20 15:34:24 +0100297 def __init__(self, baseType, valueTypes, terminator = '0'):
José Fonseca77c10d82013-07-20 15:27:29 +0100298 self.baseType = baseType
José Fonseca48a92b92013-07-20 15:34:24 +0100299 Type.__init__(self, (Pointer(self.baseType)).expr)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200300 self.valueTypes = valueTypes
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200301 self.terminator = terminator
302 self.hasKeysWithoutValues = False
303 for key, value in valueTypes:
304 if value is None:
305 self.hasKeysWithoutValues = True
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200306
307 def visit(self, visitor, *args, **kwargs):
308 return visitor.visitAttribArray(self, *args, **kwargs)
309
310
José Fonseca6fac5ae2010-11-29 16:09:13 +0000311class Blob(Type):
312
313 def __init__(self, type, size):
314 Type.__init__(self, type.expr + ' *')
315 self.type = type
316 self.size = size
317
318 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000319 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000320
321
322class Struct(Type):
323
José Fonseca02c25002011-10-15 13:17:26 +0100324 __id = 0
325
José Fonseca6fac5ae2010-11-29 16:09:13 +0000326 def __init__(self, name, members):
327 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100328
329 self.id = Struct.__id
330 Struct.__id += 1
331
José Fonseca6fac5ae2010-11-29 16:09:13 +0000332 self.name = name
José Fonsecadbf714b2012-11-20 17:03:43 +0000333 self.members = members
José Fonseca6fac5ae2010-11-29 16:09:13 +0000334
335 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000336 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000337
338
José Fonsecadbf714b2012-11-20 17:03:43 +0000339def Union(kindExpr, kindTypes, contextLess=True):
José Fonsecaeb216e62012-11-20 11:08:08 +0000340 switchTypes = []
341 for kindCase, kindType, kindMemberName in kindTypes:
342 switchType = Struct(None, [(kindType, kindMemberName)])
343 switchTypes.append((kindCase, switchType))
344 return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)
345
José Fonseca5b6fb752012-04-14 14:56:45 +0100346
José Fonseca6fac5ae2010-11-29 16:09:13 +0000347class Alias(Type):
348
349 def __init__(self, expr, type):
350 Type.__init__(self, expr)
351 self.type = type
352
353 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000354 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000355
José Fonseca6fac5ae2010-11-29 16:09:13 +0000356class Arg:
357
José Fonseca9dd8f702012-04-07 10:42:50 +0100358 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000359 self.type = type
360 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100361 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000362 self.output = output
363 self.index = None
364
365 def __str__(self):
366 return '%s %s' % (self.type, self.name)
367
368
José Fonseca9dd8f702012-04-07 10:42:50 +0100369def In(type, name):
370 return Arg(type, name, input=True, output=False)
371
372def Out(type, name):
373 return Arg(type, name, input=False, output=True)
374
375def InOut(type, name):
376 return Arg(type, name, input=True, output=True)
377
378
José Fonseca6fac5ae2010-11-29 16:09:13 +0000379class Function:
380
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000381 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False, overloaded=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000382 self.type = type
383 self.name = name
384
385 self.args = []
386 index = 0
387 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100388 if not isinstance(arg, Arg):
389 if isinstance(arg, tuple):
390 arg_type, arg_name = arg
391 else:
392 arg_type = arg
393 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000394 arg = Arg(arg_type, arg_name)
395 arg.index = index
396 index += 1
397 self.args.append(arg)
398
399 self.call = call
400 self.fail = fail
401 self.sideeffects = sideeffects
José Fonseca84cea3b2012-05-09 21:12:30 +0100402 self.internal = internal
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000403 self.overloaded = overloaded
José Fonseca6fac5ae2010-11-29 16:09:13 +0000404
405 def prototype(self, name=None):
406 if name is not None:
407 name = name.strip()
408 else:
409 name = self.name
410 s = name
411 if self.call:
412 s = self.call + ' ' + s
413 if name.startswith('*'):
414 s = '(' + s + ')'
415 s = self.type.expr + ' ' + s
416 s += "("
417 if self.args:
418 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
419 else:
420 s += "void"
421 s += ")"
422 return s
423
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000424 def sigName(self):
425 name = self.name
426 if self.overloaded:
427 # suffix used to make overloaded functions/methods unique
428 suffix = ','.join([str(arg.type) for arg in self.args])
429 suffix = suffix.replace(' *', '*')
430 suffix = suffix.replace(' &', '&')
431 suffix = '(' + suffix + ')'
432 name += suffix
433 return name
434
José Fonseca568ecc22012-01-15 13:57:03 +0000435 def argNames(self):
436 return [arg.name for arg in self.args]
437
José Fonseca999284f2013-02-19 13:29:26 +0000438 def getArgByName(self, name):
439 for arg in self.args:
440 if arg.name == name:
441 return arg
442 return None
443
José Fonseca50c2a142015-01-15 13:10:46 +0000444 def getArgByType(self, type):
445 for arg in self.args:
446 if arg.type is type:
447 return arg
448 return None
449
José Fonseca6fac5ae2010-11-29 16:09:13 +0000450
451def StdFunction(*args, **kwargs):
452 kwargs.setdefault('call', '__stdcall')
453 return Function(*args, **kwargs)
454
455
456def FunctionPointer(type, name, args, **kwargs):
457 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
458 return Opaque(name)
459
460
461class Interface(Type):
462
463 def __init__(self, name, base=None):
464 Type.__init__(self, name)
465 self.name = name
466 self.base = base
467 self.methods = []
468
469 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000470 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000471
José Fonsecabd086342012-04-18 19:58:32 +0100472 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100473 for method in self.iterMethods():
474 if method.name == name:
475 return method
José Fonsecabd086342012-04-18 19:58:32 +0100476 return None
477
José Fonseca54f304a2012-01-14 19:33:08 +0000478 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000479 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000480 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000481 yield method
482 for method in self.methods:
483 yield method
484 raise StopIteration
485
José Fonseca143e9252012-04-15 09:31:18 +0100486 def iterBases(self):
487 iface = self
488 while iface is not None:
489 yield iface
490 iface = iface.base
491 raise StopIteration
492
José Fonseca5abf5602013-05-30 14:00:44 +0100493 def hasBase(self, *bases):
494 for iface in self.iterBases():
495 if iface in bases:
496 return True
497 return False
498
José Fonseca4220b1b2012-02-03 19:05:29 +0000499 def iterBaseMethods(self):
500 if self.base is not None:
501 for iface, method in self.base.iterBaseMethods():
502 yield iface, method
503 for method in self.methods:
504 yield self, method
505 raise StopIteration
506
José Fonseca6fac5ae2010-11-29 16:09:13 +0000507
508class Method(Function):
509
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000510 def __init__(self, type, name, args, call = '', const=False, sideeffects=True, overloaded=False):
José Fonseca43aa19f2012-11-10 09:29:38 +0000511 assert call == '__stdcall'
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000512 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects, overloaded=overloaded)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000513 for index in range(len(self.args)):
514 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000515 self.const = const
516
517 def prototype(self, name=None):
518 s = Function.prototype(self, name)
519 if self.const:
520 s += ' const'
521 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000522
José Fonsecabcb26b22012-04-15 08:42:25 +0100523
José Fonseca5b6fb752012-04-14 14:56:45 +0100524def StdMethod(*args, **kwargs):
525 kwargs.setdefault('call', '__stdcall')
526 return Method(*args, **kwargs)
527
José Fonseca6fac5ae2010-11-29 16:09:13 +0000528
José Fonseca6fac5ae2010-11-29 16:09:13 +0000529class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100530 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000531
José Fonsecabcfc81b2012-08-07 21:07:22 +0100532 def __init__(self, type = Char, length = None, wide = False):
533 assert isinstance(type, Type)
534 Type.__init__(self, type.expr + ' *')
535 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000536 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100537 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000538
539 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000540 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000541
José Fonseca6fac5ae2010-11-29 16:09:13 +0000542
543class Opaque(Type):
544 '''Opaque pointer.'''
545
546 def __init__(self, expr):
547 Type.__init__(self, expr)
548
549 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000550 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000551
552
553def OpaquePointer(type, *args):
554 return Opaque(type.expr + ' *')
555
556def OpaqueArray(type, size):
557 return Opaque(type.expr + ' *')
558
559def OpaqueBlob(type, size):
560 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900561
José Fonseca501f2862010-11-19 20:41:18 +0000562
José Fonseca16d46dd2011-10-13 09:52:52 +0100563class Polymorphic(Type):
564
José Fonsecaeb216e62012-11-20 11:08:08 +0000565 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
566 if defaultType is None:
567 Type.__init__(self, None)
568 contextLess = False
569 else:
570 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000571 self.switchExpr = switchExpr
572 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100573 self.defaultType = defaultType
574 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100575
576 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000577 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100578
José Fonseca54f304a2012-01-14 19:33:08 +0000579 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000580 cases = []
581 types = []
582
583 if self.defaultType is not None:
584 cases.append(['default'])
585 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100586
José Fonseca54f304a2012-01-14 19:33:08 +0000587 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100588 case = 'case %s' % expr
589 try:
590 i = types.index(type)
591 except ValueError:
592 cases.append([case])
593 types.append(type)
594 else:
595 cases[i].append(case)
596
597 return zip(cases, types)
598
José Fonseca16d46dd2011-10-13 09:52:52 +0100599
José Fonsecab95e3722012-04-16 14:01:15 +0100600def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
601 enumValues = [expr for expr, type in switchTypes]
602 enum = Enum(enumName, enumValues)
603 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
604 return enum, polymorphic
605
606
José Fonseca501f2862010-11-19 20:41:18 +0000607class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000608 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000609
610 def visit(self, type, *args, **kwargs):
611 return type.visit(self, *args, **kwargs)
612
José Fonseca54f304a2012-01-14 19:33:08 +0000613 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000614 raise NotImplementedError
615
José Fonseca54f304a2012-01-14 19:33:08 +0000616 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000617 raise NotImplementedError
618
José Fonseca54f304a2012-01-14 19:33:08 +0000619 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000620 raise NotImplementedError
621
José Fonseca54f304a2012-01-14 19:33:08 +0000622 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000623 raise NotImplementedError
624
José Fonseca54f304a2012-01-14 19:33:08 +0000625 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000626 raise NotImplementedError
627
José Fonseca54f304a2012-01-14 19:33:08 +0000628 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000629 raise NotImplementedError
630
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200631 def visitAttribArray(self, array, *args, **kwargs):
632 raise NotImplementedError
633
José Fonseca54f304a2012-01-14 19:33:08 +0000634 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000635 raise NotImplementedError
636
José Fonseca54f304a2012-01-14 19:33:08 +0000637 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000638 raise NotImplementedError
639
José Fonseca54f304a2012-01-14 19:33:08 +0000640 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000641 raise NotImplementedError
642
José Fonseca54f304a2012-01-14 19:33:08 +0000643 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000644 raise NotImplementedError
645
José Fonseca59ee88e2012-01-15 14:24:10 +0000646 def visitIntPointer(self, pointer, *args, **kwargs):
647 raise NotImplementedError
648
José Fonsecafbcf6832012-04-05 07:10:30 +0100649 def visitObjPointer(self, pointer, *args, **kwargs):
650 raise NotImplementedError
651
José Fonseca59ee88e2012-01-15 14:24:10 +0000652 def visitLinearPointer(self, pointer, *args, **kwargs):
653 raise NotImplementedError
654
José Fonsecab89c5932012-04-01 22:47:11 +0200655 def visitReference(self, reference, *args, **kwargs):
656 raise NotImplementedError
657
José Fonseca54f304a2012-01-14 19:33:08 +0000658 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000659 raise NotImplementedError
660
José Fonseca54f304a2012-01-14 19:33:08 +0000661 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000662 raise NotImplementedError
663
José Fonseca54f304a2012-01-14 19:33:08 +0000664 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000665 raise NotImplementedError
666
José Fonseca54f304a2012-01-14 19:33:08 +0000667 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000668 raise NotImplementedError
669
José Fonseca54f304a2012-01-14 19:33:08 +0000670 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100671 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000672 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100673
José Fonsecac356d6a2010-11-23 14:27:25 +0000674
675class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000676 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000677
678 def __init__(self):
679 self.__visited = set()
680
681 def visit(self, type, *args, **kwargs):
682 if type not in self.__visited:
683 self.__visited.add(type)
684 return type.visit(self, *args, **kwargs)
685 return None
686
José Fonseca501f2862010-11-19 20:41:18 +0000687
José Fonsecac9edb832010-11-20 09:03:10 +0000688class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000689 '''Visitor which rebuild types as it visits them.
690
691 By itself it is a no-op -- it is intended to be overwritten.
692 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000693
José Fonseca54f304a2012-01-14 19:33:08 +0000694 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000695 return void
696
José Fonseca54f304a2012-01-14 19:33:08 +0000697 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000698 return literal
699
José Fonseca54f304a2012-01-14 19:33:08 +0000700 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100701 string_type = self.visit(string.type)
702 if string_type is string.type:
703 return string
704 else:
705 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000706
José Fonseca54f304a2012-01-14 19:33:08 +0000707 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100708 const_type = self.visit(const.type)
709 if const_type is const.type:
710 return const
711 else:
712 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000713
José Fonseca54f304a2012-01-14 19:33:08 +0000714 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100715 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000716 return Struct(struct.name, members)
717
José Fonseca54f304a2012-01-14 19:33:08 +0000718 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000719 type = self.visit(array.type)
720 return Array(type, array.length)
721
José Fonseca54f304a2012-01-14 19:33:08 +0000722 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000723 type = self.visit(blob.type)
724 return Blob(type, blob.size)
725
José Fonseca54f304a2012-01-14 19:33:08 +0000726 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000727 return enum
728
José Fonseca54f304a2012-01-14 19:33:08 +0000729 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000730 type = self.visit(bitmask.type)
731 return Bitmask(type, bitmask.values)
732
José Fonseca54f304a2012-01-14 19:33:08 +0000733 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100734 pointer_type = self.visit(pointer.type)
735 if pointer_type is pointer.type:
736 return pointer
737 else:
738 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000739
José Fonseca59ee88e2012-01-15 14:24:10 +0000740 def visitIntPointer(self, pointer):
741 return pointer
742
José Fonsecafbcf6832012-04-05 07:10:30 +0100743 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100744 pointer_type = self.visit(pointer.type)
745 if pointer_type is pointer.type:
746 return pointer
747 else:
748 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100749
José Fonseca59ee88e2012-01-15 14:24:10 +0000750 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100751 pointer_type = self.visit(pointer.type)
752 if pointer_type is pointer.type:
753 return pointer
754 else:
755 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000756
José Fonsecab89c5932012-04-01 22:47:11 +0200757 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100758 reference_type = self.visit(reference.type)
759 if reference_type is reference.type:
760 return reference
761 else:
762 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200763
José Fonseca54f304a2012-01-14 19:33:08 +0000764 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100765 handle_type = self.visit(handle.type)
766 if handle_type is handle.type:
767 return handle
768 else:
769 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000770
José Fonseca54f304a2012-01-14 19:33:08 +0000771 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100772 alias_type = self.visit(alias.type)
773 if alias_type is alias.type:
774 return alias
775 else:
776 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000777
José Fonseca54f304a2012-01-14 19:33:08 +0000778 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000779 return opaque
780
José Fonseca7814edf2012-01-31 10:55:49 +0000781 def visitInterface(self, interface, *args, **kwargs):
782 return interface
783
José Fonseca54f304a2012-01-14 19:33:08 +0000784 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000785 switchExpr = polymorphic.switchExpr
786 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000787 if polymorphic.defaultType is None:
788 defaultType = None
789 else:
790 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100791 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100792
José Fonsecac9edb832010-11-20 09:03:10 +0000793
José Fonseca0075f152012-04-14 20:25:52 +0100794class MutableRebuilder(Rebuilder):
795 '''Type visitor which derives a mutable type.'''
796
José Fonsecabcfc81b2012-08-07 21:07:22 +0100797 def visitString(self, string):
798 return string
799
José Fonseca0075f152012-04-14 20:25:52 +0100800 def visitConst(self, const):
801 # Strip out const qualifier
802 return const.type
803
804 def visitAlias(self, alias):
805 # Tear the alias on type changes
806 type = self.visit(alias.type)
807 if type is alias.type:
808 return alias
809 return type
810
811 def visitReference(self, reference):
812 # Strip out references
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000813 return self.visit(reference.type)
José Fonseca0075f152012-04-14 20:25:52 +0100814
815
816class Traverser(Visitor):
817 '''Visitor which all types.'''
818
819 def visitVoid(self, void, *args, **kwargs):
820 pass
821
822 def visitLiteral(self, literal, *args, **kwargs):
823 pass
824
825 def visitString(self, string, *args, **kwargs):
826 pass
827
828 def visitConst(self, const, *args, **kwargs):
829 self.visit(const.type, *args, **kwargs)
830
831 def visitStruct(self, struct, *args, **kwargs):
832 for type, name in struct.members:
833 self.visit(type, *args, **kwargs)
834
835 def visitArray(self, array, *args, **kwargs):
836 self.visit(array.type, *args, **kwargs)
837
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200838 def visitAttribArray(self, attribs, *args, **kwargs):
839 for key, valueType in attribs.valueTypes:
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200840 if valueType is not None:
841 self.visit(valueType, *args, **kwargs)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200842
José Fonseca0075f152012-04-14 20:25:52 +0100843 def visitBlob(self, array, *args, **kwargs):
844 pass
845
846 def visitEnum(self, enum, *args, **kwargs):
847 pass
848
849 def visitBitmask(self, bitmask, *args, **kwargs):
850 self.visit(bitmask.type, *args, **kwargs)
851
852 def visitPointer(self, pointer, *args, **kwargs):
853 self.visit(pointer.type, *args, **kwargs)
854
855 def visitIntPointer(self, pointer, *args, **kwargs):
856 pass
857
858 def visitObjPointer(self, pointer, *args, **kwargs):
859 self.visit(pointer.type, *args, **kwargs)
860
861 def visitLinearPointer(self, pointer, *args, **kwargs):
862 self.visit(pointer.type, *args, **kwargs)
863
864 def visitReference(self, reference, *args, **kwargs):
865 self.visit(reference.type, *args, **kwargs)
866
867 def visitHandle(self, handle, *args, **kwargs):
868 self.visit(handle.type, *args, **kwargs)
869
870 def visitAlias(self, alias, *args, **kwargs):
871 self.visit(alias.type, *args, **kwargs)
872
873 def visitOpaque(self, opaque, *args, **kwargs):
874 pass
875
876 def visitInterface(self, interface, *args, **kwargs):
877 if interface.base is not None:
878 self.visit(interface.base, *args, **kwargs)
879 for method in interface.iterMethods():
880 for arg in method.args:
881 self.visit(arg.type, *args, **kwargs)
882 self.visit(method.type, *args, **kwargs)
883
884 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100885 for expr, type in polymorphic.switchTypes:
886 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000887 if polymorphic.defaultType is not None:
888 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100889
890
891class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000892 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000893
894 def __init__(self):
895 self.__visited = set()
896 self.types = []
897
898 def visit(self, type):
899 if type in self.__visited:
900 return
901 self.__visited.add(type)
902 Visitor.visit(self, type)
903 self.types.append(type)
904
José Fonseca16d46dd2011-10-13 09:52:52 +0100905
José Fonsecadbf714b2012-11-20 17:03:43 +0000906class ExpanderMixin:
907 '''Mixin class that provides a bunch of methods to expand C expressions
908 from the specifications.'''
909
910 __structs = None
911 __indices = None
912
913 def expand(self, expr):
914 # Expand a C expression, replacing certain variables
915 if not isinstance(expr, basestring):
916 return expr
917 variables = {}
918
919 if self.__structs is not None:
920 variables['self'] = '(%s)' % self.__structs[0]
921 if self.__indices is not None:
922 variables['i'] = self.__indices[0]
923
924 expandedExpr = expr.format(**variables)
925 if expandedExpr != expr and 0:
926 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
927 return expandedExpr
928
929 def visitMember(self, member, structInstance, *args, **kwargs):
930 memberType, memberName = member
931 if memberName is None:
932 # Anonymous structure/union member
933 memberInstance = structInstance
934 else:
935 memberInstance = '(%s).%s' % (structInstance, memberName)
936 self.__structs = (structInstance, self.__structs)
937 try:
938 return self.visit(memberType, memberInstance, *args, **kwargs)
939 finally:
940 _, self.__structs = self.__structs
941
942 def visitElement(self, elementIndex, elementType, *args, **kwargs):
943 self.__indices = (elementIndex, self.__indices)
944 try:
945 return self.visit(elementType, *args, **kwargs)
946 finally:
947 _, self.__indices = self.__indices
948
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000949
José Fonseca81301932012-11-11 00:10:20 +0000950class Module:
951 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000952
José Fonseca68ec4122011-02-20 11:25:25 +0000953 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000954 self.name = name
955 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000956 self.functions = []
957 self.interfaces = []
958
José Fonseca54f304a2012-01-14 19:33:08 +0000959 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000960 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000961
José Fonseca54f304a2012-01-14 19:33:08 +0000962 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000963 self.interfaces.extend(interfaces)
964
José Fonseca81301932012-11-11 00:10:20 +0000965 def mergeModule(self, module):
966 self.headers.extend(module.headers)
967 self.functions.extend(module.functions)
968 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000969
José Fonseca1b6c8752012-04-15 14:33:00 +0100970 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000971 for function in self.functions:
972 if function.name == name:
973 return function
974 return None
975
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000976
José Fonseca81301932012-11-11 00:10:20 +0000977class API:
978 '''API abstraction.
979
980 Essentially, a collection of types, functions, and interfaces.
981 '''
982
983 def __init__(self, modules = None):
984 self.modules = []
985 if modules is not None:
986 self.modules.extend(modules)
987
988 def getAllTypes(self):
989 collector = Collector()
990 for module in self.modules:
991 for function in module.functions:
992 for arg in function.args:
993 collector.visit(arg.type)
994 collector.visit(function.type)
995 for interface in module.interfaces:
996 collector.visit(interface)
997 for method in interface.iterMethods():
998 for arg in method.args:
999 collector.visit(arg.type)
1000 collector.visit(method.type)
1001 return collector.types
1002
1003 def getAllFunctions(self):
1004 functions = []
1005 for module in self.modules:
1006 functions.extend(module.functions)
1007 return functions
1008
1009 def getAllInterfaces(self):
1010 types = self.getAllTypes()
1011 interfaces = [type for type in types if isinstance(type, Interface)]
1012 for module in self.modules:
1013 for interface in module.interfaces:
1014 if interface not in interfaces:
1015 interfaces.append(interface)
1016 return interfaces
1017
1018 def addModule(self, module):
1019 self.modules.append(module)
1020
1021 def getFunctionByName(self, name):
1022 for module in self.modules:
1023 for function in module.functions:
1024 if function.name == name:
1025 return function
1026 return None
1027
1028
José Fonseca280a1762012-01-31 15:10:13 +00001029# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +01001030CString = String(Char)
1031WString = String(WChar, wide=True)
1032ConstCString = String(Const(Char))
1033ConstWString = String(Const(WChar), wide=True)