typeload.ml 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. (*
  2. * Copyright (C)2005-2013 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *)
  22. open Ast
  23. open Type
  24. open Common
  25. open Typecore
  26. (*
  27. Build module structure : should be atomic - no type loading is possible
  28. *)
  29. let make_module ctx mpath file tdecls loadp =
  30. let decls = ref [] in
  31. let make_path name priv =
  32. if List.exists (fun (t,_) -> snd (t_path t) = name) !decls then error ("Type name " ^ name ^ " is already defined in this module") loadp;
  33. if priv then (fst mpath @ ["_" ^ snd mpath], name) else (fst mpath, name)
  34. in
  35. let m = {
  36. m_id = alloc_mid();
  37. m_path = mpath;
  38. m_types = [];
  39. 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);
  40. } in
  41. let pt = ref None in
  42. let rec make_decl acc decl =
  43. let p = snd decl in
  44. let acc = (match fst decl with
  45. | EImport _ | EUsing _ ->
  46. (match !pt with
  47. | None -> acc
  48. | Some pt ->
  49. display_error ctx "import and using may not appear after a type declaration" p;
  50. error "Previous type declaration found here" pt)
  51. | EClass d ->
  52. pt := Some p;
  53. let priv = List.mem HPrivate d.d_flags in
  54. let path = make_path d.d_name priv in
  55. let c = mk_class m path p in
  56. c.cl_module <- m;
  57. c.cl_private <- priv;
  58. c.cl_doc <- d.d_doc;
  59. c.cl_meta <- d.d_meta;
  60. decls := (TClassDecl c, decl) :: !decls;
  61. acc
  62. | EEnum d ->
  63. pt := Some p;
  64. let priv = List.mem EPrivate d.d_flags in
  65. let path = make_path d.d_name priv in
  66. let e = {
  67. e_path = path;
  68. e_module = m;
  69. e_pos = p;
  70. e_doc = d.d_doc;
  71. e_meta = d.d_meta;
  72. e_types = [];
  73. e_private = priv;
  74. e_extern = List.mem EExtern d.d_flags;
  75. e_constrs = PMap.empty;
  76. e_names = [];
  77. } in
  78. decls := (TEnumDecl e, decl) :: !decls;
  79. acc
  80. | ETypedef d ->
  81. pt := Some p;
  82. let priv = List.mem EPrivate d.d_flags in
  83. let path = make_path d.d_name priv in
  84. let t = {
  85. t_path = path;
  86. t_module = m;
  87. t_pos = p;
  88. t_doc = d.d_doc;
  89. t_private = priv;
  90. t_types = [];
  91. t_type = mk_mono();
  92. t_meta = d.d_meta;
  93. } in
  94. decls := (TTypeDecl t, decl) :: !decls;
  95. acc
  96. | EAbstract d ->
  97. let priv = List.mem APrivAbstract d.d_flags in
  98. let path = make_path d.d_name priv in
  99. let a = {
  100. a_path = path;
  101. a_private = priv;
  102. a_module = m;
  103. a_pos = p;
  104. a_doc = d.d_doc;
  105. a_types = [];
  106. a_meta = d.d_meta;
  107. a_from = [];
  108. a_to = [];
  109. a_ops = [];
  110. a_impl = None;
  111. a_array = [];
  112. a_this = mk_mono();
  113. } in
  114. decls := (TAbstractDecl a, decl) :: !decls;
  115. match d.d_data with
  116. | [] -> acc
  117. | fields ->
  118. let rec loop = function
  119. | [] ->
  120. let params = List.map (fun t -> TPType (CTPath { tname = t.tp_name; tparams = []; tsub = None; tpackage = [] })) d.d_params in
  121. CTPath { tpackage = []; tname = d.d_name; tparams = params; tsub = None }
  122. | AIsType t :: _ -> t
  123. | _ :: l -> loop l
  124. in
  125. let this_t = loop d.d_flags in
  126. let fields = List.map (fun f ->
  127. let stat = List.mem AStatic f.cff_access in
  128. let p = f.cff_pos in
  129. match f.cff_kind with
  130. | FVar _ | FProp _ when not stat ->
  131. display_error ctx "Cannot declare member variable or property in abstract" p;
  132. f
  133. | FFun fu when f.cff_name = "new" && not stat ->
  134. let init p = (EVars ["this",Some this_t,None],p) in
  135. let ret p = (EReturn (Some (EConst (Ident "this"),p)),p) in
  136. if Meta.has Meta.MultiType a.a_meta then begin
  137. if List.mem AInline f.cff_access then error "MultiType constructors cannot be inline" f.cff_pos;
  138. if fu.f_expr <> None then error "MultiType constructors cannot have a body" f.cff_pos;
  139. end;
  140. let fu = {
  141. fu with
  142. f_expr = (match fu.f_expr with
  143. | None -> if Meta.has Meta.MultiType a.a_meta then Some (EConst (Ident "null"),p) else None
  144. | Some (EBlock [EBinop (OpAssign,(EConst (Ident "this"),_),e),_],_ | EBinop (OpAssign,(EConst (Ident "this"),_),e),_) ->
  145. Some (EReturn (Some e), pos e)
  146. | Some (EBlock el,p) -> Some (EBlock (init p :: el @ [ret p]),p)
  147. | Some e -> Some (EBlock [init p;e;ret p],p)
  148. );
  149. f_type = Some this_t;
  150. } in
  151. { f with cff_name = "_new"; cff_access = AStatic :: f.cff_access; cff_kind = FFun fu; cff_meta = (Meta.Impl,[],p) :: f.cff_meta }
  152. | FFun fu when not stat ->
  153. if Meta.has Meta.From f.cff_meta then error "@:from cast functions must be static" f.cff_pos;
  154. let fu = { fu with f_args = ("this",false,Some this_t,None) :: fu.f_args } in
  155. { f with cff_kind = FFun fu; cff_access = AStatic :: f.cff_access; cff_meta = (Meta.Impl,[],p) :: f.cff_meta }
  156. | _ ->
  157. f
  158. ) fields in
  159. let acc = make_decl acc (EClass { d_name = d.d_name ^ "Impl"; d_flags = [HPrivate]; d_data = fields; d_doc = None; d_params = []; d_meta = [] },p) in
  160. (match !decls with
  161. | (TClassDecl c,_) :: _ ->
  162. a.a_impl <- Some c;
  163. c.cl_kind <- KAbstractImpl a
  164. | _ -> assert false);
  165. acc
  166. ) in
  167. decl :: acc
  168. in
  169. let tdecls = List.fold_left make_decl [] tdecls in
  170. let decls = List.rev !decls in
  171. m.m_types <- List.map fst decls;
  172. m, decls, List.rev tdecls
  173. let parse_file com file p =
  174. let ch = (try open_in_bin file with _ -> error ("Could not open " ^ file) p) in
  175. let t = Common.timer "parsing" in
  176. Lexer.init file;
  177. incr stats.s_files_parsed;
  178. let data = (try Parser.parse com (Lexing.from_channel ch) with e -> close_in ch; t(); raise e) in
  179. close_in ch;
  180. t();
  181. Common.log com ("Parsed " ^ file);
  182. data
  183. let parse_hook = ref parse_file
  184. let type_module_hook = ref (fun _ _ _ -> None)
  185. let type_function_params_rec = ref (fun _ _ _ _ -> assert false)
  186. let return_partial_type = ref false
  187. let type_function_param ctx t e opt p =
  188. if opt then
  189. let e = (match e with None -> Some (EConst (Ident "null"),p) | _ -> e) in
  190. ctx.t.tnull t, e
  191. else
  192. t, e
  193. let type_var_field ctx t e stat p =
  194. if stat then ctx.curfun <- FunStatic;
  195. let e = type_expr ctx e (WithType t) in
  196. unify ctx e.etype t p;
  197. match t with
  198. | TType ({ t_path = ([],"UInt") },[]) | TAbstract ({ a_path = ([],"UInt") },[]) when stat -> { e with etype = t }
  199. | _ -> e
  200. let apply_macro ctx mode path el p =
  201. let cpath, meth = (match List.rev (ExtString.String.nsplit path ".") with
  202. | meth :: name :: pack -> (List.rev pack,name), meth
  203. | _ -> error "Invalid macro path" p
  204. ) in
  205. ctx.g.do_macro ctx mode cpath meth el p
  206. (** since load_type_def and load_instance are used in PASS2, they should not access the structure of a type **)
  207. (*
  208. load a type or a subtype definition
  209. *)
  210. let rec load_type_def ctx p t =
  211. let no_pack = t.tpackage = [] in
  212. let tname = (match t.tsub with None -> t.tname | Some n -> n) in
  213. try
  214. if t.tsub <> None then raise Not_found;
  215. List.find (fun t2 ->
  216. let tp = t_path t2 in
  217. tp = (t.tpackage,tname) || (no_pack && snd tp = tname)
  218. ) (ctx.m.curmod.m_types @ ctx.m.module_types)
  219. with
  220. Not_found ->
  221. let next() =
  222. let t, m = (try
  223. t, ctx.g.do_load_module ctx (t.tpackage,t.tname) p
  224. with Error (Module_not_found _,p2) as e when p == p2 ->
  225. match t.tpackage with
  226. | "std" :: l ->
  227. let t = { t with tpackage = l } in
  228. t, ctx.g.do_load_module ctx (t.tpackage,t.tname) p
  229. | _ -> raise e
  230. ) in
  231. let tpath = (t.tpackage,tname) in
  232. try
  233. List.find (fun t -> not (t_infos t).mt_private && t_path t = tpath) m.m_types
  234. with
  235. Not_found -> raise (Error (Type_not_found (m.m_path,tname),p))
  236. in
  237. (* lookup in wildcard imported packages *)
  238. try
  239. if not no_pack then raise Exit;
  240. let rec loop = function
  241. | [] -> raise Exit
  242. | wp :: l ->
  243. try
  244. load_type_def ctx p { t with tpackage = wp }
  245. with
  246. | Error (Module_not_found _,p2)
  247. | Error (Type_not_found _,p2) when p == p2 -> loop l
  248. in
  249. loop ctx.m.wildcard_packages
  250. with Exit ->
  251. (* lookup in our own package - and its upper packages *)
  252. let rec loop = function
  253. | [] -> raise Exit
  254. | (_ :: lnext) as l ->
  255. try
  256. load_type_def ctx p { t with tpackage = List.rev l }
  257. with
  258. | Error (Module_not_found _,p2)
  259. | Error (Type_not_found _,p2) when p == p2 -> loop lnext
  260. in
  261. try
  262. if not no_pack then raise Exit;
  263. (match fst ctx.m.curmod.m_path with
  264. | [] -> raise Exit
  265. | x :: _ ->
  266. (* this can occur due to haxe remoting : a module can be
  267. already defined in the "js" package and is not allowed
  268. to access the js classes *)
  269. try
  270. (match PMap.find x ctx.com.package_rules with
  271. | Forbidden -> raise Exit
  272. | _ -> ())
  273. with Not_found -> ());
  274. loop (List.rev (fst ctx.m.curmod.m_path));
  275. with
  276. Exit -> next()
  277. let check_param_constraints ctx types t pl c p =
  278. match follow t with
  279. | TMono _ -> ()
  280. | _ ->
  281. let ctl = (match c.cl_kind with KTypeParameter l -> l | _ -> []) in
  282. List.iter (fun ti ->
  283. let ti = apply_params types pl ti in
  284. let ti = (match follow ti with
  285. | TInst ({ cl_kind = KGeneric }as c,pl) ->
  286. (* if we solve a generic contraint, let's substitute with the actual generic instance before unifying *)
  287. let _,_, f = ctx.g.do_build_instance ctx (TClassDecl c) p in
  288. f pl
  289. | _ -> ti
  290. ) in
  291. unify ctx t ti p
  292. ) ctl
  293. (* build an instance from a full type *)
  294. let rec load_instance ctx t p allow_no_params =
  295. try
  296. if t.tpackage <> [] || t.tsub <> None then raise Not_found;
  297. let pt = List.assoc t.tname ctx.type_params in
  298. if t.tparams <> [] then error ("Class type parameter " ^ t.tname ^ " can't have parameters") p;
  299. pt
  300. with Not_found ->
  301. let mt = load_type_def ctx p t in
  302. let is_generic = match mt with TClassDecl {cl_kind = KGeneric} -> true | _ -> false in
  303. let types , path , f = ctx.g.do_build_instance ctx mt p in
  304. if allow_no_params && t.tparams = [] then begin
  305. let pl = ref [] in
  306. pl := List.map (fun (name,t) ->
  307. match follow t with
  308. | TInst (c,_) ->
  309. let t = mk_mono() in
  310. if c.cl_kind <> KTypeParameter [] || is_generic then delay ctx PCheckConstraint (fun() -> check_param_constraints ctx types t (!pl) c p);
  311. t;
  312. | _ -> assert false
  313. ) types;
  314. f (!pl)
  315. end else if path = ([],"Dynamic") then
  316. match t.tparams with
  317. | [] -> t_dynamic
  318. | [TPType t] -> TDynamic (load_complex_type ctx p t)
  319. | _ -> error "Too many parameters for Dynamic" p
  320. else begin
  321. if List.length types <> List.length t.tparams then error ("Invalid number of type parameters for " ^ s_type_path path) p;
  322. let tparams = List.map (fun t ->
  323. match t with
  324. | TPExpr e ->
  325. let name = (match fst e with
  326. | EConst (String s) -> "S" ^ s
  327. | EConst (Int i) -> "I" ^ i
  328. | EConst (Float f) -> "F" ^ f
  329. | _ -> "Expr"
  330. ) in
  331. let c = mk_class null_module ([],name) p in
  332. c.cl_kind <- KExpr e;
  333. TInst (c,[])
  334. | TPType t -> load_complex_type ctx p t
  335. ) t.tparams in
  336. let params = List.map2 (fun t (name,t2) ->
  337. let isconst = (match t with TInst ({ cl_kind = KExpr _ },_) -> true | _ -> false) in
  338. if isconst <> (name = "Const") && t != t_dynamic then error (if isconst then "Constant value unexpected here" else "Constant value excepted as type parameter") p;
  339. match follow t2 with
  340. | TInst ({ cl_kind = KTypeParameter [] }, []) when not is_generic ->
  341. t
  342. | TInst (c,[]) ->
  343. let r = exc_protect ctx (fun r ->
  344. r := (fun() -> t);
  345. delay ctx PCheckConstraint (fun() -> check_param_constraints ctx types t tparams c p);
  346. t
  347. ) "constraint" in
  348. delay ctx PForce (fun () -> ignore(!r()));
  349. TLazy r
  350. | _ -> assert false
  351. ) tparams types in
  352. f params
  353. end
  354. (*
  355. build an instance from a complex type
  356. *)
  357. and load_complex_type ctx p t =
  358. match t with
  359. | CTParent t -> load_complex_type ctx p t
  360. | CTPath t -> load_instance ctx t p false
  361. | CTOptional _ -> error "Optional type not allowed here" p
  362. | CTExtend (t,l) ->
  363. (match load_complex_type ctx p (CTAnonymous l) with
  364. | TAnon a ->
  365. let rec loop t =
  366. match follow t with
  367. | TInst (c,tl) ->
  368. let c2 = mk_class null_module (fst c.cl_path,"+" ^ snd c.cl_path) p in
  369. c2.cl_private <- true;
  370. PMap.iter (fun f _ ->
  371. try
  372. ignore(class_field c f);
  373. error ("Cannot redefine field " ^ f) p
  374. with
  375. Not_found -> ()
  376. ) a.a_fields;
  377. (* do NOT tag as extern - for protect *)
  378. c2.cl_kind <- KExtension (c,tl);
  379. c2.cl_super <- Some (c,tl);
  380. c2.cl_fields <- a.a_fields;
  381. TInst (c2,[])
  382. | TMono _ ->
  383. error "Please ensure correct initialization of cascading signatures" p
  384. | TAnon a2 ->
  385. PMap.iter (fun f _ ->
  386. if PMap.mem f a2.a_fields then error ("Cannot redefine field " ^ f) p
  387. ) a.a_fields;
  388. mk_anon (PMap.foldi PMap.add a.a_fields a2.a_fields)
  389. | _ -> error "Can only extend classes and structures" p
  390. in
  391. let i = load_instance ctx t p false in
  392. let tr = ref None in
  393. let t = TMono tr in
  394. let r = exc_protect ctx (fun r ->
  395. r := (fun _ -> t);
  396. tr := Some (loop i);
  397. t
  398. ) "constraint" in
  399. delay ctx PForce (fun () -> ignore(!r()));
  400. TLazy r
  401. | _ -> assert false)
  402. | CTAnonymous l ->
  403. let rec loop acc f =
  404. let n = f.cff_name in
  405. let p = f.cff_pos in
  406. if PMap.mem n acc then error ("Duplicate field declaration : " ^ n) p;
  407. let topt = function
  408. | None -> error ("Explicit type required for field " ^ n) p
  409. | Some t -> load_complex_type ctx p t
  410. in
  411. let no_expr = function
  412. | None -> ()
  413. | Some (_,p) -> error "Expression not allowed here" p
  414. in
  415. let pub = ref true in
  416. let dyn = ref false in
  417. let params = ref [] in
  418. List.iter (fun a ->
  419. match a with
  420. | APublic -> ()
  421. | APrivate -> pub := false;
  422. | ADynamic when (match f.cff_kind with FFun _ -> true | _ -> false) -> dyn := true
  423. | AStatic | AOverride | AInline | ADynamic | AMacro -> error ("Invalid access " ^ Ast.s_access a) p
  424. ) f.cff_access;
  425. let t , access = (match f.cff_kind with
  426. | FVar (Some (CTPath({tpackage=[];tname="Void"})), _) | FProp (_,_,Some (CTPath({tpackage=[];tname="Void"})),_) ->
  427. error "Fields of type Void are not allowed in structures" p
  428. | FVar (t, e) ->
  429. no_expr e;
  430. topt t, Var { v_read = AccNormal; v_write = AccNormal }
  431. | FFun fd ->
  432. params := (!type_function_params_rec) ctx fd f.cff_name p;
  433. no_expr fd.f_expr;
  434. let old = ctx.type_params in
  435. ctx.type_params <- !params @ old;
  436. let args = List.map (fun (name,o,t,e) -> no_expr e; name, o, topt t) fd.f_args in
  437. let t = TFun (args,topt fd.f_type), Method (if !dyn then MethDynamic else MethNormal) in
  438. ctx.type_params <- old;
  439. t
  440. | FProp (i1,i2,t,e) ->
  441. no_expr e;
  442. let access m get =
  443. match m with
  444. | "null" -> AccNo
  445. | "never" -> AccNever
  446. | "default" -> AccNormal
  447. | "dynamic" -> AccCall ((if get then "get_" else "set_") ^ n)
  448. | "get" when get -> AccCall ("get_" ^ n)
  449. | "set" when not get -> AccCall ("set_" ^ n)
  450. | x when get && x = "get_" ^ n -> AccCall x
  451. | x when not get && x = "set_" ^ n -> AccCall x
  452. | _ ->
  453. error "Custom property access is no longer supported in Haxe 3" f.cff_pos;
  454. in
  455. let t = (match t with None -> error "Type required for structure property" p | Some t -> t) in
  456. load_complex_type ctx p t, Var { v_read = access i1 true; v_write = access i2 false }
  457. ) in
  458. let cf = {
  459. cf_name = n;
  460. cf_type = t;
  461. cf_pos = p;
  462. cf_public = !pub;
  463. cf_kind = access;
  464. cf_params = !params;
  465. cf_expr = None;
  466. cf_doc = f.cff_doc;
  467. cf_meta = f.cff_meta;
  468. cf_overloads = [];
  469. } in
  470. init_meta_overloads ctx cf;
  471. PMap.add n cf acc
  472. in
  473. mk_anon (List.fold_left loop PMap.empty l)
  474. | CTFunction (args,r) ->
  475. match args with
  476. | [CTPath { tpackage = []; tparams = []; tname = "Void" }] ->
  477. TFun ([],load_complex_type ctx p r)
  478. | _ ->
  479. TFun (List.map (fun t ->
  480. let t, opt = (match t with CTOptional t -> t, true | _ -> t,false) in
  481. "",opt,load_complex_type ctx p t
  482. ) args,load_complex_type ctx p r)
  483. and init_meta_overloads ctx cf =
  484. let overloads = ref [] in
  485. cf.cf_meta <- List.filter (fun m ->
  486. match m with
  487. | (Meta.Overload,[(EFunction (fname,f),p)],_) ->
  488. if fname <> None then error "Function name must not be part of @:overload" p;
  489. (match f.f_expr with Some (EBlock [], _) -> () | _ -> error "Overload must only declare an empty method body {}" p);
  490. let old = ctx.type_params in
  491. (match cf.cf_params with
  492. | [] -> ()
  493. | l -> ctx.type_params <- List.filter (fun t -> not (List.mem t l)) ctx.type_params);
  494. let params = (!type_function_params_rec) ctx f cf.cf_name p in
  495. ctx.type_params <- params @ ctx.type_params;
  496. let topt = function None -> error "Explicit type required" p | Some t -> load_complex_type ctx p t in
  497. let args = List.map (fun (a,opt,t,_) -> a,opt,topt t) f.f_args in
  498. overloads := (args,topt f.f_type, params) :: !overloads;
  499. ctx.type_params <- old;
  500. false
  501. | _ ->
  502. true
  503. ) cf.cf_meta;
  504. cf.cf_overloads <- List.map (fun (args,ret,params) -> { cf with cf_type = TFun (args,ret); cf_params = params }) (List.rev !overloads)
  505. let hide_types ctx =
  506. let old_m = ctx.m in
  507. let old_type_params = ctx.type_params in
  508. ctx.m <- {
  509. curmod = ctx.g.std;
  510. module_types = [];
  511. module_using = [];
  512. module_globals = PMap.empty;
  513. wildcard_packages = [];
  514. };
  515. ctx.type_params <- [];
  516. (fun() ->
  517. ctx.m <- old_m;
  518. ctx.type_params <- old_type_params;
  519. )
  520. (*
  521. load a type while ignoring the current imports or local types
  522. *)
  523. let load_core_type ctx name =
  524. let show = hide_types ctx in
  525. let t = load_instance ctx { tpackage = []; tname = name; tparams = []; tsub = None; } null_pos false in
  526. show();
  527. t
  528. let t_iterator ctx =
  529. let show = hide_types ctx in
  530. match load_type_def ctx null_pos { tpackage = []; tname = "Iterator"; tparams = []; tsub = None } with
  531. | TTypeDecl t ->
  532. show();
  533. if List.length t.t_types <> 1 then assert false;
  534. let pt = mk_mono() in
  535. apply_params t.t_types [pt] t.t_type, pt
  536. | _ ->
  537. assert false
  538. (*
  539. load either a type t or Null<Unknown> if not defined
  540. *)
  541. let load_type_opt ?(opt=false) ctx p t =
  542. let t = (match t with None -> mk_mono() | Some t -> load_complex_type ctx p t) in
  543. if opt then ctx.t.tnull t else t
  544. (* ---------------------------------------------------------------------- *)
  545. (* Structure check *)
  546. let valid_redefinition ctx f1 t1 f2 t2 =
  547. let valid t1 t2 =
  548. Type.unify t1 t2;
  549. if is_null t1 <> is_null t2 then raise (Unify_error [Cannot_unify (t1,t2)]);
  550. in
  551. let t1, t2 = (match f1.cf_params, f2.cf_params with
  552. | [], [] -> t1, t2
  553. | l1, l2 when List.length l1 = List.length l2 ->
  554. let to_check = ref [] in
  555. let monos = List.map2 (fun (name,p1) (_,p2) ->
  556. (match follow p1, follow p2 with
  557. | TInst ({ cl_kind = KTypeParameter ct1 } as c1,pl1), TInst ({ cl_kind = KTypeParameter ct2 } as c2,pl2) ->
  558. (match ct1, ct2 with
  559. | [], [] -> ()
  560. | _, _ when List.length ct1 = List.length ct2 ->
  561. (* if same constraints, they are the same type *)
  562. let check monos =
  563. List.iter2 (fun t1 t2 ->
  564. try
  565. let t1 = apply_params l1 monos (apply_params c1.cl_types pl1 t1) in
  566. let t2 = apply_params l2 monos (apply_params c2.cl_types pl2 t2) in
  567. type_eq EqStrict t1 t2
  568. with Unify_error l ->
  569. raise (Unify_error (Unify_custom "Constraints differ" :: l))
  570. ) ct1 ct2
  571. in
  572. to_check := check :: !to_check;
  573. | _ ->
  574. raise (Unify_error [Unify_custom "Different number of constraints"]))
  575. | _ -> ());
  576. TInst (mk_class null_module ([],name) Ast.null_pos,[])
  577. ) l1 l2 in
  578. List.iter (fun f -> f monos) !to_check;
  579. apply_params l1 monos t1, apply_params l2 monos t2
  580. | _ ->
  581. (* ignore type params, will create other errors later *)
  582. t1, t2
  583. ) in
  584. match follow t1, follow t2 with
  585. | TFun (args1,r1) , TFun (args2,r2) when List.length args1 = List.length args2 -> (try
  586. List.iter2 (fun (n,o1,a1) (_,o2,a2) ->
  587. if o1 <> o2 then raise (Unify_error [Not_matching_optional n]);
  588. (try valid a2 a1 with Unify_error _ -> raise (Unify_error [Cannot_unify(a1,a2)]))
  589. ) args1 args2;
  590. valid r1 r2
  591. with Unify_error l ->
  592. raise (Unify_error (Cannot_unify (t1,t2) :: l)))
  593. | _ , _ ->
  594. (* in case args differs, or if an interface var *)
  595. type_eq EqStrict t1 t2;
  596. if is_null t1 <> is_null t2 then raise (Unify_error [Cannot_unify (t1,t2)])
  597. let copy_meta meta_src meta_target sl =
  598. let meta = ref meta_target in
  599. List.iter (fun (m,e,p) ->
  600. if List.mem m sl then meta := (m,e,p) :: !meta
  601. ) meta_src;
  602. !meta
  603. let check_overriding ctx c =
  604. let p = c.cl_pos in
  605. match c.cl_super with
  606. | None ->
  607. (match c.cl_overrides with
  608. | [] -> ()
  609. | i :: _ ->
  610. display_error ctx ("Field " ^ i ^ " is declared 'override' but doesn't override any field") p)
  611. | Some (csup,params) ->
  612. PMap.iter (fun i f ->
  613. let p = f.cf_pos in
  614. try
  615. let _, t , f2 = raw_class_field (fun f -> f.cf_type) csup i in
  616. (* allow to define fields that are not defined for this platform version in superclass *)
  617. (match f2.cf_kind with
  618. | Var { v_read = AccRequire _ } -> raise Not_found;
  619. | _ -> ());
  620. if not (List.mem i c.cl_overrides) then
  621. display_error ctx ("Field " ^ i ^ " should be declared with 'override' since it is inherited from superclass") p
  622. else if not f.cf_public && f2.cf_public then
  623. display_error ctx ("Field " ^ i ^ " has less visibility (public/private) than superclass one") p
  624. else (match f.cf_kind, f2.cf_kind with
  625. | _, Method MethInline ->
  626. display_error ctx ("Field " ^ i ^ " is inlined and cannot be overridden") p
  627. | a, b when a = b -> ()
  628. | Method MethInline, Method MethNormal ->
  629. () (* allow to redefine a method as inlined *)
  630. | _ ->
  631. display_error ctx ("Field " ^ i ^ " has different property access than in superclass") p);
  632. try
  633. let t = apply_params csup.cl_types params t in
  634. valid_redefinition ctx f f.cf_type f2 t
  635. with
  636. Unify_error l ->
  637. display_error ctx ("Field " ^ i ^ " overloads parent class with different or incomplete type") p;
  638. display_error ctx (error_msg (Unify l)) p;
  639. with
  640. Not_found ->
  641. if List.mem i c.cl_overrides then display_error ctx ("Field " ^ i ^ " is declared 'override' but doesn't override any field") p
  642. ) c.cl_fields
  643. let class_field_no_interf c i =
  644. try
  645. let f = PMap.find i c.cl_fields in
  646. f.cf_type , f
  647. with Not_found ->
  648. match c.cl_super with
  649. | None ->
  650. raise Not_found
  651. | Some (c,tl) ->
  652. (* rec over class_field *)
  653. let _, t , f = raw_class_field (fun f -> f.cf_type) c i in
  654. apply_params c.cl_types tl t , f
  655. let rec check_interface ctx c intf params =
  656. let p = c.cl_pos in
  657. PMap.iter (fun i f ->
  658. try
  659. let t2, f2 = class_field_no_interf c i in
  660. ignore(follow f2.cf_type); (* force evaluation *)
  661. let p = (match f2.cf_expr with None -> p | Some e -> e.epos) in
  662. let mkind = function
  663. | MethNormal | MethInline -> 0
  664. | MethDynamic -> 1
  665. | MethMacro -> 2
  666. in
  667. if f.cf_public && not f2.cf_public then
  668. display_error ctx ("Field " ^ i ^ " should be public as requested by " ^ s_type_path intf.cl_path) p
  669. 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
  670. 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
  671. else try
  672. valid_redefinition ctx f2 t2 f (apply_params intf.cl_types params f.cf_type)
  673. with
  674. Unify_error l ->
  675. display_error ctx ("Field " ^ i ^ " has different type than in " ^ s_type_path intf.cl_path) p;
  676. display_error ctx (error_msg (Unify l)) p;
  677. with
  678. Not_found ->
  679. if not c.cl_interface then display_error ctx ("Field " ^ i ^ " needed by " ^ s_type_path intf.cl_path ^ " is missing") p
  680. ) intf.cl_fields;
  681. List.iter (fun (i2,p2) ->
  682. check_interface ctx c i2 (List.map (apply_params intf.cl_types params) p2)
  683. ) intf.cl_implements
  684. let check_interfaces ctx c =
  685. match c.cl_path with
  686. | "Proxy" :: _ , _ -> ()
  687. | _ ->
  688. List.iter (fun (intf,params) -> check_interface ctx c intf params) c.cl_implements
  689. let rec return_flow ctx e =
  690. let error() = display_error ctx "A return is missing here" e.epos; raise Exit in
  691. let return_flow = return_flow ctx in
  692. match e.eexpr with
  693. | TReturn _ | TThrow _ -> ()
  694. | TParenthesis e ->
  695. return_flow e
  696. | TBlock el ->
  697. let rec loop = function
  698. | [] -> error()
  699. | [e] -> return_flow e
  700. | { eexpr = TReturn _ } :: _ | { eexpr = TThrow _ } :: _ -> ()
  701. | _ :: l -> loop l
  702. in
  703. loop el
  704. | TIf (_,e1,Some e2) ->
  705. return_flow e1;
  706. return_flow e2;
  707. | TSwitch (v,cases,Some e) ->
  708. List.iter (fun (_,e) -> return_flow e) cases;
  709. return_flow e
  710. | TSwitch (e,cases,None) when (match follow e.etype with TEnum _ -> true | _ -> false) ->
  711. List.iter (fun (_,e) -> return_flow e) cases;
  712. | TMatch (_,_,cases,def) ->
  713. List.iter (fun (_,_,e) -> return_flow e) cases;
  714. (match def with None -> () | Some e -> return_flow e)
  715. | TTry (e,cases) ->
  716. return_flow e;
  717. List.iter (fun (_,e) -> return_flow e) cases;
  718. | TWhile({eexpr = (TConst (TBool true))},e,_) ->
  719. (* a special case for "inifite" while loops that have no break *)
  720. let rec loop e = match e.eexpr with
  721. (* ignore nested loops to not accidentally get one of its breaks *)
  722. | TWhile _ | TFor _ -> ()
  723. | TBreak -> error()
  724. | _ -> Type.iter loop e
  725. in
  726. loop e
  727. | _ ->
  728. error()
  729. (* ---------------------------------------------------------------------- *)
  730. (* PASS 1 & 2 : Module and Class Structure *)
  731. let set_heritance ctx c herits p =
  732. let ctx = { ctx with curclass = c; type_params = c.cl_types; } in
  733. let process_meta csup =
  734. List.iter (fun m ->
  735. match m with
  736. | Meta.Final, _, _ -> if not (Meta.has Meta.Hack c.cl_meta || (match c.cl_kind with KTypeParameter _ -> true | _ -> false)) then error "Cannot extend a final class" p;
  737. | Meta.AutoBuild, el, p -> c.cl_meta <- (Meta.Build,el,p) :: m :: c.cl_meta
  738. | _ -> ()
  739. ) csup.cl_meta
  740. in
  741. let has_interf = ref false in
  742. let rec loop = function
  743. | HPrivate | HExtern | HInterface ->
  744. ()
  745. | HExtends t ->
  746. if c.cl_super <> None then error "Cannot extend several classes" p;
  747. let t = load_instance ctx t p false in
  748. (match follow t with
  749. | TInst ({ cl_path = [],"Array" },_)
  750. | TInst ({ cl_path = [],"String" },_)
  751. | TInst ({ cl_path = [],"Date" },_)
  752. | TInst ({ cl_path = [],"Xml" },_) when ((not (platform ctx.com Cpp)) && (match c.cl_path with "mt" :: _ , _ -> false | _ -> true)) ->
  753. error "Cannot extend basic class" p;
  754. | TInst (csup,params) ->
  755. csup.cl_build();
  756. if is_parent c csup then error "Recursive class" p;
  757. process_meta csup;
  758. (* interface extends are listed in cl_implements ! *)
  759. if c.cl_interface then begin
  760. if not csup.cl_interface then error "Cannot extend by using a class" p;
  761. c.cl_implements <- (csup,params) :: c.cl_implements
  762. end else begin
  763. if csup.cl_interface then error "Cannot extend by using an interface" p;
  764. c.cl_super <- Some (csup,params)
  765. end
  766. | _ -> error "Should extend by using a class" p)
  767. | HImplements t ->
  768. let t = load_instance ctx t p false in
  769. (match follow t with
  770. | TInst ({ cl_path = [],"ArrayAccess"; cl_extern = true; },[t]) ->
  771. if c.cl_array_access <> None then error "Duplicate array access" p;
  772. c.cl_array_access <- Some t
  773. | TInst (intf,params) ->
  774. intf.cl_build();
  775. if is_parent c intf then error "Recursive class" p;
  776. if c.cl_interface then error "Interfaces cannot implements another interface (use extends instead)" p;
  777. if not intf.cl_interface then error "You can only implements an interface" p;
  778. process_meta intf;
  779. c.cl_implements <- (intf, params) :: c.cl_implements;
  780. if not !has_interf then begin
  781. delay ctx PForce (fun() -> check_interfaces ctx c);
  782. has_interf := true;
  783. end
  784. | TDynamic t ->
  785. if c.cl_dynamic <> None then error "Cannot have several dynamics" p;
  786. c.cl_dynamic <- Some t
  787. | _ -> error "Should implement by using an interface" p)
  788. in
  789. (*
  790. resolve imports before calling build_inheritance, since it requires full paths.
  791. that means that typedefs are not working, but that's a fair limitation
  792. *)
  793. let rec resolve_imports t =
  794. match t.tpackage with
  795. | _ :: _ -> t
  796. | [] ->
  797. try
  798. let find = List.find (fun lt -> snd (t_path lt) = t.tname) in
  799. let lt = try find ctx.m.curmod.m_types with Not_found -> find ctx.m.module_types in
  800. { t with tpackage = fst (t_path lt) }
  801. with
  802. Not_found -> t
  803. in
  804. let herits = List.map (function
  805. | HExtends t -> HExtends (resolve_imports t)
  806. | HImplements t -> HImplements (resolve_imports t)
  807. | h -> h
  808. ) herits in
  809. List.iter loop (List.filter (ctx.g.do_inherit ctx c p) herits)
  810. let rec type_type_params ctx path get_params p tp =
  811. let n = tp.tp_name in
  812. let c = mk_class ctx.m.curmod (fst path @ [snd path],n) p in
  813. c.cl_types <- List.map (type_type_params ctx c.cl_path get_params p) tp.tp_params;
  814. let t = TInst (c,List.map snd c.cl_types) in
  815. match tp.tp_constraints with
  816. | [] ->
  817. c.cl_kind <- KTypeParameter [];
  818. n, t
  819. | _ ->
  820. let r = exc_protect ctx (fun r ->
  821. r := (fun _ -> error "Recursive constraint parameter is now allowed" p);
  822. let ctx = { ctx with type_params = ctx.type_params @ get_params() } in
  823. let constr = List.map (load_complex_type ctx p) tp.tp_constraints in
  824. List.iter (fun t -> ignore(follow t)) constr; (* force other constraints evaluation to check recursion *)
  825. c.cl_kind <- KTypeParameter constr;
  826. r := (fun _ -> t);
  827. t
  828. ) "constraint" in
  829. delay ctx PForce (fun () -> ignore(!r()));
  830. n, TLazy r
  831. let type_function_params ctx fd fname p =
  832. let params = ref [] in
  833. params := List.map (fun tp ->
  834. type_type_params ctx ([],fname) (fun() -> !params) p tp
  835. ) fd.f_params;
  836. !params
  837. let type_function ctx args ret fmode f p =
  838. let locals = save_locals ctx in
  839. let fargs = List.map (fun (n,c,t) ->
  840. let c = (match c with
  841. | None -> None
  842. | Some e ->
  843. let p = pos e in
  844. let e = ctx.g.do_optimize ctx (type_expr ctx e (WithType t)) in
  845. unify ctx e.etype t p;
  846. match e.eexpr with
  847. | TConst c -> Some c
  848. | _ -> display_error ctx "Parameter default value should be constant" p; None
  849. ) in
  850. add_local ctx n t, c
  851. ) args in
  852. let old_ret = ctx.ret in
  853. let old_fun = ctx.curfun in
  854. let old_opened = ctx.opened in
  855. ctx.curfun <- fmode;
  856. ctx.ret <- ret;
  857. ctx.opened <- [];
  858. let e = type_expr ctx (match f.f_expr with None -> error "Function body required" p | Some e -> e) NoValue in
  859. let rec loop e =
  860. match e.eexpr with
  861. | TReturn (Some _) -> raise Exit
  862. | TFunction _ -> ()
  863. | _ -> Type.iter loop e
  864. in
  865. let have_ret = (try loop e; false with Exit -> true) in
  866. if have_ret then
  867. (try return_flow ctx e with Exit -> ())
  868. else (try type_eq EqStrict ret ctx.t.tvoid with Unify_error _ -> display_error ctx ("Missing return " ^ (s_type (print_context()) ret)) p);
  869. let rec loop e =
  870. match e.eexpr with
  871. | TCall ({ eexpr = TConst TSuper },_) -> raise Exit
  872. | TFunction _ -> ()
  873. | _ -> Type.iter loop e
  874. in
  875. let has_super_constr() =
  876. match ctx.curclass.cl_super with
  877. | None -> false
  878. | Some (csup,_) ->
  879. try ignore(get_constructor (fun f->f.cf_type) csup); true with Not_found -> false
  880. in
  881. if fmode = FunConstructor && has_super_constr() then
  882. (try
  883. loop e;
  884. display_error ctx "Missing super constructor call" p
  885. with
  886. Exit -> ());
  887. locals();
  888. let e = match ctx.curfun, ctx.vthis with
  889. | (FunMember|FunConstructor), Some v ->
  890. let ev = mk (TVars [v,Some (mk (TConst TThis) ctx.tthis p)]) ctx.t.tvoid p in
  891. (match e.eexpr with
  892. | TBlock l -> { e with eexpr = TBlock (ev::l) }
  893. | _ -> mk (TBlock [ev;e]) e.etype p)
  894. | _ -> e
  895. in
  896. List.iter (fun r -> r := Closed) ctx.opened;
  897. ctx.ret <- old_ret;
  898. ctx.curfun <- old_fun;
  899. ctx.opened <- old_opened;
  900. e , fargs
  901. let init_core_api ctx c =
  902. let ctx2 = (match ctx.g.core_api with
  903. | None ->
  904. let com2 = Common.clone ctx.com in
  905. com2.defines <- PMap.empty;
  906. Common.define com2 Define.CoreApi;
  907. Common.define com2 Define.Sys;
  908. if ctx.in_macro then Common.define com2 Define.Macro;
  909. com2.class_path <- ctx.com.std_path;
  910. let ctx2 = ctx.g.do_create com2 in
  911. ctx.g.core_api <- Some ctx2;
  912. ctx2
  913. | Some c ->
  914. c
  915. ) in
  916. let t = load_instance ctx2 { tpackage = fst c.cl_path; tname = snd c.cl_path; tparams = []; tsub = None; } c.cl_pos true in
  917. flush_pass ctx2 PFinal "core_final";
  918. match t with
  919. | TInst (ccore,_) ->
  920. (match c.cl_doc with
  921. | None -> c.cl_doc <- ccore.cl_doc
  922. | Some _ -> ());
  923. let compare_fields f f2 =
  924. let p = (match f2.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  925. (try
  926. type_eq EqCoreType (apply_params ccore.cl_types (List.map snd c.cl_types) f.cf_type) f2.cf_type
  927. with Unify_error l ->
  928. display_error ctx ("Field " ^ f.cf_name ^ " has different type than in core type") p;
  929. display_error ctx (error_msg (Unify l)) p);
  930. if f2.cf_public <> f.cf_public then error ("Field " ^ f.cf_name ^ " has different visibility than core type") p;
  931. (match f2.cf_doc with
  932. | None -> f2.cf_doc <- f.cf_doc
  933. | Some _ -> ());
  934. if f2.cf_kind <> f.cf_kind then begin
  935. match f2.cf_kind, f.cf_kind with
  936. | Method MethInline, Method MethNormal -> () (* allow to add 'inline' *)
  937. | Method MethNormal, Method MethInline -> () (* allow to disable 'inline' *)
  938. | _ ->
  939. error ("Field " ^ f.cf_name ^ " has different property access than core type") p;
  940. end;
  941. (match follow f.cf_type, follow f2.cf_type with
  942. | TFun (pl1,_), TFun (pl2,_) ->
  943. if List.length pl1 != List.length pl2 then error "Argument count mismatch" p;
  944. List.iter2 (fun (n1,_,_) (n2,_,_) ->
  945. if n1 <> n2 then error ("Method parameter name '" ^ n2 ^ "' should be '" ^ n1 ^ "'") p;
  946. ) pl1 pl2;
  947. | _ -> ());
  948. in
  949. let check_fields fcore fl =
  950. PMap.iter (fun i f ->
  951. if not f.cf_public then () else
  952. let f2 = try PMap.find f.cf_name fl with Not_found -> error ("Missing field " ^ i ^ " required by core type") c.cl_pos in
  953. compare_fields f f2;
  954. ) fcore;
  955. PMap.iter (fun i f ->
  956. let p = (match f.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  957. if f.cf_public && not (Meta.has Meta.Hack f.cf_meta) && not (PMap.mem f.cf_name fcore) && not (List.mem f.cf_name c.cl_overrides) then error ("Public field " ^ i ^ " is not part of core type") p;
  958. ) fl;
  959. in
  960. check_fields ccore.cl_fields c.cl_fields;
  961. check_fields ccore.cl_statics c.cl_statics;
  962. (match ccore.cl_constructor, c.cl_constructor with
  963. | None, None -> ()
  964. | Some { cf_public = false }, _ -> ()
  965. | Some f, Some f2 -> compare_fields f f2
  966. | None, Some { cf_public = false } -> ()
  967. | _ -> error "Constructor differs from core type" c.cl_pos)
  968. | _ -> assert false
  969. let patch_class ctx c fields =
  970. let h = (try Some (Hashtbl.find ctx.g.type_patches c.cl_path) with Not_found -> None) in
  971. match h with
  972. | None -> fields
  973. | Some (h,hcl) ->
  974. c.cl_meta <- c.cl_meta @ hcl.tp_meta;
  975. let rec loop acc = function
  976. | [] -> acc
  977. | f :: l ->
  978. (* patch arguments types *)
  979. (match f.cff_kind with
  980. | FFun ff ->
  981. let param ((n,opt,t,e) as p) =
  982. try
  983. let t2 = (try Hashtbl.find h (("$" ^ f.cff_name ^ "__" ^ n),false) with Not_found -> Hashtbl.find h (("$" ^ n),false)) in
  984. n, opt, t2.tp_type, e
  985. with Not_found ->
  986. p
  987. in
  988. f.cff_kind <- FFun { ff with f_args = List.map param ff.f_args }
  989. | _ -> ());
  990. (* other patches *)
  991. match (try Some (Hashtbl.find h (f.cff_name,List.mem AStatic f.cff_access)) with Not_found -> None) with
  992. | None -> loop (f :: acc) l
  993. | Some { tp_remove = true } -> loop acc l
  994. | Some p ->
  995. f.cff_meta <- f.cff_meta @ p.tp_meta;
  996. (match p.tp_type with
  997. | None -> ()
  998. | Some t ->
  999. f.cff_kind <- match f.cff_kind with
  1000. | FVar (_,e) -> FVar (Some t,e)
  1001. | FProp (get,set,_,eo) -> FProp (get,set,Some t,eo)
  1002. | FFun f -> FFun { f with f_type = Some t });
  1003. loop (f :: acc) l
  1004. in
  1005. List.rev (loop [] fields)
  1006. let rec string_list_of_expr_path (e,p) =
  1007. match e with
  1008. | EConst (Ident i) -> [i]
  1009. | EField (e,f) -> f :: string_list_of_expr_path e
  1010. | _ -> error "Invalid path" p
  1011. let build_module_def ctx mt meta fvars context_init fbuild =
  1012. let rec loop = function
  1013. | (Meta.Build,args,p) :: l ->
  1014. let epath, el = (match args with
  1015. | [ECall (epath,el),p] -> epath, el
  1016. | _ -> error "Invalid build parameters" p
  1017. ) in
  1018. let s = try String.concat "." (List.rev (string_list_of_expr_path epath)) with Error (_,p) -> error "Build call parameter must be a class path" p in
  1019. if ctx.in_macro then error "You cannot use @:build inside a macro : make sure that your enum is not used in macro" p;
  1020. let old = ctx.g.get_build_infos in
  1021. ctx.g.get_build_infos <- (fun() -> Some (mt, fvars()));
  1022. context_init();
  1023. let r = try apply_macro ctx MBuild s el p with e -> ctx.g.get_build_infos <- old; raise e in
  1024. ctx.g.get_build_infos <- old;
  1025. (match r with
  1026. | None -> error "Build failure" p
  1027. | Some e -> fbuild e; loop l)
  1028. | _ :: l -> loop l
  1029. | [] -> ()
  1030. in
  1031. (* let errors go through to prevent resume if build fails *)
  1032. loop meta
  1033. let init_class ctx c p context_init herits fields =
  1034. let ctx = {
  1035. ctx with
  1036. curclass = c;
  1037. type_params = c.cl_types;
  1038. pass = PBuildClass;
  1039. tthis = (match c.cl_kind with
  1040. | KAbstractImpl a ->
  1041. (match a.a_this with
  1042. | TMono r when !r = None -> TAbstract (a,List.map snd c.cl_types)
  1043. | t -> t)
  1044. | _ -> TInst (c,List.map snd c.cl_types));
  1045. on_error = (fun ctx msg ep ->
  1046. ctx.com.error msg ep;
  1047. (* macros expressions might reference other code, let's recall which class we are actually compiling *)
  1048. if ep.pfile <> c.cl_pos.pfile then ctx.com.error "Defined in this class" c.cl_pos
  1049. );
  1050. } in
  1051. incr stats.s_classes_built;
  1052. let fields = patch_class ctx c fields in
  1053. let fields = ref fields in
  1054. let get_fields() = !fields in
  1055. build_module_def ctx (TClassDecl c) c.cl_meta get_fields context_init (fun (e,p) ->
  1056. match e with
  1057. | EVars [_,Some (CTAnonymous f),None] ->
  1058. List.iter (fun f ->
  1059. if List.mem AMacro f.cff_access then
  1060. (match ctx.g.macros with
  1061. | Some (_,mctx) when Hashtbl.mem mctx.g.types_module c.cl_path ->
  1062. (* assume that if we had already a macro with the same name, it has not been changed during the @:build operation *)
  1063. if not (List.exists (fun f2 -> f2.cff_name = f.cff_name && List.mem AMacro f2.cff_access) (!fields)) then
  1064. error "Class build macro cannot return a macro function when the class has already been compiled into the macro context" p
  1065. | _ -> ())
  1066. ) f;
  1067. fields := f
  1068. | _ -> error "Class build macro must return a single variable with anonymous fields" p
  1069. );
  1070. let fields = !fields in
  1071. let core_api = Meta.has Meta.CoreApi c.cl_meta in
  1072. let is_class_macro = Meta.has Meta.Macro c.cl_meta in
  1073. if is_class_macro then display_error ctx "Macro classes are no longer allowed in haxe 3" p;
  1074. let fields, herits = if is_class_macro && not ctx.in_macro then begin
  1075. c.cl_extern <- true;
  1076. List.filter (fun f -> List.mem AStatic f.cff_access) fields, []
  1077. end else fields, herits in
  1078. if core_api && not ctx.com.display then delay ctx PForce (fun() -> init_core_api ctx c);
  1079. let rec extends_public c =
  1080. Meta.has Meta.PublicFields c.cl_meta ||
  1081. match c.cl_super with
  1082. | None -> false
  1083. | Some (c,_) -> extends_public c
  1084. in
  1085. let extends_public = extends_public c in
  1086. let is_public access parent =
  1087. if List.mem APrivate access then
  1088. false
  1089. else if List.mem APublic access then
  1090. true
  1091. else match parent with
  1092. | Some { cf_public = p } -> p
  1093. | _ -> c.cl_extern || c.cl_interface || extends_public
  1094. in
  1095. let rec get_parent c name =
  1096. match c.cl_super with
  1097. | None -> None
  1098. | Some (csup,_) ->
  1099. try
  1100. Some (PMap.find name csup.cl_fields)
  1101. with
  1102. Not_found -> get_parent csup name
  1103. in
  1104. let type_opt ctx p t =
  1105. match t with
  1106. | None when c.cl_extern || c.cl_interface ->
  1107. display_error ctx "Type required for extern classes and interfaces" p;
  1108. t_dynamic
  1109. | None when core_api ->
  1110. display_error ctx "Type required for core api classes" p;
  1111. t_dynamic
  1112. | _ ->
  1113. load_type_opt ctx p t
  1114. in
  1115. let rec has_field f = function
  1116. | None -> false
  1117. | Some (c,_) ->
  1118. PMap.exists f c.cl_fields || has_field f c.cl_super || List.exists (fun i -> has_field f (Some i)) c.cl_implements
  1119. in
  1120. (match c.cl_super with None -> () | Some _ -> delay ctx PForce (fun() -> check_overriding ctx c));
  1121. (* ----------------------- COMPLETION ----------------------------- *)
  1122. let display_file = if ctx.com.display then Common.unique_full_path p.pfile = (!Parser.resume_display).pfile else false in
  1123. let fields = if not display_file || Common.defined ctx.com Define.NoCOpt then fields else Optimizer.optimize_completion c fields in
  1124. let delayed_expr = ref [] in
  1125. let rec is_full_type t =
  1126. match t with
  1127. | TFun (args,ret) -> is_full_type ret && List.for_all (fun (_,_,t) -> is_full_type t) args
  1128. | TMono r -> (match !r with None -> false | Some t -> is_full_type t)
  1129. | TAbstract _ | TInst _ | TEnum _ | TLazy _ | TDynamic _ | TAnon _ | TType _ -> true
  1130. in
  1131. let bind_type ctx cf r p macro =
  1132. if ctx.com.display then begin
  1133. let cp = !Parser.resume_display in
  1134. if display_file && (cp.pmin = 0 || (p.pmin <= cp.pmin && p.pmax >= cp.pmax)) then begin
  1135. if macro && not ctx.in_macro then
  1136. (* force macro system loading of this class in order to get completion *)
  1137. delay ctx PTypeField (fun() -> ignore(ctx.g.do_macro ctx MExpr c.cl_path cf.cf_name [] p))
  1138. else begin
  1139. cf.cf_type <- TLazy r;
  1140. delayed_expr := (ctx,r) :: !delayed_expr;
  1141. end
  1142. end else begin
  1143. if not (is_full_type cf.cf_type) then cf.cf_type <- TLazy r;
  1144. end
  1145. end else if macro && not ctx.in_macro then
  1146. ()
  1147. else begin
  1148. cf.cf_type <- TLazy r;
  1149. delayed_expr := (ctx,r) :: !delayed_expr;
  1150. end
  1151. in
  1152. let bind_var ctx cf e stat inline =
  1153. let p = cf.cf_pos in
  1154. if not stat && has_field cf.cf_name c.cl_super then error ("Redefinition of variable " ^ cf.cf_name ^ " in subclass is not allowed") p;
  1155. let t = cf.cf_type in
  1156. match e with
  1157. | None -> ()
  1158. | Some e ->
  1159. let r = exc_protect ctx (fun r ->
  1160. if not !return_partial_type then begin
  1161. r := (fun() -> t);
  1162. context_init();
  1163. if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ cf.cf_name);
  1164. let e = type_var_field ctx t e stat p in
  1165. let e = (match cf.cf_kind with
  1166. | Var v when c.cl_extern || Meta.has Meta.Extern cf.cf_meta ->
  1167. if not stat then begin
  1168. display_error ctx "Extern non-static variables may not be initialized" p;
  1169. e
  1170. end else if v.v_read <> AccInline then begin
  1171. display_error ctx "Extern non-inline variables may not be initialized" p;
  1172. e
  1173. end else begin
  1174. match Optimizer.make_constant_expression ctx e with
  1175. | Some e -> e
  1176. | None -> display_error ctx "Extern variable initialization must be a constant value" p; e
  1177. end
  1178. | Var v when not stat || (v.v_read = AccInline) ->
  1179. let e = match Optimizer.make_constant_expression ctx e with Some e -> e | None -> display_error ctx "Variable initialization must be a constant value" p; e in
  1180. e
  1181. | _ ->
  1182. e
  1183. ) in
  1184. cf.cf_expr <- Some e;
  1185. cf.cf_type <- t;
  1186. end;
  1187. t
  1188. ) "bind_var" in
  1189. bind_type ctx cf r (snd e) false
  1190. in
  1191. (* ----------------------- FIELD INIT ----------------------------- *)
  1192. let loop_cf f =
  1193. let name = f.cff_name in
  1194. let p = f.cff_pos in
  1195. let stat = List.mem AStatic f.cff_access in
  1196. let extern = Meta.has Meta.Extern f.cff_meta || c.cl_extern in
  1197. let allow_inline() =
  1198. match c.cl_kind, f.cff_kind with
  1199. | KAbstractImpl _, _ -> true
  1200. |_, FFun _ -> ctx.g.doinline || extern
  1201. | _ -> true
  1202. in
  1203. let inline = List.mem AInline f.cff_access && allow_inline() in
  1204. let override = List.mem AOverride f.cff_access in
  1205. let is_macro = Meta.has Meta.Macro f.cff_meta in
  1206. if is_macro then ctx.com.warning "@:macro should now be 'macro' accessor'" p;
  1207. let is_macro = is_macro || List.mem AMacro f.cff_access in
  1208. List.iter (fun acc ->
  1209. match (acc, f.cff_kind) with
  1210. | APublic, _ | APrivate, _ | AStatic, _ -> ()
  1211. | ADynamic, FFun _ | AOverride, FFun _ | AMacro, FFun _ | AInline, FFun _ | AInline, FVar _ -> ()
  1212. | _, FVar _ -> error ("Invalid accessor '" ^ Ast.s_access acc ^ "' for variable " ^ name) p
  1213. | _, FProp _ -> error ("Invalid accessor '" ^ Ast.s_access acc ^ "' for property " ^ name) p
  1214. ) f.cff_access;
  1215. if override then (match c.cl_super with None -> error "Invalid override: class has no super class" p | _ -> ());
  1216. (* build the per-field context *)
  1217. let ctx = {
  1218. ctx with
  1219. pass = PBuildClass; (* will be set later to PTypeExpr *)
  1220. } in
  1221. match f.cff_kind with
  1222. | FVar (t,e) ->
  1223. if inline && not stat then error "Inline variable must be static" p;
  1224. if inline && e = None then error "Inline variable must be initialized" p;
  1225. let t = (match t with
  1226. | None when not stat && e = None ->
  1227. error ("Type required for member variable " ^ name) p;
  1228. | None ->
  1229. mk_mono()
  1230. | Some t ->
  1231. let old = ctx.type_params in
  1232. if stat then ctx.type_params <- [];
  1233. let t = load_complex_type ctx p t in
  1234. if stat then ctx.type_params <- old;
  1235. t
  1236. ) in
  1237. let cf = {
  1238. cf_name = name;
  1239. cf_doc = f.cff_doc;
  1240. cf_meta = f.cff_meta;
  1241. cf_type = t;
  1242. cf_pos = f.cff_pos;
  1243. cf_kind = Var (if inline then { v_read = AccInline ; v_write = AccNever } else { v_read = AccNormal; v_write = AccNormal });
  1244. cf_expr = None;
  1245. cf_public = is_public f.cff_access None;
  1246. cf_params = [];
  1247. cf_overloads = [];
  1248. } in
  1249. ctx.curfield <- cf;
  1250. bind_var ctx cf e stat inline;
  1251. f, false, cf
  1252. | FFun fd ->
  1253. let params = type_function_params ctx fd f.cff_name p in
  1254. if inline && c.cl_interface then error "You can't declare inline methods in interfaces" p;
  1255. let is_macro = is_macro || (is_class_macro && stat) in
  1256. let f, stat, fd = if not is_macro || stat then
  1257. f, stat, fd
  1258. else if ctx.in_macro then
  1259. (* non-static macros methods are turned into static when we are running the macro *)
  1260. { f with cff_access = AStatic :: f.cff_access }, true, fd
  1261. else
  1262. (* remove display of first argument which will contain the "this" expression *)
  1263. f, stat, { fd with f_args = match fd.f_args with [] -> [] | _ :: l -> l }
  1264. in
  1265. let fd = if not is_macro then
  1266. fd
  1267. else if ctx.in_macro then
  1268. let texpr = CTPath { tpackage = ["haxe";"macro"]; tname = "Expr"; tparams = []; tsub = None } in
  1269. {
  1270. f_params = fd.f_params;
  1271. f_type = (match fd.f_type with None -> Some texpr | t -> t);
  1272. f_args = List.map (fun (a,o,t,e) -> a,o,(match t with None -> Some texpr | _ -> t),e) fd.f_args;
  1273. f_expr = fd.f_expr;
  1274. }
  1275. else
  1276. let tdyn = Some (CTPath { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None }) in
  1277. let to_dyn = function
  1278. | { tpackage = ["haxe";"macro"]; tname = "Expr"; tsub = Some ("ExprRequire"|"ExprOf"); tparams = [TPType t] } -> Some t
  1279. | { tpackage = []; tname = ("ExprRequire"|"ExprOf"); tsub = None; tparams = [TPType t] } -> Some t
  1280. | { tpackage = ["haxe"]; tname = ("PosInfos"); tsub = None; tparams = [] } -> error "haxe.PosInfos is not allowed on macro functions, use Context.currentPos() instead" p
  1281. | _ -> tdyn
  1282. in
  1283. {
  1284. f_params = fd.f_params;
  1285. f_type = (match fd.f_type with Some (CTPath t) -> to_dyn t | _ -> tdyn);
  1286. f_args = List.map (fun (a,o,t,_) -> a,o,(match t with Some (CTPath t) -> to_dyn t | _ -> tdyn),None) fd.f_args;
  1287. f_expr = None;
  1288. }
  1289. in
  1290. let parent = (if not stat then get_parent c name else None) in
  1291. let dynamic = List.mem ADynamic f.cff_access || (match parent with Some { cf_kind = Method MethDynamic } -> true | _ -> false) in
  1292. if inline && dynamic then error "You can't have both 'inline' and 'dynamic'" p;
  1293. ctx.type_params <- (match c.cl_kind with
  1294. | KAbstractImpl a ->
  1295. params @ a.a_types
  1296. | _ ->
  1297. if stat then params else params @ ctx.type_params);
  1298. let constr = (name = "new") in
  1299. let ret = if constr then ctx.t.tvoid else type_opt ctx p fd.f_type in
  1300. let args = List.map (fun (name,opt,t,c) ->
  1301. let t, c = type_function_param ctx (type_opt ctx p t) c opt p in
  1302. name, c, t
  1303. ) fd.f_args in
  1304. let t = TFun (fun_args args,ret) in
  1305. if constr && c.cl_interface then error "An interface cannot have a constructor" p;
  1306. if c.cl_interface && not stat && fd.f_expr <> None then error "An interface method cannot have a body" p;
  1307. if constr then (match fd.f_type with
  1308. | None | Some (CTPath { tpackage = []; tname = "Void" }) -> ()
  1309. | _ -> error "A class constructor can't have a return value" p
  1310. );
  1311. let cf = {
  1312. cf_name = name;
  1313. cf_doc = f.cff_doc;
  1314. cf_meta = f.cff_meta;
  1315. cf_type = t;
  1316. cf_pos = f.cff_pos;
  1317. cf_kind = Method (if is_macro then MethMacro else if inline then MethInline else if dynamic then MethDynamic else MethNormal);
  1318. cf_expr = None;
  1319. cf_public = is_public f.cff_access parent;
  1320. cf_params = params;
  1321. cf_overloads = [];
  1322. } in
  1323. let do_bind = ref (not (cf.cf_name <> "__init__" && (c.cl_extern && not inline) || c.cl_interface)) in
  1324. (match c.cl_kind with
  1325. | KAbstractImpl a ->
  1326. let m = mk_mono() in
  1327. if Meta.has Meta.From f.cff_meta then begin
  1328. let ta = TAbstract(a, List.map (fun _ -> mk_mono()) a.a_types) in
  1329. unify ctx t (tfun [m] ta) f.cff_pos;
  1330. a.a_from <- (follow m, Some cf) :: a.a_from
  1331. end else if Meta.has Meta.To f.cff_meta then begin
  1332. let ta = monomorphs a.a_types (monomorphs params a.a_this) in
  1333. unify ctx t (tfun [ta] m) f.cff_pos;
  1334. if not (Meta.has Meta.Impl cf.cf_meta) then cf.cf_meta <- (Meta.Impl,[],cf.cf_pos) :: cf.cf_meta;
  1335. a.a_to <- (follow m, Some cf) :: a.a_to
  1336. end else if Meta.has Meta.ArrayAccess f.cff_meta then begin
  1337. a.a_array <- cf :: a.a_array;
  1338. end else if f.cff_name = "_new" && Meta.has Meta.MultiType a.a_meta then
  1339. do_bind := false
  1340. else (try match Meta.get Meta.Op cf.cf_meta with
  1341. | _,[EBinop(op,_,_),_],_ ->
  1342. a.a_ops <- (op,cf) :: a.a_ops;
  1343. if fd.f_expr = None then do_bind := false;
  1344. | _ -> ()
  1345. with Not_found -> ())
  1346. | _ ->
  1347. ());
  1348. init_meta_overloads ctx cf;
  1349. ctx.curfield <- cf;
  1350. let r = exc_protect ctx (fun r ->
  1351. if not !return_partial_type then begin
  1352. r := (fun() -> t);
  1353. context_init();
  1354. incr stats.s_methods_typed;
  1355. if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ name);
  1356. let fmode = (match c.cl_kind with
  1357. | KAbstractImpl _ ->
  1358. (match args with
  1359. | ("this",_,_) :: _ -> FunMemberAbstract
  1360. | _ when name = "_new" -> FunMemberAbstract
  1361. | _ -> FunStatic)
  1362. | _ ->
  1363. if constr then FunConstructor else if stat then FunStatic else FunMember
  1364. ) in
  1365. let e , fargs = type_function ctx args ret fmode fd p in
  1366. let f = {
  1367. tf_args = fargs;
  1368. tf_type = ret;
  1369. tf_expr = e;
  1370. } in
  1371. if stat && name = "__init__" then
  1372. (match e.eexpr with
  1373. | TBlock [] | TBlock [{ eexpr = TConst _ }] | TConst _ | TObjectDecl [] -> ()
  1374. | _ -> c.cl_init <- Some e);
  1375. if Meta.has Meta.DefineFeature cf.cf_meta then add_feature ctx.com (s_type_path c.cl_path ^ "." ^ cf.cf_name);
  1376. cf.cf_expr <- Some (mk (TFunction f) t p);
  1377. cf.cf_type <- t;
  1378. end;
  1379. t
  1380. ) "type_fun" in
  1381. if !do_bind then bind_type ctx cf r (match fd.f_expr with Some e -> snd e | None -> f.cff_pos) is_macro;
  1382. f, constr, cf
  1383. | FProp (get,set,t,eo) ->
  1384. let ret = (match t, eo with
  1385. | None, None -> error "Property must either define a type or a default value" p;
  1386. | None, _ -> mk_mono()
  1387. | Some t, _ -> load_complex_type ctx p t
  1388. ) in
  1389. let check_method m t req_name =
  1390. if ctx.com.display then () else
  1391. try
  1392. let _, t2, f = (if stat then let f = PMap.find m c.cl_statics in Some c, f.cf_type, f else class_field c m) in
  1393. (match f.cf_kind with
  1394. | Method MethMacro ->
  1395. display_error ctx "Macro methods cannot be used as property accessor" p;
  1396. display_error ctx "Accessor method is here" f.cf_pos;
  1397. | _ -> ());
  1398. unify_raise ctx t2 t f.cf_pos;
  1399. (match req_name with None -> () | Some n -> display_error ctx ("Please use " ^ n ^ " to name your property access method") f.cf_pos);
  1400. with
  1401. | Error (Unify l,p) -> raise (Error (Stack (Custom ("In method " ^ m ^ " required by property " ^ name),Unify l),p))
  1402. | Not_found ->
  1403. if req_name <> None then display_error ctx "Custom property accessor is no longer supported, please use get/set" p else
  1404. if not (c.cl_interface || c.cl_extern) then display_error ctx ("Method " ^ m ^ " required by property " ^ name ^ " is missing") p
  1405. in
  1406. let get = (match get with
  1407. | "null" -> AccNo
  1408. | "dynamic" -> AccCall ("get_" ^ name)
  1409. | "never" -> AccNever
  1410. | "default" -> AccNormal
  1411. | _ ->
  1412. let get = if get = "get" then "get_" ^ name else get in
  1413. delay ctx PForce (fun() -> check_method get (TFun ([],ret)) (if get <> "get" && get <> "get_" ^ name then Some ("get_" ^ name) else None));
  1414. AccCall get
  1415. ) in
  1416. let set = (match set with
  1417. | "null" ->
  1418. (* standard flash library read-only variables can't be accessed for writing, even in subclasses *)
  1419. if c.cl_extern && (match c.cl_path with "flash" :: _ , _ -> true | _ -> false) && ctx.com.platform = Flash then
  1420. AccNever
  1421. else
  1422. AccNo
  1423. | "never" -> AccNever
  1424. | "dynamic" -> AccCall ("set_" ^ name)
  1425. | "default" -> AccNormal
  1426. | _ ->
  1427. let set = if set = "set" then "set_" ^ name else set in
  1428. delay ctx PForce (fun() -> check_method set (TFun (["",false,ret],ret)) (if set <> "set" && set <> "set_" ^ name then Some ("set_" ^ name) else None));
  1429. AccCall set
  1430. ) in
  1431. if set = AccNormal && (match get with AccCall _ -> true | _ -> false) then error "Unsupported property combination" p;
  1432. let cf = {
  1433. cf_name = name;
  1434. cf_doc = f.cff_doc;
  1435. cf_meta = f.cff_meta;
  1436. cf_pos = f.cff_pos;
  1437. cf_kind = Var { v_read = get; v_write = set };
  1438. cf_expr = None;
  1439. cf_type = ret;
  1440. cf_public = is_public f.cff_access None;
  1441. cf_params = [];
  1442. cf_overloads = [];
  1443. } in
  1444. ctx.curfield <- cf;
  1445. bind_var ctx cf eo stat inline;
  1446. f, false, cf
  1447. in
  1448. let rec check_require = function
  1449. | [] -> None
  1450. | (Meta.Require,conds,_) :: l ->
  1451. let rec loop = function
  1452. | [] -> check_require l
  1453. | [EConst (String _),_] -> check_require l
  1454. | (EConst (Ident i),_) :: l ->
  1455. if not (Common.raw_defined ctx.com i) then
  1456. Some (i,(match List.rev l with (EConst (String msg),_) :: _ -> Some msg | _ -> None))
  1457. else
  1458. loop l
  1459. | _ -> error "Invalid require identifier" p
  1460. in
  1461. loop conds
  1462. | _ :: l ->
  1463. check_require l
  1464. in
  1465. let cl_req = check_require c.cl_meta in
  1466. List.iter (fun f ->
  1467. try
  1468. let p = f.cff_pos in
  1469. let fd , constr, f = loop_cf f in
  1470. let is_static = List.mem AStatic fd.cff_access in
  1471. if (is_static || constr) && c.cl_interface && f.cf_name <> "__init__" then error "You can't declare static fields in interfaces" p;
  1472. let req = check_require fd.cff_meta in
  1473. let req = (match req with None -> if is_static || constr then cl_req else None | _ -> req) in
  1474. (match req with
  1475. | None -> ()
  1476. | Some r -> f.cf_kind <- Var { v_read = AccRequire (fst r, snd r); v_write = AccRequire (fst r, snd r) });
  1477. if constr then begin
  1478. if c.cl_constructor <> None then error "Duplicate constructor" p;
  1479. c.cl_constructor <- Some f;
  1480. end else if not is_static || f.cf_name <> "__init__" then begin
  1481. if PMap.mem f.cf_name (if is_static then c.cl_statics else c.cl_fields) then
  1482. display_error ctx ("Duplicate class field declaration : " ^ f.cf_name) p
  1483. else
  1484. let dup = if is_static then PMap.exists f.cf_name c.cl_fields || has_field f.cf_name c.cl_super else PMap.exists f.cf_name c.cl_statics in
  1485. if dup then error ("Same field name can't be use for both static and instance : " ^ f.cf_name) p;
  1486. if is_static then begin
  1487. c.cl_statics <- PMap.add f.cf_name f c.cl_statics;
  1488. c.cl_ordered_statics <- f :: c.cl_ordered_statics;
  1489. end else begin
  1490. c.cl_fields <- PMap.add f.cf_name f c.cl_fields;
  1491. c.cl_ordered_fields <- f :: c.cl_ordered_fields;
  1492. if List.mem AOverride fd.cff_access then c.cl_overrides <- f.cf_name :: c.cl_overrides;
  1493. end;
  1494. end
  1495. with Error (Custom str,p) ->
  1496. display_error ctx str p
  1497. ) fields;
  1498. (match c.cl_kind with
  1499. | KAbstractImpl a ->
  1500. a.a_to <- List.rev a.a_to;
  1501. a.a_from <- List.rev a.a_from;
  1502. a.a_ops <- List.rev a.a_ops;
  1503. | _ -> ());
  1504. c.cl_ordered_statics <- List.rev c.cl_ordered_statics;
  1505. c.cl_ordered_fields <- List.rev c.cl_ordered_fields;
  1506. (*
  1507. make sure a default contructor with same access as super one will be added to the class structure at some point.
  1508. *)
  1509. let rec add_constructor c =
  1510. match c.cl_constructor, c.cl_super with
  1511. | None, Some ({ cl_constructor = Some cfsup } as csup,cparams) when not c.cl_extern ->
  1512. let cf = {
  1513. cfsup with
  1514. cf_pos = p;
  1515. cf_meta = [];
  1516. cf_doc = None;
  1517. cf_expr = None;
  1518. } in
  1519. let r = exc_protect ctx (fun r ->
  1520. let t = mk_mono() in
  1521. r := (fun() -> t);
  1522. let ctx = { ctx with
  1523. curfield = cf;
  1524. pass = PTypeField;
  1525. } in
  1526. ignore (follow cfsup.cf_type); (* make sure it's typed *)
  1527. let args = (match cfsup.cf_expr with
  1528. | Some { eexpr = TFunction f } ->
  1529. List.map (fun (v,def) ->
  1530. (*
  1531. let's optimize a bit the output by not always copying the default value
  1532. into the inherited constructor when it's not necessary for the platform
  1533. *)
  1534. match ctx.com.platform, def with
  1535. | _, Some _ when not ctx.com.config.pf_static -> v, (Some TNull)
  1536. | Flash, Some (TString _) -> v, (Some TNull)
  1537. | Cpp, Some (TString _) -> v, def
  1538. | Cpp, Some _ -> { v with v_type = ctx.t.tnull v.v_type }, (Some TNull)
  1539. | _ -> v, def
  1540. ) f.tf_args
  1541. | _ ->
  1542. match follow cfsup.cf_type with
  1543. | TFun (args,_) -> List.map (fun (n,o,t) -> alloc_var n (if o then ctx.t.tnull t else t), if o then Some TNull else None) args
  1544. | _ -> assert false
  1545. ) in
  1546. let p = c.cl_pos in
  1547. let vars = List.map (fun (v,def) -> alloc_var v.v_name (apply_params csup.cl_types cparams v.v_type), def) args in
  1548. 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
  1549. let constr = mk (TFunction {
  1550. tf_args = vars;
  1551. tf_type = ctx.t.tvoid;
  1552. tf_expr = super_call;
  1553. }) (TFun (List.map (fun (v,c) -> v.v_name, c <> None, v.v_type) vars,ctx.t.tvoid)) p in
  1554. cf.cf_expr <- Some constr;
  1555. cf.cf_type <- t;
  1556. unify ctx t constr.etype p;
  1557. t
  1558. ) "add_constructor" in
  1559. cf.cf_type <- TLazy r;
  1560. c.cl_constructor <- Some cf;
  1561. delay ctx PForce (fun() -> ignore((!r)()));
  1562. | _ ->
  1563. (* nothing to do *)
  1564. ()
  1565. in
  1566. add_constructor c;
  1567. (* push delays in reverse order so they will be run in correct order *)
  1568. List.iter (fun (ctx,r) ->
  1569. ctx.pass <- PTypeField;
  1570. delay ctx PTypeField (fun() -> ignore((!r)()))
  1571. ) !delayed_expr
  1572. let resolve_typedef t =
  1573. match t with
  1574. | TClassDecl _ | TEnumDecl _ | TAbstractDecl _ -> t
  1575. | TTypeDecl td ->
  1576. match follow td.t_type with
  1577. | TEnum (e,_) -> TEnumDecl e
  1578. | TInst (c,_) -> TClassDecl c
  1579. | TAbstract (a,_) -> TAbstractDecl a
  1580. | _ -> t
  1581. let add_module ctx m p =
  1582. let decl_type t =
  1583. let t = t_infos t in
  1584. try
  1585. let m2 = Hashtbl.find ctx.g.types_module t.mt_path in
  1586. 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;
  1587. error ("Type name " ^ s_type_path t.mt_path ^ " is redefined from module " ^ s_type_path m2) p
  1588. with
  1589. Not_found ->
  1590. Hashtbl.add ctx.g.types_module t.mt_path m.m_path
  1591. in
  1592. List.iter decl_type m.m_types;
  1593. Hashtbl.add ctx.g.modules m.m_path m
  1594. (*
  1595. In this pass, we can access load and access other modules types, but we cannot follow them or access their structure
  1596. since they have not been setup. We also build a context_init list that will be evaluated the first time we evaluate
  1597. an expression into the context
  1598. *)
  1599. let rec init_module_type ctx context_init do_init (decl,p) =
  1600. let get_type name =
  1601. try List.find (fun t -> snd (t_infos t).mt_path = name) ctx.m.curmod.m_types with Not_found -> assert false
  1602. in
  1603. match decl with
  1604. | EImport (path,mode) ->
  1605. let rec loop acc = function
  1606. | x :: l when is_lower_ident (fst x) -> loop (x::acc) l
  1607. | rest -> List.rev acc, rest
  1608. in
  1609. let pack, rest = loop [] path in
  1610. (match rest with
  1611. | [] ->
  1612. (match mode with
  1613. | IAll ->
  1614. ctx.m.wildcard_packages <- List.map fst pack :: ctx.m.wildcard_packages
  1615. | _ ->
  1616. (match List.rev path with
  1617. | [] -> assert false
  1618. | (_,p) :: _ -> error "Module name must start with an uppercase letter" p))
  1619. | (tname,p2) :: rest ->
  1620. let p1 = (match pack with [] -> p2 | (_,p1) :: _ -> p1) in
  1621. let p = punion p1 p2 in
  1622. let md = ctx.g.do_load_module ctx (List.map fst pack,tname) p in
  1623. let types = md.m_types in
  1624. let no_private t = not (t_infos t).mt_private in
  1625. let chk_private t p = if (t_infos t).mt_private then error "You can't import a private type" p in
  1626. let has_name name t = snd (t_infos t).mt_path = name in
  1627. let get_type tname =
  1628. let t = (try List.find (has_name tname) types with Not_found -> error (string_error tname (List.map (fun mt -> snd (t_infos mt).mt_path) types) ("Module " ^ s_type_path md.m_path ^ " does not define type " ^ tname)) p) in
  1629. chk_private t p;
  1630. t
  1631. in
  1632. let rebind t name =
  1633. let _, _, f = ctx.g.do_build_instance ctx t p in
  1634. (* create a temp private typedef, does not register it in module *)
  1635. TTypeDecl {
  1636. t_path = (fst md.m_path @ ["_" ^ snd md.m_path],name);
  1637. t_module = md;
  1638. t_pos = p;
  1639. t_private = true;
  1640. t_doc = None;
  1641. t_meta = [];
  1642. t_types = (t_infos t).mt_types;
  1643. t_type = f (List.map snd (t_infos t).mt_types);
  1644. }
  1645. in
  1646. let add_static_init t name s =
  1647. let name = (match name with None -> s | Some n -> n) in
  1648. match resolve_typedef t with
  1649. | TClassDecl c ->
  1650. c.cl_build();
  1651. ignore(PMap.find s c.cl_statics);
  1652. ctx.m.module_globals <- PMap.add name (TClassDecl c,s) ctx.m.module_globals
  1653. | TEnumDecl e ->
  1654. ignore(PMap.find s e.e_constrs);
  1655. ctx.m.module_globals <- PMap.add name (TEnumDecl e,s) ctx.m.module_globals
  1656. | _ ->
  1657. raise Not_found
  1658. in
  1659. (match mode with
  1660. | INormal | IAsName _ ->
  1661. let name = (match mode with IAsName n -> Some n | _ -> None) in
  1662. (match rest with
  1663. | [] ->
  1664. (match name with
  1665. | None ->
  1666. ctx.m.module_types <- List.filter no_private types @ ctx.m.module_types
  1667. | Some newname ->
  1668. ctx.m.module_types <- rebind (get_type tname) newname :: ctx.m.module_types);
  1669. | [tsub,p2] ->
  1670. let p = punion p1 p2 in
  1671. (try
  1672. let tsub = List.find (has_name tsub) types in
  1673. chk_private tsub p;
  1674. ctx.m.module_types <- (match name with None -> tsub | Some n -> rebind tsub n) :: ctx.m.module_types
  1675. with Not_found ->
  1676. (* this might be a static property, wait later to check *)
  1677. let tmain = get_type tname in
  1678. context_init := (fun() ->
  1679. try
  1680. add_static_init tmain name tsub
  1681. with Not_found ->
  1682. error (s_type_path (t_infos tmain).mt_path ^ " has no field or subtype " ^ tsub) p
  1683. ) :: !context_init)
  1684. | (tsub,p2) :: (fname,p3) :: rest ->
  1685. (match rest with
  1686. | [] -> ()
  1687. | (n,p) :: _ -> error ("Unexpected " ^ n) p);
  1688. let tsub = get_type tsub in
  1689. context_init := (fun() ->
  1690. try
  1691. add_static_init tsub name fname
  1692. with Not_found ->
  1693. error (s_type_path (t_infos tsub).mt_path ^ " has no field " ^ fname) (punion p p3)
  1694. ) :: !context_init;
  1695. )
  1696. | IAll ->
  1697. let t = (match rest with
  1698. | [] -> get_type tname
  1699. | [tsub,_] -> get_type tsub
  1700. | _ :: (n,p) :: _ -> error ("Unexpected " ^ n) p
  1701. ) in
  1702. context_init := (fun() ->
  1703. match resolve_typedef t with
  1704. | TClassDecl c ->
  1705. c.cl_build();
  1706. PMap.iter (fun _ cf -> ctx.m.module_globals <- PMap.add cf.cf_name (TClassDecl c,cf.cf_name) ctx.m.module_globals) c.cl_statics
  1707. | TEnumDecl e ->
  1708. PMap.iter (fun _ c -> ctx.m.module_globals <- PMap.add c.ef_name (TEnumDecl e,c.ef_name) ctx.m.module_globals) e.e_constrs
  1709. | _ ->
  1710. error "No statics to import from this type" p
  1711. ) :: !context_init
  1712. ))
  1713. | EUsing t ->
  1714. (* do the import first *)
  1715. let types = (match t.tsub with
  1716. | None ->
  1717. let md = ctx.g.do_load_module ctx (t.tpackage,t.tname) p in
  1718. let types = List.filter (fun t -> not (t_infos t).mt_private) md.m_types in
  1719. ctx.m.module_types <- types @ ctx.m.module_types;
  1720. types
  1721. | Some _ ->
  1722. let t = load_type_def ctx p t in
  1723. ctx.m.module_types <- t :: ctx.m.module_types;
  1724. [t]
  1725. ) in
  1726. (* delay the using since we need to resolve typedefs *)
  1727. let filter_classes types =
  1728. let rec loop acc types = match types with
  1729. | td :: l ->
  1730. (match resolve_typedef td with
  1731. | TClassDecl c ->
  1732. loop (c :: acc) l
  1733. | td ->
  1734. loop acc l)
  1735. | [] ->
  1736. acc
  1737. in
  1738. loop [] types
  1739. in
  1740. context_init := (fun() -> ctx.m.module_using <- filter_classes types @ ctx.m.module_using) :: !context_init
  1741. | EClass d ->
  1742. let c = (match get_type d.d_name with TClassDecl c -> c | _ -> assert false) in
  1743. let herits = d.d_flags in
  1744. if Meta.has Meta.Generic c.cl_meta && c.cl_types <> [] then c.cl_kind <- KGeneric;
  1745. if c.cl_path = (["haxe";"macro"],"MacroType") then c.cl_kind <- KMacroType;
  1746. c.cl_extern <- List.mem HExtern herits;
  1747. c.cl_interface <- List.mem HInterface herits;
  1748. let build() =
  1749. c.cl_build <- (fun()->());
  1750. set_heritance ctx c herits p;
  1751. init_class ctx c p do_init d.d_flags d.d_data
  1752. in
  1753. ctx.pass <- PBuildClass;
  1754. ctx.curclass <- c;
  1755. c.cl_build <- make_pass ctx build;
  1756. ctx.pass <- PBuildModule;
  1757. ctx.curclass <- null_class;
  1758. delay ctx PBuildClass (fun() -> c.cl_build());
  1759. | EEnum d ->
  1760. let e = (match get_type d.d_name with TEnumDecl e -> e | _ -> assert false) in
  1761. let ctx = { ctx with type_params = e.e_types } in
  1762. let h = (try Some (Hashtbl.find ctx.g.type_patches e.e_path) with Not_found -> None) in
  1763. (match h with
  1764. | None -> ()
  1765. | Some (h,hcl) ->
  1766. Hashtbl.iter (fun _ _ -> error "Field type patch not supported for enums" e.e_pos) h;
  1767. e.e_meta <- e.e_meta @ hcl.tp_meta);
  1768. let constructs = ref d.d_data in
  1769. let get_constructs() =
  1770. List.map (fun c ->
  1771. {
  1772. cff_name = c.ec_name;
  1773. cff_doc = c.ec_doc;
  1774. cff_meta = c.ec_meta;
  1775. cff_pos = c.ec_pos;
  1776. cff_access = [];
  1777. cff_kind = (match c.ec_args, c.ec_params with
  1778. | [], [] -> FVar (c.ec_type,None)
  1779. | _ -> FFun { f_params = c.ec_params; f_type = c.ec_type; f_expr = None; f_args = List.map (fun (n,o,t) -> n,o,Some t,None) c.ec_args });
  1780. }
  1781. ) (!constructs)
  1782. in
  1783. let init () = List.iter (fun f -> f()) !context_init in
  1784. build_module_def ctx (TEnumDecl e) e.e_meta get_constructs init (fun (e,p) ->
  1785. match e with
  1786. | EVars [_,Some (CTAnonymous fields),None] ->
  1787. constructs := List.map (fun f ->
  1788. let args, params, t = (match f.cff_kind with
  1789. | FVar (t,None) -> [], [], t
  1790. | FFun { f_params = pl; f_type = t; f_expr = (None|Some (EBlock [],_)); f_args = al } ->
  1791. let al = List.map (fun (n,o,t,_) -> match t with None -> error "Missing function parameter type" f.cff_pos | Some t -> n,o,t) al in
  1792. al, pl, t
  1793. | _ ->
  1794. error "Invalid enum constructor in @:build result" p
  1795. ) in
  1796. {
  1797. ec_name = f.cff_name;
  1798. ec_doc = f.cff_doc;
  1799. ec_meta = f.cff_meta;
  1800. ec_pos = f.cff_pos;
  1801. ec_args = args;
  1802. ec_params = params;
  1803. ec_type = t;
  1804. }
  1805. ) fields
  1806. | _ -> error "Enum build macro must return a single variable with anonymous object fields" p
  1807. );
  1808. let et = TEnum (e,List.map snd e.e_types) in
  1809. let names = ref [] in
  1810. let index = ref 0 in
  1811. List.iter (fun c ->
  1812. let p = c.ec_pos in
  1813. let params = ref [] in
  1814. params := List.map (fun tp -> type_type_params ctx ([],c.ec_name) (fun() -> !params) c.ec_pos tp) c.ec_params;
  1815. let params = !params in
  1816. let ctx = { ctx with type_params = params @ ctx.type_params } in
  1817. let rt = (match c.ec_type with
  1818. | None -> et
  1819. | Some t ->
  1820. let t = load_complex_type ctx p t in
  1821. (match follow t with
  1822. | TEnum (te,_) when te == e ->
  1823. ()
  1824. | _ ->
  1825. error "Explicit enum type must be of the same enum type" p);
  1826. t
  1827. ) in
  1828. let t = (match c.ec_args with
  1829. | [] -> rt
  1830. | l ->
  1831. let pnames = ref PMap.empty in
  1832. TFun (List.map (fun (s,opt,t) ->
  1833. (match t with CTPath({tpackage=[];tname="Void"}) -> error "Arguments of type Void are not allowed in enum constructors" c.ec_pos | _ -> ());
  1834. if PMap.mem s (!pnames) then error ("Duplicate parameter '" ^ s ^ "' in enum constructor " ^ c.ec_name) p;
  1835. pnames := PMap.add s () (!pnames);
  1836. s, opt, load_type_opt ~opt ctx p (Some t)
  1837. ) l, rt)
  1838. ) in
  1839. if PMap.mem c.ec_name e.e_constrs then error ("Duplicate constructor " ^ c.ec_name) p;
  1840. e.e_constrs <- PMap.add c.ec_name {
  1841. ef_name = c.ec_name;
  1842. ef_type = t;
  1843. ef_pos = p;
  1844. ef_doc = c.ec_doc;
  1845. ef_index = !index;
  1846. ef_params = params;
  1847. ef_meta = c.ec_meta;
  1848. } e.e_constrs;
  1849. incr index;
  1850. names := c.ec_name :: !names;
  1851. ) (!constructs);
  1852. e.e_names <- List.rev !names;
  1853. e.e_extern <- e.e_extern
  1854. | ETypedef d ->
  1855. let t = (match get_type d.d_name with TTypeDecl t -> t | _ -> assert false) in
  1856. let ctx = { ctx with type_params = t.t_types } in
  1857. let tt = load_complex_type ctx p d.d_data in
  1858. (*
  1859. we exceptionnaly allow follow here because we don't care the type we get as long as it's not our own
  1860. *)
  1861. if t.t_type == follow tt then error "Recursive typedef is not allowed" p;
  1862. (match t.t_type with
  1863. | TMono r ->
  1864. (match !r with
  1865. | None -> r := Some tt;
  1866. | Some _ -> assert false);
  1867. | _ -> assert false);
  1868. | EAbstract d ->
  1869. let a = (match get_type d.d_name with TAbstractDecl a -> a | _ -> assert false) in
  1870. let ctx = { ctx with type_params = a.a_types } in
  1871. let is_type = ref false in
  1872. let load_type t =
  1873. let t = load_complex_type ctx p t in
  1874. if not (Meta.has Meta.CoreType a.a_meta) then begin
  1875. if !is_type then begin
  1876. delay ctx PFinal (fun () ->
  1877. (try type_eq EqStrict a.a_this t with Unify_error _ -> error "You can only declare from/to with your underlying type" p)
  1878. );
  1879. end else
  1880. error "Missing underlying type declaration or @:coreType declaration" p;
  1881. end;
  1882. t
  1883. in
  1884. List.iter (function
  1885. | AFromType t -> a.a_from <- (load_type t, None) :: a.a_from
  1886. | AToType t -> a.a_to <- (load_type t, None) :: a.a_to
  1887. | AIsType t ->
  1888. if a.a_impl = None then error "Abstracts with underlying type must have an implementation" a.a_pos;
  1889. let at = load_complex_type ctx p t in
  1890. (match at with TAbstract(a2,_) when a == a2 -> error "Abstract underlying type cannot be recursive" a.a_pos | _ -> ());
  1891. a.a_this <- at;
  1892. is_type := true;
  1893. | APrivAbstract -> ()
  1894. ) d.d_flags;
  1895. if not !is_type && (match a.a_impl with Some _ -> true | None -> not (Meta.has Meta.CoreType a.a_meta)) then
  1896. error "Abstract is missing underlying type declaration" a.a_pos
  1897. let type_module ctx m file tdecls p =
  1898. let m, decls, tdecls = make_module ctx m file tdecls p in
  1899. add_module ctx m p;
  1900. (* define the per-module context for the next pass *)
  1901. let ctx = {
  1902. com = ctx.com;
  1903. g = ctx.g;
  1904. t = ctx.t;
  1905. m = {
  1906. curmod = m;
  1907. module_types = ctx.g.std.m_types;
  1908. module_using = [];
  1909. module_globals = PMap.empty;
  1910. wildcard_packages = [];
  1911. };
  1912. meta = [];
  1913. pass = PBuildModule;
  1914. on_error = (fun ctx msg p -> ctx.com.error msg p);
  1915. macro_depth = ctx.macro_depth;
  1916. curclass = null_class;
  1917. curfield = null_field;
  1918. tthis = ctx.tthis;
  1919. ret = ctx.ret;
  1920. locals = PMap.empty;
  1921. type_params = [];
  1922. curfun = FunStatic;
  1923. untyped = false;
  1924. in_super_call = false;
  1925. in_macro = ctx.in_macro;
  1926. in_display = false;
  1927. in_loop = false;
  1928. opened = [];
  1929. vthis = None;
  1930. } in
  1931. (* here is an additional PASS 1 phase, which define the type parameters for all module types.
  1932. Constraints are handled lazily (no other type is loaded) because they might be recursive anyway *)
  1933. List.iter (fun d ->
  1934. match d with
  1935. | (TClassDecl c, (EClass d, p)) ->
  1936. c.cl_types <- List.map (type_type_params ctx c.cl_path (fun() -> c.cl_types) p) d.d_params;
  1937. | (TEnumDecl e, (EEnum d, p)) ->
  1938. e.e_types <- List.map (type_type_params ctx e.e_path (fun() -> e.e_types) p) d.d_params;
  1939. | (TTypeDecl t, (ETypedef d, p)) ->
  1940. t.t_types <- List.map (type_type_params ctx t.t_path (fun() -> t.t_types) p) d.d_params;
  1941. | (TAbstractDecl a, (EAbstract d, p)) ->
  1942. a.a_types <- List.map (type_type_params ctx a.a_path (fun() -> a.a_types) p) d.d_params;
  1943. | _ ->
  1944. assert false
  1945. ) decls;
  1946. (* setup module types *)
  1947. let context_init = ref [] in
  1948. let do_init() =
  1949. match !context_init with
  1950. | [] -> ()
  1951. | l -> context_init := []; List.iter (fun f -> f()) (List.rev l)
  1952. in
  1953. List.iter (init_module_type ctx context_init do_init) tdecls;
  1954. m
  1955. let resolve_module_file com m remap p =
  1956. let forbid = ref false in
  1957. let file = (match m with
  1958. | [] , name -> name
  1959. | x :: l , name ->
  1960. let x = (try
  1961. match PMap.find x com.package_rules with
  1962. | Forbidden -> forbid := true; x
  1963. | Directory d -> d
  1964. | Remap d -> remap := d :: l; d
  1965. with Not_found -> x
  1966. ) in
  1967. String.concat "/" (x :: l) ^ "/" ^ name
  1968. ) ^ ".hx" in
  1969. let file = Common.find_file com file in
  1970. let file = (match String.lowercase (snd m) with
  1971. | "con" | "aux" | "prn" | "nul" | "com1" | "com2" | "com3" | "lpt1" | "lpt2" | "lpt3" when Sys.os_type = "Win32" ->
  1972. (* these names are reserved by the OS - old DOS legacy, such files cannot be easily created but are reported as visible *)
  1973. if (try (Unix.stat file).Unix.st_size with _ -> 0) > 0 then file else raise Not_found
  1974. | _ -> file
  1975. ) in
  1976. if !forbid then begin
  1977. let _, decls = (!parse_hook) com file p in
  1978. let meta = (match decls with
  1979. | (EClass d,_) :: _ -> d.d_meta
  1980. | (EEnum d,_) :: _ -> d.d_meta
  1981. | (EAbstract d,_) :: _ -> d.d_meta
  1982. | (ETypedef d,_) :: _ -> d.d_meta
  1983. | _ -> []
  1984. ) in
  1985. if not (Meta.has Meta.NoPackageRestrict meta) then begin
  1986. let x = (match fst m with [] -> assert false | x :: _ -> x) in
  1987. raise (Forbid_package ((x,m,p),[],if Common.defined com Define.Macro then "macro" else platform_name com.platform));
  1988. end;
  1989. end;
  1990. file
  1991. let parse_module ctx m p =
  1992. let remap = ref (fst m) in
  1993. let file = resolve_module_file ctx.com m remap p in
  1994. let pack, decls = (!parse_hook) ctx.com file p in
  1995. if pack <> !remap then begin
  1996. let spack m = if m = [] then "<empty>" else String.concat "." m in
  1997. if p == Ast.null_pos then
  1998. display_error ctx ("Invalid commandline class : " ^ s_type_path m ^ " should be " ^ s_type_path (pack,snd m)) p
  1999. else
  2000. display_error ctx ("Invalid package : " ^ spack (fst m) ^ " should be " ^ spack pack) p
  2001. end;
  2002. file, if !remap <> fst m then
  2003. (* build typedefs to redirect to real package *)
  2004. List.rev (List.fold_left (fun acc (t,p) ->
  2005. let build f d =
  2006. let priv = List.mem f d.d_flags in
  2007. (ETypedef {
  2008. d_name = d.d_name;
  2009. d_doc = None;
  2010. d_meta = [];
  2011. d_params = d.d_params;
  2012. d_flags = if priv then [EPrivate] else [];
  2013. d_data = CTPath (if priv then { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None; } else
  2014. {
  2015. tpackage = !remap;
  2016. tname = d.d_name;
  2017. tparams = List.map (fun tp ->
  2018. TPType (CTPath { tpackage = []; tname = tp.tp_name; tparams = []; tsub = None; })
  2019. ) d.d_params;
  2020. tsub = None;
  2021. });
  2022. },p) :: acc
  2023. in
  2024. match t with
  2025. | EClass d -> build HPrivate d
  2026. | EEnum d -> build EPrivate d
  2027. | ETypedef d -> build EPrivate d
  2028. | EAbstract d -> build APrivAbstract d
  2029. | EImport _ | EUsing _ -> acc
  2030. ) [(EImport (List.map (fun s -> s,null_pos) (!remap @ [snd m]),INormal),null_pos)] decls)
  2031. else
  2032. decls
  2033. let load_module ctx m p =
  2034. let m2 = (try
  2035. Hashtbl.find ctx.g.modules m
  2036. with
  2037. Not_found ->
  2038. match !type_module_hook ctx m p with
  2039. | Some m -> m
  2040. | None ->
  2041. let file, decls = (try
  2042. parse_module ctx m p
  2043. with Not_found ->
  2044. let rec loop = function
  2045. | [] ->
  2046. raise (Error (Module_not_found m,p))
  2047. | load :: l ->
  2048. match load m p with
  2049. | None -> loop l
  2050. | Some (file,(_,a)) -> file, a
  2051. in
  2052. loop ctx.com.load_extern_type
  2053. ) in
  2054. try
  2055. type_module ctx m file decls p
  2056. with Forbid_package (inf,pl,pf) when p <> Ast.null_pos ->
  2057. raise (Forbid_package (inf,p::pl,pf))
  2058. ) in
  2059. add_dependency ctx.m.curmod m2;
  2060. if ctx.pass = PTypeField then flush_pass ctx PBuildClass "load_module";
  2061. m2
  2062. ;;
  2063. type_function_params_rec := type_function_params