typeload.ml 51 KB

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