typeload.ml 95 KB

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