typeload.ml 123 KB

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