From a2bbb11dc385d7076a1868f6431392ed16ee057d Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Thu, 11 Jan 2024 20:10:25 +0100 Subject: [PATCH] remove m_if_feature, make DCE check the meta directly instead also copy class @:ifFeature to field @:ifFeature --- src/context/feature.ml | 11 ---------- src/core/tFunctions.ml | 1 - src/core/tPrinting.ml | 1 - src/core/tType.ml | 1 - src/optimization/dce.ml | 40 +++++++++++++++++++++++++----------- src/typing/typeloadFields.ml | 26 +++++++++++++---------- 6 files changed, 43 insertions(+), 37 deletions(-) delete mode 100644 src/context/feature.ml diff --git a/src/context/feature.ml b/src/context/feature.ml deleted file mode 100644 index 647d03da10b..00000000000 --- a/src/context/feature.ml +++ /dev/null @@ -1,11 +0,0 @@ -open Ast -open Type -open Error - -let rec check_if_feature = function - | [] -> [] - | (Meta.IfFeature,el,_) :: _ -> List.map (fun (e,p) -> match e with EConst (String(s,_)) -> s | _ -> raise_typing_error "String expected" p) el - | _ :: l -> check_if_feature l - -let set_feature m cf_ref s = - m.m_extra.m_if_feature <- (s, cf_ref) :: m.m_extra.m_if_feature diff --git a/src/core/tFunctions.ml b/src/core/tFunctions.ml index ad0cb531bcc..116c0f9f333 100644 --- a/src/core/tFunctions.ml +++ b/src/core/tFunctions.ml @@ -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; } diff --git a/src/core/tPrinting.ml b/src/core/tPrinting.ml index 5ac61c1d827..4b83743db41 100644 --- a/src/core/tPrinting.ml +++ b/src/core/tPrinting.ml @@ -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 *) ] diff --git a/src/core/tType.ml b/src/core/tType.ml index 8d89f77950a..fb08dac3de4 100644 --- a/src/core/tType.ml +++ b/src/core/tType.ml @@ -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; } diff --git a/src/optimization/dce.ml b/src/optimization/dce.ml index d9a0b410116..f754582d117 100644 --- a/src/optimization/dce.ml +++ b/src/optimization/dce.ml @@ -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 = @@ -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 -> () @@ -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; @@ -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; @@ -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 -> () @@ -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; diff --git a/src/typing/typeloadFields.ml b/src/typing/typeloadFields.ml index fbc8a656ac3..a6d906d15cf 100644 --- a/src/typing/typeloadFields.ml +++ b/src/typing/typeloadFields.ml @@ -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 -> @@ -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