gtsam/gtwrap/template_instantiator.py

499 lines
19 KiB
Python
Raw Normal View History

"""Code to help instantiate templated classes, methods and functions."""
# pylint: disable=too-many-arguments, too-many-instance-attributes, no-self-use, no-else-return, too-many-arguments, unused-format-string-argument, unused-variable
import itertools
from copy import deepcopy
from typing import List
Squashed 'wrap/' changes from dfa624e77..09f8bbf71 09f8bbf71 Merge pull request #25 from borglab/fix/function-name 0dbfb6c13 fix function name to be the correct one f69f8b01f Merge pull request #24 from borglab/fix/pip 6519a6627 use pip install to overcome superuser issues b11ecf4e8 Merge pull request #23 from borglab/fix/remove-pip-args 813030108 remove pip-args since we are using setup.py 498d233e0 Merge pull request #22 from borglab/fix/package-install 846212ac3 set correct flags for installing gtwrap package 62161cd20 Merge pull request #21 from borglab/feature/script-vars 93be1d9f8 set script variables and move pybind11 loading so gtwrap can be used under gtsam 8770e3c7e Merge pull request #20 from borglab/fix/pybind-include 8c3c83618 proper placement of pybind11 include a9ad4f504 Merge pull request #19 from borglab/feature/package 99d8a12c7 added more documentation 4cbec1579 change to macro so we don't have to deal with function scopes b83e405b8 updates to completely install the package 38a64b3de new scripts which will be installed to bin directory bf9646235 Merge pull request #18 from borglab/fix/cmake-min c7c280099 Consistent cmake minimum required 42df58f62 Merge pull request #17 from borglab/fix/cleanup e580b282d version bump 4ccd66fa5 More finegrained handling of Python version 6476fd710 Merge pull request #16 from borglab/feature/better-find-python 8ac1296a0 use setup.py to install dependencies e9ac473be install dependencies and support versions of CMake<3.12 cf272dbd2 Merge pull request #15 from borglab/feature/utils ffc9cc4f7 new utils to reduce boilerplate 20e8e8b7a Merge pull request #11 from borglab/feature/package 04b844bd6 use new version of FindPython and be consistent 3f9d7a32a Merge pull request #13 from borglab/add_license c791075a6 Add LICENSE 517b67c46 correct working directory for setup.py 1b22b47ae move matlab.h to root directory 37b407214 Proper source directory path for use in other projects 61696dd5d configure PybindWrap within the cmake directory 1b91fc9af add config file so we can use find_package a1e6f4f53 small typo da9f351be updated README and housekeeping 64b8f78d5 files needed to allow for packaging bddda7f54 package structure git-subtree-dir: wrap git-subtree-split: 09f8bbf7172ba8b1bd3d2484795743f16e1a5893
2021-01-05 02:11:36 +08:00
import gtwrap.interface_parser as parser
def instantiate_type(ctype: parser.Type,
template_typenames: List[str],
instantiations: List[parser.Typename],
cpp_typename: parser.Typename,
instantiated_class=None):
"""
Instantiate template typename for @p ctype.
Args:
instiated_class (InstantiatedClass):
@return If ctype's name is in the @p template_typenames, return the
corresponding type to replace in @p instantiations.
If ctype name is `This`, return the new typename @p `cpp_typename`.
Otherwise, return the original ctype.
"""
# make a deep copy so that there is no overwriting of original template params
ctype = deepcopy(ctype)
# Check if the return type has template parameters
if len(ctype.typename.instantiations) > 0:
for idx, instantiation in enumerate(ctype.typename.instantiations):
if instantiation.name in template_typenames:
template_idx = template_typenames.index(instantiation.name)
ctype.typename.instantiations[idx] = instantiations[
template_idx]
return ctype
str_arg_typename = str(ctype.typename)
if str_arg_typename in template_typenames:
idx = template_typenames.index(str_arg_typename)
return parser.Type(
typename=instantiations[idx],
is_const=ctype.is_const,
Squashed 'wrap/' changes from aae9b4605..3eff76f60 3eff76f60 Merge pull request #53 from borglab/feature/refactor 13215dfa7 Merge pull request #52 from borglab/fix/tests 696913b11 install setuptools 9523328ba Merge branch 'master' into fix/tests 7c630b361 some more cleanup 656993a71 cleaned up Typename a16f6f38e move qualified and basis type outside to their own class scope 72ead8bd7 Merge pull request #51 from borglab/fix/test-interface-parser 6deefd4fc added tests for interface_parser 50d490a85 Merge pull request #50 from borglab/feature/refs-all-types be4511290 updated docs for BasisType 0e80b0d8c update MATLAB tests 0015d7397 added support for shared pointer and ref for basis types 86d2158f1 remove std::string from list of Basis types 94f928441 ignore code coverage reports 2033dd345 replace prints with log.debug statements ae98091b3 fix deprecation in doc tests 13a2f66c4 Merge pull request #46 from borglab/feature/new-shared-pointer 3c7d85865 updated docs 6d7897088 use @ for raw pointer, go back to * for shared pointer 1d6194c57 updated matlab wrapper to handle both raw and shared pointers 1448f6924 fix some failing tests 2ab1dae32 Merge branch 'master' into feature/new-shared-pointer 96f8a56bd Merge pull request #47 from borglab/fix/ci 6003203f3 run CI only for pull requests a8f29ead1 fix the python version yml key fcae17227 check if directory exists when testing f592f08c9 explicit pip3 so that we don't use Python2 d49c2f3c2 correct call for pip dfe360526 fix the CI 149b7c523 docs for templated functions f2189acc6 support typedefing for templated functions 965458a2b added test for templated functions eaff6e6ab made is_const common for all types 3d9c70b32 added tests and cleaned up a bit 010b89626 support for simple pointers on basis types 6b98fd80c new syntax for shared_ptr ff7ad0b78 support for templated functions a1a443c8d Merge pull request #43 from borglab/fix/cmake-and-matlab 2f3a055e4 remove accidentally committed file 770d055e2 set proper paths for cmake and eschew relative paths 773d01ae1 fix bug in matlab wrapper 721ef740f Merge pull request #41 from borglab/feature/type-hints 67aac9758 minor refactor of CI yml e6a63ae0c fix all mypy issues a3aaa3e7c remove a lot of linter issues from matlab_wrapper a96db522f static typing for interface_parser git-subtree-dir: wrap git-subtree-split: 3eff76f604b5ba9e71cf4947654e288142ed7a94
2021-03-24 12:36:02 +08:00
is_shared_ptr=ctype.is_shared_ptr,
is_ptr=ctype.is_ptr,
is_ref=ctype.is_ref,
is_basis=ctype.is_basis,
)
elif str_arg_typename == 'This':
if instantiated_class:
name = instantiated_class.original.name
namespaces_name = instantiated_class.namespaces()
namespaces_name.append(name)
# print("INST: {}, {}, CPP: {}, CLS: {}".format(
# ctype, instantiations, cpp_typename, instantiated_class.instantiations
# ), file=sys.stderr)
cpp_typename = parser.Typename(
namespaces_name,
instantiations=instantiated_class.instantiations)
return parser.Type(
typename=cpp_typename,
is_const=ctype.is_const,
Squashed 'wrap/' changes from aae9b4605..3eff76f60 3eff76f60 Merge pull request #53 from borglab/feature/refactor 13215dfa7 Merge pull request #52 from borglab/fix/tests 696913b11 install setuptools 9523328ba Merge branch 'master' into fix/tests 7c630b361 some more cleanup 656993a71 cleaned up Typename a16f6f38e move qualified and basis type outside to their own class scope 72ead8bd7 Merge pull request #51 from borglab/fix/test-interface-parser 6deefd4fc added tests for interface_parser 50d490a85 Merge pull request #50 from borglab/feature/refs-all-types be4511290 updated docs for BasisType 0e80b0d8c update MATLAB tests 0015d7397 added support for shared pointer and ref for basis types 86d2158f1 remove std::string from list of Basis types 94f928441 ignore code coverage reports 2033dd345 replace prints with log.debug statements ae98091b3 fix deprecation in doc tests 13a2f66c4 Merge pull request #46 from borglab/feature/new-shared-pointer 3c7d85865 updated docs 6d7897088 use @ for raw pointer, go back to * for shared pointer 1d6194c57 updated matlab wrapper to handle both raw and shared pointers 1448f6924 fix some failing tests 2ab1dae32 Merge branch 'master' into feature/new-shared-pointer 96f8a56bd Merge pull request #47 from borglab/fix/ci 6003203f3 run CI only for pull requests a8f29ead1 fix the python version yml key fcae17227 check if directory exists when testing f592f08c9 explicit pip3 so that we don't use Python2 d49c2f3c2 correct call for pip dfe360526 fix the CI 149b7c523 docs for templated functions f2189acc6 support typedefing for templated functions 965458a2b added test for templated functions eaff6e6ab made is_const common for all types 3d9c70b32 added tests and cleaned up a bit 010b89626 support for simple pointers on basis types 6b98fd80c new syntax for shared_ptr ff7ad0b78 support for templated functions a1a443c8d Merge pull request #43 from borglab/fix/cmake-and-matlab 2f3a055e4 remove accidentally committed file 770d055e2 set proper paths for cmake and eschew relative paths 773d01ae1 fix bug in matlab wrapper 721ef740f Merge pull request #41 from borglab/feature/type-hints 67aac9758 minor refactor of CI yml e6a63ae0c fix all mypy issues a3aaa3e7c remove a lot of linter issues from matlab_wrapper a96db522f static typing for interface_parser git-subtree-dir: wrap git-subtree-split: 3eff76f604b5ba9e71cf4947654e288142ed7a94
2021-03-24 12:36:02 +08:00
is_shared_ptr=ctype.is_shared_ptr,
is_ptr=ctype.is_ptr,
is_ref=ctype.is_ref,
is_basis=ctype.is_basis,
)
else:
return ctype
def instantiate_args_list(args_list, template_typenames, instantiations,
cpp_typename):
"""
Instantiate template typenames in an argument list.
Type with name `This` will be replaced by @p `cpp_typename`.
@param[in] args_list A list of `parser.Argument` to instantiate.
@param[in] template_typenames List of template typenames to instantiate,
e.g. ['T', 'U', 'V'].
@param[in] instantiations List of specific types to instantiate, each
associated with each template typename. Each type is a parser.Typename,
including its name and full namespaces.
@param[in] cpp_typename Full-namespace cpp class name of this instantiation
to replace for arguments of type named `This`.
@return A new list of parser.Argument which types are replaced with their
instantiations.
"""
instantiated_args = []
for arg in args_list:
new_type = instantiate_type(
arg.ctype, template_typenames, instantiations, cpp_typename)
instantiated_args.append(
parser.Argument(name=arg.name, ctype=new_type))
return instantiated_args
def instantiate_return_type(return_type, template_typenames, instantiations,
cpp_typename, instantiated_class=None):
"""Instantiate the return type."""
new_type1 = instantiate_type(return_type.type1,
template_typenames,
instantiations,
cpp_typename,
instantiated_class=instantiated_class)
if return_type.type2:
new_type2 = instantiate_type(return_type.type2,
template_typenames,
instantiations,
cpp_typename,
instantiated_class=instantiated_class)
else:
new_type2 = ''
return parser.ReturnType(new_type1, new_type2)
def instantiate_name(original_name, instantiations):
"""
Concatenate instantiated types with an @p original name to form a new
instantiated name.
TODO(duy): To avoid conflicts, we should include the instantiation's
namespaces, but I find that too verbose.
"""
inst_name = ''
instantiated_names = []
for inst in instantiations:
# Ensure the first character of the type is capitalized
name = inst.instantiated_name()
Squashed 'wrap/' changes from aae9b4605..3eff76f60 3eff76f60 Merge pull request #53 from borglab/feature/refactor 13215dfa7 Merge pull request #52 from borglab/fix/tests 696913b11 install setuptools 9523328ba Merge branch 'master' into fix/tests 7c630b361 some more cleanup 656993a71 cleaned up Typename a16f6f38e move qualified and basis type outside to their own class scope 72ead8bd7 Merge pull request #51 from borglab/fix/test-interface-parser 6deefd4fc added tests for interface_parser 50d490a85 Merge pull request #50 from borglab/feature/refs-all-types be4511290 updated docs for BasisType 0e80b0d8c update MATLAB tests 0015d7397 added support for shared pointer and ref for basis types 86d2158f1 remove std::string from list of Basis types 94f928441 ignore code coverage reports 2033dd345 replace prints with log.debug statements ae98091b3 fix deprecation in doc tests 13a2f66c4 Merge pull request #46 from borglab/feature/new-shared-pointer 3c7d85865 updated docs 6d7897088 use @ for raw pointer, go back to * for shared pointer 1d6194c57 updated matlab wrapper to handle both raw and shared pointers 1448f6924 fix some failing tests 2ab1dae32 Merge branch 'master' into feature/new-shared-pointer 96f8a56bd Merge pull request #47 from borglab/fix/ci 6003203f3 run CI only for pull requests a8f29ead1 fix the python version yml key fcae17227 check if directory exists when testing f592f08c9 explicit pip3 so that we don't use Python2 d49c2f3c2 correct call for pip dfe360526 fix the CI 149b7c523 docs for templated functions f2189acc6 support typedefing for templated functions 965458a2b added test for templated functions eaff6e6ab made is_const common for all types 3d9c70b32 added tests and cleaned up a bit 010b89626 support for simple pointers on basis types 6b98fd80c new syntax for shared_ptr ff7ad0b78 support for templated functions a1a443c8d Merge pull request #43 from borglab/fix/cmake-and-matlab 2f3a055e4 remove accidentally committed file 770d055e2 set proper paths for cmake and eschew relative paths 773d01ae1 fix bug in matlab wrapper 721ef740f Merge pull request #41 from borglab/feature/type-hints 67aac9758 minor refactor of CI yml e6a63ae0c fix all mypy issues a3aaa3e7c remove a lot of linter issues from matlab_wrapper a96db522f static typing for interface_parser git-subtree-dir: wrap git-subtree-split: 3eff76f604b5ba9e71cf4947654e288142ed7a94
2021-03-24 12:36:02 +08:00
# Using `capitalize` on the complete name causes other caps to be lower case
instantiated_names.append(name.replace(name[0], name[0].capitalize()))
return "{}{}".format(original_name, "".join(instantiated_names))
Squashed 'wrap/' changes from aae9b4605..3eff76f60 3eff76f60 Merge pull request #53 from borglab/feature/refactor 13215dfa7 Merge pull request #52 from borglab/fix/tests 696913b11 install setuptools 9523328ba Merge branch 'master' into fix/tests 7c630b361 some more cleanup 656993a71 cleaned up Typename a16f6f38e move qualified and basis type outside to their own class scope 72ead8bd7 Merge pull request #51 from borglab/fix/test-interface-parser 6deefd4fc added tests for interface_parser 50d490a85 Merge pull request #50 from borglab/feature/refs-all-types be4511290 updated docs for BasisType 0e80b0d8c update MATLAB tests 0015d7397 added support for shared pointer and ref for basis types 86d2158f1 remove std::string from list of Basis types 94f928441 ignore code coverage reports 2033dd345 replace prints with log.debug statements ae98091b3 fix deprecation in doc tests 13a2f66c4 Merge pull request #46 from borglab/feature/new-shared-pointer 3c7d85865 updated docs 6d7897088 use @ for raw pointer, go back to * for shared pointer 1d6194c57 updated matlab wrapper to handle both raw and shared pointers 1448f6924 fix some failing tests 2ab1dae32 Merge branch 'master' into feature/new-shared-pointer 96f8a56bd Merge pull request #47 from borglab/fix/ci 6003203f3 run CI only for pull requests a8f29ead1 fix the python version yml key fcae17227 check if directory exists when testing f592f08c9 explicit pip3 so that we don't use Python2 d49c2f3c2 correct call for pip dfe360526 fix the CI 149b7c523 docs for templated functions f2189acc6 support typedefing for templated functions 965458a2b added test for templated functions eaff6e6ab made is_const common for all types 3d9c70b32 added tests and cleaned up a bit 010b89626 support for simple pointers on basis types 6b98fd80c new syntax for shared_ptr ff7ad0b78 support for templated functions a1a443c8d Merge pull request #43 from borglab/fix/cmake-and-matlab 2f3a055e4 remove accidentally committed file 770d055e2 set proper paths for cmake and eschew relative paths 773d01ae1 fix bug in matlab wrapper 721ef740f Merge pull request #41 from borglab/feature/type-hints 67aac9758 minor refactor of CI yml e6a63ae0c fix all mypy issues a3aaa3e7c remove a lot of linter issues from matlab_wrapper a96db522f static typing for interface_parser git-subtree-dir: wrap git-subtree-split: 3eff76f604b5ba9e71cf4947654e288142ed7a94
2021-03-24 12:36:02 +08:00
class InstantiatedGlobalFunction(parser.GlobalFunction):
"""
Instantiate global functions.
E.g.
template<T = {double}>
T add(const T& x, const T& y);
"""
def __init__(self, original, instantiations=(), new_name=''):
self.original = original
self.instantiations = instantiations
self.template = ''
self.parent = original.parent
if not original.template:
self.name = original.name
self.return_type = original.return_type
self.args = original.args
else:
self.name = instantiate_name(
original.name, instantiations) if not new_name else new_name
self.return_type = instantiate_return_type(
original.return_type,
self.original.template.typenames,
self.instantiations,
# Keyword type name `This` should already be replaced in the
# previous class template instantiation round.
cpp_typename='',
)
instantiated_args = instantiate_args_list(
original.args.args_list,
self.original.template.typenames,
self.instantiations,
# Keyword type name `This` should already be replaced in the
# previous class template instantiation round.
cpp_typename='',
)
self.args = parser.ArgumentList(instantiated_args)
super().__init__(self.name,
self.return_type,
self.args,
self.template,
parent=self.parent)
def to_cpp(self):
"""Generate the C++ code for wrapping."""
if self.original.template:
instantiated_names = [inst.instantiated_name() for inst in self.instantiations]
ret = "{}<{}>".format(self.original.name, ",".join(instantiated_names))
else:
ret = self.original.name
return ret
def __repr__(self):
return "Instantiated {}".format(
super(InstantiatedGlobalFunction, self).__repr__()
)
class InstantiatedMethod(parser.Method):
"""
We can only instantiate template methods with a single template parameter.
"""
def __init__(self, original, instantiation=''):
self.original = original
self.instantiation = instantiation
self.template = ''
self.is_const = original.is_const
self.parent = original.parent
if not original.template:
self.name = original.name
self.return_type = original.return_type
self.args = original.args
else:
#TODO(Varun) enable multiple templates for methods
if len(self.original.template.typenames) > 1:
raise ValueError("Can only instantiate template method with "
"single template parameter.")
self.name = instantiate_name(original.name, [self.instantiation])
self.return_type = instantiate_return_type(
original.return_type,
[self.original.template.typenames[0]],
[self.instantiation],
# Keyword type name `This` should already be replaced in the
# previous class template instantiation round.
cpp_typename='',
)
instantiated_args = instantiate_args_list(
original.args.args_list,
[self.original.template.typenames[0]],
[self.instantiation],
# Keyword type name `This` should already be replaced in the
# previous class template instantiation round.
cpp_typename='',
)
self.args = parser.ArgumentList(instantiated_args)
super().__init__(self.template,
self.name,
self.return_type,
self.args,
self.is_const,
parent=self.parent)
def to_cpp(self):
"""Generate the C++ code for wrapping."""
if self.original.template:
ret = "{}<{}>".format(self.original.name, self.instantiation)
else:
ret = self.original.name
return ret
def __repr__(self):
return "Instantiated {}".format(
super(InstantiatedMethod, self).__repr__()
)
class InstantiatedClass(parser.Class):
"""
Instantiate the class defined in the interface file.
"""
def __init__(self, original, instantiations=(), new_name=''):
"""
Template <T, U>
Instantiations: [T1, U1]
"""
self.original = original
self.instantiations = instantiations
self.template = ''
self.is_virtual = original.is_virtual
self.parent_class = original.parent_class
self.parent = original.parent
if not original.template:
self.name = original.name
self.ctors = list(original.ctors)
self.static_methods = list(original.static_methods)
class_instantiated_methods = list(original.methods)
self.properties = list(original.properties)
else:
# Check conditions.
assert len(original.template.typenames) == len(
instantiations), "Typenames and instantiations mismatch!"
self.name = instantiate_name(
original.name, instantiations) if not new_name else new_name
self.ctors = self.instantiate_ctors()
self.static_methods = self.instantiate_static_methods()
class_instantiated_methods = \
self.instantiate_class_templates_in_methods()
self.properties = self.instantiate_properties()
# Second instantiation round to instantiate template methods.
self.methods = []
for method in class_instantiated_methods:
if not method.template:
self.methods.append(InstantiatedMethod(method, ''))
else:
assert len(
method.template.typenames) == 1, ""\
"Can only instantiate single template methods"
for inst in method.template.instantiations[0]:
self.methods.append(InstantiatedMethod(method, inst))
super().__init__(
self.template,
self.is_virtual,
self.name,
[self.parent_class],
self.ctors,
self.methods,
self.static_methods,
self.properties,
parent=self.parent,
)
def __repr__(self):
return "{virtual} class {name} [{cpp_class}]: {parent_class}\n"\
"{ctors}\n{static_methods}\n{methods}".format(
virtual="virtual" if self.is_virtual else '',
name=self.name,
cpp_class=self.cpp_class(),
parent_class=self.parent,
ctors="\n".join([ctor.__repr__() for ctor in self.ctors]),
methods="\n".join([m.__repr__() for m in self.methods]),
static_methods="\n".join([m.__repr__()
for m in self.static_methods]),
)
def instantiate_ctors(self):
"""Instantiate the class constructors."""
instantiated_ctors = []
for ctor in self.original.ctors:
instantiated_args = instantiate_args_list(
ctor.args.args_list,
self.original.template.typenames,
self.instantiations,
self.cpp_typename(),
)
instantiated_ctors.append(parser.Constructor(
name=self.name,
args=parser.ArgumentList(instantiated_args),
parent=self,
))
return instantiated_ctors
def instantiate_static_methods(self):
"""Instantiate static methods in the class."""
instantiated_static_methods = []
for static_method in self.original.static_methods:
instantiated_args = instantiate_args_list(
static_method.args.args_list,
self.original.template.typenames,
self.instantiations,
self.cpp_typename()
)
instantiated_static_methods.append(
parser.StaticMethod(
name=static_method.name,
return_type=instantiate_return_type(
static_method.return_type,
self.original.template.typenames,
self.instantiations,
self.cpp_typename(),
instantiated_class=self
),
args=parser.ArgumentList(instantiated_args),
parent=self,
)
)
return instantiated_static_methods
def instantiate_class_templates_in_methods(self):
"""
This function only instantiates class templates in the methods.
Template methods are instantiated in InstantiatedMethod in the second
round.
"""
class_instantiated_methods = []
for method in self.original.methods:
instantiated_args = instantiate_args_list(
method.args.args_list,
self.original.template.typenames,
self.instantiations,
self.cpp_typename(),
)
class_instantiated_methods.append(parser.Method(
template=method.template,
name=method.name,
return_type=instantiate_return_type(
method.return_type,
self.original.template.typenames,
self.instantiations,
self.cpp_typename(),
),
args=parser.ArgumentList(instantiated_args),
is_const=method.is_const,
parent=self,
))
return class_instantiated_methods
def instantiate_properties(self):
"""Instantiate the class properties."""
instantiated_properties = instantiate_args_list(
self.original.properties,
self.original.template.typenames,
self.instantiations,
self.cpp_typename(),
)
return instantiated_properties
def cpp_class(self):
"""Generate the C++ code for wrapping."""
return self.cpp_typename().to_cpp()
def cpp_typename(self):
"""
Return a parser.Typename including namespaces and cpp name of this
class.
"""
if self.original.template:
name = "{}<{}>".format(
self.original.name,
", ".join([inst.to_cpp() for inst in self.instantiations]))
else:
name = self.original.name
namespaces_name = self.namespaces()
namespaces_name.append(name)
return parser.Typename(namespaces_name)
def instantiate_namespace_inplace(namespace):
"""
Instantiate the classes and other elements in the `namespace` content and
assign it back to the namespace content attribute.
@param[in/out] namespace The namespace whose content will be replaced with
the instantiated content.
"""
instantiated_content = []
typedef_content = []
for element in namespace.content:
if isinstance(element, parser.Class):
original_class = element
if not original_class.template:
instantiated_content.append(
InstantiatedClass(original_class, []))
else:
# Use itertools to get all possible combinations of instantiations
# Works even if one template does not have an instantiation list
for instantiations in itertools.product(
*original_class.template.instantiations):
instantiated_content.append(
InstantiatedClass(original_class,
list(instantiations)))
Squashed 'wrap/' changes from aae9b4605..3eff76f60 3eff76f60 Merge pull request #53 from borglab/feature/refactor 13215dfa7 Merge pull request #52 from borglab/fix/tests 696913b11 install setuptools 9523328ba Merge branch 'master' into fix/tests 7c630b361 some more cleanup 656993a71 cleaned up Typename a16f6f38e move qualified and basis type outside to their own class scope 72ead8bd7 Merge pull request #51 from borglab/fix/test-interface-parser 6deefd4fc added tests for interface_parser 50d490a85 Merge pull request #50 from borglab/feature/refs-all-types be4511290 updated docs for BasisType 0e80b0d8c update MATLAB tests 0015d7397 added support for shared pointer and ref for basis types 86d2158f1 remove std::string from list of Basis types 94f928441 ignore code coverage reports 2033dd345 replace prints with log.debug statements ae98091b3 fix deprecation in doc tests 13a2f66c4 Merge pull request #46 from borglab/feature/new-shared-pointer 3c7d85865 updated docs 6d7897088 use @ for raw pointer, go back to * for shared pointer 1d6194c57 updated matlab wrapper to handle both raw and shared pointers 1448f6924 fix some failing tests 2ab1dae32 Merge branch 'master' into feature/new-shared-pointer 96f8a56bd Merge pull request #47 from borglab/fix/ci 6003203f3 run CI only for pull requests a8f29ead1 fix the python version yml key fcae17227 check if directory exists when testing f592f08c9 explicit pip3 so that we don't use Python2 d49c2f3c2 correct call for pip dfe360526 fix the CI 149b7c523 docs for templated functions f2189acc6 support typedefing for templated functions 965458a2b added test for templated functions eaff6e6ab made is_const common for all types 3d9c70b32 added tests and cleaned up a bit 010b89626 support for simple pointers on basis types 6b98fd80c new syntax for shared_ptr ff7ad0b78 support for templated functions a1a443c8d Merge pull request #43 from borglab/fix/cmake-and-matlab 2f3a055e4 remove accidentally committed file 770d055e2 set proper paths for cmake and eschew relative paths 773d01ae1 fix bug in matlab wrapper 721ef740f Merge pull request #41 from borglab/feature/type-hints 67aac9758 minor refactor of CI yml e6a63ae0c fix all mypy issues a3aaa3e7c remove a lot of linter issues from matlab_wrapper a96db522f static typing for interface_parser git-subtree-dir: wrap git-subtree-split: 3eff76f604b5ba9e71cf4947654e288142ed7a94
2021-03-24 12:36:02 +08:00
elif isinstance(element, parser.GlobalFunction):
original_func = element
if not original_func.template:
instantiated_content.append(
InstantiatedGlobalFunction(original_func, []))
else:
# Use itertools to get all possible combinations of instantiations
# Works even if one template does not have an instantiation list
for instantiations in itertools.product(
*original_func.template.instantiations):
instantiated_content.append(
InstantiatedGlobalFunction(original_func,
list(instantiations)))
elif isinstance(element, parser.TypedefTemplateInstantiation):
typedef_inst = element
Squashed 'wrap/' changes from aae9b4605..3eff76f60 3eff76f60 Merge pull request #53 from borglab/feature/refactor 13215dfa7 Merge pull request #52 from borglab/fix/tests 696913b11 install setuptools 9523328ba Merge branch 'master' into fix/tests 7c630b361 some more cleanup 656993a71 cleaned up Typename a16f6f38e move qualified and basis type outside to their own class scope 72ead8bd7 Merge pull request #51 from borglab/fix/test-interface-parser 6deefd4fc added tests for interface_parser 50d490a85 Merge pull request #50 from borglab/feature/refs-all-types be4511290 updated docs for BasisType 0e80b0d8c update MATLAB tests 0015d7397 added support for shared pointer and ref for basis types 86d2158f1 remove std::string from list of Basis types 94f928441 ignore code coverage reports 2033dd345 replace prints with log.debug statements ae98091b3 fix deprecation in doc tests 13a2f66c4 Merge pull request #46 from borglab/feature/new-shared-pointer 3c7d85865 updated docs 6d7897088 use @ for raw pointer, go back to * for shared pointer 1d6194c57 updated matlab wrapper to handle both raw and shared pointers 1448f6924 fix some failing tests 2ab1dae32 Merge branch 'master' into feature/new-shared-pointer 96f8a56bd Merge pull request #47 from borglab/fix/ci 6003203f3 run CI only for pull requests a8f29ead1 fix the python version yml key fcae17227 check if directory exists when testing f592f08c9 explicit pip3 so that we don't use Python2 d49c2f3c2 correct call for pip dfe360526 fix the CI 149b7c523 docs for templated functions f2189acc6 support typedefing for templated functions 965458a2b added test for templated functions eaff6e6ab made is_const common for all types 3d9c70b32 added tests and cleaned up a bit 010b89626 support for simple pointers on basis types 6b98fd80c new syntax for shared_ptr ff7ad0b78 support for templated functions a1a443c8d Merge pull request #43 from borglab/fix/cmake-and-matlab 2f3a055e4 remove accidentally committed file 770d055e2 set proper paths for cmake and eschew relative paths 773d01ae1 fix bug in matlab wrapper 721ef740f Merge pull request #41 from borglab/feature/type-hints 67aac9758 minor refactor of CI yml e6a63ae0c fix all mypy issues a3aaa3e7c remove a lot of linter issues from matlab_wrapper a96db522f static typing for interface_parser git-subtree-dir: wrap git-subtree-split: 3eff76f604b5ba9e71cf4947654e288142ed7a94
2021-03-24 12:36:02 +08:00
top_level = namespace.top_level()
original_element = top_level.find_class_or_function(
typedef_inst.typename)
Squashed 'wrap/' changes from aae9b4605..3eff76f60 3eff76f60 Merge pull request #53 from borglab/feature/refactor 13215dfa7 Merge pull request #52 from borglab/fix/tests 696913b11 install setuptools 9523328ba Merge branch 'master' into fix/tests 7c630b361 some more cleanup 656993a71 cleaned up Typename a16f6f38e move qualified and basis type outside to their own class scope 72ead8bd7 Merge pull request #51 from borglab/fix/test-interface-parser 6deefd4fc added tests for interface_parser 50d490a85 Merge pull request #50 from borglab/feature/refs-all-types be4511290 updated docs for BasisType 0e80b0d8c update MATLAB tests 0015d7397 added support for shared pointer and ref for basis types 86d2158f1 remove std::string from list of Basis types 94f928441 ignore code coverage reports 2033dd345 replace prints with log.debug statements ae98091b3 fix deprecation in doc tests 13a2f66c4 Merge pull request #46 from borglab/feature/new-shared-pointer 3c7d85865 updated docs 6d7897088 use @ for raw pointer, go back to * for shared pointer 1d6194c57 updated matlab wrapper to handle both raw and shared pointers 1448f6924 fix some failing tests 2ab1dae32 Merge branch 'master' into feature/new-shared-pointer 96f8a56bd Merge pull request #47 from borglab/fix/ci 6003203f3 run CI only for pull requests a8f29ead1 fix the python version yml key fcae17227 check if directory exists when testing f592f08c9 explicit pip3 so that we don't use Python2 d49c2f3c2 correct call for pip dfe360526 fix the CI 149b7c523 docs for templated functions f2189acc6 support typedefing for templated functions 965458a2b added test for templated functions eaff6e6ab made is_const common for all types 3d9c70b32 added tests and cleaned up a bit 010b89626 support for simple pointers on basis types 6b98fd80c new syntax for shared_ptr ff7ad0b78 support for templated functions a1a443c8d Merge pull request #43 from borglab/fix/cmake-and-matlab 2f3a055e4 remove accidentally committed file 770d055e2 set proper paths for cmake and eschew relative paths 773d01ae1 fix bug in matlab wrapper 721ef740f Merge pull request #41 from borglab/feature/type-hints 67aac9758 minor refactor of CI yml e6a63ae0c fix all mypy issues a3aaa3e7c remove a lot of linter issues from matlab_wrapper a96db522f static typing for interface_parser git-subtree-dir: wrap git-subtree-split: 3eff76f604b5ba9e71cf4947654e288142ed7a94
2021-03-24 12:36:02 +08:00
# Check if element is a typedef'd class or function.
if isinstance(original_element, parser.Class):
typedef_content.append(
InstantiatedClass(original_element,
typedef_inst.typename.instantiations,
typedef_inst.new_name))
elif isinstance(original_element, parser.GlobalFunction):
typedef_content.append(
InstantiatedGlobalFunction(
original_element, typedef_inst.typename.instantiations,
typedef_inst.new_name))
elif isinstance(element, parser.Namespace):
instantiate_namespace_inplace(element)
instantiated_content.append(element)
else:
instantiated_content.append(element)
instantiated_content.extend(typedef_content)
namespace.content = instantiated_content