浏览代码

made call to Codegen.add_field_inits (with explanation why there)

Simon Krajewski 13 年之前
父节点
当前提交
4a8fefbed3
共有 4 个文件被更改,包括 44 次插入41 次删除
  1. 39 32
      codegen.ml
  2. 4 0
      main.ml
  3. 1 5
      tests/unit/MyClass.hx
  4. 0 4
      tests/unit/TestType.hx

+ 39 - 32
codegen.ml

@@ -1291,38 +1291,45 @@ let fix_abstract_inheritance com t =
 (*
 	Adds member field initializations as assignments to the constructor
 *)
-let add_field_inits ctx c =
-	let inits = List.filter (fun cf ->
-		match cf.cf_kind,cf.cf_expr with
-		| Var _, Some _ -> true
-		| _ -> false
-	) c.cl_ordered_fields in
-	match inits with
-	| [] -> ()
-	| _ ->
-		let ethis = mk (TConst TThis) t_dynamic c.cl_pos in
-		let el = List.map (fun cf ->
-			match cf.cf_expr with None -> assert false | Some e ->
-				let lhs = mk (TField(ethis,cf.cf_name)) e.etype e.epos in
-				mk (TBinop(OpAssign,lhs,e)) lhs.etype e.epos
-		) inits in
-		let ct = (TFun([],ctx.basic.tvoid)) in
-		match c.cl_constructor with
-			| None ->
-				let ce = mk (TFunction {
-					tf_args = [];
-					tf_type = ctx.basic.tvoid;
-					tf_expr = mk (TBlock el) ctx.basic.tvoid c.cl_pos;
-				}) ct c.cl_pos in
-				let ctor = mk_field "new" ct c.cl_pos in
-				c.cl_constructor <- Some ({ctor with cf_expr = Some ce});
-			| Some cf ->
-				(match cf.cf_expr with
-				| Some({eexpr = TFunction(f)}) ->
-					let bl = match f.tf_expr with {eexpr = TBlock b } -> b | x -> [x] in
-					let ce = mk (TFunction {f with tf_expr = mk (TBlock (el @ bl)) ctx.basic.tvoid c.cl_pos }) cf.cf_type cf.cf_pos in
-					c.cl_constructor <- Some ({cf with cf_expr = Some ce})
-				| _ -> assert false)
+let add_field_inits ctx =
+	let loop c =
+		let inits = List.filter (fun cf ->
+			match cf.cf_kind,cf.cf_expr with
+			| Var _, Some _ -> true
+			| _ -> false
+		) c.cl_ordered_fields in
+		match inits with
+		| [] -> ()
+		| _ ->
+			let ethis = mk (TConst TThis) t_dynamic c.cl_pos in
+			let el = List.map (fun cf ->
+				match cf.cf_expr with None -> assert false | Some e ->
+					let lhs = mk (TField(ethis,cf.cf_name)) e.etype e.epos in
+					mk (TBinop(OpAssign,lhs,e)) lhs.etype e.epos
+			) inits in
+			let ct = (TFun([],ctx.basic.tvoid)) in
+			match c.cl_constructor with
+				| None ->
+					let ce = mk (TFunction {
+						tf_args = [];
+						tf_type = ctx.basic.tvoid;
+						tf_expr = mk (TBlock el) ctx.basic.tvoid c.cl_pos;
+					}) ct c.cl_pos in
+					let ctor = mk_field "new" ct c.cl_pos in
+					c.cl_constructor <- Some ({ctor with cf_expr = Some ce});
+				| Some cf ->
+					(match cf.cf_expr with
+					| Some({eexpr = TFunction(f)}) ->
+						let bl = match f.tf_expr with {eexpr = TBlock b } -> b | x -> [x] in
+						let ce = mk (TFunction {f with tf_expr = mk (TBlock (el @ bl)) ctx.basic.tvoid c.cl_pos }) cf.cf_type cf.cf_pos in
+						c.cl_constructor <- Some ({cf with cf_expr = Some ce})
+					| _ -> assert false)
+	in
+	List.iter (fun t ->
+		match t with
+		| TClassDecl c -> loop c
+		| _ -> ()
+	) ctx.types
 
 (* -------------------------------------------------------------------------- *)
 (* MISC FEATURES *)

+ 4 - 0
main.ml

@@ -1047,6 +1047,10 @@ try
 			Genxml.generate com file);
 		if com.platform = Flash || com.platform = Cpp then List.iter (Codegen.fix_overrides com) com.types;
 		if Common.defined com "dump" then Codegen.dump_types com;
+		(* At this point the typer did finalize, which means that the delayed add_constructor calls were made and 
+		   DCE has run. There is also no need to let the generated init expressions go through the filters, and they
+		   should not be dumped either because they are just an implementation detail. *)
+		(match com.platform with Php | Flash8 -> () | _ -> Codegen.add_field_inits com);	
 		t();
 		(match com.platform with
 		| _ when !no_output ->

+ 1 - 5
tests/unit/MyClass.hx

@@ -95,8 +95,6 @@ class Ctrv2 extends Ctrv1 {
 	public override function contravariant(arg:Base) { }
 }
 
-#if false
-
 class InitBase {
 	public var i = 2;
 	public var s = "foo";
@@ -128,6 +126,4 @@ class InitProperties {
 	function set_accFunc(v) return throw "setter was called"
 	
 	public function new() { }
-}
-
-#end
+}

+ 0 - 4
tests/unit/TestType.hx

@@ -339,8 +339,6 @@ class TestType extends Test {
 		#end
 	}
 	
-	#if false
-	
 	function testInitFields()
 	{
 		var c = new InitBase();
@@ -370,6 +368,4 @@ class TestType extends Test {
 		eq(c.accDynamic, 3);
 		exc(function() c.accFunc = 4);
 	}
-	
-	#end
 }