Dan Korostelev 4 سال پیش
والد
کامیت
1cbd848436
4فایلهای تغییر یافته به همراه47 افزوده شده و 12 حذف شده
  1. 1 0
      src/context/common.ml
  2. 1 0
      src/core/tType.ml
  3. 39 0
      src/filters/filters.ml
  4. 6 12
      src/typing/typer.ml

+ 1 - 0
src/context/common.ml

@@ -710,6 +710,7 @@ let create version s_version args =
 			tnull = (fun _ -> die "" __LOC__);
 			tstring = m;
 			tarray = (fun _ -> die "" __LOC__);
+			evoid = (fun _ -> die "" __LOC__);
 		};
 		file_lookup_cache = Hashtbl.create 0;
 		file_keys = new file_keys;

+ 1 - 0
src/core/tType.ml

@@ -371,6 +371,7 @@ type basic_types = {
 	mutable tnull : t -> t;
 	mutable tstring : t;
 	mutable tarray : t -> t;
+	mutable evoid : pos -> texpr;
 }
 
 type class_field_scope =

+ 39 - 0
src/filters/filters.ml

@@ -249,6 +249,44 @@ let check_unification ctx e t =
 	end;
 	e
 
+let is_evoid e = match e.eexpr with
+	| TTypeExpr (TAbstractDecl { a_path = [],"Void" }) -> true
+	| _ -> false
+
+let eliminate_void ctx e =
+	let is_void t = ExtType.is_void (follow t) in
+	let evoid = ctx.com.basic.evoid in
+	let value e =
+		if is_void e.etype && not (is_evoid e) then
+			let el = [ e; evoid e.epos ] in
+			{ e with eexpr = TMeta ((Meta.MergeBlock,[],null_pos), { e with eexpr = TBlock el }) }
+		else
+			e
+	in
+	let rec loop e =
+		match e.eexpr with
+		| TLocal v when is_void v.v_type ->
+			evoid e.epos
+		| TVar (v,eo) when is_void v.v_type ->
+			begin match eo with
+			| Some e -> loop e
+			| None -> evoid e.epos (* TODO: make fix_void return None/Some? *)
+			end
+		| TCall (eobj, args) ->
+			let eobj = loop eobj in
+			let args = List.map (fun e -> value (loop e)) args in
+			{ e with eexpr = TCall (eobj, args)}
+		| TBlock el ->
+			let el = ExtList.List.filter_map (fun e ->
+				let e = loop e in
+				if is_evoid e then None else Some e (* TODO: check last element to avoid work? *)
+			) el in
+			{ e with eexpr = TBlock el }
+		| _ ->
+			Type.map_expr loop e
+	in
+	loop e
+
 let rec fix_return_dynamic_from_void_function ctx return_is_void e =
 	match e.eexpr with
 	| TFunction fn ->
@@ -730,6 +768,7 @@ let run com tctx main =
 	] in
 	List.iter (run_expression_filters (timer_label detail_times ["expr 0"]) tctx filters) new_types;
 	let filters = [
+		"eliminate_void",eliminate_void tctx;
 		"fix_return_dynamic_from_void_function",fix_return_dynamic_from_void_function tctx true;
 		"check_local_vars_init",check_local_vars_init tctx.com;
 		"check_abstract_as_value",check_abstract_as_value;

+ 6 - 12
src/typing/typer.ml

@@ -1553,17 +1553,7 @@ and type_call_target ctx e el with_type inline p =
 and type_call ?(mode=MGet) ctx e el (with_type:WithType.t) inline p =
 	let def () =
 		let e = type_call_target ctx e el with_type inline p in
-		let ecall = build_call ~mode ctx e el with_type p in
-		(* TEMPORARY HACK *) begin
-			match ctx.com.platform with
-			| Js | Eval -> ecall
-			| _ ->
-				if ExtType.is_void (follow ecall.etype) && with_type <> NoValue then
-					let enull = mk (TTypeExpr (match ctx.com.basic.tvoid with TAbstract (a,_) -> TAbstractDecl a | _ -> assert false)) ctx.com.basic.tvoid p in
-					{ ecall with eexpr = TBlock [ecall; enull] }
-				else
-					ecall
-		(* TEMPORARY HACK *) end
+		build_call ~mode ctx e el with_type p
 	in
 	match e, el with
 	| (EConst (Ident "trace"),p) , e :: el ->
@@ -1917,7 +1907,11 @@ let rec create com =
 		match t with
 		| TAbstractDecl a ->
 			(match snd a.a_path with
-			| "Void" -> ctx.t.tvoid <- TAbstract (a,[]);
+			| "Void" ->
+				let typeexpr = TTypeExpr t in
+				let tvoid = TAbstract (a,[]) in
+				ctx.t.tvoid <- tvoid;
+				ctx.t.evoid <- mk typeexpr tvoid;
 			| "Float" -> ctx.t.tfloat <- TAbstract (a,[]);
 			| "Int" -> ctx.t.tint <- TAbstract (a,[])
 			| "Bool" -> ctx.t.tbool <- TAbstract (a,[])