typeload.ml 74 KB

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