Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hard references from if_features #11315

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/core/tFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ let module_extra file sign time kind policy =
m_check_policy = policy;
}

let mk_class_field_ref (c : tclass) (cf : tclass_field) (is_static : bool) (is_macro : bool) = {
cfr_sign = c.cl_module.m_extra.m_sign;
cfr_path = c.cl_path;
cfr_field = cf.cf_name;
cfr_is_static = is_static;
cfr_is_macro = is_macro;
}

let mk_field name ?(public = true) ?(static = false) t p name_pos = {
cf_name = name;
Expand Down
12 changes: 10 additions & 2 deletions src/core/tType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,18 @@ and module_def_extra = {
mutable m_deps : (int,(string (* sign *) * path)) PMap.t;
mutable m_kind : module_kind;
mutable m_binded_res : (string, string) PMap.t;
mutable m_if_feature : (string *(tclass * tclass_field * bool)) list;
mutable m_if_feature : (string * class_field_ref) list;
mutable m_features : (string,bool) Hashtbl.t;
}

and class_field_ref = {
cfr_sign : string;
cfr_path : path;
cfr_field : string;
cfr_is_static : bool;
cfr_is_macro : bool;
}

and module_kind =
| MCode
| MMacro
Expand Down Expand Up @@ -461,4 +469,4 @@ type flag_tvar =

let flag_tvar_names = [
"VCaptured";"VFinal";"VUsed";"VAssigned";"VCaught";"VStatic"
]
]
45 changes: 30 additions & 15 deletions src/optimization/dce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type dce = {
mutable marked_maybe_fields : tclass_field list;
mutable t_stack : t list;
mutable ts_stack : t list;
mutable features : (string,(tclass * tclass_field * bool) list) Hashtbl.t;
mutable features : (string, class_field_ref list) Hashtbl.t;
}

let push_class dce c =
Expand All @@ -50,6 +50,26 @@ let push_class dce c =
dce.curclass <- old
)

let find_field c name is_static =
match name with
| "new" when not is_static ->
Simn marked this conversation as resolved.
Show resolved Hide resolved
begin match c.cl_constructor with Some cf -> cf | None -> raise Not_found end
| name ->
let fields = if is_static then c.cl_statics else c.cl_fields in
PMap.find name fields

let resolve_class_field_ref ctx cfr =
let ctx = if cfr.cfr_is_macro && not ctx.is_macro_context then Option.get (ctx.get_macros()) else ctx in
let path = ctx.type_to_module#find cfr.cfr_path in
let m = ctx.module_lut#find path in

Option.get (ExtList.List.find_map (fun mt -> match mt with
| TClassDecl c when c.cl_path = cfr.cfr_path ->
let cf = find_field c cfr.cfr_field cfr.cfr_is_static in
Some (c, cf)
| _ -> None
) m.m_types)

(* checking *)

(* check for @:keepSub metadata, which forces @:keep on child classes *)
Expand Down Expand Up @@ -138,8 +158,9 @@ let rec keep_field dce cf c is_static =
let rec check_feature dce s =
try
let l = Hashtbl.find dce.features s in
List.iter (fun (c,cf,stat) ->
mark_field dce c cf stat
List.iter (fun cfr ->
let (c, cf) = resolve_class_field_ref dce.com cfr in
mark_field dce c cf cfr.cfr_is_static
) l;
Hashtbl.remove dce.features s;
with Not_found ->
Expand All @@ -152,18 +173,18 @@ and check_and_add_feature dce s =

(* mark a field as kept *)
and mark_field dce c cf stat =
let add cf =
let add c' cf =
if not (Meta.has Meta.Used cf.cf_meta) then begin
cf.cf_meta <- (mk_used_meta cf.cf_pos) :: cf.cf_meta;
dce.added_fields <- (c,cf,stat) :: dce.added_fields;
dce.added_fields <- (c',cf,stat) :: dce.added_fields;
dce.marked_fields <- cf :: dce.marked_fields;
check_feature dce (Printf.sprintf "%s.%s" (s_type_path c.cl_path) cf.cf_name);
end
in
if cf.cf_name = "new" then begin
let rec loop c =
begin match c.cl_constructor with
| Some cf -> add cf
| Some cf -> add c cf
| None -> ()
end;
match c.cl_super with
Expand All @@ -174,10 +195,10 @@ and mark_field dce c cf stat =
end else begin
if not (PMap.mem cf.cf_name (if stat then c.cl_statics else c.cl_fields)) then begin
match c.cl_super with
| None -> add cf
| None -> add c cf
| Some (c,_) -> mark_field dce c cf stat
end else
add cf;
add c cf;
if not stat && is_physical_field cf then
match c.cl_constructor with
| None -> ()
Expand Down Expand Up @@ -319,14 +340,8 @@ let rec to_string dce t = match t with
()

and field dce c n stat =
let find_field n =
if n = "new" then match c.cl_constructor with
| None -> raise Not_found
| Some cf -> cf
else PMap.find n (if stat then c.cl_statics else c.cl_fields)
in
(try
let cf = find_field n in
let cf = find_field c n stat in
mark_field dce c cf stat;
with Not_found -> try
if (has_class_flag c CInterface) then begin
Expand Down
2 changes: 1 addition & 1 deletion src/typing/typeloadFields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@ let init_class ctx c p context_init herits fields =
if fctx.is_static && (has_class_flag c CInterface) && fctx.field_kind <> FKInit && not cctx.is_lib && not ((has_class_flag c CExtern)) then
raise_typing_error "You can only declare static fields in extern interfaces" p;
let set_feature s =
ctx.m.curmod.m_extra.m_if_feature <- (s,(c,cf,fctx.is_static)) :: ctx.m.curmod.m_extra.m_if_feature
ctx.m.curmod.m_extra.m_if_feature <- (s, (mk_class_field_ref c cf fctx.is_static fctx.is_macro)) :: ctx.m.curmod.m_extra.m_if_feature;
in
List.iter set_feature cl_if_feature;
List.iter set_feature (check_if_feature cf.cf_meta);
Expand Down