def _make_symbol_table(self, symbols, root): '''Make symbol table.''' symbol_table = [] for spec, mangled_name in symbols: if isinstance(spec, str): symbol = spec matcher = SyntaxTreeMatcher.make([{'name': '^%s$' % spec}]) else: symbol = spec['name'] matcher = SyntaxTreeMatcher.make([spec]) symbol_table.append({ 'symbol': symbol, 'matcher': matcher, 'mangled_name': mangled_name, }) def search_node(tree): '''Search matched node.''' for blob in symbol_table: if blob['matcher'].do_match(tree): self.assertTrue('tree' not in blob) # Unique matching blob['tree'] = tree root.traverse(preorder=search_node) for blob in symbol_table: blob['output_name'] = mangle(blob['tree']) return symbol_table
def _make_function(tree, output, cls_name=None): '''Generate ctypes binding of a function declaration.''' if not tree.is_external_linkage(): return cxx_method = tree.kind == CursorKind.CXX_METHOD if cxx_method: name = '%s.%s' % (cls_name, tree.name) symbol_name = mangle(tree) else: name = tree.name symbol_name = tree.spelling output.write('{0} = {1}.{2}\n'.format(name, LIBNAME, symbol_name)) argtypes = ', '.join(make_function_argtypes(tree)) if argtypes: output.write('%s.argtypes = [%s]\n' % (name, argtypes)) if tree.result_type.kind != TypeKind.VOID: restype = make_function_restype(tree) output.write('%s.restype = %s\n' % (name, restype)) errcheck = tree.get_annotation(annotations.ERRCHECK, False) if errcheck: output.write('%s.errcheck = %s\n' % (name, errcheck)) method = tree.get_annotation(annotations.METHOD, False) if cxx_method: if tree.is_static_method(): wrapper = 'staticmethod' else: wrapper = '_CtypesFunctor' output.write('{name} = {wrapper}({name})\n'.format( name=name, wrapper=wrapper)) elif method: output.write('%s = _CtypesFunctor(%s)\n' % (method, name))