summaryrefslogtreecommitdiff
path: root/specs/stdapi.py
blob: f36c4e2e58276521be54ec0c9b104bd3d6ed241b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
##########################################################################
#
# Copyright 2008-2010 VMware, Inc.
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##########################################################################/

"""C basic types"""


import sys

import debug


class Type:
    """Base class for all types."""

    __tags = set()

    def __init__(self, expr, tag = None):
        self.expr = expr

        # Generate a default tag, used when naming functions that will operate
        # on this type, so it should preferrably be something representative of
        # the type.
        if tag is None:
            if expr is not None:
                tag = ''.join([c for c in expr if c.isalnum() or c in '_'])
            else:
                tag = 'anonynoums'
        else:
            for c in tag:
                assert c.isalnum() or c in '_'

        # Ensure it is unique.
        if tag in Type.__tags:
            suffix = 1
            while tag + str(suffix) in Type.__tags:
                suffix += 1
            tag += str(suffix)

        assert tag not in Type.__tags
        Type.__tags.add(tag)

        self.tag = tag

    def __str__(self):
        """Return the C/C++ type expression for this type."""
        return self.expr

    def visit(self, visitor, *args, **kwargs):
        raise NotImplementedError

    def mutable(self):
        '''Return a mutable version of this type.

        Convenience wrapper around MutableRebuilder.'''
        visitor = MutableRebuilder()
        return visitor.visit(self)

    def depends(self, other):
        '''Whether this type depends on another.'''

        collector = Collector()
        collector.visit(self)
        return other in collector.types


class _Void(Type):
    """Singleton void type."""

    def __init__(self):
        Type.__init__(self, "void")

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitVoid(self, *args, **kwargs)

Void = _Void()


class Literal(Type):
    """Class to describe literal types.

    Types which are not defined in terms of other types, such as integers and
    floats."""

    def __init__(self, expr, kind):
        Type.__init__(self, expr)
        self.kind = kind

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitLiteral(self, *args, **kwargs)


Bool = Literal("bool", "Bool")
SChar = Literal("signed char", "SInt")
UChar = Literal("unsigned char", "UInt")
Short = Literal("short", "SInt")
Int = Literal("int", "SInt")
Long = Literal("long", "SInt")
LongLong = Literal("long long", "SInt")
UShort = Literal("unsigned short", "UInt")
UInt = Literal("unsigned int", "UInt")
ULong = Literal("unsigned long", "UInt")
ULongLong = Literal("unsigned long long", "UInt")
Float = Literal("float", "Float")
Double = Literal("double", "Double")
SizeT = Literal("size_t", "UInt")

Char = Literal("char", "SInt")
WChar = Literal("wchar_t", "SInt")

Int8 = Literal("int8_t", "SInt")
UInt8 = Literal("uint8_t", "UInt")
Int16 = Literal("int16_t", "SInt")
UInt16 = Literal("uint16_t", "UInt")
Int32 = Literal("int32_t", "SInt")
UInt32 = Literal("uint32_t", "UInt")
Int64 = Literal("int64_t", "SInt")
UInt64 = Literal("uint64_t", "UInt")

IntPtr = Literal("intptr_t", "SInt")
UIntPtr = Literal("uintptr_t", "UInt")

class Const(Type):

    def __init__(self, type):
        # While "const foo" and "foo const" are synonymous, "const foo *" and
        # "foo * const" are not quite the same, and some compilers do enforce
        # strict const correctness.
        if type.expr.startswith("const ") or '*' in type.expr:
            expr = type.expr + " const"
        else:
            # The most legible
            expr = "const " + type.expr

        Type.__init__(self, expr, 'C' + type.tag)

        self.type = type

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitConst(self, *args, **kwargs)


class Pointer(Type):

    def __init__(self, type):
        Type.__init__(self, type.expr + " *", 'P' + type.tag)
        self.type = type

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitPointer(self, *args, **kwargs)


class IntPointer(Type):
    '''Integer encoded as a pointer.'''

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitIntPointer(self, *args, **kwargs)


class ObjPointer(Type):
    '''Pointer to an object.'''

    def __init__(self, type):
        Type.__init__(self, type.expr + " *", 'P' + type.tag)
        self.type = type

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitObjPointer(self, *args, **kwargs)


class LinearPointer(Type):
    '''Pointer to a linear range of memory.'''

    def __init__(self, type, size = None):
        Type.__init__(self, type.expr + " *", 'P' + type.tag)
        self.type = type
        self.size = size

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitLinearPointer(self, *args, **kwargs)


class Reference(Type):
    '''C++ references.'''

    def __init__(self, type):
        Type.__init__(self, type.expr + " &", 'R' + type.tag)
        self.type = type

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitReference(self, *args, **kwargs)


class Handle(Type):

    def __init__(self, name, type, range=None, key=None):
        Type.__init__(self, type.expr, 'P' + type.tag)
        self.name = name
        self.type = type
        self.range = range
        self.key = key

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitHandle(self, *args, **kwargs)


def ConstPointer(type):
    return Pointer(Const(type))


class Enum(Type):

    __id = 0

    def __init__(self, name, values):
        Type.__init__(self, name)

        self.id = Enum.__id
        Enum.__id += 1

        self.values = list(values)

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitEnum(self, *args, **kwargs)


def FakeEnum(type, values):
    return Enum(type.expr, values)


class Bitmask(Type):

    __id = 0

    def __init__(self, type, values):
        Type.__init__(self, type.expr)

        self.id = Bitmask.__id
        Bitmask.__id += 1

        self.type = type
        self.values = values

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitBitmask(self, *args, **kwargs)

Flags = Bitmask


def EnumFlags(name, values):
    return Flags(Alias(name, UInt), values)


class Array(Type):

    def __init__(self, type_, length):
        Type.__init__(self, type_.expr + " *")
        self.type = type_
        self.length = length
        if not isinstance(length, int):
            assert isinstance(length, basestring)
            # Check if length is actually a valid constant expression
            try:
                eval(length, {}, {})
            except:
                pass
            else:
                raise ValueError("length %r should be an integer" % length)

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitArray(self, *args, **kwargs)


class AttribArray(Type):

    def __init__(self, baseType, valueTypes, terminator = '0'):
        self.baseType = baseType
        Type.__init__(self, (Pointer(self.baseType)).expr)
        self.valueTypes = valueTypes
        self.terminator = terminator
        self.hasKeysWithoutValues = False
        for key, value in valueTypes:
            if value is None:
                self.hasKeysWithoutValues = True

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitAttribArray(self, *args, **kwargs)


class Blob(Type):

    def __init__(self, type, size):
        Type.__init__(self, type.expr + ' *')
        self.type = type
        self.size = size

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitBlob(self, *args, **kwargs)


class Struct(Type):

    __id = 0

    def __init__(self, name, members):
        Type.__init__(self, name)

        self.id = Struct.__id
        Struct.__id += 1

        self.name = name
        self.members = members

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitStruct(self, *args, **kwargs)


def Union(kindExpr, kindTypes, contextLess=True):
    switchTypes = []
    for kindCase, kindType, kindMemberName in kindTypes:
        switchType = Struct(None, [(kindType, kindMemberName)])
        switchTypes.append((kindCase, switchType))
    return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)


class Alias(Type):

    def __init__(self, expr, type):
        Type.__init__(self, expr)
        self.type = type

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitAlias(self, *args, **kwargs)

class Arg:

    def __init__(self, type, name, input=True, output=False):
        self.type = type
        self.name = name
        self.input = input
        self.output = output
        self.index = None

    def __str__(self):
        return '%s %s' % (self.type, self.name)


def In(type, name):
    return Arg(type, name, input=True, output=False)

def Out(type, name):
    return Arg(type, name, input=False, output=True)

def InOut(type, name):
    return Arg(type, name, input=True, output=True)


class Function:

    def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False, overloaded=False):
        self.type = type
        self.name = name

        self.args = []
        index = 0
        for arg in args:
            if not isinstance(arg, Arg):
                if isinstance(arg, tuple):
                    arg_type, arg_name = arg
                else:
                    arg_type = arg
                    arg_name = "arg%u" % index
                arg = Arg(arg_type, arg_name)
            arg.index = index
            index += 1
            self.args.append(arg)

        self.call = call
        self.fail = fail
        self.sideeffects = sideeffects
        self.internal = internal
        self.overloaded = overloaded

    def prototype(self, name=None):
        if name is not None:
            name = name.strip()
        else:
            name = self.name
        s = name
        if self.call:
            s = self.call + ' ' + s
        if name.startswith('*'):
            s = '(' + s + ')'
        s = self.type.expr + ' ' + s
        s += "("
        if self.args:
            s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
        else:
            s += "void"
        s += ")"
        return s

    def sigName(self):
        name = self.name
        if self.overloaded:
            # suffix used to make overloaded functions/methods unique
            suffix = ','.join([str(arg.type) for arg in self.args])
            suffix = suffix.replace(' *', '*')
            suffix = suffix.replace(' &', '&')
            suffix = '(' + suffix + ')'
            name += suffix
        return name

    def argNames(self):
        return [arg.name for arg in self.args]

    def getArgByName(self, name):
        for arg in self.args:
            if arg.name == name:
                return arg
        return None

    def getArgByType(self, type):
        for arg in self.args:
            if arg.type is type:
                return arg
        return None


def StdFunction(*args, **kwargs):
    kwargs.setdefault('call', '__stdcall')
    return Function(*args, **kwargs)


def FunctionPointer(type, name, args, **kwargs):
    # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
    return Opaque(name)


class Interface(Type):

    def __init__(self, name, base=None):
        Type.__init__(self, name)
        self.name = name
        self.base = base
        self.methods = []

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitInterface(self, *args, **kwargs)

    def getMethodByName(self, name):
        for method in self.iterMethods():
            if method.name == name:
                return method
        return None

    def iterMethods(self):
        if self.base is not None:
            for method in self.base.iterMethods():
                yield method
        for method in self.methods:
            yield method
        raise StopIteration

    def iterBases(self):
        iface = self
        while iface is not None:
            yield iface
            iface = iface.base
        raise StopIteration

    def hasBase(self, *bases):
        for iface in self.iterBases():
            if iface in bases:
                return True
        return False

    def iterBaseMethods(self):
        if self.base is not None:
            for iface, method in self.base.iterBaseMethods():
                yield iface, method
        for method in self.methods:
            yield self, method
        raise StopIteration


class Method(Function):

    def __init__(self, type, name, args, call = '', const=False, sideeffects=True, overloaded=False):
        assert call == '__stdcall'
        Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects, overloaded=overloaded)
        for index in range(len(self.args)):
            self.args[index].index = index + 1
        self.const = const

    def prototype(self, name=None):
        s = Function.prototype(self, name)
        if self.const:
            s += ' const'
        return s


def StdMethod(*args, **kwargs):
    kwargs.setdefault('call', '__stdcall')
    return Method(*args, **kwargs)


class String(Type):
    '''Human-legible character string.'''

    def __init__(self, type = Char, length = None, wide = False):
        assert isinstance(type, Type)
        Type.__init__(self, type.expr + ' *')
        self.type = type
        self.length = length
        self.wide = wide

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitString(self, *args, **kwargs)


class Opaque(Type):
    '''Opaque pointer.'''

    def __init__(self, expr):
        Type.__init__(self, expr)

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitOpaque(self, *args, **kwargs)


def OpaquePointer(type, *args):
    return Opaque(type.expr + ' *')

def OpaqueArray(type, size):
    return Opaque(type.expr + ' *')

def OpaqueBlob(type, size):
    return Opaque(type.expr + ' *')


class Polymorphic(Type):

    def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
        if defaultType is None:
            Type.__init__(self, None)
            contextLess = False
        else:
            Type.__init__(self, defaultType.expr)
        self.switchExpr = switchExpr
        self.switchTypes = switchTypes
        self.defaultType = defaultType
        self.contextLess = contextLess

    def visit(self, visitor, *args, **kwargs):
        return visitor.visitPolymorphic(self, *args, **kwargs)

    def iterSwitch(self):
        cases = []
        types = []

        if self.defaultType is not None:
            cases.append(['default'])
            types.append(self.defaultType)

        for expr, type in self.switchTypes:
            case = 'case %s' % expr
            try:
                i = types.index(type)
            except ValueError:
                cases.append([case])
                types.append(type)
            else:
                cases[i].append(case)

        return zip(cases, types)


def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
    enumValues = [expr for expr, type in switchTypes]
    enum = Enum(enumName, enumValues)
    polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
    return enum, polymorphic


class Visitor:
    '''Abstract visitor for the type hierarchy.'''

    def visit(self, type, *args, **kwargs):
        return type.visit(self, *args, **kwargs)

    def visitVoid(self, void, *args, **kwargs):
        raise NotImplementedError

    def visitLiteral(self, literal, *args, **kwargs):
        raise NotImplementedError

    def visitString(self, string, *args, **kwargs):
        raise NotImplementedError

    def visitConst(self, const, *args, **kwargs):
        raise NotImplementedError

    def visitStruct(self, struct, *args, **kwargs):
        raise NotImplementedError

    def visitArray(self, array, *args, **kwargs):
        raise NotImplementedError

    def visitAttribArray(self, array, *args, **kwargs):
        raise NotImplementedError

    def visitBlob(self, blob, *args, **kwargs):
        raise NotImplementedError

    def visitEnum(self, enum, *args, **kwargs):
        raise NotImplementedError

    def visitBitmask(self, bitmask, *args, **kwargs):
        raise NotImplementedError

    def visitPointer(self, pointer, *args, **kwargs):
        raise NotImplementedError

    def visitIntPointer(self, pointer, *args, **kwargs):
        raise NotImplementedError

    def visitObjPointer(self, pointer, *args, **kwargs):
        raise NotImplementedError

    def visitLinearPointer(self, pointer, *args, **kwargs):
        raise NotImplementedError

    def visitReference(self, reference, *args, **kwargs):
        raise NotImplementedError

    def visitHandle(self, handle, *args, **kwargs):
        raise NotImplementedError

    def visitAlias(self, alias, *args, **kwargs):
        raise NotImplementedError

    def visitOpaque(self, opaque, *args, **kwargs):
        raise NotImplementedError

    def visitInterface(self, interface, *args, **kwargs):
        raise NotImplementedError

    def visitPolymorphic(self, polymorphic, *args, **kwargs):
        raise NotImplementedError
        #return self.visit(polymorphic.defaultType, *args, **kwargs)


class OnceVisitor(Visitor):
    '''Visitor that guarantees that each type is visited only once.'''

    def __init__(self):
        self.__visited = set()

    def visit(self, type, *args, **kwargs):
        if type not in self.__visited:
            self.__visited.add(type)
            return type.visit(self, *args, **kwargs)
        return None


class Rebuilder(Visitor):
    '''Visitor which rebuild types as it visits them.

    By itself it is a no-op -- it is intended to be overwritten.
    '''

    def visitVoid(self, void):
        return void

    def visitLiteral(self, literal):
        return literal

    def visitString(self, string):
        string_type = self.visit(string.type)
        if string_type is string.type:
            return string
        else:
            return String(string_type, string.length, string.wide)

    def visitConst(self, const):
        const_type = self.visit(const.type)
        if const_type is const.type:
            return const
        else:
            return Const(const_type)

    def visitStruct(self, struct):
        members = [(self.visit(type), name) for type, name in struct.members]
        return Struct(struct.name, members)

    def visitArray(self, array):
        type = self.visit(array.type)
        return Array(type, array.length)

    def visitBlob(self, blob):
        type = self.visit(blob.type)
        return Blob(type, blob.size)

    def visitEnum(self, enum):
        return enum

    def visitBitmask(self, bitmask):
        type = self.visit(bitmask.type)
        return Bitmask(type, bitmask.values)

    def visitPointer(self, pointer):
        pointer_type = self.visit(pointer.type)
        if pointer_type is pointer.type:
            return pointer
        else:
            return Pointer(pointer_type)

    def visitIntPointer(self, pointer):
        return pointer

    def visitObjPointer(self, pointer):
        pointer_type = self.visit(pointer.type)
        if pointer_type is pointer.type:
            return pointer
        else:
            return ObjPointer(pointer_type)

    def visitLinearPointer(self, pointer):
        pointer_type = self.visit(pointer.type)
        if pointer_type is pointer.type:
            return pointer
        else:
            return LinearPointer(pointer_type)

    def visitReference(self, reference):
        reference_type = self.visit(reference.type)
        if reference_type is reference.type:
            return reference
        else:
            return Reference(reference_type)

    def visitHandle(self, handle):
        handle_type = self.visit(handle.type)
        if handle_type is handle.type:
            return handle
        else:
            return Handle(handle.name, handle_type, range=handle.range, key=handle.key)

    def visitAlias(self, alias):
        alias_type = self.visit(alias.type)
        if alias_type is alias.type:
            return alias
        else:
            return Alias(alias.expr, alias_type)

    def visitOpaque(self, opaque):
        return opaque

    def visitInterface(self, interface, *args, **kwargs):
        return interface

    def visitPolymorphic(self, polymorphic):
        switchExpr = polymorphic.switchExpr
        switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
        if polymorphic.defaultType is None:
            defaultType = None
        else:
            defaultType = self.visit(polymorphic.defaultType)
        return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)


class MutableRebuilder(Rebuilder):
    '''Type visitor which derives a mutable type.'''

    def visitString(self, string):
        return string

    def visitConst(self, const):
        # Strip out const qualifier
        return const.type

    def visitAlias(self, alias):
        # Tear the alias on type changes
        type = self.visit(alias.type)
        if type is alias.type:
            return alias
        return type

    def visitReference(self, reference):
        # Strip out references
        return self.visit(reference.type)


class Traverser(Visitor):
    '''Visitor which all types.'''

    def visitVoid(self, void, *args, **kwargs):
        pass

    def visitLiteral(self, literal, *args, **kwargs):
        pass

    def visitString(self, string, *args, **kwargs):
        pass

    def visitConst(self, const, *args, **kwargs):
        self.visit(const.type, *args, **kwargs)

    def visitStruct(self, struct, *args, **kwargs):
        for type, name in struct.members:
            self.visit(type, *args, **kwargs)

    def visitArray(self, array, *args, **kwargs):
        self.visit(array.type, *args, **kwargs)

    def visitAttribArray(self, attribs, *args, **kwargs):
        for key, valueType in attribs.valueTypes:
            if valueType is not None:
                self.visit(valueType, *args, **kwargs)

    def visitBlob(self, array, *args, **kwargs):
        pass

    def visitEnum(self, enum, *args, **kwargs):
        pass

    def visitBitmask(self, bitmask, *args, **kwargs):
        self.visit(bitmask.type, *args, **kwargs)

    def visitPointer(self, pointer, *args, **kwargs):
        self.visit(pointer.type, *args, **kwargs)

    def visitIntPointer(self, pointer, *args, **kwargs):
        pass

    def visitObjPointer(self, pointer, *args, **kwargs):
        self.visit(pointer.type, *args, **kwargs)

    def visitLinearPointer(self, pointer, *args, **kwargs):
        self.visit(pointer.type, *args, **kwargs)

    def visitReference(self, reference, *args, **kwargs):
        self.visit(reference.type, *args, **kwargs)

    def visitHandle(self, handle, *args, **kwargs):
        self.visit(handle.type, *args, **kwargs)

    def visitAlias(self, alias, *args, **kwargs):
        self.visit(alias.type, *args, **kwargs)

    def visitOpaque(self, opaque, *args, **kwargs):
        pass

    def visitInterface(self, interface, *args, **kwargs):
        if interface.base is not None:
            self.visit(interface.base, *args, **kwargs)
        for method in interface.iterMethods():
            for arg in method.args:
                self.visit(arg.type, *args, **kwargs)
            self.visit(method.type, *args, **kwargs)

    def visitPolymorphic(self, polymorphic, *args, **kwargs):
        for expr, type in polymorphic.switchTypes:
            self.visit(type, *args, **kwargs)
        if polymorphic.defaultType is not None:
            self.visit(polymorphic.defaultType, *args, **kwargs)


class Collector(Traverser):
    '''Visitor which collects all unique types as it traverses them.'''

    def __init__(self):
        self.__visited = set()
        self.types = []

    def visit(self, type):
        if type in self.__visited:
            return
        self.__visited.add(type)
        Visitor.visit(self, type)
        self.types.append(type)


class ExpanderMixin:
    '''Mixin class that provides a bunch of methods to expand C expressions
    from the specifications.'''

    __structs = None
    __indices = None

    def expand(self, expr):
        # Expand a C expression, replacing certain variables
        if not isinstance(expr, basestring):
            return expr
        variables = {}

        if self.__structs is not None:
            variables['self'] = '(%s)' % self.__structs[0]
        if self.__indices is not None:
            variables['i'] = self.__indices[0]

        expandedExpr = expr.format(**variables)
        if expandedExpr != expr and 0:
            sys.stderr.write("  %r -> %r\n" % (expr, expandedExpr))
        return expandedExpr

    def visitMember(self, member, structInstance, *args, **kwargs):
        memberType, memberName = member
        if memberName is None:
            # Anonymous structure/union member
            memberInstance = structInstance
        else:
            memberInstance = '(%s).%s' % (structInstance, memberName)
        self.__structs = (structInstance, self.__structs)
        try:
            return self.visit(memberType, memberInstance, *args, **kwargs)
        finally:
            _, self.__structs = self.__structs

    def visitElement(self, elementIndex, elementType, *args, **kwargs):
        self.__indices = (elementIndex, self.__indices)
        try:
            return self.visit(elementType, *args, **kwargs)
        finally:
            _, self.__indices = self.__indices


class Module:
    '''A collection of functions.'''

    def __init__(self, name = None):
        self.name = name
        self.headers = []
        self.functions = []
        self.interfaces = []

    def addFunctions(self, functions):
        self.functions.extend(functions)

    def addInterfaces(self, interfaces):
        self.interfaces.extend(interfaces)

    def mergeModule(self, module):
        self.headers.extend(module.headers)
        self.functions.extend(module.functions)
        self.interfaces.extend(module.interfaces)

    def getFunctionByName(self, name):
        for function in self.functions:
            if function.name == name:
                return function
        return None


class API:
    '''API abstraction.

    Essentially, a collection of types, functions, and interfaces.
    '''

    def __init__(self, modules = None):
        self.modules = []
        if modules is not None:
            self.modules.extend(modules)

    def getAllTypes(self):
        collector = Collector()
        for module in self.modules:
            for function in module.functions:
                for arg in function.args:
                    collector.visit(arg.type)
                collector.visit(function.type)
            for interface in module.interfaces:
                collector.visit(interface)
                for method in interface.iterMethods():
                    for arg in method.args:
                        collector.visit(arg.type)
                    collector.visit(method.type)
        return collector.types

    def getAllFunctions(self):
        functions = []
        for module in self.modules:
            functions.extend(module.functions)
        return functions

    def getAllInterfaces(self):
        types = self.getAllTypes()
        interfaces = [type for type in types if isinstance(type, Interface)]
        for module in self.modules:
            for interface in module.interfaces:
                if interface not in interfaces:
                    interfaces.append(interface)
        return interfaces

    def addModule(self, module):
        self.modules.append(module)

    def getFunctionByName(self, name):
        for module in self.modules:
            for function in module.functions:
                if function.name == name:
                    return function
        return None


# C string (i.e., zero terminated)
CString = String(Char)
WString = String(WChar, wide=True)
ConstCString = String(Const(Char))
ConstWString = String(Const(WChar), wide=True)