typeload.ml 91 KB

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