]> git.cworth.org Git - apitrace/blobdiff - base.py
Refer args by index.
[apitrace] / base.py
diff --git a/base.py b/base.py
index 77409bb4ce3214838afecc61ac1cefc8f52d1c7b..8213403aafccc37043aedac98f4e9410cd0df9eb 100644 (file)
--- a/base.py
+++ b/base.py
@@ -67,6 +67,9 @@ class Visitor:
     def visit_pointer(self, pointer, *args, **kwargs):
         raise NotImplementedError
 
+    def visit_handle(self, handle, *args, **kwargs):
+        raise NotImplementedError
+
     def visit_alias(self, alias, *args, **kwargs):
         raise NotImplementedError
 
@@ -126,6 +129,10 @@ class Rebuilder(Visitor):
         type = self.visit(pointer.type)
         return Pointer(type)
 
+    def visit_handle(self, handle):
+        type = self.visit(handle.type)
+        return Handle(handle.name, type)
+
     def visit_alias(self, alias):
         type = self.visit(alias.type)
         return Alias(alias.expr, type)
@@ -229,6 +236,17 @@ class Pointer(Type):
         return visitor.visit_pointer(self, *args, **kwargs)
 
 
+class Handle(Type):
+
+    def __init__(self, name, type):
+        Type.__init__(self, type.expr, 'P' + type.id)
+        self.name = name
+        self.type = type
+
+    def visit(self, visitor, *args, **kwargs):
+        return visitor.visit_handle(self, *args, **kwargs)
+
+
 def ConstPointer(type):
     return Pointer(Const(type))
 
@@ -314,6 +332,7 @@ class Arg:
         self.type = type
         self.name = name
         self.output = output
+        self.index = None
 
     def __str__(self):
         return '%s %s' % (self.type, self.name)
@@ -326,10 +345,13 @@ class Function:
         self.name = name
 
         self.args = []
+        index = 0
         for arg in args:
             if isinstance(arg, tuple):
                 arg_type, arg_name = arg
                 arg = Arg(arg_type, arg_name)
+            arg.index = index
+            index += 1
             self.args.append(arg)
 
         self.call = call
@@ -397,15 +419,16 @@ def WrapPointer(type):
     return Pointer(type)
 
 
-class _String(Type):
+class String(Type):
 
-    def __init__(self):
-        Type.__init__(self, "char *")
+    def __init__(self, expr = "char *", length = None):
+        Type.__init__(self, expr)
+        self.length = length
 
     def visit(self, visitor, *args, **kwargs):
         return visitor.visit_string(self, *args, **kwargs)
 
-String = _String()
+CString = String()
 
 
 class Opaque(Type):
@@ -422,25 +445,88 @@ def OpaquePointer(type):
     return Opaque(type.expr + ' *')
 
 
+class Collector(Visitor):
+    '''Collect.'''
+
+    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)
+
+    def visit_void(self, literal):
+        pass
+
+    def visit_literal(self, literal):
+        pass
+
+    def visit_string(self, string):
+        pass
+
+    def visit_const(self, const):
+        self.visit(const.type)
+
+    def visit_struct(self, struct):
+        for type, name in struct.members:
+            self.visit(type)
+
+    def visit_array(self, array):
+        self.visit(array.type)
+
+    def visit_blob(self, array):
+        pass
+
+    def visit_enum(self, enum):
+        pass
+
+    def visit_bitmask(self, bitmask):
+        self.visit(bitmask.type)
+
+    def visit_pointer(self, pointer):
+        self.visit(pointer.type)
+
+    def visit_handle(self, handle):
+        self.visit(handle.type)
+
+    def visit_alias(self, alias):
+        self.visit(alias.type)
+
+    def visit_opaque(self, opaque):
+        pass
+
+    def visit_interface(self, interface):
+        pass
+
 
 class API:
 
     def __init__(self, name):
         self.name = name
         self.headers = []
-        self.types = set()
         self.functions = []
         self.interfaces = []
 
-    def add_type(self, type):
-        if type not in self.types:
-            self.types.add(type)
+    def all_types(self):
+        collector = Collector()
+        for function in self.functions:
+            for arg in function.args:
+                collector.visit(arg.type)
+            collector.visit(function.type)
+        for interface in self.interfaces:
+            collector.visit(interface)
+            for method in interface.methods:
+                for arg in method.args:
+                    collector.visit(arg.type)
+                collector.visit(method.type)
+        return collector.types
 
     def add_function(self, function):
         self.functions.append(function)
-        for arg in function.args:
-            self.add_type(arg.type)
-        self.add_type(function.type)
 
     def add_functions(self, functions):
         for function in functions: