浏览代码

[hxb] fix enum.e_type

Rudy Ges 2 年之前
父节点
当前提交
76846c2932
共有 2 个文件被更改,包括 48 次插入42 次删除
  1. 20 14
      src/compiler/hxb/hxbReader.ml
  2. 28 28
      src/compiler/hxb/hxbWriter.ml

+ 20 - 14
src/compiler/hxb/hxbReader.ml

@@ -592,11 +592,9 @@ class hxb_reader
 			TEnum(self#read_enum_ref,[])
 		| 12 ->
 			begin match self#read_u8 with
-				| 0 -> TType(class_module_type self#read_class_ref,[])
-				| 1 ->
-					let e = self#read_enum_ref in
-					TType(enum_module_type e.e_module e.e_path e.e_pos,[])
-				| 2 -> TType(abstract_module_type self#read_abstract_ref [],[])
+				(* TODO wrap those two in TType? *)
+				| 0 -> mk_anon (ref Closed)
+				| 1 -> TAnon self#read_anon_ref
 				| _ -> TType(self#read_typedef_ref,[])
 			end
 		| 13 ->
@@ -1270,16 +1268,20 @@ class hxb_reader
 	method read_annd (m : module_def) =
 		let l = self#read_uleb128 in
 		for i = 0 to l - 1 do
-			let tname = self#read_string in
-			match List.find_opt (fun t -> snd (t_path t) = tname) m.m_types with
+			let tname = self#read_option (fun () -> self#read_string) in
+			match tname with
 			| None -> ()
-			| Some parent ->
-				begin match parent with
-				| TClassDecl c -> type_type_parameters <- Array.of_list c.cl_params;
-				| TEnumDecl en -> type_type_parameters <- Array.of_list en.e_params;
-				| TTypeDecl td -> type_type_parameters <- Array.of_list td.t_params;
-				| TAbstractDecl a -> type_type_parameters <- Array.of_list a.a_params;
-				end;
+			| Some tname ->
+				(match List.find_opt (fun t -> snd (t_path t) = tname) m.m_types with
+				| None -> ()
+				| Some parent ->
+					begin match parent with
+					| TClassDecl c -> type_type_parameters <- Array.of_list c.cl_params;
+					| TEnumDecl en -> type_type_parameters <- Array.of_list en.e_params;
+					| TTypeDecl td -> type_type_parameters <- Array.of_list td.t_params;
+					| TAbstractDecl a -> type_type_parameters <- Array.of_list a.a_params;
+					end
+				);
 
 			let an = anons.(i) in
 			let read_fields () =
@@ -1418,6 +1420,10 @@ class hxb_reader
 				TClassDecl c
 			| 1 ->
 				let en = mk_enum m path pos name_pos in
+				(match self#read_u8 with
+				| 0 -> en.e_type.t_type <- (mk_anon (ref Closed))
+				| _ -> en.e_type.t_type <- TAnon self#read_anon_ref);
+
 				enums <- Array.append enums (Array.make 1 en);
 
 				let read_field () =

+ 28 - 28
src/compiler/hxb/hxbWriter.ml

@@ -226,7 +226,7 @@ class ['a] hxb_writer
 	val own_typedefs = new pool
 
 	val type_param_lut = new pool
-	val mutable ttp_key = ([],"")
+	val mutable ttp_key = None
 	val mutable type_type_parameters = new pool
 	val mutable field_type_parameters = new pool
 
@@ -313,7 +313,6 @@ class ['a] hxb_writer
 			self#write_type_instance t;
 		in
 		match t with
-		(* TODO: we might need to properly restore monomorphs... *)
 		| TMono r ->
 			begin match r.tm_type with
 			| None ->
@@ -343,23 +342,13 @@ class ['a] hxb_writer
 		| TType(td,[]) ->
 			chunk#write_byte 12;
 			begin match td.t_type with
+				| TAnon an when PMap.is_empty an.a_fields ->
+					chunk#write_byte 0;
 				| TAnon an ->
-					begin match !(an.a_status) with
-						| Statics c ->
-							chunk#write_byte 0;
-							self#write_class_ref c;
-						| EnumStatics en ->
-							chunk#write_byte 1;
-							self#write_enum_ref en;
-						| AbstractStatics a ->
-							chunk#write_byte 2;
-							self#write_abstract_ref a;
-						| _ ->
-							chunk#write_byte 3;
-							self#write_typedef_ref td;
-					end
+					chunk#write_byte 1;
+					self#write_anon_ref an;
 				| _ ->
-					chunk#write_byte 3;
+					chunk#write_byte 2;
 					self#write_typedef_ref td;
 			end;
 		| TAbstract(a,[]) ->
@@ -1087,7 +1076,7 @@ class ['a] hxb_writer
 
 	method select_type (path : path) =
 		(* Printf.eprintf "Select type %s\n" (s_type_path path); *)
-		ttp_key <- path;
+		ttp_key <- Some path;
 		type_type_parameters <- type_param_lut#extract path
 
 	method write_common_module_type (infos : tinfos) : unit =
@@ -1207,9 +1196,11 @@ class ['a] hxb_writer
 		self#write_common_module_type (Obj.magic td);
 		self#write_type_instance td.t_type;
 
-	method write_anon (m : module_def) ((an : tanon), (ttp_key : path)) =
-		chunk#write_string (snd ttp_key);
-		self#select_type ttp_key;
+	method write_anon (m : module_def) ((an : tanon), (ttp_key : path option)) =
+		chunk#write_option ttp_key (fun (_,k) -> chunk#write_string k);
+		match ttp_key with
+		| None -> ()
+		| Some ttp_key -> self#select_type ttp_key;
 
 		let write_fields () =
 			chunk#write_list (PMap.foldi (fun s f acc -> (s,f) :: acc) an.a_fields []) (fun (_,cf) ->
@@ -1233,6 +1224,7 @@ class ['a] hxb_writer
 			self#write_class_ref c;
 		| EnumStatics en ->
 			chunk#write_byte 4;
+			self#write_enum_ref en;
 			write_fields ()
 		| AbstractStatics a ->
 			chunk#write_byte 5;
@@ -1294,13 +1286,21 @@ class ['a] hxb_writer
 			chunk#write_list c.cl_ordered_fields write_field;
 			chunk#write_list c.cl_ordered_statics write_field;
 		| TEnumDecl e ->
-				chunk#write_list (PMap.foldi (fun s f acc -> (s,f) :: acc) e.e_constrs []) (fun (s,ef) ->
-					(* Printf.eprintf "  forward declare enum field %s.%s\n" (s_type_path e.e_path) s; *)
-					chunk#write_string s;
-					self#write_pos ef.ef_pos;
-					self#write_pos ef.ef_name_pos;
-					chunk#write_byte ef.ef_index
-				);
+			(match e.e_type.t_type with
+			| TAnon an when PMap.is_empty an.a_fields ->
+				chunk#write_byte 0;
+			| TAnon an ->
+				chunk#write_byte 1;
+				self#write_anon_ref an;
+			| _ -> assert false);
+
+			chunk#write_list (PMap.foldi (fun s f acc -> (s,f) :: acc) e.e_constrs []) (fun (s,ef) ->
+				(* Printf.eprintf "  forward declare enum field %s.%s\n" (s_type_path e.e_path) s; *)
+				chunk#write_string s;
+				self#write_pos ef.ef_pos;
+				self#write_pos ef.ef_name_pos;
+				chunk#write_byte ef.ef_index
+			);
 		| TAbstractDecl a ->
 			(* TODO ? *)
 			()