Browse Source

- removed inline removal in DCE
- keepClass is now keep because it works with both classes and packages

Franco Ponticelli 14 years ago
parent
commit
a546153128
2 changed files with 40 additions and 29 deletions
  1. 1 22
      optimizer.ml
  2. 39 7
      std/haxe/macro/Compiler.hx

+ 1 - 22
optimizer.ml

@@ -594,38 +594,17 @@ let reduce_expression ctx e =
 (* ELIMINATE DEAD CODE *)
 
 (*
-	if dead code elimination is on, any class without fields is eliminated from the output. Also inline members
-	are eliminated unless marked as @:keep
+	if dead code elimination is on, any class without fields is eliminated from the output.
 *)
 
 let filter_dead_code com =
 	let s_class c = s_type_path c.cl_path in
-	let s_field c cf = (s_class c) ^ "." ^ cf.cf_name in
-	let keep c cf = (has_meta ":core_api" c.cl_meta) || (has_meta ":keep" c.cl_meta) || (has_meta ":keep" cf.cf_meta) in
-	let remove_inlines c =
-		let remove_inline_fields lst =
-			List.filter(fun cf ->
-				match cf.cf_kind with
-				| Var k when ((k.v_read = AccInline) && (not (keep c cf))) ->
-					if com.verbose then print_endline ("Remove inline var " ^ s_field c cf);
-					false;
-				| Method k when ((k = MethInline) && (not (keep c cf))) ->
-					if com.verbose then print_endline ("Remove inline method " ^ s_field c cf);
-					false;
-				| _ ->
-					true;
-			) lst
-		in
-		c.cl_ordered_statics <- remove_inline_fields c.cl_ordered_statics;
-		c.cl_ordered_fields <- remove_inline_fields c.cl_ordered_fields
-	in
 	com.types <- List.filter (fun t ->
 		match t with
 		| TClassDecl c ->
 			if (c.cl_extern or has_meta ":keep" c.cl_meta) then
 				true
 			else (
-				remove_inlines c;
 				match (c.cl_ordered_statics, c.cl_ordered_fields, c.cl_constructor) with
 				| ([], [], None) ->
 					if com.verbose then print_endline ("Remove class " ^ s_class c);

+ 39 - 7
std/haxe/macro/Compiler.hx

@@ -173,14 +173,46 @@ class Compiler {
 	/**
 		Mark a class (or array of classes) with the metadata @:keep
 	**/
-	public static function keepClass(?cl : String, ?acl : Array<String>)
+	public static function keep(?path : String, ?paths : Array<String>, rec = false)
 	{
-		if (null == acl)
-			acl = [];
-		if (null != cl)
-			acl.push(cl);
-		for(cl in acl)
-			addMetadata("@:keep", cl);
+		if (null == paths)
+			paths = [];
+		if (null != path)
+			paths.push(path);
+		for (path in paths)
+		{
+			for ( p in Context.getClassPath() ) {
+				var p = p + path.split(".").join("/");
+				if (neko.FileSystem.exists(p) && neko.FileSystem.isDirectory(p))
+				{
+					for( file in neko.FileSystem.readDirectory(p) ) {
+						if( StringTools.endsWith(file, ".hx") ) {
+							var module = path + "." + file.substr(0, file.length - 3);
+							var types = Context.getModule(module);
+							for (type in types)
+							{
+								switch(type)
+								{
+									case TInst(cls, _):
+										addMetadata("@:keep", cls.toString());
+									default:
+										//
+								}
+							}
+						} else if( rec && neko.FileSystem.isDirectory(p + "/" + file) )
+							keep(path + "." + file, true);
+					}
+				} else {
+					try
+					{
+						// if it's not a loaded type or a type at all just continue the loop
+						Context.getType(path);
+						addMetadata("@:keep", path);
+						break;
+					} catch(e : Dynamic){}
+				}
+			}			
+		}
 	}
 
 	/**