Skip to content

Commit

Permalink
Turn cl_init into a class field (#11500)
Browse files Browse the repository at this point in the history
* turn cl_init into a class field

* add CfrInit

* remove field_kind
  • Loading branch information
Simn authored Jan 20, 2024
1 parent 39ee4d2 commit 61f8436
Show file tree
Hide file tree
Showing 31 changed files with 105 additions and 99 deletions.
9 changes: 2 additions & 7 deletions src/codegen/codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ module Dump = struct
| Some f -> print_field false f);
List.iter (print_field false) c.cl_ordered_fields;
List.iter (print_field true) c.cl_ordered_statics;
(match c.cl_init with
(match TClass.get_cl_init c with
| None -> ()
| Some e ->
print "\n\tstatic function __init__() ";
Expand Down Expand Up @@ -490,14 +490,9 @@ let map_source_header com f =

(* Static extensions for classes *)
module ExtClass = struct

let add_cl_init c e = match c.cl_init with
| None -> c.cl_init <- Some e
| Some e' -> c.cl_init <- Some (concat e' e)

let add_static_init c cf e p =
let ethis = Texpr.Builder.make_static_this c p in
let ef1 = mk (TField(ethis,FStatic(c,cf))) cf.cf_type p in
let e_assign = mk (TBinop(OpAssign,ef1,e)) e.etype p in
add_cl_init c e_assign
TClass.add_cl_init c e_assign
end
4 changes: 2 additions & 2 deletions src/codegen/gencommon/gencommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,8 @@ let run_filters_from gen t filters =
gen.gcurrent_classfield <- None;
(match c.cl_init with
| None -> ()
| Some e ->
c.cl_init <- Some (List.fold_left (fun e f -> f e) e filters));
| Some f ->
process_field f);
| TClassDecl _ | TEnumDecl _ | TTypeDecl _ | TAbstractDecl _ ->
()

Expand Down
4 changes: 2 additions & 2 deletions src/codegen/gencommon/initFunction.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let handle_override_dynfun acc e this field =

let handle_class gen cl =
let com = gen.gcon in
let init = match cl.cl_init with
let init = match TClass.get_cl_init cl with
| None -> []
| Some i -> [i]
in
Expand Down Expand Up @@ -109,7 +109,7 @@ let handle_class gen cl =
let init = List.rev init in
(match init with
| [] -> cl.cl_init <- None
| _ -> cl.cl_init <- Some (mk (TBlock init) com.basic.tvoid cl.cl_pos));
| _ -> TClass.set_cl_init cl (mk (TBlock init) com.basic.tvoid cl.cl_pos));

(* FIXME: find a way to tell OverloadingConstructor to execute this code even with empty constructors *)
let vars, funs = List.fold_left (fun (acc_vars,acc_funs) cf ->
Expand Down
2 changes: 1 addition & 1 deletion src/context/display/deprecationCheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ let run com =
| TClassDecl c when not (Meta.has Meta.Deprecated c.cl_meta) ->
let dctx = {dctx with class_meta = c.cl_meta; curmod = c.cl_module} in
(match c.cl_constructor with None -> () | Some cf -> run_on_field dctx cf);
(match c.cl_init with None -> () | Some e -> run_on_expr dctx e);
(match TClass.get_cl_init c with None -> () | Some e -> run_on_expr dctx e);
List.iter (run_on_field dctx) c.cl_ordered_statics;
List.iter (run_on_field dctx) c.cl_ordered_fields;
| _ ->
Expand Down
2 changes: 1 addition & 1 deletion src/core/json/genjson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ let generate_class ctx c =
"fields",jlist (generate_class_field ctx CFSMember) c.cl_ordered_fields;
"statics",jlist (generate_class_field ctx CFSStatic) c.cl_ordered_statics;
"constructor",jopt (generate_class_field ctx CFSConstructor) c.cl_constructor;
"init",jopt (generate_texpr ctx) c.cl_init;
"init",jopt (generate_texpr ctx) (TClass.get_cl_init c);
"overrides",jlist (classfield_ref ctx) (List.filter (fun cf -> has_class_field_flag cf CfOverride) c.cl_ordered_fields);
"isExtern",jbool (has_class_flag c CExtern);
"isFinal",jbool (has_class_flag c CFinal);
Expand Down
2 changes: 2 additions & 0 deletions src/core/tFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ let find_field c name kind =
PMap.find name c.cl_statics
| CfrMember ->
PMap.find name c.cl_fields
| CfrInit ->
begin match c.cl_init with Some cf -> cf | None -> raise Not_found end

let null_module = {
m_id = alloc_mid();
Expand Down
24 changes: 24 additions & 0 deletions src/core/tOther.ml
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,30 @@ module TClass = struct
in
let apply = apply_params c.cl_params tl in
loop apply c


let get_cl_init c = match c.cl_init with
| Some {cf_expr = Some e} -> Some e
| _ -> None

let modify_cl_init c e append = match c.cl_init with
| Some cf ->
begin match cf.cf_expr with
| Some e' when append ->
cf.cf_expr <- Some (concat e' e)
| _ ->
cf.cf_expr <- Some e
end
| None ->
let cf = mk_field "__init__" t_dynamic null_pos null_pos in
cf.cf_expr <- Some e;
c.cl_init <- Some cf

let add_cl_init c e =
modify_cl_init c e true

let set_cl_init c e =
modify_cl_init c e false
end

let s_class_path c =
Expand Down
3 changes: 2 additions & 1 deletion src/core/tPrinting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ let s_class_field_ref_kind = function
| CfrStatic -> "CfrStatic"
| CfrMember -> "CfrMember"
| CfrConstructor -> "CfrConstructor"
| CfrInit -> "CfrInit"

module Printer = struct

Expand Down Expand Up @@ -506,7 +507,7 @@ module Printer = struct
"cl_super",s_opt (fun (c,tl) -> s_type (TInst(c,tl))) c.cl_super;
"cl_implements",s_list ", " (fun (c,tl) -> s_type (TInst(c,tl))) c.cl_implements;
"cl_array_access",s_opt s_type c.cl_array_access;
"cl_init",s_opt (s_expr_ast true "" s_type) c.cl_init;
"cl_init",s_opt (s_expr_ast true "" s_type) (TOther.TClass.get_cl_init c);
"cl_constructor",s_opt (s_tclass_field (tabs ^ "\t")) c.cl_constructor;
"cl_ordered_fields",s_list "\n\t" (s_tclass_field (tabs ^ "\t")) c.cl_ordered_fields;
"cl_ordered_statics",s_list "\n\t" (s_tclass_field (tabs ^ "\t")) c.cl_ordered_statics;
Expand Down
3 changes: 2 additions & 1 deletion src/core/tType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ and tclass = {
mutable cl_dynamic : t option;
mutable cl_array_access : t option;
mutable cl_constructor : tclass_field option;
mutable cl_init : texpr option;
mutable cl_init : tclass_field option;

mutable cl_build : unit -> build_state;
(*
Expand Down Expand Up @@ -421,6 +421,7 @@ and class_field_ref_kind =
| CfrStatic
| CfrMember
| CfrConstructor
| CfrInit

and class_field_ref = {
cfr_sign : string;
Expand Down
5 changes: 2 additions & 3 deletions src/filters/filters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -666,15 +666,14 @@ let save_class_state com t =
let csr = Option.map (mk_field_restore) c.cl_constructor in
let ofr = List.map (mk_field_restore) c.cl_ordered_fields in
let osr = List.map (mk_field_restore) c.cl_ordered_statics in
let init = c.cl_init in
Option.may save_vars init;
let init = Option.map mk_field_restore c.cl_init in
c.cl_restore <- (fun() ->
c.cl_super <- sup;
c.cl_implements <- impl;
c.cl_meta <- meta;
if ext then add_class_flag c CExtern else remove_class_flag c CExtern;
c.cl_path <- path;
c.cl_init <- init;
c.cl_init <- Option.map restore_field init;
c.cl_ordered_fields <- List.map restore_field ofr;
c.cl_ordered_statics <- List.map restore_field osr;
c.cl_fields <- mk_pmap c.cl_ordered_fields;
Expand Down
4 changes: 2 additions & 2 deletions src/filters/filtersCommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ let run_expression_filters ?(ignore_processed_status=false) ctx detail_times fil
(match c.cl_constructor with
| None -> ()
| Some f -> process_field f);
(match c.cl_init with
(match TClass.get_cl_init c with
| None -> ()
| Some e ->
let identifier = Printf.sprintf "%s.__init__" (s_type_path c.cl_path) in
c.cl_init <- Some (run (Some identifier) e));
TClass.set_cl_init c (run (Some identifier) e))
| TEnumDecl _ -> ()
| TTypeDecl _ -> ()
| TAbstractDecl _ -> ()
Expand Down
12 changes: 6 additions & 6 deletions src/generators/gencpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5003,7 +5003,7 @@ let find_referenced_types_flags ctx obj field_name super_deps constructor_deps h
(* Body of main function *)
(match obj with
| TClassDecl class_def -> visit_class class_def;
(match class_def.cl_init with Some expression -> visit_params expression | _ -> ())
(match TClass.get_cl_init class_def with Some expression -> visit_params expression | _ -> ())
| TEnumDecl enum_def -> visit_enum enum_def
| TTypeDecl _ | TAbstractDecl _ -> (* These are expanded *) ());

Expand Down Expand Up @@ -5455,7 +5455,7 @@ let rec find_next_super_iteration ctx class_def =
;;

let has_init_field class_def =
match class_def.cl_init with
match TClass.get_cl_init class_def with
| Some _ -> true
| _ -> false;;

Expand Down Expand Up @@ -5533,7 +5533,7 @@ let has_compare_field class_def =


let has_boot_field class_def =
match class_def.cl_init with
match TClass.get_cl_init class_def with
| None -> List.exists has_field_init (List.filter should_implement_field class_def.cl_ordered_statics)
| _ -> true
;;
Expand Down Expand Up @@ -6094,7 +6094,7 @@ let generate_class_files baseCtx super_deps constructor_deps class_def inScripta
end;
end;

(match class_def.cl_init with
(match TClass.get_cl_init class_def with
| Some expression ->
let ctx = file_context baseCtx cpp_file debug false in
output_cpp ("void " ^ class_name^ "::__init__()");
Expand Down Expand Up @@ -8379,7 +8379,7 @@ let generate_script_class common_ctx script class_def =
script#write ((string_of_int ( (List.length ordered_fields) +
(List.length ordered_statics) +
(match class_def.cl_constructor with Some _ -> 1 | _ -> 0 ) +
(match class_def.cl_init with Some _ -> 1 | _ -> 0 ) ) )
(match TClass.get_cl_init class_def with Some _ -> 1 | _ -> 0 ) ) )
^ "\n");

let generate_field isStatic field =
Expand Down Expand Up @@ -8412,7 +8412,7 @@ let generate_script_class common_ctx script class_def =
(match class_def.cl_constructor with
| Some field -> generate_field true field
| _ -> () );
(match class_def.cl_init with
(match TClass.get_cl_init class_def with
| Some expression -> script#voidFunc true false "__init__" expression
| _ -> () );

Expand Down
10 changes: 3 additions & 7 deletions src/generators/gencs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2702,7 +2702,7 @@ let generate con =
end_block w
) main_expr;

(match cl.cl_init with
(match TClass.get_cl_init cl with
| None -> ()
| Some init ->
let needs_block,write_expr =
Expand Down Expand Up @@ -2957,7 +2957,7 @@ let generate con =
let super_map (cl,tl) = (cl, List.map run_follow_gen tl) in
List.iter (function
| TClassDecl cl ->
let all_fields = (Option.map_default (fun cf -> [cf]) [] cl.cl_constructor) @ cl.cl_ordered_fields @ cl.cl_ordered_statics in
let all_fields = (Option.map_default (fun cf -> [cf]) [] cl.cl_constructor) @ cl.cl_ordered_fields @ cl.cl_ordered_statics @ (Option.map_default (fun cf -> [cf]) [] cl.cl_init) in
List.iter (fun cf ->
cf.cf_type <- run_follow_gen cf.cf_type;
cf.cf_expr <- Option.map type_map cf.cf_expr;
Expand All @@ -2971,7 +2971,6 @@ let generate con =
) all_fields;
cl.cl_dynamic <- Option.map run_follow_gen cl.cl_dynamic;
cl.cl_array_access <- Option.map run_follow_gen cl.cl_array_access;
cl.cl_init <- Option.map type_map cl.cl_init;
cl.cl_super <- Option.map super_map cl.cl_super;
cl.cl_implements <- List.map super_map cl.cl_implements
| _ -> ()
Expand Down Expand Up @@ -3483,10 +3482,7 @@ let generate con =
mk_nativearray_decl gen basic.tint (List.map (fun (i,s) -> { eexpr = TConst(TInt (i)); etype = basic.tint; epos = c.cl_pos }) all) c.cl_pos;
mk_nativearray_decl gen basic.tstring (List.map (fun (i,s) -> { eexpr = TConst(TString (s)); etype = basic.tstring; epos = c.cl_pos }) all) c.cl_pos;
]); etype = basic.tvoid; epos = c.cl_pos } in
match c.cl_init with
| None -> c.cl_init <- Some expr
| Some e ->
c.cl_init <- Some { eexpr = TBlock([expr;e]); etype = basic.tvoid; epos = e.epos }
TClass.add_cl_init c expr
end
with | Not_found -> ())
| _ -> ()) gen.gtypes;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genhl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3696,7 +3696,7 @@ let generate_static_init ctx types main =
(* init class statics *)
let init_exprs = ref [] in
List.iter (fun t ->
(match t with TClassDecl { cl_init = Some e } -> init_exprs := e :: !init_exprs | _ -> ());
(match t with TClassDecl { cl_init = Some {cf_expr = Some e} } -> init_exprs := e :: !init_exprs | _ -> ());
match t with
| TClassDecl c when not (has_class_flag c CExtern) ->
List.iter (fun f ->
Expand Down
5 changes: 2 additions & 3 deletions src/generators/genjava.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2201,7 +2201,7 @@ let generate con =
newline w
| _ -> ());

(match cl.cl_init with
(match TClass.get_cl_init cl with
| None -> ()
| Some init ->
write w "static";
Expand Down Expand Up @@ -2298,14 +2298,13 @@ let generate con =
let super_map (cl,tl) = (cl, List.map run_follow_gen tl) in
List.iter (function
| TClassDecl cl ->
let all_fields = (Option.map_default (fun cf -> [cf]) [] cl.cl_constructor) @ cl.cl_ordered_fields @ cl.cl_ordered_statics in
let all_fields = (Option.map_default (fun cf -> [cf]) [] cl.cl_constructor) @ cl.cl_ordered_fields @ cl.cl_ordered_statics @ (Option.map_default (fun cf -> [cf]) [] cl.cl_init)in
List.iter (fun cf ->
cf.cf_type <- run_follow_gen cf.cf_type;
cf.cf_expr <- Option.map type_map cf.cf_expr
) all_fields;
cl.cl_dynamic <- Option.map run_follow_gen cl.cl_dynamic;
cl.cl_array_access <- Option.map run_follow_gen cl.cl_array_access;
cl.cl_init <- Option.map type_map cl.cl_init;
cl.cl_super <- Option.map super_map cl.cl_super;
cl.cl_implements <- List.map super_map cl.cl_implements
| _ -> ()
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genjs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,7 @@ let need_to_generate_interface ctx cl_iface =

let generate_type ctx = function
| TClassDecl c ->
(match c.cl_init with
(match TClass.get_cl_init c with
| None -> ()
| Some e ->
ctx.inits <- e :: ctx.inits);
Expand Down
7 changes: 2 additions & 5 deletions src/generators/genjvm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2532,10 +2532,7 @@ class tclass_to_jvm gctx c = object(self)
let p = null_pos in
let efield = Texpr.Builder.make_static_field c cf 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
| Some e -> c.cl_init <- Some (concat e eop)
end
TClass.add_cl_init c eop
in
begin match cf.cf_expr with
| None ->
Expand Down Expand Up @@ -2618,7 +2615,7 @@ class tclass_to_jvm gctx c = object(self)
| Some cf,None -> field MConstructor cf
| None,_ -> ()
end;
begin match c.cl_init with
begin match TClass.get_cl_init c with
| None ->
()
| Some e ->
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genlua.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,7 @@ let generate_require ctx path meta =

let generate_type ctx = function
| TClassDecl c ->
(match c.cl_init with
(match TClass.get_cl_init c with
| None -> ()
| Some e ->
ctx.inits <- e :: ctx.inits);
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genneko.ml
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ let gen_enum ctx e =
let gen_type ctx t acc =
match t with
| TClassDecl c ->
(match c.cl_init with
(match TClass.get_cl_init c with
| None -> ()
| Some e -> ctx.inits <- (c,e) :: ctx.inits);
if (has_class_flag c CExtern) then
Expand Down
8 changes: 4 additions & 4 deletions src/generators/genphp7.ml
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ class class_wrapper (cls) =
if (has_class_flag cls CInterface) then
false
else
match cls.cl_init with
match TClass.get_cl_init cls with
| Some _ -> true
| None ->
List.exists
Expand All @@ -978,7 +978,7 @@ class class_wrapper (cls) =
Returns expression of a user-defined static __init__ method
@see http://old.haxe.org/doc/advanced/magic#initialization-magic
*)
method! get_magic_init = cls.cl_init
method! get_magic_init = TClass.get_cl_init cls
(**
Returns hx source file name where this type was declared
*)
Expand All @@ -994,7 +994,7 @@ class class_wrapper (cls) =
if not (has_class_flag cls CExtern) then
None
else
match cls.cl_init with
match TClass.get_cl_init cls with
| None -> None
| Some body ->
let path =
Expand All @@ -1009,8 +1009,8 @@ class class_wrapper (cls) =
cl_ordered_fields = [];
cl_ordered_statics = [];
cl_constructor = None;
cl_init = Some body
} in
TClass.set_cl_init additional_cls body;
remove_class_flag additional_cls CExtern;
Some (TClassDecl additional_cls)
end
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genpy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ module Generator = struct
!has_static_methods || !has_empty_static_vars

let gen_class_init ctx c =
match c.cl_init with
match TClass.get_cl_init c with
| None ->
()
| Some e ->
Expand Down
Loading

0 comments on commit 61f8436

Please sign in to comment.