typeload.ml 106 KB

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