typeload.ml 73 KB

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