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_class_member_access( self, class_member_access: ir0.ClassMemberAccess): if (isinstance(class_member_access.expr, ir0.TemplateInstantiation) and isinstance(class_member_access.expr.template_expr, ir0.AtomicTypeLiteral) and class_member_access.expr.template_expr.cpp_type in self.movable_arg_indexes_by_template_name): assert isinstance(class_member_access.expr.template_expr.expr_type, ir0.TemplateType) # F<X, Y>::type -> F<X>::type<Y> (if X is used in non-trivial patterns and Y isn't) args = self.transform_exprs(class_member_access.expr.args, class_member_access.expr) movable_arg_indexes = self.movable_arg_indexes_by_template_name[ class_member_access.expr.template_expr.cpp_type] template_instantiation_arg_exprs = [ arg for index, arg in enumerate(args) if index not in movable_arg_indexes ] typedef_instantiation_arg_exprs = [ arg for index, arg in enumerate(args) if index in movable_arg_indexes ] typedef_instantiation_arg_types = [ arg_type for index, arg_type in enumerate( class_member_access.expr.template_expr.expr_type.args) if index in movable_arg_indexes ] template_instantiation = ir0.TemplateInstantiation( template_expr=ir0.AtomicTypeLiteral.for_nonlocal_template( cpp_type=class_member_access.expr.template_expr.cpp_type, args=[ arg for index, arg in enumerate(class_member_access.expr.template_expr. expr_type.args) if index not in movable_arg_indexes ], is_metafunction_that_may_return_error=class_member_access. expr.template_expr.is_metafunction_that_may_return_error, may_be_alias=True), args=template_instantiation_arg_exprs, instantiation_might_trigger_static_asserts=class_member_access. expr.instantiation_might_trigger_static_asserts) new_class_member_access = ir0.ClassMemberAccess( class_type_expr=template_instantiation, member_name=class_member_access.member_name, member_type=ir0.TemplateType(typedef_instantiation_arg_types)) return ir0.TemplateInstantiation( template_expr=new_class_member_access, args=typedef_instantiation_arg_exprs, instantiation_might_trigger_static_asserts=class_member_access. expr.instantiation_might_trigger_static_asserts) else: return super().transform_class_member_access(class_member_access)
def _metafunction_call(template_expr: ir0.Expr, args: Sequence[ir0.Expr], instantiation_might_trigger_static_asserts: bool, member_name: str, member_type: ir0.ExprType): return ir0.ClassMemberAccess(class_type_expr=ir0.TemplateInstantiation( template_expr=template_expr, args=args, instantiation_might_trigger_static_asserts= instantiation_might_trigger_static_asserts), member_name=member_name, member_type=member_type)
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', 'ArrayTypeExpr', 'FunctionTypeExpr (no args)', 'FunctionTypeExpr (1 arg)', 'ComparisonExpr', 'Int64BinaryOpExpr', 'BoolBinaryOpExpr',
def bool_member_access(class_expr: ir0.Expr, member_name: str): return ir0.ClassMemberAccess(class_type_expr=class_expr, member_name=member_name, member_type=ir0.BoolType())
def int_member_access(class_expr: ir0.Expr, member_name: str): return ir0.ClassMemberAccess(class_type_expr=class_expr, member_name=member_name, member_type=ir0.Int64Type())
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())
def type_member_access(class_expr: ir0.Expr, member_name: str): return ir0.ClassMemberAccess(inner_expr=class_expr, member_name=member_name, expr_type=ir0.TypeType())