]> git.cworth.org Git - apitrace/blobdiff - specs/stdapi.py
d3dretrace: Cope with _MAP_FLAG_DO_NOT_WAIT flags.
[apitrace] / specs / stdapi.py
index 1097347f38ce3a97a26b9312c7de183c469e8c0f..c652736a8383e9156e5bd7e88af34dde79760382 100644 (file)
@@ -292,33 +292,18 @@ class Struct(Type):
         Struct.__id += 1
 
         self.name = name
-        self.members = []
-
-        # Eliminate anonymous unions
-        for type, name in members:
-            if name is not None:
-                self.members.append((type, name))
-            else:
-                assert isinstance(type, Union)
-                assert type.name is None
-                self.members.extend(type.members)
+        self.members = members
 
     def visit(self, visitor, *args, **kwargs):
         return visitor.visitStruct(self, *args, **kwargs)
 
 
-class Union(Type):
-
-    __id = 0
-
-    def __init__(self, name, members):
-        Type.__init__(self, name)
-
-        self.id = Union.__id
-        Union.__id += 1
-
-        self.name = name
-        self.members = members
+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):
@@ -355,13 +340,7 @@ def InOut(type, name):
 
 class Function:
 
-    # 0-3 are reserved to memcpy, malloc, free, and realloc
-    __id = 4
-
     def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
-        self.id = Function.__id
-        Function.__id += 1
-
         self.type = type
         self.name = name
 
@@ -406,6 +385,12 @@ class Function:
     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 StdFunction(*args, **kwargs):
     kwargs.setdefault('call', '__stdcall')
@@ -515,8 +500,12 @@ def OpaqueBlob(type, size):
 
 class Polymorphic(Type):
 
-    def __init__(self, switchExpr, switchTypes, defaultType, contextLess=True):
-        Type.__init__(self, defaultType.expr)
+    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
@@ -526,8 +515,12 @@ class Polymorphic(Type):
         return visitor.visitPolymorphic(self, *args, **kwargs)
 
     def iterSwitch(self):
-        cases = [['default']]
-        types = [self.defaultType]
+        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
@@ -726,7 +719,10 @@ class Rebuilder(Visitor):
     def visitPolymorphic(self, polymorphic):
         switchExpr = polymorphic.switchExpr
         switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
-        defaultType = self.visit(polymorphic.defaultType)
+        if polymorphic.defaultType is None:
+            defaultType = None
+        else:
+            defaultType = self.visit(polymorphic.defaultType)
         return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
 
 
@@ -816,9 +812,10 @@ class Traverser(Visitor):
             self.visit(method.type, *args, **kwargs)
 
     def visitPolymorphic(self, polymorphic, *args, **kwargs):
-        self.visit(polymorphic.defaultType, *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):
@@ -836,6 +833,49 @@ class Collector(Traverser):
         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.'''