genxml.ml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. (*
  2. * Copyright (C)2005-2013 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *)
  22. open Ast
  23. open Type
  24. open Common
  25. type xml =
  26. | Node of string * (string * string) list * xml list
  27. | PCData of string
  28. | CData of string
  29. let tag name = Node (name,[],[])
  30. let xml name att = Node (name,att,[])
  31. let node name att childs = Node (name,att,childs)
  32. let pcdata s = PCData s
  33. let cdata s = CData s
  34. let pmap f m =
  35. PMap.fold (fun x acc -> f x :: acc) m []
  36. let gen_path (p,n) priv =
  37. ("path",String.concat "." (p @ [n]))
  38. let gen_string s =
  39. if String.contains s '<' || String.contains s '>' || String.contains s '&' then cdata s else pcdata s
  40. let gen_doc s =
  41. (* remove trailing space and convert newlines *)
  42. let s = ExtString.String.strip s in
  43. let s = String.concat "\n" (ExtString.String.nsplit (String.concat "\n" (ExtString.String.nsplit s "\r\n")) "\r") in
  44. node "haxe_doc" [] [gen_string s]
  45. let gen_doc_opt d =
  46. match d with
  47. | None -> []
  48. | Some s -> [gen_doc s]
  49. let gen_arg_name (name,opt,_) =
  50. (if opt then "?" else "") ^ name
  51. let real_path path meta =
  52. let rec loop = function
  53. | [] -> path
  54. | (Meta.RealPath,[(Ast.EConst (Ast.String s),_)],_) :: _ -> parse_path s
  55. | _ :: l -> loop l
  56. in
  57. loop meta
  58. let tpath t =
  59. let i = t_infos t in
  60. real_path i.mt_path i.mt_meta
  61. let rec follow_param t =
  62. match t with
  63. | TMono r ->
  64. (match !r with
  65. | Some t -> follow_param t
  66. | _ -> t)
  67. | TType ({ t_path = [],"Null" } as t,tl) ->
  68. follow_param (apply_params t.t_types tl t.t_type)
  69. | _ ->
  70. t
  71. let rec sexpr (e,_) =
  72. match e with
  73. | EConst c -> s_constant c
  74. | EParenthesis e -> "(" ^ (sexpr e) ^ ")"
  75. | EArrayDecl el -> "[" ^ (String.concat "," (List.map sexpr el)) ^ "]"
  76. | EObjectDecl fl -> "{" ^ (String.concat "," (List.map (fun (n,e) -> n ^ ":" ^ (sexpr e)) fl)) ^ "}"
  77. | _ -> "'???'"
  78. let gen_meta meta =
  79. let meta = List.filter (fun (m,_,_) -> match m with Meta.Used | Meta.MaybeUsed | Meta.RealPath -> false | _ -> true) meta in
  80. match meta with
  81. | [] -> []
  82. | _ ->
  83. let nodes = List.map (fun (m,el,_) ->
  84. node "m" ["n",fst (MetaInfo.to_string m)] (List.map (fun e -> node "e" [] [gen_string (sexpr e)]) el)
  85. ) meta in
  86. [node "meta" [] nodes]
  87. let rec gen_type t =
  88. match t with
  89. | TMono m -> (match !m with None -> tag "unknown" | Some t -> gen_type t)
  90. | TEnum (e,params) -> gen_type_decl "e" (TEnumDecl e) params
  91. | TInst (c,params) -> gen_type_decl "c" (TClassDecl c) params
  92. | TAbstract (a,params) -> gen_type_decl "x" (TAbstractDecl a) params
  93. | TType (t,params) -> gen_type_decl "t" (TTypeDecl t) params
  94. | TFun (args,r) -> node "f" ["a",String.concat ":" (List.map gen_arg_name args)] (List.map gen_type (List.map (fun (_,opt,t) -> if opt then follow_param t else t) args @ [r]))
  95. | TAnon a -> node "a" [] (pmap (fun f -> gen_field [] { f with cf_public = false }) a.a_fields)
  96. | TDynamic t2 -> node "d" [] (if t == t2 then [] else [gen_type t2])
  97. | TLazy f -> gen_type (!f())
  98. and gen_type_decl n t pl =
  99. let i = t_infos t in
  100. node n [gen_path (tpath t) i.mt_private] (List.map gen_type pl)
  101. and gen_field att f =
  102. let add_get_set acc name att =
  103. match acc with
  104. | AccNormal | AccResolve | AccRequire _ -> att
  105. | AccNo | AccNever -> (name, "null") :: att
  106. | AccCall -> (name,"accessor") :: att
  107. | AccInline -> (name,"inline") :: att
  108. in
  109. let att = (match f.cf_expr with None -> att | Some e -> ("line",string_of_int (Lexer.get_error_line e.epos)) :: att) in
  110. let att = (match f.cf_kind with
  111. | Var v -> add_get_set v.v_read "get" (add_get_set v.v_write "set" att)
  112. | Method m ->
  113. (match m with
  114. | MethNormal | MethMacro -> ("set", "method") :: att
  115. | MethDynamic -> ("set", "dynamic") :: att
  116. | MethInline -> ("get", "inline") :: ("set","null") :: att)
  117. ) in
  118. let att = (match f.cf_params with [] -> att | l -> ("params", String.concat ":" (List.map (fun (n,_) -> n) l)) :: att) in
  119. node f.cf_name (if f.cf_public then ("public","1") :: att else att) (gen_type f.cf_type :: gen_meta f.cf_meta @ gen_doc_opt f.cf_doc)
  120. let gen_constr e =
  121. let doc = gen_doc_opt e.ef_doc in
  122. let args, t = (match follow e.ef_type with
  123. | TFun (args,_) ->
  124. ["a",String.concat ":" (List.map gen_arg_name args)] ,
  125. List.map (fun (_,opt,t) -> gen_type (if opt then follow_param t else t)) args @ doc
  126. | _ ->
  127. [] , doc
  128. ) in
  129. node e.ef_name args t
  130. let gen_type_params ipos priv path params pos m =
  131. let mpriv = (if priv then [("private","1")] else []) in
  132. let mpath = (if m.m_path <> path then [("module",snd (gen_path m.m_path false))] else []) in
  133. let file = (if ipos && pos <> null_pos then [("file",pos.pfile)] else []) in
  134. gen_path path priv :: ("params", String.concat ":" (List.map fst params)) :: (file @ mpriv @ mpath)
  135. let gen_class_path name (c,pl) =
  136. node name [("path",s_type_path (tpath (TClassDecl c)))] (List.map gen_type pl)
  137. let rec exists f c =
  138. PMap.exists f.cf_name c.cl_fields ||
  139. match c.cl_super with
  140. | None -> false
  141. | Some (csup,_) -> exists f csup
  142. let gen_type_decl com pos t =
  143. let m = (t_infos t).mt_module in
  144. match t with
  145. | TClassDecl c ->
  146. let stats = List.map (gen_field ["static","1"]) (List.filter (fun cf -> cf.cf_name <> "__meta__") c.cl_ordered_statics) in
  147. let fields = (match c.cl_super with
  148. | None -> List.map (fun f -> f,[]) c.cl_ordered_fields
  149. | Some (csup,_) -> List.map (fun f -> if exists f csup then (f,["override","1"]) else (f,[])) c.cl_ordered_fields
  150. ) in
  151. let fields = List.map (fun (f,att) -> gen_field att f) fields in
  152. let constr = (match c.cl_constructor with None -> [] | Some f -> [gen_field [] f]) in
  153. let impl = List.map (gen_class_path (if c.cl_interface then "extends" else "implements")) c.cl_implements in
  154. let tree = (match c.cl_super with
  155. | None -> impl
  156. | Some x -> gen_class_path "extends" x :: impl
  157. ) in
  158. let doc = gen_doc_opt c.cl_doc in
  159. let meta = gen_meta c.cl_meta in
  160. let ext = (if c.cl_extern then [("extern","1")] else []) in
  161. let interf = (if c.cl_interface then [("interface","1")] else []) in
  162. let dynamic = (match c.cl_dynamic with
  163. | None -> []
  164. | Some t -> [node "haxe_dynamic" [] [gen_type t]]
  165. ) in
  166. node "class" (gen_type_params pos c.cl_private (tpath t) c.cl_types c.cl_pos m @ ext @ interf) (tree @ stats @ fields @ constr @ doc @ meta @ dynamic)
  167. | TEnumDecl e ->
  168. let doc = gen_doc_opt e.e_doc in
  169. let meta = gen_meta e.e_meta in
  170. node "enum" (gen_type_params pos e.e_private (tpath t) e.e_types e.e_pos m) (pmap gen_constr e.e_constrs @ doc @ meta)
  171. | TTypeDecl t ->
  172. let doc = gen_doc_opt t.t_doc in
  173. let meta = gen_meta t.t_meta in
  174. let tt = gen_type t.t_type in
  175. node "typedef" (gen_type_params pos t.t_private t.t_path t.t_types t.t_pos m) (tt :: doc @ meta)
  176. | TAbstractDecl a ->
  177. let doc = gen_doc_opt a.a_doc in
  178. let meta = gen_meta a.a_meta in
  179. let sub = (match a.a_from with [] -> [] | l -> [node "from" [] (List.map (fun (t,_) -> gen_type t) l)]) in
  180. let super = (match a.a_to with [] -> [] | l -> [node "to" [] (List.map (fun (t,_) -> gen_type t) l)]) in
  181. node "abstract" (gen_type_params pos a.a_private (tpath t) a.a_types a.a_pos m) (sub @ super @ doc @ meta)
  182. let att_str att =
  183. String.concat "" (List.map (fun (a,v) -> Printf.sprintf " %s=\"%s\"" a v) att)
  184. let rec write_xml ch tabs x =
  185. match x with
  186. | Node (name,att,[]) ->
  187. IO.printf ch "%s<%s%s/>" tabs name (att_str att)
  188. | Node (name,att,[x]) ->
  189. IO.printf ch "%s<%s%s>" tabs name (att_str att);
  190. write_xml ch "" x;
  191. IO.printf ch "</%s>" name;
  192. | Node (name,att,childs) ->
  193. IO.printf ch "%s<%s%s>\n" tabs name (att_str att);
  194. List.iter (fun x ->
  195. write_xml ch (tabs ^ "\t") x;
  196. IO.printf ch "\n";
  197. ) childs;
  198. IO.printf ch "%s</%s>" tabs name
  199. | PCData s ->
  200. IO.printf ch "%s" s
  201. | CData s ->
  202. IO.printf ch "<![CDATA[%s]]>" s
  203. let generate com file =
  204. let t = Common.timer "construct xml" in
  205. let x = node "haxe" [] (List.map (gen_type_decl com true) (List.filter (fun t -> not (Meta.has Meta.NoDoc (t_infos t).mt_meta)) com.types)) in
  206. t();
  207. let t = Common.timer "write xml" in
  208. let ch = IO.output_channel (open_out_bin file) in
  209. write_xml ch "" x;
  210. IO.close_out ch;
  211. t()
  212. let gen_type_string ctx t =
  213. let x = gen_type_decl ctx false t in
  214. let ch = IO.output_string() in
  215. write_xml ch "" x;
  216. IO.close_out ch
  217. (* -------------------------------------------------------------------------- *)
  218. (* PRINT HX FROM TYPE *)
  219. let rec create_dir acc = function
  220. | [] -> ()
  221. | d :: l ->
  222. let path = acc ^ "/" ^ d in
  223. (try Unix.mkdir path 0o777 with _ -> ());
  224. create_dir path l
  225. let conv_path p =
  226. match List.rev (fst p) with
  227. | x :: l when x.[0] = '_' -> List.rev (("priv" ^ x) :: l), snd p
  228. | _ -> p
  229. let generate_type com t =
  230. let base_path = "hxclasses" in
  231. let pack , name = conv_path (t_path t) in
  232. create_dir "." (base_path :: pack);
  233. match pack, name with
  234. | ["flash";"net"], "NetStreamPlayTransitions"
  235. | ["flash";"filters"], "BitmapFilterQuality"
  236. | ["flash";"display"], ("BitmapDataChannel" | "GraphicsPathCommand") -> ()
  237. | _ ->
  238. let f = open_out_bin (base_path ^ "/" ^ (match pack with [] -> "" | l -> String.concat "/" l ^ "/") ^ name ^ ".hx") in
  239. let ch = IO.output_channel f in
  240. let p fmt = IO.printf ch fmt in
  241. if pack <> [] then IO.printf ch "package %s;\n\n" (String.concat "." pack);
  242. let rec notnull t =
  243. match t with
  244. | TMono r ->
  245. (match !r with
  246. | None -> t
  247. | Some t -> notnull t)
  248. | TLazy f ->
  249. notnull ((!f)())
  250. | TType ({ t_path = [],"Null" },[t]) ->
  251. t
  252. | _ ->
  253. t
  254. in
  255. let rec path p tl =
  256. let p = conv_path p in
  257. (if fst p = pack then snd p else s_type_path p) ^ (match tl with [] -> "" | _ -> "<" ^ String.concat "," (List.map stype tl) ^ ">")
  258. and stype t =
  259. match t with
  260. | TMono r ->
  261. (match !r with
  262. | None -> "Unknown"
  263. | Some t -> stype t)
  264. | TInst ({ cl_kind = KTypeParameter _ } as c,tl) ->
  265. path ([],snd c.cl_path) tl
  266. | TInst (c,tl) ->
  267. path c.cl_path tl
  268. | TEnum (e,tl) ->
  269. path e.e_path tl
  270. | TType (t,tl) ->
  271. path t.t_path tl
  272. | TAbstract (a,tl) ->
  273. path a.a_path tl
  274. | TAnon a ->
  275. let fields = PMap.fold (fun f acc -> (f.cf_name ^ " : " ^ stype f.cf_type) :: acc) a.a_fields [] in
  276. "{" ^ String.concat ", " fields ^ "}"
  277. | TLazy f ->
  278. stype ((!f)())
  279. | TDynamic t2 ->
  280. if t == t2 then "Dynamic" else "Dynamic<" ^ stype t2 ^ ">"
  281. | TFun ([],ret) ->
  282. "Void -> " ^ ftype ret
  283. | TFun (args,ret) ->
  284. String.concat " -> " (List.map (fun (_,_,t) -> ftype t) args) ^ " -> " ^ ftype ret
  285. and ftype t =
  286. match t with
  287. | TMono r ->
  288. (match !r with
  289. | None -> stype t
  290. | Some t -> ftype t)
  291. | TLazy f ->
  292. ftype ((!f)())
  293. | TFun _ ->
  294. "(" ^ stype t ^ ")"
  295. | _ ->
  296. stype t
  297. in
  298. let sparam (n,v,t) =
  299. match v with
  300. | None ->
  301. n ^ " : " ^ stype t
  302. | Some (Ident "null") ->
  303. "?" ^ n ^ " : " ^ stype (notnull t)
  304. | Some v ->
  305. n ^ " : " ^ stype t ^ " = " ^ (match s_constant v with "nan" -> "0./*NaN*/" | v -> v)
  306. in
  307. let print_meta ml =
  308. List.iter (fun (m,pl,_) ->
  309. match m with
  310. | Meta.DefParam | Meta.CoreApi | Meta.Used | Meta.MaybeUsed -> ()
  311. | _ ->
  312. match pl with
  313. | [] -> p "@%s " (fst (MetaInfo.to_string m))
  314. | l -> p "@%s(%s) " (fst (MetaInfo.to_string m)) (String.concat "," (List.map sexpr pl))
  315. ) ml
  316. in
  317. let access a =
  318. match a, pack with
  319. | AccNever, "flash" :: _ -> "null"
  320. | _ -> s_access a
  321. in
  322. let print_field stat f =
  323. p "\t";
  324. print_meta f.cf_meta;
  325. if stat then p "static ";
  326. (match f.cf_kind with
  327. | Var v ->
  328. p "var %s" f.cf_name;
  329. if v.v_read <> AccNormal || v.v_write <> AccNormal then p "(%s,%s)" (access v.v_read) (access v.v_write);
  330. p " : %s" (stype f.cf_type);
  331. | Method m ->
  332. let params, ret = (match follow f.cf_type with
  333. | TFun (args,ret) ->
  334. List.map (fun (a,o,t) ->
  335. let rec loop = function
  336. | [] -> Ident "null"
  337. | (Meta.DefParam,[(EConst (String p),_);(EConst v,_)],_) :: _ when p = a ->
  338. (match v with
  339. | Float "1.#QNAN" -> Float "0./*NaN*/"
  340. | Float "4294967295." -> Int "0xFFFFFFFF"
  341. | Int "16777215" -> Int "0xFFFFFF"
  342. | Float x ->
  343. (try
  344. let f = float_of_string x in
  345. let s = string_of_int (int_of_float f) in
  346. if s ^ "." = x then Int s else v
  347. with _ ->
  348. v)
  349. | _ -> v)
  350. | _ :: l -> loop l
  351. in
  352. a,(if o then Some (loop f.cf_meta) else None ),t
  353. ) args, ret
  354. | _ ->
  355. assert false
  356. ) in
  357. let tparams = (match f.cf_params with [] -> "" | l -> "<" ^ String.concat "," (List.map fst l) ^ ">") in
  358. p "function %s%s(%s) : %s" f.cf_name tparams (String.concat ", " (List.map sparam params)) (stype ret);
  359. );
  360. p ";\n"
  361. in
  362. (match t with
  363. | TClassDecl c ->
  364. print_meta c.cl_meta;
  365. p "extern %s %s" (if c.cl_interface then "interface" else "class") (stype (TInst (c,List.map snd c.cl_types)));
  366. let ext = (match c.cl_super with
  367. | None -> []
  368. | Some (c,pl) -> [" extends " ^ stype (TInst (c,pl))]
  369. ) in
  370. let ext = List.fold_left (fun acc (i,pl) -> ((if c.cl_interface then " extends " else " implements ") ^ stype (TInst (i,pl))) :: acc) ext c.cl_implements in
  371. let ext = (match c.cl_dynamic with
  372. | None -> ext
  373. | Some t ->
  374. (match c.cl_path with
  375. | ["flash";"errors"], _ -> ext
  376. | _ when t == t_dynamic -> " implements Dynamic" :: ext
  377. | _ -> (" implements Dynamic<" ^ stype t ^ ">") :: ext)
  378. ) in
  379. let ext = (match c.cl_path with
  380. | ["flash";"utils"], "ByteArray" -> " implements ArrayAccess<Int>" :: ext
  381. | ["flash";"utils"], "Dictionnary" -> [" implements ArrayAccess<Dynamic>"]
  382. | ["flash";"xml"], "XML" -> [" implements Dynamic<XMLList>"]
  383. | ["flash";"xml"], "XMLList" -> [" implements ArrayAccess<XML>"]
  384. | ["flash";"display"],"MovieClip" -> [" extends Sprite #if !flash_strict implements Dynamic #end"]
  385. | ["flash";"errors"], "Error" -> [" #if !flash_strict implements Dynamic #end"]
  386. | _ -> ext
  387. ) in
  388. p "%s" (String.concat "" (List.rev ext));
  389. p " {\n";
  390. let sort l =
  391. let a = Array.of_list (List.filter (fun f -> f.cf_public && not (List.memq f c.cl_overrides)) l) in
  392. let name = function "new" -> "" | n -> n in
  393. Array.sort (fun f1 f2 ->
  394. match f1.cf_kind, f2.cf_kind with
  395. | Var _, Var _ | Method _ , Method _ -> compare (name f1.cf_name) (name f2.cf_name)
  396. | Var _, _ -> -1
  397. | _ -> 1
  398. ) a;
  399. Array.to_list a
  400. in
  401. List.iter (print_field false) (sort (match c.cl_constructor with None -> c.cl_ordered_fields | Some f -> f :: c.cl_ordered_fields));
  402. List.iter (print_field true) (sort c.cl_ordered_statics);
  403. p "}\n";
  404. | TEnumDecl e ->
  405. print_meta e.e_meta;
  406. p "extern enum %s {\n" (stype (TEnum(e,List.map snd e.e_types)));
  407. let sort l =
  408. let a = Array.of_list l in
  409. Array.sort compare a;
  410. Array.to_list a
  411. in
  412. List.iter (fun n ->
  413. let c = PMap.find n e.e_constrs in
  414. p "\t%s" c.ef_name;
  415. (match follow c.ef_type with
  416. | TFun (args,_) -> p "(%s)" (String.concat ", " (List.map sparam (List.map (fun (a,o,t) -> a,(if o then Some (Ident "null") else None),t) args)))
  417. | _ -> ());
  418. p ";\n";
  419. ) (if Meta.has Meta.FakeEnum e.e_meta then sort e.e_names else e.e_names);
  420. p "}\n"
  421. | TTypeDecl t ->
  422. print_meta t.t_meta;
  423. p "typedef %s = " (stype (TType (t,List.map snd t.t_types)));
  424. p "%s" (stype t.t_type);
  425. p "\n";
  426. | TAbstractDecl a ->
  427. print_meta a.a_meta;
  428. p "abstract %s {}" (stype (TAbstract (a,List.map snd a.a_types)));
  429. );
  430. IO.close_out ch
  431. let generate_hx com =
  432. List.iter (generate_type com) com.types