Ejemplo n.º 1
0
 def transform_template_defn(self, template_defn: ir.TemplateDefn):
     self.writer.write(ir.TemplateDefn(args=template_defn.args,
                                       main_definition=self._transform_template_specialization(template_defn.main_definition, template_defn.result_element_names) if template_defn.main_definition is not None else None,
                                       specializations=[self._transform_template_specialization(specialization, template_defn.result_element_names) for specialization in template_defn.specializations],
                                       name=template_defn.name,
                                       description=template_defn.description,
                                       result_element_names=template_defn.result_element_names))
Ejemplo n.º 2
0
    def transform_template_defn(self, template_defn: ir0.TemplateDefn):
        if template_defn.name not in self.movable_arg_indexes_by_template_name:
            super().transform_template_defn(template_defn)
            return

        assert not self.movable_arg_indexes_in_current_template
        assert not self.additional_typedef_args_in_current_template
        self.movable_arg_indexes_in_current_template = self.movable_arg_indexes_by_template_name[template_defn.name]
        self.additional_typedef_args_in_current_template = tuple(arg_decl
                                                                 for index, arg_decl in enumerate(template_defn.args)
                                                                 if index in self.movable_arg_indexes_in_current_template)
        args = tuple(arg_decl
                     for index, arg_decl in enumerate(template_defn.args)
                     if index not in self.movable_arg_indexes_in_current_template)


        template_specialization = self.transform_template_specialization(template_defn.main_definition) if template_defn.main_definition is not None else None
        specializations = tuple(self.transform_template_specialization(specialization)
                                for specialization in template_defn.specializations)

        self.writer.write(ir.TemplateDefn(args=args,
                                          main_definition=template_specialization,
                                          specializations=specializations,
                                          name=template_defn.name,
                                          description=template_defn.description,
                                          result_element_names=template_defn.result_element_names))

        self.movable_arg_indexes_in_current_template = set()
        self.additional_typedef_args_in_current_template = ()
        self.locals_to_instantiate = set()
Ejemplo n.º 3
0
 def transform_template_defn(self, template_defn: ir.TemplateDefn):
     self.writer.write(
         ir.TemplateDefn(
             args=tuple(
                 self.transform_template_arg_decl(arg_decl)
                 for arg_decl in template_defn.args),
             main_definition=self.transform_template_specialization(
                 template_defn.main_definition)
             if template_defn.main_definition is not None else None,
             specializations=tuple(
                 self.transform_template_specialization(specialization)
                 for specialization in template_defn.specializations),
             name=self._transform_name(template_defn.name),
             description=template_defn.description,
             result_element_names=template_defn.result_element_names))
Ejemplo n.º 4
0
 def transform_template_defn(self, template_defn: ir.TemplateDefn):
     args = [
         self.transform_template_arg_decl(arg_decl)
         for arg_decl in template_defn.args
     ]
     template_specialization = self.transform_template_specialization(
         template_defn.main_definition
     ) if template_defn.main_definition is not None else None
     specializations = [
         self.transform_template_specialization(specialization)
         for specialization in template_defn.specializations
     ]
     self.writer.write(
         ir.TemplateDefn(
             args=args,
             main_definition=template_specialization,
             specializations=specializations,
             name=template_defn.name,
             description=template_defn.description,
             result_element_names=template_defn.result_element_names))
Ejemplo n.º 5
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
 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',
                                                expr_type=ir.TypeType(),
                                                is_variadic=False)
             ],
             body=[ir.ConstantDef(name='value', expr=ir.Literal(True))],
             is_metafunction=True)
     ]),