Skip to content

Commit

Permalink
remove m_if_feature, make DCE check the meta directly instead
Browse files Browse the repository at this point in the history
also copy class @:ifFeature to field @:ifFeature
  • Loading branch information
Simn committed Jan 11, 2024
1 parent ba272a6 commit a2bbb11
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 37 deletions.
11 changes: 0 additions & 11 deletions src/context/feature.ml

This file was deleted.

1 change: 0 additions & 1 deletion src/core/tFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ let module_extra file sign time kind added policy =
m_deps = PMap.empty;
m_kind = kind;
m_cache_bound_objects = DynArray.create ();
m_if_feature = [];
m_features = Hashtbl.create 0;
m_check_policy = policy;
}
Expand Down
1 change: 0 additions & 1 deletion src/core/tPrinting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,6 @@ module Printer = struct
"m_processed",string_of_int me.m_processed;
"m_kind",s_module_kind me.m_kind;
"m_binded_res",""; (* TODO *)
"m_if_feature",""; (* TODO *)
"m_features",""; (* TODO *)
]

Expand Down
1 change: 0 additions & 1 deletion src/core/tType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ and module_def_extra = {
mutable m_deps : (int,(Digest.t (* sign *) * path)) PMap.t;
mutable m_kind : module_kind;
mutable m_cache_bound_objects : cache_bound_object DynArray.t;
mutable m_if_feature : (string * class_field_ref) list;
mutable m_features : (string,bool) Hashtbl.t;
}

Expand Down
40 changes: 28 additions & 12 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, class_field_ref list) Hashtbl.t;
mutable features : (string, class_field_ref list ref) Hashtbl.t;
}

let push_class dce c =
Expand Down Expand Up @@ -153,7 +153,7 @@ let rec check_feature dce s =
List.iter (fun cfr ->
let (c, cf) = resolve_class_field_ref dce.com cfr in
mark_field dce c cf cfr.cfr_kind
) l;
) !l;
Hashtbl.remove dce.features s;
with Not_found ->
()
Expand Down Expand Up @@ -717,6 +717,7 @@ let fix_accessors com =
) com.types

let collect_entry_points dce com =
let delayed = ref [] in
List.iter (fun t ->
let mt = t_infos t in
mt.mt_meta <- Meta.remove Meta.Used mt.mt_meta;
Expand All @@ -725,7 +726,25 @@ let collect_entry_points dce com =
let keep_class = keep_whole_class dce c && (not (has_class_flag c CExtern) || (has_class_flag c CInterface)) in
let is_struct = dce.com.platform = Hl && Meta.has Meta.Struct c.cl_meta in
let loop kind cf =
if keep_class || is_struct || keep_field dce cf c kind then mark_field dce c cf kind
List.iter (fun (m,el,p) -> match m with
| Meta.IfFeature ->
let cf_ref = mk_class_field_ref c cf kind com.is_macro_context in
List.iter (fun (e,p) -> match e with
| EConst (String(s,_)) ->
begin try
let l = Hashtbl.find dce.features s in
l := cf_ref :: !l
with Not_found ->
Hashtbl.add dce.features s (ref [cf_ref])
end
| _ ->
Error.raise_typing_error "String expected" p
) el
| _ ->
()
) cf.cf_meta;
(* Have to delay mark_field so that we see all @:ifFeature *)
if keep_class || is_struct || keep_field dce cf c kind then delayed := (fun () -> mark_field dce c cf kind) :: !delayed
in
List.iter (loop CfrStatic) c.cl_ordered_statics;
List.iter (loop CfrMember) c.cl_ordered_fields;
Expand All @@ -743,12 +762,15 @@ let collect_entry_points dce com =
()
end;
| TEnumDecl en when keep_whole_enum dce en ->
let pop = push_class dce {null_class with cl_module = en.e_module} in
mark_enum dce en;
pop()
delayed := (fun () ->
let pop = push_class dce {null_class with cl_module = en.e_module} in
mark_enum dce en;
pop()
) :: !delayed;
| _ ->
()
) com.types;
List.iter (fun f -> f()) !delayed;
if dce.debug then begin
List.iter (fun (c,cf,_) -> match cf.cf_expr with
| None -> ()
Expand Down Expand Up @@ -879,12 +901,6 @@ let run com main mode =
features = Hashtbl.create 0;
curclass = null_class;
} in
List.iter (fun m ->
List.iter (fun (s,v) ->
if Hashtbl.mem dce.features s then Hashtbl.replace dce.features s (v :: Hashtbl.find dce.features s)
else Hashtbl.add dce.features s [v]
) m.m_extra.m_if_feature;
) com.modules;

(* first step: get all entry points, which is the main method and all class methods which are marked with @:keep *)
collect_entry_points dce com;
Expand Down
26 changes: 15 additions & 11 deletions src/typing/typeloadFields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,15 @@ let init_class ctx c p herits fields =
| _ :: l ->
check_require l
in
let cl_if_feature = Feature.check_if_feature c.cl_meta in
let rec check_if_feature = function
| [] ->
[]
| (Meta.IfFeature,el,_) :: _ ->
el
| _ :: l ->
check_if_feature l
in
let cl_if_feature = check_if_feature c.cl_meta in
let cl_req = check_require c.cl_meta in
let has_init = ref false in
List.iter (fun f ->
Expand All @@ -1801,16 +1809,12 @@ let init_class ctx c p herits fields =
if fctx.is_field_debug then print_endline ("Created field: " ^ Printer.s_tclass_field "" cf);
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 =
let ref_kind = match fctx.field_kind with
| FKConstructor -> CfrConstructor
| _ -> if fctx.is_static then CfrStatic else CfrMember
in
let cf_ref = mk_class_field_ref c cf ref_kind fctx.is_macro in
Feature.set_feature ctx.m.curmod cf_ref s;
in
List.iter set_feature cl_if_feature;
List.iter set_feature (Feature.check_if_feature cf.cf_meta);
begin match cl_if_feature with
| [] ->
()
| el ->
f.cff_meta <- (Meta.IfFeature,el,null_pos) :: f.cff_meta
end;
let req = check_require f.cff_meta in
let req = (match req with None -> if fctx.is_static || fctx.field_kind = FKConstructor then cl_req else None | _ -> req) in
(match req with
Expand Down

0 comments on commit a2bbb11

Please sign in to comment.