瀏覽代碼

add pf_can_skip_non_nullable_argument (closes issue #2039)

Simon Krajewski 12 年之前
父節點
當前提交
691cf17f88
共有 2 個文件被更改,包括 28 次插入11 次删除
  1. 12 0
      common.ml
  2. 16 11
      typer.ml

+ 12 - 0
common.ml

@@ -95,6 +95,8 @@ type platform_config = {
 	pf_overload : bool;
 	(** does the platform generator handle pattern matching *)
 	pf_pattern_matching : bool;
+	(** can the platform use default values for non-nullable arguments *)
+	pf_can_skip_non_nullable_argument : bool;
 }
 
 type context = {
@@ -443,6 +445,7 @@ let default_config =
 		pf_add_final_return = false;
 		pf_overload = false;
 		pf_pattern_matching = false;
+		pf_can_skip_non_nullable_argument = true;
 	}
 
 let get_config com =
@@ -463,6 +466,7 @@ let get_config com =
 			pf_add_final_return = false;
 			pf_overload = false;
 			pf_pattern_matching = false;
+			pf_can_skip_non_nullable_argument = true;
 		}
 	| Js ->
 		{
@@ -477,6 +481,7 @@ let get_config com =
 			pf_add_final_return = false;
 			pf_overload = false;
 			pf_pattern_matching = false;
+			pf_can_skip_non_nullable_argument = true;
 		}
 	| Neko ->
 		{
@@ -491,6 +496,7 @@ let get_config com =
 			pf_add_final_return = false;
 			pf_overload = false;
 			pf_pattern_matching = false;
+			pf_can_skip_non_nullable_argument = true;
 		}
 	| Flash when defined Define.As3 ->
 		{
@@ -505,6 +511,7 @@ let get_config com =
 			pf_add_final_return = true;
 			pf_overload = false;
 			pf_pattern_matching = false;
+			pf_can_skip_non_nullable_argument = false;
 		}
 	| Flash ->
 		{
@@ -519,6 +526,7 @@ let get_config com =
 			pf_add_final_return = false;
 			pf_overload = false;
 			pf_pattern_matching = false;
+			pf_can_skip_non_nullable_argument = false;
 		}
 	| Php ->
 		{
@@ -538,6 +546,7 @@ let get_config com =
 			pf_add_final_return = false;
 			pf_overload = false;
 			pf_pattern_matching = false;
+			pf_can_skip_non_nullable_argument = true;
 		}
 	| Cpp ->
 		{
@@ -552,6 +561,7 @@ let get_config com =
 			pf_add_final_return = true;
 			pf_overload = false;
 			pf_pattern_matching = false;
+			pf_can_skip_non_nullable_argument = true;
 		}
 	| Cs ->
 		{
@@ -566,6 +576,7 @@ let get_config com =
 			pf_add_final_return = false;
 			pf_overload = true;
 			pf_pattern_matching = false;
+			pf_can_skip_non_nullable_argument = true;
 		}
 	| Java ->
 		{
@@ -580,6 +591,7 @@ let get_config com =
 			pf_add_final_return = false;
 			pf_overload = true;
 			pf_pattern_matching = false;
+			pf_can_skip_non_nullable_argument = true;
 		}
 
 let create v args =

+ 16 - 11
typer.ml

@@ -533,13 +533,18 @@ let rec unify_call_params ctx ?(overloads=None) cf el args r p inline =
 		| ({ eexpr = TConst TNull },true) :: l -> no_opt l
 		| l -> l
 	in
-	let rec default_value t =
+	let rec default_value t po =
 		if is_pos_infos t then
 			let infos = mk_infos ctx p [] in
 			let e = type_expr ctx infos (WithType t) in
 			(e, true)
-		else
+		else begin
+			if not ctx.com.config.pf_can_skip_non_nullable_argument then begin match po with
+				| Some (name,p) when not (is_nullable t) -> display_error ctx ("Cannot skip non-nullable argument " ^ name) p
+				| _ -> ()
+			end;
 			(null (ctx.t.tnull t) p, true)
+		end
 	in
 	let rec loop acc l l2 skip =
 		match l , l2 with
@@ -558,9 +563,9 @@ let rec unify_call_params ctx ?(overloads=None) cf el args r p inline =
 			else
 				List.map fst args, tf
 		| [] , (_,false,_) :: _ ->
-			error (List.fold_left (fun acc (_,_,t) -> default_value t :: acc) acc l2) "Not enough"
+			error (List.fold_left (fun acc (_,_,t) -> default_value t None :: acc) acc l2) "Not enough"
 		| [] , (name,true,t) :: l ->
-			loop (default_value t :: acc) [] l skip
+			loop (default_value t None :: acc) [] l skip
 		| _ , [] ->
 			(match List.rev skip with
 			| [] -> error acc "Too many"
@@ -574,7 +579,7 @@ let rec unify_call_params ctx ?(overloads=None) cf el args r p inline =
 			with
 				WithTypeError (ul,p) ->
 					if opt then
-						loop (default_value t :: acc) (ee :: l) l2 ((name,ul) :: skip)
+						loop (default_value t (Some (name,p)) :: acc) (ee :: l) l2 ((name,ul) :: skip)
 					else
 						arg_error ul name false p
 	in
@@ -1275,8 +1280,8 @@ let type_bind ctx (e : texpr) params p =
 					ordered_args
 			in
 			loop args [] given_args missing_args a
-		| (n,o,t) :: _ , (EConst(Ident "_"),p) :: _ when ctx.com.platform = Flash && o && not (is_nullable t) ->
-			error "Usage of _ is currently not supported for optional non-nullable arguments on flash9" p
+		| (n,o,t) :: _ , (EConst(Ident "_"),p) :: _ when not ctx.com.config.pf_can_skip_non_nullable_argument && o && not (is_nullable t) ->
+			error "Usage of _ is not supported for optional non-nullable arguments" p
 		| (n,o,t) :: args , ([] as params)
 		| (n,o,t) :: args , (EConst(Ident "_"),_) :: params ->
 			let v = alloc_var (alloc_name n) (if o then ctx.t.tnull t else t) in
@@ -2609,7 +2614,7 @@ and type_expr ctx (e,p) (with_type:with_type) =
 				wrap (Codegen.PatternMatchConversion.to_typed_ast ctx dt p)
 		with Exit ->
 			type_switch_old ctx e1 cases def with_type p
-		end	
+		end
 	| EReturn e ->
 		let e , t = (match e with
 			| None ->
@@ -2671,10 +2676,10 @@ and type_expr ctx (e,p) (with_type:with_type) =
 			let restore = fun () ->
 				ctx.m.module_types <- List.tl ctx.m.module_types;
 				ctx.on_error <- old;
-			in	
+			in
 			ctx.on_error <- (fun ctx msg ep ->
 				raise Not_found;
-			);			
+			);
 			begin try
 				let e = type_call ctx e el with_type p in
 				restore();
@@ -3142,7 +3147,7 @@ and build_call ctx acc el (with_type:with_type) p =
 					| _ -> assert false
 					end
 				| _ -> assert false
-			in		
+			in
 			make_call ctx et (eparam :: params) r p
 		end
 	| AKMacro (ethis,f) ->