]> git.cworth.org Git - apitrace/blobdiff - base.py
Improve the XML semantic info. CSS magic.
[apitrace] / base.py
diff --git a/base.py b/base.py
index ea0034200c47dbce729ce7836301ae8dd437d5bf..64a02ed558914b479f57b4cc0954921242903da0 100644 (file)
--- a/base.py
+++ b/base.py
@@ -1,5 +1,30 @@
+#############################################################################
+#
+# Copyright 2008 Jose Fonseca
+#
+# 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
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+#############################################################################
+
 """C basic types"""
 
+
+import debug
+
+
+all_types = {}
+
 class Type:
 
     def __init__(self, name):
@@ -11,24 +36,73 @@ class Type:
     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 
+
+    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 == '_'
 
-    def __init__(self, name, format):
         Type.__init__(self, name)
+        
+        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.name, str(self))
+    
+    def impl(self):
+        print 'void Dump%s(const %s &value) {' % (self.name, str(self))
+        self._dump("value");
+        print '}'
+        print
+    
+    def _dump(self, instance):
+        raise NotImplementedError
+    
+    def dump(self, instance):
+        print '    Dump%s(%s);' % (self.name, 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):
 
@@ -36,6 +110,9 @@ class Const(Type):
         Type.__init__(self, 'C' + type.name)
         self.type = type
 
+    def dump(self, instance):
+        self.type.dump(instance)
+
     def __str__(self):
         return "const " + str(self.type)
 
@@ -48,47 +125,89 @@ class Pointer(Type):
 
     def __str__(self):
         return str(self.type) + " *"
+    
+    def dump(self, instance):
+        print '    if(%s) {' % instance
+        print '        Log::BeginReference("%s", %s);' % (self.type, instance)
+        try:
+            self.type.dump("*" + instance)
+        except NotImplementedError:
+            pass
+        print '        Log::EndReference();'
+        print '    }'
+        print '    else'
+        print '        Log::Text("NULL");'
 
     def wrap_instance(self, instance):
         self.type.wrap_instance("*" + instance)
 
+    def unwrap_instance(self, instance):
+        self.type.wrap_instance("*" + instance)
 
-class Output(Type):
 
-    def __init__(self, type):
-        Type.__init__(self, type.name)
-        self.type = type
+def ConstPointer(type):
+    return Pointer(Const(type))
 
-    def __str__(self):
-        return str(self.type)
+
+class OutPointer(Pointer):
 
     def isoutput(self):
         return True
 
-    def wrap_instance(self, instance):
-        self.type.wrap_instance(instance)
-
 
-class Enum(Type):
+class Enum(Concrete):
 
     def __init__(self, name, values):
-        Type.__init__(self, name)
+        Concrete.__init__(self, name)
         self.values = values
-
-
-class Flags(Type):
-
+    
+    def _dump(self, instance):
+        print '    switch(%s) {' % instance
+        for value in self.values:
+            print '    case %s:' % value
+            print '        Log::Text("%s");' % value
+            print '        break;'
+        print '    default:'
+        print '        Log::TextF("%%i", %s);' % instance
+        print '        break;'
+        print '    }'
+
+
+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 __str__(self):
+        return str(self.type)
+    
+    def _dump(self, instance):
+        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 '    }'
+        self.type.dump("l_Value");
 
-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):
+        for type, name in self.members:
+            print '    Log::BeginElement("%s", "%s");' % (type, name)
+            type.dump('(%s).%s' % (instance, name))
+            print '    Log::EndElement();'
+
 
 class Alias(Type):
 
@@ -96,6 +215,9 @@ class Alias(Type):
         Type.__init__(self, name)
         self.type = type
 
+    def dump(self, instance):
+        self.type.dump(instance)
+
 
 class Function:
 
@@ -156,8 +278,7 @@ class Interface(Type):
         for method in self.itermethods():
             print "    " + method.prototype() + ";"
         print
-        print "private:"
-        print "    Log *m_pLog;"
+        #print "private:"
         print "    %s * m_pInstance;" % (self.name,)
         print "};"
         print
@@ -177,14 +298,29 @@ class Interface(Type):
             else:
                 print '    %s result;' % method.type
                 result = 'result = '
-            print '    g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
+            print '    Log::BeginCall("%s");' % (self.name + '::' + method.name)
+            print '    Log::BeginArg("%s *", "this");' % self.name
+            print '    Log::TextF("%p", m_pInstance);'
+            print '    Log::EndArg();'
+            for type, name in method.args:
+                if not type.isoutput():
+                    type.unwrap_instance(name)
+                    print '    Log::BeginArg("%s", "%s");' % (type, name)
+                    type.dump(name)
+                    print '    Log::EndArg();'
             print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
-            print '    g_pLog->EndCall();'
             for type, name in method.args:
                 if type.isoutput():
+                    print '    Log::BeginArg("%s", "%s");' % (type, name)
+                    type.dump(name)
+                    print '    Log::EndArg();'
                     type.wrap_instance(name)
             if method.type is not Void:
+                print '    Log::BeginReturn("%s");' % method.type
+                method.type.dump("result")
+                print '    Log::EndReturn();'
                 method.type.wrap_instance('result')
+            print '    Log::EndCall();'
             if method.name == 'QueryInterface':
                 print '    if(*ppvObj == m_pInstance)'
                 print '        *ppvObj = this;'
@@ -218,13 +354,38 @@ class WrapPointer(Pointer):
         print "    if(%s)" % instance
         print "        %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
 
-String = Intrinsic("char *", "%s")
+    def unwrap_instance(self, instance):
+        print "    if(%s)" % instance
+        print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
+
+
+class _String(Type):
+
+    def __init__(self):
+        Type.__init__(self, "String")
+
+    def __str__(self):
+        return "char *"
+
+    def dump(self, instance):
+        print '    Log::DumpString(%s);' % instance
+
+String = _String()
+
+
+Short = Intrinsic("short", "%i")
 Int = Intrinsic("int", "%i")
 Long = Intrinsic("long", "%li")
 Float = Intrinsic("float", "%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