typeload.ml 125 KB

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