typeload.ml 76 KB

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