Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/generators/cpp/cppRetyper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1808,10 +1808,29 @@ let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
let cpp_type_of = cpp_type_of ctx.ctx_common.basic in
let retype_func_arg = retype_func_arg ctx.ctx_common.basic in

(* A method's argument with a constant default keeps its scalar type, printed as ::hx::Null<T> and read with .Default, so a call
passing a number allocates nothing. Its function value (gen's callable_arg_type) takes the argument as Dynamic,
which ::hx::Null<T> converts from, null included. A closure captures the body's copy, not the argument, so an override's
signature doesn't depend on what its body captures. A default that isn't a constant and a dynamic method's arguments,
whose function is a field's value, stay as they were. *)
let retype_method_arg v i =
let arg = retype_func_arg v i in
match i with
| Some { eexpr = TConst TNull } | None -> arg
| Some { eexpr = TConst _ } ->
(match cpp_type_of with_stack_value_type v.v_type with
| TCppScalar _ as scalar -> { arg with tcppv_type = scalar }
| _ -> arg)
| Some _ -> arg
in

let create_function field func = {
tcf_field = field;
tcf_name = native_field_name_remap field;
tcf_args = List.map (fun (v, i) -> retype_func_arg v i, i) func.tf_args;
tcf_args =
(match field.cf_kind with
| Method MethDynamic -> List.map (fun (v, i) -> retype_func_arg v i, i) func.tf_args
| _ -> List.map (fun (v, i) -> retype_method_arg v i, i) func.tf_args);
tcf_func = func;
tcf_is_virtual = not (has_meta Meta.NonVirtual field.cf_meta);
tcf_is_reflective = reflective class_def field;
Expand Down
11 changes: 9 additions & 2 deletions src/generators/cpp/gen/cppGen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,16 @@ let cpp_no_debug_synbol ctx var =

let cpp_debug_var_visible ctx var = not (cpp_no_debug_synbol ctx (fst var))

(* A function value takes a scalar argument with a constant default as Dynamic, so a null passed to it takes the default. *)
let callable_arg_type (v, o) =
match o with
| Some { eexpr = TConst TNull } -> v.tcppv_type
| Some { eexpr = TConst _ } when is_cpp_scalar v.tcppv_type -> TCppDynamic
| _ -> v.tcppv_type

let cpp_callable_args arguments prefix =
let make_arg (v, o) =
let return = tcpp_to_string v.tcppv_type in
let return = tcpp_to_string (callable_arg_type (v, o)) in
let prefixed = match o with
| Some {eexpr = TConst TNull} -> v.tcppv_name
| Some _ -> prefix ^ v.tcppv_name
Expand All @@ -125,7 +132,7 @@ let cpp_callable_signaure closure =

let func_to_callable_string wrapper func =
let return_str = tcpp_to_string func.tcf_return in
let arguments_str = func.tcf_args |> List.map (fun (v, _) -> v.tcppv_type) |> List.map tcpp_to_string |> String.concat "," in
let arguments_str = func.tcf_args |> List.map callable_arg_type |> List.map tcpp_to_string |> String.concat "," in

Printf.sprintf "%s< %s (%s) >" wrapper return_str arguments_str

Expand Down
143 changes: 143 additions & 0 deletions tests/unit/src/unit/issues/Issue13249.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package unit.issues;

private class Base {
public function new() {}

public function scale(x:Float = 2.0):Float {
return x;
}

public function shift(?x:Float = 1.0):Float {
return x;
}
}

private class Child extends Base {
override public function scale(x:Float = 5.0):Float {
return x * 10;
}

override public function shift(?x:Float):Float {
return x == null ? -1 : x;
}
}

private class Captures extends Base {
override public function scale(x:Float = 5.0):Float {
var f = () -> x * 10;
return f();
}
}

private class Mutates extends Base {
override public function scale(x:Float = 5.0):Float {
var f = () -> x += 1;
f();
return x;
}
}

private class CapturingBase {
public function new() {}

public function scale(x:Float = 2.0):Float {
var f = () -> x;
return f();
}
}

private class PlainChild extends CapturingBase {
override public function scale(x:Float = 3.0):Float {
return x * 10;
}
}

private interface Scaler {
function scaleBy(?x:Float):Float;
}

private class ScalerImpl implements Scaler {
public function new() {}

public function scaleBy(?x:Float = 3.0):Float {
return 1 / x;
}
}

#if (cpp && !cppia)
@:cppFileCode("#include <type_traits>")
#end
class Issue13249 extends Test {
static function inverse(x:Float = 2.0):Float {
return 1 / x;
}

static function inverseOptional(?x:Float = 2.0):Float {
return 1 / x;
}

static function plusOne(n:Int = 1):Int {
return n + 1;
}

static function negate(b:Bool = true):Bool {
return !b;
}

function inverseOf(x:Float = 2.0):Float {
return 1 / x;
}

#if cpp
function test() {
// A Float, Int or Bool argument with a constant default isn't taken as Dynamic, so passing a number allocates nothing (cppia boxes it still).
// The check is that it isn't Dynamic, whatever type it takes instead.
#if !cppia
untyped __cpp__('static_assert(!std::is_same<decltype(&::unit::issues::Issue13249_obj::inverse), Float (*)(::Dynamic)>::value, "inverse takes its Float as Dynamic")');
untyped __cpp__('static_assert(!std::is_same<decltype(&::unit::issues::Issue13249_obj::inverseOf), Float (::unit::issues::Issue13249_obj::*)(::Dynamic)>::value, "inverseOf takes its Float as Dynamic")');
untyped __cpp__('static_assert(!std::is_same<decltype(&::unit::issues::Issue13249_obj::plusOne), int (*)(::Dynamic)>::value, "plusOne takes its Int as Dynamic")');
untyped __cpp__('static_assert(!std::is_same<decltype(&::unit::issues::Issue13249_obj::negate), bool (*)(::Dynamic)>::value, "negate takes its Bool as Dynamic")');
#end
eq(2, plusOne());
eq(5, plusOne(4));
f(negate());
t(negate(false));
eq(0.25, inverse(4));
eq(0.4, inverseOf(2.5));
eq(0.5, inverse());
eq(0.5, inverseOptional());
eq(0.5, inverseOf());
// A null takes the default, called directly or through a function value.
eq(0.5, inverseOptional(null));
var f:Dynamic = inverseOptional;
eq(0.5, f(null));
eq(0.5, f());
var g:Dynamic = inverse;
eq(0.5, g(null));
eq(0.5, Reflect.callMethod(this, Reflect.field(this, "inverseOf"), [null]));
// Overrides with another default or none, and an interface's method, take their own.
var base:Base = new Child();
eq(50.0, base.scale());
eq(-10.0, base.scale(-1));
eq(-1.0, base.shift());
eq(4.0, base.shift(4));
eq(1.0, new Base().shift());
// An override capturing its argument in a closure, or overriding a method that does, takes it as the method it overrides does.
var captures:Base = new Captures();
eq(50.0, captures.scale());
eq(-10.0, captures.scale(-1));
var mutates:Base = new Mutates();
eq(6.0, mutates.scale());
eq(0.0, mutates.scale(-1));
var plain:CapturingBase = new PlainChild();
eq(30.0, plain.scale());
eq(2.0, new CapturingBase().scale());
eq(-1.0, new CapturingBase().scale(-1));
var scaler:Scaler = new ScalerImpl();
eq(1 / 3, scaler.scaleBy());
var d:Dynamic = new Child();
eq(50.0, d.scale());
eq(-1.0, d.shift());
}
#end
}
Loading