[TYPES PARSING] correctly catch unsigned long int, and struct types.
[gnome.gobject-introspection] / giscanner / transformer.py
index cc9e0b5..a3fcff9 100644 (file)
@@ -19,6 +19,7 @@
 #
 
 import os
+import sys
 
 from .ast import (Bitfield, Callback, Enum, Function, Namespace, Member,
                   Parameter, Return, Struct, Field,
@@ -46,6 +47,8 @@ _xdg_data_dirs = [x for x in os.environ.get('XDG_DATA_DIRS', '').split(':') \
 class SkipError(Exception):
     pass
 
+class VaListSkipError(SkipError):
+    pass
 
 class Names(object):
     names = property(lambda self: self._names)
@@ -80,6 +83,9 @@ class Transformer(object):
     def get_includes(self):
         return self._includes
 
+    def set_strip_suffix(self, strip_suffix):
+        self._strip_suffix = strip_suffix
+
     def set_strip_prefix(self, strip_prefix):
         self._strip_prefix = strip_prefix
 
@@ -125,8 +131,9 @@ class Transformer(object):
             path = os.path.join(d, girname)
             if os.path.exists(path):
                 return path
-        raise ValueError("Couldn't find include %r (search path: %r)"\
+        sys.stderr.write("Couldn't find include %r (search path: %r)\n"\
                          % (girname, searchdirs))
+        sys.exit(1)
 
     def _parse_include(self, filename):
         parser = self._cachestore.load(filename)
@@ -177,14 +184,23 @@ class Transformer(object):
         # when --strip-prefix=g:
         #   GHashTable -> HashTable
         #   g_hash_table_new -> hash_table_new
+        stripped = False
         prefix = self._strip_prefix.lower()
-        if isfunction:
+        
+        if isfunction and '_' in name:
             prefix += '_'
         if len(name) > len(prefix) and name.lower().startswith(prefix):
             name = name[len(prefix):]
+            stripped = True
 
         while name.startswith('_'):
             name = name[1:]
+
+        if (stripped and self._strip_suffix and 
+            len(name) > len(self._strip_suffix) and
+            name.endswith(self._strip_suffix)
+            name = name[:-1*len(self._strip_suffix)]
+            
         return name
 
     def _traverse_one(self, symbol, stype=None):
@@ -214,15 +230,13 @@ class Transformer(object):
 
     def _enum_common_prefix(self, symbol):
         def common_prefix(a, b):
-            alen = len(a)
-            blen = len(b)
-            l = min(alen, blen)
-            for i in xrange(l):
-                if a[i] != b[i]:
-                    return a[:i]
-            if alen > blen:
-                return b
-            return a
+            commonparts = []
+            for aword, bword in zip(a.split('_'), b.split('_')):
+                if aword != bword:
+                    return '_'.join(commonparts) + '_'
+                commonparts.append(aword)
+            return min(a, b)
+
         # Nothing less than 2 has a common prefix
         if len(list(symbol.base_type.child_list)) < 2:
             return None
@@ -269,8 +283,10 @@ class Transformer(object):
                       symbol.ident)
 
     def _type_is_callback(self, type):
-        if (isinstance(type, Callback) or
-            isinstance(self._typedefs_ns.get(type.name), Callback)):
+        if isinstance(type, Callback):
+            return True
+        node = self._names.names.get(type.name)
+        if node and isinstance(node[1], Callback):
             return True
         return False
 
@@ -283,9 +299,8 @@ class Transformer(object):
         return False
 
     def _handle_destroy(self, param, destroy_idx, destroy_param):
-        if ((self._namespace.name == 'GLib' and
-             destroy_param.type.name == 'DestroyNotify') or
-            destroy_param.type.name == 'GLib.DestroyNotify'):
+        if (destroy_param.type.name == 'GLib.DestroyNotify' or
+            destroy_param.type.ctype == 'GDestroyNotify'):
             param.destroy_name = destroy_param.name
             param.destroy_index = destroy_idx
             return True
@@ -296,6 +311,10 @@ class Transformer(object):
             if not self._type_is_callback(param.type):
                 continue
 
+            # set a default scope
+            if param.scope is None:
+                param.scope = 'call'
+
             # j is the index where we look for closure/destroy to
             # group with the callback param
             j = i + 1
@@ -331,12 +350,16 @@ class Transformer(object):
             value = 'void'
         elif source_type.type == CTYPE_BASIC_TYPE:
             value = source_type.name
+            if source_type.base_type:
+                value += ' ' + self._create_source_type(source_type.base_type)
         elif source_type.type == CTYPE_TYPEDEF:
             value = source_type.name
         elif source_type.type == CTYPE_ARRAY:
             return self._create_source_type(source_type.base_type)
         elif source_type.type == CTYPE_POINTER:
             value = self._create_source_type(source_type.base_type) + '*'
+        elif source_type.type == CTYPE_STRUCT:
+            value = source_type.name
         else:
             value = 'any'
         return value
@@ -352,7 +375,17 @@ class Transformer(object):
         source_type = symbol.base_type
         if (source_type.type == CTYPE_POINTER and
             symbol.base_type.base_type.type == CTYPE_FUNCTION):
-            node = self._create_callback(symbol)
+            try:
+                node = self._create_callback(symbol)
+            except VaListSkipError:
+                #this handles va_list members, and converts them
+                #to unwritable, unreadable void*
+                ftype = Type("any", "void*")
+                ftype = self.resolve_param_type(ftype)
+                node = Field(symbol.ident, ftype, ftype.name,
+                         readable=False, writable=False, bits=symbol.const_int)
+
+
         elif source_type.type == CTYPE_STRUCT and source_type.name is None:
             node = self._create_struct(symbol, anonymous=True)
         elif source_type.type == CTYPE_UNION and source_type.name is None:
@@ -388,6 +421,8 @@ class Transformer(object):
         if (ctype == CTYPE_POINTER and
             symbol.base_type.base_type.type == CTYPE_FUNCTION):
             node = self._create_typedef_callback(symbol)
+        if (ctype ==  CTYPE_FUNCTION):
+            node = self._create_typedef_callback(symbol)
         elif (ctype == CTYPE_POINTER and
             symbol.base_type.base_type.type == CTYPE_STRUCT):
             node = self._create_typedef_struct(symbol, disguised=True)
@@ -404,6 +439,13 @@ class Transformer(object):
             name = self.remove_prefix(symbol.ident)
             if symbol.base_type.name:
                 target = self.remove_prefix(symbol.base_type.name)
+            elif (ctype == CTYPE_POINTER and
+                symbol.base_type.base_type.name):
+                target = self.remove_prefix(
+                    symbol.base_type.base_type.name) + '*'
+            elif (ctype == CTYPE_POINTER and
+                symbol.base_type.base_type.type == CTYPE_VOID):
+                target = 'any'
             else:
                 target = 'none'
             if name in type_names:
@@ -459,7 +501,7 @@ class Transformer(object):
     def _create_type(self, source_type, is_param, is_retval):
         ctype = self._create_source_type(source_type)
         if ctype.startswith('va_list'):
-            raise SkipError()
+            raise VaListSkipError()
         # FIXME: FILE* should not be skipped, it should be handled
         #        properly instead
         elif ctype == 'FILE*':
@@ -501,7 +543,8 @@ class Transformer(object):
     def _create_const(self, symbol):
         # Don't create constants for non-public things
         # http://bugzilla.gnome.org/show_bug.cgi?id=572790
-        if not symbol.source_filename.endswith('.h'):
+        if (symbol.source_filename is None or
+            not symbol.source_filename.endswith('.h')):
             return None
         name = self.remove_prefix(symbol.ident)
         if symbol.const_string is not None:
@@ -575,6 +618,10 @@ class Transformer(object):
     def _create_callback(self, symbol):
         parameters = list(self._create_parameters(symbol.base_type.base_type))
         retval = self._create_return(symbol.base_type.base_type.base_type)
+        ret_type = symbol.base_type.base_type.base_type
+        if not ret_type:
+            ret_type = symbol.base_type.base_type
+        retval = self._create_return(ret_type)
 
         # Mark the 'user_data' arguments
         for i, param in enumerate(parameters):
@@ -666,7 +713,7 @@ class Transformer(object):
                                                      self.ctype_of(ptype),
                                                      names, **kwargs)
         elif isinstance(ptype, basestring):
-            return self.resolve_type_name_full(ptype, None, names, **kwargs)
+            return self.resolve_type_name_full(ptype, ptype, names, **kwargs)
         else:
             raise AssertionError("Unhandled param: %r" % (ptype, ))
         return ptype
@@ -686,3 +733,8 @@ class Transformer(object):
             else:
                 break
         return type_name
+
+    def iter_enums(self):
+        for node in self._namespace.nodes:
+            if isinstance(node, Enum):
+                yield node