Exemple #1
0
 def _transform_template_specialization(self,
                                        specialization: ir.TemplateSpecialization,
                                        result_element_names: Sequence[str]) -> ir.TemplateSpecialization:
     return ir.TemplateSpecialization(args=specialization.args,
                                      patterns=specialization.patterns,
                                      body=self.transform_template_body_elems(specialization.body,
                                                                               result_element_names),
                                      is_metafunction=specialization.is_metafunction)
Exemple #2
0
    def transform_template_specialization(self, specialization: ir.TemplateSpecialization):
        if specialization.patterns is not None:
            patterns = [pattern
                        for index, pattern in enumerate(specialization.patterns)
                        if not index in self.movable_arg_indexes_in_current_template]
        else:
            patterns = None

        args = tuple(self.transform_template_arg_decl(arg_decl)
                     for index, arg_decl in enumerate(specialization.args)
                     if not index in self.movable_arg_indexes_in_current_template)

        body = self.transform_template_body_elems(specialization.body)
        return ir.TemplateSpecialization(args=args,
                                         patterns=patterns,
                                         body=body,
                                         is_metafunction=specialization.is_metafunction)
Exemple #3
0
    def transform_template_specialization(
        self, specialization: ir.TemplateSpecialization
    ) -> ir.TemplateSpecialization:
        if specialization.patterns is not None:
            patterns = tuple(
                self.transform_pattern(pattern)
                for pattern in specialization.patterns)
        else:
            patterns = None

        args = tuple(
            self.transform_template_arg_decl(arg_decl)
            for arg_decl in specialization.args)
        body = self.transform_template_body_elems(specialization.body)
        return ir.TemplateSpecialization(
            args=args,
            patterns=patterns,
            body=body,
            is_metafunction=specialization.is_metafunction)
Exemple #4
0
def split_template_defn_with_multiple_outputs(
    template_defn: ir.TemplateDefn,
    split_template_name_by_old_name_and_result_element_name: MutableMapping[
        Tuple[str, str], str], identifier_generator: Iterator[str]
) -> Tuple[Tuple[ir.TemplateDefn, ...], bool]:
    type_by_result_elem_name = {
        elem.name: elem.expr.expr_type
        for specialization in template_defn.all_definitions
        for elem in specialization.body
        if isinstance(elem, (ir.ConstantDef, ir.Typedef))
        and elem.name in template_defn.result_element_names
    }
    actual_result_elem_names = tuple(sorted(type_by_result_elem_name.keys()))
    if len(type_by_result_elem_name) <= 1 or any(
            not specialization.is_metafunction
            for specialization in template_defn.all_definitions):
        return (template_defn, ), False

    new_template_defns = []
    if template_defn.main_definition:
        args = template_defn.main_definition.args
    else:
        args = template_defn.args
    arg_decls = tuple(
        ir.TemplateArgType(expr_type=arg.expr_type,
                           is_variadic=arg.is_variadic) for arg in args)
    template_defn_by_result_elem_name = {
        result_elem: ir.TemplateDefn(
            main_definition=template_defn.main_definition,
            specializations=template_defn.specializations,
            name=split_template_name_by_old_name_and_result_element_name.
            setdefault((template_defn.name, result_elem),
                       next(identifier_generator)),
            description='Split that generates %s of: %s' %
            (result_elem, template_defn.description or template_defn.name),
            result_element_names=frozenset((result_elem, )),
            args=args)
        for result_elem in actual_result_elem_names
    }
    for new_template_defn in template_defn_by_result_elem_name.values():
        new_template_defns.append(new_template_defn)

    dispatcher_body = []
    for result_elem_name in actual_result_elem_names:
        expr_type = type_by_result_elem_name[result_elem_name]

        split_template_literal = ir.AtomicTypeLiteral.for_nonlocal_template(
            cpp_type=template_defn_by_result_elem_name[result_elem_name].name,
            args=arg_decls,
            is_metafunction_that_may_return_error=False,
            may_be_alias=False)
        inner_expr = ir.ClassMemberAccess(inner_expr=ir.TemplateInstantiation(
            template_expr=split_template_literal,
            args=tuple(
                ir.AtomicTypeLiteral.for_local(cpp_type=arg.name,
                                               expr_type=arg.expr_type,
                                               is_variadic=arg.is_variadic)
                for arg in args),
            instantiation_might_trigger_static_asserts=True),
                                          member_name=result_elem_name,
                                          expr_type=expr_type)
        if expr_type.kind in (ir.ExprKind.TYPE, ir.ExprKind.TEMPLATE):
            dispatcher_body.append(
                ir.Typedef(name=result_elem_name, expr=inner_expr))
        else:
            dispatcher_body.append(
                ir.ConstantDef(name=result_elem_name, expr=inner_expr))

    new_template_defns.append(
        ir.TemplateDefn(
            main_definition=ir.TemplateSpecialization(
                args=args,
                patterns=None,
                body=tuple(dispatcher_body),
                is_metafunction=True),
            specializations=(),
            name=template_defn.name,
            description=template_defn.description,
            result_element_names=frozenset(actual_result_elem_names),
            args=args))
    return tuple(new_template_defns), False
TEMPLATE_DEFNS_DEFINED_AS_IR0 = [
    ir.TemplateDefn(
        name='std::is_same',
        description='',
        result_element_names=['value'],
        args=[
            ir.TemplateArgDecl(
                name='T', expr_type=ir.TypeType(), is_variadic=False),
            ir.TemplateArgDecl(
                name='U', expr_type=ir.TypeType(), is_variadic=False)
        ],
        main_definition=ir.TemplateSpecialization(
            args=[
                ir.TemplateArgDecl(
                    name='T', expr_type=ir.TypeType(), is_variadic=False),
                ir.TemplateArgDecl(
                    name='U', expr_type=ir.TypeType(), is_variadic=False)
            ],
            patterns=None,
            body=[ir.ConstantDef(name='value', expr=ir.Literal(False))],
            is_metafunction=True),
        specializations=[
            ir.TemplateSpecialization(
                args=[
                    ir.TemplateArgDecl(
                        name='T', expr_type=ir.TypeType(), is_variadic=False)
                ],
                patterns=[
                    ir.AtomicTypeLiteral.for_local(cpp_type='T',
                                                   expr_type=ir.TypeType(),
                                                   is_variadic=False),
                    ir.AtomicTypeLiteral.for_local(cpp_type='T',