]> git.cworth.org Git - apitrace/blobdiff - trace.py
Generate more compact switch statements for polymorphic types.
[apitrace] / trace.py
index 7e40e4676fb8c77ea6999d535c86c2c403d54cc5..9cc2b0a77a3e746c424d05aae3f84c13af0a0fe1 100644 (file)
--- a/trace.py
+++ b/trace.py
@@ -26,7 +26,7 @@
 """Common trace code generation."""
 
 
-import stdapi
+import specs.stdapi as stdapi
 from dispatch import Dispatcher
 
 
@@ -135,6 +135,9 @@ class DumpDeclarator(stdapi.OnceVisitor):
         print "};"
         print
 
+    def visit_polymorphic(self, polymorphic):
+        pass
+
 
 class DumpImplementer(stdapi.Visitor):
     '''Dump an instance.'''
@@ -202,6 +205,15 @@ class DumpImplementer(stdapi.Visitor):
     def visit_interface(self, interface, instance):
         print '    Trace::localWriter.writeOpaque((const void *)&%s);' % instance
 
+    def visit_polymorphic(self, polymorphic, instance):
+        print '    switch (%s) {' % polymorphic.switch_expr
+        for cases, type in polymorphic.iterswitch():
+            for case in cases:
+                print '    %s:' % case
+            self.visit(type, 'static_cast<%s>(%s)' % (type, instance));
+            print '        break;'
+        print '    }'
+
 
 dump_instance = DumpImplementer().visit
 
@@ -259,6 +271,10 @@ class Wrapper(stdapi.Visitor):
         print "    if (%s) {" % instance
         print "        %s = new %s(%s);" % (instance, interface_wrap_name(interface), instance)
         print "    }"
+    
+    def visit_polymorphic(self, type, instance):
+        # XXX: There might be polymorphic values that need wrapping in the future
+        pass
 
 
 class Unwrapper(Wrapper):
@@ -324,9 +340,6 @@ class Tracer:
         print 'static const Trace::FunctionSig __%s_sig = {%u, "%s", %u, __%s_args};' % (function.name, int(function.id), function.name, len(function.args), function.name)
         print
 
-    def get_dispatch_function(self, function):
-        return '__' + function.name
-
     def is_public_function(self, function):
         return True
 
@@ -362,12 +375,12 @@ class Tracer:
             self.dump_ret(function, "__result")
         print '    Trace::localWriter.endLeave();'
 
-    def dispatch_function(self, function):
+    def dispatch_function(self, function, prefix='__', suffix=''):
         if function.type is stdapi.Void:
             result = ''
         else:
             result = '__result = '
-        dispatch = self.get_dispatch_function(function)
+        dispatch = prefix + function.name + suffix
         print '    %s%s(%s);' % (result, dispatch, ', '.join([str(arg.name) for arg in function.args]))
 
     def dump_arg(self, function, arg):