Skip to content

Commit

Permalink
clean up make_static_call
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Oct 25, 2023
1 parent de296a0 commit 3482f68
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/context/abstractCast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let rec make_static_call ctx c cf a pl args t p =
e
| _ -> die "" __LOC__
end else
Typecore.make_static_call ctx c cf (apply_params a.a_params pl) args t p
make_static_abstract_call ctx a pl c cf args p

and do_check_cast ctx uctx tleft eright p =
let recurse cf f =
Expand Down
32 changes: 18 additions & 14 deletions src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,24 @@ let warning ?(depth=0) ctx w msg p =

let make_call ctx e el t p = (!make_call_ref) ctx e el t p

let make_static_call_gen ctx c cf el map p =
let monos = Monomorph.spawn_constrained_monos map cf.cf_params in
let t = map (apply_params cf.cf_params monos cf.cf_type) in
match follow t with
| TFun(args,ret) ->
let ethis = Texpr.Builder.make_static_this c p in
let ef = mk (TField(ethis,FStatic(c,cf))) t p in
make_call ctx ef el ret p
| t ->
raise_typing_error (s_type (print_context()) t ^ " cannot be called") p

let make_static_class_call ctx c cf el p =
make_static_call_gen ctx c cf el (fun t -> t) p

let make_static_abstract_call ctx a tl c cf el p =
let map = apply_params a.a_params tl in
make_static_call_gen ctx c cf el map p

let type_expr ?(mode=MGet) ctx e with_type = (!type_expr_ref) ~mode ctx e with_type

let unify_min ctx el = (!unify_min_ref) ctx el
Expand All @@ -247,20 +265,6 @@ let spawn_monomorph' ctx p =
let spawn_monomorph ctx p =
TMono (spawn_monomorph' ctx p)

let make_static_this c p =
let ta = mk_anon ~fields:c.cl_statics (ref (ClassStatics c)) in
mk (TTypeExpr (TClassDecl c)) ta p

let make_static_field_access c cf t p =
let ethis = make_static_this c p in
mk (TField (ethis,(FStatic (c,cf)))) t p

let make_static_call ctx c cf map args t p =
let monos = List.map (fun _ -> spawn_monomorph ctx p) cf.cf_params in
let map t = map (apply_params cf.cf_params monos t) in
let ef = make_static_field_access c cf (map cf.cf_type) p in
make_call ctx ef args (map t) p

let raise_with_type_error ?(depth = 0) msg p =
raise (WithTypeError (make_error ~depth (Custom msg) p))

Expand Down
6 changes: 3 additions & 3 deletions src/core/texpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ module Builder = struct
in
mk (TTypeExpr mt) t pos

let make_static_field c cf p =
let e_this = make_static_this c p in
mk (TField(e_this,FStatic(c,cf))) cf.cf_type p
let make_static_field_access c cf t p =
let ethis = make_static_this c p in
mk (TField (ethis,(FStatic (c,cf)))) t p

let make_throw e p =
mk (TThrow e) t_dynamic p
Expand Down
21 changes: 3 additions & 18 deletions src/filters/exceptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,8 @@ let haxe_exception_static_call ctx method_name args p =
try PMap.find method_name ctx.haxe_exception_class.cl_statics
with Not_found -> raise_typing_error ("haxe.Exception has no field " ^ method_name) p
in
let return_type =
match follow method_field.cf_type with
| TFun(_,t) -> t
| _ -> raise_typing_error ("haxe.Exception." ^ method_name ^ " is not a function and cannot be called") p
in
add_dependency ctx.typer.curclass.cl_module ctx.haxe_exception_class.cl_module;
make_static_call ctx.typer ctx.haxe_exception_class method_field (fun t -> t) args return_type p
make_static_class_call ctx.typer ctx.haxe_exception_class method_field args p

(**
Generate `haxe_exception.method_name(args)`
Expand Down Expand Up @@ -74,13 +69,8 @@ let std_is ctx e t p =
try PMap.find "isOfType" std_cls.cl_statics
with Not_found -> raise_typing_error ("Std has no field isOfType") p
in
let return_type =
match follow isOfType_field.cf_type with
| TFun(_,t) -> t
| _ -> raise_typing_error ("Std.isOfType is not a function and cannot be called") p
in
let type_expr = { eexpr = TTypeExpr(module_type_of_type t); etype = t; epos = p } in
make_static_call ctx.typer std_cls isOfType_field (fun t -> t) [e; type_expr] return_type p
make_static_class_call ctx.typer std_cls isOfType_field [e; type_expr] p

(**
Check if type path of `t` exists in `lst`
Expand Down Expand Up @@ -619,15 +609,10 @@ let insert_save_stacks tctx =
try PMap.find "saveStack" native_stack_trace_cls.cl_statics
with Not_found -> raise_typing_error ("haxe.NativeStackTrace has no field saveStack") null_pos
in
let return_type =
match follow method_field.cf_type with
| TFun(_,t) -> t
| _ -> raise_typing_error ("haxe.NativeStackTrace." ^ method_field.cf_name ^ " is not a function and cannot be called") null_pos
in
let catch_local = mk (TLocal catch_var) catch_var.v_type catch_var.v_pos in
begin
add_dependency tctx.curclass.cl_module native_stack_trace_cls.cl_module;
make_static_call tctx native_stack_trace_cls method_field (fun t -> t) [catch_local] return_type catch_var.v_pos
make_static_class_call tctx native_stack_trace_cls method_field [catch_local] catch_var.v_pos
end
else
mk (TBlock[]) tctx.t.tvoid catch_var.v_pos
Expand Down
2 changes: 1 addition & 1 deletion src/filters/filters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module LocalStatic = struct
| TLocal v when has_var_flag v VStatic ->
begin try
let cf = find_local_static local_static_lut v in
Texpr.Builder.make_static_field c cf e.epos
Texpr.Builder.make_static_field_access c cf cf.cf_type e.epos
with Not_found ->
raise_typing_error (Printf.sprintf "Could not find local static %s (id %i)" v.v_name v.v_id) e.epos
end
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genjvm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2517,7 +2517,7 @@ class tclass_to_jvm gctx c = object(self)
let jm = jc#spawn_field cf.cf_name jsig flags in
let default e =
let p = null_pos in
let efield = Texpr.Builder.make_static_field c cf p in
let efield = Texpr.Builder.make_static_field_access c cf cf.cf_type p in
let eop = mk (TBinop(OpAssign,efield,e)) cf.cf_type p in
begin match c.cl_init with
| None -> c.cl_init <- Some eop
Expand Down
6 changes: 3 additions & 3 deletions src/optimization/inline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ let api_inline ctx c field params p =
let m = (try ctx.com.module_lut#find path with Not_found -> die "" __LOC__) in
add_dependency ctx.m.curmod m;
Option.get (ExtList.List.find_map (function
| TClassDecl cl when cl.cl_path = path -> Some (make_static_this cl p)
| TClassDecl cl when cl.cl_path = path -> Some (Texpr.Builder.make_static_this cl p)
| _ -> None
) m.m_types)
in
Expand Down Expand Up @@ -171,7 +171,7 @@ let api_inline ctx c field params p =
None)
| (["js"],"Boot"),"__downcastCheck",[o; {eexpr = TTypeExpr (TClassDecl cls) } as t] when ctx.com.platform = Js ->
if (has_class_flag cls CInterface) then
Some (Texpr.Builder.fcall (make_static_this c p) "__implements" [o;t] tbool p)
Some (Texpr.Builder.fcall (Texpr.Builder.make_static_this c p) "__implements" [o;t] tbool p)
else
Some (Texpr.Builder.fcall (eJsSyntax()) "instanceof" [o;t] tbool p)
| (["cs" | "java"],"Lib"),("nativeArray"),[{ eexpr = TArrayDecl args } as edecl; _]
Expand Down Expand Up @@ -927,7 +927,7 @@ and inline_rest_params ctx f params map_type p =
in
let array = mk (TArrayDecl params) (ctx.t.tarray t_params) p in
(* haxe.Rest.of(array) *)
let e = make_static_call ctx c cf (apply_params a.a_params [t]) [array] (TAbstract(a,[t_params])) p in
let e = make_static_abstract_call ctx a [t] c cf [array] p in
[e]
| _ ->
die ~p:v.v_pos "Unexpected rest arguments type" __LOC__
Expand Down
2 changes: 1 addition & 1 deletion src/typing/fieldAccess.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Typecore
open Type
open Error

type field_host =
type field_access_mode =
(* Get the plain expression with applied field type parameters. *)
| FGet
(* Does not apply field type parameters. *)
Expand Down
4 changes: 2 additions & 2 deletions src/typing/forLoop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ module IterationKind = struct
| TAbstract({a_impl = Some c} as a,tl) ->
let cf_length = PMap.find "get_length" c.cl_statics in
let get_length e p =
make_static_call ctx c cf_length (apply_params a.a_params tl) [e] ctx.com.basic.tint p
make_static_abstract_call ctx a tl c cf_length [e] p
in
(match follow cf_length.cf_type with
| TFun(_,tr) ->
Expand All @@ -155,7 +155,7 @@ module IterationKind = struct
let todo = mk (TConst TNull) ctx.t.tint p in
let cf,_,r,_ = AbstractCast.find_array_read_access_raise ctx a tl todo p in
let get_next e_base e_index t p =
make_static_call ctx c cf (apply_params a.a_params tl) [e_base;e_index] r p
make_static_abstract_call ctx a tl c cf [e_base;e_index] p
in
IteratorCustom(get_next,get_length),e,r
with Not_found ->
Expand Down
2 changes: 1 addition & 1 deletion src/typing/matcher/texprConverter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let constructor_to_texpr ctx con =
| ConConst ct -> make_const_texpr ctx.com.basic ct p
| ConArray i -> make_int ctx.com.basic i p
| ConTypeExpr mt -> TyperBase.type_module_type ctx mt p
| ConStatic(c,cf) -> make_static_field c cf p
| ConStatic(c,cf) -> make_static_field_access c cf cf.cf_type p
| ConFields _ -> raise_typing_error "Something went wrong" p

let s_subject v_lookup s e =
Expand Down
6 changes: 3 additions & 3 deletions src/typing/operators.ml
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ let make_binop ctx op e1 e2 is_assign_op with_type p =
die "" __LOC__

let find_abstract_binop_overload ctx op e1 e2 a c tl left is_assign_op with_type p =
let map = apply_params a.a_params tl in
let make op_cf cf e1 e2 tret needs_assign swapped =
if cf.cf_expr = None && not (has_class_field_flag cf CfExtern) then begin
if not (Meta.has Meta.NoExpr cf.cf_meta) then Common.display_error ctx.com "Recursive operator method" p;
Expand Down Expand Up @@ -437,11 +436,11 @@ let find_abstract_binop_overload ctx op e1 e2 a c tl left is_assign_op with_type
let vr = new value_reference ctx in
let e2' = vr#as_var "lhs" e2 in
let e1' = vr#as_var "rhs" e1 in
let e = make_static_call ctx c cf map [e1';e2'] tret p in
let e = make_static_abstract_call ctx a tl c cf [e1';e2'] p in
let e = vr#to_texpr e in
BinopResult.create_special e needs_assign
end else
BinopResult.create_special (make_static_call ctx c cf map [e1;e2] tret p) needs_assign
BinopResult.create_special (make_static_abstract_call ctx a tl c cf [e1;e2] p) needs_assign
in
(* special case for == and !=: if the second type is a monomorph, assume that we want to unify
it with the first type to preserve comparison semantics. *)
Expand All @@ -467,6 +466,7 @@ let find_abstract_binop_overload ctx op e1 e2 a c tl left is_assign_op with_type
let check e1 e2 swapped =
let map_arguments () =
let monos = Monomorph.spawn_constrained_monos (fun t -> t) cf.cf_params in
let map = apply_params a.a_params tl in
let map t = map (apply_params cf.cf_params monos t) in
let t1 = map t1 in
let t2 = map t2 in
Expand Down
2 changes: 1 addition & 1 deletion src/typing/typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2015,7 +2015,7 @@ and type_expr ?(mode=MGet) ctx (e,p) (with_type:WithType.t) =
try PMap.find "isOfType" c.cl_statics
with Not_found -> die "" __LOC__
in
Texpr.Builder.make_static_field c cf (mk_zero_range_pos p)
Texpr.Builder.make_static_field_access c cf cf.cf_type (mk_zero_range_pos p)
| _ -> die "" __LOC__
in
mk (TCall (e_Std_isOfType, [e; e_t])) ctx.com.basic.tbool p
Expand Down

2 comments on commit 3482f68

@Simn
Copy link
Member Author

@Simn Simn commented on 3482f68 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This somehow broke the Java tests, so there must be some unintended behavior change.

@Simn
Copy link
Member Author

@Simn Simn commented on 3482f68 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is (and has been) some strange inference behavior here. Before this change, make_static_call would spawn monomorphs, but those are never unified with anything as far as I can tell. The only reason they don't leak out is because the return type uses map t, which is the input return type. This seems quite strange...

The other thing I'm noticing is that there are some typing changes related to @:implicitCast now. In particular, we're losing the first TCast in all the @:implicitCast TCast TCast structures. I'm not even sure where that came from at the moment.

Please sign in to comment.