Browse Source

promote return into complex right expressions (closes #1912)

Simon Krajewski 12 years ago
parent
commit
6ee285bb7c
2 changed files with 25 additions and 0 deletions
  1. 24 0
      codegen.ml
  2. 1 0
      main.ml

+ 24 - 0
codegen.ml

@@ -1666,6 +1666,30 @@ module PatternMatchConversion = struct
 		end
 		end
 end
 end
 
 
+let promote_complex_returns ctx e =
+	let rec loop t e = match e.eexpr with
+		| TBlock(el) ->
+			begin match List.rev el with
+				| elast :: el -> {e with eexpr = TBlock(List.rev ((loop t elast) :: el))}
+				| [] -> e
+			end
+		| TSwitch(es,cases,edef) ->
+			{e with eexpr = TSwitch(es,List.map (fun (el,e) -> el,loop t e) cases,match edef with None -> None | Some e -> Some (loop t e))}
+		| TIf(eif,ethen,eelse) ->
+			{e with eexpr = TIf(eif, loop t ethen, match eelse with None -> None | Some e -> Some (loop t e))}
+		| TTry(e1,el) ->
+			{e with eexpr = TTry(loop t e1, List.map (fun (el,e) -> el,loop t e) el)}
+		| TReturn _ ->
+			e
+		| _ ->
+			mk (TReturn (Some e)) t e.epos
+	in
+	let rec find e = match e.eexpr with
+		| TReturn (Some e1) -> loop e.etype e1
+		| _ -> Type.map_expr find e
+	in
+	find e
+
 (* -------------------------------------------------------------------------- *)
 (* -------------------------------------------------------------------------- *)
 (* USAGE *)
 (* USAGE *)
 
 

+ 1 - 0
main.ml

@@ -1218,6 +1218,7 @@ try
 		) com.types;
 		) com.types;
 		let filters = [
 		let filters = [
 			Codegen.Abstract.handle_abstract_casts tctx;
 			Codegen.Abstract.handle_abstract_casts tctx;
+			Codegen.promote_complex_returns com;
 			if com.foptimize then (fun e -> Optimizer.reduce_expression tctx (Optimizer.inline_constructors tctx e)) else Optimizer.sanitize tctx;
 			if com.foptimize then (fun e -> Optimizer.reduce_expression tctx (Optimizer.inline_constructors tctx e)) else Optimizer.sanitize tctx;
 			Codegen.check_local_vars_init;
 			Codegen.check_local_vars_init;
 			Codegen.captured_vars com;
 			Codegen.captured_vars com;