typeload.ml 58 KB

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