]> git.notmuchmail.org Git - apitrace/blobdiff - base.py
Allow failure handling for functions without return value.
[apitrace] / base.py
diff --git a/base.py b/base.py
index 8f91a6dcdcecf55a0d701c2317b392b3c0cfe3b8..24641bc16aefb4648ed90d3832bbe9190c9f89a3 100644 (file)
--- a/base.py
+++ b/base.py
@@ -1,6 +1,6 @@
 #############################################################################
 #
-# Copyright 2008 Jose Fonseca
+# Copyright 2008 Tungsten Graphics, Inc.
 #
 # This program is free software: you can redistribute it and/or modify it
 # under the terms of the GNU Lesser General Public License as published
 
 """C basic types"""
 
+
+import debug
+
+
+all_types = {}
+
 class Type:
 
     def __init__(self, name):
@@ -27,35 +33,78 @@ class Type:
     def __str__(self):
         return self.name
 
+    def identifier(self):
+        return self.name.replace(' ', '_')
+
     def isoutput(self):
         return False
 
+    def decl(self):
+        pass
+
+    def impl(self):
+        pass
+
     def dump(self, instance):
         raise NotImplementedError
     
     def wrap_instance(self, instance):
-        pass
+        pass 
 
     def unwrap_instance(self, instance):
         pass
 
 
-class Void(Type):
+class _Void(Type):
 
     def __init__(self):
         Type.__init__(self, "void")
 
-Void = Void()
+Void = _Void()
 
 
-class Intrinsic(Type):
+class Concrete(Type):
+
+    def __init__(self, name):
+        for char in name:
+            assert char.isalnum() or char in '_ '
 
-    def __init__(self, name, format):
         Type.__init__(self, name)
-        self.format = format
+        
+        assert self.name not in all_types
+        if self.name not in all_types:
+            all_types[self.name] = self
 
+    def decl(self):
+        print 'void Dump%s(const %s &value);' % (self.identifier(), str(self))
+    
+    def impl(self):
+        print 'void Dump%s(const %s &value) {' % (self.identifier(), str(self))
+        self._dump("value");
+        print '}'
+        print
+    
+    def _dump(self, instance):
+        raise NotImplementedError
+    
     def dump(self, instance):
+        print '    Dump%s(%s);' % (self.identifier(), instance)
+    
+
+class Intrinsic(Concrete):
+
+    def __init__(self, expr, format, name = None):
+        if name is None:
+            name = expr
+        Concrete.__init__(self, name)
+        self.expr = expr
+        self.format = format
+
+    def _dump(self, instance):
         print '    Log::TextF("%s", %s);' % (self.format, instance)
+        
+    def __str__(self):
+        return self.expr
 
 
 class Const(Type):
@@ -82,10 +131,12 @@ class Pointer(Type):
     
     def dump(self, instance):
         print '    if(%s) {' % instance
+        print '        Log::BeginReference("%s", %s);' % (self.type, instance)
         try:
             self.type.dump("*" + instance)
         except NotImplementedError:
-            print '        Log::TextF("%%p", %s);' % instance
+            pass
+        print '        Log::EndReference();'
         print '    }'
         print '    else'
         print '        Log::Text("NULL");'
@@ -107,13 +158,13 @@ class OutPointer(Pointer):
         return True
 
 
-class Enum(Type):
+class Enum(Concrete):
 
     def __init__(self, name, values):
-        Type.__init__(self, name)
+        Concrete.__init__(self, name)
         self.values = values
     
-    def dump(self, instance):
+    def _dump(self, instance):
         print '    switch(%s) {' % instance
         for value in self.values:
             print '    case %s:' % value
@@ -125,41 +176,61 @@ class Enum(Type):
         print '    }'
 
 
-class Flags(Type):
+class FakeEnum(Enum):
+
+    __seq = 0
+    
+    def __init__(self, type, values):
+        FakeEnum.__seq += 1
+        Enum.__init__(self, type.name + str(FakeEnum.__seq), values)
+        self.type = type
+    
+    def __str__(self):
+        return str(self.type)
+
+
+class Flags(Concrete):
 
+    __seq = 0
+    
     def __init__(self, type, values):
-        Type.__init__(self, type.name)
+        Flags.__seq += 1
+        Concrete.__init__(self, type.name + str(Flags.__seq))
         self.type = type
         self.values = values
 
-    def dump(self, instance):
-        print '    {'
-        print '        %s l_Value = %s;' % (self.type, instance)
+    def __str__(self):
+        return str(self.type)
+    
+    def _dump(self, instance):
+        print '    bool l_First = TRUE;'
+        print '    %s l_Value = %s;' % (self.type, instance)
         for value in self.values:
-            print '        if((l_Value & %s) == %s) {' % (value, value)
-            print '            Log::Text("%s | ");' % value
-            print '            l_Value &= ~%s;' % value
-            print '        }'
+            print '    if((l_Value & %s) == %s) {' % (value, value)
+            print '        if(!l_First)'
+            print '            Log::Text(" | ");'
+            print '        Log::Text("%s");' % value
+            print '        l_Value &= ~%s;' % value
+            print '        l_First = FALSE;'
+            print '    }'
+        print '    if(l_Value || l_First) {'
+        print '        if(!l_First)'
+        print '            Log::Text(" | ");'
         self.type.dump("l_Value");
         print '    }'
 
 
-class Struct(Type):
+class Struct(Concrete):
 
     def __init__(self, name, members):
-        Type.__init__(self, name)
+        Concrete.__init__(self, name)
         self.members = members
 
-    def dump(self, instance):
-        print '    Log::Text("{");'
-        first = True
+    def _dump(self, instance):
         for type, name in self.members:
-            if first:
-                first = False
-            else:
-                print '    Log::Text(", ");'
+            print '    Log::BeginElement("%s", "%s");' % (type, name)
             type.dump('(%s).%s' % (instance, name))
-        print '    Log::Text("}");'
+            print '    Log::EndElement();'
 
 
 class Alias(Type):
@@ -174,11 +245,12 @@ class Alias(Type):
 
 class Function:
 
-    def __init__(self, type, name, args, call = '__stdcall'):
+    def __init__(self, type, name, args, call = '__stdcall', fail = None):
         self.type = type
         self.name = name
         self.args = args
         self.call = call
+        self.fail = fail
 
     def prototype(self, name=None):
         if name is not None:
@@ -199,6 +271,71 @@ class Function:
         s += ")"
         return s
 
+    def pointer_type(self):
+        return 'P' + self.name
+
+    def pointer_value(self):
+        return 'p' + self.name
+
+    def wrap_decl(self):
+        ptype = self.pointer_type()
+        pvalue = self.pointer_value()
+        print 'typedef ' + self.prototype('* %s' % ptype) + ';'
+        print 'static %s %s = NULL;' % (ptype, pvalue)
+        print
+
+    def get_true_pointer(self):
+        raise NotImplementedError
+
+    def fail_impl(self):
+        if self.fail is not None:
+            if self.type is Void:
+                assert self.fail == ''
+                print '            return;' 
+            else:
+                assert self.fail != ''
+                print '            return %s;' % self.fail
+        else:
+            print '            ExitProcess(0);'
+
+    def wrap_impl(self):
+        pvalue = self.pointer_value()
+        print self.prototype() + ' {'
+        if self.type is Void:
+            result = ''
+        else:
+            print '    %s result;' % self.type
+            result = 'result = '
+        self.get_true_pointer()
+        print '    Log::BeginCall("%s");' % (self.name)
+        for type, name in self.args:
+            if not type.isoutput():
+                type.unwrap_instance(name)
+                print '    Log::BeginArg("%s", "%s");' % (type, name)
+                type.dump(name)
+                print '    Log::EndArg();'
+        print '    %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
+        for type, name in self.args:
+            if type.isoutput():
+                print '    Log::BeginArg("%s", "%s");' % (type, name)
+                type.dump(name)
+                print '    Log::EndArg();'
+                type.wrap_instance(name)
+        if self.type is not Void:
+            print '    Log::BeginReturn("%s");' % self.type
+            self.type.dump("result")
+            print '    Log::EndReturn();'
+            self.type.wrap_instance('result')
+        print '    Log::EndCall();'
+        self.post_call_impl()
+        if self.type is not Void:
+            print '    return result;'
+        print '}'
+        print
+
+    def post_call_impl(self):
+        pass
+
 
 class Interface(Type):
 
@@ -252,21 +389,22 @@ class Interface(Type):
                 print '    %s result;' % method.type
                 result = 'result = '
             print '    Log::BeginCall("%s");' % (self.name + '::' + method.name)
-            print '    Log::BeginParam("this", "%s *");' % self.name
-            print '    Log::TextF("%p", m_pInstance);'
-            print '    Log::EndParam();'
+            print '    Log::BeginArg("%s *", "this");' % self.name
+            print '    Log::BeginReference("%s", m_pInstance);' % self.name
+            print '    Log::EndReference();'
+            print '    Log::EndArg();'
             for type, name in method.args:
                 if not type.isoutput():
                     type.unwrap_instance(name)
-                    print '    Log::BeginParam("%s", "%s");' % (name, type)
+                    print '    Log::BeginArg("%s", "%s");' % (type, name)
                     type.dump(name)
-                    print '    Log::EndParam();'
+                    print '    Log::EndArg();'
             print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
             for type, name in method.args:
                 if type.isoutput():
-                    print '    Log::BeginParam("%s", "%s");' % (name, type)
+                    print '    Log::BeginArg("%s", "%s");' % (type, name)
                     type.dump(name)
-                    print '    Log::EndParam();'
+                    print '    Log::EndArg();'
                     type.wrap_instance(name)
             if method.type is not Void:
                 print '    Log::BeginReturn("%s");' % method.type
@@ -311,14 +449,53 @@ class WrapPointer(Pointer):
         print "    if(%s)" % instance
         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
 
-String = Intrinsic("char *", "%s")
+
+class _String(Type):
+
+    def __init__(self):
+        Type.__init__(self, "String")
+
+    def __str__(self):
+        return "const char *"
+
+    def dump(self, instance):
+        print '    Log::DumpString((const char *)%s);' % instance
+
+String = _String()
+
+class _WString(Type):
+
+    def __init__(self):
+        Type.__init__(self, "WString")
+
+    def __str__(self):
+        return "const wchar_t *"
+
+    def dump(self, instance):
+        print '    Log::DumpWString(%s);' % instance
+
+WString = _WString()
+
+
+SChar = Intrinsic("signed char", "%i")
+UChar = Intrinsic("unsigned char", "%u")
 Short = Intrinsic("short", "%i")
 Int = Intrinsic("int", "%i")
 Long = Intrinsic("long", "%li")
+UShort = Intrinsic("unsigned short", "%u")
+UInt = Intrinsic("unsigned int", "%u")
+ULong = Intrinsic("unsigned long", "%lu")
 Float = Intrinsic("float", "%f")
+Double = Intrinsic("double", "%f")
 
 
 def wrap():
+    for type in all_types.itervalues():
+        type.decl()
+    print
+    for type in all_types.itervalues():
+        type.impl()
+    print
     for type in towrap:
         type.wrap_pre_decl()
     print