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_static_methods(self, typenames): """ Instantiate static methods in the class. Args: typenames: List of template types to instantiate. Return: List of static methods instantiated with provided template args. """ instantiated_static_methods = [] for static_method in self.original.static_methods: instantiated_args = instantiate_args_list( static_method.args.list(), 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, typenames, self.instantiations, self.cpp_typename(), instantiated_class=self), args=parser.ArgumentList(instantiated_args), parent=self, )) return instantiated_static_methods
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: 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)
def instantiate_operators(self, typenames): """ Instantiate the class-level template in the operator overload. Args: typenames: List of template types to instantiate. Return: List of methods instantiated with provided template args on the class. """ instantiated_operators = [] for operator in self.original.operators: instantiated_args = instantiate_args_list( operator.args.list(), typenames, self.instantiations, self.cpp_typename(), ) instantiated_operators.append( parser.Operator( name=operator.name, operator=operator.operator, return_type=instantiate_return_type( operator.return_type, typenames, self.instantiations, self.cpp_typename(), ), args=parser.ArgumentList(instantiated_args), is_const=operator.is_const, parent=self, )) return instantiated_operators
def instantiate_ctors(self, typenames): """ Instantiate the class constructors. Args: typenames: List of template types to instantiate. Return: List of constructors instantiated with provided template args. """ instantiated_ctors = [] for ctor in self.original.ctors: instantiated_args = instantiate_args_list( ctor.args.list(), 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 construct(cls, original: parser.Constructor, typenames: List[str], class_instantiations: List[parser.Typename], method_instantiations: List[parser.Typename], instantiated_args: List[parser.Argument], parent: 'InstantiatedClass'): """Class method to construct object as required by InstantiationHelper.""" method = parser.Constructor( name=parent.name, args=parser.ArgumentList(instantiated_args), template=original.template, parent=parent, ) return InstantiatedConstructor(method, instantiations=method_instantiations)
def instantiate_ctors(self): 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 construct(cls, original, typenames, class_instantiations, method_instantiations, instantiated_args, parent): """Class method to construct object as required by InstantiationHelper.""" method = parser.Method( template=original.template, name=original.name, return_type=instantiate_return_type( original.return_type, typenames, class_instantiations + method_instantiations, parent.cpp_typename()), args=parser.ArgumentList(instantiated_args), is_const=original.is_const, parent=parent, ) return InstantiatedMethod(method, instantiations=method_instantiations)
def instantiate_class_templates_in_methods(self, typenames): """ This function only instantiates the class-level templates in the methods. Template methods are instantiated in InstantiatedMethod in the second round. E.g. ``` template<T={string}> class Greeter{ void sayHello(T& name); }; Args: typenames: List of template types to instantiate. Return: List of methods instantiated with provided template args on the class. """ class_instantiated_methods = [] for method in self.original.methods: instantiated_args = instantiate_args_list( method.args.list(), 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, typenames, self.instantiations, self.cpp_typename(), ), args=parser.ArgumentList(instantiated_args), is_const=method.is_const, parent=self, )) return class_instantiated_methods
def __init__(self, original, instantiations: Iterable[parser.Typename] = ()): self.original = original self.instantiations = instantiations self.template = '' self.is_const = original.is_const self.parent = original.parent # Check for typenames if templated. # This way, we can gracefully handle both templated and non-templated methods. typenames = self.original.template.typenames if self.original.template else [] self.name = instantiate_name(original.name, self.instantiations) self.return_type = instantiate_return_type( original.return_type, 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.list(), 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.template, self.name, self.return_type, self.args, self.is_const, parent=self.parent)
def instantiate_static_methods(self): 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 __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.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)