Browse Source

added Iterable/Array top-down type inference (fixed issue #1650)

Nicolas Cannasse 12 years ago
parent
commit
6047f9cd74
1 changed files with 31 additions and 0 deletions
  1. 31 0
      typer.ml

+ 31 - 0
typer.ml

@@ -102,6 +102,32 @@ let object_field f =
 	let pflen = String.length pf in
 	let pflen = String.length pf in
 	if String.length f >= pflen && String.sub f 0 pflen = pf then String.sub f pflen (String.length f - pflen), false else f, true
 	if String.length f >= pflen && String.sub f 0 pflen = pf then String.sub f pflen (String.length f - pflen), false else f, true
 
 
+let get_iterator_param t =
+	match follow t with
+	| TAnon a ->
+		if !(a.a_status) <> Closed then raise Not_found;
+		(match follow (PMap.find "hasNext" a.a_fields).cf_type, follow (PMap.find "next" a.a_fields).cf_type with
+		| TFun ([],tb), TFun([],t) when (match follow tb with TAbstract ({ a_path = [],"Bool" },[]) -> true | _ -> false) ->
+			if PMap.fold (fun _ acc -> acc + 1) a.a_fields 0 <> 2 then raise Not_found;
+			t
+		| _ ->
+			raise Not_found)
+	| _ ->
+		raise Not_found
+	
+let get_iterable_param t =
+	match follow t with
+	| TAnon a ->
+		if !(a.a_status) <> Closed then raise Not_found;
+		(match follow (PMap.find "iterator" a.a_fields).cf_type with
+		| TFun ([],it) ->
+			let t = get_iterator_param it in
+			if PMap.fold (fun _ acc -> acc + 1) a.a_fields 0 <> 1 then raise Not_found;
+			t
+		| _ ->
+			raise Not_found)
+	| _ -> raise Not_found
+	
 let rec is_pos_infos = function
 let rec is_pos_infos = function
 	| TMono r ->
 	| TMono r ->
 		(match !r with
 		(match !r with
@@ -2454,6 +2480,11 @@ and type_expr ctx (e,p) (with_type:with_type) =
 				(match follow tp with
 				(match follow tp with
 				| TMono _ -> None
 				| TMono _ -> None
 				| _ -> Some tp)
 				| _ -> Some tp)
+			| TAnon _ ->
+				(try
+					Some (get_iterable_param t)
+				with Not_found ->
+					None)
 			| _ ->
 			| _ ->
 				if t == t_dynamic then Some t else None)
 				if t == t_dynamic then Some t else None)
 		| _ ->
 		| _ ->