typeload.ml 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  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. c.cl_meta <- (":hack",[],p) :: c.cl_meta;
  559. let t = TInst (c,[]) in
  560. match flags with
  561. | [] -> n, t
  562. | _ ->
  563. let r = exc_protect ctx (fun r ->
  564. r := (fun _ -> t);
  565. let ctx = { ctx with type_params = ctx.type_params @ get_params() } in
  566. set_heritance ctx c (List.map (fun t -> match t with CTPath t -> HImplements t | _ -> error "Unsupported type constraint" p) flags) p;
  567. t
  568. ) in
  569. delay ctx (fun () -> ignore(!r()));
  570. n, TLazy r
  571. let type_function_params ctx fd fname fmeta p =
  572. let params = ref [] in
  573. params := List.map (fun (n,flags) ->
  574. type_type_params ctx ([],fname) (fun() -> !params) p (n,flags)
  575. ) fd.f_params;
  576. !params
  577. let type_function ctx args ret fmode f p =
  578. let locals = save_locals ctx in
  579. let fargs = List.map (fun (n,c,t) ->
  580. let c = (match c with
  581. | None -> None
  582. | Some e ->
  583. let p = pos e in
  584. let e = ctx.g.do_optimize ctx (type_expr ctx e true) in
  585. unify ctx e.etype t p;
  586. match e.eexpr with
  587. | TConst c -> Some c
  588. | _ -> display_error ctx "Parameter default value should be constant" p; None
  589. ) in
  590. add_local ctx n t, c
  591. ) args in
  592. let old_ret = ctx.ret in
  593. let old_fun = ctx.curfun in
  594. let old_opened = ctx.opened in
  595. ctx.curfun <- fmode;
  596. ctx.ret <- ret;
  597. ctx.opened <- [];
  598. let e = type_expr ctx (match f.f_expr with None -> error "Function body required" p | Some e -> e) false in
  599. let rec loop e =
  600. match e.eexpr with
  601. | TReturn (Some _) -> raise Exit
  602. | TFunction _ -> ()
  603. | _ -> Type.iter loop e
  604. in
  605. let have_ret = (try loop e; false with Exit -> true) in
  606. if have_ret then
  607. (try return_flow ctx e with Exit -> ())
  608. else (try unify_raise ctx ret ctx.t.tvoid p
  609. with Error(Unify _,_) -> display_error ctx ("Missing return: " ^ (s_type (print_context()) ret)) p);
  610. let rec loop e =
  611. match e.eexpr with
  612. | TCall ({ eexpr = TConst TSuper },_) -> raise Exit
  613. | TFunction _ -> ()
  614. | _ -> Type.iter loop e
  615. in
  616. let has_super_constr() =
  617. match ctx.curclass.cl_super with
  618. | None -> false
  619. | Some (csup,_) ->
  620. try ignore(get_constructor (fun f->f.cf_type) csup); true with Not_found -> false
  621. in
  622. if fmode = FConstructor && has_super_constr() then
  623. (try
  624. loop e;
  625. display_error ctx "Missing super constructor call" p
  626. with
  627. Exit -> ());
  628. locals();
  629. let e = match ctx.curfun, ctx.vthis with
  630. | (FMember|FConstructor), Some v ->
  631. let ev = mk (TVars [v,Some (mk (TConst TThis) ctx.tthis p)]) ctx.t.tvoid p in
  632. (match e.eexpr with
  633. | TBlock l -> { e with eexpr = TBlock (ev::l) }
  634. | _ -> mk (TBlock [ev;e]) e.etype p)
  635. | _ -> e
  636. in
  637. List.iter (fun r -> r := Closed) ctx.opened;
  638. ctx.ret <- old_ret;
  639. ctx.curfun <- old_fun;
  640. ctx.opened <- old_opened;
  641. e , fargs
  642. let init_core_api ctx c =
  643. let ctx2 = (match ctx.g.core_api with
  644. | None ->
  645. let com2 = Common.clone ctx.com in
  646. Common.define com2 "core_api";
  647. com2.class_path <- ctx.com.std_path;
  648. let ctx2 = ctx.g.do_create com2 ctx.in_macro in
  649. ctx.g.core_api <- Some ctx2;
  650. ctx2
  651. | Some c ->
  652. c
  653. ) in
  654. let t = load_instance ctx2 { tpackage = fst c.cl_path; tname = snd c.cl_path; tparams = []; tsub = None; } c.cl_pos true in
  655. match t with
  656. | TInst (ccore,_) ->
  657. (match c.cl_doc with
  658. | None -> c.cl_doc <- ccore.cl_doc
  659. | Some _ -> ());
  660. let compare_fields f f2 =
  661. let p = (match f2.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  662. (try
  663. type_eq EqCoreType (apply_params ccore.cl_types (List.map snd c.cl_types) f.cf_type) f2.cf_type
  664. with Unify_error l ->
  665. display_error ctx ("Field " ^ f.cf_name ^ " has different type than in core type") p;
  666. display_error ctx (error_msg (Unify l)) p);
  667. if f2.cf_public <> f.cf_public then error ("Field " ^ f.cf_name ^ " has different visibility than core type") p;
  668. (match f2.cf_doc with
  669. | None -> f2.cf_doc <- f.cf_doc
  670. | Some _ -> ());
  671. if f2.cf_kind <> f.cf_kind then begin
  672. match f2.cf_kind, f.cf_kind with
  673. | Method MethInline, Method MethNormal -> () (* allow to add 'inline' *)
  674. | Method MethNormal, Method MethInline -> () (* allow to disable 'inline' *)
  675. | _ ->
  676. error ("Field " ^ f.cf_name ^ " has different property access than core type") p;
  677. end;
  678. (match follow f.cf_type, follow f2.cf_type with
  679. | TFun (pl1,_), TFun (pl2,_) ->
  680. if List.length pl1 != List.length pl2 then assert false;
  681. List.iter2 (fun (n1,_,_) (n2,_,_) ->
  682. if n1 <> n2 then error ("Method parameter name '" ^ n2 ^ "' should be '" ^ n1 ^ "'") p;
  683. ) pl1 pl2;
  684. | _ -> ());
  685. in
  686. let check_fields fcore fl =
  687. PMap.iter (fun i f ->
  688. if not f.cf_public then () else
  689. let f2 = try PMap.find f.cf_name fl with Not_found -> error ("Missing field " ^ i ^ " required by core type") c.cl_pos in
  690. compare_fields f f2;
  691. ) fcore;
  692. PMap.iter (fun i f ->
  693. let p = (match f.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  694. 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;
  695. ) fl;
  696. in
  697. check_fields ccore.cl_fields c.cl_fields;
  698. check_fields ccore.cl_statics c.cl_statics;
  699. (match ccore.cl_constructor, c.cl_constructor with
  700. | None, None -> ()
  701. | Some f, Some f2 -> compare_fields f f2
  702. | None, Some { cf_public = false } -> ()
  703. | _ -> error "Constructor differs from core type" c.cl_pos)
  704. | _ -> assert false
  705. let patch_class ctx c fields =
  706. let h = (try Some (Hashtbl.find ctx.g.type_patches c.cl_path) with Not_found -> None) in
  707. match h with
  708. | None -> fields
  709. | Some (h,hcl) ->
  710. c.cl_meta <- c.cl_meta @ hcl.tp_meta;
  711. let rec loop acc = function
  712. | [] -> acc
  713. | f :: l ->
  714. (* patch arguments types *)
  715. (match f.cff_kind with
  716. | FFun ff ->
  717. let param ((n,opt,t,e) as p) =
  718. try
  719. let t2 = (try Hashtbl.find h (("$" ^ f.cff_name ^ "__" ^ n),false) with Not_found -> Hashtbl.find h (("$" ^ n),false)) in
  720. n, opt, t2.tp_type, e
  721. with Not_found ->
  722. p
  723. in
  724. f.cff_kind <- FFun { ff with f_args = List.map param ff.f_args }
  725. | _ -> ());
  726. (* other patches *)
  727. match (try Some (Hashtbl.find h (f.cff_name,List.mem AStatic f.cff_access)) with Not_found -> None) with
  728. | None -> loop (f :: acc) l
  729. | Some { tp_remove = true } -> loop acc l
  730. | Some p ->
  731. f.cff_meta <- f.cff_meta @ p.tp_meta;
  732. (match p.tp_type with
  733. | None -> ()
  734. | Some t ->
  735. f.cff_kind <- match f.cff_kind with
  736. | FVar (_,e) -> FVar (Some t,e)
  737. | FProp (get,set,_,eo) -> FProp (get,set,t,eo)
  738. | FFun f -> FFun { f with f_type = Some t });
  739. loop (f :: acc) l
  740. in
  741. List.rev (loop [] fields)
  742. let rec string_list_of_expr_path (e,p) =
  743. match e with
  744. | EConst (Ident i) -> [i]
  745. | EField (e,f) -> f :: string_list_of_expr_path e
  746. | _ -> error "Invalid path" p
  747. let build_module_def ctx mt meta fvars fbuild =
  748. let rec loop = function
  749. | (":build",args,p) :: l ->
  750. let epath, el = (match args with
  751. | [ECall (epath,el),p] -> epath, el
  752. | _ -> error "Invalid build parameters" p
  753. ) in
  754. 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
  755. if ctx.in_macro then error "You cannot used :build inside a macro : make sure that your enum is not used in macro" p;
  756. let old = ctx.g.get_build_infos in
  757. ctx.g.get_build_infos <- (fun() -> Some (mt, fvars()));
  758. let r = try apply_macro ctx MBuild s el p with e -> ctx.g.get_build_infos <- old; raise e in
  759. ctx.g.get_build_infos <- old;
  760. (match r with
  761. | None -> error "Build failure" p
  762. | Some e -> fbuild e; loop l)
  763. | _ :: l -> loop l
  764. | [] -> ()
  765. in
  766. try
  767. loop meta
  768. with Error (Custom msg,p) ->
  769. display_error ctx msg p
  770. let init_class ctx c p herits fields =
  771. incr stats.s_classes_built;
  772. let fields = patch_class ctx c fields in
  773. let ctx = { ctx with type_params = c.cl_types } in
  774. c.cl_extern <- List.mem HExtern herits;
  775. c.cl_interface <- List.mem HInterface herits;
  776. if c.cl_path = (["haxe";"macro"],"MacroType") then c.cl_kind <- KMacroType;
  777. set_heritance ctx c herits p;
  778. let fields = ref fields in
  779. let get_fields() = !fields in
  780. build_module_def ctx (TClassDecl c) c.cl_meta get_fields (fun (e,p) ->
  781. match e with
  782. | EVars [_,Some (CTAnonymous f),None] -> fields := f
  783. | _ -> error "Class build macro must return a single variable with anonymous fields" p
  784. );
  785. let fields = !fields in
  786. let core_api = has_meta ":core_api" c.cl_meta in
  787. let is_macro = has_meta ":macro" c.cl_meta in
  788. let fields, herits = if is_macro && not ctx.in_macro then begin
  789. c.cl_extern <- true;
  790. List.filter (fun f -> List.mem AStatic f.cff_access) fields, []
  791. end else fields, herits in
  792. if core_api && not (ctx.com.display || ctx.com.dead_code_elimination) then delay ctx (fun() -> init_core_api ctx c);
  793. let tthis = TInst (c,List.map snd c.cl_types) in
  794. let rec extends_public c =
  795. List.exists (fun (c,_) -> c.cl_path = (["haxe"],"Public") || extends_public c) c.cl_implements ||
  796. match c.cl_super with
  797. | None -> false
  798. | Some (c,_) -> extends_public c
  799. in
  800. let extends_public = extends_public c in
  801. let is_public access parent =
  802. if List.mem APrivate access then
  803. false
  804. else if List.mem APublic access then
  805. true
  806. else match parent with
  807. | Some { cf_public = p } -> p
  808. | _ -> c.cl_extern || c.cl_interface || extends_public
  809. in
  810. let rec get_parent c name =
  811. match c.cl_super with
  812. | None -> None
  813. | Some (csup,_) ->
  814. try
  815. Some (PMap.find name csup.cl_fields)
  816. with
  817. Not_found -> get_parent csup name
  818. in
  819. let type_opt ctx p t =
  820. match t with
  821. | None when c.cl_extern || c.cl_interface ->
  822. display_error ctx "Type required for extern classes and interfaces" p;
  823. t_dynamic
  824. | None when core_api ->
  825. display_error ctx "Type required for core api classes" p;
  826. t_dynamic
  827. | _ ->
  828. load_type_opt ctx p t
  829. in
  830. let rec has_field f = function
  831. | None -> false
  832. | Some (c,_) ->
  833. PMap.exists f c.cl_fields || has_field f c.cl_super || List.exists (fun i -> has_field f (Some i)) c.cl_implements
  834. in
  835. (* ----------------------- COMPLETION ----------------------------- *)
  836. let display_file = if ctx.com.display then Common.unique_full_path p.pfile = (!Parser.resume_display).pfile else false in
  837. let fields = if not display_file || Common.defined ctx.com "no-copt" then fields else Optimizer.optimize_completion c fields in
  838. let mark_used cf = if ctx.com.dead_code_elimination then cf.cf_meta <- (":?used",[],p) :: cf.cf_meta in
  839. let rec is_full_type t =
  840. match t with
  841. | TFun (args,ret) -> is_full_type ret && List.for_all (fun (_,_,t) -> is_full_type t) args
  842. | TMono r -> (match !r with None -> false | Some t -> is_full_type t)
  843. | TInst _ | TEnum _ | TLazy _ | TDynamic _ | TAnon _ | TType _ -> true
  844. in
  845. let bind_type cf r p macro =
  846. if ctx.com.display then begin
  847. let cp = !Parser.resume_display in
  848. if display_file && (cp.pmin = 0 || (p.pmin <= cp.pmin && p.pmax >= cp.pmax)) then begin
  849. if macro && not ctx.in_macro then
  850. (* force macro system loading of this class in order to get completion *)
  851. (fun() -> ignore(ctx.g.do_macro ctx MExpr c.cl_path cf.cf_name [] p))
  852. else begin
  853. cf.cf_type <- TLazy r;
  854. (fun() -> ignore((!r)()))
  855. end
  856. end else begin
  857. if not (is_full_type cf.cf_type) then cf.cf_type <- TLazy r;
  858. (fun() -> ())
  859. end
  860. end else if macro && not ctx.in_macro then
  861. (fun () -> ())
  862. else begin
  863. cf.cf_type <- TLazy r;
  864. if ctx.com.dead_code_elimination && cf.cf_name <> "__init__" then (fun() -> ()) else (fun () -> ignore(!r()))
  865. end
  866. in
  867. let bind_var ctx cf e stat inline =
  868. let p = cf.cf_pos in
  869. 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;
  870. let t = cf.cf_type in
  871. match e with
  872. | None when ctx.com.dead_code_elimination && not ctx.com.display ->
  873. let r = exc_protect ctx (fun r ->
  874. r := (fun() -> t);
  875. mark_used cf;
  876. t
  877. ) in
  878. cf.cf_type <- TLazy r;
  879. (fun() -> ())
  880. | None ->
  881. (fun() -> ())
  882. | Some e ->
  883. let r = exc_protect ctx (fun r ->
  884. if not !return_partial_type then begin
  885. r := (fun() -> t);
  886. 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);
  887. if not inline then mark_used cf;
  888. let e = type_var_field ctx t e stat p in
  889. let e = (match cf.cf_kind with
  890. | Var v when not stat || (v.v_read = AccInline && Common.defined ctx.com "haxe3") ->
  891. let e = ctx.g.do_optimize ctx e in
  892. let rec is_const e =
  893. match e.eexpr with
  894. | TConst _ -> true
  895. | TBinop ((OpAdd|OpSub|OpMult|OpDiv|OpMod),e1,e2) -> is_const e1 && is_const e2
  896. | TParenthesis e -> is_const e
  897. | TTypeExpr e -> true
  898. | _ -> false
  899. in
  900. if not (is_const e) then display_error ctx "Variable initialization must be a constant value" p;
  901. e
  902. | _ ->
  903. e
  904. ) in
  905. cf.cf_expr <- Some e;
  906. cf.cf_type <- t;
  907. end;
  908. t
  909. ) in
  910. bind_type cf r (snd e) false
  911. in
  912. (* ----------------------- FIELD INIT ----------------------------- *)
  913. let loop_cf f =
  914. let name = f.cff_name in
  915. let p = f.cff_pos in
  916. let stat = List.mem AStatic f.cff_access in
  917. let inline = List.mem AInline f.cff_access in
  918. let override = List.mem AOverride f.cff_access in
  919. let ctx = { ctx with curclass = c; tthis = tthis } in
  920. match f.cff_kind with
  921. | FVar (t,e) ->
  922. if inline && not stat then error "Inline variable must be static" p;
  923. if inline && e = None then error "Inline variable must be initialized" p;
  924. if override then error "You cannot override variables" p;
  925. let t = (match t with
  926. | None when not stat && e = None ->
  927. error ("Type required for member variable " ^ name) p;
  928. | None ->
  929. mk_mono()
  930. | Some t ->
  931. let old = ctx.type_params in
  932. if stat then ctx.type_params <- [];
  933. let t = load_complex_type ctx p t in
  934. if stat then ctx.type_params <- old;
  935. t
  936. ) in
  937. let cf = {
  938. cf_name = name;
  939. cf_doc = f.cff_doc;
  940. cf_meta = f.cff_meta;
  941. cf_type = t;
  942. cf_pos = f.cff_pos;
  943. cf_kind = Var (if inline then { v_read = AccInline ; v_write = AccNever } else { v_read = AccNormal; v_write = AccNormal });
  944. cf_expr = None;
  945. cf_public = is_public f.cff_access None;
  946. cf_params = [];
  947. cf_overloads = [];
  948. } in
  949. let delay = bind_var ctx cf e stat inline in
  950. f, false, cf, delay
  951. | FFun fd ->
  952. let params = type_function_params ctx fd f.cff_name f.cff_meta p in
  953. if inline && c.cl_interface then error "You can't declare inline methods in interfaces" p;
  954. let is_macro = (is_macro && stat) || has_meta ":macro" f.cff_meta in
  955. let f, stat, fd = if not is_macro || stat then
  956. f, stat, fd
  957. else if ctx.in_macro then
  958. (* non-static macros methods are turned into static when we are running the macro *)
  959. { f with cff_access = AStatic :: f.cff_access }, true, fd
  960. else
  961. (* remove display of first argument which will contain the "this" expression *)
  962. f, stat, { fd with f_args = match fd.f_args with [] -> [] | _ :: l -> l }
  963. in
  964. let fd = if not is_macro then
  965. fd
  966. else if ctx.in_macro then
  967. let texpr = CTPath { tpackage = ["haxe";"macro"]; tname = "Expr"; tparams = []; tsub = None } in
  968. {
  969. f_params = fd.f_params;
  970. f_type = (match fd.f_type with None -> Some texpr | t -> t);
  971. f_args = List.map (fun (a,o,t,e) -> a,o,(match t with None -> Some texpr | _ -> t),e) fd.f_args;
  972. f_expr = fd.f_expr;
  973. }
  974. else
  975. let tdyn = Some (CTPath { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None }) in
  976. let to_dyn = function
  977. | { tpackage = ["haxe";"macro"]; tname = "Expr"; tsub = Some ("ExprRequire"|"ExprOf"); tparams = [TPType t] } -> Some t
  978. | { tpackage = []; tname = ("ExprRequire"|"ExprOf"); tsub = None; tparams = [TPType t] } -> Some t
  979. | _ -> tdyn
  980. in
  981. {
  982. f_params = fd.f_params;
  983. f_type = (match fd.f_type with Some (CTPath t) -> to_dyn t | _ -> tdyn);
  984. f_args = List.map (fun (a,o,t,_) -> a,o,(match t with Some (CTPath t) -> to_dyn t | _ -> tdyn),None) fd.f_args;
  985. f_expr = None;
  986. }
  987. in
  988. let parent = (if not stat then get_parent c name else None) in
  989. let dynamic = List.mem ADynamic f.cff_access || (match parent with Some { cf_kind = Method MethDynamic } -> true | _ -> false) in
  990. if inline && dynamic then error "You can't have both 'inline' and 'dynamic'" p;
  991. ctx.curmethod <- name;
  992. ctx.type_params <- if stat then params else params @ ctx.type_params;
  993. let ret = type_opt ctx p fd.f_type in
  994. let args = List.map (fun (name,opt,t,c) ->
  995. let t, c = type_function_param ctx (type_opt ctx p t) c opt p in
  996. name, c, t
  997. ) fd.f_args in
  998. let t = TFun (fun_args args,ret) in
  999. let constr = (name = "new") in
  1000. if constr && c.cl_interface then error "An interface cannot have a constructor" p;
  1001. if c.cl_interface && not stat && fd.f_expr <> None then error "An interface method cannot have a body" p;
  1002. if constr then (match fd.f_type with
  1003. | None | Some (CTPath { tpackage = []; tname = "Void" }) -> ()
  1004. | _ -> error "A class constructor can't have a return value" p
  1005. );
  1006. let cf = {
  1007. cf_name = name;
  1008. cf_doc = f.cff_doc;
  1009. cf_meta = f.cff_meta;
  1010. cf_type = t;
  1011. cf_pos = f.cff_pos;
  1012. cf_kind = Method (if is_macro then MethMacro else if inline then MethInline else if dynamic then MethDynamic else MethNormal);
  1013. cf_expr = None;
  1014. cf_public = is_public f.cff_access parent;
  1015. cf_params = params;
  1016. cf_overloads = [];
  1017. } in
  1018. init_meta_overloads ctx cf;
  1019. let r = exc_protect ctx (fun r ->
  1020. if not !return_partial_type then begin
  1021. r := (fun() -> t);
  1022. incr stats.s_methods_typed;
  1023. if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ name);
  1024. let e , fargs = type_function ctx args ret (if constr then FConstructor else if stat then FStatic else FMember) fd p in
  1025. let f = {
  1026. tf_args = fargs;
  1027. tf_type = ret;
  1028. tf_expr = e;
  1029. } in
  1030. if stat && name = "__init__" then
  1031. (match e.eexpr with
  1032. | TBlock [] | TBlock [{ eexpr = TConst _ }] | TConst _ | TObjectDecl [] -> ()
  1033. | _ -> c.cl_init <- Some e);
  1034. if not (constr || inline) then mark_used cf;
  1035. if has_meta ":defineFeature" cf.cf_meta then add_feature ctx.com (s_type_path c.cl_path ^ "." ^ cf.cf_name);
  1036. cf.cf_expr <- Some (mk (TFunction f) t p);
  1037. cf.cf_type <- t;
  1038. end;
  1039. t
  1040. ) in
  1041. let delay = if ((c.cl_extern && not inline) || c.cl_interface) && cf.cf_name <> "__init__" then
  1042. (fun() -> ())
  1043. else
  1044. bind_type cf r (match fd.f_expr with Some e -> snd e | None -> f.cff_pos) is_macro
  1045. in
  1046. f, constr, cf, delay
  1047. | FProp (get,set,t,eo) ->
  1048. if override then error "You cannot override properties" p;
  1049. let ret = load_complex_type ctx p t in
  1050. let check_get = ref (fun() -> ()) in
  1051. let check_set = ref (fun() -> ()) in
  1052. let check_method m t () =
  1053. if ctx.com.display then () else
  1054. try
  1055. let t2 = (if stat then (PMap.find m c.cl_statics).cf_type else fst (class_field c m)) in
  1056. unify_raise ctx t2 t p;
  1057. with
  1058. | Error (Unify l,_) -> raise (Error (Stack (Custom ("In method " ^ m ^ " required by property " ^ name),Unify l),p))
  1059. | Not_found -> if not (c.cl_interface || c.cl_extern) then display_error ctx ("Method " ^ m ^ " required by property " ^ name ^ " is missing") p
  1060. in
  1061. let get = (match get with
  1062. | "null" -> AccNo
  1063. | "dynamic" -> AccCall ("get_" ^ name)
  1064. | "never" -> AccNever
  1065. | "default" -> AccNormal
  1066. | _ ->
  1067. check_get := check_method get (TFun ([],ret));
  1068. AccCall get
  1069. ) in
  1070. let set = (match set with
  1071. | "null" ->
  1072. (* standard flash library read-only variables can't be accessed for writing, even in subclasses *)
  1073. if c.cl_extern && (match c.cl_path with "flash" :: _ , _ -> true | _ -> false) && Common.defined ctx.com "flash9" then
  1074. AccNever
  1075. else
  1076. AccNo
  1077. | "never" -> AccNever
  1078. | "dynamic" -> AccCall ("set_" ^ name)
  1079. | "default" -> AccNormal
  1080. | _ ->
  1081. check_set := check_method set (TFun (["",false,ret],ret));
  1082. AccCall set
  1083. ) in
  1084. if set = AccNormal && (match get with AccCall _ -> true | _ -> false) then error "Unsupported property combination" p;
  1085. let cf = {
  1086. cf_name = name;
  1087. cf_doc = f.cff_doc;
  1088. cf_meta = f.cff_meta;
  1089. cf_pos = f.cff_pos;
  1090. cf_kind = Var { v_read = get; v_write = set };
  1091. cf_expr = None;
  1092. cf_type = ret;
  1093. cf_public = is_public f.cff_access None;
  1094. cf_params = [];
  1095. cf_overloads = [];
  1096. } in
  1097. let delay = bind_var ctx cf eo stat inline in
  1098. f, false, cf, (fun() -> delay(); (!check_get)(); (!check_set)())
  1099. in
  1100. let rec check_require = function
  1101. | [] -> None
  1102. | (":require",conds,_) :: l ->
  1103. let rec loop = function
  1104. | [] -> check_require l
  1105. | (EConst (Ident i),_) :: l ->
  1106. if not (Common.defined ctx.com i) then
  1107. Some i
  1108. else
  1109. loop l
  1110. | _ -> error "Invalid require identifier" p
  1111. in
  1112. loop conds
  1113. | _ :: l ->
  1114. check_require l
  1115. in
  1116. let cl_req = check_require c.cl_meta in
  1117. let fl = List.fold_left (fun acc f ->
  1118. try
  1119. let p = f.cff_pos in
  1120. let fd , constr, f , delayed = loop_cf f in
  1121. let is_static = List.mem AStatic fd.cff_access in
  1122. if is_static && f.cf_name = "name" && Common.defined ctx.com "js" then error "This identifier cannot be used in Javascript for statics" p;
  1123. if (is_static || constr) && c.cl_interface && f.cf_name <> "__init__" then error "You can't declare static fields in interfaces" p;
  1124. let req = check_require fd.cff_meta in
  1125. let req = (match req with None -> if is_static || constr then cl_req else None | _ -> req) in
  1126. (match req with
  1127. | None -> ()
  1128. | Some r -> f.cf_kind <- Var { v_read = AccRequire r; v_write = AccRequire r });
  1129. if constr then begin
  1130. if c.cl_constructor <> None then error "Duplicate constructor" p;
  1131. c.cl_constructor <- Some f;
  1132. end else if not is_static || f.cf_name <> "__init__" then begin
  1133. 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;
  1134. 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;
  1135. if is_static then begin
  1136. c.cl_statics <- PMap.add f.cf_name f c.cl_statics;
  1137. c.cl_ordered_statics <- f :: c.cl_ordered_statics;
  1138. end else begin
  1139. c.cl_fields <- PMap.add f.cf_name f c.cl_fields;
  1140. c.cl_ordered_fields <- f :: c.cl_ordered_fields;
  1141. if List.mem AOverride fd.cff_access then c.cl_overrides <- f.cf_name :: c.cl_overrides;
  1142. end;
  1143. end;
  1144. delayed :: acc
  1145. with Error (Custom str,p) ->
  1146. display_error ctx str p;
  1147. acc
  1148. ) [] fields in
  1149. c.cl_ordered_statics <- List.rev c.cl_ordered_statics;
  1150. c.cl_ordered_fields <- List.rev c.cl_ordered_fields;
  1151. (*
  1152. make sure a default contructor with same access as super one will be added to the class structure at some point.
  1153. *)
  1154. let rec add_constructor c =
  1155. match c.cl_constructor, c.cl_super with
  1156. | None, Some (csup,cparams) when not c.cl_extern ->
  1157. add_constructor csup;
  1158. (match csup.cl_constructor with
  1159. | None -> ()
  1160. | Some cf ->
  1161. ignore (follow cf.cf_type); (* make sure it's typed *)
  1162. let args = (match cf.cf_expr with
  1163. | Some { eexpr = TFunction f } ->
  1164. List.map (fun (v,def) ->
  1165. (*
  1166. let's optimize a bit the output by not always copying the default value
  1167. into the inherited constructor when it's not necessary for the platform
  1168. *)
  1169. match ctx.com.platform, def with
  1170. | (Php | Js | Neko | Flash8), Some _ -> v, (Some TNull)
  1171. | Flash, Some (TString _) -> v, (Some TNull)
  1172. | Cpp, Some (TString _) -> v, def
  1173. | Cpp, Some _ -> { v with v_type = ctx.t.tnull v.v_type }, (Some TNull)
  1174. | _ -> v, def
  1175. ) f.tf_args
  1176. | _ ->
  1177. match follow cf.cf_type with
  1178. | 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
  1179. | _ -> assert false
  1180. ) in
  1181. let p = c.cl_pos in
  1182. let vars = List.map (fun (v,def) -> alloc_var v.v_name (apply_params csup.cl_types cparams v.v_type), def) args in
  1183. 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
  1184. let constr = mk (TFunction {
  1185. tf_args = vars;
  1186. tf_type = ctx.t.tvoid;
  1187. tf_expr = super_call;
  1188. }) (TFun (List.map (fun (v,c) -> v.v_name, c <> None, v.v_type) vars,ctx.t.tvoid)) p in
  1189. c.cl_constructor <- Some { cf with cf_pos = p; cf_type = constr.etype; cf_meta = []; cf_doc = None; cf_expr = Some constr })
  1190. | _ ->
  1191. (* nothing to do *)
  1192. ()
  1193. in
  1194. delay ctx (fun() -> add_constructor c);
  1195. List.rev fl
  1196. let resolve_typedef ctx t =
  1197. match t with
  1198. | TClassDecl _ | TEnumDecl _ -> t
  1199. | TTypeDecl td ->
  1200. match follow td.t_type with
  1201. | TEnum (e,_) -> TEnumDecl e
  1202. | TInst (c,_) -> TClassDecl c
  1203. | _ -> t
  1204. let add_module ctx m p =
  1205. let decl_type t =
  1206. let t = t_infos t in
  1207. try
  1208. let m2 = Hashtbl.find ctx.g.types_module t.mt_path in
  1209. 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;
  1210. error ("Type name " ^ s_type_path t.mt_path ^ " is redefined from module " ^ s_type_path m2) p
  1211. with
  1212. Not_found ->
  1213. Hashtbl.add ctx.g.types_module t.mt_path m.m_path
  1214. in
  1215. List.iter decl_type m.m_types;
  1216. Hashtbl.add ctx.g.modules m.m_path m
  1217. let type_module ctx m file tdecls loadp =
  1218. (* PASS 1 : build module structure - does not load any module or type - should be atomic ! *)
  1219. let decls = ref [] in
  1220. let make_path name priv =
  1221. if List.exists (fun t -> snd (t_path t) = name) (!decls) then error ("Type name " ^ name ^ " is already defined in this module") loadp;
  1222. if priv then (fst m @ ["_" ^ snd m], name) else (fst m, name)
  1223. in
  1224. let m = {
  1225. m_id = alloc_mid();
  1226. m_path = m;
  1227. m_types = [];
  1228. 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);
  1229. } in
  1230. List.iter (fun (d,p) ->
  1231. match d with
  1232. | EImport _ | EUsing _ -> ()
  1233. | EClass d ->
  1234. let priv = List.mem HPrivate d.d_flags in
  1235. let path = make_path d.d_name priv in
  1236. let c = mk_class m path p in
  1237. c.cl_module <- m;
  1238. c.cl_private <- priv;
  1239. c.cl_doc <- d.d_doc;
  1240. c.cl_meta <- d.d_meta;
  1241. decls := TClassDecl c :: !decls
  1242. | EEnum d ->
  1243. let priv = List.mem EPrivate d.d_flags in
  1244. let path = make_path d.d_name priv in
  1245. let e = {
  1246. e_path = path;
  1247. e_module = m;
  1248. e_pos = p;
  1249. e_doc = d.d_doc;
  1250. e_meta = d.d_meta;
  1251. e_types = [];
  1252. e_private = priv;
  1253. e_extern = List.mem EExtern d.d_flags;
  1254. e_constrs = PMap.empty;
  1255. e_names = [];
  1256. } in
  1257. decls := TEnumDecl e :: !decls
  1258. | ETypedef d ->
  1259. let priv = List.mem EPrivate d.d_flags in
  1260. let path = make_path d.d_name priv in
  1261. let t = {
  1262. t_path = path;
  1263. t_module = m;
  1264. t_pos = p;
  1265. t_doc = d.d_doc;
  1266. t_private = priv;
  1267. t_types = [];
  1268. t_type = mk_mono();
  1269. t_meta = d.d_meta;
  1270. } in
  1271. decls := TTypeDecl t :: !decls
  1272. ) tdecls;
  1273. m.m_types <- List.rev !decls;
  1274. add_module ctx m loadp;
  1275. (* PASS 2 : build types structure - does not type any expression ! *)
  1276. let ctx = {
  1277. com = ctx.com;
  1278. g = ctx.g;
  1279. t = ctx.t;
  1280. macro_depth = ctx.macro_depth;
  1281. curclass = ctx.curclass;
  1282. tthis = ctx.tthis;
  1283. ret = ctx.ret;
  1284. current = m;
  1285. locals = PMap.empty;
  1286. local_types = ctx.g.std.m_types @ m.m_types;
  1287. local_using = [];
  1288. type_params = [];
  1289. curmethod = "";
  1290. curfun = FStatic;
  1291. untyped = false;
  1292. in_super_call = false;
  1293. in_macro = ctx.in_macro;
  1294. in_display = false;
  1295. in_loop = false;
  1296. opened = [];
  1297. param_type = None;
  1298. vthis = None;
  1299. } in
  1300. let delays = ref [] in
  1301. let get_class name =
  1302. let c = List.find (fun d -> match d with TClassDecl { cl_path = _ , n } -> n = name | _ -> false) m.m_types in
  1303. match c with TClassDecl c -> c | _ -> assert false
  1304. in
  1305. let get_enum name =
  1306. let e = List.find (fun d -> match d with TEnumDecl { e_path = _ , n } -> n = name | _ -> false) m.m_types in
  1307. match e with TEnumDecl e -> e | _ -> assert false
  1308. in
  1309. let get_tdef name =
  1310. let s = List.find (fun d -> match d with TTypeDecl { t_path = _ , n } -> n = name | _ -> false) m.m_types in
  1311. match s with TTypeDecl s -> s | _ -> assert false
  1312. in
  1313. (* here is an additional PASS 1 phase, which handle the type parameters declaration, with lazy contraints *)
  1314. List.iter (fun (d,p) ->
  1315. match d with
  1316. | EImport _ | EUsing _ -> ()
  1317. | EClass d ->
  1318. let c = get_class d.d_name in
  1319. c.cl_types <- List.map (type_type_params ctx c.cl_path (fun() -> c.cl_types) p) d.d_params;
  1320. | EEnum d ->
  1321. let e = get_enum d.d_name in
  1322. e.e_types <- List.map (type_type_params ctx e.e_path (fun() -> e.e_types) p) d.d_params;
  1323. | ETypedef d ->
  1324. let t = get_tdef d.d_name in
  1325. t.t_types <- List.map (type_type_params ctx t.t_path (fun() -> t.t_types) p) d.d_params;
  1326. ) tdecls;
  1327. (* back to PASS2 *)
  1328. List.iter (fun (d,p) ->
  1329. match d with
  1330. | EImport t ->
  1331. (match t.tsub with
  1332. | None ->
  1333. let md = ctx.g.do_load_module ctx (t.tpackage,t.tname) p in
  1334. let types = List.filter (fun t -> not (t_infos t).mt_private) md.m_types in
  1335. ctx.local_types <- ctx.local_types @ types
  1336. | Some _ ->
  1337. let t = load_type_def ctx p t in
  1338. ctx.local_types <- ctx.local_types @ [t]
  1339. )
  1340. | EUsing t ->
  1341. (match t.tsub with
  1342. | None ->
  1343. let md = ctx.g.do_load_module ctx (t.tpackage,t.tname) p in
  1344. let types = List.filter (fun t -> not (t_infos t).mt_private) md.m_types in
  1345. ctx.local_using <- ctx.local_using @ (List.map (resolve_typedef ctx) types);
  1346. ctx.local_types <- ctx.local_types @ types
  1347. | Some _ ->
  1348. let t = load_type_def ctx p t in
  1349. ctx.local_using<- ctx.local_using @ [resolve_typedef ctx t];
  1350. ctx.local_types <- ctx.local_types @ [t])
  1351. | EClass d ->
  1352. let c = get_class d.d_name in
  1353. let checks = if not ctx.com.display then [check_overriding ctx c p; check_interfaces ctx c p] else [] in
  1354. delays := !delays @ (checks @ init_class ctx c p d.d_flags d.d_data)
  1355. | EEnum d ->
  1356. let e = get_enum d.d_name in
  1357. let ctx = { ctx with type_params = e.e_types } in
  1358. let h = (try Some (Hashtbl.find ctx.g.type_patches e.e_path) with Not_found -> None) in
  1359. (match h with
  1360. | None -> ()
  1361. | Some (h,hcl) ->
  1362. Hashtbl.iter (fun _ _ -> error "Field type patch not supported for enums" e.e_pos) h;
  1363. e.e_meta <- e.e_meta @ hcl.tp_meta);
  1364. let constructs = ref d.d_data in
  1365. let get_constructs() =
  1366. List.map (fun (c,doc,meta,pl,p) ->
  1367. {
  1368. cff_name = c;
  1369. cff_doc = doc;
  1370. cff_meta = meta;
  1371. cff_pos = p;
  1372. cff_access = [];
  1373. cff_kind = (match pl with
  1374. | [] -> FVar (None,None)
  1375. | _ -> FFun { f_params = []; f_type = None; f_expr = None; f_args = List.map (fun (n,o,t) -> n,o,Some t,None) pl });
  1376. }
  1377. ) (!constructs)
  1378. in
  1379. build_module_def ctx (TEnumDecl e) e.e_meta get_constructs (fun (e,p) ->
  1380. match e with
  1381. | EVars [_,Some (CTAnonymous fields),None] ->
  1382. constructs := List.map (fun f ->
  1383. (f.cff_name,f.cff_doc,f.cff_meta,(match f.cff_kind with
  1384. | FVar (None,None) -> []
  1385. | 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
  1386. | _ -> error "Invalid enum constructor in @:build result" p
  1387. ),f.cff_pos)
  1388. ) fields
  1389. | _ -> error "Enum build macro must return a single variable with anonymous object fields" p
  1390. );
  1391. let et = TEnum (e,List.map snd e.e_types) in
  1392. let names = ref [] in
  1393. let index = ref 0 in
  1394. List.iter (fun (c,doc,meta,t,p) ->
  1395. if c = "name" && Common.defined ctx.com "js" then error "This identifier cannot be used in Javascript" p;
  1396. let t = (match t with
  1397. | [] -> et
  1398. | l ->
  1399. let pnames = ref PMap.empty in
  1400. TFun (List.map (fun (s,opt,t) ->
  1401. if PMap.mem s (!pnames) then error ("Duplicate parameter '" ^ s ^ "' in enum constructor " ^ c) p;
  1402. pnames := PMap.add s () (!pnames);
  1403. s, opt, load_type_opt ~opt ctx p (Some t)
  1404. ) l, et)
  1405. ) in
  1406. if PMap.mem c e.e_constrs then error ("Duplicate constructor " ^ c) p;
  1407. e.e_constrs <- PMap.add c {
  1408. ef_name = c;
  1409. ef_type = t;
  1410. ef_pos = p;
  1411. ef_doc = doc;
  1412. ef_index = !index;
  1413. ef_meta = meta;
  1414. } e.e_constrs;
  1415. incr index;
  1416. names := c :: !names;
  1417. ) (!constructs);
  1418. e.e_names <- List.rev !names;
  1419. e.e_extern <- e.e_extern || e.e_names = [];
  1420. | ETypedef d ->
  1421. let t = get_tdef d.d_name in
  1422. let ctx = { ctx with type_params = t.t_types } in
  1423. let tt = load_complex_type ctx p d.d_data in
  1424. if t.t_type == follow tt then error "Recursive typedef is not allowed" p;
  1425. (match t.t_type with
  1426. | TMono r ->
  1427. (match !r with
  1428. | None -> r := Some tt;
  1429. | Some _ -> assert false);
  1430. | _ -> assert false);
  1431. ) tdecls;
  1432. (* PASS 3 : type checking, delayed until all modules and types are built *)
  1433. List.iter (delay ctx) (List.rev (!delays));
  1434. m
  1435. let resolve_module_file com m remap p =
  1436. let file = (match m with
  1437. | [] , name -> name
  1438. | x :: l , name ->
  1439. let x = (try
  1440. match PMap.find x com.package_rules with
  1441. | Forbidden -> raise (Error (Forbid_package (x,m),p));
  1442. | Directory d -> d
  1443. | Remap d -> remap := d :: l; d
  1444. with Not_found -> x
  1445. ) in
  1446. String.concat "/" (x :: l) ^ "/" ^ name
  1447. ) ^ ".hx" in
  1448. let file = Common.find_file com file in
  1449. match String.lowercase (snd m) with
  1450. | "con" | "aux" | "prn" | "nul" | "com1" | "com2" | "com3" | "lpt1" | "lpt2" | "lpt3" when Sys.os_type = "Win32" ->
  1451. (* these names are reserved by the OS - old DOS legacy, such files cannot be easily created but are reported as visible *)
  1452. if (try (Unix.stat file).Unix.st_size with _ -> 0) > 0 then file else raise Not_found
  1453. | _ -> file
  1454. let parse_module ctx m p =
  1455. let remap = ref (fst m) in
  1456. let file = resolve_module_file ctx.com m remap p in
  1457. let pack, decls = (!parse_hook) ctx.com file p in
  1458. if pack <> !remap then begin
  1459. let spack m = if m = [] then "<empty>" else String.concat "." m in
  1460. if p == Ast.null_pos then
  1461. display_error ctx ("Invalid commandline class : " ^ s_type_path m ^ " should be " ^ s_type_path (pack,snd m)) p
  1462. else
  1463. display_error ctx ("Invalid package : " ^ spack (fst m) ^ " should be " ^ spack pack) p
  1464. end;
  1465. file, if !remap <> fst m then
  1466. (* build typedefs to redirect to real package *)
  1467. List.rev (List.fold_left (fun acc (t,p) ->
  1468. let build f d =
  1469. let priv = List.mem f d.d_flags in
  1470. (ETypedef {
  1471. d_name = d.d_name;
  1472. d_doc = None;
  1473. d_meta = [];
  1474. d_params = d.d_params;
  1475. d_flags = if priv then [EPrivate] else [];
  1476. d_data = CTPath (if priv then { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None; } else
  1477. {
  1478. tpackage = !remap;
  1479. tname = d.d_name;
  1480. tparams = List.map (fun (s,_) ->
  1481. TPType (CTPath { tpackage = []; tname = s; tparams = []; tsub = None; })
  1482. ) d.d_params;
  1483. tsub = None;
  1484. });
  1485. },p) :: acc
  1486. in
  1487. match t with
  1488. | EClass d -> build HPrivate d
  1489. | EEnum d -> build EPrivate d
  1490. | ETypedef d -> build EPrivate d
  1491. | EImport _ | EUsing _ -> acc
  1492. ) [(EImport { tpackage = !remap; tname = snd m; tparams = []; tsub = None; },null_pos)] decls)
  1493. else
  1494. decls
  1495. let load_module ctx m p =
  1496. let m2 = (try
  1497. Hashtbl.find ctx.g.modules m
  1498. with
  1499. Not_found ->
  1500. match !type_module_hook ctx m p with
  1501. | Some m -> m
  1502. | None ->
  1503. let file, decls = (try
  1504. parse_module ctx m p
  1505. with Not_found ->
  1506. let rec loop = function
  1507. | [] ->
  1508. if s_type_path m = "neko.db._Mysql.D" then begin
  1509. prerr_endline (String.concat ";" (fst m));
  1510. assert false;
  1511. end;
  1512. raise (Error (Module_not_found m,p))
  1513. | load :: l ->
  1514. match load m p with
  1515. | None -> loop l
  1516. | Some (file,(_,a)) -> file, a
  1517. in
  1518. loop ctx.com.load_extern_type
  1519. ) in
  1520. type_module ctx m file decls p
  1521. ) in
  1522. add_dependency ctx.current m2;
  1523. m2