Browse Source

[hlc] sort types by names and functions by file position

ncannasse 6 years ago
parent
commit
0005f710fd
1 changed files with 16 additions and 3 deletions
  1. 16 3
      src/generators/hl2c.ml

+ 16 - 3
src/generators/hl2c.ml

@@ -1335,6 +1335,13 @@ let make_modules ctx all_types =
 let generate_module_types ctx m =
 let generate_module_types ctx m =
 	let def_name = "INC_" ^ String.concat "__" (ExtString.String.nsplit m.m_name "/") in
 	let def_name = "INC_" ^ String.concat "__" (ExtString.String.nsplit m.m_name "/") in
 	let line = line ctx and expr = expr ctx and sexpr fmt = Printf.ksprintf (expr ctx) fmt in
 	let line = line ctx and expr = expr ctx and sexpr fmt = Printf.ksprintf (expr ctx) fmt in
+	let type_name t =
+		match t with
+		| HObj o | HStruct o -> o.pname
+		| HEnum e -> e.ename
+		| _ -> ""
+	in
+	let types = List.sort (fun t1 t2 -> compare (type_name t1) (type_name t2)) m.m_types in
 	define ctx (sprintf "#ifndef %s" def_name);
 	define ctx (sprintf "#ifndef %s" def_name);
 	define ctx (sprintf "#define %s" def_name);
 	define ctx (sprintf "#define %s" def_name);
 	List.iter (fun t ->
 	List.iter (fun t ->
@@ -1344,7 +1351,7 @@ let generate_module_types ctx m =
 			ctx.defined_types <- PMap.add t () ctx.defined_types;
 			ctx.defined_types <- PMap.add t () ctx.defined_types;
 			define ctx (sprintf "typedef struct _%s *%s;" name name);
 			define ctx (sprintf "typedef struct _%s *%s;" name name);
 		| _ -> ()
 		| _ -> ()
-	) m.m_types;
+	) types;
 	line "";
 	line "";
 	List.iter (fun t ->
 	List.iter (fun t ->
 		match t with
 		match t with
@@ -1388,7 +1395,7 @@ let generate_module_types ctx m =
 			) e.efields
 			) e.efields
 		| _ ->
 		| _ ->
 			()
 			()
-	) m.m_types;
+	) types;
 	line "#endif";
 	line "#endif";
 	line ""
 	line ""
 
 
@@ -1695,7 +1702,13 @@ let write_c com file (code:code) gnames =
 			define ctx "#define HLC_BOOT";
 			define ctx "#define HLC_BOOT";
 			define ctx "#include <hlc.h>";
 			define ctx "#include <hlc.h>";
 			if m.m_types <> [] then define ctx (sprintf "#include <%s.h>" m.m_name);
 			if m.m_types <> [] then define ctx (sprintf "#include <%s.h>" m.m_name);
-			List.iter (fun fe -> match fe.fe_decl with None -> () | Some f -> generate_function ctx f) m.m_functions;
+			let file_pos f =
+				match f.fe_decl with
+				| Some f when Array.length f.debug > 0 -> f.debug.(Array.length f.debug - 1)
+				| _ -> (0,0)
+			in
+			let funcs = List.sort (fun f1 f2 -> compare (file_pos f1) (file_pos f2)) m.m_functions in
+			List.iter (fun fe -> match fe.fe_decl with None -> () | Some f -> generate_function ctx f) funcs;
 		end;
 		end;
 	) modules;
 	) modules;