typeload.ml 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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 do_create = ref (fun com -> assert false)
  24. let type_constant ctx c p =
  25. match c with
  26. | Int s ->
  27. if String.length s > 10 && String.sub s 0 2 = "0x" then error "Invalid hexadecimal integer" p;
  28. (try
  29. mk (TConst (TInt (Int32.of_string s))) ctx.api.tint p
  30. with
  31. _ -> mk (TConst (TFloat s)) ctx.api.tfloat p)
  32. | Float f -> mk (TConst (TFloat f)) ctx.api.tfloat p
  33. | String s -> mk (TConst (TString s)) ctx.api.tstring p
  34. | Ident "true" -> mk (TConst (TBool true)) ctx.api.tbool p
  35. | Ident "false" -> mk (TConst (TBool false)) ctx.api.tbool p
  36. | Ident "null" -> mk (TConst TNull) (ctx.api.tnull (mk_mono())) p
  37. | _ -> assert false
  38. let type_function_param ctx t e opt p =
  39. match e with
  40. | None ->
  41. if opt then ctx.api.tnull t, Some (EConst (Ident "null"),p) else t, None
  42. | Some e ->
  43. t, Some e
  44. let type_static_var ctx t e p =
  45. ctx.in_static <- true;
  46. let e = type_expr ctx e true in
  47. unify ctx e.etype t p;
  48. (* specific case for UInt statics *)
  49. match t with
  50. | TType ({ t_path = ([],"UInt") },[]) -> { e with etype = t }
  51. | _ -> e
  52. (** since load_type_def and load_instance are used in PASS2, they should not access the structure of a type **)
  53. (*
  54. load a type or a subtype definition
  55. *)
  56. let rec load_type_def ctx p t =
  57. let no_pack = t.tpackage = [] in
  58. let tname = (match t.tsub with None -> t.tname | Some n -> n) in
  59. try
  60. List.find (fun t2 ->
  61. let tp = t_path t2 in
  62. tp = (t.tpackage,tname) || (no_pack && snd tp = tname)
  63. ) ctx.local_types
  64. with
  65. Not_found ->
  66. let next() =
  67. let m = ctx.api.load_module (t.tpackage,t.tname) p in
  68. let tpath = (t.tpackage,tname) in
  69. try
  70. List.find (fun t -> not (t_private t) && t_path t = tpath) m.mtypes
  71. with
  72. Not_found -> raise (Error (Type_not_found (m.mpath,tname),p))
  73. in
  74. try
  75. if not no_pack then raise Exit;
  76. (match fst ctx.current.mpath with
  77. | [] -> raise Exit
  78. | x :: _ ->
  79. (* this can occur due to haxe remoting : a module can be
  80. already defined in the "js" package and is not allowed
  81. to access the js classes *)
  82. try
  83. (match PMap.find x ctx.com.package_rules with
  84. | Forbidden -> raise Exit
  85. | _ -> ())
  86. with Not_found -> ());
  87. load_type_def ctx p { t with tpackage = fst ctx.current.mpath }
  88. with
  89. | Error (Module_not_found _,p2)
  90. | Error (Type_not_found _,p2) when p == p2 -> next()
  91. | Exit -> next()
  92. (* build an instance from a full type *)
  93. let rec load_instance ctx t p allow_no_params =
  94. try
  95. if t.tpackage <> [] || t.tsub <> None then raise Not_found;
  96. let pt = List.assoc t.tname ctx.type_params in
  97. if t.tparams <> [] then error ("Class type parameter " ^ t.tname ^ " can't have parameters") p;
  98. pt
  99. with Not_found ->
  100. let types , path , f = ctx.api.build_instance (load_type_def ctx p t) p in
  101. if allow_no_params && t.tparams = [] then
  102. f (List.map (fun (name,t) ->
  103. match follow t with
  104. | TInst (c,_) -> if c.cl_implements = [] then mk_mono() else error ("Type parameter " ^ name ^ " need constraint") p
  105. | _ -> assert false
  106. ) types)
  107. else if path = ([],"Dynamic") then
  108. match t.tparams with
  109. | [] -> t_dynamic
  110. | [TPType t] -> TDynamic (load_complex_type ctx p t)
  111. | _ -> error "Too many parameters for Dynamic" p
  112. else begin
  113. if List.length types <> List.length t.tparams then error ("Invalid number of type parameters for " ^ s_type_path path) p;
  114. let tparams = List.map (fun t ->
  115. match t with
  116. | TPConst c ->
  117. let name, const = (match c with
  118. | String s -> "S" ^ s, TString s
  119. | Int i -> "I" ^ i, TInt (Int32.of_string i)
  120. | Float f -> "F" ^ f, TFloat f
  121. | _ -> assert false
  122. ) in
  123. let c = mk_class ([],name) p in
  124. c.cl_kind <- KConstant const;
  125. TInst (c,[])
  126. | TPType t -> load_complex_type ctx p t
  127. ) t.tparams in
  128. let params = List.map2 (fun t (name,t2) ->
  129. let isconst = (match t with TInst ({ cl_kind = KConstant _ },_) -> true | _ -> false) in
  130. if isconst <> (name = "Const") && t != t_dynamic then error (if isconst then "Constant value unexpected here" else "Constant value excepted as type parameter") p;
  131. match follow t2 with
  132. | TInst ({ cl_implements = [] }, []) ->
  133. t
  134. | TInst (c,[]) ->
  135. let r = exc_protect (fun r ->
  136. r := (fun() -> t);
  137. List.iter (fun (i,params) ->
  138. unify ctx t (apply_params types tparams (TInst (i,params))) p
  139. ) c.cl_implements;
  140. t
  141. ) in
  142. ctx.delays := [(fun () -> ignore(!r()))] :: !(ctx.delays);
  143. TLazy r
  144. | _ -> assert false
  145. ) tparams types in
  146. f params
  147. end
  148. (*
  149. build an instance from a complex type
  150. *)
  151. and load_complex_type ctx p t =
  152. match t with
  153. | TPParent t -> load_complex_type ctx p t
  154. | TPNormal t -> load_instance ctx t p false
  155. | TPExtend (t,l) ->
  156. (match load_complex_type ctx p (TPAnonymous l) with
  157. | TAnon a ->
  158. let rec loop t =
  159. match follow t with
  160. | TInst (c,tl) ->
  161. let c2 = mk_class (fst c.cl_path,"+" ^ snd c.cl_path) p in
  162. c2.cl_private <- true;
  163. PMap.iter (fun f _ ->
  164. try
  165. ignore(class_field c f);
  166. error ("Cannot redefine field " ^ f) p
  167. with
  168. Not_found -> ()
  169. ) a.a_fields;
  170. (* do NOT tag as extern - for protect *)
  171. c2.cl_kind <- KExtension (c,tl);
  172. c2.cl_super <- Some (c,tl);
  173. c2.cl_fields <- a.a_fields;
  174. TInst (c2,[])
  175. | TMono _ ->
  176. error "Please ensure correct initialization of cascading signatures" p
  177. | TAnon a2 ->
  178. PMap.iter (fun f _ ->
  179. if PMap.mem f a2.a_fields then error ("Cannot redefine field " ^ f) p
  180. ) a.a_fields;
  181. mk_anon (PMap.foldi PMap.add a.a_fields a2.a_fields)
  182. | _ -> error "Cannot only extend classes and anonymous" p
  183. in
  184. loop (load_instance ctx t p false)
  185. | _ -> assert false)
  186. | TPAnonymous l ->
  187. let rec loop acc (n,pub,f,p) =
  188. if PMap.mem n acc then error ("Duplicate field declaration : " ^ n) p;
  189. let t , get, set = (match f with
  190. | AFVar t ->
  191. load_complex_type ctx p t, NormalAccess, NormalAccess
  192. | AFFun (tl,t) ->
  193. let t = load_complex_type ctx p t in
  194. let args = List.map (fun (name,o,t) -> name , o, load_complex_type ctx p t) tl in
  195. TFun (args,t), NormalAccess, MethodAccess false
  196. | AFProp (t,i1,i2) ->
  197. let access m get =
  198. match m with
  199. | "null" -> NoAccess
  200. | "never" -> NeverAccess
  201. | "default" -> NormalAccess
  202. | "dynamic" -> CallAccess ((if get then "get_" else "set_") ^ n)
  203. | _ -> CallAccess m
  204. in
  205. load_complex_type ctx p t, access i1 true, access i2 false
  206. ) in
  207. PMap.add n {
  208. cf_name = n;
  209. cf_type = t;
  210. cf_public = (match pub with None -> true | Some p -> p);
  211. cf_get = get;
  212. cf_set = set;
  213. cf_params = [];
  214. cf_expr = None;
  215. cf_doc = None;
  216. cf_meta = [];
  217. } acc
  218. in
  219. mk_anon (List.fold_left loop PMap.empty l)
  220. | TPFunction (args,r) ->
  221. match args with
  222. | [TPNormal { tpackage = []; tparams = []; tname = "Void" }] ->
  223. TFun ([],load_complex_type ctx p r)
  224. | _ ->
  225. TFun (List.map (fun t -> "",false,load_complex_type ctx p t) args,load_complex_type ctx p r)
  226. let hide_types ctx =
  227. let old_locals = ctx.local_types in
  228. let old_type_params = ctx.type_params in
  229. ctx.local_types <- ctx.std.mtypes;
  230. ctx.type_params <- [];
  231. (fun() ->
  232. ctx.local_types <- old_locals;
  233. ctx.type_params <- old_type_params;
  234. )
  235. (*
  236. load a type while ignoring the current imports or local types
  237. *)
  238. let load_core_type ctx name =
  239. let show = hide_types ctx in
  240. let t = load_instance ctx { tpackage = []; tname = name; tparams = []; tsub = None; } null_pos false in
  241. show();
  242. t
  243. let t_iterator ctx =
  244. let show = hide_types ctx in
  245. match load_type_def ctx null_pos { tpackage = []; tname = "Iterator"; tparams = []; tsub = None } with
  246. | TTypeDecl t ->
  247. show();
  248. if List.length t.t_types <> 1 then assert false;
  249. let pt = mk_mono() in
  250. apply_params t.t_types [pt] t.t_type, pt
  251. | _ ->
  252. assert false
  253. (*
  254. load either a type t or Null<Unknown> if not defined
  255. *)
  256. let load_type_opt ?(opt=false) ctx p t =
  257. let t = (match t with None -> mk_mono() | Some t -> load_complex_type ctx p t) in
  258. if opt then ctx.api.tnull t else t
  259. (* ---------------------------------------------------------------------- *)
  260. (* Structure check *)
  261. let valid_redefinition ctx f1 t1 f2 t2 =
  262. let valid t1 t2 =
  263. type_eq EqStrict t1 t2;
  264. if is_null t1 <> is_null t2 then raise (Unify_error [Cannot_unify (t1,t2)]);
  265. in
  266. let t1, t2 = (match f1.cf_params, f2.cf_params with
  267. | [], [] -> t1, t2
  268. | l1, l2 when List.length l1 = List.length l2 ->
  269. let monos = List.map (fun _ -> mk_mono()) l1 in
  270. apply_params l1 monos t1, apply_params l2 monos t2
  271. | _ -> t1, t2
  272. ) in
  273. match follow t1, follow t2 with
  274. | TFun (args1,r1) , TFun (args2,r2) when List.length args1 = List.length args2 ->
  275. List.iter2 (fun (n,o1,a1) (_,o2,a2) ->
  276. if o1 <> o2 then raise (Unify_error [Not_matching_optional n]);
  277. valid a1 a2;
  278. ) args1 args2;
  279. valid r1 r2;
  280. | _ , _ ->
  281. (* in case args differs, or if an interface var *)
  282. valid t1 t2
  283. let check_overriding ctx c p () =
  284. match c.cl_super with
  285. | None ->
  286. (match c.cl_overrides with
  287. | [] -> ()
  288. | i :: _ ->
  289. display_error ctx ("Field " ^ i ^ " is declared 'override' but doesn't override any field") p)
  290. | Some (csup,params) ->
  291. PMap.iter (fun i f ->
  292. try
  293. let t , f2 = raw_class_field (fun f -> f.cf_type) csup i in
  294. ignore(follow f.cf_type); (* force evaluation *)
  295. let p = (match f.cf_expr with None -> p | Some e -> e.epos) in
  296. if not (List.mem i c.cl_overrides) then
  297. display_error ctx ("Field " ^ i ^ " should be declared with 'override' since it is inherited from superclass") p
  298. else if f.cf_public <> f2.cf_public then
  299. display_error ctx ("Field " ^ i ^ " has different visibility (public/private) than superclass one") p
  300. else if f2.cf_get = InlineAccess then
  301. display_error ctx ("Field " ^ i ^ " is inlined and cannot be overridden") p
  302. else if f2.cf_get <> f.cf_get || f2.cf_set <> f.cf_set then
  303. display_error ctx ("Field " ^ i ^ " has different property access than in superclass") p
  304. else try
  305. let t = apply_params csup.cl_types params t in
  306. valid_redefinition ctx f f.cf_type f2 t
  307. with
  308. Unify_error l ->
  309. display_error ctx ("Field " ^ i ^ " overload parent class with different or incomplete type") p;
  310. display_error ctx (error_msg (Unify l)) p;
  311. with
  312. Not_found ->
  313. if List.mem i c.cl_overrides then display_error ctx ("Field " ^ i ^ " is declared 'override' but doesn't override any field") p
  314. ) c.cl_fields
  315. let class_field_no_interf c i =
  316. try
  317. let f = PMap.find i c.cl_fields in
  318. f.cf_type , f
  319. with Not_found ->
  320. match c.cl_super with
  321. | None ->
  322. raise Not_found
  323. | Some (c,tl) ->
  324. (* rec over class_field *)
  325. let t , f = raw_class_field (fun f -> f.cf_type) c i in
  326. apply_params c.cl_types tl t , f
  327. let rec check_interface ctx c p intf params =
  328. PMap.iter (fun i f ->
  329. try
  330. let t2, f2 = class_field_no_interf c i in
  331. ignore(follow f2.cf_type); (* force evaluation *)
  332. let p = (match f2.cf_expr with None -> p | Some e -> e.epos) in
  333. if f.cf_public && not f2.cf_public then
  334. display_error ctx ("Field " ^ i ^ " should be public as requested by " ^ s_type_path intf.cl_path) p
  335. else if not (unify_access f2.cf_get f.cf_get) then
  336. display_error ctx ("Field " ^ i ^ " has different property access than in " ^ s_type_path intf.cl_path ^ " (" ^ s_access f2.cf_get ^ " should be " ^ s_access f.cf_get ^ ")") p
  337. else if not (unify_access f2.cf_set f.cf_set) then
  338. display_error ctx ("Field " ^ i ^ " has different property access than in " ^ s_type_path intf.cl_path ^ " (" ^ s_access f2.cf_set ^ " should be " ^ s_access f.cf_set ^ ")") p
  339. else try
  340. valid_redefinition ctx f2 t2 f (apply_params intf.cl_types params f.cf_type)
  341. with
  342. Unify_error l ->
  343. display_error ctx ("Field " ^ i ^ " has different type than in " ^ s_type_path intf.cl_path) p;
  344. display_error ctx (error_msg (Unify l)) p;
  345. with
  346. Not_found ->
  347. if not c.cl_interface then display_error ctx ("Field " ^ i ^ " needed by " ^ s_type_path intf.cl_path ^ " is missing") p
  348. ) intf.cl_fields;
  349. List.iter (fun (i2,p2) ->
  350. check_interface ctx c p i2 (List.map (apply_params intf.cl_types params) p2)
  351. ) intf.cl_implements
  352. let check_interfaces ctx c p () =
  353. match c.cl_path with
  354. | "Proxy" :: _ , _ -> ()
  355. | _ ->
  356. List.iter (fun (intf,params) -> check_interface ctx c p intf params) c.cl_implements
  357. let rec return_flow ctx e =
  358. let error() = display_error ctx "A return is missing here" e.epos; raise Exit in
  359. let return_flow = return_flow ctx in
  360. match e.eexpr with
  361. | TReturn _ | TThrow _ -> ()
  362. | TParenthesis e ->
  363. return_flow e
  364. | TBlock el ->
  365. let rec loop = function
  366. | [] -> error()
  367. | [e] -> return_flow e
  368. | { eexpr = TReturn _ } :: _ | { eexpr = TThrow _ } :: _ -> ()
  369. | _ :: l -> loop l
  370. in
  371. loop el
  372. | TIf (_,e1,Some e2) ->
  373. return_flow e1;
  374. return_flow e2;
  375. | TSwitch (v,cases,Some e) ->
  376. List.iter (fun (_,e) -> return_flow e) cases;
  377. return_flow e
  378. | TSwitch (e,cases,None) when (match follow e.etype with TEnum _ -> true | _ -> false) ->
  379. List.iter (fun (_,e) -> return_flow e) cases;
  380. | TMatch (_,_,cases,def) ->
  381. List.iter (fun (_,_,e) -> return_flow e) cases;
  382. (match def with None -> () | Some e -> return_flow e)
  383. | TTry (e,cases) ->
  384. return_flow e;
  385. List.iter (fun (_,_,e) -> return_flow e) cases;
  386. | _ ->
  387. error()
  388. (* ---------------------------------------------------------------------- *)
  389. (* PASS 1 & 2 : Module and Class Structure *)
  390. let set_heritance ctx c herits p =
  391. let rec loop = function
  392. | HPrivate | HExtern | HInterface ->
  393. ()
  394. | HExtends t ->
  395. if c.cl_super <> None then error "Cannot extend several classes" p;
  396. let t = load_instance ctx t p false in
  397. (match follow t with
  398. | TInst ({ cl_path = [],"Array" },_)
  399. | TInst ({ cl_path = [],"String" },_)
  400. | TInst ({ cl_path = [],"Date" },_)
  401. | TInst ({ cl_path = [],"Xml" },_) when ((not (platform ctx.com Cpp)) && (match c.cl_path with "mt" :: _ , _ -> false | _ -> true)) ->
  402. error "Cannot extend basic class" p;
  403. | TInst (cl,params) ->
  404. if is_parent c cl then error "Recursive class" p;
  405. if c.cl_interface then error "Cannot extend an interface" p;
  406. if cl.cl_interface then error "Cannot extend by using an interface" p;
  407. if List.mem (":final",[]) cl.cl_meta && not (List.mem (":hack",[]) c.cl_meta) then error "Cannot extend a final class" p;
  408. c.cl_super <- Some (cl,params)
  409. | _ -> error "Should extend by using a class" p)
  410. | HImplements t ->
  411. let t = load_instance ctx t p false in
  412. (match follow t with
  413. | TInst ({ cl_path = [],"ArrayAccess"; cl_extern = true; },[t]) ->
  414. if c.cl_array_access <> None then error "Duplicate array access" p;
  415. c.cl_array_access <- Some t
  416. | TInst (cl,params) ->
  417. if is_parent c cl then error "Recursive class" p;
  418. c.cl_implements <- (cl, params) :: c.cl_implements
  419. | TDynamic t ->
  420. if c.cl_dynamic <> None then error "Cannot have several dynamics" p;
  421. c.cl_dynamic <- Some t
  422. | _ -> error "Should implement by using an interface or a class" p)
  423. in
  424. (*
  425. resolve imports before calling build_inheritance, since it requires full paths.
  426. that means that typedefs are not working, but that's a fair limitation
  427. *)
  428. let rec resolve_imports t =
  429. match t.tpackage with
  430. | _ :: _ -> t
  431. | [] ->
  432. try
  433. let lt = List.find (fun lt -> snd (t_path lt) = t.tname) ctx.local_types in
  434. { t with tpackage = fst (t_path lt) }
  435. with
  436. Not_found -> t
  437. in
  438. let herits = List.map (function
  439. | HExtends t -> HExtends (resolve_imports t)
  440. | HImplements t -> HImplements (resolve_imports t)
  441. | h -> h
  442. ) herits in
  443. List.iter loop (List.filter ((!build_inheritance) ctx c p) herits)
  444. let type_type_params ctx path p (n,flags) =
  445. let c = mk_class (fst path @ [snd path],n) p in
  446. c.cl_kind <- KTypeParameter;
  447. let t = TInst (c,[]) in
  448. match flags with
  449. | [] -> n, t
  450. | _ ->
  451. let r = exc_protect (fun r ->
  452. r := (fun _ -> t);
  453. set_heritance ctx c (List.map (fun t -> HImplements t) flags) p;
  454. t
  455. ) in
  456. ctx.delays := [(fun () -> ignore(!r()))] :: !(ctx.delays);
  457. n, TLazy r
  458. let type_function ctx args ret static constr f p =
  459. let locals = save_locals ctx in
  460. let fargs = List.map (fun (n,c,t) ->
  461. let c = (match c with
  462. | None -> None
  463. | Some e ->
  464. let p = pos e in
  465. let e = ctx.api.optimize (type_expr ctx e true) in
  466. unify ctx e.etype t p;
  467. match e.eexpr with
  468. | TConst c -> Some c
  469. | _ -> error "Parameter default value should be constant" p
  470. ) in
  471. let n = add_local ctx n t in
  472. n, c, t
  473. ) args in
  474. let old_ret = ctx.ret in
  475. let old_static = ctx.in_static in
  476. let old_constr = ctx.in_constructor in
  477. let old_opened = ctx.opened in
  478. ctx.in_static <- static;
  479. ctx.in_constructor <- constr;
  480. ctx.ret <- ret;
  481. ctx.opened <- [];
  482. let e = type_expr ctx f.f_expr false in
  483. let rec loop e =
  484. match e.eexpr with
  485. | TReturn (Some _) -> raise Exit
  486. | TFunction _ -> ()
  487. | _ -> Type.iter loop e
  488. in
  489. let have_ret = (try loop e; false with Exit -> true) in
  490. if have_ret then
  491. (try return_flow ctx e with Exit -> ())
  492. else
  493. unify ctx ret ctx.api.tvoid p;
  494. let rec loop e =
  495. match e.eexpr with
  496. | TCall ({ eexpr = TConst TSuper },_) -> raise Exit
  497. | TFunction _ -> ()
  498. | _ -> Type.iter loop e
  499. in
  500. if constr && (match ctx.curclass.cl_super with None -> false | Some (cl,_) -> cl.cl_constructor <> None) then
  501. (try
  502. loop e;
  503. error "Missing super constructor call" p
  504. with
  505. Exit -> ());
  506. locals();
  507. List.iter (fun r -> r := Closed) ctx.opened;
  508. ctx.ret <- old_ret;
  509. ctx.in_static <- old_static;
  510. ctx.in_constructor <- old_constr;
  511. ctx.opened <- old_opened;
  512. e , fargs
  513. let type_meta ctx meta =
  514. let notconst p = error "Metadata should be constant" p in
  515. let rec mk_const (e,p) =
  516. match e with
  517. | EConst c ->
  518. (match c with
  519. | Int _ | Float _ | String _ | Ident "true" | Ident "false" | Ident "null" -> type_constant ctx c p
  520. | _ -> notconst p)
  521. | EObjectDecl fl ->
  522. let rec loop (l,acc) (f,e) =
  523. if PMap.mem f acc then error ("Duplicate field in object declaration : " ^ f) p;
  524. let e = mk_const e in
  525. let cf = mk_field f e.etype in
  526. ((f,e) :: l, PMap.add f cf acc)
  527. in
  528. let fields , types = List.fold_left loop ([],PMap.empty) fl in
  529. mk (TObjectDecl (List.rev fields)) (TAnon { a_fields = types; a_status = ref Closed }) p
  530. | EArrayDecl el ->
  531. mk (TArrayDecl (List.map mk_const el)) (ctx.api.tarray t_dynamic) p
  532. | EBlock [] ->
  533. mk (TObjectDecl []) (TAnon { a_fields = PMap.empty; a_status = ref Closed}) p
  534. | _ ->
  535. notconst p
  536. in
  537. List.map (fun (s,el) -> s, List.map mk_const el) meta
  538. let init_core_api ctx c =
  539. let ctx2 = (match !(ctx.core_api) with
  540. | None ->
  541. let com = ctx.com in
  542. let com = { com with class_path = com.std_path; type_api = { com.type_api with tvoid = com.type_api.tvoid } } in
  543. let ctx2 = (!do_create) com in
  544. ctx.core_api := Some ctx2;
  545. ctx2
  546. | Some c ->
  547. c
  548. ) in
  549. let t = load_instance ctx2 { tpackage = fst c.cl_path; tname = snd c.cl_path; tparams = []; tsub = None; } c.cl_pos true in
  550. match t with
  551. | TInst (ccore,_) ->
  552. let check_fields fcore fl =
  553. PMap.iter (fun i f ->
  554. let f2 = try PMap.find f.cf_name fl with Not_found -> error ("Missing field " ^ i ^ " required by core type") c.cl_pos in
  555. let p = (match f2.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  556. (try
  557. type_eq EqCoreType (apply_params ccore.cl_types (List.map snd c.cl_types) f.cf_type) f2.cf_type
  558. with Unify_error l ->
  559. display_error ctx ("Field " ^ i ^ " has different type than in core type") p;
  560. display_error ctx (error_msg (Unify l)) p);
  561. if f2.cf_public <> f.cf_public then error ("Field " ^ i ^ " has different visibility than core type") p;
  562. if f2.cf_get <> f.cf_get || f2.cf_set <> f.cf_set then begin
  563. match f2.cf_get, f.cf_get, f2.cf_set, f.cf_set with
  564. | InlineAccess, NormalAccess, NeverAccess, MethodAccess false -> () (* allow to add 'inline' *)
  565. | NormalAccess, InlineAccess, MethodAccess false, NeverAccess -> () (* allow to remove 'inline' - only during transition ? *)
  566. | _ ->
  567. error ("Field " ^ i ^ " has different property access than core type") p;
  568. end;
  569. ) fcore;
  570. PMap.iter (fun i f ->
  571. let p = (match f.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  572. if f.cf_public && not (PMap.mem f.cf_name fcore) then error ("Public field " ^ i ^ " is not part of core type") p;
  573. ) fl;
  574. in
  575. check_fields ccore.cl_fields c.cl_fields;
  576. check_fields ccore.cl_statics c.cl_statics;
  577. | _ -> assert false
  578. let init_class ctx c p herits fields =
  579. ctx.type_params <- c.cl_types;
  580. c.cl_extern <- List.mem HExtern herits;
  581. c.cl_interface <- List.mem HInterface herits;
  582. set_heritance ctx c herits p;
  583. let core_api = List.mem (":core_api",[]) c.cl_meta in
  584. if core_api then ctx.delays := [(fun() -> init_core_api ctx c)] :: !(ctx.delays);
  585. let tthis = TInst (c,List.map snd c.cl_types) in
  586. let rec extends_public c =
  587. List.exists (fun (c,_) -> c.cl_path = (["haxe"],"Public") || extends_public c) c.cl_implements ||
  588. match c.cl_super with
  589. | None -> false
  590. | Some (c,_) -> extends_public c
  591. in
  592. let extends_public = extends_public c in
  593. let is_public access parent =
  594. if List.mem APrivate access then
  595. false
  596. else if List.mem APublic access then
  597. true
  598. else match parent with
  599. | Some { cf_public = p } -> p
  600. | _ -> c.cl_extern || c.cl_interface || extends_public
  601. in
  602. let rec get_parent c name =
  603. match c.cl_super with
  604. | None -> None
  605. | Some (csup,_) ->
  606. try
  607. Some (PMap.find name csup.cl_fields)
  608. with
  609. Not_found -> get_parent csup name
  610. in
  611. let type_opt ctx p t =
  612. match t with
  613. | None when c.cl_extern || c.cl_interface ->
  614. display_error ctx "Type required for extern classes and interfaces" p;
  615. t_dynamic
  616. | None when core_api ->
  617. display_error ctx "Type required for core api classes" p;
  618. t_dynamic
  619. | _ ->
  620. load_type_opt ctx p t
  621. in
  622. let rec has_field f = function
  623. | None -> false
  624. | Some (c,_) ->
  625. PMap.exists f c.cl_fields || has_field f c.cl_super || List.exists (fun i -> has_field f (Some i)) c.cl_implements
  626. in
  627. let loop_cf f p =
  628. match f with
  629. | FVar (name,doc,meta,access,t,e) ->
  630. let stat = List.mem AStatic access in
  631. let inline = List.mem AInline access in
  632. if not stat && has_field name c.cl_super then error ("Redefinition of variable " ^ name ^ " in subclass is not allowed") p;
  633. if inline && not stat then error "Inline variable must be static" p;
  634. if inline && e = None then error "Inline variable must be initialized" p;
  635. let t = (match t with
  636. | None ->
  637. if not stat then display_error ctx ("Type required for member variable " ^ name) p;
  638. mk_mono()
  639. | Some t ->
  640. let old = ctx.type_params in
  641. if stat then ctx.type_params <- [];
  642. let t = load_complex_type ctx p t in
  643. if stat then ctx.type_params <- old;
  644. t
  645. ) in
  646. let cf = {
  647. cf_name = name;
  648. cf_doc = doc;
  649. cf_meta = type_meta ctx meta;
  650. cf_type = t;
  651. cf_get = if inline then InlineAccess else NormalAccess;
  652. cf_set = if inline then NeverAccess else NormalAccess;
  653. cf_expr = None;
  654. cf_public = is_public access None;
  655. cf_params = [];
  656. } in
  657. let delay = (match e with
  658. | None -> (fun() -> ())
  659. | Some e ->
  660. let ctx = { ctx with curclass = c; tthis = tthis } in
  661. let r = exc_protect (fun r ->
  662. r := (fun() -> t);
  663. if ctx.com.verbose then print_endline ("Typing " ^ s_type_path c.cl_path ^ "." ^ name);
  664. cf.cf_expr <- Some (type_static_var ctx t e p);
  665. t
  666. ) in
  667. cf.cf_type <- TLazy r;
  668. (fun () -> ignore(!r()))
  669. ) in
  670. access, false, cf, delay
  671. | FFun (name,doc,meta,access,params,f) ->
  672. let params = List.map (fun (n,flags) ->
  673. match flags with
  674. | [] ->
  675. type_type_params ctx ([],name) p (n,[])
  676. | _ -> error "This notation is not allowed because it can't be checked" p
  677. ) params in
  678. let stat = List.mem AStatic access in
  679. let inline = List.mem AInline access in
  680. if inline && c.cl_interface then error "You can't declare inline methods in interfaces" p;
  681. let parent = (if not stat then get_parent c name else None) in
  682. let dynamic = List.mem ADynamic access || (match parent with Some { cf_set = MethodAccess true } -> true | _ -> false) in
  683. let ctx = { ctx with
  684. curclass = c;
  685. curmethod = name;
  686. tthis = tthis;
  687. type_params = if stat then params else params @ ctx.type_params;
  688. } in
  689. let ret = type_opt ctx p f.f_type in
  690. let args = List.map (fun (name,opt,t,c) ->
  691. let t, c = type_function_param ctx (type_opt ctx p t) c opt p in
  692. name, c, t
  693. ) f.f_args in
  694. let t = TFun (fun_args args,ret) in
  695. let constr = (name = "new") in
  696. if constr && c.cl_interface then error "An interface cannot have a constructor" p;
  697. if c.cl_interface && not stat && (match f.f_expr with EBlock [] , _ -> false | _ -> true) then error "An interface method cannot have a body" p;
  698. if constr then (match f.f_type with
  699. | None | Some (TPNormal { tpackage = []; tname = "Void" }) -> ()
  700. | _ -> error "A class constructor can't have a return value" p
  701. );
  702. let cf = {
  703. cf_name = name;
  704. cf_doc = doc;
  705. cf_meta = type_meta ctx meta;
  706. cf_type = t;
  707. cf_get = if inline then InlineAccess else NormalAccess;
  708. cf_set = (if inline then NeverAccess else MethodAccess dynamic);
  709. cf_expr = None;
  710. cf_public = is_public access parent;
  711. cf_params = params;
  712. } in
  713. let r = exc_protect (fun r ->
  714. r := (fun() -> t);
  715. if ctx.com.verbose then print_endline ("Typing " ^ s_type_path c.cl_path ^ "." ^ name);
  716. let e , fargs = type_function ctx args ret stat constr f p in
  717. let f = {
  718. tf_args = fargs;
  719. tf_type = ret;
  720. tf_expr = e;
  721. } in
  722. if stat && name = "__init__" then
  723. (match e.eexpr with
  724. | TBlock [] | TBlock [{ eexpr = TConst _ }] | TConst _ | TObjectDecl [] -> ()
  725. | _ -> c.cl_init <- Some e);
  726. cf.cf_expr <- Some (mk (TFunction f) t p);
  727. t
  728. ) in
  729. let delay = (
  730. if ((c.cl_extern && not inline) || c.cl_interface) && cf.cf_name <> "__init__" then
  731. (fun() -> ())
  732. else begin
  733. cf.cf_type <- TLazy r;
  734. (fun() -> ignore((!r)()))
  735. end
  736. ) in
  737. access, constr, cf, delay
  738. | FProp (name,doc,meta,access,get,set,t) ->
  739. let ret = load_complex_type ctx p t in
  740. let check_get = ref (fun() -> ()) in
  741. let check_set = ref (fun() -> ()) in
  742. let check_method m t () =
  743. try
  744. let t2 = (if List.mem AStatic access then (PMap.find m c.cl_statics).cf_type else fst (class_field c m)) in
  745. unify_raise ctx t2 t p;
  746. with
  747. | Error (Unify l,_) -> raise (Error (Stack (Custom ("In method " ^ m ^ " required by property " ^ name),Unify l),p))
  748. | Not_found -> if not c.cl_interface then error ("Method " ^ m ^ " required by property " ^ name ^ " is missing") p
  749. in
  750. let get = (match get with
  751. | "null" -> NoAccess
  752. | "dynamic" -> CallAccess ("get_" ^ name)
  753. | "never" -> NeverAccess
  754. | "default" -> NormalAccess
  755. | _ ->
  756. check_get := check_method get (TFun ([],ret));
  757. CallAccess get
  758. ) in
  759. let set = (match set with
  760. | "null" ->
  761. (* standard flash library read-only variables can't be accessed for writing, even in subclasses *)
  762. if c.cl_extern && (match c.cl_path with "flash" :: _ , _ -> true | _ -> false) && Common.defined ctx.com "flash9" then
  763. NeverAccess
  764. else
  765. NoAccess
  766. | "never" -> NeverAccess
  767. | "dynamic" -> CallAccess ("set_" ^ name)
  768. | "default" -> NormalAccess
  769. | _ ->
  770. check_set := check_method set (TFun (["",false,ret],ret));
  771. CallAccess set
  772. ) in
  773. if set = NormalAccess && (match get with CallAccess _ -> true | _ -> false) then error "Unsupported property combination" p;
  774. let cf = {
  775. cf_name = name;
  776. cf_doc = doc;
  777. cf_meta = type_meta ctx meta;
  778. cf_get = get;
  779. cf_set = set;
  780. cf_expr = None;
  781. cf_type = ret;
  782. cf_public = is_public access None;
  783. cf_params = [];
  784. } in
  785. access, false, cf, (fun() -> (!check_get)(); (!check_set)())
  786. in
  787. let fl = List.map (fun (f,p) ->
  788. let access , constr, f , delayed = loop_cf f p in
  789. let is_static = List.mem AStatic access in
  790. if is_static && f.cf_name = "name" && Common.defined ctx.com "js" then error "This identifier cannot be used in Javascript for statics" p;
  791. if (is_static || constr) && c.cl_interface && f.cf_name <> "__init__" then error "You can't declare static fields in interfaces" p;
  792. if constr then begin
  793. if c.cl_constructor <> None then error "Duplicate constructor" p;
  794. c.cl_constructor <- Some f;
  795. end else if not is_static || f.cf_name <> "__init__" then begin
  796. 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;
  797. 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;
  798. if is_static then begin
  799. c.cl_statics <- PMap.add f.cf_name f c.cl_statics;
  800. c.cl_ordered_statics <- f :: c.cl_ordered_statics;
  801. end else begin
  802. c.cl_fields <- PMap.add f.cf_name f c.cl_fields;
  803. c.cl_ordered_fields <- f :: c.cl_ordered_fields;
  804. if List.mem AOverride access then c.cl_overrides <- f.cf_name :: c.cl_overrides;
  805. end;
  806. end;
  807. delayed
  808. ) fields in
  809. c.cl_ordered_statics <- List.rev c.cl_ordered_statics;
  810. c.cl_ordered_fields <- List.rev c.cl_ordered_fields;
  811. (*
  812. define a default inherited constructor.
  813. This is actually pretty tricky since we can't assume that the constructor of the
  814. superclass has been defined yet because type structure is not stabilized wrt recursion.
  815. *)
  816. let rec define_constructor ctx c =
  817. try
  818. Some (Hashtbl.find ctx.constructs c.cl_path)
  819. with Not_found ->
  820. match c.cl_super with
  821. | None -> None
  822. | Some (csuper,_) ->
  823. match define_constructor ctx csuper with
  824. | None -> None
  825. | Some (acc,pl,f) as infos ->
  826. let p = c.cl_pos in
  827. let esuper = (ECall ((EConst (Ident "super"),p),List.map (fun (n,_,_,_) -> (EConst (Ident n),p)) f.f_args),p) in
  828. let acc = (if csuper.cl_extern && acc = [] then [APublic] else acc) in
  829. let fnew = { f with f_expr = esuper; f_args = List.map (fun (a,opt,t,def) ->
  830. (*
  831. we are removing the type and letting the type inference
  832. work because the current package is not the same as the superclass one
  833. or there might be private and/or imported types
  834. if we are an extern class then we need a type
  835. if the type is Dynamic also because it would not propagate
  836. if we have a package declaration, we are sure it's fully qualified
  837. *)
  838. let rec is_qualified = function
  839. | TPNormal t -> is_qual_name t
  840. | TPParent t -> is_qualified t
  841. | TPFunction (tl,t) -> List.for_all is_qualified tl && is_qualified t
  842. | TPAnonymous fl -> List.for_all (fun (_,_,f,_) -> is_qual_field f) fl
  843. | TPExtend (t,fl) -> is_qual_name t && List.for_all (fun (_,_,f,_) -> is_qual_field f) fl
  844. and is_qual_field = function
  845. | AFVar t -> is_qualified t
  846. | AFProp (t,_,_) -> is_qualified t
  847. | AFFun (pl,t) -> List.for_all (fun (_,_,t) -> is_qualified t) pl && is_qualified t
  848. and is_qual_name t =
  849. match t.tpackage with
  850. | [] -> t.tname = "Dynamic" && List.for_all is_qual_param t.tparams
  851. | _ :: _ -> true
  852. and is_qual_param = function
  853. | TPType t -> is_qualified t
  854. | TPConst _ -> false (* prevent multiple incompatible types *)
  855. in
  856. let t = (match t with
  857. | Some t when is_qualified t -> Some t
  858. | _ -> None
  859. ) in
  860. a,opt,t,def
  861. ) f.f_args } in
  862. let _, _, cf, delayed = loop_cf (FFun ("new",None,[],acc,pl,fnew)) p in
  863. c.cl_constructor <- Some cf;
  864. Hashtbl.add ctx.constructs c.cl_path (acc,pl,f);
  865. ctx.delays := [delayed] :: !(ctx.delays);
  866. infos
  867. in
  868. (*
  869. extern classes will browse superclass to find a constructor
  870. *)
  871. if not c.cl_extern then ignore(define_constructor ctx c);
  872. fl
  873. let resolve_typedef ctx t =
  874. match t with
  875. | TClassDecl _ | TEnumDecl _ -> t
  876. | TTypeDecl td ->
  877. match follow td.t_type with
  878. | TEnum (e,_) -> TEnumDecl e
  879. | TInst (c,_) -> TClassDecl c
  880. | _ -> t
  881. let type_module ctx m tdecls loadp =
  882. (* PASS 1 : build module structure - does not load any module or type - should be atomic ! *)
  883. let decls = ref [] in
  884. let decl_with_name name p priv =
  885. let tpath = if priv then (fst m @ ["_" ^ snd m], name) else (fst m, name) in
  886. if priv && List.exists (fun t -> tpath = t_path t) (!decls) then error ("Type name " ^ name ^ " is already defined in this module") p;
  887. try
  888. let m2 = Hashtbl.find ctx.types_module tpath in
  889. if m <> m2 && String.lowercase (s_type_path m2) = String.lowercase (s_type_path m) then error ("Module " ^ s_type_path m2 ^ " is loaded with a different case than " ^ s_type_path m) loadp;
  890. error ("Type name " ^ s_type_path tpath ^ " is redefined from module " ^ s_type_path m2) p
  891. with
  892. Not_found ->
  893. Hashtbl.add ctx.types_module tpath m;
  894. tpath
  895. in
  896. List.iter (fun (d,p) ->
  897. match d with
  898. | EImport _ | EUsing _ -> ()
  899. | EClass d ->
  900. let priv = List.mem HPrivate d.d_flags in
  901. let path = decl_with_name d.d_name p priv in
  902. let c = mk_class path p in
  903. c.cl_private <- priv;
  904. c.cl_doc <- d.d_doc;
  905. c.cl_meta <- type_meta ctx d.d_meta;
  906. (* store the constructor for later usage *)
  907. List.iter (fun (cf,_) ->
  908. match cf with
  909. | FFun ("new",_,_,acc,pl,f) -> Hashtbl.add ctx.constructs path (acc,pl,f)
  910. | _ -> ()
  911. ) d.d_data;
  912. decls := TClassDecl c :: !decls
  913. | EEnum d ->
  914. let priv = List.mem EPrivate d.d_flags in
  915. let path = decl_with_name d.d_name p priv in
  916. let e = {
  917. e_path = path;
  918. e_pos = p;
  919. e_doc = d.d_doc;
  920. e_meta = type_meta ctx d.d_meta;
  921. e_types = [];
  922. e_private = priv;
  923. e_extern = List.mem EExtern d.d_flags || d.d_data = [];
  924. e_constrs = PMap.empty;
  925. e_names = [];
  926. } in
  927. decls := TEnumDecl e :: !decls
  928. | ETypedef d ->
  929. let priv = List.mem EPrivate d.d_flags in
  930. let path = decl_with_name d.d_name p priv in
  931. let t = {
  932. t_path = path;
  933. t_pos = p;
  934. t_doc = d.d_doc;
  935. t_private = priv;
  936. t_types = [];
  937. t_type = mk_mono();
  938. t_meta = type_meta ctx d.d_meta;
  939. } in
  940. decls := TTypeDecl t :: !decls
  941. ) tdecls;
  942. let m = {
  943. mpath = m;
  944. mtypes = List.rev !decls;
  945. } in
  946. Hashtbl.add ctx.modules m.mpath m;
  947. (* PASS 2 : build types structure - does not type any expression ! *)
  948. let ctx = {
  949. com = ctx.com;
  950. api = ctx.api;
  951. core_api = ctx.core_api;
  952. modules = ctx.modules;
  953. delays = ctx.delays;
  954. constructs = ctx.constructs;
  955. types_module = ctx.types_module;
  956. curclass = ctx.curclass;
  957. tthis = ctx.tthis;
  958. std = ctx.std;
  959. ret = ctx.ret;
  960. doinline = ctx.doinline;
  961. current = m;
  962. locals = PMap.empty;
  963. locals_map = PMap.empty;
  964. locals_map_inv = PMap.empty;
  965. local_types = ctx.std.mtypes @ m.mtypes;
  966. local_using = [];
  967. type_params = [];
  968. curmethod = "";
  969. super_call = false;
  970. in_constructor = false;
  971. in_static = false;
  972. in_display = false;
  973. in_loop = false;
  974. untyped = false;
  975. opened = [];
  976. param_type = None;
  977. } in
  978. let delays = ref [] in
  979. let get_class name =
  980. let c = List.find (fun d -> match d with TClassDecl { cl_path = _ , n } -> n = name | _ -> false) m.mtypes in
  981. match c with TClassDecl c -> c | _ -> assert false
  982. in
  983. let get_enum name =
  984. let e = List.find (fun d -> match d with TEnumDecl { e_path = _ , n } -> n = name | _ -> false) m.mtypes in
  985. match e with TEnumDecl e -> e | _ -> assert false
  986. in
  987. let get_tdef name =
  988. let s = List.find (fun d -> match d with TTypeDecl { t_path = _ , n } -> n = name | _ -> false) m.mtypes in
  989. match s with TTypeDecl s -> s | _ -> assert false
  990. in
  991. (* here is an additional PASS 1 phase, which handle the type parameters declaration, with lazy contraints *)
  992. List.iter (fun (d,p) ->
  993. match d with
  994. | EImport _ | EUsing _ -> ()
  995. | EClass d ->
  996. let c = get_class d.d_name in
  997. c.cl_types <- List.map (type_type_params ctx c.cl_path p) d.d_params;
  998. | EEnum d ->
  999. let e = get_enum d.d_name in
  1000. e.e_types <- List.map (type_type_params ctx e.e_path p) d.d_params;
  1001. | ETypedef d ->
  1002. let t = get_tdef d.d_name in
  1003. t.t_types <- List.map (type_type_params ctx t.t_path p) d.d_params;
  1004. ) tdecls;
  1005. (* back to PASS2 *)
  1006. List.iter (fun (d,p) ->
  1007. match d with
  1008. | EImport t ->
  1009. (match t.tsub with
  1010. | None ->
  1011. let md = ctx.api.load_module (t.tpackage,t.tname) p in
  1012. let types = List.filter (fun t -> not (t_private t)) md.mtypes in
  1013. ctx.local_types <- ctx.local_types @ types
  1014. | Some _ ->
  1015. let t = load_type_def ctx p t in
  1016. ctx.local_types <- ctx.local_types @ [t]
  1017. )
  1018. | EUsing t ->
  1019. (match t.tsub with
  1020. | None ->
  1021. let md = ctx.api.load_module (t.tpackage,t.tname) p in
  1022. let types = List.filter (fun t -> not (t_private t)) md.mtypes in
  1023. ctx.local_using <- ctx.local_using @ (List.map (resolve_typedef ctx) types);
  1024. | Some _ ->
  1025. let t = load_type_def ctx p t in
  1026. ctx.local_using<- ctx.local_using @ [resolve_typedef ctx t])
  1027. | EClass d ->
  1028. let c = get_class d.d_name in
  1029. delays := !delays @ check_overriding ctx c p :: check_interfaces ctx c p :: init_class ctx c p d.d_flags d.d_data
  1030. | EEnum d ->
  1031. let e = get_enum d.d_name in
  1032. ctx.type_params <- e.e_types;
  1033. let et = TEnum (e,List.map snd e.e_types) in
  1034. let names = ref [] in
  1035. let index = ref 0 in
  1036. List.iter (fun (c,doc,meta,t,p) ->
  1037. if c = "name" && Common.defined ctx.com "js" then error "This identifier cannot be used in Javascript" p;
  1038. let t = (match t with
  1039. | [] -> et
  1040. | l ->
  1041. let pnames = ref PMap.empty in
  1042. TFun (List.map (fun (s,opt,t) ->
  1043. if PMap.mem s (!pnames) then error ("Duplicate parameter '" ^ s ^ "' in enum constructor " ^ c) p;
  1044. pnames := PMap.add s () (!pnames);
  1045. s, opt, load_type_opt ~opt ctx p (Some t)
  1046. ) l, et)
  1047. ) in
  1048. if PMap.mem c e.e_constrs then error ("Duplicate constructor " ^ c) p;
  1049. e.e_constrs <- PMap.add c {
  1050. ef_name = c;
  1051. ef_type = t;
  1052. ef_pos = p;
  1053. ef_doc = doc;
  1054. ef_index = !index;
  1055. ef_meta = type_meta ctx meta;
  1056. } e.e_constrs;
  1057. incr index;
  1058. names := c :: !names;
  1059. ) d.d_data;
  1060. e.e_names <- List.rev !names;
  1061. | ETypedef d ->
  1062. let t = get_tdef d.d_name in
  1063. ctx.type_params <- t.t_types;
  1064. let tt = load_complex_type ctx p d.d_data in
  1065. if t.t_type == follow tt then error "Recursive typedef is not allowed" p;
  1066. (match t.t_type with
  1067. | TMono r ->
  1068. (match !r with
  1069. | None -> r := Some tt;
  1070. | Some _ -> assert false);
  1071. | _ -> assert false);
  1072. ) tdecls;
  1073. (* PASS 3 : type checking, delayed until all modules and types are built *)
  1074. ctx.delays := !delays :: !(ctx.delays);
  1075. m
  1076. let parse_module ctx m p =
  1077. let remap = ref (fst m) in
  1078. let file = (match m with
  1079. | [] , name -> name
  1080. | x :: l , name ->
  1081. let x = (try
  1082. match PMap.find x ctx.com.package_rules with
  1083. | Forbidden -> error ("You can't access the " ^ x ^ " package with current compilation flags (for " ^ s_type_path m ^ ")") p;
  1084. | Directory d -> d
  1085. | Remap d -> remap := d :: l; d
  1086. with Not_found -> x
  1087. ) in
  1088. String.concat "/" (x :: l) ^ "/" ^ name
  1089. ) ^ ".hx" in
  1090. let file = Common.find_file ctx.com file in
  1091. let ch = (try open_in_bin file with _ -> error ("Could not open " ^ file) p) in
  1092. let t = Common.timer "parsing" in
  1093. let pack , decls = (try Parser.parse ctx.com (Lexing.from_channel ch) file with e -> close_in ch; t(); raise e) in
  1094. t();
  1095. close_in ch;
  1096. if ctx.com.verbose then print_endline ("Parsed " ^ file);
  1097. if pack <> !remap then begin
  1098. let spack m = if m = [] then "<empty>" else String.concat "." m in
  1099. if p == Ast.null_pos then
  1100. error ("Invalid commandline class : " ^ s_type_path m ^ " should be " ^ s_type_path (pack,snd m)) p
  1101. else
  1102. error ("Invalid package : " ^ spack (fst m) ^ " should be " ^ spack pack) p
  1103. end;
  1104. if !remap <> fst m then
  1105. (* build typedefs to redirect to real package *)
  1106. List.rev (List.fold_left (fun acc (t,p) ->
  1107. let build f d =
  1108. let priv = List.mem f d.d_flags in
  1109. (ETypedef {
  1110. d_name = d.d_name;
  1111. d_doc = None;
  1112. d_meta = [];
  1113. d_params = d.d_params;
  1114. d_flags = if priv then [EPrivate] else [];
  1115. d_data = TPNormal (if priv then { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None; } else
  1116. {
  1117. tpackage = !remap;
  1118. tname = d.d_name;
  1119. tparams = List.map (fun (s,_) ->
  1120. TPType (TPNormal { tpackage = []; tname = s; tparams = []; tsub = None; })
  1121. ) d.d_params;
  1122. tsub = None;
  1123. });
  1124. },p) :: acc
  1125. in
  1126. match t with
  1127. | EClass d -> build HPrivate d
  1128. | EEnum d -> build EPrivate d
  1129. | ETypedef d -> build EPrivate d
  1130. | EImport _ | EUsing _ -> acc
  1131. ) [(EImport { tpackage = !remap; tname = snd m; tparams = []; tsub = None; },null_pos)] decls)
  1132. else
  1133. decls
  1134. let load_module ctx m p =
  1135. try
  1136. Hashtbl.find ctx.modules m
  1137. with
  1138. Not_found ->
  1139. let decls = (try
  1140. parse_module ctx m p
  1141. with Not_found ->
  1142. let rec loop = function
  1143. | [] -> raise (Error (Module_not_found m,p))
  1144. | load :: l -> try snd (load m p) with Not_found -> loop l
  1145. in
  1146. loop ctx.api.load_extern_type
  1147. ) in
  1148. type_module ctx m decls p