]> git.cworth.org Git - apitrace/blob - specs/stdapi.py
specs: Allow an API to spread across multiple modules.
[apitrace] / specs / stdapi.py
1 ##########################################################################
2 #
3 # Copyright 2008-2010 VMware, Inc.
4 # All Rights Reserved.
5 #
6 # 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:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # 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.
23 #
24 ##########################################################################/
25
26 """C basic types"""
27
28
29 import debug
30
31
32 class Type:
33     """Base class for all types."""
34
35     __tags = set()
36
37     def __init__(self, expr, tag = None):
38         self.expr = expr
39
40         # 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:
44             if expr is not None:
45                 tag = ''.join([c for c in expr if c.isalnum() or c in '_'])
46             else:
47                 tag = 'anonynoums'
48         else:
49             for c in tag:
50                 assert c.isalnum() or c in '_'
51
52         # 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
63
64     def __str__(self):
65         """Return the C/C++ type expression for this type."""
66         return self.expr
67
68     def visit(self, visitor, *args, **kwargs):
69         raise NotImplementedError
70
71     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)
77
78
79 class _Void(Type):
80     """Singleton void type."""
81
82     def __init__(self):
83         Type.__init__(self, "void")
84
85     def visit(self, visitor, *args, **kwargs):
86         return visitor.visitVoid(self, *args, **kwargs)
87
88 Void = _Void()
89
90
91 class Literal(Type):
92     """Class to describe literal types.
93
94     Types which are not defined in terms of other types, such as integers and
95     floats."""
96
97     def __init__(self, expr, kind):
98         Type.__init__(self, expr)
99         self.kind = kind
100
101     def visit(self, visitor, *args, **kwargs):
102         return visitor.visitLiteral(self, *args, **kwargs)
103
104
105 Bool = Literal("bool", "Bool")
106 SChar = Literal("signed char", "SInt")
107 UChar = Literal("unsigned char", "UInt")
108 Short = Literal("short", "SInt")
109 Int = Literal("int", "SInt")
110 Long = Literal("long", "SInt")
111 LongLong = Literal("long long", "SInt")
112 UShort = Literal("unsigned short", "UInt")
113 UInt = Literal("unsigned int", "UInt")
114 ULong = Literal("unsigned long", "UInt")
115 ULongLong = Literal("unsigned long long", "UInt")
116 Float = Literal("float", "Float")
117 Double = Literal("double", "Double")
118 SizeT = Literal("size_t", "UInt")
119
120 Char = Literal("char", "SInt")
121 WChar = Literal("wchar_t", "SInt")
122
123 Int8 = Literal("int8_t", "SInt")
124 UInt8 = Literal("uint8_t", "UInt")
125 Int16 = Literal("int16_t", "SInt")
126 UInt16 = Literal("uint16_t", "UInt")
127 Int32 = Literal("int32_t", "SInt")
128 UInt32 = Literal("uint32_t", "UInt")
129 Int64 = Literal("int64_t", "SInt")
130 UInt64 = Literal("uint64_t", "UInt")
131
132 IntPtr = Literal("intptr_t", "SInt")
133 UIntPtr = Literal("uintptr_t", "UInt")
134
135 class Const(Type):
136
137     def __init__(self, type):
138         # While "const foo" and "foo const" are synonymous, "const foo *" and
139         # "foo * const" are not quite the same, and some compilers do enforce
140         # strict const correctness.
141         if type.expr.startswith("const ") or '*' in type.expr:
142             expr = type.expr + " const"
143         else:
144             # The most legible
145             expr = "const " + type.expr
146
147         Type.__init__(self, expr, 'C' + type.tag)
148
149         self.type = type
150
151     def visit(self, visitor, *args, **kwargs):
152         return visitor.visitConst(self, *args, **kwargs)
153
154
155 class Pointer(Type):
156
157     def __init__(self, type):
158         Type.__init__(self, type.expr + " *", 'P' + type.tag)
159         self.type = type
160
161     def visit(self, visitor, *args, **kwargs):
162         return visitor.visitPointer(self, *args, **kwargs)
163
164
165 class IntPointer(Type):
166     '''Integer encoded as a pointer.'''
167
168     def visit(self, visitor, *args, **kwargs):
169         return visitor.visitIntPointer(self, *args, **kwargs)
170
171
172 class ObjPointer(Type):
173     '''Pointer to an object.'''
174
175     def __init__(self, type):
176         Type.__init__(self, type.expr + " *", 'P' + type.tag)
177         self.type = type
178
179     def visit(self, visitor, *args, **kwargs):
180         return visitor.visitObjPointer(self, *args, **kwargs)
181
182
183 class LinearPointer(Type):
184     '''Pointer to a linear range of memory.'''
185
186     def __init__(self, type, size = None):
187         Type.__init__(self, type.expr + " *", 'P' + type.tag)
188         self.type = type
189         self.size = size
190
191     def visit(self, visitor, *args, **kwargs):
192         return visitor.visitLinearPointer(self, *args, **kwargs)
193
194
195 class Reference(Type):
196     '''C++ references.'''
197
198     def __init__(self, type):
199         Type.__init__(self, type.expr + " &", 'R' + type.tag)
200         self.type = type
201
202     def visit(self, visitor, *args, **kwargs):
203         return visitor.visitReference(self, *args, **kwargs)
204
205
206 class Handle(Type):
207
208     def __init__(self, name, type, range=None, key=None):
209         Type.__init__(self, type.expr, 'P' + type.tag)
210         self.name = name
211         self.type = type
212         self.range = range
213         self.key = key
214
215     def visit(self, visitor, *args, **kwargs):
216         return visitor.visitHandle(self, *args, **kwargs)
217
218
219 def ConstPointer(type):
220     return Pointer(Const(type))
221
222
223 class Enum(Type):
224
225     __id = 0
226
227     def __init__(self, name, values):
228         Type.__init__(self, name)
229
230         self.id = Enum.__id
231         Enum.__id += 1
232
233         self.values = list(values)
234
235     def visit(self, visitor, *args, **kwargs):
236         return visitor.visitEnum(self, *args, **kwargs)
237
238
239 def FakeEnum(type, values):
240     return Enum(type.expr, values)
241
242
243 class Bitmask(Type):
244
245     __id = 0
246
247     def __init__(self, type, values):
248         Type.__init__(self, type.expr)
249
250         self.id = Bitmask.__id
251         Bitmask.__id += 1
252
253         self.type = type
254         self.values = values
255
256     def visit(self, visitor, *args, **kwargs):
257         return visitor.visitBitmask(self, *args, **kwargs)
258
259 Flags = Bitmask
260
261
262 class Array(Type):
263
264     def __init__(self, type, length):
265         Type.__init__(self, type.expr + " *")
266         self.type = type
267         self.length = length
268
269     def visit(self, visitor, *args, **kwargs):
270         return visitor.visitArray(self, *args, **kwargs)
271
272
273 class Blob(Type):
274
275     def __init__(self, type, size):
276         Type.__init__(self, type.expr + ' *')
277         self.type = type
278         self.size = size
279
280     def visit(self, visitor, *args, **kwargs):
281         return visitor.visitBlob(self, *args, **kwargs)
282
283
284 class Struct(Type):
285
286     __id = 0
287
288     def __init__(self, name, members):
289         Type.__init__(self, name)
290
291         self.id = Struct.__id
292         Struct.__id += 1
293
294         self.name = name
295         self.members = []
296
297         # Eliminate anonymous unions
298         for type, name in members:
299             if name is not None:
300                 self.members.append((type, name))
301             else:
302                 assert isinstance(type, Union)
303                 assert type.name is None
304                 self.members.extend(type.members)
305
306     def visit(self, visitor, *args, **kwargs):
307         return visitor.visitStruct(self, *args, **kwargs)
308
309
310 class Union(Type):
311
312     __id = 0
313
314     def __init__(self, name, members):
315         Type.__init__(self, name)
316
317         self.id = Union.__id
318         Union.__id += 1
319
320         self.name = name
321         self.members = members
322
323
324 class Alias(Type):
325
326     def __init__(self, expr, type):
327         Type.__init__(self, expr)
328         self.type = type
329
330     def visit(self, visitor, *args, **kwargs):
331         return visitor.visitAlias(self, *args, **kwargs)
332
333 class Arg:
334
335     def __init__(self, type, name, input=True, output=False):
336         self.type = type
337         self.name = name
338         self.input = input
339         self.output = output
340         self.index = None
341
342     def __str__(self):
343         return '%s %s' % (self.type, self.name)
344
345
346 def In(type, name):
347     return Arg(type, name, input=True, output=False)
348
349 def Out(type, name):
350     return Arg(type, name, input=False, output=True)
351
352 def InOut(type, name):
353     return Arg(type, name, input=True, output=True)
354
355
356 class Function:
357
358     # 0-3 are reserved to memcpy, malloc, free, and realloc
359     __id = 4
360
361     def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
362         self.id = Function.__id
363         Function.__id += 1
364
365         self.type = type
366         self.name = name
367
368         self.args = []
369         index = 0
370         for arg in args:
371             if not isinstance(arg, Arg):
372                 if isinstance(arg, tuple):
373                     arg_type, arg_name = arg
374                 else:
375                     arg_type = arg
376                     arg_name = "arg%u" % index
377                 arg = Arg(arg_type, arg_name)
378             arg.index = index
379             index += 1
380             self.args.append(arg)
381
382         self.call = call
383         self.fail = fail
384         self.sideeffects = sideeffects
385         self.internal = internal
386
387     def prototype(self, name=None):
388         if name is not None:
389             name = name.strip()
390         else:
391             name = self.name
392         s = name
393         if self.call:
394             s = self.call + ' ' + s
395         if name.startswith('*'):
396             s = '(' + s + ')'
397         s = self.type.expr + ' ' + s
398         s += "("
399         if self.args:
400             s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
401         else:
402             s += "void"
403         s += ")"
404         return s
405
406     def argNames(self):
407         return [arg.name for arg in self.args]
408
409
410 def StdFunction(*args, **kwargs):
411     kwargs.setdefault('call', '__stdcall')
412     return Function(*args, **kwargs)
413
414
415 def FunctionPointer(type, name, args, **kwargs):
416     # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
417     return Opaque(name)
418
419
420 class Interface(Type):
421
422     def __init__(self, name, base=None):
423         Type.__init__(self, name)
424         self.name = name
425         self.base = base
426         self.methods = []
427
428     def visit(self, visitor, *args, **kwargs):
429         return visitor.visitInterface(self, *args, **kwargs)
430
431     def getMethodByName(self, name):
432         for method in self.iterMethods():
433             if method.name == name:
434                 return method
435         return None
436
437     def iterMethods(self):
438         if self.base is not None:
439             for method in self.base.iterMethods():
440                 yield method
441         for method in self.methods:
442             yield method
443         raise StopIteration
444
445     def iterBases(self):
446         iface = self
447         while iface is not None:
448             yield iface
449             iface = iface.base
450         raise StopIteration
451
452     def iterBaseMethods(self):
453         if self.base is not None:
454             for iface, method in self.base.iterBaseMethods():
455                 yield iface, method
456         for method in self.methods:
457             yield self, method
458         raise StopIteration
459
460
461 class Method(Function):
462
463     def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
464         assert call == '__stdcall'
465         Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
466         for index in range(len(self.args)):
467             self.args[index].index = index + 1
468         self.const = const
469
470     def prototype(self, name=None):
471         s = Function.prototype(self, name)
472         if self.const:
473             s += ' const'
474         return s
475
476
477 def StdMethod(*args, **kwargs):
478     kwargs.setdefault('call', '__stdcall')
479     return Method(*args, **kwargs)
480
481
482 class String(Type):
483     '''Human-legible character string.'''
484
485     def __init__(self, type = Char, length = None, wide = False):
486         assert isinstance(type, Type)
487         Type.__init__(self, type.expr + ' *')
488         self.type = type
489         self.length = length
490         self.wide = wide
491
492     def visit(self, visitor, *args, **kwargs):
493         return visitor.visitString(self, *args, **kwargs)
494
495
496 class Opaque(Type):
497     '''Opaque pointer.'''
498
499     def __init__(self, expr):
500         Type.__init__(self, expr)
501
502     def visit(self, visitor, *args, **kwargs):
503         return visitor.visitOpaque(self, *args, **kwargs)
504
505
506 def OpaquePointer(type, *args):
507     return Opaque(type.expr + ' *')
508
509 def OpaqueArray(type, size):
510     return Opaque(type.expr + ' *')
511
512 def OpaqueBlob(type, size):
513     return Opaque(type.expr + ' *')
514
515
516 class Polymorphic(Type):
517
518     def __init__(self, switchExpr, switchTypes, defaultType, contextLess=True):
519         Type.__init__(self, defaultType.expr)
520         self.switchExpr = switchExpr
521         self.switchTypes = switchTypes
522         self.defaultType = defaultType
523         self.contextLess = contextLess
524
525     def visit(self, visitor, *args, **kwargs):
526         return visitor.visitPolymorphic(self, *args, **kwargs)
527
528     def iterSwitch(self):
529         cases = [['default']]
530         types = [self.defaultType]
531
532         for expr, type in self.switchTypes:
533             case = 'case %s' % expr
534             try:
535                 i = types.index(type)
536             except ValueError:
537                 cases.append([case])
538                 types.append(type)
539             else:
540                 cases[i].append(case)
541
542         return zip(cases, types)
543
544
545 def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
546     enumValues = [expr for expr, type in switchTypes]
547     enum = Enum(enumName, enumValues)
548     polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
549     return enum, polymorphic
550
551
552 class Visitor:
553     '''Abstract visitor for the type hierarchy.'''
554
555     def visit(self, type, *args, **kwargs):
556         return type.visit(self, *args, **kwargs)
557
558     def visitVoid(self, void, *args, **kwargs):
559         raise NotImplementedError
560
561     def visitLiteral(self, literal, *args, **kwargs):
562         raise NotImplementedError
563
564     def visitString(self, string, *args, **kwargs):
565         raise NotImplementedError
566
567     def visitConst(self, const, *args, **kwargs):
568         raise NotImplementedError
569
570     def visitStruct(self, struct, *args, **kwargs):
571         raise NotImplementedError
572
573     def visitArray(self, array, *args, **kwargs):
574         raise NotImplementedError
575
576     def visitBlob(self, blob, *args, **kwargs):
577         raise NotImplementedError
578
579     def visitEnum(self, enum, *args, **kwargs):
580         raise NotImplementedError
581
582     def visitBitmask(self, bitmask, *args, **kwargs):
583         raise NotImplementedError
584
585     def visitPointer(self, pointer, *args, **kwargs):
586         raise NotImplementedError
587
588     def visitIntPointer(self, pointer, *args, **kwargs):
589         raise NotImplementedError
590
591     def visitObjPointer(self, pointer, *args, **kwargs):
592         raise NotImplementedError
593
594     def visitLinearPointer(self, pointer, *args, **kwargs):
595         raise NotImplementedError
596
597     def visitReference(self, reference, *args, **kwargs):
598         raise NotImplementedError
599
600     def visitHandle(self, handle, *args, **kwargs):
601         raise NotImplementedError
602
603     def visitAlias(self, alias, *args, **kwargs):
604         raise NotImplementedError
605
606     def visitOpaque(self, opaque, *args, **kwargs):
607         raise NotImplementedError
608
609     def visitInterface(self, interface, *args, **kwargs):
610         raise NotImplementedError
611
612     def visitPolymorphic(self, polymorphic, *args, **kwargs):
613         raise NotImplementedError
614         #return self.visit(polymorphic.defaultType, *args, **kwargs)
615
616
617 class OnceVisitor(Visitor):
618     '''Visitor that guarantees that each type is visited only once.'''
619
620     def __init__(self):
621         self.__visited = set()
622
623     def visit(self, type, *args, **kwargs):
624         if type not in self.__visited:
625             self.__visited.add(type)
626             return type.visit(self, *args, **kwargs)
627         return None
628
629
630 class Rebuilder(Visitor):
631     '''Visitor which rebuild types as it visits them.
632
633     By itself it is a no-op -- it is intended to be overwritten.
634     '''
635
636     def visitVoid(self, void):
637         return void
638
639     def visitLiteral(self, literal):
640         return literal
641
642     def visitString(self, string):
643         string_type = self.visit(string.type)
644         if string_type is string.type:
645             return string
646         else:
647             return String(string_type, string.length, string.wide)
648
649     def visitConst(self, const):
650         const_type = self.visit(const.type)
651         if const_type is const.type:
652             return const
653         else:
654             return Const(const_type)
655
656     def visitStruct(self, struct):
657         members = [(self.visit(type), name) for type, name in struct.members]
658         return Struct(struct.name, members)
659
660     def visitArray(self, array):
661         type = self.visit(array.type)
662         return Array(type, array.length)
663
664     def visitBlob(self, blob):
665         type = self.visit(blob.type)
666         return Blob(type, blob.size)
667
668     def visitEnum(self, enum):
669         return enum
670
671     def visitBitmask(self, bitmask):
672         type = self.visit(bitmask.type)
673         return Bitmask(type, bitmask.values)
674
675     def visitPointer(self, pointer):
676         pointer_type = self.visit(pointer.type)
677         if pointer_type is pointer.type:
678             return pointer
679         else:
680             return Pointer(pointer_type)
681
682     def visitIntPointer(self, pointer):
683         return pointer
684
685     def visitObjPointer(self, pointer):
686         pointer_type = self.visit(pointer.type)
687         if pointer_type is pointer.type:
688             return pointer
689         else:
690             return ObjPointer(pointer_type)
691
692     def visitLinearPointer(self, pointer):
693         pointer_type = self.visit(pointer.type)
694         if pointer_type is pointer.type:
695             return pointer
696         else:
697             return LinearPointer(pointer_type)
698
699     def visitReference(self, reference):
700         reference_type = self.visit(reference.type)
701         if reference_type is reference.type:
702             return reference
703         else:
704             return Reference(reference_type)
705
706     def visitHandle(self, handle):
707         handle_type = self.visit(handle.type)
708         if handle_type is handle.type:
709             return handle
710         else:
711             return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
712
713     def visitAlias(self, alias):
714         alias_type = self.visit(alias.type)
715         if alias_type is alias.type:
716             return alias
717         else:
718             return Alias(alias.expr, alias_type)
719
720     def visitOpaque(self, opaque):
721         return opaque
722
723     def visitInterface(self, interface, *args, **kwargs):
724         return interface
725
726     def visitPolymorphic(self, polymorphic):
727         switchExpr = polymorphic.switchExpr
728         switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
729         defaultType = self.visit(polymorphic.defaultType)
730         return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
731
732
733 class MutableRebuilder(Rebuilder):
734     '''Type visitor which derives a mutable type.'''
735
736     def visitString(self, string):
737         return string
738
739     def visitConst(self, const):
740         # Strip out const qualifier
741         return const.type
742
743     def visitAlias(self, alias):
744         # Tear the alias on type changes
745         type = self.visit(alias.type)
746         if type is alias.type:
747             return alias
748         return type
749
750     def visitReference(self, reference):
751         # Strip out references
752         return reference.type
753
754
755 class Traverser(Visitor):
756     '''Visitor which all types.'''
757
758     def visitVoid(self, void, *args, **kwargs):
759         pass
760
761     def visitLiteral(self, literal, *args, **kwargs):
762         pass
763
764     def visitString(self, string, *args, **kwargs):
765         pass
766
767     def visitConst(self, const, *args, **kwargs):
768         self.visit(const.type, *args, **kwargs)
769
770     def visitStruct(self, struct, *args, **kwargs):
771         for type, name in struct.members:
772             self.visit(type, *args, **kwargs)
773
774     def visitArray(self, array, *args, **kwargs):
775         self.visit(array.type, *args, **kwargs)
776
777     def visitBlob(self, array, *args, **kwargs):
778         pass
779
780     def visitEnum(self, enum, *args, **kwargs):
781         pass
782
783     def visitBitmask(self, bitmask, *args, **kwargs):
784         self.visit(bitmask.type, *args, **kwargs)
785
786     def visitPointer(self, pointer, *args, **kwargs):
787         self.visit(pointer.type, *args, **kwargs)
788
789     def visitIntPointer(self, pointer, *args, **kwargs):
790         pass
791
792     def visitObjPointer(self, pointer, *args, **kwargs):
793         self.visit(pointer.type, *args, **kwargs)
794
795     def visitLinearPointer(self, pointer, *args, **kwargs):
796         self.visit(pointer.type, *args, **kwargs)
797
798     def visitReference(self, reference, *args, **kwargs):
799         self.visit(reference.type, *args, **kwargs)
800
801     def visitHandle(self, handle, *args, **kwargs):
802         self.visit(handle.type, *args, **kwargs)
803
804     def visitAlias(self, alias, *args, **kwargs):
805         self.visit(alias.type, *args, **kwargs)
806
807     def visitOpaque(self, opaque, *args, **kwargs):
808         pass
809
810     def visitInterface(self, interface, *args, **kwargs):
811         if interface.base is not None:
812             self.visit(interface.base, *args, **kwargs)
813         for method in interface.iterMethods():
814             for arg in method.args:
815                 self.visit(arg.type, *args, **kwargs)
816             self.visit(method.type, *args, **kwargs)
817
818     def visitPolymorphic(self, polymorphic, *args, **kwargs):
819         self.visit(polymorphic.defaultType, *args, **kwargs)
820         for expr, type in polymorphic.switchTypes:
821             self.visit(type, *args, **kwargs)
822
823
824 class Collector(Traverser):
825     '''Visitor which collects all unique types as it traverses them.'''
826
827     def __init__(self):
828         self.__visited = set()
829         self.types = []
830
831     def visit(self, type):
832         if type in self.__visited:
833             return
834         self.__visited.add(type)
835         Visitor.visit(self, type)
836         self.types.append(type)
837
838
839
840 class Module:
841     '''A collection of functions.'''
842
843     def __init__(self, name = None):
844         self.name = name
845         self.headers = []
846         self.functions = []
847         self.interfaces = []
848
849     def addFunctions(self, functions):
850         self.functions.extend(functions)
851
852     def addInterfaces(self, interfaces):
853         self.interfaces.extend(interfaces)
854
855     def mergeModule(self, module):
856         self.headers.extend(module.headers)
857         self.functions.extend(module.functions)
858         self.interfaces.extend(module.interfaces)
859
860     def getFunctionByName(self, name):
861         for function in self.functions:
862             if function.name == name:
863                 return function
864         return None
865
866
867 class API:
868     '''API abstraction.
869
870     Essentially, a collection of types, functions, and interfaces.
871     '''
872
873     def __init__(self, modules = None):
874         self.modules = []
875         if modules is not None:
876             self.modules.extend(modules)
877
878     def getAllTypes(self):
879         collector = Collector()
880         for module in self.modules:
881             for function in module.functions:
882                 for arg in function.args:
883                     collector.visit(arg.type)
884                 collector.visit(function.type)
885             for interface in module.interfaces:
886                 collector.visit(interface)
887                 for method in interface.iterMethods():
888                     for arg in method.args:
889                         collector.visit(arg.type)
890                     collector.visit(method.type)
891         return collector.types
892
893     def getAllFunctions(self):
894         functions = []
895         for module in self.modules:
896             functions.extend(module.functions)
897         return functions
898
899     def getAllInterfaces(self):
900         types = self.getAllTypes()
901         interfaces = [type for type in types if isinstance(type, Interface)]
902         for module in self.modules:
903             for interface in module.interfaces:
904                 if interface not in interfaces:
905                     interfaces.append(interface)
906         return interfaces
907
908     def addModule(self, module):
909         self.modules.append(module)
910
911     def getFunctionByName(self, name):
912         for module in self.modules:
913             for function in module.functions:
914                 if function.name == name:
915                     return function
916         return None
917
918
919 # C string (i.e., zero terminated)
920 CString = String(Char)
921 WString = String(WChar, wide=True)
922 ConstCString = String(Const(Char))
923 ConstWString = String(Const(WChar), wide=True)