typeload.ml 123 KB

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