typeload.ml 76 KB

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