genxml.ml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. open ExtString
  26. type xml =
  27. | Node of string * (string * string) list * xml list
  28. | PCData of string
  29. | CData of string
  30. let tag name = Node (name,[],[])
  31. let xml name att = Node (name,att,[])
  32. let node name att childs = Node (name,att,childs)
  33. let pcdata s = PCData s
  34. let cdata s = CData s
  35. let pmap f m =
  36. PMap.fold (fun x acc -> f x :: acc) m []
  37. let gen_path (p,n) priv =
  38. ("path",String.concat "." (p @ [n]))
  39. let gen_string s =
  40. if String.contains s '<' || String.contains s '>' || String.contains s '&' then cdata s else pcdata s
  41. let gen_doc s =
  42. (* remove trailing space and convert newlines *)
  43. let s = ExtString.String.strip s in
  44. let s = String.concat "\n" (ExtString.String.nsplit (String.concat "\n" (ExtString.String.nsplit s "\r\n")) "\r") in
  45. node "haxe_doc" [] [gen_string s]
  46. let gen_doc_opt d =
  47. match d with
  48. | None -> []
  49. | Some s -> [gen_doc s]
  50. let gen_arg_name (name,opt,_) =
  51. (if opt then "?" else "") ^ name
  52. let real_path path meta =
  53. let rec loop = function
  54. | [] -> path
  55. | (Meta.RealPath,[(Ast.EConst (Ast.String s),_)],_) :: _ -> parse_path s
  56. | _ :: l -> loop l
  57. in
  58. loop meta
  59. let tpath t =
  60. let i = t_infos t in
  61. real_path i.mt_path i.mt_meta
  62. let rec follow_param t =
  63. match t with
  64. | TMono r ->
  65. (match !r with
  66. | Some t -> follow_param t
  67. | _ -> t)
  68. | TType ({ t_path = [],"Null" } as t,tl) ->
  69. follow_param (apply_params t.t_params tl t.t_type)
  70. | _ ->
  71. t
  72. let gen_meta meta =
  73. let meta = List.filter (fun (m,_,_) -> match m with Meta.Used | Meta.MaybeUsed | Meta.RealPath -> false | _ -> true) meta in
  74. match meta with
  75. | [] -> []
  76. | _ ->
  77. let nodes = List.map (fun (m,el,_) ->
  78. node "m" ["n",fst (MetaInfo.to_string m)] (List.map (fun e -> node "e" [] [gen_string (Ast.s_expr e)]) el)
  79. ) meta in
  80. [node "meta" [] nodes]
  81. let rec gen_type ?(values=None) t =
  82. match t with
  83. | TMono m -> (match !m with None -> tag "unknown" | Some t -> gen_type t)
  84. | TEnum (e,params) -> gen_type_decl "e" (TEnumDecl e) params
  85. | TInst (c,params) -> gen_type_decl "c" (TClassDecl c) params
  86. | TAbstract (a,params) -> gen_type_decl "x" (TAbstractDecl a) params
  87. | TType (t,params) -> gen_type_decl "t" (TTypeDecl t) params
  88. | TFun (args,r) ->
  89. let names = String.concat ":" (List.map gen_arg_name args) in
  90. let values = match values with
  91. | None -> []
  92. | Some values ->
  93. let has_value = ref false in
  94. let values = List.map (fun (n,_,_) ->
  95. try
  96. let e = PMap.find n values in
  97. has_value := true;
  98. let s = Ast.s_expr e in
  99. s
  100. with Not_found ->
  101. ""
  102. ) args in
  103. if !has_value then
  104. ["v",String.concat ":" values]
  105. else
  106. []
  107. in
  108. let args = List.map (fun (_,opt,t) ->
  109. if opt then follow_param t else t
  110. ) args in
  111. node "f" (("a",names) :: values) (List.map gen_type (args @ [r]))
  112. | TAnon a -> node "a" [] (pmap (fun f -> gen_field [] { f with cf_public = false }) a.a_fields)
  113. | TDynamic t2 -> node "d" [] (if t == t2 then [] else [gen_type t2])
  114. | TLazy f -> gen_type (!f())
  115. and gen_type_decl n t pl =
  116. let i = t_infos t in
  117. node n [gen_path (tpath t) i.mt_private] (List.map gen_type pl)
  118. and gen_field att f =
  119. let add_get_set acc name att =
  120. match acc with
  121. | AccNormal | AccResolve | AccRequire _ -> att
  122. | AccNo | AccNever -> (name, "null") :: att
  123. | AccCall -> (name,"accessor") :: att
  124. | AccInline -> (name,"inline") :: att
  125. in
  126. let att = (match f.cf_expr with None -> att | Some e -> ("line",string_of_int (Lexer.get_error_line e.epos)) :: att) in
  127. let att,values = (match f.cf_kind with
  128. | Var v ->
  129. let att = try
  130. begin match Meta.get Meta.Value f.cf_meta with
  131. | (_,[e],_) -> ("expr",Ast.s_expr e) :: att
  132. | _ -> att
  133. end
  134. with Not_found ->
  135. att
  136. in
  137. add_get_set v.v_read "get" (add_get_set v.v_write "set" att),PMap.empty
  138. | Method m ->
  139. let att = match m with
  140. | MethNormal | MethMacro -> ("set", "method") :: att
  141. | MethDynamic -> ("set", "dynamic") :: att
  142. | MethInline -> ("get", "inline") :: ("set","null") :: att
  143. in
  144. att,get_value_meta f.cf_meta
  145. ) in
  146. let att = (match f.cf_params with [] -> att | l -> ("params", String.concat ":" (List.map (fun (n,_) -> n) l)) :: att) in
  147. let overloads = match List.map (gen_field []) f.cf_overloads with
  148. | [] -> []
  149. | nl -> [node "overloads" [] nl]
  150. in
  151. let field_name cf =
  152. try
  153. begin match Meta.get Meta.RealPath cf.cf_meta with
  154. | _,[EConst (String (s)),_],_ -> s
  155. | _ -> raise Not_found
  156. end;
  157. with Not_found ->
  158. cf.cf_name
  159. in
  160. node (field_name f) (if f.cf_public then ("public","1") :: att else att) (gen_type ~values:(Some values) f.cf_type :: gen_meta f.cf_meta @ gen_doc_opt f.cf_doc @ overloads)
  161. let gen_constr e =
  162. let doc = gen_doc_opt e.ef_doc in
  163. let args, t = (match follow e.ef_type with
  164. | TFun (args,_) ->
  165. ["a",String.concat ":" (List.map gen_arg_name args)] ,
  166. List.map (fun (_,opt,t) -> gen_type (if opt then follow_param t else t)) args @ doc
  167. | _ ->
  168. [] , doc
  169. ) in
  170. node e.ef_name args (t @ gen_meta e.ef_meta)
  171. let gen_ordered_constr e =
  172. let rec loop el = match el with
  173. | n :: el ->
  174. gen_constr (PMap.find n e.e_constrs) :: loop el
  175. | [] ->
  176. []
  177. in
  178. loop e.e_names
  179. let gen_type_params ipos priv path params pos m =
  180. let mpriv = (if priv then [("private","1")] else []) in
  181. let mpath = (if m.m_path <> path then [("module",snd (gen_path m.m_path false))] else []) in
  182. let file = (if ipos && pos <> null_pos then [("file",pos.pfile)] else []) in
  183. gen_path path priv :: ("params", String.concat ":" (List.map fst params)) :: (file @ mpriv @ mpath)
  184. let gen_class_path name (c,pl) =
  185. node name [("path",s_type_path (tpath (TClassDecl c)))] (List.map gen_type pl)
  186. let rec exists f c =
  187. PMap.exists f.cf_name c.cl_fields ||
  188. match c.cl_super with
  189. | None -> false
  190. | Some (csup,_) -> exists f csup
  191. let rec gen_type_decl com pos t =
  192. let m = (t_infos t).mt_module in
  193. match t with
  194. | TClassDecl c ->
  195. let stats = List.filter (fun cf ->
  196. cf.cf_name <> "__meta__" && not (Meta.has Meta.GenericInstance cf.cf_meta)
  197. ) c.cl_ordered_statics in
  198. let stats = List.map (gen_field ["static","1"]) stats in
  199. let fields = List.filter (fun cf ->
  200. not (Meta.has Meta.GenericInstance cf.cf_meta)
  201. ) c.cl_ordered_fields in
  202. let fields = (match c.cl_super with
  203. | None -> List.map (fun f -> f,[]) fields
  204. | Some (csup,_) -> List.map (fun f -> if exists f csup then (f,["override","1"]) else (f,[])) fields
  205. ) in
  206. let fields = List.map (fun (f,att) -> gen_field att f) fields in
  207. let constr = (match c.cl_constructor with None -> [] | Some f -> [gen_field [] f]) in
  208. let impl = List.map (gen_class_path (if c.cl_interface then "extends" else "implements")) c.cl_implements in
  209. let tree = (match c.cl_super with
  210. | None -> impl
  211. | Some x -> gen_class_path "extends" x :: impl
  212. ) in
  213. let doc = gen_doc_opt c.cl_doc in
  214. let meta = gen_meta c.cl_meta in
  215. let ext = (if c.cl_extern then [("extern","1")] else []) in
  216. let interf = (if c.cl_interface then [("interface","1")] else []) in
  217. let dynamic = (match c.cl_dynamic with
  218. | None -> []
  219. | Some t -> [node "haxe_dynamic" [] [gen_type t]]
  220. ) in
  221. node "class" (gen_type_params pos c.cl_private (tpath t) c.cl_params c.cl_pos m @ ext @ interf) (tree @ stats @ fields @ constr @ doc @ meta @ dynamic)
  222. | TEnumDecl e ->
  223. let doc = gen_doc_opt e.e_doc in
  224. let meta = gen_meta e.e_meta in
  225. node "enum" (gen_type_params pos e.e_private (tpath t) e.e_params e.e_pos m) (gen_ordered_constr e @ doc @ meta)
  226. | TTypeDecl t ->
  227. let doc = gen_doc_opt t.t_doc in
  228. let meta = gen_meta t.t_meta in
  229. let tt = gen_type t.t_type in
  230. node "typedef" (gen_type_params pos t.t_private t.t_path t.t_params t.t_pos m) (tt :: doc @ meta)
  231. | TAbstractDecl a ->
  232. let doc = gen_doc_opt a.a_doc in
  233. let meta = gen_meta a.a_meta in
  234. let mk_cast t = node "icast" [] [gen_type t] in
  235. let mk_field_cast (t,cf) = node "icast" ["field",cf.cf_name] [gen_type t] in
  236. let sub = (match a.a_from,a.a_from_field with [],[] -> [] | l1,l2 -> [node "from" [] ((List.map mk_cast l1) @ (List.map mk_field_cast l2))]) in
  237. let super = (match a.a_to,a.a_to_field with [],[] -> [] | l1,l2 -> [node "to" [] ((List.map mk_cast l1) @ (List.map mk_field_cast l2))]) in
  238. let impl = (match a.a_impl with None -> [] | Some c -> [node "impl" [] [gen_type_decl com pos (TClassDecl c)]]) in
  239. let this = [node "this" [] [gen_type a.a_this]] in
  240. node "abstract" (gen_type_params pos a.a_private (tpath t) a.a_params a.a_pos m) (sub @ this @ super @ doc @ meta @ impl)
  241. let escape_entities s =
  242. Xml.to_string (Xml.PCData s)
  243. let att_str att =
  244. String.concat "" (List.map (fun (a,v) -> Printf.sprintf " %s=\"%s\"" a (escape_entities v)) att)
  245. let rec write_xml ch tabs x =
  246. match x with
  247. | Node (name,att,[]) ->
  248. IO.printf ch "%s<%s%s/>" tabs name (att_str att)
  249. | Node (name,att,[x]) ->
  250. IO.printf ch "%s<%s%s>" tabs name (att_str att);
  251. write_xml ch "" x;
  252. IO.printf ch "</%s>" name;
  253. | Node (name,att,childs) ->
  254. IO.printf ch "%s<%s%s>\n" tabs name (att_str att);
  255. List.iter (fun x ->
  256. write_xml ch (tabs ^ "\t") x;
  257. IO.printf ch "\n";
  258. ) childs;
  259. IO.printf ch "%s</%s>" tabs name
  260. | PCData s ->
  261. IO.printf ch "%s" s
  262. | CData s ->
  263. IO.printf ch "<![CDATA[%s]]>" s
  264. let generate com file =
  265. let t = Common.timer "construct xml" in
  266. 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
  267. t();
  268. let t = Common.timer "write xml" in
  269. let ch = IO.output_channel (open_out_bin file) in
  270. write_xml ch "" x;
  271. IO.close_out ch;
  272. t()
  273. let gen_type_string ctx t =
  274. let x = gen_type_decl ctx false t in
  275. let ch = IO.output_string() in
  276. write_xml ch "" x;
  277. IO.close_out ch
  278. (* -------------------------------------------------------------------------- *)
  279. (* PRINT HX FROM TYPE *)
  280. let rec create_dir acc = function
  281. | [] -> ()
  282. | d :: l ->
  283. let path = acc ^ "/" ^ d in
  284. (try Unix.mkdir path 0o777 with _ -> ());
  285. create_dir path l
  286. let conv_path p =
  287. match List.rev (fst p) with
  288. | x :: l when x.[0] = '_' -> List.rev (("priv" ^ x) :: l), snd p
  289. | _ -> p
  290. let get_real_path meta path =
  291. try
  292. let real_path = match Meta.get Meta.RealPath meta with
  293. | (_,[(EConst(String s),_)],_) ->
  294. s
  295. | _ -> raise Not_found
  296. in
  297. match List.rev (String.nsplit real_path ".") with
  298. | name :: pack ->
  299. (List.rev pack), name
  300. | _ -> raise Not_found
  301. with | Not_found ->
  302. path
  303. let generate_type com t =
  304. let base_path = "hxclasses" in
  305. let pack, name =
  306. let info = t_infos t in
  307. get_real_path info.mt_meta info.mt_path
  308. in
  309. create_dir "." (base_path :: pack);
  310. match pack, name with
  311. | ["flash";"net"], "NetStreamPlayTransitions"
  312. | ["flash";"filters"], "BitmapFilterQuality"
  313. | ["flash";"display"], ("BitmapDataChannel" | "GraphicsPathCommand") -> ()
  314. | _ ->
  315. let f = open_out_bin (base_path ^ "/" ^ (match pack with [] -> "" | l -> String.concat "/" l ^ "/") ^ name ^ ".hx") in
  316. let ch = IO.output_channel f in
  317. let p fmt = IO.printf ch fmt in
  318. if pack <> [] then IO.printf ch "package %s;\n\n" (String.concat "." pack);
  319. let rec notnull t =
  320. match t with
  321. | TMono r ->
  322. (match !r with
  323. | None -> t
  324. | Some t -> notnull t)
  325. | TLazy f ->
  326. notnull ((!f)())
  327. | TType ({ t_path = [],"Null" },[t]) ->
  328. t
  329. | _ ->
  330. t
  331. in
  332. let rec path meta p tl =
  333. let p = conv_path (get_real_path meta p) in
  334. (if fst p = pack then snd p else s_type_path p) ^ (match tl with [] -> "" | _ -> "<" ^ String.concat "," (List.map stype tl) ^ ">")
  335. and stype t =
  336. match t with
  337. | TMono r ->
  338. (match !r with
  339. | None -> "Unknown"
  340. | Some t -> stype t)
  341. | TInst ({ cl_kind = KTypeParameter _ } as c,tl) ->
  342. path [] ([],snd c.cl_path) tl
  343. | TInst (c,tl) ->
  344. path c.cl_meta c.cl_path tl
  345. | TEnum (e,tl) ->
  346. path e.e_meta e.e_path tl
  347. | TType (t,tl) ->
  348. path t.t_meta t.t_path tl
  349. | TAbstract (a,tl) ->
  350. path a.a_meta a.a_path tl
  351. | TAnon a ->
  352. let fields = PMap.fold (fun f acc -> (f.cf_name ^ " : " ^ stype f.cf_type) :: acc) a.a_fields [] in
  353. "{" ^ String.concat ", " fields ^ "}"
  354. | TLazy f ->
  355. stype ((!f)())
  356. | TDynamic t2 ->
  357. if t == t2 then "Dynamic" else "Dynamic<" ^ stype t2 ^ ">"
  358. | TFun ([],ret) ->
  359. "Void -> " ^ ftype ret
  360. | TFun (args,ret) ->
  361. String.concat " -> " (List.map (fun (_,_,t) -> ftype t) args) ^ " -> " ^ ftype ret
  362. and ftype t =
  363. match t with
  364. | TMono r ->
  365. (match !r with
  366. | None -> stype t
  367. | Some t -> ftype t)
  368. | TLazy f ->
  369. ftype ((!f)())
  370. | TFun _ ->
  371. "(" ^ stype t ^ ")"
  372. | _ ->
  373. stype t
  374. in
  375. let sparam (n,v,t) =
  376. match v with
  377. | None ->
  378. n ^ " : " ^ stype t
  379. | Some (Ident "null") ->
  380. if is_nullable (notnull t) then
  381. "?" ^ n ^ " : " ^ stype (notnull t)
  382. else
  383. (* we have not found a default value stored in metadata, let's generate it *)
  384. n ^ " : " ^ stype t ^ " = " ^ (match follow t with
  385. | TAbstract ({ a_path = [],("Int"|"Float"|"UInt") },_) -> "0"
  386. | TAbstract ({ a_path = [],"Bool" },_) -> "false"
  387. | _ -> "null")
  388. | Some v ->
  389. n ^ " : " ^ stype t ^ " = " ^ (match s_constant v with "nan" -> "0./*NaN*/" | v -> v)
  390. in
  391. let print_meta ml =
  392. List.iter (fun (m,pl,_) ->
  393. match m with
  394. | Meta.DefParam | Meta.CoreApi | Meta.Used | Meta.MaybeUsed | Meta.FlatEnum | Meta.Value | Meta.DirectlyUsed -> ()
  395. | _ ->
  396. match pl with
  397. | [] -> p "@%s " (fst (MetaInfo.to_string m))
  398. | l -> p "@%s(%s) " (fst (MetaInfo.to_string m)) (String.concat "," (List.map Ast.s_expr pl))
  399. ) ml
  400. in
  401. let access is_read a =
  402. match a, pack with
  403. | AccNever, "flash" :: _ -> "null"
  404. | _ -> s_access is_read a
  405. in
  406. let rec print_field stat f =
  407. p "\t";
  408. print_meta f.cf_meta;
  409. if stat then p "static ";
  410. let name = try (match Meta.get Meta.RealPath f.cf_meta with
  411. | (Meta.RealPath, [EConst( String s ), _], _) ->
  412. s
  413. | _ ->
  414. raise Not_found)
  415. with Not_found ->
  416. f.cf_name
  417. in
  418. (match f.cf_kind with
  419. | Var v ->
  420. p "var %s" name;
  421. if v.v_read <> AccNormal || v.v_write <> AccNormal then p "(%s,%s)" (access true v.v_read) (access false v.v_write);
  422. p " : %s" (stype f.cf_type);
  423. | Method m ->
  424. let params, ret = (match follow f.cf_type with
  425. | TFun (args,ret) ->
  426. List.map (fun (a,o,t) ->
  427. let rec loop = function
  428. | [] -> Ident "null"
  429. | (Meta.DefParam,[(EConst (String p),_);(EConst v,_)],_) :: _ when p = a ->
  430. (match v with
  431. | Float "1.#QNAN" -> Float "0./*NaN*/"
  432. | Float "4294967295." -> Int "0xFFFFFFFF"
  433. | Int "16777215" -> Int "0xFFFFFF"
  434. | Float x ->
  435. (try
  436. let f = float_of_string x in
  437. let s = string_of_int (int_of_float f) in
  438. if s ^ "." = x then Int s else v
  439. with _ ->
  440. v)
  441. | _ -> v)
  442. | _ :: l -> loop l
  443. in
  444. a,(if o then Some (loop f.cf_meta) else None ),t
  445. ) args, ret
  446. | _ ->
  447. assert false
  448. ) in
  449. let tparams = (match f.cf_params with [] -> "" | l -> "<" ^ String.concat "," (List.map fst l) ^ ">") in
  450. p "function %s%s(%s) : %s" name tparams (String.concat ", " (List.map sparam params)) (stype ret);
  451. );
  452. p ";\n";
  453. if Meta.has Meta.Overload f.cf_meta then List.iter (fun f -> print_field stat f) f.cf_overloads
  454. in
  455. (match t with
  456. | TClassDecl c ->
  457. print_meta c.cl_meta;
  458. p "extern %s %s" (if c.cl_interface then "interface" else "class") (stype (TInst (c,List.map snd c.cl_params)));
  459. let ext = (match c.cl_super with
  460. | None -> []
  461. | Some (c,pl) -> [" extends " ^ stype (TInst (c,pl))]
  462. ) in
  463. 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
  464. let ext = (match c.cl_dynamic with
  465. | None -> ext
  466. | Some t ->
  467. (match c.cl_path with
  468. | ["flash";"errors"], _ -> ext
  469. | _ when t == t_dynamic -> " implements Dynamic" :: ext
  470. | _ -> (" implements Dynamic<" ^ stype t ^ ">") :: ext)
  471. ) in
  472. let ext = (match c.cl_path with
  473. | ["flash";"utils"], "ByteArray" -> " implements ArrayAccess<Int>" :: ext
  474. | ["flash";"utils"], "Dictionnary" -> [" implements ArrayAccess<Dynamic>"]
  475. | ["flash";"xml"], "XML" -> [" implements Dynamic<XMLList>"]
  476. | ["flash";"xml"], "XMLList" -> [" implements ArrayAccess<XML>"]
  477. | ["flash";"display"],"MovieClip" -> [" extends Sprite #if !flash_strict implements Dynamic #end"]
  478. | ["flash";"errors"], "Error" -> [" #if !flash_strict implements Dynamic #end"]
  479. | _ -> ext
  480. ) in
  481. p "%s" (String.concat "" (List.rev ext));
  482. p " {\n";
  483. let sort l =
  484. let a = Array.of_list (List.filter (fun f -> f.cf_public && not (List.memq f c.cl_overrides)) l) in
  485. let name = function "new" -> "" | n -> n in
  486. Array.sort (fun f1 f2 ->
  487. match f1.cf_kind, f2.cf_kind with
  488. | Var _, Var _ | Method _ , Method _ -> compare (name f1.cf_name) (name f2.cf_name)
  489. | Var _, _ -> -1
  490. | _ -> 1
  491. ) a;
  492. Array.to_list a
  493. in
  494. List.iter (print_field false) (sort (match c.cl_constructor with None -> c.cl_ordered_fields | Some f -> f :: c.cl_ordered_fields));
  495. List.iter (print_field true) (sort c.cl_ordered_statics);
  496. p "}\n";
  497. | TEnumDecl e ->
  498. print_meta e.e_meta;
  499. p "extern enum %s {\n" (stype (TEnum(e,List.map snd e.e_params)));
  500. let sort l =
  501. let a = Array.of_list l in
  502. Array.sort compare a;
  503. Array.to_list a
  504. in
  505. List.iter (fun n ->
  506. let c = PMap.find n e.e_constrs in
  507. p "\t%s" c.ef_name;
  508. (match follow c.ef_type with
  509. | 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)))
  510. | _ -> ());
  511. p ";\n";
  512. ) (if Meta.has Meta.FakeEnum e.e_meta then sort e.e_names else e.e_names);
  513. p "}\n"
  514. | TTypeDecl t ->
  515. print_meta t.t_meta;
  516. p "typedef %s = " (stype (TType (t,List.map snd t.t_params)));
  517. p "%s" (stype t.t_type);
  518. p "\n";
  519. | TAbstractDecl a ->
  520. print_meta a.a_meta;
  521. p "abstract %s {}" (stype (TAbstract (a,List.map snd a.a_params)));
  522. );
  523. IO.close_out ch
  524. let generate_hx com =
  525. List.iter (generate_type com) com.types