blob: 52bbb98c246fa844a7ced6dad9c16d884eb012bf [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
Jose Fonseca37f769f2017-06-16 17:57:39 +0100338 def getMemberByName(self, name):
339 memberNames = [memberName for memberType, memberName in self.members]
340 return memberNames.index(name)
341
José Fonseca6fac5ae2010-11-29 16:09:13 +0000342
José Fonsecadbf714b2012-11-20 17:03:43 +0000343def Union(kindExpr, kindTypes, contextLess=True):
José Fonsecaeb216e62012-11-20 11:08:08 +0000344 switchTypes = []
345 for kindCase, kindType, kindMemberName in kindTypes:
346 switchType = Struct(None, [(kindType, kindMemberName)])
347 switchTypes.append((kindCase, switchType))
348 return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)
349
José Fonseca5b6fb752012-04-14 14:56:45 +0100350
José Fonseca6fac5ae2010-11-29 16:09:13 +0000351class Alias(Type):
352
353 def __init__(self, expr, type):
354 Type.__init__(self, expr)
355 self.type = type
356
357 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000358 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000359
José Fonseca6fac5ae2010-11-29 16:09:13 +0000360class Arg:
361
José Fonseca9dd8f702012-04-07 10:42:50 +0100362 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000363 self.type = type
364 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100365 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000366 self.output = output
367 self.index = None
368
369 def __str__(self):
370 return '%s %s' % (self.type, self.name)
371
372
José Fonseca9dd8f702012-04-07 10:42:50 +0100373def In(type, name):
374 return Arg(type, name, input=True, output=False)
375
376def Out(type, name):
377 return Arg(type, name, input=False, output=True)
378
379def InOut(type, name):
380 return Arg(type, name, input=True, output=True)
381
382
José Fonseca6fac5ae2010-11-29 16:09:13 +0000383class Function:
384
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000385 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False, overloaded=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000386 self.type = type
387 self.name = name
388
389 self.args = []
390 index = 0
391 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100392 if not isinstance(arg, Arg):
393 if isinstance(arg, tuple):
394 arg_type, arg_name = arg
395 else:
396 arg_type = arg
397 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000398 arg = Arg(arg_type, arg_name)
399 arg.index = index
400 index += 1
401 self.args.append(arg)
402
403 self.call = call
404 self.fail = fail
405 self.sideeffects = sideeffects
José Fonseca84cea3b2012-05-09 21:12:30 +0100406 self.internal = internal
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000407 self.overloaded = overloaded
José Fonseca6fac5ae2010-11-29 16:09:13 +0000408
409 def prototype(self, name=None):
410 if name is not None:
411 name = name.strip()
412 else:
413 name = self.name
414 s = name
415 if self.call:
416 s = self.call + ' ' + s
417 if name.startswith('*'):
418 s = '(' + s + ')'
419 s = self.type.expr + ' ' + s
420 s += "("
421 if self.args:
422 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
423 else:
424 s += "void"
425 s += ")"
426 return s
427
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000428 def sigName(self):
429 name = self.name
430 if self.overloaded:
431 # suffix used to make overloaded functions/methods unique
432 suffix = ','.join([str(arg.type) for arg in self.args])
433 suffix = suffix.replace(' *', '*')
434 suffix = suffix.replace(' &', '&')
435 suffix = '(' + suffix + ')'
436 name += suffix
437 return name
438
José Fonseca568ecc22012-01-15 13:57:03 +0000439 def argNames(self):
440 return [arg.name for arg in self.args]
441
José Fonseca999284f2013-02-19 13:29:26 +0000442 def getArgByName(self, name):
443 for arg in self.args:
444 if arg.name == name:
445 return arg
446 return None
447
José Fonseca50c2a142015-01-15 13:10:46 +0000448 def getArgByType(self, type):
449 for arg in self.args:
450 if arg.type is type:
451 return arg
452 return None
453
José Fonseca6fac5ae2010-11-29 16:09:13 +0000454
455def StdFunction(*args, **kwargs):
456 kwargs.setdefault('call', '__stdcall')
457 return Function(*args, **kwargs)
458
459
460def FunctionPointer(type, name, args, **kwargs):
461 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
462 return Opaque(name)
463
464
465class Interface(Type):
466
467 def __init__(self, name, base=None):
468 Type.__init__(self, name)
469 self.name = name
470 self.base = base
471 self.methods = []
472
473 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000474 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000475
José Fonsecabd086342012-04-18 19:58:32 +0100476 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100477 for method in self.iterMethods():
478 if method.name == name:
479 return method
José Fonsecabd086342012-04-18 19:58:32 +0100480 return None
481
José Fonseca54f304a2012-01-14 19:33:08 +0000482 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000483 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000484 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000485 yield method
486 for method in self.methods:
487 yield method
488 raise StopIteration
489
José Fonseca143e9252012-04-15 09:31:18 +0100490 def iterBases(self):
491 iface = self
492 while iface is not None:
493 yield iface
494 iface = iface.base
495 raise StopIteration
496
José Fonseca5abf5602013-05-30 14:00:44 +0100497 def hasBase(self, *bases):
498 for iface in self.iterBases():
499 if iface in bases:
500 return True
501 return False
502
José Fonseca4220b1b2012-02-03 19:05:29 +0000503 def iterBaseMethods(self):
504 if self.base is not None:
505 for iface, method in self.base.iterBaseMethods():
506 yield iface, method
507 for method in self.methods:
508 yield self, method
509 raise StopIteration
510
José Fonseca6fac5ae2010-11-29 16:09:13 +0000511
512class Method(Function):
513
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000514 def __init__(self, type, name, args, call = '', const=False, sideeffects=True, overloaded=False):
José Fonseca43aa19f2012-11-10 09:29:38 +0000515 assert call == '__stdcall'
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000516 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects, overloaded=overloaded)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000517 for index in range(len(self.args)):
518 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000519 self.const = const
520
521 def prototype(self, name=None):
522 s = Function.prototype(self, name)
523 if self.const:
524 s += ' const'
525 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000526
José Fonsecabcb26b22012-04-15 08:42:25 +0100527
José Fonseca5b6fb752012-04-14 14:56:45 +0100528def StdMethod(*args, **kwargs):
529 kwargs.setdefault('call', '__stdcall')
530 return Method(*args, **kwargs)
531
José Fonseca6fac5ae2010-11-29 16:09:13 +0000532
José Fonseca6fac5ae2010-11-29 16:09:13 +0000533class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100534 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000535
José Fonsecabcfc81b2012-08-07 21:07:22 +0100536 def __init__(self, type = Char, length = None, wide = False):
537 assert isinstance(type, Type)
538 Type.__init__(self, type.expr + ' *')
539 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000540 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100541 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000542
543 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000544 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000545
José Fonseca6fac5ae2010-11-29 16:09:13 +0000546
547class Opaque(Type):
548 '''Opaque pointer.'''
549
550 def __init__(self, expr):
551 Type.__init__(self, expr)
552
553 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000554 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000555
556
557def OpaquePointer(type, *args):
558 return Opaque(type.expr + ' *')
559
560def OpaqueArray(type, size):
561 return Opaque(type.expr + ' *')
562
563def OpaqueBlob(type, size):
564 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900565
José Fonseca501f2862010-11-19 20:41:18 +0000566
José Fonseca16d46dd2011-10-13 09:52:52 +0100567class Polymorphic(Type):
568
José Fonsecaeb216e62012-11-20 11:08:08 +0000569 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
570 if defaultType is None:
571 Type.__init__(self, None)
572 contextLess = False
573 else:
574 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000575 self.switchExpr = switchExpr
576 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100577 self.defaultType = defaultType
578 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100579
580 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000581 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100582
José Fonseca54f304a2012-01-14 19:33:08 +0000583 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000584 cases = []
585 types = []
586
587 if self.defaultType is not None:
588 cases.append(['default'])
589 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100590
José Fonseca54f304a2012-01-14 19:33:08 +0000591 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100592 case = 'case %s' % expr
593 try:
594 i = types.index(type)
595 except ValueError:
596 cases.append([case])
597 types.append(type)
598 else:
599 cases[i].append(case)
600
601 return zip(cases, types)
602
José Fonseca16d46dd2011-10-13 09:52:52 +0100603
José Fonsecab95e3722012-04-16 14:01:15 +0100604def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
605 enumValues = [expr for expr, type in switchTypes]
606 enum = Enum(enumName, enumValues)
607 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
608 return enum, polymorphic
609
610
José Fonseca501f2862010-11-19 20:41:18 +0000611class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000612 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000613
614 def visit(self, type, *args, **kwargs):
615 return type.visit(self, *args, **kwargs)
616
José Fonseca54f304a2012-01-14 19:33:08 +0000617 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000618 raise NotImplementedError
619
José Fonseca54f304a2012-01-14 19:33:08 +0000620 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000621 raise NotImplementedError
622
José Fonseca54f304a2012-01-14 19:33:08 +0000623 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000624 raise NotImplementedError
625
José Fonseca54f304a2012-01-14 19:33:08 +0000626 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000627 raise NotImplementedError
628
José Fonseca54f304a2012-01-14 19:33:08 +0000629 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000630 raise NotImplementedError
631
José Fonseca54f304a2012-01-14 19:33:08 +0000632 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000633 raise NotImplementedError
634
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200635 def visitAttribArray(self, array, *args, **kwargs):
636 raise NotImplementedError
637
José Fonseca54f304a2012-01-14 19:33:08 +0000638 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000639 raise NotImplementedError
640
José Fonseca54f304a2012-01-14 19:33:08 +0000641 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000642 raise NotImplementedError
643
José Fonseca54f304a2012-01-14 19:33:08 +0000644 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000645 raise NotImplementedError
646
José Fonseca54f304a2012-01-14 19:33:08 +0000647 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000648 raise NotImplementedError
649
José Fonseca59ee88e2012-01-15 14:24:10 +0000650 def visitIntPointer(self, pointer, *args, **kwargs):
651 raise NotImplementedError
652
José Fonsecafbcf6832012-04-05 07:10:30 +0100653 def visitObjPointer(self, pointer, *args, **kwargs):
654 raise NotImplementedError
655
José Fonseca59ee88e2012-01-15 14:24:10 +0000656 def visitLinearPointer(self, pointer, *args, **kwargs):
657 raise NotImplementedError
658
José Fonsecab89c5932012-04-01 22:47:11 +0200659 def visitReference(self, reference, *args, **kwargs):
660 raise NotImplementedError
661
José Fonseca54f304a2012-01-14 19:33:08 +0000662 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000663 raise NotImplementedError
664
José Fonseca54f304a2012-01-14 19:33:08 +0000665 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000666 raise NotImplementedError
667
José Fonseca54f304a2012-01-14 19:33:08 +0000668 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000669 raise NotImplementedError
670
José Fonseca54f304a2012-01-14 19:33:08 +0000671 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000672 raise NotImplementedError
673
José Fonseca54f304a2012-01-14 19:33:08 +0000674 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100675 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000676 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100677
José Fonsecac356d6a2010-11-23 14:27:25 +0000678
679class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000680 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000681
682 def __init__(self):
683 self.__visited = set()
684
685 def visit(self, type, *args, **kwargs):
686 if type not in self.__visited:
687 self.__visited.add(type)
688 return type.visit(self, *args, **kwargs)
689 return None
690
José Fonseca501f2862010-11-19 20:41:18 +0000691
José Fonsecac9edb832010-11-20 09:03:10 +0000692class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000693 '''Visitor which rebuild types as it visits them.
694
695 By itself it is a no-op -- it is intended to be overwritten.
696 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000697
José Fonseca54f304a2012-01-14 19:33:08 +0000698 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000699 return void
700
José Fonseca54f304a2012-01-14 19:33:08 +0000701 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000702 return literal
703
José Fonseca54f304a2012-01-14 19:33:08 +0000704 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100705 string_type = self.visit(string.type)
706 if string_type is string.type:
707 return string
708 else:
709 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000710
José Fonseca54f304a2012-01-14 19:33:08 +0000711 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100712 const_type = self.visit(const.type)
713 if const_type is const.type:
714 return const
715 else:
716 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000717
José Fonseca54f304a2012-01-14 19:33:08 +0000718 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100719 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000720 return Struct(struct.name, members)
721
José Fonseca54f304a2012-01-14 19:33:08 +0000722 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000723 type = self.visit(array.type)
724 return Array(type, array.length)
725
José Fonseca54f304a2012-01-14 19:33:08 +0000726 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000727 type = self.visit(blob.type)
728 return Blob(type, blob.size)
729
José Fonseca54f304a2012-01-14 19:33:08 +0000730 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000731 return enum
732
José Fonseca54f304a2012-01-14 19:33:08 +0000733 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000734 type = self.visit(bitmask.type)
735 return Bitmask(type, bitmask.values)
736
José Fonseca54f304a2012-01-14 19:33:08 +0000737 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100738 pointer_type = self.visit(pointer.type)
739 if pointer_type is pointer.type:
740 return pointer
741 else:
742 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000743
José Fonseca59ee88e2012-01-15 14:24:10 +0000744 def visitIntPointer(self, pointer):
745 return pointer
746
José Fonsecafbcf6832012-04-05 07:10:30 +0100747 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100748 pointer_type = self.visit(pointer.type)
749 if pointer_type is pointer.type:
750 return pointer
751 else:
752 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100753
José Fonseca59ee88e2012-01-15 14:24:10 +0000754 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100755 pointer_type = self.visit(pointer.type)
756 if pointer_type is pointer.type:
757 return pointer
758 else:
Jose Fonseca42628ef2017-05-23 17:29:02 +0100759 return LinearPointer(pointer_type, self.size)
José Fonseca59ee88e2012-01-15 14:24:10 +0000760
José Fonsecab89c5932012-04-01 22:47:11 +0200761 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100762 reference_type = self.visit(reference.type)
763 if reference_type is reference.type:
764 return reference
765 else:
766 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200767
José Fonseca54f304a2012-01-14 19:33:08 +0000768 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100769 handle_type = self.visit(handle.type)
770 if handle_type is handle.type:
771 return handle
772 else:
773 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000774
José Fonseca54f304a2012-01-14 19:33:08 +0000775 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100776 alias_type = self.visit(alias.type)
777 if alias_type is alias.type:
778 return alias
779 else:
780 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000781
José Fonseca54f304a2012-01-14 19:33:08 +0000782 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000783 return opaque
784
José Fonseca7814edf2012-01-31 10:55:49 +0000785 def visitInterface(self, interface, *args, **kwargs):
786 return interface
787
José Fonseca54f304a2012-01-14 19:33:08 +0000788 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000789 switchExpr = polymorphic.switchExpr
790 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000791 if polymorphic.defaultType is None:
792 defaultType = None
793 else:
794 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100795 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100796
José Fonsecac9edb832010-11-20 09:03:10 +0000797
José Fonseca0075f152012-04-14 20:25:52 +0100798class MutableRebuilder(Rebuilder):
799 '''Type visitor which derives a mutable type.'''
800
José Fonsecabcfc81b2012-08-07 21:07:22 +0100801 def visitString(self, string):
802 return string
803
José Fonseca0075f152012-04-14 20:25:52 +0100804 def visitConst(self, const):
805 # Strip out const qualifier
806 return const.type
807
808 def visitAlias(self, alias):
809 # Tear the alias on type changes
810 type = self.visit(alias.type)
811 if type is alias.type:
812 return alias
813 return type
814
815 def visitReference(self, reference):
816 # Strip out references
Jose Fonsecafdcd30b2016-02-01 14:13:40 +0000817 return self.visit(reference.type)
José Fonseca0075f152012-04-14 20:25:52 +0100818
819
820class Traverser(Visitor):
821 '''Visitor which all types.'''
822
823 def visitVoid(self, void, *args, **kwargs):
824 pass
825
826 def visitLiteral(self, literal, *args, **kwargs):
827 pass
828
829 def visitString(self, string, *args, **kwargs):
830 pass
831
832 def visitConst(self, const, *args, **kwargs):
833 self.visit(const.type, *args, **kwargs)
834
835 def visitStruct(self, struct, *args, **kwargs):
836 for type, name in struct.members:
837 self.visit(type, *args, **kwargs)
838
839 def visitArray(self, array, *args, **kwargs):
840 self.visit(array.type, *args, **kwargs)
841
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200842 def visitAttribArray(self, attribs, *args, **kwargs):
843 for key, valueType in attribs.valueTypes:
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200844 if valueType is not None:
845 self.visit(valueType, *args, **kwargs)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200846
José Fonseca0075f152012-04-14 20:25:52 +0100847 def visitBlob(self, array, *args, **kwargs):
848 pass
849
850 def visitEnum(self, enum, *args, **kwargs):
851 pass
852
853 def visitBitmask(self, bitmask, *args, **kwargs):
854 self.visit(bitmask.type, *args, **kwargs)
855
856 def visitPointer(self, pointer, *args, **kwargs):
857 self.visit(pointer.type, *args, **kwargs)
858
859 def visitIntPointer(self, pointer, *args, **kwargs):
860 pass
861
862 def visitObjPointer(self, pointer, *args, **kwargs):
863 self.visit(pointer.type, *args, **kwargs)
864
865 def visitLinearPointer(self, pointer, *args, **kwargs):
866 self.visit(pointer.type, *args, **kwargs)
867
868 def visitReference(self, reference, *args, **kwargs):
869 self.visit(reference.type, *args, **kwargs)
870
871 def visitHandle(self, handle, *args, **kwargs):
872 self.visit(handle.type, *args, **kwargs)
873
874 def visitAlias(self, alias, *args, **kwargs):
875 self.visit(alias.type, *args, **kwargs)
876
877 def visitOpaque(self, opaque, *args, **kwargs):
878 pass
879
880 def visitInterface(self, interface, *args, **kwargs):
881 if interface.base is not None:
882 self.visit(interface.base, *args, **kwargs)
883 for method in interface.iterMethods():
884 for arg in method.args:
885 self.visit(arg.type, *args, **kwargs)
886 self.visit(method.type, *args, **kwargs)
887
888 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100889 for expr, type in polymorphic.switchTypes:
890 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000891 if polymorphic.defaultType is not None:
892 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100893
894
895class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000896 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000897
898 def __init__(self):
899 self.__visited = set()
900 self.types = []
901
902 def visit(self, type):
903 if type in self.__visited:
904 return
905 self.__visited.add(type)
906 Visitor.visit(self, type)
907 self.types.append(type)
908
José Fonseca16d46dd2011-10-13 09:52:52 +0100909
José Fonsecadbf714b2012-11-20 17:03:43 +0000910class ExpanderMixin:
911 '''Mixin class that provides a bunch of methods to expand C expressions
912 from the specifications.'''
913
914 __structs = None
915 __indices = None
916
917 def expand(self, expr):
918 # Expand a C expression, replacing certain variables
919 if not isinstance(expr, basestring):
920 return expr
921 variables = {}
922
923 if self.__structs is not None:
924 variables['self'] = '(%s)' % self.__structs[0]
925 if self.__indices is not None:
926 variables['i'] = self.__indices[0]
927
928 expandedExpr = expr.format(**variables)
929 if expandedExpr != expr and 0:
930 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
931 return expandedExpr
932
933 def visitMember(self, member, structInstance, *args, **kwargs):
934 memberType, memberName = member
935 if memberName is None:
936 # Anonymous structure/union member
937 memberInstance = structInstance
938 else:
939 memberInstance = '(%s).%s' % (structInstance, memberName)
940 self.__structs = (structInstance, self.__structs)
941 try:
942 return self.visit(memberType, memberInstance, *args, **kwargs)
943 finally:
944 _, self.__structs = self.__structs
945
946 def visitElement(self, elementIndex, elementType, *args, **kwargs):
947 self.__indices = (elementIndex, self.__indices)
948 try:
949 return self.visit(elementType, *args, **kwargs)
950 finally:
951 _, self.__indices = self.__indices
952
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000953
José Fonseca81301932012-11-11 00:10:20 +0000954class Module:
955 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000956
José Fonseca68ec4122011-02-20 11:25:25 +0000957 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000958 self.name = name
959 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000960 self.functions = []
961 self.interfaces = []
962
José Fonseca54f304a2012-01-14 19:33:08 +0000963 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000964 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000965
José Fonseca54f304a2012-01-14 19:33:08 +0000966 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000967 self.interfaces.extend(interfaces)
968
José Fonseca81301932012-11-11 00:10:20 +0000969 def mergeModule(self, module):
970 self.headers.extend(module.headers)
971 self.functions.extend(module.functions)
972 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000973
José Fonseca1b6c8752012-04-15 14:33:00 +0100974 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000975 for function in self.functions:
976 if function.name == name:
977 return function
978 return None
979
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000980
José Fonseca81301932012-11-11 00:10:20 +0000981class API:
982 '''API abstraction.
983
984 Essentially, a collection of types, functions, and interfaces.
985 '''
986
987 def __init__(self, modules = None):
988 self.modules = []
989 if modules is not None:
990 self.modules.extend(modules)
991
992 def getAllTypes(self):
993 collector = Collector()
994 for module in self.modules:
995 for function in module.functions:
996 for arg in function.args:
997 collector.visit(arg.type)
998 collector.visit(function.type)
999 for interface in module.interfaces:
1000 collector.visit(interface)
1001 for method in interface.iterMethods():
1002 for arg in method.args:
1003 collector.visit(arg.type)
1004 collector.visit(method.type)
1005 return collector.types
1006
1007 def getAllFunctions(self):
1008 functions = []
1009 for module in self.modules:
1010 functions.extend(module.functions)
1011 return functions
1012
1013 def getAllInterfaces(self):
1014 types = self.getAllTypes()
1015 interfaces = [type for type in types if isinstance(type, Interface)]
1016 for module in self.modules:
1017 for interface in module.interfaces:
1018 if interface not in interfaces:
1019 interfaces.append(interface)
1020 return interfaces
1021
1022 def addModule(self, module):
1023 self.modules.append(module)
1024
1025 def getFunctionByName(self, name):
1026 for module in self.modules:
1027 for function in module.functions:
1028 if function.name == name:
1029 return function
1030 return None
1031
1032
José Fonseca280a1762012-01-31 15:10:13 +00001033# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +01001034CString = String(Char)
1035WString = String(WChar, wide=True)
1036ConstCString = String(Const(Char))
1037ConstWString = String(Const(WChar), wide=True)