]> git.notmuchmail.org Git - apitrace/blobdiff - base.py
Pass-through TAB and CR characters.
[apitrace] / base.py
diff --git a/base.py b/base.py
index d2b4a22dd80d988fd2bec711f04592538239a9f5..cc66890f5e133a3bc846c0dc62b69239fe31bff4 100644 (file)
--- a/base.py
+++ b/base.py
@@ -1,21 +1,27 @@
-#############################################################################
+##########################################################################
 #
-# Copyright 2008 Jose Fonseca
+# Copyright 2008-2009 VMware, Inc.
+# All Rights Reserved.
 #
-# 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.
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
 #
-# 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.
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
 #
-# 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/>.
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
 #
-#############################################################################
+##########################################################################/
 
 """C basic types"""
 
@@ -27,11 +33,27 @@ all_types = {}
 
 class Type:
 
-    def __init__(self, name):
-        self.name = name
+    __seq = 0
+
+    def __init__(self, expr, id = ''):
+        self.expr = expr
+        
+        for char in id:
+            assert char.isalnum() or char in '_ '
+
+        id = id.replace(' ', '_')
+        
+        if id in all_types:
+            Type.__seq += 1
+            id += str(Type.__seq)
+        
+        assert id not in all_types
+        all_types[id] = self
+
+        self.id = id
 
     def __str__(self):
-        return self.name
+        return self.expr
 
     def isoutput(self):
         return False
@@ -62,21 +84,11 @@ Void = _Void()
 
 class Concrete(Type):
 
-    def __init__(self, name):
-        for char in name:
-            assert char.isalnum() or char == '_'
-
-        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))
+        print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
     
     def impl(self):
-        print 'void Dump%s(const %s &value) {' % (self.name, str(self))
+        print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
         self._dump("value");
         print '}'
         print
@@ -85,53 +97,50 @@ class Concrete(Type):
         raise NotImplementedError
     
     def dump(self, instance):
-        print '    Dump%s(%s);' % (self.name, instance)
+        print '    Dump%s(%s);' % (self.id, instance)
     
 
 class Intrinsic(Concrete):
 
-    def __init__(self, expr, format, name = None):
-        if name is None:
-            name = expr
-        Concrete.__init__(self, name)
-        self.expr = expr
+    def __init__(self, expr, format):
+        Concrete.__init__(self, 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):
 
     def __init__(self, type):
-        Type.__init__(self, 'C' + type.name)
+
+        if isinstance(type, Pointer):
+            expr = type.expr + " const"
+        else:
+            expr = "const " + type.expr
+
+        Type.__init__(self, expr, 'C' + type.id)
+
         self.type = type
 
     def dump(self, instance):
         self.type.dump(instance)
 
-    def __str__(self):
-        return "const " + str(self.type)
-
 
 class Pointer(Type):
 
     def __init__(self, type):
-        Type.__init__(self, 'P' + type.name)
+        Type.__init__(self, type.expr + " *", 'P' + type.id)
         self.type = 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:
-            print '        Log::TextF("%%p", %s);' % instance
+            pass
+        print '        Log::EndReference();'
         print '    }'
         print '    else'
         print '        Log::Text("NULL");'
@@ -171,27 +180,58 @@ class Enum(Concrete):
         print '    }'
 
 
+class FakeEnum(Enum):
+
+    def __init__(self, type, values):
+        Enum.__init__(self, type.expr, values)
+        self.type = type
+
+
 class Flags(Concrete):
 
-    __seq = 0
-    
     def __init__(self, type, values):
-        Flags.__seq += 1
-        Concrete.__init__(self, type.name + str(Flags.__seq))
+        Concrete.__init__(self, type.expr)
         self.type = type
         self.values = values
 
-    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 '        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 Array(Type):
+
+    def __init__(self, type, length):
+        Type.__init__(self, type.expr + " *", 'P' + type.id)
+        self.type = type
+        self.length = length
+
+    def dump(self, instance):
+        index = '__i' + self.type.id
+        print '    for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
+        print '        Log::BeginElement("%s");' % (self.type,)
+        self.type.dump('(%s)[%s]' % (instance, index))
+        print '        Log::EndElement();'
+        print '    }'
+
+    def wrap_instance(self, instance):
+        self.type.wrap_instance("*" + instance)
+
+    def unwrap_instance(self, instance):
+        self.type.wrap_instance("*" + instance)
 
 
 class Struct(Concrete):
@@ -201,15 +241,10 @@ class Struct(Concrete):
         self.members = members
 
     def _dump(self, instance):
-        print '    Log::Text("{");'
-        first = True
         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):
@@ -224,11 +259,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:
@@ -240,7 +276,7 @@ class Function:
             s = self.call + ' ' + s
         if name.startswith('*'):
             s = '(' + s + ')'
-        s = str(self.type) + ' ' + s
+        s = self.type.expr + ' ' + s
         s += "("
         if self.args:
             s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
@@ -249,11 +285,77 @@ 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):
 
     def __init__(self, name, base=None):
         Type.__init__(self, name)
+        self.name = name
         self.base = base
         self.methods = []
 
@@ -266,7 +368,7 @@ class Interface(Type):
         raise StopIteration
 
     def wrap_name(self):
-        return "Wrap" + self.name
+        return "Wrap" + self.expr
 
     def wrap_pre_decl(self):
         print "class %s;" % self.wrap_name()
@@ -302,21 +404,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
@@ -365,21 +468,35 @@ class WrapPointer(Pointer):
 class _String(Type):
 
     def __init__(self):
-        Type.__init__(self, "String")
-
-    def __str__(self):
-        return "char *"
+        Type.__init__(self, "char *")
 
     def dump(self, instance):
-        print '    Log::DumpString(%s);' % instance
+        print '    Log::DumpString((const char *)%s);' % instance
 
 String = _String()
 
+class _WString(Type):
+
+    def __init__(self):
+        Type.__init__(self, "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")
+SizeT = Intrinsic("size_t", "%lu")
 
 
 def wrap():