Beispiel #1
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))
Beispiel #2
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)
Beispiel #3
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
Beispiel #4
0
 def transform_constant_def(self, constant_def: ir.ConstantDef):
     self.writer.write(ir.ConstantDef(name=constant_def.name,
                                      expr=self.transform_expr(constant_def.expr, split_nontrivial_exprs=False)))
Beispiel #5
0
 def transform_constant_def(self, constant_def: ir.ConstantDef):
     expr = self.transform_expr(constant_def.expr)
     self.writer.write(ir.ConstantDef(name=constant_def.name, expr=expr))
Beispiel #6
0
 def transform_constant_def(self, constant_def: ir.ConstantDef):
     self.writer.write(
         ir.ConstantDef(name=self._transform_name(constant_def.name),
                        expr=self.transform_expr(constant_def.expr)))
 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)
         ],