typeload.ml 76 KB

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