typeload.ml 73 KB

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