]> git.cworth.org Git - apitrace/blobdiff - specs/stdapi.py
Bring some of the virtual-memory-regions
[apitrace] / specs / stdapi.py
index 53d874ee9cafc7225e716c08462a08368ce6754f..a92b9ba5669035725e9e54c5ca2d136119106caa 100644 (file)
@@ -220,7 +220,8 @@ class Arg:
 
 class Function:
 
-    __id = 0
+    # 0-3 are reserved to memcpy, malloc, free, and realloc
+    __id = 4
 
     def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
         self.id = Function.__id
@@ -338,6 +339,34 @@ def OpaqueBlob(type, size):
     return Opaque(type.expr + ' *')
 
 
+class Polymorphic(Type):
+
+    def __init__(self, default_type, switch_expr, switch_types):
+        Type.__init__(self, default_type.expr)
+        self.default_type = default_type
+        self.switch_expr = switch_expr
+        self.switch_types = switch_types
+
+    def visit(self, visitor, *args, **kwargs):
+        return visitor.visit_polymorphic(self, *args, **kwargs)
+
+    def iterswitch(self):
+        cases = [['default']]
+        types = [self.default_type]
+
+        for expr, type in self.switch_types:
+            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)
+
+
 class Visitor:
 
     def visit(self, type, *args, **kwargs):
@@ -385,6 +414,10 @@ class Visitor:
     def visit_interface(self, interface, *args, **kwargs):
         raise NotImplementedError
 
+    def visit_polymorphic(self, polymorphic, *args, **kwargs):
+        raise NotImplementedError
+        #return self.visit(polymorphic.default_type, *args, **kwargs)
+
 
 class OnceVisitor(Visitor):
 
@@ -446,6 +479,12 @@ class Rebuilder(Visitor):
     def visit_opaque(self, opaque):
         return opaque
 
+    def visit_polymorphic(self, polymorphic):
+        default_type = self.visit(polymorphic.default_type)
+        switch_expr = polymorphic.switch_expr
+        switch_types = [(expr, self.visit(type)) for expr, type in polymorphic.switch_types]
+        return Polymorphic(default_type, switch_expr, switch_types)
+
 
 class Collector(Visitor):
     '''Collect.'''
@@ -509,6 +548,11 @@ class Collector(Visitor):
                 self.visit(arg.type)
             self.visit(method.type)
 
+    def visit_polymorphic(self, polymorphic):
+        self.visit(polymorphic.default_type)
+        for expr, type in polymorphic.switch_types:
+            self.visit(type)
+
 
 class API: