typeload.ml 94 KB

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