Browse Source

eliminate inline var/functions that are not used when DCE is on

Nicolas Cannasse 13 năm trước cách đây
mục cha
commit
d6593bc4c4
3 tập tin đã thay đổi với 13 bổ sung8 xóa
  1. 4 2
      type.ml
  2. 5 5
      typeload.ml
  3. 4 1
      typer.ml

+ 4 - 2
type.ml

@@ -887,8 +887,10 @@ let rec unify a b =
 				with
 					Unify_error l -> error (invalid_field n :: l));
 				(match f1.cf_kind with
-				| Method MethInline when (c.cl_extern || has_meta ":extern" f1.cf_meta) && not (has_meta ":runtime" f1.cf_meta) ->
-					error [Has_no_runtime_field (a,n)]
+				| Method MethInline ->
+					if (c.cl_extern || has_meta ":extern" f1.cf_meta) && not (has_meta ":runtime" f1.cf_meta) then error [Has_no_runtime_field (a,n)];
+					(* mark as used so it's not removed by DCE *)
+					if not (has_meta ":?used" f1.cf_meta) then f1.cf_meta <- (":?used",[],f1.cf_pos) :: f1.cf_meta;
 				| _ -> ());
 			) an.a_fields;
 			if !(an.a_status) = Opened then an.a_status := Closed;

+ 5 - 5
typeload.ml

@@ -884,7 +884,7 @@ let init_class ctx c p herits fields =
 		end
 	in
 
-	let bind_var ctx cf e stat =
+	let bind_var ctx cf e stat inline =
 		let p = cf.cf_pos in
 		if not stat && has_field cf.cf_name c.cl_super then error ("Redefinition of variable " ^ cf.cf_name ^ " in subclass is not allowed") p;
 		let t = cf.cf_type in
@@ -905,7 +905,7 @@ let init_class ctx c p herits fields =
 				if not !return_partial_type then begin
 					r := (fun() -> t);
 					if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ cf.cf_name);
-					mark_used cf;
+					if not inline then mark_used cf;
 					let e = type_static_var ctx t e p in
 					let e = (match cf.cf_kind with
 					| Var { v_read = AccInline } ->
@@ -969,7 +969,7 @@ let init_class ctx c p herits fields =
 				cf_params = [];
 				cf_overloads = [];
 			} in
-			let delay = bind_var ctx cf e stat in
+			let delay = bind_var ctx cf e stat inline in
 			f, false, cf, delay
 		| FFun fd ->
 			let params = ref [] in
@@ -1083,7 +1083,7 @@ let init_class ctx c p herits fields =
 						(match e.eexpr with
 						| TBlock [] | TBlock [{ eexpr = TConst _ }] | TConst _ | TObjectDecl [] -> ()
 						| _ -> c.cl_init <- Some e);
-					if not constr then mark_used cf;
+					if not (constr || inline) then mark_used cf;
 					cf.cf_expr <- Some (mk (TFunction f) t p);
 					cf.cf_type <- t;
 				end;
@@ -1145,7 +1145,7 @@ let init_class ctx c p herits fields =
 				cf_params = [];
 				cf_overloads = [];
 			} in
-			let delay = bind_var ctx cf eo stat in
+			let delay = bind_var ctx cf eo stat inline in
 			f, false, cf, (fun() -> delay(); (!check_get)(); (!check_set)())
 	in
 	let rec check_require = function

+ 4 - 1
typer.ml

@@ -443,6 +443,7 @@ let rec acc_get ctx g p =
 			| TInst (c,_) -> chk_class c
 			| TAnon a -> (match !(a.a_status) with Statics c -> chk_class c | _ -> ())
 			| _ -> ());
+			mark_used_field ctx f;
 			mk (TClosure (e,f.cf_name)) t p
 		| Some e ->
 			let rec loop e = Type.map_expr loop { e with epos = p } in
@@ -476,7 +477,9 @@ let field_access ctx mode f t e p =
 		| MethInline, _ -> AKInline (e,f,t)
 		| MethMacro, MGet -> display_error ctx "Macro functions must be called immediatly" p; normal()
 		| MethMacro, MCall -> AKMacro (e,f)
-		| _ , MGet -> AKExpr (mk (TClosure (e,f.cf_name)) t p)
+		| _ , MGet ->
+			mark_used_field ctx f;
+			AKExpr (mk (TClosure (e,f.cf_name)) t p)
 		| _ -> normal())
 	| Var v ->
 		match (match mode with MGet | MCall -> v.v_read | MSet -> v.v_write) with