Exemple #1
0
 def transform_typedef(self, typedef: ir.Typedef):
     expr = self.transform_expr(typedef.expr)
     if self.additional_typedef_args_in_current_template:
         assert not typedef.template_args
         self.locals_to_instantiate.add(typedef.name)
         self.writer.write(ir.Typedef(name=typedef.name,
                                      expr=expr,
                                      description=typedef.description,
                                      template_args=self.additional_typedef_args_in_current_template))
     else:
         self.writer.write(ir.Typedef(name=typedef.name,
                                      expr=expr,
                                      description=typedef.description,
                                      template_args=typedef.template_args))
Exemple #2
0
 def transform_typedef(self, typedef: ir.Typedef):
     expr = self.transform_expr(typedef.expr)
     self.writer.write(
         ir.Typedef(name=typedef.name,
                    expr=expr,
                    description=typedef.description,
                    template_args=tuple(
                        self.transform_template_arg_decl(arg_decl)
                        for arg_decl in typedef.template_args)))
Exemple #3
0
def _create_var_to_var_assignment(lhs: str, rhs: str, expr_type: ir.ExprType):
    if expr_type.kind in (ir.ExprKind.BOOL, ir.ExprKind.INT64):
        return ir.ConstantDef(name=lhs,
                              expr=ir.AtomicTypeLiteral.for_local(cpp_type=rhs,
                                                                  expr_type=expr_type,
                                                                  is_variadic=False))
    elif expr_type.kind in (ir.ExprKind.TYPE, ir.ExprKind.TEMPLATE):
        return ir.Typedef(name=lhs,
                          expr=ir.AtomicTypeLiteral.for_local(cpp_type=rhs,
                                                              expr_type=expr_type,
                                                              is_variadic=False))
    else:
        raise NotImplementedError('Unexpected kind: %s' % str(expr_type.kind))
Exemple #4
0
    def new_constant_or_typedef(
            self, expr: ir.Expr,
            identifier_generator: Iterator[str]) -> ir.AtomicTypeLiteral:
        id = next(identifier_generator)
        if expr.expr_type.kind in (ir.ExprKind.BOOL, ir.ExprKind.INT64):
            self.write(ir.ConstantDef(name=id, expr=expr))
        elif expr.expr_type.kind in (ir.ExprKind.TYPE, ir.ExprKind.TEMPLATE):
            self.write(ir.Typedef(name=id, expr=expr))
        else:
            raise NotImplementedError('Unexpected kind: ' +
                                      str(expr.expr_type.kind))

        return ir.AtomicTypeLiteral.for_local(cpp_type=id,
                                              expr_type=expr.expr_type,
                                              is_variadic=False)
Exemple #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
Exemple #6
0
 def transform_typedef(self, typedef: ir.Typedef):
     self.writer.write(ir.Typedef(name=typedef.name,
                                  expr=self.transform_expr(typedef.expr, split_nontrivial_exprs=False),
                                  description=typedef.description,
                                  template_args=typedef.template_args))
Exemple #7
0
 def transform_typedef(self, typedef: ir.Typedef):
     self.writer.write(
         ir.Typedef(name=self._transform_name(typedef.name),
                    expr=self.transform_expr(typedef.expr),
                    description=typedef.description,
                    template_args=typedef.template_args))
             body=[ir.ConstantDef(name='value', expr=ir.Literal(True))],
             is_metafunction=True)
     ]),
 ir.TemplateDefn(
     name='std::add_pointer',
     description='',
     result_element_names=['type'],
     main_definition=ir.TemplateSpecialization(
         args=[
             ir.TemplateArgDecl(
                 name='T', expr_type=ir.TypeType(), is_variadic=False)
         ],
         patterns=None,
         body=[
             ir.Typedef(name='type',
                        expr=ir.PointerTypeExpr(
                            ir.AtomicTypeLiteral.for_local(
                                'T', ir.TypeType(), is_variadic=False)))
         ],
         is_metafunction=True),
     specializations=[]),
 ir.TemplateDefn(
     name='std::remove_pointer',
     description='',
     result_element_names=['type'],
     args=[
         ir.TemplateArgDecl(
             name='T', expr_type=ir.TypeType(), is_variadic=False)
     ],
     main_definition=ir.TemplateSpecialization(
         args=[
             ir.TemplateArgDecl(