def select1st_literal(lhs_type: ir.ExprType, rhs_type: ir.ExprType): kind_to_string = { ir.ExprKind.BOOL: 'Bool', ir.ExprKind.INT64: 'Int64', ir.ExprKind.TYPE: 'Type', } return ir.AtomicTypeLiteral.for_nonlocal_template( cpp_type='Select1st%s%s' % (kind_to_string[lhs_type.kind], kind_to_string[rhs_type.kind]), args=(ir.TemplateArgType(expr_type=lhs_type, is_variadic=False), ir.TemplateArgType(expr_type=rhs_type, is_variadic=False)), is_metafunction_that_may_return_error=False, may_be_alias=False)
def _template_template_arg_type(*args: ir.TemplateArgType): return ir.TemplateArgType(expr_type=ir.TemplateType(args), is_variadic=False)
def _variadic_int64_arg_type(): return ir.TemplateArgType(expr_type=ir.Int64Type(), is_variadic=True)
def _variadic_bool_arg_type(): return ir.TemplateArgType(expr_type=ir.BoolType(), is_variadic=True)
def _variadic_type_arg_type(): return ir.TemplateArgType(expr_type=ir.TypeType(), is_variadic=True)
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