typeload.ml 111 KB

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