Browse Source

- added --dead-code-removal to erase unreferenced methods from the output
- added @:keep to force keep a specific method or all the methods of a class

Franco Ponticelli 14 years ago
parent
commit
c237ada04b
9 changed files with 45 additions and 9 deletions
  1. 2 0
      common.ml
  2. 3 0
      doc/CHANGES.txt
  3. 3 0
      main.ml
  4. 1 1
      std/flash/Boot.hx
  5. 1 1
      std/flash9/Boot.hx
  6. 1 1
      std/js/Boot.hx
  7. 1 1
      std/neko/Boot.hx
  8. 1 1
      std/php/Boot.hx
  9. 32 4
      typeload.ml

+ 2 - 0
common.ml

@@ -50,6 +50,7 @@ type context = {
 	mutable debug : bool;
 	mutable verbose : bool;
 	mutable foptimize : bool;
+	mutable dead_code_removal : bool;
 	mutable platform : platform;
 	mutable std_path : string list;
 	mutable class_path : string list;
@@ -85,6 +86,7 @@ let create v =
 		debug = false;
 		verbose = false;
 		foptimize = true;
+		dead_code_removal = false;
 		platform = Cross;
 		std_path = [];
 		class_path = [];

+ 3 - 0
doc/CHANGES.txt

@@ -33,6 +33,9 @@
 	all : lookup unqualified types in all package hierarchy and not only in current package
 	flash : set default flash version to 10 (-swf9 deprecated, use -swf-version 8 for avm1)
 	php : added --php-lib to allow to rename the destination path of the generated lib
+	all : added --dead-code-removal, removes unused functions from the output (beta feature could 
+	         not make in the final release)
+	all : added @:keep, forces a class/method to be not descarded when used with --dead-code-removal
 
 2010-08-14: 2.06
 	neko : change serializer to be able to handle instances of basic classes from other modules

+ 3 - 0
main.ml

@@ -462,6 +462,9 @@ try
 		("--macro", Arg.String (fun e ->
 			config_macros := e :: !config_macros
 		)," : call the given macro before typing anything else");
+		("--dead-code-removal", Arg.Unit (fun () ->
+			com.dead_code_removal <- true
+		)," : remove unused methods");
 		("-swf9",Arg.String (fun file ->
 			set_platform Flash file;
 			if com.flash_version < 9. then com.flash_version <- 9.;

+ 1 - 1
std/flash/Boot.hx

@@ -24,7 +24,7 @@
  */
 package flash;
 
-class Boot {
+@:keep class Boot {
 
 	private static var def_color = 0;
 	private static var exception = null;

+ 1 - 1
std/flash9/Boot.hx

@@ -24,7 +24,7 @@
  */
 package flash;
 
-class Boot extends flash.display.MovieClip, implements Dynamic {
+@:keep class Boot extends flash.display.MovieClip, implements Dynamic {
 
 	static var init : Void -> Void;
 	static var tf : flash.text.TextField;

+ 1 - 1
std/js/Boot.hx

@@ -24,7 +24,7 @@
  */
 package js;
 
-class Boot {
+@:keep class Boot {
 
 	private static function __unhtml(s : String) {
 		return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");

+ 1 - 1
std/neko/Boot.hx

@@ -24,7 +24,7 @@
  */
 package neko;
 
-class Boot {
+@:keep class Boot {
 
 	private static function __tmp_str() {
 		return untyped "<...>".__s;

+ 1 - 1
std/php/Boot.hx

@@ -1,6 +1,6 @@
 package php;
 
-class Boot {
+@:keep class Boot {
 	static var qtypes;
 	static var ttypes;
 	static var tpaths;

+ 32 - 4
typeload.ml

@@ -799,14 +799,42 @@ let init_class ctx c p herits fields =
 				cf.cf_expr <- Some (mk (TFunction f) t p);
 				t
 			) in
-			let delay = (
-				if ((c.cl_extern && not inline) || c.cl_interface) && cf.cf_name <> "__init__" then
+			let delay = if (ctx.com.dead_code_removal && not !Common.display) then begin
+				let is_main = (match ctx.com.main_class with | Some cl when c.cl_path = cl -> true | _ -> false) && name = "main" in
+				let keep = core_api || is_main || has_meta ":keep" c.cl_meta || has_meta ":keep" f.cff_meta || (stat && name = "__init__") in
+				let remove item lst = List.filter (fun i -> item <> i.cf_name) lst in
+				if ((c.cl_extern && not inline) || c.cl_interface) && cf.cf_name <> "__init__" then begin
 					(fun() -> ())
-				else begin
+				end else begin
+					cf.cf_type <- TLazy r;
+					(fun() -> 
+						if not keep then begin
+							delay ctx (fun () ->
+								
+								match cf.cf_expr with
+								| None ->
+									if ctx.com.verbose then print_endline ("Removed " ^ (snd c.cl_path) ^ "." ^ name);
+									if stat then begin
+										c.cl_statics <- PMap.remove name c.cl_statics;
+										c.cl_ordered_statics <- remove name c.cl_ordered_statics;
+									end else begin
+										if name = "new" then c.cl_constructor <- None;
+										c.cl_fields <- PMap.remove name c.cl_fields;
+										c.cl_ordered_fields <- remove name c.cl_ordered_fields;
+									end
+								| _ -> ())
+						end else
+							ignore((!r)())
+					)
+				end
+			end else begin
+				if ((c.cl_extern && not inline) || c.cl_interface) && cf.cf_name <> "__init__" then begin
+					(fun() -> ())
+				end else begin
 					cf.cf_type <- TLazy r;
 					(fun() -> ignore((!r)()))
 				end
-			) in
+			end in
 			f, constr, cf, delay
 		| FProp (get,set,t) ->
 			let ret = load_complex_type ctx p t in