Imported Upstream version 1.61.2
[platform/upstream/gobject-introspection.git] / giscanner / transformer.py
index f70d756..bcabded 100644 (file)
 
 import os
 import sys
+import subprocess
 
 from . import ast
 from . import message
+from . import utils
 from .cachestore import CacheStore
 from .girparser import GIRParser
 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)
@@ -39,17 +41,11 @@ class TransformerException(Exception):
     pass
 
 
-_xdg_data_dirs = [x for x in os.environ.get('XDG_DATA_DIRS', '').split(os.pathsep)]
-_xdg_data_dirs.append(DATADIR)
-
-if os.name != 'nt':
-    _xdg_data_dirs.append('/usr/share')
-
-
 class Transformer(object):
     namespace = property(lambda self: self._namespace)
 
-    def __init__(self, namespace, accept_unprefixed=False):
+    def __init__(self, namespace, accept_unprefixed=False,
+                 identifier_filter_cmd=None, symbol_filter_cmd=None):
         self._cachestore = CacheStore()
         self._accept_unprefixed = accept_unprefixed
         self._namespace = namespace
@@ -58,6 +54,13 @@ class Transformer(object):
         self._parsed_includes = {}  # <string namespace -> Namespace>
         self._includepaths = []
         self._passthrough_mode = False
+        self._identifier_filter_cmd = identifier_filter_cmd
+        self._symbol_filter_cmd = symbol_filter_cmd
+
+        # Cache a list of struct/unions in C's "tag namespace". This helps
+        # manage various orderings of typedefs and structs. See:
+        # https://bugzilla.gnome.org/show_bug.cgi?id=581525
+        self._tag_ns = {}
 
     def get_pkgconfig_packages(self):
         return self._pkg_config_packages
@@ -74,7 +77,17 @@ 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
+            # happen when parsing typedefs and structs in particular orderings:
+            #   typedef struct _Foo Foo;
+            #   struct _Foo {...};
             pass
         elif original:
             positions = set()
@@ -87,32 +100,34 @@ class Transformer(object):
 
     def parse(self, symbols):
         for symbol in symbols:
-            ## WORKAROUND ##
+            # WORKAROUND
             # https://bugzilla.gnome.org/show_bug.cgi?id=550616
             if symbol.ident in ['gst_g_error_get_type']:
                 continue
-            node = self._traverse_one(symbol)
-            if node:
-                self._append_new_node(node)
 
-        # Now look through the namespace for things like
-        # typedef struct _Foo Foo;
-        # where we've never seen the struct _Foo.  Just create
-        # an empty structure for these as "disguised"
-        # If we do have a class/interface, merge fields
-        for typedef, compound in self._typedefs_ns.iteritems():
-            ns_compound = self._namespace.get(compound.name)
-            if not ns_compound:
-                ns_compound = self._namespace.get('_' + compound.name)
-            if (not ns_compound and isinstance(compound, (ast.Record, ast.Union))
-            and len(compound.fields) == 0):
-                disguised = ast.Record(compound.name, typedef, disguised=True)
-                self._namespace.append(disguised)
-            elif not ns_compound:
-                self._namespace.append(compound)
-            elif isinstance(ns_compound, (ast.Record, ast.Union)) and len(ns_compound.fields) == 0:
-                ns_compound.fields = compound.fields
-        self._typedefs_ns = None
+            try:
+                node = self._traverse_one(symbol)
+            except TransformerException as e:
+                message.warn_symbol(symbol, e)
+                continue
+
+            if node and node.name:
+                self._append_new_node(node)
+            if isinstance(node, ast.Compound) and node.tag_name and \
+                    node.tag_name not in self._tag_ns:
+                self._tag_ns[node.tag_name] = node
+
+        # Run through the tag namespace looking for structs that have not been
+        # promoted into the main namespace. In this case we simply promote them
+        # with their struct tag.
+        for tag_name, struct in self._tag_ns.items():
+            if not struct.name:
+                try:
+                    name = self.strip_identifier(tag_name)
+                    struct.name = name
+                    self._append_new_node(struct)
+                except TransformerException as e:
+                    message.warn_node(node, e)
 
     def set_include_paths(self, paths):
         self._includepaths = list(paths)
@@ -127,7 +142,7 @@ class Transformer(object):
     def register_include_uninstalled(self, include_path):
         basename = os.path.basename(include_path)
         if not basename.endswith('.gir'):
-            raise SystemExit("Include path %r must be a filename path "
+            raise SystemExit("Include path '%s' must be a filename path "
                              "ending in .gir" % (include_path, ))
         girname = basename[:-4]
         include = ast.Include.from_string(girname)
@@ -148,7 +163,7 @@ namespaces."""
             if ns == self._namespace.name:
                 return self._namespace.get(giname)
             # Fallback to the main namespace if not a dependency and matches a prefix
-            if ns in self._namespace.identifier_prefixes and not ns in self._parsed_includes:
+            if ns in self._namespace.identifier_prefixes and ns not in self._parsed_includes:
                 message.warn(("Deprecated reference to identifier " +
                               "prefix %s in GIName %s") % (ns, name))
                 return self._namespace.get(giname)
@@ -165,18 +180,27 @@ None."""
 
     # Private
 
+    def _get_gi_data_dirs(self):
+        data_dirs = utils.get_system_data_dirs()
+        data_dirs.append(DATADIR)
+        if os.name != 'nt':
+            # For backwards compatibility, was always unconditionally added to the list.
+            data_dirs.append('/usr/share')
+        return data_dirs
+
     def _find_include(self, include):
         searchdirs = self._includepaths[:]
-        for path in _xdg_data_dirs:
+        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:
             path = os.path.join(d, girname)
             if os.path.exists(path):
                 return path
-        sys.stderr.write("Couldn't find include %r (search path: %r)\n" % (girname, searchdirs))
+        sys.stderr.write("Couldn't find include '%s' (search path: '%s')\n" %
+                         (girname, searchdirs))
         sys.exit(1)
 
     @classmethod
@@ -185,8 +209,7 @@ None."""
         if extra_include_dirs is not None:
             self.set_include_paths(extra_include_dirs)
         self.set_passthrough_mode()
-        self._parse_include(filename)
-        parser = self._cachestore.load(filename)
+        parser = self._parse_include(filename)
         self._namespace = parser.get_namespace()
         del self._parsed_includes[self._namespace.name]
         return self
@@ -211,22 +234,39 @@ None."""
                 self._pkg_config_packages.add(pkg)
         namespace = parser.get_namespace()
         self._parsed_includes[namespace.name] = namespace
+        return parser
 
     def _iter_namespaces(self):
         """Return an iterator over all included namespaces; the
 currently-scanned namespace is first."""
         yield self._namespace
-        for ns in self._parsed_includes.itervalues():
+        for ns in self._parsed_includes.values():
             yield ns
 
-    def _sort_matches(self, x, y):
-        if x[0] is self._namespace:
-            return 1
-        elif y[0] is self._namespace:
-            return -1
-        return cmp(x[2], y[2])
+    def _sort_matches(self, val):
+        """Key sort which ensures items in self._namespace are last by returning
+        a tuple key starting with 1 for self._namespace entries and 0 for
+        everythin else.
+        """
+        if val[0] == self._namespace:
+            return 1, val[2]
+        else:
+            return 0, val[2]
 
     def _split_c_string_for_namespace_matches(self, name, is_identifier=False):
+        if not is_identifier and self._symbol_filter_cmd:
+            proc = subprocess.Popen(self._symbol_filter_cmd,
+                                    stdin=subprocess.PIPE,
+                                    stdout=subprocess.PIPE,
+                                    stderr=subprocess.PIPE)
+            proc_name, err = proc.communicate(name.encode())
+            proc_name = proc_name.strip()
+            if proc.returncode:
+                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
         for ns in self._iter_namespaces():
@@ -246,8 +286,8 @@ currently-scanned namespace is first."""
             else:
                 unprefixed_namespaces.append(ns)
         if matches:
-            matches.sort(self._sort_matches)
-            return map(lambda x: (x[0], x[1]), matches)
+            matches.sort(key=self._sort_matches)
+            return list(map(lambda x: (x[0], x[1]), matches))
         elif self._accept_unprefixed:
             return [(self._namespace, name)]
         elif unprefixed_namespaces:
@@ -257,7 +297,7 @@ currently-scanned namespace is first."""
             for ns in unprefixed_namespaces:
                 if name in ns:
                     return [(ns, name)]
-        raise ValueError("Unknown namespace for %s %r"
+        raise ValueError("Unknown namespace for %s '%s'"
                          % ('identifier' if is_identifier else 'symbol', name, ))
 
     def split_ctype_namespaces(self, ident):
@@ -280,6 +320,17 @@ raise ValueError."""
         return matches[-1]
 
     def strip_identifier(self, ident):
+        if self._identifier_filter_cmd:
+            proc = subprocess.Popen(self._identifier_filter_cmd,
+                                    stdin=subprocess.PIPE,
+                                    stdout=subprocess.PIPE,
+                                    stderr=subprocess.PIPE)
+            proc_ident, err = proc.communicate(ident.encode())
+            if proc.returncode:
+                raise ValueError('filter: %r exited: %d with error: %s' %
+                                 (self._identifier_filter_cmd, proc.returncode, err))
+            ident = proc_ident.decode('ascii').strip()
+
         hidden = ident.startswith('_')
         if hidden:
             ident = ident[1:]
@@ -294,8 +345,7 @@ raise ValueError."""
                 return name
         (ns, name) = matches[-1]
         raise TransformerException(
-            "Skipping foreign identifier %r from namespace %s" % (
-            ident, ns.name, ))
+            "Skipping foreign identifier '%s' from namespace %s" % (ident, ns.name, ))
         return None
 
     def _strip_symbol(self, symbol):
@@ -321,23 +371,25 @@ 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:
-            return self._create_struct(symbol)
+            return self._create_tag_ns_compound(ast.Record, symbol)
         elif stype == CSYMBOL_TYPE_ENUM:
             return self._create_enum(symbol)
         elif stype == CSYMBOL_TYPE_MEMBER:
             return self._create_member(symbol, parent_symbol)
         elif stype == CSYMBOL_TYPE_UNION:
-            return self._create_union(symbol)
+            return self._create_tag_ns_compound(ast.Union, symbol)
         elif stype == CSYMBOL_TYPE_CONST:
             return self._create_const(symbol)
         # Ignore variable declarations in the header
         elif stype == CSYMBOL_TYPE_OBJECT:
             pass
         else:
-            print 'transformer: unhandled symbol: %r' % (symbol, )
+            print("transformer: unhandled symbol: '%s'" % (symbol, ))
 
     def _enum_common_prefix(self, symbol):
         def common_prefix(a, b):
@@ -377,21 +429,13 @@ raise ValueError."""
                 # Ok, the enum members don't have a consistent prefix
                 # among them, so let's just remove the global namespace
                 # prefix.
-                try:
-                    name = self._strip_symbol(child)
-                except TransformerException as e:
-                    message.warn_symbol(symbol, e)
-                    return None
+                name = self._strip_symbol(child)
             members.append(ast.Member(name.lower(),
                                       child.const_int,
                                       child.ident,
                                       None))
 
-        try:
-            enum_name = self.strip_identifier(symbol.ident)
-        except TransformerException as e:
-            message.warn_symbol(symbol, e)
-            return None
+        enum_name = self.strip_identifier(symbol.ident)
         if symbol.base_type.is_bitfield:
             klass = ast.Bitfield
         else:
@@ -406,16 +450,25 @@ raise ValueError."""
             return None
         parameters = list(self._create_parameters(symbol, symbol.base_type))
         return_ = self._create_return(symbol.base_type.base_type)
-        try:
-            name = self._strip_symbol(symbol)
-        except TransformerException as e:
-            message.warn_symbol(symbol, e)
-            return None
+        name = self._strip_symbol(symbol)
         func = ast.Function(name, return_, parameters, False, symbol.ident)
         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'
@@ -423,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)
@@ -445,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'
@@ -470,11 +530,7 @@ raise ValueError."""
                 value = 'volatile ' + value
             return value
 
-        return value
-
     def _create_parameters(self, symbol, base_type):
-        # warn if we see annotations for unknown parameters
-        param_names = set(child.ident for child in base_type.child_list)
         for i, child in enumerate(base_type.child_list):
             yield self._create_parameter(symbol, i, child)
 
@@ -506,43 +562,39 @@ raise ValueError."""
         and symbol.base_type.base_type.type == CTYPE_FUNCTION):
             node = self._create_callback(symbol, member=True)
         elif source_type.type == CTYPE_STRUCT and source_type.name is None:
-            node = self._create_struct(symbol, anonymous=True)
+            node = self._create_member_compound(ast.Record, symbol)
         elif source_type.type == CTYPE_UNION and source_type.name is None:
-            node = self._create_union(symbol, anonymous=True)
+            node = self._create_member_compound(ast.Union, symbol)
         else:
             # 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
@@ -560,35 +612,32 @@ raise ValueError."""
         ctype = symbol.base_type.type
         if (ctype == CTYPE_POINTER and symbol.base_type.base_type.type == CTYPE_FUNCTION):
             node = self._create_typedef_callback(symbol)
+        elif (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)
+            node = self._create_typedef_compound(ast.Record, symbol, disguised=True)
         elif ctype == CTYPE_STRUCT:
-            node = self._create_typedef_struct(symbol)
+            node = self._create_typedef_compound(ast.Record, symbol)
         elif ctype == CTYPE_UNION:
-            node = self._create_typedef_union(symbol)
+            node = self._create_typedef_compound(ast.Union, symbol)
         elif ctype == CTYPE_ENUM:
             return self._create_enum(symbol)
         elif ctype in (CTYPE_TYPEDEF,
                        CTYPE_POINTER,
                        CTYPE_BASIC_TYPE,
                        CTYPE_VOID):
-            try:
-                name = self.strip_identifier(symbol.ident)
-            except TransformerException as e:
-                message.warn(e)
-                return None
-            if symbol.base_type.name:
-                complete_ctype = self._create_complete_source_type(symbol.base_type)
-                target = self.create_type_from_ctype_string(symbol.base_type.name,
-                                                            complete_ctype=complete_ctype)
-            else:
-                target = ast.TYPE_ANY
+            name = self.strip_identifier(symbol.ident)
+            target = self._create_type_from_base(symbol.base_type)
             if name in ast.type_names:
                 return None
-            return ast.Alias(name, target, ctype=symbol.ident)
+            # https://bugzilla.gnome.org/show_bug.cgi?id=755882
+            if name.endswith('_autoptr'):
+                return None
+            node = ast.Alias(name, target, ctype=symbol.ident)
+            node.add_symbol_reference(symbol)
         else:
             raise NotImplementedError(
-                "symbol %r of type %s" % (symbol.ident, ctype_name(ctype)))
+                "symbol '%s' of type %s" % (symbol.ident, ctype_name(ctype)))
         return node
 
     def _canonicalize_ctype(self, ctype):
@@ -616,23 +665,9 @@ raise ValueError."""
 
         return canonical
 
-    def parse_ctype(self, ctype, is_member=False):
-        canonical = self._canonicalize_ctype(ctype)
-
-        # Remove all pointers - we require standard calling
-        # conventions.  For example, an 'int' is always passed by
-        # value (unless it's out or inout).
-        derefed_typename = canonical.replace('*', '')
-
-        # Preserve "pointerness" of struct/union members
-        if (is_member and canonical.endswith('*') and derefed_typename in ast.basic_type_names):
-            return 'gpointer'
-        else:
-            return derefed_typename
-
     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,
@@ -648,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:
@@ -668,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()
@@ -688,18 +735,21 @@ raise ValueError."""
 
     def _create_parameter(self, parent_symbol, index, symbol):
         if symbol.type == CSYMBOL_TYPE_ELLIPSIS:
-            ptype = ast.Varargs()
+            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:
-                message.warn_symbol(parent_symbol, "missing parameter name; undocumentable")
-            ident = 'arg%d' % (index, )
-        else:
-            ident = symbol.ident
+            if symbol.ident is None:
+                if symbol.base_type and symbol.base_type.type != CTYPE_VOID:
+                    message.warn_symbol(parent_symbol, "missing parameter name; undocumentable")
+                ident = 'arg%d' % (index, )
+            else:
+                ident = symbol.ident
 
-        return ast.Parameter(ident, ptype)
+            return ast.Parameter(ident, ptype)
 
     def _create_return(self, source_type):
         typeval = self._create_type_from_base(source_type, is_return=True)
@@ -713,14 +763,10 @@ raise ValueError."""
         # http://bugzilla.gnome.org/show_bug.cgi?id=572790
         if (symbol.source_filename is None or not symbol.source_filename.endswith('.h')):
             return None
-        try:
-            name = self._strip_symbol(symbol)
-        except TransformerException as e:
-            message.warn_symbol(symbol, e)
-            return None
+        name = self._strip_symbol(symbol)
         if symbol.const_string is not None:
             typeval = ast.TYPE_STRING
-            value = unicode(symbol.const_string, 'utf-8')
+            value = symbol.const_string
         elif symbol.const_int is not None:
             if symbol.base_type is not None:
                 typeval = self._create_type_from_base(symbol.base_type)
@@ -743,6 +789,9 @@ raise ValueError."""
                 value = str(symbol.const_int % 2 ** 16)
             else:
                 value = str(symbol.const_int)
+        elif symbol.const_boolean is not None:
+            typeval = ast.TYPE_BOOLEAN
+            value = "true" if symbol.const_boolean else "false"
         elif symbol.const_double is not None:
             typeval = ast.TYPE_DOUBLE
             value = '%f' % (symbol.const_double, )
@@ -754,35 +803,88 @@ raise ValueError."""
         const.add_symbol_reference(symbol)
         return const
 
-    def _create_typedef_struct(self, symbol, disguised=False):
-        try:
-            name = self.strip_identifier(symbol.ident)
-        except TransformerException as e:
-            message.warn_symbol(symbol, e)
-            return None
-        struct = ast.Record(name, symbol.ident, disguised=disguised)
-        self._parse_fields(symbol, struct)
-        struct.add_symbol_reference(symbol)
-        self._typedefs_ns[symbol.ident] = struct
-        return None
+    def _create_typedef_compound(self, compound_class, symbol, disguised=False):
+        name = self.strip_identifier(symbol.ident)
+        assert symbol.base_type
+        if symbol.base_type.name:
+            tag_name = symbol.base_type.name
+        else:
+            tag_name = None
+
+        # If the struct already exists in the tag namespace, use it.
+        if tag_name in self._tag_ns:
+            compound = self._tag_ns[tag_name]
+            if compound.name:
+                # If the struct name is set it means the struct has already been
+                # promoted from the tag namespace to the main namespace by a
+                # prior typedef struct. If we get here it means this is another
+                # typedef of that struct. Instead of creating an alias to the
+                # primary typedef that has been promoted, we create a new Record
+                # with shared fields. This handles the case where we want to
+                # give structs like GInitiallyUnowned its own Record:
+                #    typedef struct _GObject GObject;
+                #    typedef struct _GObject GInitiallyUnowned;
+                # See: http://bugzilla.gnome.org/show_bug.cgi?id=569408
+                new_compound = compound_class(name, symbol.ident, tag_name=tag_name)
+                new_compound.fields = compound.fields
+                new_compound.add_symbol_reference(symbol)
+                return new_compound
+            else:
+                # If the struct does not have its name set, it exists only in
+                # the tag namespace. Set it here and return it which will
+                # promote it to the main namespace. Essentially the first
+                # typedef for a struct clobbers its name and ctype which is what
+                # will be visible to GI.
+                compound.name = name
+                compound.ctype = symbol.ident
+        else:
+            # Create a new struct with a typedef name and tag name when available.
+            # Structs with a typedef name are promoted into the main namespace
+            # by it being returned to the "parse" function and are also added to
+            # the tag namespace if it has a tag_name set.
+            compound = compound_class(name, symbol.ident, disguised=disguised, tag_name=tag_name)
+            if tag_name:
+                # Force the struct as disguised for now since we do not yet know
+                # if it has fields that will be parsed. Note that this is using
+                # an erroneous definition of disguised and we should eventually
+                # only look at the field count when needed.
+                compound.disguised = True
+            else:
+                # Case where we have an anonymous struct which is typedef'd:
+                #   typedef struct {...} Struct;
+                # we need to parse the fields because we never get a struct
+                # in the tag namespace which is normally where fields are parsed.
+                self._parse_fields(symbol, compound)
 
-    def _create_typedef_union(self, symbol):
-        try:
-            name = self.strip_identifier(symbol.ident)
-        except TransformerException as e:
-            message.warn(e)
-            return None
-        union = ast.Union(name, symbol.ident)
-        self._parse_fields(symbol, union)
-        union.add_symbol_reference(symbol)
-        self._typedefs_ns[symbol.ident] = union
-        return None
+        compound.add_symbol_reference(symbol)
+        return compound
+
+    def _create_tag_ns_compound(self, compound_class, symbol):
+        # Get or create a struct from C's tag namespace
+        if symbol.ident in self._tag_ns:
+            compound = self._tag_ns[symbol.ident]
+        else:
+            compound = compound_class(None, symbol.ident, tag_name=symbol.ident)
+
+        # Make sure disguised is False as we are now about to parse the
+        # fields of the real struct.
+        compound.disguised = False
+        # Fields may need to be parsed in either of the above cases because the
+        # Record can be created with a typedef prior to the struct definition.
+        self._parse_fields(symbol, compound)
+        compound.add_symbol_reference(symbol)
+        return compound
+
+    def _create_member_compound(self, compound_class, symbol):
+        compound = compound_class(symbol.ident, symbol.ident)
+        self._parse_fields(symbol, compound)
+        compound.add_symbol_reference(symbol)
+        return compound
 
     def _create_typedef_callback(self, symbol):
         callback = self._create_callback(symbol)
         if not callback:
             return None
-        self._typedefs_ns[callback.name] = callback
         return callback
 
     def _parse_fields(self, symbol, compound):
@@ -797,44 +899,15 @@ raise ValueError."""
                               anonymous_node=child_node)
             compound.fields.append(field)
 
-    def _create_compound(self, klass, symbol, anonymous):
-        if symbol.ident is None:
-            # the compound is an anonymous member of another union or a struct
-            assert anonymous
-            compound = klass(None, None)
-        else:
-            compound = self._typedefs_ns.get(symbol.ident, None)
-
-        if compound is None:
-            # This is a bit of a hack; really we should try
-            # to resolve through the typedefs to find the real
-            # name
-            if symbol.ident.startswith('_'):
-                compound = self._typedefs_ns.get(symbol.ident[1:], None)
-            if compound is None:
-                if anonymous:
-                    name = symbol.ident
-                else:
-                    try:
-                        name = self.strip_identifier(symbol.ident)
-                    except TransformerException as e:
-                        message.warn(e)
-                        return None
-                compound = klass(name, symbol.ident)
-
-        self._parse_fields(symbol, compound)
-        compound.add_symbol_reference(symbol)
-        return compound
-
-    def _create_struct(self, symbol, anonymous=False):
-        return self._create_compound(ast.Record, symbol, anonymous)
-
-    def _create_union(self, symbol, anonymous=False):
-        return self._create_compound(ast.Union, symbol, anonymous)
-
     def _create_callback(self, symbol, member=False):
-        parameters = list(self._create_parameters(symbol, symbol.base_type.base_type))
-        retval = self._create_return(symbol.base_type.base_type.base_type)
+        if (symbol.base_type.type == CTYPE_FUNCTION):  # function
+            paramtype = symbol.base_type
+            retvaltype = symbol.base_type.base_type
+        elif (symbol.base_type.type == CTYPE_POINTER):  # function pointer
+            paramtype = symbol.base_type.base_type
+            retvaltype = symbol.base_type.base_type.base_type
+        parameters = list(self._create_parameters(symbol, paramtype))
+        retval = self._create_return(retvaltype)
 
         # Mark the 'user_data' arguments
         for i, param in enumerate(parameters):
@@ -844,17 +917,9 @@ raise ValueError."""
         if member:
             name = symbol.ident
         elif symbol.ident.find('_') > 0:
-            try:
-                name = self._strip_symbol(symbol)
-            except TransformerException as e:
-                message.warn_symbol(symbol, e)
-                return None
+            name = self._strip_symbol(symbol)
         else:
-            try:
-                name = self.strip_identifier(symbol.ident)
-            except TransformerException as e:
-                message.warn(e)
-                return None
+            name = self.strip_identifier(symbol.ident)
         callback = ast.Callback(name, retval, parameters, False,
                                 ctype=symbol.ident)
         callback.add_symbol_reference(symbol)
@@ -888,7 +953,7 @@ Note that type resolution may not succeed."""
         # which has nominal namespace of "Meta", but a few classes are
         # "Mutter".  We don't export that data in introspection currently.
         # Basically the library should be fixed, but we'll hack around it here.
-        for namespace in self._parsed_includes.itervalues():
+        for namespace in self._parsed_includes.values():
             target = namespace.get_by_ctype(pointer_stripped)
             if target:
                 typeval.target_giname = '%s.%s' % (namespace.name, target.name)
@@ -953,29 +1018,6 @@ Note that type resolution may not succeed."""
 
         return typeval.resolved
 
-    def _typepair_to_str(self, item):
-        nsname, item = item
-        if nsname is None:
-            return item.name
-        return '%s.%s' % (nsname, item.name)
-
-    def gtypename_to_giname(self, gtname, names):
-        resolved = names.type_names.get(gtname)
-        if resolved:
-            return self._typepair_to_str(resolved)
-        resolved = self._names.type_names.get(gtname)
-        if resolved:
-            return self._typepair_to_str(resolved)
-        raise KeyError("Failed to resolve GType name: %r" % (gtname, ))
-
-    def ctype_of(self, obj):
-        if hasattr(obj, 'ctype'):
-            return obj.ctype
-        elif hasattr(obj, 'symbol'):
-            return obj.symbol
-        else:
-            return None
-
     def resolve_aliases(self, typenode):
         """Removes all aliases from typenode, returns first non-alias
         in the typenode alias chain.  Returns typenode argument if it
@@ -984,7 +1026,7 @@ Note that type resolution may not succeed."""
             if typenode.target.target_giname is not None:
                 typenode = self.lookup_giname(typenode.target.target_giname)
             elif typenode.target.target_fundamental is not None:
-                typenode = ast.type_names[typenode.target.target_fundamental]
+                typenode = typenode.target
             else:
                 break
         return typenode