typeload.ml 51 KB

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