typeload.ml 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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 = no_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 has_meta ":final" cl.cl_meta && not (has_meta ":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 mcache = ref None in
  515. let notconst e = error "Metadata should be constant" e.epos in
  516. let rec chk_const e =
  517. match e.eexpr with
  518. | TConst c ->
  519. (match c with
  520. | TInt _ | TFloat _ | TString _ | TBool _ | TNull -> ()
  521. | _ -> notconst e)
  522. | TParenthesis e ->
  523. chk_const e
  524. | TObjectDecl el ->
  525. List.iter (fun (_,e) -> chk_const e) el
  526. | TArrayDecl el ->
  527. List.iter chk_const el
  528. | _ ->
  529. notconst e
  530. in
  531. let mk_meta (m,el) =
  532. let el = List.map (fun e -> type_expr ctx e true) el in
  533. List.iter chk_const el;
  534. m, el
  535. in
  536. let get_meta() =
  537. match !mcache with
  538. | None ->
  539. let ml = List.map mk_meta meta in
  540. mcache := Some ml;
  541. ml
  542. | Some ml -> ml
  543. in
  544. ctx.delays := [[fun() -> ignore(get_meta())]] @ !(ctx.delays);
  545. get_meta
  546. let init_core_api ctx c =
  547. let ctx2 = (match !(ctx.core_api) with
  548. | None ->
  549. let com = ctx.com in
  550. let com = { com with class_path = com.std_path; type_api = { com.type_api with tvoid = com.type_api.tvoid } } in
  551. let ctx2 = (!do_create) com in
  552. ctx.core_api := Some ctx2;
  553. ctx2
  554. | Some c ->
  555. c
  556. ) in
  557. let t = load_instance ctx2 { tpackage = fst c.cl_path; tname = snd c.cl_path; tparams = []; tsub = None; } c.cl_pos true in
  558. match t with
  559. | TInst (ccore,_) ->
  560. (match c.cl_doc with
  561. | None -> c.cl_doc <- ccore.cl_doc
  562. | Some _ -> ());
  563. let check_fields fcore fl =
  564. PMap.iter (fun i f ->
  565. if not f.cf_public then () else
  566. let f2 = try PMap.find f.cf_name fl with Not_found -> error ("Missing field " ^ i ^ " required by core type") c.cl_pos in
  567. let p = (match f2.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  568. (try
  569. type_eq EqCoreType (apply_params ccore.cl_types (List.map snd c.cl_types) f.cf_type) f2.cf_type
  570. with Unify_error l ->
  571. display_error ctx ("Field " ^ i ^ " has different type than in core type") p;
  572. display_error ctx (error_msg (Unify l)) p);
  573. if f2.cf_public <> f.cf_public then error ("Field " ^ i ^ " has different visibility than core type") p;
  574. (match f2.cf_doc with
  575. | None -> f2.cf_doc <- f.cf_doc
  576. | Some _ -> ());
  577. if f2.cf_get <> f.cf_get || f2.cf_set <> f.cf_set then begin
  578. match f2.cf_get, f.cf_get, f2.cf_set, f.cf_set with
  579. | InlineAccess, NormalAccess, NeverAccess, MethodAccess false -> () (* allow to add 'inline' *)
  580. | NormalAccess, InlineAccess, MethodAccess false, NeverAccess -> () (* allow to remove 'inline' - only during transition ? *)
  581. | _ ->
  582. error ("Field " ^ i ^ " has different property access than core type") p;
  583. end;
  584. (match follow f.cf_type, follow f2.cf_type with
  585. | TFun (pl1,_), TFun (pl2,_) ->
  586. if List.length pl1 != List.length pl2 then assert false;
  587. List.iter2 (fun (n1,_,_) (n2,_,_) ->
  588. if n1 <> n2 then error ("Method parameter name '" ^ n2 ^ "' should be '" ^ n1 ^ "'") p;
  589. ) pl1 pl2;
  590. | _ -> ());
  591. ) fcore;
  592. PMap.iter (fun i f ->
  593. let p = (match f.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  594. if f.cf_public && not (PMap.mem f.cf_name fcore) then error ("Public field " ^ i ^ " is not part of core type") p;
  595. ) fl;
  596. in
  597. check_fields ccore.cl_fields c.cl_fields;
  598. check_fields ccore.cl_statics c.cl_statics;
  599. | _ -> assert false
  600. let init_class ctx c p herits fields =
  601. ctx.type_params <- c.cl_types;
  602. c.cl_extern <- List.mem HExtern herits;
  603. c.cl_interface <- List.mem HInterface herits;
  604. set_heritance ctx c herits p;
  605. let core_api = has_meta ":core_api" c.cl_meta in
  606. if core_api then ctx.delays := [(fun() -> init_core_api ctx c)] :: !(ctx.delays);
  607. let tthis = TInst (c,List.map snd c.cl_types) in
  608. let rec extends_public c =
  609. List.exists (fun (c,_) -> c.cl_path = (["haxe"],"Public") || extends_public c) c.cl_implements ||
  610. match c.cl_super with
  611. | None -> false
  612. | Some (c,_) -> extends_public c
  613. in
  614. let extends_public = extends_public c in
  615. let is_public access parent =
  616. if List.mem APrivate access then
  617. false
  618. else if List.mem APublic access then
  619. true
  620. else match parent with
  621. | Some { cf_public = p } -> p
  622. | _ -> c.cl_extern || c.cl_interface || extends_public
  623. in
  624. let rec get_parent c name =
  625. match c.cl_super with
  626. | None -> None
  627. | Some (csup,_) ->
  628. try
  629. Some (PMap.find name csup.cl_fields)
  630. with
  631. Not_found -> get_parent csup name
  632. in
  633. let type_opt ctx p t =
  634. match t with
  635. | None when c.cl_extern || c.cl_interface ->
  636. display_error ctx "Type required for extern classes and interfaces" p;
  637. t_dynamic
  638. | None when core_api ->
  639. display_error ctx "Type required for core api classes" p;
  640. t_dynamic
  641. | _ ->
  642. load_type_opt ctx p t
  643. in
  644. let rec has_field f = function
  645. | None -> false
  646. | Some (c,_) ->
  647. PMap.exists f c.cl_fields || has_field f c.cl_super || List.exists (fun i -> has_field f (Some i)) c.cl_implements
  648. in
  649. let loop_cf f p =
  650. match f with
  651. | FVar (name,doc,meta,access,t,e) ->
  652. let stat = List.mem AStatic access in
  653. let inline = List.mem AInline access in
  654. if not stat && has_field name c.cl_super then error ("Redefinition of variable " ^ name ^ " in subclass is not allowed") p;
  655. if inline && not stat then error "Inline variable must be static" p;
  656. if inline && e = None then error "Inline variable must be initialized" p;
  657. let t = (match t with
  658. | None ->
  659. if not stat then display_error ctx ("Type required for member variable " ^ name) p;
  660. mk_mono()
  661. | Some t ->
  662. let old = ctx.type_params in
  663. if stat then ctx.type_params <- [];
  664. let t = load_complex_type ctx p t in
  665. if stat then ctx.type_params <- old;
  666. t
  667. ) in
  668. let cf = {
  669. cf_name = name;
  670. cf_doc = doc;
  671. cf_meta = type_meta ctx meta;
  672. cf_type = t;
  673. cf_get = if inline then InlineAccess else NormalAccess;
  674. cf_set = if inline then NeverAccess else NormalAccess;
  675. cf_expr = None;
  676. cf_public = is_public access None;
  677. cf_params = [];
  678. } in
  679. let delay = (match e with
  680. | None -> (fun() -> ())
  681. | Some e ->
  682. let ctx = { ctx with curclass = c; tthis = tthis } in
  683. let r = exc_protect (fun r ->
  684. r := (fun() -> t);
  685. if ctx.com.verbose then print_endline ("Typing " ^ s_type_path c.cl_path ^ "." ^ name);
  686. cf.cf_expr <- Some (type_static_var ctx t e p);
  687. t
  688. ) in
  689. cf.cf_type <- TLazy r;
  690. (fun () -> ignore(!r()))
  691. ) in
  692. access, false, cf, delay
  693. | FFun (name,doc,meta,access,params,f) ->
  694. let params = List.map (fun (n,flags) ->
  695. match flags with
  696. | [] ->
  697. type_type_params ctx ([],name) p (n,[])
  698. | _ -> error "This notation is not allowed because it can't be checked" p
  699. ) params in
  700. let stat = List.mem AStatic access in
  701. let inline = List.mem AInline access in
  702. if inline && c.cl_interface then error "You can't declare inline methods in interfaces" p;
  703. let parent = (if not stat then get_parent c name else None) in
  704. let dynamic = List.mem ADynamic access || (match parent with Some { cf_set = MethodAccess true } -> true | _ -> false) in
  705. let ctx = { ctx with
  706. curclass = c;
  707. curmethod = name;
  708. tthis = tthis;
  709. type_params = if stat then params else params @ ctx.type_params;
  710. } in
  711. let ret = type_opt ctx p f.f_type in
  712. let args = List.map (fun (name,opt,t,c) ->
  713. let t, c = type_function_param ctx (type_opt ctx p t) c opt p in
  714. name, c, t
  715. ) f.f_args in
  716. let t = TFun (fun_args args,ret) in
  717. let constr = (name = "new") in
  718. if constr && c.cl_interface then error "An interface cannot have a constructor" p;
  719. if c.cl_interface && not stat && (match f.f_expr with EBlock [] , _ -> false | _ -> true) then error "An interface method cannot have a body" p;
  720. if constr then (match f.f_type with
  721. | None | Some (TPNormal { tpackage = []; tname = "Void" }) -> ()
  722. | _ -> error "A class constructor can't have a return value" p
  723. );
  724. let cf = {
  725. cf_name = name;
  726. cf_doc = doc;
  727. cf_meta = type_meta ctx meta;
  728. cf_type = t;
  729. cf_get = if inline then InlineAccess else NormalAccess;
  730. cf_set = (if inline then NeverAccess else MethodAccess dynamic);
  731. cf_expr = None;
  732. cf_public = is_public access parent;
  733. cf_params = params;
  734. } in
  735. let r = exc_protect (fun r ->
  736. r := (fun() -> t);
  737. if ctx.com.verbose then print_endline ("Typing " ^ s_type_path c.cl_path ^ "." ^ name);
  738. let e , fargs = type_function ctx args ret stat constr f p in
  739. let f = {
  740. tf_args = fargs;
  741. tf_type = ret;
  742. tf_expr = e;
  743. } in
  744. if stat && name = "__init__" then
  745. (match e.eexpr with
  746. | TBlock [] | TBlock [{ eexpr = TConst _ }] | TConst _ | TObjectDecl [] -> ()
  747. | _ -> c.cl_init <- Some e);
  748. cf.cf_expr <- Some (mk (TFunction f) t p);
  749. t
  750. ) in
  751. let delay = (
  752. if ((c.cl_extern && not inline) || c.cl_interface) && cf.cf_name <> "__init__" then
  753. (fun() -> ())
  754. else begin
  755. cf.cf_type <- TLazy r;
  756. (fun() -> ignore((!r)()))
  757. end
  758. ) in
  759. access, constr, cf, delay
  760. | FProp (name,doc,meta,access,get,set,t) ->
  761. let ret = load_complex_type ctx p t in
  762. let check_get = ref (fun() -> ()) in
  763. let check_set = ref (fun() -> ()) in
  764. let check_method m t () =
  765. try
  766. let t2 = (if List.mem AStatic access then (PMap.find m c.cl_statics).cf_type else fst (class_field c m)) in
  767. unify_raise ctx t2 t p;
  768. with
  769. | Error (Unify l,_) -> raise (Error (Stack (Custom ("In method " ^ m ^ " required by property " ^ name),Unify l),p))
  770. | Not_found -> if not c.cl_interface then error ("Method " ^ m ^ " required by property " ^ name ^ " is missing") p
  771. in
  772. let get = (match get with
  773. | "null" -> NoAccess
  774. | "dynamic" -> CallAccess ("get_" ^ name)
  775. | "never" -> NeverAccess
  776. | "default" -> NormalAccess
  777. | _ ->
  778. check_get := check_method get (TFun ([],ret));
  779. CallAccess get
  780. ) in
  781. let set = (match set with
  782. | "null" ->
  783. (* standard flash library read-only variables can't be accessed for writing, even in subclasses *)
  784. if c.cl_extern && (match c.cl_path with "flash" :: _ , _ -> true | _ -> false) && Common.defined ctx.com "flash9" then
  785. NeverAccess
  786. else
  787. NoAccess
  788. | "never" -> NeverAccess
  789. | "dynamic" -> CallAccess ("set_" ^ name)
  790. | "default" -> NormalAccess
  791. | _ ->
  792. check_set := check_method set (TFun (["",false,ret],ret));
  793. CallAccess set
  794. ) in
  795. if set = NormalAccess && (match get with CallAccess _ -> true | _ -> false) then error "Unsupported property combination" p;
  796. let cf = {
  797. cf_name = name;
  798. cf_doc = doc;
  799. cf_meta = type_meta ctx meta;
  800. cf_get = get;
  801. cf_set = set;
  802. cf_expr = None;
  803. cf_type = ret;
  804. cf_public = is_public access None;
  805. cf_params = [];
  806. } in
  807. access, false, cf, (fun() -> (!check_get)(); (!check_set)())
  808. in
  809. let fl = List.map (fun (f,p) ->
  810. let access , constr, f , delayed = loop_cf f p in
  811. let is_static = List.mem AStatic access in
  812. if is_static && f.cf_name = "name" && Common.defined ctx.com "js" then error "This identifier cannot be used in Javascript for statics" p;
  813. if (is_static || constr) && c.cl_interface && f.cf_name <> "__init__" then error "You can't declare static fields in interfaces" p;
  814. if constr then begin
  815. if c.cl_constructor <> None then error "Duplicate constructor" p;
  816. c.cl_constructor <- Some f;
  817. end else if not is_static || f.cf_name <> "__init__" then begin
  818. 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;
  819. 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;
  820. if is_static then begin
  821. c.cl_statics <- PMap.add f.cf_name f c.cl_statics;
  822. c.cl_ordered_statics <- f :: c.cl_ordered_statics;
  823. end else begin
  824. c.cl_fields <- PMap.add f.cf_name f c.cl_fields;
  825. c.cl_ordered_fields <- f :: c.cl_ordered_fields;
  826. if List.mem AOverride access then c.cl_overrides <- f.cf_name :: c.cl_overrides;
  827. end;
  828. end;
  829. delayed
  830. ) fields in
  831. c.cl_ordered_statics <- List.rev c.cl_ordered_statics;
  832. c.cl_ordered_fields <- List.rev c.cl_ordered_fields;
  833. (*
  834. define a default inherited constructor.
  835. This is actually pretty tricky since we can't assume that the constructor of the
  836. superclass has been defined yet because type structure is not stabilized wrt recursion.
  837. *)
  838. let rec define_constructor ctx c =
  839. try
  840. Some (Hashtbl.find ctx.constructs c.cl_path)
  841. with Not_found ->
  842. match c.cl_super with
  843. | None -> None
  844. | Some (csuper,_) ->
  845. match define_constructor ctx csuper with
  846. | None -> None
  847. | Some (acc,pl,f) as infos ->
  848. let p = c.cl_pos in
  849. let esuper = (ECall ((EConst (Ident "super"),p),List.map (fun (n,_,_,_) -> (EConst (Ident n),p)) f.f_args),p) in
  850. let acc = (if csuper.cl_extern && acc = [] then [APublic] else acc) in
  851. let fnew = { f with f_expr = esuper; f_args = List.map (fun (a,opt,t,def) ->
  852. (*
  853. we are removing the type and letting the type inference
  854. work because the current package is not the same as the superclass one
  855. or there might be private and/or imported types
  856. if we are an extern class then we need a type
  857. if the type is Dynamic also because it would not propagate
  858. if we have a package declaration, we are sure it's fully qualified
  859. *)
  860. let rec is_qualified = function
  861. | TPNormal t -> is_qual_name t
  862. | TPParent t -> is_qualified t
  863. | TPFunction (tl,t) -> List.for_all is_qualified tl && is_qualified t
  864. | TPAnonymous fl -> List.for_all (fun (_,_,f,_) -> is_qual_field f) fl
  865. | TPExtend (t,fl) -> is_qual_name t && List.for_all (fun (_,_,f,_) -> is_qual_field f) fl
  866. and is_qual_field = function
  867. | AFVar t -> is_qualified t
  868. | AFProp (t,_,_) -> is_qualified t
  869. | AFFun (pl,t) -> List.for_all (fun (_,_,t) -> is_qualified t) pl && is_qualified t
  870. and is_qual_name t =
  871. match t.tpackage with
  872. | [] -> t.tname = "Dynamic" && List.for_all is_qual_param t.tparams
  873. | _ :: _ -> true
  874. and is_qual_param = function
  875. | TPType t -> is_qualified t
  876. | TPConst _ -> false (* prevent multiple incompatible types *)
  877. in
  878. let t = (match t with
  879. | Some t when is_qualified t -> Some t
  880. | _ -> None
  881. ) in
  882. a,opt,t,def
  883. ) f.f_args } in
  884. let _, _, cf, delayed = loop_cf (FFun ("new",None,[],acc,pl,fnew)) p in
  885. c.cl_constructor <- Some cf;
  886. Hashtbl.add ctx.constructs c.cl_path (acc,pl,f);
  887. ctx.delays := [delayed] :: !(ctx.delays);
  888. infos
  889. in
  890. (*
  891. extern classes will browse superclass to find a constructor
  892. *)
  893. if not c.cl_extern then ignore(define_constructor ctx c);
  894. fl
  895. let resolve_typedef ctx t =
  896. match t with
  897. | TClassDecl _ | TEnumDecl _ -> t
  898. | TTypeDecl td ->
  899. match follow td.t_type with
  900. | TEnum (e,_) -> TEnumDecl e
  901. | TInst (c,_) -> TClassDecl c
  902. | _ -> t
  903. let type_module ctx m tdecls loadp =
  904. (* PASS 1 : build module structure - does not load any module or type - should be atomic ! *)
  905. let decls = ref [] in
  906. let decl_with_name name p priv =
  907. let tpath = if priv then (fst m @ ["_" ^ snd m], name) else (fst m, name) in
  908. if priv && List.exists (fun t -> tpath = t_path t) (!decls) then error ("Type name " ^ name ^ " is already defined in this module") p;
  909. try
  910. let m2 = Hashtbl.find ctx.types_module tpath in
  911. 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;
  912. error ("Type name " ^ s_type_path tpath ^ " is redefined from module " ^ s_type_path m2) p
  913. with
  914. Not_found ->
  915. Hashtbl.add ctx.types_module tpath m;
  916. tpath
  917. in
  918. List.iter (fun (d,p) ->
  919. match d with
  920. | EImport _ | EUsing _ -> ()
  921. | EClass d ->
  922. let priv = List.mem HPrivate d.d_flags in
  923. let path = decl_with_name d.d_name p priv in
  924. let c = mk_class path p in
  925. c.cl_private <- priv;
  926. c.cl_doc <- d.d_doc;
  927. c.cl_meta <- type_meta ctx d.d_meta;
  928. (* store the constructor for later usage *)
  929. List.iter (fun (cf,_) ->
  930. match cf with
  931. | FFun ("new",_,_,acc,pl,f) -> Hashtbl.add ctx.constructs path (acc,pl,f)
  932. | _ -> ()
  933. ) d.d_data;
  934. decls := TClassDecl c :: !decls
  935. | EEnum d ->
  936. let priv = List.mem EPrivate d.d_flags in
  937. let path = decl_with_name d.d_name p priv in
  938. let e = {
  939. e_path = path;
  940. e_pos = p;
  941. e_doc = d.d_doc;
  942. e_meta = type_meta ctx d.d_meta;
  943. e_types = [];
  944. e_private = priv;
  945. e_extern = List.mem EExtern d.d_flags || d.d_data = [];
  946. e_constrs = PMap.empty;
  947. e_names = [];
  948. } in
  949. decls := TEnumDecl e :: !decls
  950. | ETypedef d ->
  951. let priv = List.mem EPrivate d.d_flags in
  952. let path = decl_with_name d.d_name p priv in
  953. let t = {
  954. t_path = path;
  955. t_pos = p;
  956. t_doc = d.d_doc;
  957. t_private = priv;
  958. t_types = [];
  959. t_type = mk_mono();
  960. t_meta = type_meta ctx d.d_meta;
  961. } in
  962. decls := TTypeDecl t :: !decls
  963. ) tdecls;
  964. let m = {
  965. mpath = m;
  966. mtypes = List.rev !decls;
  967. } in
  968. Hashtbl.add ctx.modules m.mpath m;
  969. (* PASS 2 : build types structure - does not type any expression ! *)
  970. let ctx = {
  971. com = ctx.com;
  972. api = ctx.api;
  973. core_api = ctx.core_api;
  974. modules = ctx.modules;
  975. delays = ctx.delays;
  976. constructs = ctx.constructs;
  977. types_module = ctx.types_module;
  978. curclass = ctx.curclass;
  979. tthis = ctx.tthis;
  980. std = ctx.std;
  981. ret = ctx.ret;
  982. doinline = ctx.doinline;
  983. current = m;
  984. locals = PMap.empty;
  985. locals_map = PMap.empty;
  986. locals_map_inv = PMap.empty;
  987. local_types = ctx.std.mtypes @ m.mtypes;
  988. local_using = [];
  989. type_params = [];
  990. curmethod = "";
  991. super_call = false;
  992. in_constructor = false;
  993. in_static = false;
  994. in_display = false;
  995. in_loop = false;
  996. untyped = false;
  997. opened = [];
  998. param_type = None;
  999. } in
  1000. let delays = ref [] in
  1001. let get_class name =
  1002. let c = List.find (fun d -> match d with TClassDecl { cl_path = _ , n } -> n = name | _ -> false) m.mtypes in
  1003. match c with TClassDecl c -> c | _ -> assert false
  1004. in
  1005. let get_enum name =
  1006. let e = List.find (fun d -> match d with TEnumDecl { e_path = _ , n } -> n = name | _ -> false) m.mtypes in
  1007. match e with TEnumDecl e -> e | _ -> assert false
  1008. in
  1009. let get_tdef name =
  1010. let s = List.find (fun d -> match d with TTypeDecl { t_path = _ , n } -> n = name | _ -> false) m.mtypes in
  1011. match s with TTypeDecl s -> s | _ -> assert false
  1012. in
  1013. (* here is an additional PASS 1 phase, which handle the type parameters declaration, with lazy contraints *)
  1014. List.iter (fun (d,p) ->
  1015. match d with
  1016. | EImport _ | EUsing _ -> ()
  1017. | EClass d ->
  1018. let c = get_class d.d_name in
  1019. c.cl_types <- List.map (type_type_params ctx c.cl_path p) d.d_params;
  1020. | EEnum d ->
  1021. let e = get_enum d.d_name in
  1022. e.e_types <- List.map (type_type_params ctx e.e_path p) d.d_params;
  1023. | ETypedef d ->
  1024. let t = get_tdef d.d_name in
  1025. t.t_types <- List.map (type_type_params ctx t.t_path p) d.d_params;
  1026. ) tdecls;
  1027. (* back to PASS2 *)
  1028. List.iter (fun (d,p) ->
  1029. match d with
  1030. | EImport t ->
  1031. (match t.tsub with
  1032. | None ->
  1033. let md = ctx.api.load_module (t.tpackage,t.tname) p in
  1034. let types = List.filter (fun t -> not (t_private t)) md.mtypes in
  1035. ctx.local_types <- ctx.local_types @ types
  1036. | Some _ ->
  1037. let t = load_type_def ctx p t in
  1038. ctx.local_types <- ctx.local_types @ [t]
  1039. )
  1040. | EUsing t ->
  1041. (match t.tsub with
  1042. | None ->
  1043. let md = ctx.api.load_module (t.tpackage,t.tname) p in
  1044. let types = List.filter (fun t -> not (t_private t)) md.mtypes in
  1045. ctx.local_using <- ctx.local_using @ (List.map (resolve_typedef ctx) types);
  1046. | Some _ ->
  1047. let t = load_type_def ctx p t in
  1048. ctx.local_using<- ctx.local_using @ [resolve_typedef ctx t])
  1049. | EClass d ->
  1050. let c = get_class d.d_name in
  1051. delays := !delays @ check_overriding ctx c p :: check_interfaces ctx c p :: init_class ctx c p d.d_flags d.d_data
  1052. | EEnum d ->
  1053. let e = get_enum d.d_name in
  1054. ctx.type_params <- e.e_types;
  1055. let et = TEnum (e,List.map snd e.e_types) in
  1056. let names = ref [] in
  1057. let index = ref 0 in
  1058. List.iter (fun (c,doc,meta,t,p) ->
  1059. if c = "name" && Common.defined ctx.com "js" then error "This identifier cannot be used in Javascript" p;
  1060. let t = (match t with
  1061. | [] -> et
  1062. | l ->
  1063. let pnames = ref PMap.empty in
  1064. TFun (List.map (fun (s,opt,t) ->
  1065. if PMap.mem s (!pnames) then error ("Duplicate parameter '" ^ s ^ "' in enum constructor " ^ c) p;
  1066. pnames := PMap.add s () (!pnames);
  1067. s, opt, load_type_opt ~opt ctx p (Some t)
  1068. ) l, et)
  1069. ) in
  1070. if PMap.mem c e.e_constrs then error ("Duplicate constructor " ^ c) p;
  1071. e.e_constrs <- PMap.add c {
  1072. ef_name = c;
  1073. ef_type = t;
  1074. ef_pos = p;
  1075. ef_doc = doc;
  1076. ef_index = !index;
  1077. ef_meta = type_meta ctx meta;
  1078. } e.e_constrs;
  1079. incr index;
  1080. names := c :: !names;
  1081. ) d.d_data;
  1082. e.e_names <- List.rev !names;
  1083. | ETypedef d ->
  1084. let t = get_tdef d.d_name in
  1085. ctx.type_params <- t.t_types;
  1086. let tt = load_complex_type ctx p d.d_data in
  1087. if t.t_type == follow tt then error "Recursive typedef is not allowed" p;
  1088. (match t.t_type with
  1089. | TMono r ->
  1090. (match !r with
  1091. | None -> r := Some tt;
  1092. | Some _ -> assert false);
  1093. | _ -> assert false);
  1094. ) tdecls;
  1095. (* PASS 3 : type checking, delayed until all modules and types are built *)
  1096. ctx.delays := !delays :: !(ctx.delays);
  1097. m
  1098. let parse_module ctx m p =
  1099. let remap = ref (fst m) in
  1100. let file = (match m with
  1101. | [] , name -> name
  1102. | x :: l , name ->
  1103. let x = (try
  1104. match PMap.find x ctx.com.package_rules with
  1105. | Forbidden -> error ("You can't access the " ^ x ^ " package with current compilation flags (for " ^ s_type_path m ^ ")") p;
  1106. | Directory d -> d
  1107. | Remap d -> remap := d :: l; d
  1108. with Not_found -> x
  1109. ) in
  1110. String.concat "/" (x :: l) ^ "/" ^ name
  1111. ) ^ ".hx" in
  1112. let file = Common.find_file ctx.com file in
  1113. let ch = (try open_in_bin file with _ -> error ("Could not open " ^ file) p) in
  1114. let t = Common.timer "parsing" in
  1115. let pack , decls = (try Parser.parse ctx.com (Lexing.from_channel ch) file with e -> close_in ch; t(); raise e) in
  1116. t();
  1117. close_in ch;
  1118. if ctx.com.verbose then print_endline ("Parsed " ^ file);
  1119. if pack <> !remap then begin
  1120. let spack m = if m = [] then "<empty>" else String.concat "." m in
  1121. if p == Ast.null_pos then
  1122. error ("Invalid commandline class : " ^ s_type_path m ^ " should be " ^ s_type_path (pack,snd m)) p
  1123. else
  1124. error ("Invalid package : " ^ spack (fst m) ^ " should be " ^ spack pack) p
  1125. end;
  1126. if !remap <> fst m then
  1127. (* build typedefs to redirect to real package *)
  1128. List.rev (List.fold_left (fun acc (t,p) ->
  1129. let build f d =
  1130. let priv = List.mem f d.d_flags in
  1131. (ETypedef {
  1132. d_name = d.d_name;
  1133. d_doc = None;
  1134. d_meta = [];
  1135. d_params = d.d_params;
  1136. d_flags = if priv then [EPrivate] else [];
  1137. d_data = TPNormal (if priv then { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None; } else
  1138. {
  1139. tpackage = !remap;
  1140. tname = d.d_name;
  1141. tparams = List.map (fun (s,_) ->
  1142. TPType (TPNormal { tpackage = []; tname = s; tparams = []; tsub = None; })
  1143. ) d.d_params;
  1144. tsub = None;
  1145. });
  1146. },p) :: acc
  1147. in
  1148. match t with
  1149. | EClass d -> build HPrivate d
  1150. | EEnum d -> build EPrivate d
  1151. | ETypedef d -> build EPrivate d
  1152. | EImport _ | EUsing _ -> acc
  1153. ) [(EImport { tpackage = !remap; tname = snd m; tparams = []; tsub = None; },null_pos)] decls)
  1154. else
  1155. decls
  1156. let load_module ctx m p =
  1157. try
  1158. Hashtbl.find ctx.modules m
  1159. with
  1160. Not_found ->
  1161. let decls = (try
  1162. parse_module ctx m p
  1163. with Not_found ->
  1164. let rec loop = function
  1165. | [] -> raise (Error (Module_not_found m,p))
  1166. | load :: l -> try snd (load m p) with Not_found -> loop l
  1167. in
  1168. loop ctx.api.load_extern_type
  1169. ) in
  1170. type_module ctx m decls p