typeload.ml 91 KB

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