2
0

typeload.ml 56 KB

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