def template(cpp_type: str, num_args: int): return ir0.AtomicTypeLiteral.for_nonlocal_template( cpp_type, args=[ir0.TemplateArgType(ir0.TypeType(), is_variadic=False)] * num_args, is_metafunction_that_may_return_error=False, may_be_alias=False)
def template_member_access(class_expr: ir0.Expr, member_name: str, num_args: int): return ir0.ClassMemberAccess( class_type_expr=class_expr, member_name=member_name, member_type=ir0.TemplateType( args=[ir0.TemplateArgType(ir0.TypeType(), is_variadic=False)] * num_args))
def transform_type_literal(self, type_literal: ir.AtomicTypeLiteral): if self.additional_typedef_args_in_current_template and type_literal.cpp_type in self.locals_to_instantiate: assert isinstance(type_literal.expr_type, ir0.TypeType) # X5 -> X5<T, U>, if X5 is defined by a local typedef and we're moving {X,U} to be typedef template args # instead of args of the template defn. return ir0.TemplateInstantiation( template_expr=ir.AtomicTypeLiteral.for_local( cpp_type=type_literal.cpp_type, expr_type=ir0.TemplateType([ ir0.TemplateArgType(expr_type=arg.expr_type, is_variadic=arg.is_variadic) for arg in self.additional_typedef_args_in_current_template ]), is_variadic=False), args=[ ir0.AtomicTypeLiteral.for_local( cpp_type=arg.name, expr_type=arg.expr_type, is_variadic=arg.is_variadic) for arg in self.additional_typedef_args_in_current_template ], instantiation_might_trigger_static_asserts=False) else: return type_literal
lambda: ir0.Int64BinaryOpExpr(literal(1), literal(2), op='+'), lambda: ir0.BoolBinaryOpExpr(literal(True), literal(False), op='||'), lambda: ir0.NotExpr(literal(True)), lambda: ir0.UnaryMinusExpr(literal(1)), lambda: ir0.TemplateInstantiation( template_expr=ir0.AtomicTypeLiteral.for_nonlocal_template( cpp_type='std::vector', args=[], is_metafunction_that_may_return_error=False, may_be_alias=False), args=[], instantiation_might_trigger_static_asserts=False), lambda: ir0.TemplateInstantiation( template_expr=ir0.AtomicTypeLiteral.for_nonlocal_template( cpp_type='std::vector', args=[ir0.TemplateArgType(ir0.TypeType(), is_variadic=False)], is_metafunction_that_may_return_error=False, may_be_alias=False), args=[type_literal('int')], instantiation_might_trigger_static_asserts=False), lambda: ir0.ClassMemberAccess(class_type_expr=type_literal('MyClass'), member_name='value_type', member_type=ir0.TypeType()), ], ids=[ 'Literal', 'AtomicTypeLiteral', 'PointerTypeExpr', 'ReferenceTypeExpr', 'RvalueReferenceTypeExpr', 'ConstTypeExpr',
def _template_template_arg_type(*args: ir0.TemplateArgType): return ir0.TemplateArgType(expr_type=ir0.TemplateType(args), is_variadic=False)
def _variadic_bool_arg_type(): return ir0.TemplateArgType(expr_type=ir0.BoolType(), is_variadic=True)
def _variadic_int64_arg_type(): return ir0.TemplateArgType(expr_type=ir0.Int64Type(), is_variadic=True)
def _variadic_type_arg_type(): return ir0.TemplateArgType(expr_type=ir0.TypeType(), is_variadic=True)
def template_instantiation_to_cpp(instantiation_expr: ir0.TemplateInstantiation, enclosing_function_defn_args: List[ir0.TemplateArgDecl], writer: Writer, omit_typename=False): args = instantiation_expr.args if instantiation_expr.instantiation_might_trigger_static_asserts and enclosing_function_defn_args and args: bound_variables = {arg_decl.name for arg_decl in enclosing_function_defn_args} assert bound_variables # TODO: We could avoid adding a param dependency in more cases by checking for references to local variables # that depend (directly or indirectly) on a param. if not any(arg.references_any_of(bound_variables) for arg in args): # All template arguments are (or might be) constants, we need to add a reference to a variable bound in this # function to prevent the instantiation from happening early, potentially triggering static asserts. arg_decl = _select_best_arg_decl_for_select1st(enclosing_function_defn_args) arg_index = _select_best_arg_expr_index_for_select1st(args) arg_to_replace = args[arg_index] is_variadic = is_expr_variadic(arg_to_replace) if arg_decl.expr_type.kind != ir0.ExprKind.TEMPLATE and arg_to_replace.expr_type.kind != ir0.ExprKind.TEMPLATE: # We use lambdas here just to make sure we collect code coverage of each "branch". They are not necessary. # Note that we use the *Type variants for variadic types too. That's ok, since e.g. # Select1stBoolType<b, Args> will be expanded as e.g. Select1stBoolType<b, Args>... so it's exactly what # we want in the variadic case too. select1st_variant = { (ir0.ExprKind.BOOL, ir0.ExprKind.BOOL): lambda: 'Select1stBoolBool', (ir0.ExprKind.BOOL, ir0.ExprKind.INT64): lambda: 'Select1stBoolInt64', (ir0.ExprKind.BOOL, ir0.ExprKind.TYPE): lambda: 'Select1stBoolType', (ir0.ExprKind.INT64, ir0.ExprKind.BOOL): lambda: 'Select1stInt64Bool', (ir0.ExprKind.INT64, ir0.ExprKind.INT64): lambda: 'Select1stInt64Int64', (ir0.ExprKind.INT64, ir0.ExprKind.TYPE): lambda: 'Select1stInt64Type', (ir0.ExprKind.TYPE, ir0.ExprKind.BOOL): lambda: 'Select1stTypeBool', (ir0.ExprKind.TYPE, ir0.ExprKind.INT64): lambda: 'Select1stTypeInt64', (ir0.ExprKind.TYPE, ir0.ExprKind.TYPE): lambda: 'Select1stTypeType', }[(arg_to_replace.expr_type.kind, arg_decl.expr_type.kind)]() else: # We need to define a new Select1st variant for the desired function type. select1st_variant = writer.new_id() forwarded_param_id = writer.new_id() template_param_decl1 = _type_to_template_param_declaration(expr_type=arg_to_replace.expr_type, is_variadic=is_variadic) template_param_decl2 = _type_to_template_param_declaration(expr_type=arg_decl.expr_type, is_variadic=arg_decl.is_variadic) select1st_variant_body_writer = TemplateElemWriter(writer.get_toplevel_writer()) if arg_to_replace.expr_type.kind in (ir0.ExprKind.BOOL, ir0.ExprKind.INT64): select1st_variant_body = ir0.ConstantDef(name='value', expr=ir0.AtomicTypeLiteral.for_local(cpp_type=forwarded_param_id, expr_type=arg_to_replace.expr_type, is_variadic=is_variadic)) constant_def_to_cpp(select1st_variant_body, enclosing_function_defn_args, select1st_variant_body_writer) else: replaced_type = arg_to_replace.expr_type assert replaced_type.kind in (ir0.ExprKind.TYPE, ir0.ExprKind.TEMPLATE) select1st_variant_body = ir0.Typedef(name='value', expr=ir0.AtomicTypeLiteral.for_local(cpp_type=forwarded_param_id, expr_type=replaced_type, is_variadic=is_variadic)) typedef_to_cpp(select1st_variant_body, enclosing_function_defn_args, select1st_variant_body_writer) select1st_variant_body_str = ''.join(select1st_variant_body_writer.strings) writer.write_template_body_elem(''' // Custom Select1st* template template <{template_param_decl1} {forwarded_param_id}, {template_param_decl2}> struct {select1st_variant} {{ {select1st_variant_body_str} }}; '''.format(**locals())) select1st_type = ir0.TemplateType(args=[ ir0.TemplateArgType(expr_type=arg_to_replace.expr_type, is_variadic=is_variadic), arg_decl]) select1st_instantiation = ir0.TemplateInstantiation(template_expr=ir0.AtomicTypeLiteral.for_local(cpp_type=select1st_variant, expr_type=select1st_type, is_variadic=False), args=[arg_to_replace, ir0.AtomicTypeLiteral.for_local(cpp_type=arg_decl.name, expr_type=arg_decl.expr_type, is_variadic=arg_decl.is_variadic)], instantiation_might_trigger_static_asserts=False) new_arg = ir0.ClassMemberAccess(class_type_expr=select1st_instantiation, member_name='value', member_type=arg_to_replace.expr_type) args = args[:arg_index] + (new_arg,) + args[arg_index + 1:] template_params = ', '.join(expr_to_cpp(arg, enclosing_function_defn_args, writer) for arg in args) if isinstance(instantiation_expr.template_expr, ir0.ClassMemberAccess): cpp_fun = class_member_access_to_cpp(instantiation_expr.template_expr, enclosing_function_defn_args, writer, omit_typename=omit_typename, parent_expr_is_template_instantiation=True) else: cpp_fun = expr_to_cpp(instantiation_expr.template_expr, enclosing_function_defn_args, writer) return '{cpp_fun}<{template_params}>'.format(**locals())
lambda: ir0.Int64BinaryOpExpr(literal(1), literal(2), op='+'), lambda: ir0.BoolBinaryOpExpr(literal(True), literal(False), op='||'), lambda: ir0.NotExpr(literal(True)), lambda: ir0.UnaryMinusExpr(literal(1)), lambda: ir0.TemplateInstantiation( template_expr=ir0.AtomicTypeLiteral.for_nonlocal_template( cpp_type='std::vector', args=(), is_metafunction_that_may_return_error=False, may_be_alias=False), args=(), instantiation_might_trigger_static_asserts=False), lambda: ir0.TemplateInstantiation( template_expr=ir0.AtomicTypeLiteral.for_nonlocal_template( cpp_type='std::vector', args=(ir0.TemplateArgType(ir0.TypeType(), is_variadic=False), ), is_metafunction_that_may_return_error=False, may_be_alias=False), args=(type_literal('int'), ), instantiation_might_trigger_static_asserts=False), lambda: ir0.ClassMemberAccess(inner_expr=type_literal('MyClass'), member_name='value_type', expr_type=ir0.TypeType()), ], ids=[ 'Literal', 'AtomicTypeLiteral', 'PointerTypeExpr', 'ReferenceTypeExpr', 'RvalueReferenceTypeExpr', 'ConstTypeExpr',
def _int64_arg_type() -> ir0.TemplateArgType: return ir0.TemplateArgType(expr_type=ir0.Int64Type(), is_variadic=False)
def _bool_arg_type() -> ir0.TemplateArgType: return ir0.TemplateArgType(expr_type=ir0.BoolType(), is_variadic=False)