Imported Upstream version 1.61.2
[platform/upstream/gobject-introspection.git] / giscanner / transformer.py
index 81b4d0a..bcabded 100644 (file)
 # Boston, MA 02111-1307, USA.
 #
 
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-from __future__ import unicode_literals
-
 import os
 import sys
 import subprocess
@@ -36,7 +31,7 @@ from .sourcescanner import (
     SourceSymbol, ctype_name, CTYPE_POINTER,
     CTYPE_BASIC_TYPE, CTYPE_UNION, CTYPE_ARRAY, CTYPE_TYPEDEF,
     CTYPE_VOID, CTYPE_ENUM, CTYPE_FUNCTION, CTYPE_STRUCT,
-    CSYMBOL_TYPE_FUNCTION, CSYMBOL_TYPE_TYPEDEF, CSYMBOL_TYPE_STRUCT,
+    CSYMBOL_TYPE_FUNCTION, CSYMBOL_TYPE_FUNCTION_MACRO, CSYMBOL_TYPE_TYPEDEF, CSYMBOL_TYPE_STRUCT,
     CSYMBOL_TYPE_ENUM, CSYMBOL_TYPE_UNION, CSYMBOL_TYPE_OBJECT,
     CSYMBOL_TYPE_MEMBER, CSYMBOL_TYPE_ELLIPSIS, CSYMBOL_TYPE_CONST,
     TYPE_QUALIFIER_CONST, TYPE_QUALIFIER_VOLATILE)
@@ -50,7 +45,7 @@ class Transformer(object):
     namespace = property(lambda self: self._namespace)
 
     def __init__(self, namespace, accept_unprefixed=False,
-                 identifier_filter_cmd='', symbol_filter_cmd=''):
+                 identifier_filter_cmd=None, symbol_filter_cmd=None):
         self._cachestore = CacheStore()
         self._accept_unprefixed = accept_unprefixed
         self._namespace = namespace
@@ -82,7 +77,11 @@ class Transformer(object):
         # handle #ifdef.  But this introduces an arch-dependency in the .gir
         # file.  So far this has only come up scanning glib - in theory, other
         # modules will just depend on that.
-        if isinstance(original, ast.Constant) and isinstance(node, ast.Constant):
+        if original and\
+                (isinstance(original, ast.FunctionMacro) or isinstance(node,
+                    ast.FunctionMacro)):
+            pass
+        elif isinstance(original, ast.Constant) and isinstance(node, ast.Constant):
             pass
         elif original is node:
             # Ignore attempts to add the same node to the namespace. This can
@@ -191,9 +190,9 @@ None."""
 
     def _find_include(self, include):
         searchdirs = self._includepaths[:]
+        searchdirs.extend(GIRDIR)
         for path in self._get_gi_data_dirs():
             searchdirs.append(os.path.join(path, 'gir-1.0'))
-        searchdirs.append(os.path.join(DATADIR, 'gir-1.0'))
 
         girname = '%s-%s.gir' % (include.name, include.version)
         for d in searchdirs:
@@ -259,14 +258,14 @@ currently-scanned namespace is first."""
             proc = subprocess.Popen(self._symbol_filter_cmd,
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
-                                    stderr=subprocess.PIPE,
-                                    shell=True)
-            _name = name
+                                    stderr=subprocess.PIPE)
             proc_name, err = proc.communicate(name.encode())
+            proc_name = proc_name.strip()
             if proc.returncode:
-                raise ValueError('filter: "%s" exited: %d with error: %s' %
+                raise ValueError('filter: %r exited: %d with error: %s' %
                                  (self._symbol_filter_cmd, proc.returncode, err))
             name = proc_name.decode('ascii')
+            name = name.strip()
 
         matches = []  # Namespaces which might contain this name
         unprefixed_namespaces = []  # Namespaces with no prefix, last resort
@@ -325,13 +324,12 @@ raise ValueError."""
             proc = subprocess.Popen(self._identifier_filter_cmd,
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
-                                    stderr=subprocess.PIPE,
-                                    shell=True)
+                                    stderr=subprocess.PIPE)
             proc_ident, err = proc.communicate(ident.encode())
             if proc.returncode:
-                raise ValueError('filter: "%s" exited: %d with error: %s' %
+                raise ValueError('filter: %r exited: %d with error: %s' %
                                  (self._identifier_filter_cmd, proc.returncode, err))
-            ident = proc_ident.decode('ascii')
+            ident = proc_ident.decode('ascii').strip()
 
         hidden = ident.startswith('_')
         if hidden:
@@ -373,6 +371,8 @@ raise ValueError."""
             stype = symbol.type
         if stype == CSYMBOL_TYPE_FUNCTION:
             return self._create_function(symbol)
+        elif stype == CSYMBOL_TYPE_FUNCTION_MACRO:
+            return self._create_function_macro(symbol)
         elif stype == CSYMBOL_TYPE_TYPEDEF:
             return self._create_typedef(symbol)
         elif stype == CSYMBOL_TYPE_STRUCT:
@@ -455,7 +455,20 @@ raise ValueError."""
         func.add_symbol_reference(symbol)
         return func
 
-    def _create_source_type(self, source_type):
+    def _create_function_macro(self, symbol):
+        if symbol.ident.startswith('_'):
+            return None
+
+        if (symbol.source_filename is None or not symbol.source_filename.endswith('.h')):
+            return None
+
+        parameters = list(self._create_parameters(symbol, symbol.base_type))
+        name = self._strip_symbol(symbol)
+        macro = ast.FunctionMacro(name, parameters, symbol.ident)
+        macro.add_symbol_reference(symbol)
+        return macro
+
+    def _create_source_type(self, source_type, is_parameter=False):
         assert source_type is not None
         if source_type.type == CTYPE_VOID:
             value = 'void'
@@ -463,15 +476,19 @@ raise ValueError."""
             value = source_type.name
         elif source_type.type == CTYPE_TYPEDEF:
             value = source_type.name
+        elif (source_type.type == CTYPE_POINTER or
+                # Array to pointer adjustment as per 6.7.6.3.
+                # This is performed only on the outermost array,
+                # so we don't forward is_parameter.
+                (source_type.type == CTYPE_ARRAY and is_parameter)):
+            value = self._create_source_type(source_type.base_type) + '*'
         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) + '*'
         else:
             value = 'gpointer'
         return value
 
-    def _create_complete_source_type(self, source_type):
+    def _create_complete_source_type(self, source_type, is_parameter=False):
         assert source_type is not None
 
         const = (source_type.type_qualifier & TYPE_QUALIFIER_CONST)
@@ -485,22 +502,25 @@ raise ValueError."""
                                   CTYPE_UNION,
                                   CTYPE_ENUM]:
             value = source_type.name
-            if not value:
-                value = 'gpointer'
             if const:
                 value = 'const ' + value
             if volatile:
                 value = 'volatile ' + value
-        elif source_type.type == CTYPE_ARRAY:
-            return self._create_complete_source_type(source_type.base_type)
-        elif source_type.type == CTYPE_POINTER:
+            return value
+        elif (source_type.type == CTYPE_POINTER or
+                # Array to pointer adjustment as per 6.7.6.3.
+                # This is performed only on the outermost array,
+                # so we don't forward is_parameter.
+                (source_type.type == CTYPE_ARRAY and is_parameter)):
             value = self._create_complete_source_type(source_type.base_type) + '*'
             # TODO: handle pointer to function as a special case?
             if const:
                 value += ' const'
             if volatile:
                 value += ' volatile'
-
+            return value
+        elif source_type.type == CTYPE_ARRAY:
+            return self._create_complete_source_type(source_type.base_type)
         else:
             if const:
                 value = 'gconstpointer'
@@ -510,8 +530,6 @@ raise ValueError."""
                 value = 'volatile ' + value
             return value
 
-        return value
-
     def _create_parameters(self, symbol, base_type):
         for i, child in enumerate(base_type.child_list):
             yield self._create_parameter(symbol, i, child)
@@ -551,36 +569,32 @@ raise ValueError."""
             # Special handling for fields; we don't have annotations on them
             # to apply later, yet.
             if source_type.type == CTYPE_ARRAY:
-                complete_ctype = self._create_complete_source_type(source_type)
+                # Determine flattened array size and its element type.
+                flattened_size = 1
+                while source_type.type == CTYPE_ARRAY:
+                    for child in source_type.child_list:
+                        if flattened_size is not None:
+                            flattened_size *= child.const_int
+                        break
+                    else:
+                        flattened_size = None
+                    source_type = source_type.base_type
+
                 # If the array contains anonymous unions, like in the GValue
                 # struct, we need to handle this specially.  This is necessary
                 # to be able to properly calculate the size of the compound
                 # type (e.g. GValue) that contains this array, see
                 # <https://bugzilla.gnome.org/show_bug.cgi?id=657040>.
-                if (source_type.base_type.type == CTYPE_UNION
-                and source_type.base_type.name is None):
-                    synthesized_type = self._synthesize_union_type(symbol, parent_symbol)
-                    ftype = ast.Array(None, synthesized_type, complete_ctype=complete_ctype)
+                if source_type.type == CTYPE_UNION and source_type.name is None:
+                    element_type = self._synthesize_union_type(symbol, parent_symbol)
                 else:
                     ctype = self._create_source_type(source_type)
-                    canonical_ctype = self._canonicalize_ctype(ctype)
-                    if canonical_ctype[-1] == '*':
-                        derefed_name = canonical_ctype[:-1]
-                    else:
-                        derefed_name = canonical_ctype
-                    if complete_ctype[-1] == '*':
-                        derefed_complete_ctype = complete_ctype[:-1]
-                    else:
-                        derefed_complete_ctype = complete_ctype
-                    from_ctype = self.create_type_from_ctype_string(ctype,
-                                                                    complete_ctype=complete_ctype)
-                    ftype = ast.Array(None, from_ctype,
-                                      ctype=derefed_name,
-                                      complete_ctype=derefed_complete_ctype)
-                child_list = list(symbol.base_type.child_list)
+                    complete_ctype = self._create_complete_source_type(source_type)
+                    element_type = self.create_type_from_ctype_string(ctype,
+                                                                      complete_ctype=complete_ctype)
+                ftype = ast.Array(None, element_type)
                 ftype.zeroterminated = False
-                if child_list:
-                    ftype.size = child_list[0].const_int
+                ftype.size = flattened_size
             else:
                 ftype = self._create_type_from_base(symbol.base_type)
             # ast.Fields are assumed to be read-write
@@ -619,7 +633,8 @@ raise ValueError."""
             # https://bugzilla.gnome.org/show_bug.cgi?id=755882
             if name.endswith('_autoptr'):
                 return None
-            return ast.Alias(name, target, ctype=symbol.ident)
+            node = ast.Alias(name, target, ctype=symbol.ident)
+            node.add_symbol_reference(symbol)
         else:
             raise NotImplementedError(
                 "symbol '%s' of type %s" % (symbol.ident, ctype_name(ctype)))
@@ -651,8 +666,8 @@ raise ValueError."""
         return canonical
 
     def _create_type_from_base(self, source_type, is_parameter=False, is_return=False):
-        ctype = self._create_source_type(source_type)
-        complete_ctype = self._create_complete_source_type(source_type)
+        ctype = self._create_source_type(source_type, is_parameter=is_parameter)
+        complete_ctype = self._create_complete_source_type(source_type, is_parameter=is_parameter)
         const = ((source_type.type == CTYPE_POINTER) and
                  (source_type.base_type.type_qualifier & TYPE_QUALIFIER_CONST))
         return self.create_type_from_ctype_string(ctype, is_const=const,
@@ -668,9 +683,12 @@ raise ValueError."""
                 name = base
             return ast.List(name, ast.TYPE_ANY, ctype=ctype,
                         is_const=is_const, complete_ctype=complete_ctype)
-        elif base in ('GArray', 'GPtrArray', 'GByteArray',
-                      'GLib.Array', 'GLib.PtrArray', 'GLib.ByteArray',
-                      'GObject.Array', 'GObject.PtrArray', 'GObject.ByteArray'):
+        elif base in ('GByteArray', 'GLib.ByteArray', 'GObject.ByteArray'):
+            return ast.Array('GLib.ByteArray', ast.TYPE_UINT8, ctype=ctype,
+                         is_const=is_const, complete_ctype=complete_ctype)
+        elif base in ('GArray', 'GPtrArray',
+                      'GLib.Array', 'GLib.PtrArray',
+                      'GObject.Array', 'GObject.PtrArray'):
             if '.' in base:
                 name = 'GLib.' + base.split('.', 1)[1]
             else:
@@ -688,6 +706,15 @@ raise ValueError."""
         canonical = self._canonicalize_ctype(ctype)
         base = canonical.replace('*', '')
 
+        # While gboolean and _Bool are distinct types, they used to be treated
+        # by scanner as exactly the same one. In general this is incorrect
+        # because of different ABI, but this usually works fine,
+        # so for backward compatibility lets continue for now:
+        # https://gitlab.gnome.org/GNOME/gobject-introspection/merge_requests/24#note_92792
+        if canonical in ('_Bool', 'bool'):
+            canonical = 'gboolean'
+            base = canonical
+
         # Special default: char ** -> ast.Array, same for GStrv
         if (is_return and canonical == 'utf8*') or base == 'GStrv':
             bare_utf8 = ast.TYPE_STRING.clone()
@@ -710,7 +737,10 @@ raise ValueError."""
         if symbol.type == CSYMBOL_TYPE_ELLIPSIS:
             return ast.Parameter('...', ast.Varargs())
         else:
-            ptype = self._create_type_from_base(symbol.base_type, is_parameter=True)
+            if symbol.base_type:
+                ptype = self._create_type_from_base(symbol.base_type, is_parameter=True)
+            else:
+                ptype = None
 
             if symbol.ident is None:
                 if symbol.base_type and symbol.base_type.type != CTYPE_VOID: