diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2013-06-25 22:59:01 +0000 |
---|---|---|
committer | <> | 2013-09-27 11:49:28 +0000 |
commit | 8c4528713d907ee2cfd3bfcbbad272c749867f84 (patch) | |
tree | c09e2ce80f47b90c85cc720f5139089ad9c8cfff /libs/python/pyste/src/Pyste/FunctionExporter.py | |
download | boost-tarball-baserock/morph.tar.gz |
Imported from /home/lorry/working-area/delta_boost-tarball/boost_1_54_0.tar.bz2.boost_1_54_0baserock/morph
Diffstat (limited to 'libs/python/pyste/src/Pyste/FunctionExporter.py')
-rw-r--r-- | libs/python/pyste/src/Pyste/FunctionExporter.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/libs/python/pyste/src/Pyste/FunctionExporter.py b/libs/python/pyste/src/Pyste/FunctionExporter.py new file mode 100644 index 000000000..5765f65e9 --- /dev/null +++ b/libs/python/pyste/src/Pyste/FunctionExporter.py @@ -0,0 +1,92 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +from Exporter import Exporter +from policies import * +from declarations import * +from settings import * +import utils +import exporterutils + + +#============================================================================== +# FunctionExporter +#============================================================================== +class FunctionExporter(Exporter): + 'Generates boost.python code to export the given function.' + + def __init__(self, info, tail=None): + Exporter.__init__(self, info, tail) + + + def Export(self, codeunit, exported_names): + if not self.info.exclude: + decls = self.GetDeclarations(self.info.name) + for decl in decls: + self.info.policy = exporterutils.HandlePolicy(decl, self.info.policy) + self.ExportDeclaration(decl, len(decls) == 1, codeunit) + self.ExportOpaquePointer(decl, codeunit) + self.GenerateOverloads(decls, codeunit) + exported_names[self.Name()] = 1 + + + def ExportDeclaration(self, decl, unique, codeunit): + name = self.info.rename or decl.name + defs = namespaces.python + 'def("%s", ' % name + wrapper = self.info.wrapper + if wrapper: + pointer = '&' + wrapper.FullName() + else: + pointer = decl.PointerDeclaration() + defs += pointer + defs += self.PolicyCode() + overload = self.OverloadName(decl) + if overload: + defs += ', %s()' % (namespaces.pyste + overload) + defs += ');' + codeunit.Write('module', self.INDENT + defs + '\n') + # add the code of the wrapper + if wrapper and wrapper.code: + codeunit.Write('declaration', wrapper.code + '\n') + + + def OverloadName(self, decl): + if decl.minArgs != decl.maxArgs: + return '%s_overloads_%i_%i' % \ + (decl.name, decl.minArgs, decl.maxArgs) + else: + return '' + + + def GenerateOverloads(self, declarations, codeunit): + codes = {} + for decl in declarations: + overload = self.OverloadName(decl) + if overload and overload not in codes: + code = 'BOOST_PYTHON_FUNCTION_OVERLOADS(%s, %s, %i, %i)' %\ + (overload, decl.FullName(), decl.minArgs, decl.maxArgs) + codeunit.Write('declaration', code + '\n') + codes[overload] = None + + + def PolicyCode(self): + policy = self.info.policy + if policy is not None: + assert isinstance(policy, Policy) + return ', %s()' % policy.Code() + else: + return '' + + + def ExportOpaquePointer(self, function, codeunit): + if self.info.policy == return_value_policy(return_opaque_pointer): + typename = function.result.name + macro = exporterutils.EspecializeTypeID(typename) + if macro: + codeunit.Write('declaration-outside', macro) + + + def Name(self): + return self.info.name |