typeload.ml 98 KB

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