Type, Array, Alias, Interface, Class, Node, Union,
Varargs, Constant, type_name_from_ctype,
type_names, TYPE_STRING, BASIC_GIR_TYPES)
-from .config import DATADIR
+from .config import DATADIR, GIR_DIR, GIR_SUFFIX
from .glibast import GLibBoxed
from .girparser import GIRParser
from .odict import odict
CSYMBOL_TYPE_ENUM, CSYMBOL_TYPE_UNION, CSYMBOL_TYPE_OBJECT,
CSYMBOL_TYPE_MEMBER, CSYMBOL_TYPE_ELLIPSIS, CSYMBOL_TYPE_CONST,
TYPE_QUALIFIER_CONST)
-from .utils import strip_common_prefix, to_underscores
+from .utils import to_underscores
_xdg_data_dirs = [x for x in os.environ.get('XDG_DATA_DIRS', '').split(':') \
+ [DATADIR, '/usr/share'] if x]
class Transformer(object):
- def __init__(self, cachestore, generator,
- namespace_name, namespace_version):
+ def __init__(self, cachestore, namespace_name, namespace_version):
self._cachestore = cachestore
- self.generator = generator
+ self.generator = None
self._namespace = Namespace(namespace_name, namespace_version)
self._names = Names()
+ self._pkg_config_packages = set()
self._typedefs_ns = {}
self._strip_prefix = ''
self._includes = set()
def set_strip_prefix(self, strip_prefix):
self._strip_prefix = strip_prefix
+ def get_strip_prefix(self):
+ return self._strip_prefix
+
+ def get_pkgconfig_packages(self):
+ return self._pkg_config_packages
+
+ def set_source_ast(self, src_ast):
+ self.generator = src_ast
+
def parse(self):
nodes = []
for symbol in self.generator.get_symbols():
def _find_include(self, include):
searchdirs = self._includepaths[:]
for path in _xdg_data_dirs:
- searchdirs.append(os.path.join(path, 'gir-1.0'))
+ searchdirs.append(os.path.join(path, GIR_SUFFIX))
+ searchdirs.append(GIR_DIR)
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
- else:
- raise ValueError("Couldn't find include %r (search path: %r)"\
- % (girname, searchdirs))
+ raise ValueError("Couldn't find include %r (search path: %r)"\
+ % (girname, searchdirs))
def _parse_include(self, filename):
parser = self._cachestore.load(filename)
for include in parser.get_includes():
self.register_include(include)
+ for pkg in parser.get_pkgconfig_packages():
+ self._pkg_config_packages.add(pkg)
namespace = parser.get_namespace()
nsname = namespace.name
for node in namespace.nodes:
raise NotImplementedError(
'Transformer: unhandled symbol: %r' % (symbol, ))
+ def _enum_common_prefix(self, symbol):
+ def common_prefix(a, b):
+ 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
+ prefix = None
+ for child in symbol.base_type.child_list:
+ if prefix is None:
+ prefix = child.ident
+ else:
+ prefix = common_prefix(prefix, child.ident)
+ if prefix == '':
+ return None
+ return prefix
+
def _create_enum(self, symbol):
+ prefix = self._enum_common_prefix(symbol)
+ if prefix:
+ prefixlen = len(prefix)
+ else:
+ prefixlen = 0
members = []
for child in symbol.base_type.child_list:
- name = strip_common_prefix(symbol.ident, child.ident).lower()
- members.append(Member(name,
+ if prefixlen > 0:
+ name = child.ident[prefixlen:]
+ else:
+ # Ok, the enum members don't have a consistent prefix
+ # among them, so let's just remove the global namespace
+ # prefix.
+ name = self.remove_prefix(child.ident)
+ members.append(Member(name.lower(),
child.const_int,
child.ident))
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
def _handle_closure(self, param, closure_idx, closure_param):
if (closure_param.type.name == 'any' and
- closure_param.name == 'user_data'):
+ closure_param.name.endswith('data')):
param.closure_name = closure_param.name
param.closure_index = closure_idx
return True
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
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
if j == len(params):
- continue # no more args -> nothing to group look
- # at the param directly following for either a closure
- # or a destroy; only one of these will fire
+ continue # no more args -> nothing to group
+ # look at the param directly following for either a
+ # closure or a destroy; only one of these will fire
had_closure = self._handle_closure(param, j, params[j])
had_destroy = self._handle_destroy(param, j, params[j])
j += 1
if (source_type.type == CTYPE_POINTER and
symbol.base_type.base_type.type == CTYPE_FUNCTION):
node = self._create_callback(symbol)
+ 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:
+ node = self._create_union(symbol, anonymous=True)
else:
# Special handling for fields; we don't have annotations on them
# to apply later, yet.
def _create_type(self, source_type, is_param, is_retval):
ctype = self._create_source_type(source_type)
- if ctype == 'va_list':
+ if ctype.startswith('va_list'):
raise SkipError()
# FIXME: FILE* should not be skipped, it should be handled
# properly instead
rettype.derefed_canonical = self._canonicalize_ctype(derefed_ctype)
canontype = type_name_from_ctype(ctype)
- if ((canontype == TYPE_STRING or
- source_type.type == CTYPE_POINTER) and
- source_type.base_type.type_qualifier & TYPE_QUALIFIER_CONST):
+ # Is it a const char * or a const gpointer?
+ if ((canontype == TYPE_STRING or source_type.type == CTYPE_POINTER) and
+ (source_type.base_type.type_qualifier & TYPE_QUALIFIER_CONST)):
rettype.is_const = True
return rettype
return return_
def _create_const(self, symbol):
+ # Don't create constants for non-public things
+ # http://bugzilla.gnome.org/show_bug.cgi?id=572790
+ 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 None:
+ if symbol.const_string is not None:
+ type_name = 'utf8'
+ value = symbol.const_string
+ elif symbol.const_int is not None:
type_name = 'int'
value = symbol.const_int
+ elif symbol.const_double is not None:
+ type_name = 'double'
+ value = symbol.const_double
else:
- type_name = 'utf8'
- value = symbol.const_string
+ raise AssertionError()
+
const = Constant(name, type_name, value)
return const
self._typedefs_ns[callback.name] = callback
return callback
- def _create_compound(self, klass, symbol):
- compound = self._typedefs_ns.get(symbol.ident, None)
+ 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
return compound
- def _create_struct(self, symbol):
- return self._create_compound(Struct, symbol)
+ def _create_struct(self, symbol, anonymous=False):
+ return self._create_compound(Struct, symbol, anonymous)
- def _create_union(self, symbol):
- return self._create_compound(Union, symbol)
+ def _create_union(self, symbol, anonymous=False):
+ return self._create_compound(Union, symbol, anonymous)
def _create_callback(self, symbol):
- parameters = self._create_parameters(symbol.base_type.base_type)
+ parameters = list(self._create_parameters(symbol.base_type.base_type))
retval = self._create_return(symbol.base_type.base_type.base_type)
+
+ # Mark the 'user_data' arguments
+ for i, param in enumerate(parameters):
+ if (param.type.name == 'any' and
+ param.name == 'user_data'):
+ param.closure_index = i
+
if symbol.ident.find('_') > 0:
name = self.remove_prefix(symbol.ident, True)
else:
name = self.remove_prefix(symbol.ident)
- callback = Callback(name, retval, list(parameters), symbol.ident)
+ callback = Callback(name, retval, parameters, symbol.ident)
return callback
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