typeload.ml 75 KB

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