typeload.ml 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. (*
  2. * Haxe Compiler
  3. * Copyright (c)2005-2008 Nicolas Cannasse
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *)
  19. open Ast
  20. open Type
  21. open Common
  22. open Typecore
  23. let parse_file com file p =
  24. let ch = (try open_in_bin file with _ -> error ("Could not open " ^ file) p) in
  25. let t = Common.timer "parsing" in
  26. Lexer.init file;
  27. incr stats.s_files_parsed;
  28. let data = (try Parser.parse com (Lexing.from_channel ch) with e -> close_in ch; t(); raise e) in
  29. close_in ch;
  30. t();
  31. Common.log com ("Parsed " ^ file);
  32. data
  33. let parse_hook = ref parse_file
  34. let type_module_hook = ref (fun _ _ _ -> None)
  35. let return_partial_type = ref false
  36. let type_function_param ctx t e opt p =
  37. if opt then
  38. let e = (match e with None -> Some (EConst (Ident "null"),p) | _ -> e) in
  39. ctx.t.tnull t, e
  40. else
  41. t, e
  42. let type_var_field ctx t e stat p =
  43. if stat then ctx.curfun <- FStatic;
  44. let e = type_expr_with_type ctx e (Some t) false in
  45. unify ctx e.etype t p;
  46. match t with
  47. | TType ({ t_path = ([],"UInt") },[]) when stat -> { e with etype = t }
  48. | _ -> e
  49. let apply_macro ctx mode path el p =
  50. let cpath, meth = (match List.rev (ExtString.String.nsplit path ".") with
  51. | meth :: name :: pack -> (List.rev pack,name), meth
  52. | _ -> error "Invalid macro path" p
  53. ) in
  54. ctx.g.do_macro ctx mode cpath meth el p
  55. (** since load_type_def and load_instance are used in PASS2, they should not access the structure of a type **)
  56. (*
  57. load a type or a subtype definition
  58. *)
  59. let rec load_type_def ctx p t =
  60. let no_pack = t.tpackage = [] in
  61. let tname = (match t.tsub with None -> t.tname | Some n -> n) in
  62. try
  63. if t.tsub <> None then raise Not_found;
  64. List.find (fun t2 ->
  65. let tp = t_path t2 in
  66. tp = (t.tpackage,tname) || (no_pack && snd tp = tname)
  67. ) ctx.local_types
  68. with
  69. Not_found ->
  70. let next() =
  71. let t, m = (try
  72. t, ctx.g.do_load_module ctx (t.tpackage,t.tname) p
  73. with Error (Module_not_found _,p2) as e when p == p2 ->
  74. match t.tpackage with
  75. | "std" :: l ->
  76. let t = { t with tpackage = l } in
  77. t, ctx.g.do_load_module ctx (t.tpackage,t.tname) p
  78. | _ -> raise e
  79. ) in
  80. let tpath = (t.tpackage,tname) in
  81. try
  82. List.find (fun t -> not (t_infos t).mt_private && t_path t = tpath) m.m_types
  83. with
  84. Not_found -> raise (Error (Type_not_found (m.m_path,tname),p))
  85. in
  86. let rec loop = function
  87. | [] -> raise Exit
  88. | (_ :: lnext) as l ->
  89. try
  90. load_type_def ctx p { t with tpackage = List.rev l }
  91. with
  92. | Error (Module_not_found _,p2)
  93. | Error (Type_not_found _,p2) when p == p2 -> loop lnext
  94. in
  95. try
  96. if not no_pack then raise Exit;
  97. (match fst ctx.current.m_path with
  98. | [] -> raise Exit
  99. | x :: _ ->
  100. (* this can occur due to haxe remoting : a module can be
  101. already defined in the "js" package and is not allowed
  102. to access the js classes *)
  103. try
  104. (match PMap.find x ctx.com.package_rules with
  105. | Forbidden -> raise Exit
  106. | _ -> ())
  107. with Not_found -> ());
  108. loop (List.rev (fst ctx.current.m_path));
  109. with
  110. Exit -> next()
  111. let check_param_constraints ctx types t pl c p =
  112. match follow t with
  113. | TMono _ -> ()
  114. | _ ->
  115. List.iter (fun (i,tl) ->
  116. let ti = try snd (List.find (fun (_,t) -> match follow t with TInst(i2,[]) -> i == i2 | _ -> false) types) with Not_found -> TInst (i,tl) in
  117. let ti = apply_params types pl ti in
  118. unify ctx t ti p
  119. ) c.cl_implements
  120. (* build an instance from a full type *)
  121. let rec load_instance ctx t p allow_no_params =
  122. try
  123. if t.tpackage <> [] || t.tsub <> None then raise Not_found;
  124. let pt = List.assoc t.tname ctx.type_params in
  125. if t.tparams <> [] then error ("Class type parameter " ^ t.tname ^ " can't have parameters") p;
  126. pt
  127. with Not_found ->
  128. let types , path , f = ctx.g.do_build_instance ctx (load_type_def ctx p t) p in
  129. if allow_no_params && t.tparams = [] then begin
  130. let pl = ref [] in
  131. pl := List.map (fun (name,t) ->
  132. match follow t with
  133. | TInst (c,_) ->
  134. let t = mk_mono() in
  135. if c.cl_implements <> [] then delay_late ctx (fun() -> check_param_constraints ctx types t (!pl) c p);
  136. t;
  137. | _ -> assert false
  138. ) types;
  139. f (!pl)
  140. end else if path = ([],"Dynamic") then
  141. match t.tparams with
  142. | [] -> t_dynamic
  143. | [TPType t] -> TDynamic (load_complex_type ctx p t)
  144. | _ -> error "Too many parameters for Dynamic" p
  145. else begin
  146. if List.length types <> List.length t.tparams then error ("Invalid number of type parameters for " ^ s_type_path path) p;
  147. let tparams = List.map (fun t ->
  148. match t with
  149. | TPExpr e ->
  150. let name = (match fst e with
  151. | EConst (String s) -> "S" ^ s
  152. | EConst (Int i) -> "I" ^ i
  153. | EConst (Float f) -> "F" ^ f
  154. | _ -> "Expr"
  155. ) in
  156. let c = mk_class null_module ([],name) p in
  157. c.cl_kind <- KExpr e;
  158. TInst (c,[])
  159. | TPType t -> load_complex_type ctx p t
  160. ) t.tparams in
  161. let params = List.map2 (fun t (name,t2) ->
  162. let isconst = (match t with TInst ({ cl_kind = KExpr _ },_) -> true | _ -> false) in
  163. if isconst <> (name = "Const") && t != t_dynamic then error (if isconst then "Constant value unexpected here" else "Constant value excepted as type parameter") p;
  164. match follow t2 with
  165. | TInst ({ cl_implements = [] }, []) ->
  166. t
  167. | TInst (c,[]) ->
  168. let r = exc_protect ctx (fun r ->
  169. r := (fun() -> t);
  170. check_param_constraints ctx types t tparams c p;
  171. t
  172. ) in
  173. delay ctx (fun () -> ignore(!r()));
  174. TLazy r
  175. | _ -> assert false
  176. ) tparams types in
  177. f params
  178. end
  179. (*
  180. build an instance from a complex type
  181. *)
  182. and load_complex_type ctx p t =
  183. match t with
  184. | CTParent t -> load_complex_type ctx p t
  185. | CTPath t -> load_instance ctx t p false
  186. | CTOptional _ -> error "Optional type not allowed here" p
  187. | CTExtend (t,l) ->
  188. (match load_complex_type ctx p (CTAnonymous l) with
  189. | TAnon a ->
  190. let rec loop t =
  191. match follow t with
  192. | TInst (c,tl) ->
  193. let c2 = mk_class null_module (fst c.cl_path,"+" ^ snd c.cl_path) p in
  194. c2.cl_private <- true;
  195. PMap.iter (fun f _ ->
  196. try
  197. ignore(class_field c f);
  198. error ("Cannot redefine field " ^ f) p
  199. with
  200. Not_found -> ()
  201. ) a.a_fields;
  202. (* do NOT tag as extern - for protect *)
  203. c2.cl_kind <- KExtension (c,tl);
  204. c2.cl_super <- Some (c,tl);
  205. c2.cl_fields <- a.a_fields;
  206. TInst (c2,[])
  207. | TMono _ ->
  208. error "Please ensure correct initialization of cascading signatures" p
  209. | TAnon a2 ->
  210. PMap.iter (fun f _ ->
  211. if PMap.mem f a2.a_fields then error ("Cannot redefine field " ^ f) p
  212. ) a.a_fields;
  213. mk_anon (PMap.foldi PMap.add a.a_fields a2.a_fields)
  214. | _ -> error "Cannot only extend classes and anonymous" p
  215. in
  216. loop (load_instance ctx t p false)
  217. | _ -> assert false)
  218. | CTAnonymous l ->
  219. let rec loop acc f =
  220. let n = f.cff_name in
  221. let p = f.cff_pos in
  222. if PMap.mem n acc then error ("Duplicate field declaration : " ^ n) p;
  223. let topt = function
  224. | None -> error ("Explicit type required for field " ^ n) p
  225. | Some t -> load_complex_type ctx p t
  226. in
  227. let no_expr = function
  228. | None -> ()
  229. | Some (_,p) -> error "Expression not allowed here" p
  230. in
  231. let pub = ref true in
  232. let dyn = ref false in
  233. List.iter (fun a ->
  234. match a with
  235. | APublic -> ()
  236. | APrivate -> pub := false;
  237. | ADynamic when (match f.cff_kind with FFun _ -> true | _ -> false) -> dyn := true
  238. | AStatic | AOverride | AInline | ADynamic -> error ("Invalid access " ^ Ast.s_access a) p
  239. ) f.cff_access;
  240. let t , access = (match f.cff_kind with
  241. | FVar (t, e) ->
  242. no_expr e;
  243. topt t, Var { v_read = AccNormal; v_write = AccNormal }
  244. | FFun f ->
  245. if f.f_params <> [] then error "Type parameters are not allowed in structures" p;
  246. no_expr f.f_expr;
  247. let args = List.map (fun (name,o,t,e) -> no_expr e; name, o, topt t) f.f_args in
  248. TFun (args,topt f.f_type), Method (if !dyn then MethDynamic else MethNormal)
  249. | FProp (i1,i2,t,e) ->
  250. no_expr e;
  251. let access m get =
  252. match m with
  253. | "null" -> AccNo
  254. | "never" -> AccNever
  255. | "default" -> AccNormal
  256. | "dynamic" -> AccCall ((if get then "get_" else "set_") ^ n)
  257. | _ -> AccCall m
  258. in
  259. load_complex_type ctx p t, Var { v_read = access i1 true; v_write = access i2 false }
  260. ) in
  261. let cf = {
  262. cf_name = n;
  263. cf_type = t;
  264. cf_pos = p;
  265. cf_public = !pub;
  266. cf_kind = access;
  267. cf_params = [];
  268. cf_expr = None;
  269. cf_doc = f.cff_doc;
  270. cf_meta = f.cff_meta;
  271. cf_overloads = [];
  272. } in
  273. init_meta_overloads ctx cf;
  274. PMap.add n cf acc
  275. in
  276. mk_anon (List.fold_left loop PMap.empty l)
  277. | CTFunction (args,r) ->
  278. match args with
  279. | [CTPath { tpackage = []; tparams = []; tname = "Void" }] ->
  280. TFun ([],load_complex_type ctx p r)
  281. | _ ->
  282. TFun (List.map (fun t ->
  283. let t, opt = (match t with CTOptional t -> t, true | _ -> t,false) in
  284. "",opt,load_complex_type ctx p t
  285. ) args,load_complex_type ctx p r)
  286. and init_meta_overloads ctx cf =
  287. let overloads = ref [] in
  288. cf.cf_meta <- List.filter (fun m ->
  289. match m with
  290. | (":overload",[(EFunction (fname,f),p)],_) ->
  291. if fname <> None then error "Function name must not be part of @:overload" p;
  292. (match f.f_expr with Some (EBlock [], _) -> () | _ -> error "Overload must only declare an empty method body {}" p);
  293. let topt = function None -> error "Explicit type required" p | Some t -> load_complex_type ctx p t in
  294. let args = List.map (fun (a,opt,t,_) -> a,opt,topt t) f.f_args in
  295. overloads := (args,topt f.f_type) :: !overloads;
  296. false
  297. | _ ->
  298. true
  299. ) cf.cf_meta;
  300. cf.cf_overloads <- List.map (fun (args,ret) -> { cf with cf_type = TFun (args,ret) }) (List.rev !overloads)
  301. let hide_types ctx =
  302. let old_locals = ctx.local_types in
  303. let old_type_params = ctx.type_params in
  304. ctx.local_types <- ctx.g.std.m_types;
  305. ctx.type_params <- [];
  306. (fun() ->
  307. ctx.local_types <- old_locals;
  308. ctx.type_params <- old_type_params;
  309. )
  310. (*
  311. load a type while ignoring the current imports or local types
  312. *)
  313. let load_core_type ctx name =
  314. let show = hide_types ctx in
  315. let t = load_instance ctx { tpackage = []; tname = name; tparams = []; tsub = None; } null_pos false in
  316. show();
  317. t
  318. let t_iterator ctx =
  319. let show = hide_types ctx in
  320. match load_type_def ctx null_pos { tpackage = []; tname = "Iterator"; tparams = []; tsub = None } with
  321. | TTypeDecl t ->
  322. show();
  323. if List.length t.t_types <> 1 then assert false;
  324. let pt = mk_mono() in
  325. apply_params t.t_types [pt] t.t_type, pt
  326. | _ ->
  327. assert false
  328. (*
  329. load either a type t or Null<Unknown> if not defined
  330. *)
  331. let load_type_opt ?(opt=false) ctx p t =
  332. let t = (match t with None -> mk_mono() | Some t -> load_complex_type ctx p t) in
  333. if opt then ctx.t.tnull t else t
  334. (* ---------------------------------------------------------------------- *)
  335. (* Structure check *)
  336. let valid_redefinition ctx f1 t1 f2 t2 =
  337. let valid t1 t2 =
  338. unify_raise ctx t1 t2 f1.cf_pos;
  339. if is_null t1 <> is_null t2 then raise (Unify_error [Cannot_unify (t1,t2)]);
  340. in
  341. let t1, t2 = (match f1.cf_params, f2.cf_params with
  342. | [], [] -> t1, t2
  343. | l1, l2 when List.length l1 = List.length l2 ->
  344. let monos = List.map (fun _ -> mk_mono()) l1 in
  345. apply_params l1 monos t1, apply_params l2 monos t2
  346. | _ -> t1, t2
  347. ) in
  348. match follow t1, follow t2 with
  349. | TFun (args1,r1) , TFun (args2,r2) when List.length args1 = List.length args2 ->
  350. List.iter2 (fun (n,o1,a1) (_,o2,a2) ->
  351. if o1 <> o2 then raise (Unify_error [Not_matching_optional n]);
  352. valid a2 a1;
  353. ) args1 args2;
  354. valid r1 r2;
  355. | _ , _ ->
  356. (* in case args differs, or if an interface var *)
  357. type_eq EqStrict t1 t2;
  358. if is_null t1 <> is_null t2 then raise (Unify_error [Cannot_unify (t1,t2)])
  359. let copy_meta meta_src meta_target sl =
  360. let meta = ref meta_target in
  361. List.iter (fun (m,e,p) ->
  362. if List.mem m sl then meta := (m,e,p) :: !meta
  363. ) meta_src;
  364. !meta
  365. let check_overriding ctx c p () =
  366. match c.cl_super with
  367. | None ->
  368. (match c.cl_overrides with
  369. | [] -> ()
  370. | i :: _ ->
  371. display_error ctx ("Field " ^ i ^ " is declared 'override' but doesn't override any field") p)
  372. | Some (csup,params) ->
  373. PMap.iter (fun i f ->
  374. try
  375. let t , f2 = raw_class_field (fun f -> f.cf_type) csup i in
  376. (* allow to define fields that are not defined for this platform version in superclass *)
  377. (match f2.cf_kind with
  378. | Var { v_read = AccRequire _ } -> raise Not_found;
  379. | _ -> ());
  380. ignore(follow f.cf_type); (* force evaluation *)
  381. let p = (match f.cf_expr with None -> p | Some e -> e.epos) in
  382. if not (List.mem i c.cl_overrides) then
  383. display_error ctx ("Field " ^ i ^ " should be declared with 'override' since it is inherited from superclass") p
  384. else if not f.cf_public && f2.cf_public then
  385. display_error ctx ("Field " ^ i ^ " has less visibility (public/private) than superclass one") p
  386. else (match f.cf_kind, f2.cf_kind with
  387. | _, Method MethInline ->
  388. display_error ctx ("Field " ^ i ^ " is inlined and cannot be overridden") p
  389. | a, b when a = b -> ()
  390. | Method MethInline, Method MethNormal ->
  391. () (* allow to redefine a method as inlined *)
  392. | _ ->
  393. display_error ctx ("Field " ^ i ^ " has different property access than in superclass") p);
  394. try
  395. let t = apply_params csup.cl_types params t in
  396. valid_redefinition ctx f f.cf_type f2 t
  397. with
  398. Unify_error l ->
  399. display_error ctx ("Field " ^ i ^ " overload parent class with different or incomplete type") p;
  400. display_error ctx (error_msg (Unify l)) p;
  401. with
  402. Not_found ->
  403. if List.mem i c.cl_overrides then display_error ctx ("Field " ^ i ^ " is declared 'override' but doesn't override any field") p
  404. ) c.cl_fields
  405. let class_field_no_interf c i =
  406. try
  407. let f = PMap.find i c.cl_fields in
  408. f.cf_type , f
  409. with Not_found ->
  410. match c.cl_super with
  411. | None ->
  412. raise Not_found
  413. | Some (c,tl) ->
  414. (* rec over class_field *)
  415. let t , f = raw_class_field (fun f -> f.cf_type) c i in
  416. apply_params c.cl_types tl t , f
  417. let rec check_interface ctx c p intf params =
  418. PMap.iter (fun i f ->
  419. try
  420. let t2, f2 = class_field_no_interf c i in
  421. ignore(follow f2.cf_type); (* force evaluation *)
  422. let p = (match f2.cf_expr with None -> p | Some e -> e.epos) in
  423. let mkind = function
  424. | MethNormal | MethInline -> 0
  425. | MethDynamic -> 1
  426. | MethMacro -> 2
  427. in
  428. if f.cf_public && not f2.cf_public then
  429. display_error ctx ("Field " ^ i ^ " should be public as requested by " ^ s_type_path intf.cl_path) p
  430. else if not (unify_kind f2.cf_kind f.cf_kind) || not (match f.cf_kind, f2.cf_kind with Var _ , Var _ -> true | Method m1, Method m2 -> mkind m1 = mkind m2 | _ -> false) then
  431. display_error ctx ("Field " ^ i ^ " has different property access than in " ^ s_type_path intf.cl_path ^ " (" ^ s_kind f2.cf_kind ^ " should be " ^ s_kind f.cf_kind ^ ")") p
  432. else try
  433. valid_redefinition ctx f2 t2 f (apply_params intf.cl_types params f.cf_type)
  434. with
  435. Unify_error l ->
  436. display_error ctx ("Field " ^ i ^ " has different type than in " ^ s_type_path intf.cl_path) p;
  437. display_error ctx (error_msg (Unify l)) p;
  438. with
  439. Not_found ->
  440. if not c.cl_interface then display_error ctx ("Field " ^ i ^ " needed by " ^ s_type_path intf.cl_path ^ " is missing") p
  441. ) intf.cl_fields;
  442. List.iter (fun (i2,p2) ->
  443. check_interface ctx c p i2 (List.map (apply_params intf.cl_types params) p2)
  444. ) intf.cl_implements
  445. let check_interfaces ctx c p () =
  446. match c.cl_path with
  447. | "Proxy" :: _ , _ -> ()
  448. | _ ->
  449. List.iter (fun (intf,params) -> check_interface ctx c p intf params) c.cl_implements
  450. let rec return_flow ctx e =
  451. let error() = display_error ctx "A return is missing here" e.epos; raise Exit in
  452. let return_flow = return_flow ctx in
  453. match e.eexpr with
  454. | TReturn _ | TThrow _ -> ()
  455. | TParenthesis e ->
  456. return_flow e
  457. | TBlock el ->
  458. let rec loop = function
  459. | [] -> error()
  460. | [e] -> return_flow e
  461. | { eexpr = TReturn _ } :: _ | { eexpr = TThrow _ } :: _ -> ()
  462. | _ :: l -> loop l
  463. in
  464. loop el
  465. | TIf (_,e1,Some e2) ->
  466. return_flow e1;
  467. return_flow e2;
  468. | TSwitch (v,cases,Some e) ->
  469. List.iter (fun (_,e) -> return_flow e) cases;
  470. return_flow e
  471. | TSwitch (e,cases,None) when (match follow e.etype with TEnum _ -> true | _ -> false) ->
  472. List.iter (fun (_,e) -> return_flow e) cases;
  473. | TMatch (_,_,cases,def) ->
  474. List.iter (fun (_,_,e) -> return_flow e) cases;
  475. (match def with None -> () | Some e -> return_flow e)
  476. | TTry (e,cases) ->
  477. return_flow e;
  478. List.iter (fun (_,e) -> return_flow e) cases;
  479. | TWhile({eexpr = (TConst (TBool true))},e,_) ->
  480. (* a special case for "inifite" while loops that have no break *)
  481. let rec loop e = match e.eexpr with
  482. (* ignore nested loops to not accidentally get one of its breaks *)
  483. | TWhile _ | TFor _ -> ()
  484. | TBreak -> error()
  485. | _ -> Type.iter loop e
  486. in
  487. loop e
  488. | _ ->
  489. error()
  490. (* ---------------------------------------------------------------------- *)
  491. (* PASS 1 & 2 : Module and Class Structure *)
  492. let set_heritance ctx c herits p =
  493. let process_meta csup =
  494. List.iter (fun m ->
  495. match m with
  496. | ":final", _, _ -> if not (Type.has_meta ":hack" c.cl_meta) then error "Cannot extend a final class" p;
  497. | ":autoBuild", el, p -> c.cl_meta <- (":build",el,p) :: m :: c.cl_meta;
  498. | _ -> ()
  499. ) csup.cl_meta
  500. in
  501. let rec loop = function
  502. | HPrivate | HExtern | HInterface ->
  503. ()
  504. | HExtends t ->
  505. if c.cl_super <> None then error "Cannot extend several classes" p;
  506. let t = load_instance ctx t p false in
  507. (match follow t with
  508. | TInst ({ cl_path = [],"Array" },_)
  509. | TInst ({ cl_path = [],"String" },_)
  510. | TInst ({ cl_path = [],"Date" },_)
  511. | TInst ({ cl_path = [],"Xml" },_) when ((not (platform ctx.com Cpp)) && (match c.cl_path with "mt" :: _ , _ -> false | _ -> true)) ->
  512. error "Cannot extend basic class" p;
  513. | TInst (csup,params) ->
  514. if is_parent c csup then error "Recursive class" p;
  515. if c.cl_interface then error "Cannot extend an interface" p;
  516. if csup.cl_interface then error "Cannot extend by using an interface" p;
  517. process_meta csup;
  518. c.cl_super <- Some (csup,params)
  519. | _ -> error "Should extend by using a class" p)
  520. | HImplements t ->
  521. let t = load_instance ctx t p false in
  522. (match follow t with
  523. | TInst ({ cl_path = [],"ArrayAccess"; cl_extern = true; },[t]) ->
  524. if c.cl_array_access <> None then error "Duplicate array access" p;
  525. c.cl_array_access <- Some t
  526. | TInst (intf,params) ->
  527. if is_parent c intf then error "Recursive class" p;
  528. process_meta intf;
  529. c.cl_implements <- (intf, params) :: c.cl_implements
  530. | TDynamic t ->
  531. if c.cl_dynamic <> None then error "Cannot have several dynamics" p;
  532. c.cl_dynamic <- Some t
  533. | _ -> error "Should implement by using an interface or a class" p)
  534. in
  535. (*
  536. resolve imports before calling build_inheritance, since it requires full paths.
  537. that means that typedefs are not working, but that's a fair limitation
  538. *)
  539. let rec resolve_imports t =
  540. match t.tpackage with
  541. | _ :: _ -> t
  542. | [] ->
  543. try
  544. let lt = List.find (fun lt -> snd (t_path lt) = t.tname) ctx.local_types in
  545. { t with tpackage = fst (t_path lt) }
  546. with
  547. Not_found -> t
  548. in
  549. let herits = List.map (function
  550. | HExtends t -> HExtends (resolve_imports t)
  551. | HImplements t -> HImplements (resolve_imports t)
  552. | h -> h
  553. ) herits in
  554. List.iter loop (List.filter (ctx.g.do_inherit ctx c p) herits)
  555. let type_type_params ctx path get_params p (n,flags) =
  556. let c = mk_class ctx.current (fst path @ [snd path],n) p in
  557. c.cl_kind <- KTypeParameter;
  558. let t = TInst (c,[]) in
  559. match flags with
  560. | [] -> n, t
  561. | _ ->
  562. let r = exc_protect ctx (fun r ->
  563. r := (fun _ -> t);
  564. let ctx = { ctx with type_params = ctx.type_params @ get_params() } in
  565. set_heritance ctx c (List.map (fun t -> match t with CTPath t -> HImplements t | _ -> error "Unsupported type constraint" p) flags) p;
  566. t
  567. ) in
  568. delay ctx (fun () -> ignore(!r()));
  569. n, TLazy r
  570. let type_function_params ctx fd fname fmeta p =
  571. let params = ref [] in
  572. params := List.map (fun (n,flags) ->
  573. type_type_params ctx ([],fname) (fun() -> !params) p (n,flags)
  574. ) fd.f_params;
  575. !params
  576. let type_function ctx args ret fmode f p =
  577. let locals = save_locals ctx in
  578. let fargs = List.map (fun (n,c,t) ->
  579. let c = (match c with
  580. | None -> None
  581. | Some e ->
  582. let p = pos e in
  583. let e = ctx.g.do_optimize ctx (type_expr ctx e true) in
  584. unify ctx e.etype t p;
  585. match e.eexpr with
  586. | TConst c -> Some c
  587. | _ -> display_error ctx "Parameter default value should be constant" p; None
  588. ) in
  589. add_local ctx n t, c
  590. ) args in
  591. let old_ret = ctx.ret in
  592. let old_fun = ctx.curfun in
  593. let old_opened = ctx.opened in
  594. ctx.curfun <- fmode;
  595. ctx.ret <- ret;
  596. ctx.opened <- [];
  597. let e = type_expr ctx (match f.f_expr with None -> error "Function body required" p | Some e -> e) false in
  598. let rec loop e =
  599. match e.eexpr with
  600. | TReturn (Some _) -> raise Exit
  601. | TFunction _ -> ()
  602. | _ -> Type.iter loop e
  603. in
  604. let have_ret = (try loop e; false with Exit -> true) in
  605. if have_ret then
  606. (try return_flow ctx e with Exit -> ())
  607. else (try unify_raise ctx ret ctx.t.tvoid p
  608. with Error(Unify _,_) -> display_error ctx ("Missing return: " ^ (s_type (print_context()) ret)) p);
  609. let rec loop e =
  610. match e.eexpr with
  611. | TCall ({ eexpr = TConst TSuper },_) -> raise Exit
  612. | TFunction _ -> ()
  613. | _ -> Type.iter loop e
  614. in
  615. let has_super_constr() =
  616. match ctx.curclass.cl_super with
  617. | None -> false
  618. | Some (csup,_) ->
  619. try ignore(get_constructor (fun f->f.cf_type) csup); true with Not_found -> false
  620. in
  621. if fmode = FConstructor && has_super_constr() then
  622. (try
  623. loop e;
  624. display_error ctx "Missing super constructor call" p
  625. with
  626. Exit -> ());
  627. locals();
  628. let e = match ctx.curfun, ctx.vthis with
  629. | (FMember|FConstructor), Some v ->
  630. let ev = mk (TVars [v,Some (mk (TConst TThis) ctx.tthis p)]) ctx.t.tvoid p in
  631. (match e.eexpr with
  632. | TBlock l -> { e with eexpr = TBlock (ev::l) }
  633. | _ -> mk (TBlock [ev;e]) e.etype p)
  634. | _ -> e
  635. in
  636. List.iter (fun r -> r := Closed) ctx.opened;
  637. ctx.ret <- old_ret;
  638. ctx.curfun <- old_fun;
  639. ctx.opened <- old_opened;
  640. e , fargs
  641. let init_core_api ctx c =
  642. let ctx2 = (match ctx.g.core_api with
  643. | None ->
  644. let com2 = Common.clone ctx.com in
  645. Common.define com2 "core_api";
  646. com2.class_path <- ctx.com.std_path;
  647. let ctx2 = ctx.g.do_create com2 ctx.in_macro in
  648. ctx.g.core_api <- Some ctx2;
  649. ctx2
  650. | Some c ->
  651. c
  652. ) in
  653. let t = load_instance ctx2 { tpackage = fst c.cl_path; tname = snd c.cl_path; tparams = []; tsub = None; } c.cl_pos true in
  654. match t with
  655. | TInst (ccore,_) ->
  656. (match c.cl_doc with
  657. | None -> c.cl_doc <- ccore.cl_doc
  658. | Some _ -> ());
  659. let compare_fields f f2 =
  660. let p = (match f2.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  661. (try
  662. type_eq EqCoreType (apply_params ccore.cl_types (List.map snd c.cl_types) f.cf_type) f2.cf_type
  663. with Unify_error l ->
  664. display_error ctx ("Field " ^ f.cf_name ^ " has different type than in core type") p;
  665. display_error ctx (error_msg (Unify l)) p);
  666. if f2.cf_public <> f.cf_public then error ("Field " ^ f.cf_name ^ " has different visibility than core type") p;
  667. (match f2.cf_doc with
  668. | None -> f2.cf_doc <- f.cf_doc
  669. | Some _ -> ());
  670. if f2.cf_kind <> f.cf_kind then begin
  671. match f2.cf_kind, f.cf_kind with
  672. | Method MethInline, Method MethNormal -> () (* allow to add 'inline' *)
  673. | Method MethNormal, Method MethInline -> () (* allow to disable 'inline' *)
  674. | _ ->
  675. error ("Field " ^ f.cf_name ^ " has different property access than core type") p;
  676. end;
  677. (match follow f.cf_type, follow f2.cf_type with
  678. | TFun (pl1,_), TFun (pl2,_) ->
  679. if List.length pl1 != List.length pl2 then assert false;
  680. List.iter2 (fun (n1,_,_) (n2,_,_) ->
  681. if n1 <> n2 then error ("Method parameter name '" ^ n2 ^ "' should be '" ^ n1 ^ "'") p;
  682. ) pl1 pl2;
  683. | _ -> ());
  684. in
  685. let check_fields fcore fl =
  686. PMap.iter (fun i f ->
  687. if not f.cf_public then () else
  688. let f2 = try PMap.find f.cf_name fl with Not_found -> error ("Missing field " ^ i ^ " required by core type") c.cl_pos in
  689. compare_fields f f2;
  690. ) fcore;
  691. PMap.iter (fun i f ->
  692. let p = (match f.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  693. if f.cf_public && not (PMap.mem f.cf_name fcore) && not (List.mem f.cf_name c.cl_overrides) then error ("Public field " ^ i ^ " is not part of core type") p;
  694. ) fl;
  695. in
  696. check_fields ccore.cl_fields c.cl_fields;
  697. check_fields ccore.cl_statics c.cl_statics;
  698. (match ccore.cl_constructor, c.cl_constructor with
  699. | None, None -> ()
  700. | Some f, Some f2 -> compare_fields f f2
  701. | None, Some { cf_public = false } -> ()
  702. | _ -> error "Constructor differs from core type" c.cl_pos)
  703. | _ -> assert false
  704. let patch_class ctx c fields =
  705. let h = (try Some (Hashtbl.find ctx.g.type_patches c.cl_path) with Not_found -> None) in
  706. match h with
  707. | None -> fields
  708. | Some (h,hcl) ->
  709. c.cl_meta <- c.cl_meta @ hcl.tp_meta;
  710. let rec loop acc = function
  711. | [] -> acc
  712. | f :: l ->
  713. (* patch arguments types *)
  714. (match f.cff_kind with
  715. | FFun ff ->
  716. let param ((n,opt,t,e) as p) =
  717. try
  718. let t2 = (try Hashtbl.find h (("$" ^ f.cff_name ^ "__" ^ n),false) with Not_found -> Hashtbl.find h (("$" ^ n),false)) in
  719. n, opt, t2.tp_type, e
  720. with Not_found ->
  721. p
  722. in
  723. f.cff_kind <- FFun { ff with f_args = List.map param ff.f_args }
  724. | _ -> ());
  725. (* other patches *)
  726. match (try Some (Hashtbl.find h (f.cff_name,List.mem AStatic f.cff_access)) with Not_found -> None) with
  727. | None -> loop (f :: acc) l
  728. | Some { tp_remove = true } -> loop acc l
  729. | Some p ->
  730. f.cff_meta <- f.cff_meta @ p.tp_meta;
  731. (match p.tp_type with
  732. | None -> ()
  733. | Some t ->
  734. f.cff_kind <- match f.cff_kind with
  735. | FVar (_,e) -> FVar (Some t,e)
  736. | FProp (get,set,_,eo) -> FProp (get,set,t,eo)
  737. | FFun f -> FFun { f with f_type = Some t });
  738. loop (f :: acc) l
  739. in
  740. List.rev (loop [] fields)
  741. let rec string_list_of_expr_path (e,p) =
  742. match e with
  743. | EConst (Ident i) -> [i]
  744. | EField (e,f) -> f :: string_list_of_expr_path e
  745. | _ -> error "Invalid path" p
  746. let build_module_def ctx mt meta fvars fbuild =
  747. let rec loop = function
  748. | (":build",args,p) :: l ->
  749. let epath, el = (match args with
  750. | [ECall (epath,el),p] -> epath, el
  751. | _ -> error "Invalid build parameters" p
  752. ) in
  753. let s = try String.concat "." (List.rev (string_list_of_expr_path epath)) with Error (_,p) -> error "Build call parameter must be a class path" p in
  754. if ctx.in_macro then error "You cannot used :build inside a macro : make sure that your enum is not used in macro" p;
  755. let old = ctx.g.get_build_infos in
  756. ctx.g.get_build_infos <- (fun() -> Some (mt, fvars()));
  757. let r = try apply_macro ctx MBuild s el p with e -> ctx.g.get_build_infos <- old; raise e in
  758. ctx.g.get_build_infos <- old;
  759. (match r with
  760. | None -> error "Build failure" p
  761. | Some e -> fbuild e; loop l)
  762. | _ :: l -> loop l
  763. | [] -> ()
  764. in
  765. try
  766. loop meta
  767. with Error (Custom msg,p) ->
  768. display_error ctx msg p
  769. let init_class ctx c p herits fields =
  770. incr stats.s_classes_built;
  771. let fields = patch_class ctx c fields in
  772. let ctx = { ctx with type_params = c.cl_types } in
  773. c.cl_extern <- List.mem HExtern herits;
  774. c.cl_interface <- List.mem HInterface herits;
  775. if c.cl_path = (["haxe";"macro"],"MacroType") then c.cl_kind <- KMacroType;
  776. set_heritance ctx c herits p;
  777. let fields = ref fields in
  778. let get_fields() = !fields in
  779. build_module_def ctx (TClassDecl c) c.cl_meta get_fields (fun (e,p) ->
  780. match e with
  781. | EVars [_,Some (CTAnonymous f),None] -> fields := f
  782. | _ -> error "Class build macro must return a single variable with anonymous fields" p
  783. );
  784. let fields = !fields in
  785. let core_api = has_meta ":core_api" c.cl_meta in
  786. let is_macro = has_meta ":macro" c.cl_meta in
  787. let fields, herits = if is_macro && not ctx.in_macro then begin
  788. c.cl_extern <- true;
  789. List.filter (fun f -> List.mem AStatic f.cff_access) fields, []
  790. end else fields, herits in
  791. if core_api && not (ctx.com.display || ctx.com.dead_code_elimination) then delay ctx (fun() -> init_core_api ctx c);
  792. let tthis = TInst (c,List.map snd c.cl_types) in
  793. let rec extends_public c =
  794. List.exists (fun (c,_) -> c.cl_path = (["haxe"],"Public") || extends_public c) c.cl_implements ||
  795. match c.cl_super with
  796. | None -> false
  797. | Some (c,_) -> extends_public c
  798. in
  799. let extends_public = extends_public c in
  800. let is_public access parent =
  801. if List.mem APrivate access then
  802. false
  803. else if List.mem APublic access then
  804. true
  805. else match parent with
  806. | Some { cf_public = p } -> p
  807. | _ -> c.cl_extern || c.cl_interface || extends_public
  808. in
  809. let rec get_parent c name =
  810. match c.cl_super with
  811. | None -> None
  812. | Some (csup,_) ->
  813. try
  814. Some (PMap.find name csup.cl_fields)
  815. with
  816. Not_found -> get_parent csup name
  817. in
  818. let type_opt ctx p t =
  819. match t with
  820. | None when c.cl_extern || c.cl_interface ->
  821. display_error ctx "Type required for extern classes and interfaces" p;
  822. t_dynamic
  823. | None when core_api ->
  824. display_error ctx "Type required for core api classes" p;
  825. t_dynamic
  826. | _ ->
  827. load_type_opt ctx p t
  828. in
  829. let rec has_field f = function
  830. | None -> false
  831. | Some (c,_) ->
  832. PMap.exists f c.cl_fields || has_field f c.cl_super || List.exists (fun i -> has_field f (Some i)) c.cl_implements
  833. in
  834. (* ----------------------- COMPLETION ----------------------------- *)
  835. let display_file = if ctx.com.display then Common.unique_full_path p.pfile = (!Parser.resume_display).pfile else false in
  836. let fields = if not display_file || Common.defined ctx.com "no-copt" then fields else Optimizer.optimize_completion c fields in
  837. let mark_used cf = if ctx.com.dead_code_elimination then cf.cf_meta <- (":?used",[],p) :: cf.cf_meta in
  838. let rec is_full_type t =
  839. match t with
  840. | TFun (args,ret) -> is_full_type ret && List.for_all (fun (_,_,t) -> is_full_type t) args
  841. | TMono r -> (match !r with None -> false | Some t -> is_full_type t)
  842. | TInst _ | TEnum _ | TLazy _ | TDynamic _ | TAnon _ | TType _ -> true
  843. in
  844. let bind_type cf r p macro =
  845. if ctx.com.display then begin
  846. let cp = !Parser.resume_display in
  847. if display_file && (cp.pmin = 0 || (p.pmin <= cp.pmin && p.pmax >= cp.pmax)) then begin
  848. if macro && not ctx.in_macro then
  849. (* force macro system loading of this class in order to get completion *)
  850. (fun() -> ignore(ctx.g.do_macro ctx MExpr c.cl_path cf.cf_name [] p))
  851. else begin
  852. cf.cf_type <- TLazy r;
  853. (fun() -> ignore((!r)()))
  854. end
  855. end else begin
  856. if not (is_full_type cf.cf_type) then cf.cf_type <- TLazy r;
  857. (fun() -> ())
  858. end
  859. end else if macro && not ctx.in_macro then
  860. (fun () -> ())
  861. else begin
  862. cf.cf_type <- TLazy r;
  863. if ctx.com.dead_code_elimination && cf.cf_name <> "__init__" then (fun() -> ()) else (fun () -> ignore(!r()))
  864. end
  865. in
  866. let bind_var ctx cf e stat inline =
  867. let p = cf.cf_pos in
  868. if not stat && has_field cf.cf_name c.cl_super then error ("Redefinition of variable " ^ cf.cf_name ^ " in subclass is not allowed") p;
  869. let t = cf.cf_type in
  870. match e with
  871. | None when ctx.com.dead_code_elimination && not ctx.com.display ->
  872. let r = exc_protect ctx (fun r ->
  873. r := (fun() -> t);
  874. mark_used cf;
  875. t
  876. ) in
  877. cf.cf_type <- TLazy r;
  878. (fun() -> ())
  879. | None ->
  880. (fun() -> ())
  881. | Some e ->
  882. let r = exc_protect ctx (fun r ->
  883. if not !return_partial_type then begin
  884. r := (fun() -> t);
  885. if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ cf.cf_name);
  886. if not inline then mark_used cf;
  887. let e = type_var_field ctx t e stat p in
  888. let e = (match cf.cf_kind with
  889. | Var v when not stat || (v.v_read = AccInline && Common.defined ctx.com "haxe3") ->
  890. let e = ctx.g.do_optimize ctx e in
  891. let rec is_const e =
  892. match e.eexpr with
  893. | TConst _ -> true
  894. | TBinop ((OpAdd|OpSub|OpMult|OpDiv|OpMod),e1,e2) -> is_const e1 && is_const e2
  895. | TParenthesis e -> is_const e
  896. | TTypeExpr e -> true
  897. | _ -> false
  898. in
  899. if not (is_const e) then display_error ctx "Variable initialization must be a constant value" p;
  900. e
  901. | _ ->
  902. e
  903. ) in
  904. cf.cf_expr <- Some e;
  905. cf.cf_type <- t;
  906. end;
  907. t
  908. ) in
  909. bind_type cf r (snd e) false
  910. in
  911. (* ----------------------- FIELD INIT ----------------------------- *)
  912. let loop_cf f =
  913. let name = f.cff_name in
  914. let p = f.cff_pos in
  915. let stat = List.mem AStatic f.cff_access in
  916. let inline = List.mem AInline f.cff_access in
  917. let override = List.mem AOverride f.cff_access in
  918. let ctx = { ctx with curclass = c; tthis = tthis } in
  919. match f.cff_kind with
  920. | FVar (t,e) ->
  921. if inline && not stat then error "Inline variable must be static" p;
  922. if inline && e = None then error "Inline variable must be initialized" p;
  923. if override then error "You cannot override variables" p;
  924. let t = (match t with
  925. | None when not stat && e = None ->
  926. error ("Type required for member variable " ^ name) p;
  927. | None ->
  928. mk_mono()
  929. | Some t ->
  930. let old = ctx.type_params in
  931. if stat then ctx.type_params <- [];
  932. let t = load_complex_type ctx p t in
  933. if stat then ctx.type_params <- old;
  934. t
  935. ) in
  936. let cf = {
  937. cf_name = name;
  938. cf_doc = f.cff_doc;
  939. cf_meta = f.cff_meta;
  940. cf_type = t;
  941. cf_pos = f.cff_pos;
  942. cf_kind = Var (if inline then { v_read = AccInline ; v_write = AccNever } else { v_read = AccNormal; v_write = AccNormal });
  943. cf_expr = None;
  944. cf_public = is_public f.cff_access None;
  945. cf_params = [];
  946. cf_overloads = [];
  947. } in
  948. let delay = bind_var ctx cf e stat inline in
  949. f, false, cf, delay
  950. | FFun fd ->
  951. let params = type_function_params ctx fd f.cff_name f.cff_meta p in
  952. if inline && c.cl_interface then error "You can't declare inline methods in interfaces" p;
  953. let is_macro = (is_macro && stat) || has_meta ":macro" f.cff_meta in
  954. let f, stat, fd = if not is_macro || stat then
  955. f, stat, fd
  956. else if ctx.in_macro then
  957. (* non-static macros methods are turned into static when we are running the macro *)
  958. { f with cff_access = AStatic :: f.cff_access }, true, fd
  959. else
  960. (* remove display of first argument which will contain the "this" expression *)
  961. f, stat, { fd with f_args = match fd.f_args with [] -> [] | _ :: l -> l }
  962. in
  963. let fd = if not is_macro then
  964. fd
  965. else if ctx.in_macro then
  966. let texpr = CTPath { tpackage = ["haxe";"macro"]; tname = "Expr"; tparams = []; tsub = None } in
  967. {
  968. f_params = fd.f_params;
  969. f_type = (match fd.f_type with None -> Some texpr | t -> t);
  970. f_args = List.map (fun (a,o,t,e) -> a,o,(match t with None -> Some texpr | _ -> t),e) fd.f_args;
  971. f_expr = fd.f_expr;
  972. }
  973. else
  974. let tdyn = Some (CTPath { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None }) in
  975. let to_dyn = function
  976. | { tpackage = ["haxe";"macro"]; tname = "Expr"; tsub = Some ("ExprRequire"|"ExprOf"); tparams = [TPType t] } -> Some t
  977. | { tpackage = []; tname = ("ExprRequire"|"ExprOf"); tsub = None; tparams = [TPType t] } -> Some t
  978. | _ -> tdyn
  979. in
  980. {
  981. f_params = fd.f_params;
  982. f_type = (match fd.f_type with Some (CTPath t) -> to_dyn t | _ -> tdyn);
  983. f_args = List.map (fun (a,o,t,_) -> a,o,(match t with Some (CTPath t) -> to_dyn t | _ -> tdyn),None) fd.f_args;
  984. f_expr = None;
  985. }
  986. in
  987. let parent = (if not stat then get_parent c name else None) in
  988. let dynamic = List.mem ADynamic f.cff_access || (match parent with Some { cf_kind = Method MethDynamic } -> true | _ -> false) in
  989. if inline && dynamic then error "You can't have both 'inline' and 'dynamic'" p;
  990. ctx.curmethod <- name;
  991. ctx.type_params <- if stat then params else params @ ctx.type_params;
  992. let ret = type_opt ctx p fd.f_type in
  993. let args = List.map (fun (name,opt,t,c) ->
  994. let t, c = type_function_param ctx (type_opt ctx p t) c opt p in
  995. name, c, t
  996. ) fd.f_args in
  997. let t = TFun (fun_args args,ret) in
  998. let constr = (name = "new") in
  999. if constr && c.cl_interface then error "An interface cannot have a constructor" p;
  1000. if c.cl_interface && not stat && fd.f_expr <> None then error "An interface method cannot have a body" p;
  1001. if constr then (match fd.f_type with
  1002. | None | Some (CTPath { tpackage = []; tname = "Void" }) -> ()
  1003. | _ -> error "A class constructor can't have a return value" p
  1004. );
  1005. let cf = {
  1006. cf_name = name;
  1007. cf_doc = f.cff_doc;
  1008. cf_meta = f.cff_meta;
  1009. cf_type = t;
  1010. cf_pos = f.cff_pos;
  1011. cf_kind = Method (if is_macro then MethMacro else if inline then MethInline else if dynamic then MethDynamic else MethNormal);
  1012. cf_expr = None;
  1013. cf_public = is_public f.cff_access parent;
  1014. cf_params = params;
  1015. cf_overloads = [];
  1016. } in
  1017. init_meta_overloads ctx cf;
  1018. let r = exc_protect ctx (fun r ->
  1019. if not !return_partial_type then begin
  1020. r := (fun() -> t);
  1021. incr stats.s_methods_typed;
  1022. if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ name);
  1023. let e , fargs = type_function ctx args ret (if constr then FConstructor else if stat then FStatic else FMember) fd p in
  1024. let f = {
  1025. tf_args = fargs;
  1026. tf_type = ret;
  1027. tf_expr = e;
  1028. } in
  1029. if stat && name = "__init__" then
  1030. (match e.eexpr with
  1031. | TBlock [] | TBlock [{ eexpr = TConst _ }] | TConst _ | TObjectDecl [] -> ()
  1032. | _ -> c.cl_init <- Some e);
  1033. if not (constr || inline) then mark_used cf;
  1034. if has_meta ":defineFeature" cf.cf_meta then add_feature ctx.com (s_type_path c.cl_path ^ "." ^ cf.cf_name);
  1035. cf.cf_expr <- Some (mk (TFunction f) t p);
  1036. cf.cf_type <- t;
  1037. end;
  1038. t
  1039. ) in
  1040. let delay = if ((c.cl_extern && not inline) || c.cl_interface) && cf.cf_name <> "__init__" then
  1041. (fun() -> ())
  1042. else
  1043. bind_type cf r (match fd.f_expr with Some e -> snd e | None -> f.cff_pos) is_macro
  1044. in
  1045. f, constr, cf, delay
  1046. | FProp (get,set,t,eo) ->
  1047. if override then error "You cannot override properties" p;
  1048. let ret = load_complex_type ctx p t in
  1049. let check_get = ref (fun() -> ()) in
  1050. let check_set = ref (fun() -> ()) in
  1051. let check_method m t () =
  1052. if ctx.com.display then () else
  1053. try
  1054. let t2 = (if stat then (PMap.find m c.cl_statics).cf_type else fst (class_field c m)) in
  1055. unify_raise ctx t2 t p;
  1056. with
  1057. | Error (Unify l,_) -> raise (Error (Stack (Custom ("In method " ^ m ^ " required by property " ^ name),Unify l),p))
  1058. | Not_found -> if not (c.cl_interface || c.cl_extern) then display_error ctx ("Method " ^ m ^ " required by property " ^ name ^ " is missing") p
  1059. in
  1060. let get = (match get with
  1061. | "null" -> AccNo
  1062. | "dynamic" -> AccCall ("get_" ^ name)
  1063. | "never" -> AccNever
  1064. | "default" -> AccNormal
  1065. | _ ->
  1066. check_get := check_method get (TFun ([],ret));
  1067. AccCall get
  1068. ) in
  1069. let set = (match set with
  1070. | "null" ->
  1071. (* standard flash library read-only variables can't be accessed for writing, even in subclasses *)
  1072. if c.cl_extern && (match c.cl_path with "flash" :: _ , _ -> true | _ -> false) && Common.defined ctx.com "flash9" then
  1073. AccNever
  1074. else
  1075. AccNo
  1076. | "never" -> AccNever
  1077. | "dynamic" -> AccCall ("set_" ^ name)
  1078. | "default" -> AccNormal
  1079. | _ ->
  1080. check_set := check_method set (TFun (["",false,ret],ret));
  1081. AccCall set
  1082. ) in
  1083. if set = AccNormal && (match get with AccCall _ -> true | _ -> false) then error "Unsupported property combination" p;
  1084. let cf = {
  1085. cf_name = name;
  1086. cf_doc = f.cff_doc;
  1087. cf_meta = f.cff_meta;
  1088. cf_pos = f.cff_pos;
  1089. cf_kind = Var { v_read = get; v_write = set };
  1090. cf_expr = None;
  1091. cf_type = ret;
  1092. cf_public = is_public f.cff_access None;
  1093. cf_params = [];
  1094. cf_overloads = [];
  1095. } in
  1096. let delay = bind_var ctx cf eo stat inline in
  1097. f, false, cf, (fun() -> delay(); (!check_get)(); (!check_set)())
  1098. in
  1099. let rec check_require = function
  1100. | [] -> None
  1101. | (":require",conds,_) :: l ->
  1102. let rec loop = function
  1103. | [] -> check_require l
  1104. | (EConst (Ident i),_) :: l ->
  1105. if not (Common.defined ctx.com i) then
  1106. Some i
  1107. else
  1108. loop l
  1109. | _ -> error "Invalid require identifier" p
  1110. in
  1111. loop conds
  1112. | _ :: l ->
  1113. check_require l
  1114. in
  1115. let cl_req = check_require c.cl_meta in
  1116. let fl = List.fold_left (fun acc f ->
  1117. try
  1118. let p = f.cff_pos in
  1119. let fd , constr, f , delayed = loop_cf f in
  1120. let is_static = List.mem AStatic fd.cff_access in
  1121. if is_static && f.cf_name = "name" && Common.defined ctx.com "js" then error "This identifier cannot be used in Javascript for statics" p;
  1122. if (is_static || constr) && c.cl_interface && f.cf_name <> "__init__" then error "You can't declare static fields in interfaces" p;
  1123. let req = check_require fd.cff_meta in
  1124. let req = (match req with None -> if is_static || constr then cl_req else None | _ -> req) in
  1125. (match req with
  1126. | None -> ()
  1127. | Some r -> f.cf_kind <- Var { v_read = AccRequire r; v_write = AccRequire r });
  1128. if constr then begin
  1129. if c.cl_constructor <> None then error "Duplicate constructor" p;
  1130. c.cl_constructor <- Some f;
  1131. end else if not is_static || f.cf_name <> "__init__" then begin
  1132. if PMap.mem f.cf_name (if is_static then c.cl_statics else c.cl_fields) then error ("Duplicate class field declaration : " ^ f.cf_name) p;
  1133. if PMap.exists f.cf_name (if is_static then c.cl_fields else c.cl_statics) then error ("Same field name can't be use for both static and instance : " ^ f.cf_name) p;
  1134. if is_static then begin
  1135. c.cl_statics <- PMap.add f.cf_name f c.cl_statics;
  1136. c.cl_ordered_statics <- f :: c.cl_ordered_statics;
  1137. end else begin
  1138. c.cl_fields <- PMap.add f.cf_name f c.cl_fields;
  1139. c.cl_ordered_fields <- f :: c.cl_ordered_fields;
  1140. if List.mem AOverride fd.cff_access then c.cl_overrides <- f.cf_name :: c.cl_overrides;
  1141. end;
  1142. end;
  1143. delayed :: acc
  1144. with Error (Custom str,p) ->
  1145. display_error ctx str p;
  1146. acc
  1147. ) [] fields in
  1148. c.cl_ordered_statics <- List.rev c.cl_ordered_statics;
  1149. c.cl_ordered_fields <- List.rev c.cl_ordered_fields;
  1150. (*
  1151. make sure a default contructor with same access as super one will be added to the class structure at some point.
  1152. *)
  1153. let rec add_constructor c =
  1154. match c.cl_constructor, c.cl_super with
  1155. | None, Some (csup,cparams) when not c.cl_extern ->
  1156. add_constructor csup;
  1157. (match csup.cl_constructor with
  1158. | None -> ()
  1159. | Some cf ->
  1160. ignore (follow cf.cf_type); (* make sure it's typed *)
  1161. let args = (match cf.cf_expr with
  1162. | Some { eexpr = TFunction f } ->
  1163. List.map (fun (v,def) ->
  1164. (*
  1165. let's optimize a bit the output by not always copying the default value
  1166. into the inherited constructor when it's not necessary for the platform
  1167. *)
  1168. match ctx.com.platform, def with
  1169. | (Php | Js | Neko | Flash8), Some _ -> v, (Some TNull)
  1170. | Flash, Some (TString _) -> v, (Some TNull)
  1171. | Cpp, Some (TString _) -> v, def
  1172. | Cpp, Some _ -> { v with v_type = ctx.t.tnull v.v_type }, (Some TNull)
  1173. | _ -> v, def
  1174. ) f.tf_args
  1175. | _ ->
  1176. match follow cf.cf_type with
  1177. | TFun (args,_) -> List.map (fun (n,o,t) -> alloc_var n (if o then ctx.t.tnull t else t), if o then Some TNull else None) args
  1178. | _ -> assert false
  1179. ) in
  1180. let p = c.cl_pos in
  1181. let vars = List.map (fun (v,def) -> alloc_var v.v_name (apply_params csup.cl_types cparams v.v_type), def) args in
  1182. let super_call = mk (TCall (mk (TConst TSuper) (TInst (csup,cparams)) p,List.map (fun (v,_) -> mk (TLocal v) v.v_type p) vars)) ctx.t.tvoid p in
  1183. let constr = mk (TFunction {
  1184. tf_args = vars;
  1185. tf_type = ctx.t.tvoid;
  1186. tf_expr = super_call;
  1187. }) (TFun (List.map (fun (v,c) -> v.v_name, c <> None, v.v_type) vars,ctx.t.tvoid)) p in
  1188. c.cl_constructor <- Some { cf with cf_pos = p; cf_type = constr.etype; cf_meta = []; cf_doc = None; cf_expr = Some constr })
  1189. | _ ->
  1190. (* nothing to do *)
  1191. ()
  1192. in
  1193. delay ctx (fun() -> add_constructor c);
  1194. List.rev fl
  1195. let resolve_typedef ctx t =
  1196. match t with
  1197. | TClassDecl _ | TEnumDecl _ -> t
  1198. | TTypeDecl td ->
  1199. match follow td.t_type with
  1200. | TEnum (e,_) -> TEnumDecl e
  1201. | TInst (c,_) -> TClassDecl c
  1202. | _ -> t
  1203. let add_module ctx m p =
  1204. let decl_type t =
  1205. let t = t_infos t in
  1206. try
  1207. let m2 = Hashtbl.find ctx.g.types_module t.mt_path in
  1208. if m.m_path <> m2 && String.lowercase (s_type_path m2) = String.lowercase (s_type_path m.m_path) then error ("Module " ^ s_type_path m2 ^ " is loaded with a different case than " ^ s_type_path m.m_path) p;
  1209. error ("Type name " ^ s_type_path t.mt_path ^ " is redefined from module " ^ s_type_path m2) p
  1210. with
  1211. Not_found ->
  1212. Hashtbl.add ctx.g.types_module t.mt_path m.m_path
  1213. in
  1214. List.iter decl_type m.m_types;
  1215. Hashtbl.add ctx.g.modules m.m_path m
  1216. let type_module ctx m file tdecls loadp =
  1217. (* PASS 1 : build module structure - does not load any module or type - should be atomic ! *)
  1218. let decls = ref [] in
  1219. let make_path name priv =
  1220. if List.exists (fun t -> snd (t_path t) = name) (!decls) then error ("Type name " ^ name ^ " is already defined in this module") loadp;
  1221. if priv then (fst m @ ["_" ^ snd m], name) else (fst m, name)
  1222. in
  1223. let m = {
  1224. m_id = alloc_mid();
  1225. m_path = m;
  1226. m_types = [];
  1227. m_extra = module_extra (Common.unique_full_path file) (Common.get_signature ctx.com) (file_time file) (if ctx.in_macro then MMacro else MCode);
  1228. } in
  1229. List.iter (fun (d,p) ->
  1230. match d with
  1231. | EImport _ | EUsing _ -> ()
  1232. | EClass d ->
  1233. let priv = List.mem HPrivate d.d_flags in
  1234. let path = make_path d.d_name priv in
  1235. let c = mk_class m path p in
  1236. c.cl_module <- m;
  1237. c.cl_private <- priv;
  1238. c.cl_doc <- d.d_doc;
  1239. c.cl_meta <- d.d_meta;
  1240. decls := TClassDecl c :: !decls
  1241. | EEnum d ->
  1242. let priv = List.mem EPrivate d.d_flags in
  1243. let path = make_path d.d_name priv in
  1244. let e = {
  1245. e_path = path;
  1246. e_module = m;
  1247. e_pos = p;
  1248. e_doc = d.d_doc;
  1249. e_meta = d.d_meta;
  1250. e_types = [];
  1251. e_private = priv;
  1252. e_extern = List.mem EExtern d.d_flags;
  1253. e_constrs = PMap.empty;
  1254. e_names = [];
  1255. } in
  1256. decls := TEnumDecl e :: !decls
  1257. | ETypedef d ->
  1258. let priv = List.mem EPrivate d.d_flags in
  1259. let path = make_path d.d_name priv in
  1260. let t = {
  1261. t_path = path;
  1262. t_module = m;
  1263. t_pos = p;
  1264. t_doc = d.d_doc;
  1265. t_private = priv;
  1266. t_types = [];
  1267. t_type = mk_mono();
  1268. t_meta = d.d_meta;
  1269. } in
  1270. decls := TTypeDecl t :: !decls
  1271. ) tdecls;
  1272. m.m_types <- List.rev !decls;
  1273. add_module ctx m loadp;
  1274. (* PASS 2 : build types structure - does not type any expression ! *)
  1275. let ctx = {
  1276. com = ctx.com;
  1277. g = ctx.g;
  1278. t = ctx.t;
  1279. macro_depth = ctx.macro_depth;
  1280. curclass = ctx.curclass;
  1281. tthis = ctx.tthis;
  1282. ret = ctx.ret;
  1283. current = m;
  1284. locals = PMap.empty;
  1285. local_types = ctx.g.std.m_types @ m.m_types;
  1286. local_using = [];
  1287. type_params = [];
  1288. curmethod = "";
  1289. curfun = FStatic;
  1290. untyped = false;
  1291. in_super_call = false;
  1292. in_macro = ctx.in_macro;
  1293. in_display = false;
  1294. in_loop = false;
  1295. opened = [];
  1296. param_type = None;
  1297. vthis = None;
  1298. } in
  1299. let delays = ref [] in
  1300. let get_class name =
  1301. let c = List.find (fun d -> match d with TClassDecl { cl_path = _ , n } -> n = name | _ -> false) m.m_types in
  1302. match c with TClassDecl c -> c | _ -> assert false
  1303. in
  1304. let get_enum name =
  1305. let e = List.find (fun d -> match d with TEnumDecl { e_path = _ , n } -> n = name | _ -> false) m.m_types in
  1306. match e with TEnumDecl e -> e | _ -> assert false
  1307. in
  1308. let get_tdef name =
  1309. let s = List.find (fun d -> match d with TTypeDecl { t_path = _ , n } -> n = name | _ -> false) m.m_types in
  1310. match s with TTypeDecl s -> s | _ -> assert false
  1311. in
  1312. (* here is an additional PASS 1 phase, which handle the type parameters declaration, with lazy contraints *)
  1313. List.iter (fun (d,p) ->
  1314. match d with
  1315. | EImport _ | EUsing _ -> ()
  1316. | EClass d ->
  1317. let c = get_class d.d_name in
  1318. c.cl_types <- List.map (type_type_params ctx c.cl_path (fun() -> c.cl_types) p) d.d_params;
  1319. | EEnum d ->
  1320. let e = get_enum d.d_name in
  1321. e.e_types <- List.map (type_type_params ctx e.e_path (fun() -> e.e_types) p) d.d_params;
  1322. | ETypedef d ->
  1323. let t = get_tdef d.d_name in
  1324. t.t_types <- List.map (type_type_params ctx t.t_path (fun() -> t.t_types) p) d.d_params;
  1325. ) tdecls;
  1326. (* back to PASS2 *)
  1327. List.iter (fun (d,p) ->
  1328. match d with
  1329. | EImport t ->
  1330. (match t.tsub with
  1331. | None ->
  1332. let md = ctx.g.do_load_module ctx (t.tpackage,t.tname) p in
  1333. let types = List.filter (fun t -> not (t_infos t).mt_private) md.m_types in
  1334. ctx.local_types <- ctx.local_types @ types
  1335. | Some _ ->
  1336. let t = load_type_def ctx p t in
  1337. ctx.local_types <- ctx.local_types @ [t]
  1338. )
  1339. | EUsing t ->
  1340. (match t.tsub with
  1341. | None ->
  1342. let md = ctx.g.do_load_module ctx (t.tpackage,t.tname) p in
  1343. let types = List.filter (fun t -> not (t_infos t).mt_private) md.m_types in
  1344. ctx.local_using <- ctx.local_using @ (List.map (resolve_typedef ctx) types);
  1345. ctx.local_types <- ctx.local_types @ types
  1346. | Some _ ->
  1347. let t = load_type_def ctx p t in
  1348. ctx.local_using<- ctx.local_using @ [resolve_typedef ctx t];
  1349. ctx.local_types <- ctx.local_types @ [t])
  1350. | EClass d ->
  1351. let c = get_class d.d_name in
  1352. let checks = if not ctx.com.display then [check_overriding ctx c p; check_interfaces ctx c p] else [] in
  1353. delays := !delays @ (checks @ init_class ctx c p d.d_flags d.d_data)
  1354. | EEnum d ->
  1355. let e = get_enum d.d_name in
  1356. let ctx = { ctx with type_params = e.e_types } in
  1357. let h = (try Some (Hashtbl.find ctx.g.type_patches e.e_path) with Not_found -> None) in
  1358. (match h with
  1359. | None -> ()
  1360. | Some (h,hcl) ->
  1361. Hashtbl.iter (fun _ _ -> error "Field type patch not supported for enums" e.e_pos) h;
  1362. e.e_meta <- e.e_meta @ hcl.tp_meta);
  1363. let constructs = ref d.d_data in
  1364. let get_constructs() =
  1365. List.map (fun (c,doc,meta,pl,p) ->
  1366. {
  1367. cff_name = c;
  1368. cff_doc = doc;
  1369. cff_meta = meta;
  1370. cff_pos = p;
  1371. cff_access = [];
  1372. cff_kind = (match pl with
  1373. | [] -> FVar (None,None)
  1374. | _ -> FFun { f_params = []; f_type = None; f_expr = None; f_args = List.map (fun (n,o,t) -> n,o,Some t,None) pl });
  1375. }
  1376. ) (!constructs)
  1377. in
  1378. build_module_def ctx (TEnumDecl e) e.e_meta get_constructs (fun (e,p) ->
  1379. match e with
  1380. | EVars [_,Some (CTAnonymous fields),None] ->
  1381. constructs := List.map (fun f ->
  1382. (f.cff_name,f.cff_doc,f.cff_meta,(match f.cff_kind with
  1383. | FVar (None,None) -> []
  1384. | FFun { f_params = []; f_type = None; f_expr = (None|Some (EBlock [],_)); f_args = pl } -> List.map (fun (n,o,t,_) -> match t with None -> error "Missing function parameter type" f.cff_pos | Some t -> n,o,t) pl
  1385. | _ -> error "Invalid enum constructor in @:build result" p
  1386. ),f.cff_pos)
  1387. ) fields
  1388. | _ -> error "Enum build macro must return a single variable with anonymous object fields" p
  1389. );
  1390. let et = TEnum (e,List.map snd e.e_types) in
  1391. let names = ref [] in
  1392. let index = ref 0 in
  1393. List.iter (fun (c,doc,meta,t,p) ->
  1394. if c = "name" && Common.defined ctx.com "js" then error "This identifier cannot be used in Javascript" p;
  1395. let t = (match t with
  1396. | [] -> et
  1397. | l ->
  1398. let pnames = ref PMap.empty in
  1399. TFun (List.map (fun (s,opt,t) ->
  1400. if PMap.mem s (!pnames) then error ("Duplicate parameter '" ^ s ^ "' in enum constructor " ^ c) p;
  1401. pnames := PMap.add s () (!pnames);
  1402. s, opt, load_type_opt ~opt ctx p (Some t)
  1403. ) l, et)
  1404. ) in
  1405. if PMap.mem c e.e_constrs then error ("Duplicate constructor " ^ c) p;
  1406. e.e_constrs <- PMap.add c {
  1407. ef_name = c;
  1408. ef_type = t;
  1409. ef_pos = p;
  1410. ef_doc = doc;
  1411. ef_index = !index;
  1412. ef_meta = meta;
  1413. } e.e_constrs;
  1414. incr index;
  1415. names := c :: !names;
  1416. ) (!constructs);
  1417. e.e_names <- List.rev !names;
  1418. e.e_extern <- e.e_extern || e.e_names = [];
  1419. | ETypedef d ->
  1420. let t = get_tdef d.d_name in
  1421. let ctx = { ctx with type_params = t.t_types } in
  1422. let tt = load_complex_type ctx p d.d_data in
  1423. if t.t_type == follow tt then error "Recursive typedef is not allowed" p;
  1424. (match t.t_type with
  1425. | TMono r ->
  1426. (match !r with
  1427. | None -> r := Some tt;
  1428. | Some _ -> assert false);
  1429. | _ -> assert false);
  1430. ) tdecls;
  1431. (* PASS 3 : type checking, delayed until all modules and types are built *)
  1432. List.iter (delay ctx) (List.rev (!delays));
  1433. m
  1434. let resolve_module_file com m remap p =
  1435. let file = (match m with
  1436. | [] , name -> name
  1437. | x :: l , name ->
  1438. let x = (try
  1439. match PMap.find x com.package_rules with
  1440. | Forbidden -> raise (Error (Forbid_package (x,m),p));
  1441. | Directory d -> d
  1442. | Remap d -> remap := d :: l; d
  1443. with Not_found -> x
  1444. ) in
  1445. String.concat "/" (x :: l) ^ "/" ^ name
  1446. ) ^ ".hx" in
  1447. let file = Common.find_file com file in
  1448. match String.lowercase (snd m) with
  1449. | "con" | "aux" | "prn" | "nul" | "com1" | "com2" | "com3" | "lpt1" | "lpt2" | "lpt3" when Sys.os_type = "Win32" ->
  1450. (* these names are reserved by the OS - old DOS legacy, such files cannot be easily created but are reported as visible *)
  1451. if (try (Unix.stat file).Unix.st_size with _ -> 0) > 0 then file else raise Not_found
  1452. | _ -> file
  1453. let parse_module ctx m p =
  1454. let remap = ref (fst m) in
  1455. let file = resolve_module_file ctx.com m remap p in
  1456. let pack, decls = (!parse_hook) ctx.com file p in
  1457. if pack <> !remap then begin
  1458. let spack m = if m = [] then "<empty>" else String.concat "." m in
  1459. if p == Ast.null_pos then
  1460. display_error ctx ("Invalid commandline class : " ^ s_type_path m ^ " should be " ^ s_type_path (pack,snd m)) p
  1461. else
  1462. display_error ctx ("Invalid package : " ^ spack (fst m) ^ " should be " ^ spack pack) p
  1463. end;
  1464. file, if !remap <> fst m then
  1465. (* build typedefs to redirect to real package *)
  1466. List.rev (List.fold_left (fun acc (t,p) ->
  1467. let build f d =
  1468. let priv = List.mem f d.d_flags in
  1469. (ETypedef {
  1470. d_name = d.d_name;
  1471. d_doc = None;
  1472. d_meta = [];
  1473. d_params = d.d_params;
  1474. d_flags = if priv then [EPrivate] else [];
  1475. d_data = CTPath (if priv then { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None; } else
  1476. {
  1477. tpackage = !remap;
  1478. tname = d.d_name;
  1479. tparams = List.map (fun (s,_) ->
  1480. TPType (CTPath { tpackage = []; tname = s; tparams = []; tsub = None; })
  1481. ) d.d_params;
  1482. tsub = None;
  1483. });
  1484. },p) :: acc
  1485. in
  1486. match t with
  1487. | EClass d -> build HPrivate d
  1488. | EEnum d -> build EPrivate d
  1489. | ETypedef d -> build EPrivate d
  1490. | EImport _ | EUsing _ -> acc
  1491. ) [(EImport { tpackage = !remap; tname = snd m; tparams = []; tsub = None; },null_pos)] decls)
  1492. else
  1493. decls
  1494. let load_module ctx m p =
  1495. let m2 = (try
  1496. Hashtbl.find ctx.g.modules m
  1497. with
  1498. Not_found ->
  1499. match !type_module_hook ctx m p with
  1500. | Some m -> m
  1501. | None ->
  1502. let file, decls = (try
  1503. parse_module ctx m p
  1504. with Not_found ->
  1505. let rec loop = function
  1506. | [] ->
  1507. if s_type_path m = "neko.db._Mysql.D" then begin
  1508. prerr_endline (String.concat ";" (fst m));
  1509. assert false;
  1510. end;
  1511. raise (Error (Module_not_found m,p))
  1512. | load :: l ->
  1513. match load m p with
  1514. | None -> loop l
  1515. | Some (file,(_,a)) -> file, a
  1516. in
  1517. loop ctx.com.load_extern_type
  1518. ) in
  1519. type_module ctx m file decls p
  1520. ) in
  1521. add_dependency ctx.current m2;
  1522. m2