typeload.ml 92 KB

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